diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java index 90ac8dbc..b2971516 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/MpMasterService.java @@ -93,7 +93,7 @@ public class MpMasterService extends Service { private static int mStateService = STATE_SERVICE.NOT_CONNECTED; - private int mMpHeartbeatDuration = 10; + private int mMpHeartbeatDuration = 10; // Unit: minute private boolean mMntnMode = false; private boolean mQuickHbMode = false; @@ -486,30 +486,37 @@ public class MpMasterService extends Service { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (TextUtils.equals(ACTION_HEARTBEAT, action)) { - mService.logger.info("Heartbeat Timer Fired ACTION=" + action); + boolean keepAlive = intent.getBooleanExtra("keepAlive", false); + if (keepAlive) { + mService.logger.info("KeepAlive Heartbeat Timer Fired ACTION=" + action); + } else { + mService.logger.info("Heartbeat Timer Fired ACTION=" + action); + } mService.mPreviousHB = null; mService.registerHeartbeatTimer(); - mService.startMaster(false); + if (!keepAlive) { + mService.startMaster(false); + } mService.startMpApp(); } else if (TextUtils.equals(MicroPhotoContext.ACTION_HEARTBEAT_MP, action)) { mService.mPreviousMpHbTime = System.currentTimeMillis(); - mService.mMpHeartbeatDuration = intent.getIntExtra("HeartbeatDuration", 10); + if (intent.hasExtra("HeartbeatDuration")) { + mService.mMpHeartbeatDuration = intent.getIntExtra("HeartbeatDuration", 10); + } - if (!mService.mSeparateNetwork && (!mService.mMntnMode)) { - mService.logger.info("Heartbeat Timer Fired By MpAPP ACTION=" + action); + mService.logger.info("Heartbeat Timer Fired By MpAPP ACTION=" + action); - long ts = System.currentTimeMillis(); - if (mService.mPreviousHeartbeatTime - ts < mService.mHeartbeatDuration * 1000) { - mService.registerHeartbeatTimer(mService.mPreviousHeartbeatTime + mService.mHeartbeatDuration * 1000); - } + mService.registerHeartbeatTimer(); + if (!mService.mSeparateNetwork && (!mService.mMntnMode)) { mService.startMaster(true); - mService.startMpApp(); } + mService.startMpApp(); + } else if (TextUtils.equals(ACTION_UPDATE_CONFIGS, action)) { int restart = intent.getIntExtra("restart", 0); @@ -576,11 +583,13 @@ public class MpMasterService extends Service { private void registerHeartbeatTimer() { long timeout = mHeartbeatDuration; + boolean keepAlive = false; + long currentTimeMs = System.currentTimeMillis(); if (mMntnMode) { if (mQuickHbMode) { timeout = mQuickHeartbeatDuration; } - registerHeartbeatTimer(System.currentTimeMillis() + timeout * 1000); + registerHeartbeatTimer(currentTimeMs + timeout * 1000, keepAlive); } else { long closestTime = -1; if (mUsingAbsHbTime) { @@ -616,18 +625,30 @@ public class MpMasterService extends Service { } } - registerHeartbeatTimer(zeroPoint + closestTime * 1000); + if (zeroPoint + closestTime > currentTimeMs + mMpHeartbeatDuration * 60000) { + keepAlive = true; + registerHeartbeatTimer(currentTimeMs + timeout * 1000, keepAlive); + } else { + registerHeartbeatTimer(zeroPoint + closestTime * 1000, keepAlive); + } } else { - registerHeartbeatTimer(System.currentTimeMillis() + timeout * 1000); + if (mPreviousHeartbeatTime - currentTimeMs < mHeartbeatDuration * 1000) { + registerHeartbeatTimer(mPreviousHeartbeatTime + mHeartbeatDuration * 1000, keepAlive); + } else { + registerHeartbeatTimer(currentTimeMs + timeout * 1000, keepAlive); + } } } } - private void registerHeartbeatTimer(long triggerTime) { + private void registerHeartbeatTimer(long triggerTime, boolean keepAlive) { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + if (mPreviousHB != null) { try { + logger.info(String.format("Cancel Previos HB: " + format.format(new Date(mPreviousHeartbeatTime)))); alarmManager.cancel(mPreviousHB); mPreviousHB = null; } catch (Exception ex) { @@ -636,11 +657,18 @@ public class MpMasterService extends Service { } Intent alarmIntent = new Intent(); alarmIntent.setAction(ACTION_HEARTBEAT); + if (keepAlive) { + alarmIntent.putExtra("keepAlive", keepAlive); + } PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0); Date dt = new Date(triggerTime); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - logger.info(String.format("Register HB:" + format.format(dt)) + " MntnMode" + (mMntnMode ? "1" : "0") + " QuickHb=" + (mQuickHbMode ? "1" : "0")); + + if (keepAlive) { + logger.info(String.format("Register KeepAlive HB: " + format.format(dt)) + " MntnMode=" + (mMntnMode ? "1" : "0") + " QuickHb=" + (mQuickHbMode ? "1" : "0")); + } else { + logger.info(String.format("Register HB: " + format.format(dt)) + " MntnMode=" + (mMntnMode ? "1" : "0") + " QuickHb=" + (mQuickHbMode ? "1" : "0")); + } mPreviousHB = pendingIntent; mPreviousHeartbeatTime = triggerTime; alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerTime, pendingIntent);