Sync at schedule start

This commit is contained in:
M66B 2020-08-29 11:26:47 +02:00
parent 813f8aae43
commit 627b235f93
1 changed files with 13 additions and 4 deletions

View File

@ -791,14 +791,14 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private void onAlarm(Intent intent) { private void onAlarm(Intent intent) {
Bundle command = new Bundle(); Bundle command = new Bundle();
schedule(this); schedule(this, true);
command.putString("name", "eval"); command.putString("name", "eval");
command.putBoolean("sync", true); command.putBoolean("sync", true);
liveAccountNetworkState.post(command); liveAccountNetworkState.post(command);
} }
private void onWatchdog(Intent intent) { private void onWatchdog(Intent intent) {
schedule(this); schedule(this, false);
networkCallback.onCapabilitiesChanged(null, null); networkCallback.onCapabilitiesChanged(null, null);
} }
@ -2009,7 +2009,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
// Restore schedule // Restore schedule
schedule(context); schedule(context, true);
// Init service // Init service
int accounts = db.account().getSynchronizingAccounts().size(); int accounts = db.account().getSynchronizingAccounts().size();
@ -2026,7 +2026,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
thread.start(); thread.start();
} }
private static void schedule(Context context) { private static void schedule(Context context, boolean sync) {
Intent intent = new Intent(context, ServiceSynchronize.class); Intent intent = new Intent(context, ServiceSynchronize.class);
intent.setAction("alarm"); intent.setAction("alarm");
PendingIntent pi = PendingIntentCompat.getForegroundService( PendingIntent pi = PendingIntentCompat.getForegroundService(
@ -2051,6 +2051,15 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i("Schedule poll=" + poll); Log.i("Schedule poll=" + poll);
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi); AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
if (sync) {
long dt = Math.abs(schedule[0] - now) / 1000;
long threshold = (schedule[1] - schedule[0]) / 5 / 1000;
if (dt < threshold) {
Log.i("Sync at schedule start dt=" + dt + " threshold=" + threshold);
ServiceUI.sync(context, null);
}
}
} }
ServiceUI.schedule(context, poll); ServiceUI.schedule(context, poll);