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