App.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package com.bgy.autosale;
  2. import android.annotation.SuppressLint;
  3. import android.app.ActivityManager;
  4. import android.app.Application;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.pm.PackageInfo;
  8. import android.content.pm.PackageManager;
  9. import android.content.res.Configuration;
  10. import android.graphics.Typeface;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import com.bgy.autosale.entitys.ContactBean;
  14. import com.bgy.autosale.entitys.Result;
  15. import com.bgy.autosale.helpers.CommunicationHelper;
  16. import com.bgy.autosale.helpers.ErrorViewHelper;
  17. import com.bgy.autosale.helpers.SmallApkHelper;
  18. import com.bgy.autosale.interfaces.Callback;
  19. import com.bgy.autosale.payutil.MQService;
  20. import com.bgy.autosale.plc.BasePlcAgreement;
  21. import com.bgy.autosale.ui.operator.fragments.PriceFragment;
  22. import com.bgy.autosale.utils.CrashHandler;
  23. import com.example.offpay.service.ModuleOffPayInitializer;
  24. import com.example.offpay.wmdb.WeaverUtil;
  25. import com.example.spunsugar.system.model.bean.PriceBean;
  26. import com.example.spunsugar.user.model.bean.GoodsBean;
  27. import com.hboxs.base_library.base.BaseApplication;
  28. import com.hboxs.base_library.base.ModuleInitializer;
  29. import com.hboxs.base_library.constant.Global;
  30. import com.hboxs.base_library.constant.Heartbeat;
  31. import com.hboxs.base_library.constant.Name;
  32. import com.hboxs.base_library.constant.ProductAbout;
  33. import com.hboxs.base_library.event.ApiMessageEvent;
  34. import com.hboxs.base_library.event.ErrorApiMessageEvent;
  35. import com.hboxs.base_library.util.FileUtil;
  36. import com.hboxs.base_library.util.HotWifitUtil;
  37. import com.hboxs.base_library.util.LogUtil;
  38. import com.hboxs.base_library.util.SharedPreferencesUtils;
  39. import com.orhanobut.hawk.Hawk;
  40. import org.greenrobot.eventbus.EventBus;
  41. import java.lang.reflect.Field;
  42. import java.util.ArrayList;
  43. import java.util.Arrays;
  44. import java.util.HashSet;
  45. import java.util.List;
  46. import java.util.Locale;
  47. import ZtlApi.ZtlManager;
  48. /**
  49. * create by cjx on 2023/11/14
  50. */
  51. public class App extends BaseApplication {
  52. private static final String TAG = "AppBaseApplication";
  53. public static App app;
  54. // 联系方式加载完成通知
  55. public static final String ACTION_CONTACT_LOAD = "ACTION_CONTACT_LOAD";
  56. // 设备状态变正常通知
  57. public static final String ACTION_DEVICE_NORMAL = "ACTION_DEVICE_NORMAL";
  58. //标记连接状态,防止状态过多上报,界面刷新频繁
  59. private boolean pclDeviceConnected = true;
  60. public String appId = "1429622061189443585";//使用pos的id 做为所有pos合并一个版本的更新id
  61. public String language; // 语言类型
  62. public int mcuVersion = 0;
  63. public boolean unCheckStatus = false;
  64. // 异常信息界面显示的联系方式
  65. public ContactBean contactBean;
  66. // 支付渠道
  67. public int payChannel;
  68. // 运行模式
  69. public int runMode;
  70. public String currencySymbol;
  71. public Locale locale = Locale.CHINA;
  72. public boolean specMultipleChoices = true;
  73. public boolean isDeviceStatus=false;
  74. @SuppressLint("SdCardPath")
  75. @Override
  76. public void onCreate() {
  77. super.onCreate();
  78. app = this;
  79. Hawk.init(this).build();
  80. //初始化工具库
  81. boolean isMainProcess = getApplicationContext().getPackageName().equals
  82. (getCurrentProcessName());
  83. Log.e("TAG", "start app is main process " + isMainProcess);
  84. if (isMainProcess) {
  85. //用于启动或者安装一个小应用守护当前的应用的
  86. SmallApkHelper.initAndCheckSmallApk(this);
  87. CrashHandler.getsInstance().init(getApplicationContext());
  88. try {
  89. Constant.versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
  90. } catch (PackageManager.NameNotFoundException e) {
  91. throw new RuntimeException(e);
  92. }
  93. }
  94. new Thread(new Runnable() {
  95. @Override
  96. public void run() {
  97. initGoodsBean();
  98. // 启动服务
  99. ZtlManager.GetInstance().setContext(getApplicationContext());//屏幕厂家数据初始化
  100. openService();//打开服务
  101. initializeModules();
  102. Heartbeat.deviceId = FileUtil.getDeviceId();
  103. Log.d("openService", "run: "+Heartbeat.deviceId+":"+FileUtil.getDeviceId());
  104. Log.d(TAG, "run: ");
  105. }
  106. }).start();
  107. }
  108. private void initializeModules() {
  109. Log.d("TAG", "initializeModules1: ");
  110. List<Class<? extends ModuleInitializer>> modules = Arrays.asList(
  111. ModuleOffPayInitializer.class
  112. );
  113. Log.d("TAG", "initializeModules2: ");
  114. for (Class<? extends ModuleInitializer> initializerClass : modules) {
  115. try {
  116. ModuleInitializer initializer = initializerClass.newInstance();
  117. initializer.initialize();
  118. } catch (InstantiationException | IllegalAccessException e) {
  119. e.printStackTrace();
  120. }
  121. }
  122. }
  123. private void openMqService() {
  124. //开启MQ
  125. startService(new Intent(this, MQService.class));
  126. }
  127. private void openService() {
  128. openHotWifi();//打开后台、前台服务
  129. openRestService();
  130. openMqService();//打开mq
  131. }
  132. private void openHotWifi() {
  133. if (Global.isVersions == 1 | Global.isVersions == 2) {
  134. boolean isAutoOpenHot = (boolean) SharedPreferencesUtils.getParam(Name.IS_AUTO_OPENHOT, false);
  135. if (!isAutoOpenHot) {
  136. Intent intent = new Intent(this, RemoteSupportService.class);
  137. Bundle bundle = new Bundle();
  138. bundle.putInt("first", 4);
  139. intent.putExtras(bundle);
  140. startService(intent);
  141. } else {
  142. HotWifitUtil hotWifitUtil = new HotWifitUtil();
  143. String name = hotWifitUtil.getName();
  144. String password = hotWifitUtil.getPassword();
  145. LogUtil.d(TAG, "openHotWifi: " + name + password);
  146. String hotWifi = "HotWifi";
  147. Intent intent = new Intent(this, RemoteSupportService.class);
  148. intent.putExtra("HotWifi", hotWifi);
  149. intent.putExtra("ApName", "sunzee");
  150. intent.putExtra("key", "66666666");
  151. Bundle bundle = new Bundle();
  152. bundle.putInt("first", 4);
  153. intent.putExtras(bundle);
  154. startService(intent);
  155. }
  156. }
  157. }
  158. private void openRestService() {
  159. Intent intent = new Intent(this, RemoteSupportService.class);
  160. startService(intent);
  161. }
  162. public void replaceDefaultFont(Context context, String fontPath) {//字体全局替换
  163. try {
  164. Field field = Typeface.class.getDeclaredField("MONOSPACE");
  165. field.setAccessible(true);
  166. field.set(null, Typeface.createFromAsset(context.getAssets(), fontPath));
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. }
  171. /**
  172. * 获取当前进程名
  173. */
  174. private String getCurrentProcessName() {
  175. int pid = android.os.Process.myPid();
  176. String processName = "";
  177. ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService
  178. (Context.ACTIVITY_SERVICE);
  179. for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
  180. if (process.pid == pid) {
  181. processName = process.processName;
  182. }
  183. }
  184. return processName;
  185. }
  186. //监听plc设备状态
  187. public void initDeviceStateListener() {
  188. CommunicationHelper.getInstance().setDeviceStatusListener(new Callback<Result<Boolean>>() {
  189. @Override
  190. public void onResult(Result<Boolean> result) {
  191. switch (result.code) {
  192. case BasePlcAgreement.PLC_HEART_CONNECT:
  193. if (!pclDeviceConnected) {
  194. pclDeviceConnected = true;
  195. isDeviceStatus=false;
  196. ErrorViewHelper.getInstance().hideErrorView(R.string.error_plc_disconnect);
  197. Log.d(TAG, "onResult: PLC_HEART_CONNECT");
  198. }
  199. break;
  200. case BasePlcAgreement.PLC_HEART_DISCONNECT:
  201. if (pclDeviceConnected) {
  202. pclDeviceConnected = false;
  203. isDeviceStatus=false;
  204. ErrorViewHelper.getInstance().showErrorView(R.string.error_plc_disconnect, new String[]{getString(R.string.error_plc_disconnect), getString(R.string.make_fail_describe)});
  205. Log.d(TAG, "onResult: PLC_HEART_CONNECT1");
  206. }
  207. break;
  208. case BasePlcAgreement.PLC_HEART_REPORT:
  209. if (result.data != null) {
  210. isDeviceStatus=true;
  211. Log.d(TAG, "onResult:测试报警1 "+result.message);
  212. if (result.data) {
  213. ErrorViewHelper.getInstance().hideErrorView(R.string.setting_plc_connect);
  214. Log.d("上传报警", "onResult: 上传报警:清除了未进入自动售卖的问题");
  215. autoMode.clear();
  216. } else {
  217. if (result.message != null) {
  218. String[] lastErrorMessage = result.message.split("-");
  219. Log.d(TAG, "onResult: "+result.message);
  220. String prefix = result.message.substring(0, result.message.indexOf('-'));
  221. Log.d(TAG, "onResult1: "+prefix);
  222. ErrorViewHelper.getInstance().showErrorView(R.string.setting_plc_connect, lastErrorMessage);
  223. if (autoMode.add(prefix)){
  224. Log.d("上传报警", "onResult: 上传报警:"+prefix);
  225. EventBus.getDefault().post(new ErrorApiMessageEvent(Name.WARNING_ALARM, prefix,"1"));
  226. }
  227. } else {
  228. ErrorViewHelper.getInstance().showErrorView(R.string.setting_plc_connect, new String[]{getString(R.string.error_wait), getString(R.string.make_fail_describe_refund)});
  229. EventBus.getDefault().post(new ErrorApiMessageEvent(Name.WARNING_ALARM, getString(R.string.error_wait),"1"));
  230. }
  231. }
  232. }else {
  233. Log.d(TAG, "onResult: 测试报警5");
  234. autoMode.clear();
  235. }
  236. break;
  237. }
  238. }
  239. });
  240. }
  241. private static final HashSet<String> autoMode = new HashSet<>();
  242. public boolean isPlcConnected() {
  243. return pclDeviceConnected;
  244. }
  245. public void killActivities() {
  246. ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  247. List<ActivityManager.AppTask> appTaskList = activityManager.getAppTasks();
  248. for (ActivityManager.AppTask appTask : appTaskList) {
  249. appTask.finishAndRemoveTask();
  250. }
  251. System.exit(0);
  252. }
  253. public void forceLocale(Locale locale, Context context) {
  254. this.locale = locale;
  255. Configuration conf = context.getResources().getConfiguration();
  256. conf.setLocale(locale);
  257. context.getResources().updateConfiguration(conf, context.getResources().getDisplayMetrics());
  258. Locale.setDefault(locale);
  259. }
  260. //初始化商品数据
  261. private void initGoodsBean() {
  262. boolean contains = Hawk.contains("PRICEBEAN");
  263. // //如果不存在 创建默认的
  264. if (!contains) {
  265. ArrayList<PriceFragment.PriceBean> priceBeans = new ArrayList<>();
  266. for (int i = 0; i < PriceFragment.ProductAbout.chineseName.length; i++) {
  267. PriceFragment.PriceBean priceBean = new PriceFragment.PriceBean();
  268. priceBean.setChineseName(PriceFragment.ProductAbout.chineseName[i]);
  269. priceBean.setNameKey(PriceFragment.ProductAbout.nameKey[i]);
  270. priceBean.setPriceKey(PriceFragment.ProductAbout.priceKey[i]);
  271. priceBean.setDefaultName(getString(PriceFragment.ProductAbout.defaultName[i]));
  272. priceBean.setPriceDefault(PriceFragment.ProductAbout.priceDefault[i]);
  273. priceBeans.add(priceBean);
  274. Log.d(TAG, "initGoodsBean: " + priceBeans.get(i).toString());
  275. }
  276. Hawk.put("PRICEBEAN", priceBeans);
  277. }
  278. }
  279. public String getVersionName() {
  280. //获取packagemanager的实例
  281. PackageManager packageManager = getPackageManager();
  282. //getPackageName()是你当前类的包名,0代表是获取版本信息
  283. PackageInfo packInfo = null;
  284. try {
  285. packInfo = packageManager.getPackageInfo(getPackageName(), 0);
  286. } catch (PackageManager.NameNotFoundException e) {
  287. e.printStackTrace();
  288. }
  289. return packInfo.versionName;
  290. }
  291. }