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:
		
							parent
							
								
									01abcc2f4d
								
							
						
					
					
						commit
						277cd3c992
					
				| @ -142,8 +142,9 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | |||||||
|         lightWithDarkActionBar, // Obsolete |         lightWithDarkActionBar, // Obsolete | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public static final long UPDATE_INTERVAL_DISABLED = Long.MAX_VALUE; | ||||||
|     public static final long[] UPDATE_INTERVAL_VALUES = { |     public static final long[] UPDATE_INTERVAL_VALUES = { | ||||||
|             Long.MAX_VALUE,  // never |             UPDATE_INTERVAL_DISABLED, | ||||||
|             DateUtils.WEEK_IN_MILLIS * 2, |             DateUtils.WEEK_IN_MILLIS * 2, | ||||||
|             DateUtils.WEEK_IN_MILLIS, |             DateUtils.WEEK_IN_MILLIS, | ||||||
|             DateUtils.DAY_IN_MILLIS, |             DateUtils.DAY_IN_MILLIS, | ||||||
| @ -211,12 +212,8 @@ public final class Preferences implements SharedPreferences.OnSharedPreferenceCh | |||||||
|      * Get the update interval in milliseconds. |      * Get the update interval in milliseconds. | ||||||
|      */ |      */ | ||||||
|     public long getUpdateInterval() { |     public long getUpdateInterval() { | ||||||
|         if (getOverData() == OVER_NETWORK_NEVER && getOverWifi() == OVER_NETWORK_NEVER) { |         int position = preferences.getInt(PREF_UPDATE_INTERVAL, IGNORED_I); | ||||||
|             return UPDATE_INTERVAL_VALUES[0]; |         return UPDATE_INTERVAL_VALUES[position]; | ||||||
|         } else { |  | ||||||
|             int position = preferences.getInt(PREF_UPDATE_INTERVAL, IGNORED_I); |  | ||||||
|             return UPDATE_INTERVAL_VALUES[position]; |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|  | |||||||
| @ -138,6 +138,12 @@ public class UpdateService extends JobIntentService { | |||||||
|     public static void schedule(Context context) { |     public static void schedule(Context context) { | ||||||
|         Preferences prefs = Preferences.get(); |         Preferences prefs = Preferences.get(); | ||||||
|         long interval = prefs.getUpdateInterval(); |         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) { |         if (Build.VERSION.SDK_INT < 21) { | ||||||
|             Intent intent = new Intent(context, UpdateService.class); |             Intent intent = new Intent(context, UpdateService.class); | ||||||
| @ -145,7 +151,7 @@ public class UpdateService extends JobIntentService { | |||||||
| 
 | 
 | ||||||
|             AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); |             AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); | ||||||
|             alarm.cancel(pending); |             alarm.cancel(pending); | ||||||
|             if (interval > 0) { |             if (scheduleNewJob) { | ||||||
|                 alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, |                 alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, | ||||||
|                         SystemClock.elapsedRealtime() + 5000, interval, pending); |                         SystemClock.elapsedRealtime() + 5000, interval, pending); | ||||||
|                 Utils.debugLog(TAG, "Update scheduler alarm set"); |                 Utils.debugLog(TAG, "Update scheduler alarm set"); | ||||||
| @ -163,17 +169,19 @@ public class UpdateService extends JobIntentService { | |||||||
|                 builder.setRequiresBatteryNotLow(true) |                 builder.setRequiresBatteryNotLow(true) | ||||||
|                         .setRequiresStorageNotLow(true); |                         .setRequiresStorageNotLow(true); | ||||||
|             } |             } | ||||||
|             int wifi = prefs.getOverWifi(); |             if (data == Preferences.OVER_NETWORK_ALWAYS && wifi == Preferences.OVER_NETWORK_ALWAYS) { | ||||||
|             if (prefs.getOverData() == Preferences.OVER_NETWORK_ALWAYS) { |                 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); | ||||||
|                 if (Build.VERSION.SDK_INT < 26 || wifi == Preferences.OVER_NETWORK_ALWAYS) { |             } else { | ||||||
|                     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); |                 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); | ||||||
|             } |             } | ||||||
|             jobScheduler.schedule(builder.build()); | 
 | ||||||
|  |             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"); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Hans-Christoph Steiner
						Hans-Christoph Steiner