Fixed polling

pull/177/head
M66B 6 years ago
parent 0451d7f176
commit 0859a88ee4

@ -136,7 +136,7 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
@Override @Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("enabled", checked).apply(); prefs.edit().putBoolean("enabled", checked).apply();
ServiceSynchronize.eval(getContext(), "enabled=" + checked); ServiceSynchronize.reschedule(getContext());
} }
}); });

@ -1804,25 +1804,25 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi); am.cancel(pi);
boolean enabled; boolean poll;
long[] schedule = getSchedule(context); long[] schedule = getSchedule(context);
if (schedule == null) if (schedule == null)
enabled = false; poll = true;
else { else {
long now = new Date().getTime(); long now = new Date().getTime();
long next = (now < schedule[0] ? schedule[0] : schedule[1]); long next = (now < schedule[0] ? schedule[0] : schedule[1]);
enabled = (now >= schedule[0] && now < schedule[1]); poll = (now >= schedule[0] && now < schedule[1]);
Log.i("Schedule now=" + new Date(now)); Log.i("Schedule now=" + new Date(now));
Log.i("Schedule start=" + new Date(schedule[0])); Log.i("Schedule start=" + new Date(schedule[0]));
Log.i("Schedule end=" + new Date(schedule[1])); Log.i("Schedule end=" + new Date(schedule[1]));
Log.i("Schedule next=" + new Date(next)); Log.i("Schedule next=" + new Date(next));
Log.i("Schedule enabled=" + enabled); Log.i("Schedule poll=" + poll);
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi); AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
} }
ServiceUI.schedule(context, enabled); ServiceUI.schedule(context, poll);
} }
static long[] getSchedule(Context context) { static long[] getSchedule(Context context) {

@ -468,8 +468,8 @@ public class ServiceUI extends IntentService {
if (reschedule) { if (reschedule) {
long now = new Date().getTime(); long now = new Date().getTime();
long[] schedule = ServiceSynchronize.getSchedule(this); long[] schedule = ServiceSynchronize.getSchedule(this);
boolean enabled = (schedule != null && now >= schedule[0] && now < schedule[1]); boolean poll = (schedule == null || (now >= schedule[0] && now < schedule[1]));
schedule(this, enabled); schedule(this, poll);
} }
} }
@ -483,7 +483,7 @@ public class ServiceUI extends IntentService {
.setAction(account == null ? "sync" : "sync:" + account)); .setAction(account == null ? "sync" : "sync:" + account));
} }
static void schedule(Context context, boolean enabled) { static void schedule(Context context, boolean poll) {
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);
@ -494,8 +494,9 @@ public class ServiceUI extends IntentService {
am.cancel(piSync); am.cancel(piSync);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL); int pollInterval = prefs.getInt("poll_interval", ServiceSynchronize.DEFAULT_POLL_INTERVAL);
if (enabled && pollInterval > 0) { if (poll && enabled && pollInterval > 0) {
long now = new Date().getTime(); long now = new Date().getTime();
long interval = pollInterval * 60 * 1000L; long interval = pollInterval * 60 * 1000L;
long next = now + interval - now % interval; long next = now + interval - now % interval;

Loading…
Cancel
Save