package com.bgy.autosale; import android.annotation.SuppressLint; import android.app.ActivityManager; import android.app.Application; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.Typeface; import android.os.Bundle; import android.util.Log; import com.bgy.autosale.entitys.ContactBean; import com.bgy.autosale.entitys.Result; import com.bgy.autosale.helpers.CommunicationHelper; import com.bgy.autosale.helpers.ErrorViewHelper; import com.bgy.autosale.helpers.SmallApkHelper; import com.bgy.autosale.interfaces.Callback; import com.bgy.autosale.payutil.MQService; import com.bgy.autosale.plc.BasePlcAgreement; import com.bgy.autosale.ui.operator.fragments.PriceFragment; import com.bgy.autosale.utils.CrashHandler; import com.example.offpay.service.ModuleOffPayInitializer; import com.example.offpay.wmdb.WeaverUtil; import com.example.spunsugar.system.model.bean.PriceBean; import com.example.spunsugar.user.model.bean.GoodsBean; import com.hboxs.base_library.base.BaseApplication; import com.hboxs.base_library.base.ModuleInitializer; import com.hboxs.base_library.constant.Global; import com.hboxs.base_library.constant.Heartbeat; import com.hboxs.base_library.constant.Name; import com.hboxs.base_library.constant.ProductAbout; import com.hboxs.base_library.event.ApiMessageEvent; import com.hboxs.base_library.event.ErrorApiMessageEvent; import com.hboxs.base_library.util.FileUtil; import com.hboxs.base_library.util.HotWifitUtil; import com.hboxs.base_library.util.LogUtil; import com.hboxs.base_library.util.SharedPreferencesUtils; import com.orhanobut.hawk.Hawk; import org.greenrobot.eventbus.EventBus; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Locale; import ZtlApi.ZtlManager; /** * create by cjx on 2023/11/14 */ public class App extends BaseApplication { private static final String TAG = "AppBaseApplication"; public static App app; // 联系方式加载完成通知 public static final String ACTION_CONTACT_LOAD = "ACTION_CONTACT_LOAD"; // 设备状态变正常通知 public static final String ACTION_DEVICE_NORMAL = "ACTION_DEVICE_NORMAL"; //标记连接状态,防止状态过多上报,界面刷新频繁 private boolean pclDeviceConnected = true; public String appId = "1429622061189443585";//使用pos的id 做为所有pos合并一个版本的更新id public String language; // 语言类型 public int mcuVersion = 0; public boolean unCheckStatus = false; // 异常信息界面显示的联系方式 public ContactBean contactBean; // 支付渠道 public int payChannel; // 运行模式 public int runMode; public String currencySymbol; public Locale locale = Locale.CHINA; public boolean specMultipleChoices = true; public boolean isDeviceStatus=false; @SuppressLint("SdCardPath") @Override public void onCreate() { super.onCreate(); app = this; Hawk.init(this).build(); //初始化工具库 boolean isMainProcess = getApplicationContext().getPackageName().equals (getCurrentProcessName()); Log.e("TAG", "start app is main process " + isMainProcess); if (isMainProcess) { //用于启动或者安装一个小应用守护当前的应用的 SmallApkHelper.initAndCheckSmallApk(this); CrashHandler.getsInstance().init(getApplicationContext()); try { Constant.versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; } catch (PackageManager.NameNotFoundException e) { throw new RuntimeException(e); } } new Thread(new Runnable() { @Override public void run() { initGoodsBean(); // 启动服务 ZtlManager.GetInstance().setContext(getApplicationContext());//屏幕厂家数据初始化 openService();//打开服务 initializeModules(); Heartbeat.deviceId = FileUtil.getDeviceId(); Log.d("openService", "run: "+Heartbeat.deviceId+":"+FileUtil.getDeviceId()); Log.d(TAG, "run: "); } }).start(); } private void initializeModules() { Log.d("TAG", "initializeModules1: "); List> modules = Arrays.asList( ModuleOffPayInitializer.class ); Log.d("TAG", "initializeModules2: "); for (Class initializerClass : modules) { try { ModuleInitializer initializer = initializerClass.newInstance(); initializer.initialize(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); } } } private void openMqService() { //开启MQ startService(new Intent(this, MQService.class)); } private void openService() { openHotWifi();//打开后台、前台服务 openRestService(); openMqService();//打开mq } private void openHotWifi() { if (Global.isVersions == 1 | Global.isVersions == 2) { boolean isAutoOpenHot = (boolean) SharedPreferencesUtils.getParam(Name.IS_AUTO_OPENHOT, false); if (!isAutoOpenHot) { Intent intent = new Intent(this, RemoteSupportService.class); Bundle bundle = new Bundle(); bundle.putInt("first", 4); intent.putExtras(bundle); startService(intent); } else { HotWifitUtil hotWifitUtil = new HotWifitUtil(); String name = hotWifitUtil.getName(); String password = hotWifitUtil.getPassword(); LogUtil.d(TAG, "openHotWifi: " + name + password); String hotWifi = "HotWifi"; Intent intent = new Intent(this, RemoteSupportService.class); intent.putExtra("HotWifi", hotWifi); intent.putExtra("ApName", "sunzee"); intent.putExtra("key", "66666666"); Bundle bundle = new Bundle(); bundle.putInt("first", 4); intent.putExtras(bundle); startService(intent); } } } private void openRestService() { Intent intent = new Intent(this, RemoteSupportService.class); startService(intent); } public void replaceDefaultFont(Context context, String fontPath) {//字体全局替换 try { Field field = Typeface.class.getDeclaredField("MONOSPACE"); field.setAccessible(true); field.set(null, Typeface.createFromAsset(context.getAssets(), fontPath)); } catch (Exception e) { e.printStackTrace(); } } /** * 获取当前进程名 */ private String getCurrentProcessName() { int pid = android.os.Process.myPid(); String processName = ""; ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService (Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) { if (process.pid == pid) { processName = process.processName; } } return processName; } //监听plc设备状态 public void initDeviceStateListener() { CommunicationHelper.getInstance().setDeviceStatusListener(new Callback>() { @Override public void onResult(Result result) { switch (result.code) { case BasePlcAgreement.PLC_HEART_CONNECT: if (!pclDeviceConnected) { pclDeviceConnected = true; isDeviceStatus=false; ErrorViewHelper.getInstance().hideErrorView(R.string.error_plc_disconnect); Log.d(TAG, "onResult: PLC_HEART_CONNECT"); } break; case BasePlcAgreement.PLC_HEART_DISCONNECT: if (pclDeviceConnected) { pclDeviceConnected = false; isDeviceStatus=false; ErrorViewHelper.getInstance().showErrorView(R.string.error_plc_disconnect, new String[]{getString(R.string.error_plc_disconnect), getString(R.string.make_fail_describe)}); Log.d(TAG, "onResult: PLC_HEART_CONNECT1"); } break; case BasePlcAgreement.PLC_HEART_REPORT: if (result.data != null) { isDeviceStatus=true; Log.d(TAG, "onResult:测试报警1 "+result.message); if (result.data) { ErrorViewHelper.getInstance().hideErrorView(R.string.setting_plc_connect); Log.d("上传报警", "onResult: 上传报警:清除了未进入自动售卖的问题"); autoMode.clear(); } else { if (result.message != null) { String[] lastErrorMessage = result.message.split("-"); Log.d(TAG, "onResult: "+result.message); String prefix = result.message.substring(0, result.message.indexOf('-')); Log.d(TAG, "onResult1: "+prefix); ErrorViewHelper.getInstance().showErrorView(R.string.setting_plc_connect, lastErrorMessage); if (autoMode.add(prefix)){ Log.d("上传报警", "onResult: 上传报警:"+prefix); EventBus.getDefault().post(new ErrorApiMessageEvent(Name.WARNING_ALARM, prefix,"1")); } } else { ErrorViewHelper.getInstance().showErrorView(R.string.setting_plc_connect, new String[]{getString(R.string.error_wait), getString(R.string.make_fail_describe_refund)}); EventBus.getDefault().post(new ErrorApiMessageEvent(Name.WARNING_ALARM, getString(R.string.error_wait),"1")); } } }else { Log.d(TAG, "onResult: 测试报警5"); autoMode.clear(); } break; } } }); } private static final HashSet autoMode = new HashSet<>(); public boolean isPlcConnected() { return pclDeviceConnected; } public void killActivities() { ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List appTaskList = activityManager.getAppTasks(); for (ActivityManager.AppTask appTask : appTaskList) { appTask.finishAndRemoveTask(); } System.exit(0); } public void forceLocale(Locale locale, Context context) { this.locale = locale; Configuration conf = context.getResources().getConfiguration(); conf.setLocale(locale); context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics()); Locale.setDefault(locale); } //初始化商品数据 private void initGoodsBean() { boolean contains = Hawk.contains("PRICEBEAN"); // //如果不存在 创建默认的 if (!contains) { ArrayList priceBeans = new ArrayList<>(); for (int i = 0; i < PriceFragment.ProductAbout.chineseName.length; i++) { PriceFragment.PriceBean priceBean = new PriceFragment.PriceBean(); priceBean.setChineseName(PriceFragment.ProductAbout.chineseName[i]); priceBean.setNameKey(PriceFragment.ProductAbout.nameKey[i]); priceBean.setPriceKey(PriceFragment.ProductAbout.priceKey[i]); priceBean.setDefaultName(getString(PriceFragment.ProductAbout.defaultName[i])); priceBean.setPriceDefault(PriceFragment.ProductAbout.priceDefault[i]); priceBeans.add(priceBean); Log.d(TAG, "initGoodsBean: " + priceBeans.get(i).toString()); } Hawk.put("PRICEBEAN", priceBeans); } } public String getVersionName() { //获取packagemanager的实例 PackageManager packageManager = getPackageManager(); //getPackageName()是你当前类的包名,0代表是获取版本信息 PackageInfo packInfo = null; try { packInfo = packageManager.getPackageInfo(getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return packInfo.versionName; } }