diff --git a/common/src/main/java/com/xypower/common/MicroPhotoContext.java b/common/src/main/java/com/xypower/common/MicroPhotoContext.java index 2decd30e..6096a689 100644 --- a/common/src/main/java/com/xypower/common/MicroPhotoContext.java +++ b/common/src/main/java/com/xypower/common/MicroPhotoContext.java @@ -372,22 +372,22 @@ public class MicroPhotoContext { } } - public static void restartMpApp(Context context, String reason) { - /* - Context context = MicroPhotoService.this.getApplicationContext(); - Intent intent = getPackageManager().getLaunchIntentForPackage(context.getPackageName()); - - int noDelay = 1; - intent.putExtra("noDelay", noDelay); - PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, 0); - AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); - mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用 - System.exit(0); - - */ - - restartApp(context, PACKAGE_NAME_MPAPP, reason); - } +// public static void restartMpApp(Context context, String reason) { +// /* +// Context context = MicroPhotoService.this.getApplicationContext(); +// Intent intent = getPackageManager().getLaunchIntentForPackage(context.getPackageName()); +// +// int noDelay = 1; +// intent.putExtra("noDelay", noDelay); +// PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent, 0); +// AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE); +// mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用 +// System.exit(0); +// +// */ +// +// restartApp(context, PACKAGE_NAME_MPAPP, reason); +// } public static void restartMpApp(Context context, String reason, long delayedTimeMs) { Intent intent = context.getPackageManager().getLaunchIntentForPackage(PACKAGE_NAME_MPAPP); @@ -417,6 +417,28 @@ public class MicroPhotoContext { */ +// try { +// Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); +// if (intent != null) { +// intent.putExtra("noDelay", 1); +// if (!TextUtils.isEmpty(reason)) { +// intent.putExtra("reason", reason); +// } +// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); +// context.startActivity(intent); +// } +// } catch (Exception e) { +// e.printStackTrace(); +// } + + + SysApi.forceStopApp(context,packageName); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } +//// 然后启动目标应用 try { Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); if (intent != null) { diff --git a/mpmaster/build.gradle b/mpmaster/build.gradle index 5dbc9298..484f90f3 100644 --- a/mpmaster/build.gradle +++ b/mpmaster/build.gradle @@ -4,7 +4,7 @@ plugins { def AppMajorVersion = 1 def AppMinorVersion = 1 -def AppBuildNumber = 9 +def AppBuildNumber = 11 def AppVersionName = AppMajorVersion + "." + AppMinorVersion + "." + AppBuildNumber def AppVersionCode = AppMajorVersion * 100000 + AppMinorVersion * 1000 + AppBuildNumber diff --git a/mpmaster/src/main/AndroidManifest.xml b/mpmaster/src/main/AndroidManifest.xml index 2e8ff058..3bd7ba36 100644 --- a/mpmaster/src/main/AndroidManifest.xml +++ b/mpmaster/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 90b355c4..963ec29c 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -565,7 +565,8 @@ public class AppMaster { if (TextUtils.equals(cmd, CMD_REBOOT_DEV)) { mService.logger.warning("Recv Reset Cmd"); - SysApi.reboot(mService.getApplicationContext()); + MpMasterService.rebootDevice(); +// SysApi.reboot(mService.getApplicationContext()); } else if (TextUtils.equals(cmd, CMD_UPLOAD_LOGS)) { String url = jsonObject.optString("url", null); mService.logger.warning("Recv Upload Log " + url); @@ -900,7 +901,11 @@ public class AppMaster { String packageName = jsonObject.optString("packageName", null); if (packageName != null) { - MicroPhotoContext.restartApp(mService.getApplicationContext(), packageName, "Config Updated"); + if (packageName.equals(MicroPhotoContext.PACKAGE_NAME_MPMASTER)) { + MpMasterService.restartMpMasterApp(mService.getApplicationContext(), "Config Updated"); + } else { + MpMasterService.restartAppByPackage(mService.getApplicationContext(), packageName,"Config Updated"); + } } } } catch (Exception ex) { @@ -952,7 +957,7 @@ public class AppMaster { } catch (Exception ex) { } - MicroPhotoContext.restartMpApp(context, "CMA Updated"); + MpMasterService.restartMpApp(context,"CMA Updated"); } }); @@ -978,7 +983,7 @@ public class AppMaster { MicroPhotoContext.saveMpAppConfig(context, appConfig); - MicroPhotoContext.restartMpApp(mService.getApplicationContext(), "HB Duration Updated"); + MpMasterService.restartMpApp(context.getApplicationContext(),"HB Duration Updated"); return true; } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java index a25bfa8e..94f42108 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java @@ -143,23 +143,16 @@ public class MainActivity extends AppCompatActivity { } break; case R.id.action_notify_mp_reboot:{ - MicroPhotoContext.restartMpApp(getApplicationContext(), "Manual Restart from MpMst"); + MpMasterService.restartMpApp(getApplicationContext(),"Manual Restart from MpMst"); } break; case R.id.action_reboot_mp: { - Context context = getApplicationContext(); - Intent intent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP); - int noDelay = 1; - intent.putExtra("noDelay", noDelay); - intent.putExtra("reason", "Manual Restart from MpMst"); - - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - context.startActivity(intent); + MpMasterService.restartMpApp(getApplicationContext(),"Manual Restart from MpMst"); } break; case R.id.action_reboot_mpmst:{ - MpMasterService.restartApp(getApplicationContext()); + MpMasterService.restartMpMasterApp(getApplicationContext(),"MpMaster Restart"); } break; case R.id.action_refresh:{ diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index da06972b..6aa41b3d 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -155,9 +155,9 @@ public class MpMasterService extends Service { try { Intent intent = new Intent(this, MainActivity.class); int uniqueReqCode = reqCode.getAndIncrement(); - PendingIntent pi = PendingIntent.getActivity(this,uniqueReqCode, intent,0); - AlarmManager alarmManager=(AlarmManager)getSystemService(ALARM_SERVICE); - alarmManager.set(AlarmManager.RTC_WAKEUP,System.currentTimeMillis() + 5000, pi); + PendingIntent pi = PendingIntent.getActivity(this, uniqueReqCode, intent, 0); + AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); + alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000, pi); logger.info("Restart MpApp after 5s as for LowMemory"); } catch (Exception ex) { ex.printStackTrace(); @@ -390,7 +390,9 @@ public class MpMasterService extends Service { return mMntnMode; } - public boolean shouldSyncTime() { return mSyncTime; } + public boolean shouldSyncTime() { + return mSyncTime; + } public void detectMpAppAlive() { final MpMasterService thisObj = this; @@ -425,7 +427,7 @@ public class MpMasterService extends Service { if (!isMpAppRunning) { // Restart MpApp - MicroPhotoContext.restartMpApp(context, "MpMST Keep Alive Detection: NO Lock"); + MpMasterService.restartMpApp(context,"MpMST Keep Alive Detection: NO Lock"); logger.warning("Restart MpAPP as There is NO Lock"); mTimeToStartMpApp = ts; return; @@ -435,18 +437,18 @@ public class MpMasterService extends Service { } long tempduration = mMpHeartbeatDuration; - if(mMpHeartbeatDuration < 600000) + if (mMpHeartbeatDuration < 600000) tempduration = 290000; if (mPreviousMpHbTime <= ts && ts - mPreviousMpHbTime > tempduration * 2) { // MpApp is not running if (ts - mTimeToStartMpApp >= 1800000) { // 30 minutes 30 * 60 * 1000 if (false) { - MicroPhotoContext.restartMpApp(context, "MpMST Keep Alive Detection"); + MpMasterService.restartMpApp(context.getApplicationContext(),"MpMST Keep Alive Detection"); } logger.warning("Restart MpAPP as it is NOT Running Prev MPAPP HB=" + Long.toString((ts - mPreviousMpHbTime) / 1000) + " MPAPP HBDuration=" + Long.toString(mMpHeartbeatDuration) - + " Prev MpRestart Time=" + Long.toString(mTimeToStartMpApp) + " last MPAPPHB =" + (mPreviousMpHbTime/1000)); + + " Prev MpRestart Time=" + Long.toString(mTimeToStartMpApp) + " last MPAPPHB =" + (mPreviousMpHbTime / 1000)); mTimeToStartMpApp = ts; } else { @@ -643,12 +645,15 @@ public class MpMasterService extends Service { public static class AlarmReceiver extends BroadcastReceiver { private MpMasterService mService; + public AlarmReceiver() { mService = null; } + public AlarmReceiver(MpMasterService service) { mService = service; } + public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (TextUtils.equals(ACTION_HEARTBEAT, action)) { @@ -689,7 +694,7 @@ public class MpMasterService extends Service { int restart = intent.getIntExtra("restart", 0); mService.logger.info("Update Config Fired ACTION=" + action + " restart=" + restart); if (restart != 0) { - MicroPhotoContext.restartApp(context, context.getPackageName(), "Config Updated"); + MpMasterService.restartMpMasterApp(context,"Config Updated"); } else { mService.loadConfig(); mService.registerHeartbeatTimer(); @@ -699,18 +704,14 @@ public class MpMasterService extends Service { String cmd = intent.getStringExtra("cmd"); String msg = intent.getStringExtra("msg"); // mService.logger.info("cmd=" + cmd + " msg=" + msg); - if("write".equals(cmd)) { + if ("write".equals(cmd)) { // int progress = Integer.parseInt(msg); - } - else if("update".equals(cmd)) { + } else if ("update".equals(cmd)) { // int progress = Integer.parseInt(msg); - } - else if("info".equals(cmd)) { - } - else if("error".equals(cmd)) { + } else if ("info".equals(cmd)) { + } else if ("error".equals(cmd)) { mService.logger.warning("UPD OTA Failed"); - } - else if("success".equals(cmd)) { + } else if ("success".equals(cmd)) { //confirm to reboot device ?? mService.logger.warning("UPD OTA Succeeded, will REBOOT device"); Handler handler = new Handler(); @@ -1131,7 +1132,7 @@ public class MpMasterService extends Service { File file = new File(tempPath); file.mkdirs(); final String filePath = tempPath + File.separator + "mp.apk"; - Thread th =new Thread(new Runnable() { + Thread th = new Thread(new Runnable() { @Override public void run() { @@ -1162,8 +1163,7 @@ public class MpMasterService extends Service { if (rebootType == 0) { logger.warning("Recv REBOOT MpMst APP cmd"); Context context = MpMasterService.this.getApplicationContext(); - MicroPhotoContext.restartApp(context, context.getPackageName(), reason); - + MpMasterService.restartMpMasterApp(context,reason); } else { logger.warning("Recv RESET cmd"); SysApi.reboot(MpMasterService.this.getApplicationContext()); @@ -1256,15 +1256,70 @@ public class MpMasterService extends Service { return 0; } - public static void restartApp(Context context) { +//重启运维应用 + public static void restartMpMasterApp(Context context,String reason) { Intent intent = new Intent(context, MainActivity.class); - - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); + if (intent != null) { + if (!TextUtils.isEmpty(reason)) { + intent.putExtra("reason", reason); + } + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } context.startActivity(intent); - System.exit(0); } +//重启MpApp应用 + public static void restartMpApp(Context context, String reason) { + + SysApi.forceStopApp(context, MicroPhotoContext.PACKAGE_NAME_MPAPP); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } +//// 然后启动目标应用 + try { + Intent intent = context.getPackageManager().getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP); + if (intent != null) { + intent.putExtra("noDelay", 1); + if (!TextUtils.isEmpty(reason)) { + intent.putExtra("reason", reason); + } + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +//根据包名重启应用 + public static void restartAppByPackage(Context context,String packagename, String reason) { + + SysApi.forceStopApp(context, packagename); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } +//// 然后启动目标应用 + try { + Intent intent = context.getPackageManager().getLaunchIntentForPackage(packagename); + if (intent != null) { + intent.putExtra("noDelay", 1); + if (!TextUtils.isEmpty(reason)) { + intent.putExtra("reason", reason); + } + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + public void updateTime(long ms) { Intent intent = new Intent("com.xy.xsetting.action"); intent.putExtra("cmd", "settime"); @@ -1335,7 +1390,7 @@ public class MpMasterService extends Service { } } copyAssetsDir(context, "mpapp/data", tmpDestPath.getAbsolutePath()); - MicroPhotoContext.restartMpApp(context, "FIRST Config Init"); + MpMasterService.restartMpApp(context.getApplicationContext(),"FIRST Config Init"); } }; @@ -1428,10 +1483,10 @@ public class MpMasterService extends Service { } fos = new FileOutputStream(file); - int len=-1; + int len = -1; byte[] buffer = new byte[1024]; - while ((len=inputStream.read(buffer)) != -1) { - fos.write(buffer,0, len); + while ((len = inputStream.read(buffer)) != -1) { + fos.write(buffer, 0, len); } fos.flush(); } catch (Exception e) { @@ -1450,12 +1505,17 @@ public class MpMasterService extends Service { } public native static int getInt(int cmd); + public native static int setInt(int cmd, int val); + public native static int[] getStats(long ts); + public native static String getSystemProperty(String key); + public native static void rebootDevice(); + public native static boolean isMpAppRunning(); ////////////////////////GPS//////////////////// - + } \ No newline at end of file diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java b/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java index a51f5eb4..8c152507 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java @@ -110,7 +110,7 @@ public class UpdateReceiver extends BroadcastReceiver { if ((ts - modifiedTimeOfDb) > 12 * 1000) { // greater than 12 seconds // logger.warning("Start MpAPP as it is NOT running"); - MicroPhotoContext.restartMpApp(context, reason); + MpMasterService.restartMpApp(context.getApplicationContext(),reason); } } catch (Exception ex) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index 63d6ea51..5e79fc45 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -461,7 +461,6 @@ public class SimUtil { if (channel != null && resolutionCX != null && resolutionCY != null && videoCX != null && videoCY != null) { ifmessageCorrect = true; UpdateSysConfigUtil.setChannelResolution(channel, resolutionCX, resolutionCY, videoCX, videoCY); - UpdateSysConfigUtil.restartApp(context); } } } @@ -1197,7 +1196,7 @@ public class SimUtil { if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { return; } - if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) { + if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 0) { List localList = localSubscriptionManager.getActiveSubscriptionInfoList(); boolean doubleSim = isDoubleSim(mContext); SubscriptionInfo simInfoAnother = null; @@ -1209,6 +1208,10 @@ public class SimUtil { simInfoAnother = (SubscriptionInfo) localList.get(1); } } + } else { + if (localList != null && localList.size() > 0) { + simInfoAnother = (SubscriptionInfo) localList.get(0); + } } Intent itSend = new Intent(SMS_SEND_ACTION); itSend.putExtra(SMSRESTARTTYPE, restartType); diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsSendReceiver.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsSendReceiver.java index 2e147017..2698e1a8 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsSendReceiver.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SmsSendReceiver.java @@ -5,6 +5,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import com.xypower.common.MicroPhotoContext; import com.xypower.mpmaster.MpMasterService; /** @@ -50,12 +51,12 @@ public class SmsSendReceiver extends BroadcastReceiver { if (restartType == 0) { MpMasterService.rebootDevice(); } else if (restartType == 1) { - UpdateSysConfigUtil.restartApp(context); + MpMasterService.restartMpApp(context.getApplicationContext(), "SMS RESTART"); } else if (restartType == 2) { - UpdateSysConfigUtil.restartMasterApp(context); + MpMasterService.restartMpMasterApp(context.getApplicationContext(), "SMS RESTART"); } else if (restartType == 3) { - UpdateSysConfigUtil.restartApp(context); - UpdateSysConfigUtil.restartMasterApp(context); + MpMasterService.restartMpApp(context.getApplicationContext(), "SMS RESTART"); + MpMasterService.restartMpMasterApp(context.getApplicationContext(), "SMS RESTART"); } // if (type.contains(SmsTypeEnum.REBOOT1.value())) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java index 3edfa3f2..a8deaa81 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/UpdateSysConfigUtil.java @@ -1,21 +1,33 @@ package com.xypower.mpmaster.sms; +import static androidx.core.content.ContextCompat.getSystemService; +import static androidx.core.content.ContextCompat.startActivity; + +import android.app.ActivityManager; +import android.content.ActivityNotFoundException; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Build; import android.os.Environment; import android.provider.MediaStore; +import android.text.TextUtils; import android.util.Base64; +import android.util.Log; +import com.dev.devapi.api.SysApi; import com.xypower.common.FilesUtils; import com.xypower.common.JSONUtils; import com.xypower.common.MicroPhotoContext; import org.json.JSONObject; +import java.io.BufferedReader; import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; @@ -168,16 +180,16 @@ public class UpdateSysConfigUtil { //修改app的ip public static void setIP(Context context, String server, Integer port, Integer utcp, Integer encrypto) { MicroPhotoContext.AppConfig mpAppConfig = MicroPhotoContext.getMpAppConfig(context); - if (server!=null) { - mpAppConfig.server = server; + if (server != null) { + mpAppConfig.server = server; } - if (port!=null && port!=-1) { - mpAppConfig.port = port; + if (port != null && port != -1) { + mpAppConfig.port = port; } - if (utcp != null&& utcp!=-1 ) { + if (utcp != null && utcp != -1) { mpAppConfig.networkProtocol = utcp; } - if (encrypto != null&& encrypto!=-1) { + if (encrypto != null && encrypto != -1) { mpAppConfig.encryption = encrypto; } MicroPhotoContext.saveMpAppConfig(context, mpAppConfig); @@ -340,39 +352,6 @@ public class UpdateSysConfigUtil { } - //重启应用 - public static void restartApp(Context context) { - -// Intent intent = new Intent(MicroPhotoContext.ACTION_RESTART_MP); -// intent.putExtra("noDelay", 1); -// intent.setPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP); -// -// context.sendBroadcast(intent); -// try { -// Thread.sleep(200); -// } catch (Exception ex) { -// ex.printStackTrace(); -// } - - PackageManager packageManager = context.getPackageManager(); - - Intent intent = packageManager.getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPAPP); - intent.putExtra("noDelay", 1); - // intent.putExtra("reboot", 1); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK ); - context.startActivity(intent); - } - - //重启运维应用 - public static void restartMasterApp(Context context) { - PackageManager packageManager = context.getPackageManager(); - Intent intent = packageManager.getLaunchIntentForPackage(MicroPhotoContext.PACKAGE_NAME_MPMASTER); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); - context.startActivity(intent); - - System.exit(0); - } - public static void takePhotoOrVideo(Context context, long channel, long preset, boolean photoOrVideo) { List schedules = new ArrayList<>(); long ts = System.currentTimeMillis() / 1000;