specify network type to JobScheduler based on new wifi/data prefs

#1381
This commit is contained in:
Hans-Christoph Steiner 2018-04-24 13:40:59 +02:00
parent f8225f3122
commit 5946d198b0
2 changed files with 22 additions and 9 deletions

View File

@ -367,11 +367,11 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh
return false;
}
private int getOverWifi() {
public int getOverWifi() {
return preferences.getInt(PREF_OVER_WIFI, DEFAULT_OVER_WIFI);
}
private int getOverData() {
public int getOverData() {
return preferences.getInt(PREF_OVER_DATA, DEFAULT_OVER_DATA);
}

View File

@ -130,7 +130,8 @@ public class UpdateService extends IntentService {
* @see <a href="https://developer.android.com/about/versions/android-5.0.html#Power">Project Volta: Scheduling jobs</a>
*/
public static void schedule(Context context) {
long interval = Preferences.get().getUpdateInterval();
Preferences prefs = Preferences.get();
long interval = prefs.getUpdateInterval();
if (Build.VERSION.SDK_INT < 21) {
Intent intent = new Intent(context, UpdateService.class);
@ -148,13 +149,25 @@ public class UpdateService extends IntentService {
} else {
Utils.debugLog(TAG, "Using android-21 JobScheduler for updates");
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.cancelAll();
ComponentName componentName = new ComponentName(context, UpdateJobService.class);
JobInfo task = new JobInfo.Builder(0xfedcba, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setOverrideDeadline(interval)
.build();
jobScheduler.schedule(task);
JobInfo.Builder builder = new JobInfo.Builder(0xfedcba, componentName)
.setRequiresDeviceIdle(true)
.setPeriodic(interval);
if (Build.VERSION.SDK_INT >= 26) {
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) {
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.schedule(builder.build());
}
}