From 67cccef5e4498755183da150ff1693b2b61c52e4 Mon Sep 17 00:00:00 2001 From: liuguijing <1440265357@qq.com> Date: Thu, 13 Mar 2025 11:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=85=8D=E7=BD=AE=E9=87=8D?= =?UTF-8?q?=E5=90=AF=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84bug,=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E9=87=8D=E5=90=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/common/MicroPhotoContext.java | 54 +++++--- mpmaster/src/main/AndroidManifest.xml | 1 + .../java/com/xypower/mpmaster/AppMaster.java | 10 +- .../com/xypower/mpmaster/MainActivity.java | 13 +- .../com/xypower/mpmaster/MpMasterService.java | 119 +++++++++++++----- .../com/xypower/mpmaster/UpdateReceiver.java | 2 +- .../com/xypower/mpmaster/sms/SimUtil.java | 1 - .../xypower/mpmaster/sms/SmsSendReceiver.java | 9 +- .../mpmaster/sms/UpdateSysConfigUtil.java | 54 +++----- 9 files changed, 162 insertions(+), 101 deletions(-) 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/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..5295985d 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -900,7 +900,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 +956,7 @@ public class AppMaster { } catch (Exception ex) { } - MicroPhotoContext.restartMpApp(context, "CMA Updated"); + MpMasterService.restartMpApp(context,"CMA Updated"); } }); @@ -978,7 +982,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 a92a26a3..3661b376 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MainActivity.java @@ -131,23 +131,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 1671b2fc..c4213219 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,16 +437,15 @@ 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 MicroPhotoContext.restartMpApp(context, "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 { @@ -641,12 +642,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)) { @@ -687,7 +691,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(); @@ -697,18 +701,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(); @@ -1129,7 +1129,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() { @@ -1160,8 +1160,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()); @@ -1254,15 +1253,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"); @@ -1333,7 +1387,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"); } }; @@ -1426,10 +1480,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) { @@ -1448,12 +1502,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 dc2a6f33..543a80e4 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -449,7 +449,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); } } } 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 dccdc568..3737e5d6 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,8 +180,12 @@ public class UpdateSysConfigUtil { //修改app的ip public static void setIP(Context context, String server, int port, int utcp, int encrypto) { MicroPhotoContext.AppConfig mpAppConfig = MicroPhotoContext.getMpAppConfig(context); - mpAppConfig.server = server; - mpAppConfig.port = port; + if (server != null) { + mpAppConfig.server = server; + } + if (port != -1) { + mpAppConfig.port = port; + } if (utcp != -1) { mpAppConfig.networkProtocol = utcp; } @@ -335,39 +351,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 = 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 | */Intent.FLAG_ACTIVITY_CLEAR_TOP); - 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; @@ -390,7 +373,6 @@ public class UpdateSysConfigUtil { context.sendBroadcast(intent1); } - //拍照时间表修改 public static void setPhotoSchedules(int channel, String msg) { if (StringUtils.isNotEmpty(msg)) {