Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(763)

Unified Diff: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java

Issue 2819703002: [Android] Implements OS upgrade check and rescheduling (Closed)
Patch Set: Addressing final CR feedback Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
index 2fd58ac3812c438dffc583864270e6bc83ca27ca..e690273845cec8b60d884414c42f4836b2c5325b 100644
--- a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
+++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
@@ -95,6 +95,41 @@ public class BackgroundTaskScheduler {
mSchedulerDelegate.cancel(context, taskId);
}
+ /**
+ * Checks whether OS was upgraded and triggers rescheduling if it is necessary.
+ * Rescheduling is necessary if type of background task scheduler delegate is different for a
+ * new version of the OS.
+ *
+ * @param context the current context.
+ */
+ public void checkForOSUpgrade(Context context) {
+ int oldSdkInt = BackgroundTaskSchedulerPrefs.getLastSdkVersion();
+ int newSdkInt = Build.VERSION.SDK_INT;
+ // No OS upgrade detected.
+ if (oldSdkInt == newSdkInt) return;
+
+ // Save the current SDK version to preferences.
+ BackgroundTaskSchedulerPrefs.setLastSdkVersion(newSdkInt);
+
+ // Check for OS upgrades forcing delegate change or "just in case" rescheduling.
+ if (!osUpgradeChangesDelegateType(oldSdkInt, newSdkInt)) return;
+
+ // Explicitly create and invoke old delegate type to cancel all scheduled tasks.
+ // All preference entries are kept until reschedule call, which removes then then.
+ BackgroundTaskSchedulerDelegate oldDelegate =
+ BackgroundTaskSchedulerFactory.getSchedulerDelegateForSdk(oldSdkInt);
+ Set<Integer> scheduledTaskIds = BackgroundTaskSchedulerPrefs.getScheduledTaskIds();
+ for (int taskId : scheduledTaskIds) {
+ oldDelegate.cancel(context, taskId);
+ }
+
+ reschedule(context);
+ }
+
+ /**
+ * Reschedules all the tasks currently scheduler through BackgroundTaskSheduler.
+ * @param context the current context.
+ */
public void reschedule(Context context) {
Set<String> scheduledTasksClassNames = BackgroundTaskSchedulerPrefs.getScheduledTasks();
BackgroundTaskSchedulerPrefs.removeAllTasks();
@@ -108,4 +143,8 @@ public class BackgroundTaskScheduler {
task.reschedule(context);
}
}
+
+ private boolean osUpgradeChangesDelegateType(int oldSdkInt, int newSdkInt) {
+ return oldSdkInt < Build.VERSION_CODES.M && newSdkInt >= Build.VERSION_CODES.M;
+ }
}

Powered by Google App Engine
This is Rietveld 408576698