diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 33cf045d..1c568c5f 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -411,6 +411,14 @@ public class MicroPhotoService extends Service { } mService.enableGps(false); } else if (TextUtils.equals(ACTION_RESTART, action)) { + + String reason = intent.getStringExtra("reason"); + MicroPhotoService.infoLog("Recv RESTART APP cmd, reason=" + (TextUtils.isEmpty(reason) ? "" : reason)); + try { + Thread.sleep(100); + } catch (Exception ex) { + ex.printStackTrace(); + } MicroPhotoService.restartApp(context.getApplicationContext(), MicroPhotoContext.PACKAGE_NAME_MPAPP); } } diff --git a/mpmaster/src/main/cpp/mpmaster.cpp b/mpmaster/src/main/cpp/mpmaster.cpp index ce4e339b..54d16081 100644 --- a/mpmaster/src/main/cpp/mpmaster.cpp +++ b/mpmaster/src/main/cpp/mpmaster.cpp @@ -21,8 +21,11 @@ #include #include #include +#include #include +#include #include +#include #include @@ -30,7 +33,8 @@ #define IOT_PARAM_READ 0xAF #define MAX_STRING_LEN 32 -#define PATH_MPAPP_STATS "/sdcard/com.xypower.mpapp/data/stats/" +#define PATH_MPAPP_STATS "/sdcard/com.xypower.mpapp/data/stats/" +#define PATH_MPAPP_RUNNING "/sdcard/com.xypower.mpapp/running" typedef struct { @@ -148,3 +152,34 @@ Java_com_xypower_mpmaster_MpMasterService_getSystemProperty( return env->NewStringUTF(value); } +extern "C" JNIEXPORT jboolean JNICALL +Java_com_xypower_mpmaster_MpMasterService_isMpAppRunning( + JNIEnv* env, jclass cls) { + + jboolean res = JNI_FALSE; + int fd = open(PATH_MPAPP_RUNNING, O_RDWR, 0444); + if (fd != -1) + { + __android_log_print(ANDROID_LOG_INFO, "MP_TAG", "fd != -1"); + + if (flock(fd, LOCK_EX) == 0) + { + __android_log_print(ANDROID_LOG_INFO, "MP_TAG", "locked"); + + } else + { + __android_log_print(ANDROID_LOG_INFO, "MP_TAG", "can't locked"); + res = JNI_TRUE; + } + close(fd); + return res; + } + else + { + __android_log_print(ANDROID_LOG_INFO, "MP_TAG", "can't open"); + } + + return JNI_FALSE; + +} + diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index a5ac11b2..8b6c55c4 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -74,7 +74,7 @@ public class MpMasterService extends Service { private static final String ACTION_HEARTBEAT = "com.xypower.mpmaster.ACT_HB"; private static final String ACTION_TAKE_PHOTO = "com.xypower.mpapp.ACT_TP"; - public static final String ACTION_MP_RESTART = "com.xypower.mpapp.ACT_START"; + public static final String ACTION_MP_RESTART = "com.xypower.mpapp.ACT_RESTART"; public static final String ACTION_IMP_PUBKRY = "com.xypower.mpapp.ACT_IMP_PUBKEY"; private static final String FOREGROUND_CHANNEL_ID = "fg_mpmst"; @@ -196,6 +196,8 @@ public class MpMasterService extends Service { } mMpAppVersion = packageInfo == null ? "" : packageInfo.versionName; + mPreviousMpHbTime = System.currentTimeMillis(); + logger.info("MpMaster started version=" + mMpMasterVersion); mHander = new Handler(); @@ -358,21 +360,22 @@ public class MpMasterService extends Service { public void startMpApp() { try { final Context context = getApplicationContext(); - - if (!MicroPhotoContext.isAppAlive(context, MicroPhotoContext.PACKAGE_NAME_MPAPP)) { - MicroPhotoContext.restartMpApp(context); - mTimeToStartMpApp = System.currentTimeMillis(); - logger.warning("Restart MpAPP as it is NOT Running"); + long ts = System.currentTimeMillis(); + + if (ts - mPreviousMpHbTime > mMpHeartbeatDuration * 2) { + // MpApp is not running + if (ts - mTimeToStartMpApp >= 30000) { + MicroPhotoContext.restartMpApp(context); + mTimeToStartMpApp = ts; + logger.warning("Restart MpAPP as it is NOT Running Prev MPAPP HB=" + Long.toString((ts - mPreviousMpHbTime) / 1000)); + } else { + logger.warning("MpAPP has restarted during 30s, skip the check."); + } return; } + // MpApp is alive final String appPath = MicroPhotoContext.buildMpAppDir(context); - final long ts = System.currentTimeMillis(); - if (ts - mTimeToStartMpApp < 30000) { - logger.warning("MpAPP has restarted in 30s, skip the check."); - return; - } - final File mpappHb = new File(appPath + "data/alive/hb"); final long modifiedTimeOfHb = getFileModificationTime(appPath + "data/alive/hb"); final long modifiedTimeOfPhoto = getFileModificationTime(appPath + "data/alive/taking"); @@ -381,17 +384,20 @@ public class MpMasterService extends Service { if (((ts - modifiedTimeOfHb) > mTimeOfMpAppAlive) || ((ts - modifiedTimeOfPhoto) > mTimeOfMpAppAlive * 4) || ((ts - modifiedTimeOfUpload) > mTimeOfMpAppAlive * 4)) { - mHander.postDelayed(new Runnable() { - @Override - public void run() { - // greater than 30m - logger.warning("Restart MpAPP as it is NOT Running hb=" + Long.toString(ts - modifiedTimeOfHb) + - " taking=" + Long.toString(ts - modifiedTimeOfPhoto) + " sending=" + Long.toString(ts - modifiedTimeOfUpload)); - MicroPhotoContext.restartMpApp(context); - mTimeToStartMpApp = System.currentTimeMillis(); - } - }, mDelayedRestartMpTime); + String msg = "Restart MpAPP as it is NOT Running hb=" + Long.toString(ts - modifiedTimeOfHb) + + " taking=" + Long.toString(ts - modifiedTimeOfPhoto) + " sending=" + Long.toString(ts - modifiedTimeOfUpload) + + " Will restart MpApp in " + Long.toString(mDelayedRestartMpTime / 1000) + " seconds"; + logger.warning(msg); + + AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); + Intent alarmIntent = new Intent(); + alarmIntent.setAction(ACTION_MP_RESTART); + alarmIntent.putExtra("reason", msg); + + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, ts + mDelayedRestartMpTime, pendingIntent); } } catch (Exception ex) { ex.printStackTrace(); @@ -519,11 +525,11 @@ public class MpMasterService extends Service { } else if (TextUtils.equals(MicroPhotoContext.ACTION_HEARTBEAT_MP, action)) { - mService.mPreviousMpHbTime = System.currentTimeMillis(); + if (intent.hasExtra("HeartbeatDuration")) { mService.mMpHeartbeatDuration = intent.getIntExtra("HeartbeatDuration", 600000); } - + mService.mPreviousMpHbTime = intent.getLongExtra("HeartbeatTime", System.currentTimeMillis()); mService.logger.info("Heartbeat Timer Fired By MpAPP ACTION=" + action + " MpHB=" + Long.toString(mService.mMpHeartbeatDuration)); mService.registerHeartbeatTimer(); @@ -532,8 +538,6 @@ public class MpMasterService extends Service { mService.startMaster(true); } mService.startMpApp(); - - } else if (TextUtils.equals(ACTION_UPDATE_CONFIGS, action)) { int restart = intent.getIntExtra("restart", 0); mService.logger.info("Update Config Fired ACTION=" + action + " restart=" + restart); @@ -1052,6 +1056,7 @@ public class MpMasterService extends Service { 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////////////////////