| | |
| | | |
| | | import android.app.Activity; |
| | | import android.app.ActivityManager; |
| | | import android.app.AlarmManager; |
| | | import android.app.Application; |
| | | import android.app.PendingIntent; |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.content.res.Configuration; |
| | |
| | | |
| | | import androidx.multidex.MultiDex; |
| | | |
| | | import com.google.android.gms.common.ConnectionResult; |
| | | import com.google.android.gms.common.GoogleApiAvailability; |
| | | import com.google.firebase.messaging.FirebaseMessaging; |
| | | import com.google.gson.Gson; |
| | | import com.hdl.hdlhttp.HxHttpConfig; |
| | | import com.hdl.linkpm.sdk.HDLLinkPMSdk; |
| | |
| | | import com.hdl.photovoltaic.config.AppConfigManage; |
| | | import com.hdl.photovoltaic.config.ConstantManage; |
| | | import com.hdl.photovoltaic.config.UserConfigManage; |
| | | import com.hdl.photovoltaic.enums.LogoutType; |
| | | import com.hdl.photovoltaic.enums.LowerTagType; |
| | | import com.hdl.photovoltaic.enums.NetworkType; |
| | | import com.hdl.photovoltaic.listener.CloudCallBeak; |
| | | import com.hdl.photovoltaic.other.HdlAccountLogic; |
| | | import com.hdl.photovoltaic.other.HdlDeviceLogic; |
| | | import com.hdl.photovoltaic.other.HdlFileLogic; |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | |
| | | import com.hdl.photovoltaic.ui.StartActivity; |
| | | import com.hdl.photovoltaic.uni.HDLUniMPSDKManager; |
| | | import com.hdl.photovoltaic.utils.AppManagerUtils; |
| | | import com.hdl.photovoltaic.utils.BadgeUtils; |
| | | import com.hdl.photovoltaic.utils.LocalManageUtil; |
| | | import com.hdl.photovoltaic.utils.NetworkUtils; |
| | | import com.hdl.photovoltaic.utils.SharedPreUtils; |
| | |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | | Log.d("=======", "11111111"); |
| | | |
| | | // // 获取当前进程的PID |
| | | // String processMessage= "进程ID:"+Process.myPid()+" 进程名:"+RuningAcitvityUtil.getAppName(getBaseContext()); |
| | | // String processMessage = "进程Uid:" + Process.myUid() + "进程ID:" + Process.myPid() + " 进程名:" + RuningAcitvityUtil.getAppName(getBaseContext()); |
| | | Log.d("Application==onCreate", "onCreate"); |
| | | // Log.d(TAG, processMessage); |
| | | // if (RuningAcitvityUtil.getAppName(getBaseContext()).contains(":")) { |
| | | // //非原生进程,用初始化后面的 |
| | |
| | | initAutoSize(); |
| | | // //初始化极光推送 |
| | | // initJPush(); |
| | | //谷歌原生fcm推送 |
| | | // 检查 Google Play Services 后再初始化 FCM |
| | | if (isGooglePlayServicesAvailable(this)) { |
| | | initFCM(); |
| | | } else { |
| | | HdlLogLogic.print("Google Play Services 不可用,无法使用 FCM", true); |
| | | // 可以在这里初始化备选推送方案(如阿里云自建通道) |
| | | } |
| | | initHDLLinkPMSdk(); |
| | | HDLLinkLocalSdk.getInstance().init(getInstance()); |
| | | initSocket(); |
| | |
| | | * 初始化HDLLinkPMSdk |
| | | */ |
| | | private void initHDLLinkPMSdk() { |
| | | Log.d("===1====", "2222222"); |
| | | //1.初始化SDK |
| | | HDLLinkPMSdk.initWithAppKey(this, AppConfigManage.getAppKey(), AppConfigManage.getAppSecret(), AppConfigManage.getAPPRegionUrl()); |
| | | //2.设置打印 |
| | |
| | | HDLLinkPMUser.getInstance().setOnReloginListener(new IOnReloginListener() { |
| | | @Override |
| | | public void onReLogin(int type) { |
| | | |
| | | if (100 == type) { |
| | | if (type == LogoutType.ServerSwitchingLogout.getValue()) { |
| | | new Handler().postDelayed(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | BadgeUtils.cancelAllNotifications(getApplicationContext()); |
| | | BadgeUtils.clearBadge(getApplicationContext()); |
| | | //切换服务器地址用的 |
| | | UserConfigManage.getInstance().setOnlineServer(!UserConfigManage.getInstance().isOnlineServer()); |
| | | UserConfigManage.getInstance().Save(); |
| | | restartApp(mHDLApp); |
| | | } |
| | | }, 500); |
| | | } else if (type == LogoutType.AbnormalLogout.getValue()) { |
| | | //异常情况-强制重启应用 |
| | | new Handler().postDelayed(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | BadgeUtils.cancelAllNotifications(getApplicationContext()); |
| | | BadgeUtils.clearBadge(getApplicationContext()); |
| | | restartApp(mHDLApp); |
| | | } |
| | | }, 500); |
| | | } else if (type == LogoutType.UniLogout.getValue()) { |
| | | AppManagerUtils.getAppManager().finishAllActivity(); |
| | | //清空住宅id |
| | | UserConfigManage.getInstance().setHomeId(""); |
| | | UserConfigManage.getInstance().setAutomaticSkipNoNetworkFlag(true); |
| | | //重新设定一个时间 |
| | | UserConfigManage.getInstance().setLoginDateTime(0); |
| | | UserConfigManage.getInstance().Save(); |
| | | //清空消息缓存数据 |
| | | HdlMessageLogic.getInstance().clearListMessage(); |
| | | //清空电站缓存数据 |
| | | HdlResidenceLogic.getInstance().clearHouseList(); |
| | | //清空设备缓存数据 |
| | | HdlDeviceLogic.getInstance().clearDeviceList(); |
| | | //清空缓存数据 |
| | | if (mInfoMap != null) { |
| | | mInfoMap.clear(); |
| | | } |
| | | //断开mqtt连接 |
| | | if (MqttRecvClient.getInstance() != null) { |
| | | MqttRecvClient.getInstance().disConnect(); |
| | | } |
| | | //断开所有tcp连接且清空连接缓存 |
| | | // TODO: 10/10/23 |
| | | // TcpSocketBoot.stopAllConnectLinkGateway(); |
| | | if (null != HDLUniMPSDKManager.getInstance().getUniMP()) { |
| | | HDLUniMPSDKManager.getInstance().getUniMP().closeUniMP(); |
| | | } |
| | | SystemClock.sleep(200); |
| | | initDCUniMPSDK(); |
| | | BadgeUtils.cancelAllNotifications(getApplicationContext()); |
| | | BadgeUtils.clearBadge(getApplicationContext()); |
| | | Intent intent = new Intent(getInstance(), LoginActivity.class); |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| | | startActivity(intent); |
| | | } else { |
| | | HdlLogLogic.print("监听到登录退出---" + type, true); |
| | | //删除推送Token |
| | |
| | | } |
| | | SystemClock.sleep(200); |
| | | initDCUniMPSDK(); |
| | | BadgeUtils.cancelAllNotifications(getApplicationContext()); |
| | | BadgeUtils.clearBadge(getApplicationContext()); |
| | | Intent intent = new Intent(getInstance(), LoginActivity.class); |
| | | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); |
| | | if (type == 1) { |
| | |
| | | }); |
| | | |
| | | } |
| | | |
| | | public void test(Context context) { |
| | | // new Handler().postDelayed(() -> { |
| | | // // 1. 设置延迟重启 |
| | | // Intent restartIntent = new Intent(context, StartActivity.class); |
| | | // restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| | | // |
| | | // PendingIntent pendingIntent = PendingIntent.getActivity( |
| | | // context, 0, restartIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT |
| | | // ); |
| | | // |
| | | // AlarmManager am1 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |
| | | // am1.set(AlarmManager.RTC, System.currentTimeMillis() + 300, pendingIntent); |
| | | // |
| | | // int myUid = android.os.Process.myUid(); |
| | | // ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); |
| | | // String packageName = context.getPackageName(); |
| | | // |
| | | // for (ActivityManager.RunningAppProcessInfo proc : am.getRunningAppProcesses()) { |
| | | // if (proc.uid == myUid && proc.processName.startsWith(packageName)) { |
| | | // if (proc.pid != android.os.Process.myPid()) { |
| | | // try { |
| | | // android.os.Process.killProcess(proc.pid); |
| | | // } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | // } |
| | | // } |
| | | // } |
| | | // } |
| | | // |
| | | // // 3. 退出当前进程 |
| | | // android.os.Process.killProcess(android.os.Process.myPid()); |
| | | // }, 500); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 初始化UniMPSDK 小程序SDK |
| | |
| | | // JPushInterface.setDebugMode(true); |
| | | // JPushInterface.init(getBaseContext()); |
| | | } |
| | | |
| | | private void initFCM() { |
| | | // 获取 FCM Token |
| | | FirebaseMessaging.getInstance().getToken() |
| | | .addOnCompleteListener(task -> { |
| | | if (!task.isSuccessful()) { |
| | | Log.w("FCM", "获取 FCM Token 失败", task.getException()); |
| | | return; |
| | | } |
| | | // 获取新的 FCM Token |
| | | String token = task.getResult(); |
| | | Log.d("FCM", "FCM Token: " + token); |
| | | |
| | | // 保存 token |
| | | saveFCMToken(token); |
| | | }); |
| | | |
| | | // // 订阅主题(可选) |
| | | // FirebaseMessaging.getInstance().subscribeToTopic("all_users") |
| | | // .addOnCompleteListener(task -> { |
| | | // String msg = "订阅主题成功"; |
| | | // if (!task.isSuccessful()) { |
| | | // msg = "订阅主题失败"; |
| | | // } |
| | | // Log.d("FCM", msg); |
| | | // }); |
| | | } |
| | | |
| | | /** |
| | | * 检查 Google Play Services 是否可用 |
| | | */ |
| | | private boolean isGooglePlayServicesAvailable(Context context) { |
| | | GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); |
| | | int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(context); |
| | | return resultCode == ConnectionResult.SUCCESS; |
| | | } |
| | | |
| | | /** |
| | | * 保存令牌 |
| | | * |
| | | * @param token 令牌 |
| | | */ |
| | | public void saveFCMToken(String token) { |
| | | if (TextUtils.isEmpty(token.trim())) { |
| | | return; |
| | | } |
| | | UserConfigManage.getInstance().setPushToken(token); |
| | | UserConfigManage.getInstance().Save(); |
| | | |
| | | // // 发送到服务器 |
| | | // sendTokenToServer(token); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 监听网络状态变化 |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 重启app |
| | | */ |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 关掉app所有进程 |
| | | * |
| | | * @param context 上下文 |
| | | */ |
| | | private void killAppProcess(Context context) { |
| | | // AppManagerUtils.getAppManager().finishAllActivity(); |
| | | try { |
| | |
| | | List<android.app.ActivityManager.RunningAppProcessInfo> processInfos = manager.getRunningAppProcesses(); |
| | | // 先杀掉相关进程,最后再杀掉主进程 |
| | | for (android.app.ActivityManager.RunningAppProcessInfo runningAppProcessInfo : processInfos) { |
| | | if (runningAppProcessInfo.pid != android.os.Process.myPid()) { |
| | | if ((runningAppProcessInfo.uid == android.os.Process.myUid()) && (runningAppProcessInfo.pid != android.os.Process.myPid())) { |
| | | try { |
| | | android.os.Process.killProcess(runningAppProcessInfo.pid); |
| | | } catch (Exception e) { |