From 95c0a9e814835467b1040d34bb650d80b8361b43 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 6 May 2024 13:58:19 +0800 Subject: [PATCH] =?UTF-8?q?APP=E6=9B=B4=E6=96=B0=E4=B9=8B=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E9=87=8D=E5=90=AF=E5=89=8D=E5=85=88=E5=88=A4=E6=96=AD?= =?UTF-8?q?APP=E6=98=AF=E5=90=A6=E5=B7=B2=E7=BB=8F=E5=AD=98=E6=B4=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpmaster/UpdateReceiver.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java b/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java index 96946481..0085a8e0 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/UpdateReceiver.java @@ -3,7 +3,13 @@ package com.xypower.mpmaster; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; +import android.text.TextUtils; import android.util.Log; +import android.os.Handler; + +import com.xypower.common.MicroPhotoContext; + +import java.io.File; public class UpdateReceiver extends BroadcastReceiver { @@ -40,6 +46,41 @@ public class UpdateReceiver extends BroadcastReceiver { } } + private void packageChanged(Context context, String action, String packageName, String targetPackageName, String aliveFileName) { + if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) { // Upgrade Broadcast + Log.e(TAG, "onReceive:Upgraded and Restart the App:" + targetPackageName); + MpMasterService.resetVersions(); + if (packageName.equals("package:" + targetPackageName)) { + // SysApi.enableApp(context, targetPackageName); + tryToRestartApp(context, targetPackageName); + } + } else if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {// Install broadcast + Log.e(TAG, "onReceive:Installed and Start the App:" + targetPackageName); + MpMasterService.resetVersions(); + if (packageName.equals("package:" + targetPackageName)) { + /*SystemUtil.reBootDevice();*/ + // SysApi.enableApp(context, targetPackageName); + tryToRestartApp(context, targetPackageName); + } + } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) { // Uninstall + // Logger.e(TAG, "onReceive:uninstall" + packageName); + } + } + + private void tryToRestartApp(final Context context, final String targetPackageName) { + Handler handler = new Handler(); + handler.postDelayed(new Runnable() { + @Override + public void run() { + if (TextUtils.equals(targetPackageName, APP_PACKAGE_MPAPP)) { + startMpApp(context); + } else { + restartAPP(context, targetPackageName); + } + } + }, 10000); + } + public static void restartAPP(Context context, String packageName) { Intent intent = context.getPackageManager() .getLaunchIntentForPackage(packageName); @@ -47,4 +88,29 @@ public class UpdateReceiver extends BroadcastReceiver { context.startActivity(intent); // ActManager.getAppManager().finishAllActivity(); } + + public void startMpApp(final Context context) { + try { + + if (MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP)) { + return; + } + + String appPath = MicroPhotoContext.buildMpAppDir(context); + long ts = System.currentTimeMillis(); + + File mpappDb = new File(appPath + "data/App.db"); + long modifiedTimeOfDb = 0; + if (mpappDb.exists()) { + modifiedTimeOfDb = mpappDb.lastModified(); + } + if ((ts - modifiedTimeOfDb) > 12 * 1000) { + // greater than 12 seconds + // logger.warning("Start MpAPP as it is NOT running"); + MicroPhotoContext.restartMpApp(context); + } + } catch (Exception ex) { + + } + } } \ No newline at end of file