rework UpdateService scheduling to work with data/interval prefs

This was doing a couple of things wrong:

* the scheduled job should always require a network, NONE doesn't work
* when the preferences change, it should cancel any scheduled job first,
  so that if the user chooses to disable auto-updates, that takes effect

closes #1474
closes #1451
closes #1457
This commit is contained in:
Hans-Christoph Steiner 2018-07-13 18:13:03 +02:00
parent 01abcc2f4d
commit 277cd3c992
2 changed files with 22 additions and 17 deletions

View File

@ -142,8 +142,9 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
lightWithDarkActionBar, // Obsolete
}
public static final long UPDATE_INTERVAL_DISABLED = Long.MAX_VALUE;
public static final long[] UPDATE_INTERVAL_VALUES = {
Long.MAX_VALUE, // never
UPDATE_INTERVAL_DISABLED,
DateUtils.WEEK_IN_MILLIS * 2,
DateUtils.WEEK_IN_MILLIS,
DateUtils.DAY_IN_MILLIS,
@ -211,13 +212,9 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
* Get the update interval in milliseconds.
*/
public long getUpdateInterval() {
if (getOverData() == OVER_NETWORK_NEVER && getOverWifi() == OVER_NETWORK_NEVER) {
return UPDATE_INTERVAL_VALUES[0];
} else {
int position = preferences.getInt(PREF_UPDATE_INTERVAL, IGNORED_I);
return UPDATE_INTERVAL_VALUES[position];
}
}
/**
* Migrate old preferences to new preferences. These need to be processed

View File

@ -138,6 +138,12 @@ public class UpdateService extends JobIntentService {
public static void schedule(Context context) {
Preferences prefs = Preferences.get();
long interval = prefs.getUpdateInterval();
int data = prefs.getOverData();
int wifi = prefs.getOverWifi();
boolean scheduleNewJob =
interval != Preferences.UPDATE_INTERVAL_DISABLED
&& data != Preferences.OVER_NETWORK_NEVER
&& wifi != Preferences.OVER_NETWORK_NEVER;
if (Build.VERSION.SDK_INT < 21) {
Intent intent = new Intent(context, UpdateService.class);
@ -145,7 +151,7 @@ public class UpdateService extends JobIntentService {
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pending);
if (interval > 0) {
if (scheduleNewJob) {
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 5000, interval, pending);
Utils.debugLog(TAG, "Update scheduler alarm set");
@ -163,17 +169,19 @@ public class UpdateService extends JobIntentService {
builder.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true);
}
int wifi = prefs.getOverWifi();
if (prefs.getOverData() == Preferences.OVER_NETWORK_ALWAYS) {
if (Build.VERSION.SDK_INT < 26 || wifi == Preferences.OVER_NETWORK_ALWAYS) {
if (data == Preferences.OVER_NETWORK_ALWAYS && wifi == Preferences.OVER_NETWORK_ALWAYS) {
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
} else {
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_METERED);
}
} else if (wifi == Preferences.OVER_NETWORK_ALWAYS) {
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
}
jobScheduler.cancel(JOB_ID);
if (scheduleNewJob) {
jobScheduler.schedule(builder.build());
Utils.debugLog(TAG, "Update scheduler alarm set");
} else {
Utils.debugLog(TAG, "Update scheduler alarm not set");
}
}
}