Stop one shot operations after 10 seconds idle

pull/162/head
M66B 6 years ago
parent d1520be8e7
commit e1bd01be0b

@ -102,7 +102,8 @@ public class ServiceSynchronize extends ServiceBase {
private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds private static final long RECONNECT_BACKOFF = 90 * 1000L; // milliseconds
private static final int ACCOUNT_ERROR_AFTER = 60; // minutes private static final int ACCOUNT_ERROR_AFTER = 60; // minutes
private static final int BACKOFF_ERROR_AFTER = 16; // seconds private static final int BACKOFF_ERROR_AFTER = 16; // seconds
private static final long ONESHOT_DURATION = 90 * 1000L; // milliseconds private static final long ONESHOT_DURATION_RUN = 90 * 1000L; // milliseconds
private static final long ONESHOT_DURATION_IDLE = 10 * 1000L; // milliseconds
private static final long STOP_DELAY = 5000L; // milliseconds private static final long STOP_DELAY = 5000L; // milliseconds
private static final long CHECK_ALIVE_INTERVAL = 19 * 60 * 1000L; // milliseconds private static final long CHECK_ALIVE_INTERVAL = 19 * 60 * 1000L; // milliseconds
@ -144,8 +145,8 @@ public class ServiceSynchronize extends ServiceBase {
Log.e(ex); Log.e(ex);
} }
if (oneshot && stats.operations > 0) if (oneshot)
onOneshot(true); onOneshot(true, stats.operations == 0);
} }
lastStats = stats; lastStats = stats;
@ -360,11 +361,11 @@ public class ServiceSynchronize extends ServiceBase {
break; break;
case "oneshot_start": case "oneshot_start":
onOneshot(true); onOneshot(true, false);
break; break;
case "oneshot_end": case "oneshot_end":
onOneshot(false); onOneshot(false, false);
break; break;
case "watchdog": case "watchdog":
@ -449,8 +450,8 @@ public class ServiceSynchronize extends ServiceBase {
onReload(true, "reset"); onReload(true, "reset");
} }
private void onOneshot(boolean start) { private void onOneshot(boolean start, boolean idle) {
Log.i("Oneshot start=" + start); Log.i("Oneshot start=" + start + " idle=" + idle);
Intent alarm = new Intent(this, ServiceSynchronize.class); Intent alarm = new Intent(this, ServiceSynchronize.class);
alarm.setAction("oneshot_end"); alarm.setAction("oneshot_end");
@ -468,10 +469,11 @@ public class ServiceSynchronize extends ServiceBase {
if (start) { if (start) {
// Network events will manage the service // Network events will manage the service
long at = System.currentTimeMillis() + (idle ? ONESHOT_DURATION_IDLE : ONESHOT_DURATION_RUN);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot); am.set(AlarmManager.RTC_WAKEUP, at, piOneshot);
else else
am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ONESHOT_DURATION, piOneshot); am.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, at, piOneshot);
} else } else
onReload(true, "oneshot end"); onReload(true, "oneshot end");
} }

Loading…
Cancel
Save