Schedule poll after start of schedule

This commit is contained in:
M66B 2020-08-30 09:34:51 +02:00
parent dbefb3b76c
commit ceab470840
2 changed files with 23 additions and 23 deletions

View File

@ -2010,7 +2010,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
// Restore schedule // Restore schedule
schedule(context, true); schedule(context, false);
// Init service // Init service
int accounts = db.account().getSynchronizingAccounts().size(); int accounts = db.account().getSynchronizingAccounts().size();
@ -2037,6 +2037,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
am.cancel(pi); am.cancel(pi);
boolean poll; boolean poll;
Long at = null;
long[] schedule = getSchedule(context); long[] schedule = getSchedule(context);
if (schedule == null) if (schedule == null)
poll = true; poll = true;
@ -2053,17 +2054,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi); AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
if (sync) { if (sync & poll) {
long dt = Math.abs(schedule[0] - now) / 1000; at = now + 30 * 1000L;
long threshold = (schedule[1] - schedule[0]) / 5 / 1000; Log.i("Sync at schedule start=" + new Date(at));
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, at);
} }
static long[] getSchedule(Context context) { static long[] getSchedule(Context context) {

View File

@ -548,7 +548,7 @@ public class ServiceUI extends IntentService {
long now = new Date().getTime(); long now = new Date().getTime();
long[] schedule = ServiceSynchronize.getSchedule(this); long[] schedule = ServiceSynchronize.getSchedule(this);
boolean poll = (schedule == null || (now >= schedule[0] && now < schedule[1])); boolean poll = (schedule == null || (now >= schedule[0] && now < schedule[1]));
schedule(this, poll); schedule(this, poll, null);
} }
} }
@ -562,7 +562,7 @@ public class ServiceUI extends IntentService {
.setAction(account == null ? "sync" : "sync:" + account)); .setAction(account == null ? "sync" : "sync:" + account));
} }
static void schedule(Context context, boolean poll) { static void schedule(Context context, boolean poll, Long at) {
Intent intent = new Intent(context, ServiceUI.class); Intent intent = new Intent(context, ServiceUI.class);
intent.setAction("sync"); intent.setAction("sync");
intent.putExtra("reschedule", true); intent.putExtra("reschedule", true);
@ -572,20 +572,23 @@ public class ServiceUI extends IntentService {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(piSync); am.cancel(piSync);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (at == null) {
boolean enabled = prefs.getBoolean("enabled", true); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL); boolean enabled = prefs.getBoolean("enabled", true);
if (poll && enabled && pollInterval > 0) { int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL);
long now = new Date().getTime(); if (poll && enabled && pollInterval > 0) {
long interval = pollInterval * 60 * 1000L; long now = new Date().getTime();
long next = now + interval - now % interval + 30 * 1000L; long interval = pollInterval * 60 * 1000L;
if (next < now + interval / 5) long next = now + interval - now % interval + 30 * 1000L;
next += interval; if (next < now + interval / 5)
next += interval;
EntityLog.log(context, "Poll next=" + new Date(next)); EntityLog.log(context, "Poll next=" + new Date(next));
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piSync); AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piSync);
} }
} else
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, at, piSync);
} }
private static PendingIntent getBannerIntent(Context context) { private static PendingIntent getBannerIntent(Context context) {