| Index: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java
|
| diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java
|
| index 1a71d34d43da3b7f25b8f89722f520f36c5d4896..5f93cf804551d03e2c09e3966555190a3ad1daf0 100644
|
| --- a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java
|
| +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java
|
| @@ -5,6 +5,7 @@
|
| package org.chromium.components.background_task_scheduler;
|
|
|
| import android.content.SharedPreferences;
|
| +import android.os.Build;
|
|
|
| import org.chromium.base.ContextUtils;
|
|
|
| @@ -17,16 +18,73 @@ import java.util.Set;
|
| public class BackgroundTaskSchedulerPrefs {
|
| private static final String TAG = "BTSPrefs";
|
| private static final String KEY_SCHEDULED_TASKS = "bts_scheduled_tasks";
|
| - private static final String KEY_LAST_OS_VERSION = "bts_last_os_version";
|
| - private static final String KEY_LAST_APP_VERSION = "bts_last_app_version";
|
| - private static final String ENTRY_SEPARATOR = ":";
|
| + private static final String KEY_LAST_SDK_VERSION = "bts_last_sdk_version";
|
| +
|
| + /**
|
| + * Class abstracting conversions between a string kept in shared preferences and actual values
|
| + * held there.
|
| + */
|
| + private static class ScheduledTaskPreferenceEntry {
|
| + private static final String ENTRY_SEPARATOR = ":";
|
| + private String mBackgroundTaskClass;
|
| + private int mTaskId;
|
| +
|
| + /** Creates a scheduled task shared preference entry from task info. */
|
| + public static ScheduledTaskPreferenceEntry createForTaskInfo(TaskInfo taskInfo) {
|
| + return new ScheduledTaskPreferenceEntry(
|
| + taskInfo.getBackgroundTaskClass().getName(), taskInfo.getTaskId());
|
| + }
|
| +
|
| + /**
|
| + * Parses a preference entry from input string.
|
| + *
|
| + * @param entry An input string to parse.
|
| + * @return A parsed entry or null if the input is not valid.
|
| + */
|
| + public static ScheduledTaskPreferenceEntry parseEntry(String entry) {
|
| + String[] entryParts = entry.split(ENTRY_SEPARATOR);
|
| + if (entryParts.length != 2 || entryParts[0].isEmpty() || entryParts[1].isEmpty()) {
|
| + return null;
|
| + }
|
| + int taskId = 0;
|
| + try {
|
| + taskId = Integer.parseInt(entryParts[1]);
|
| + } catch (NumberFormatException e) {
|
| + return null;
|
| + }
|
| + return new ScheduledTaskPreferenceEntry(entryParts[0], taskId);
|
| + }
|
| +
|
| + public ScheduledTaskPreferenceEntry(String className, int taskId) {
|
| + mBackgroundTaskClass = className;
|
| + mTaskId = taskId;
|
| + }
|
| +
|
| + /**
|
| + * Converts a task info to a shared preference entry in the format:
|
| + * BACKGROUND_TASK_CLASS_NAME:TASK_ID.
|
| + */
|
| + public String toString() {
|
| + return mBackgroundTaskClass + ENTRY_SEPARATOR + mTaskId;
|
| + }
|
| +
|
| + /** Gets the name of background task class in this entry. */
|
| + public String getBackgroundTaskClass() {
|
| + return mBackgroundTaskClass;
|
| + }
|
| +
|
| + /** Gets the ID of the task in this entry. */
|
| + public int getTaskId() {
|
| + return mTaskId;
|
| + }
|
| + }
|
|
|
| /** Adds a task to scheduler's preferences, so that it can be rescheduled with OS upgrade. */
|
| public static void addScheduledTask(TaskInfo taskInfo) {
|
| SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
|
| Set<String> scheduledTasks =
|
| prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1));
|
| - String prefsEntry = toSharedPreferenceEntry(taskInfo);
|
| + String prefsEntry = ScheduledTaskPreferenceEntry.createForTaskInfo(taskInfo).toString();
|
| if (scheduledTasks.contains(prefsEntry)) return;
|
|
|
| // Set returned from getStringSet() should not be modified.
|
| @@ -38,12 +96,12 @@ public class BackgroundTaskSchedulerPrefs {
|
| /** Removes a task from scheduler's preferences. */
|
| public static void removeScheduledTask(int taskId) {
|
| SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
|
| - Set<String> scheduledTasks = prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>());
|
| + Set<String> scheduledTasks = getScheduledTaskEntries(prefs);
|
|
|
| String entryToRemove = null;
|
| - String taskSuffix = ENTRY_SEPARATOR + taskId;
|
| for (String entry : scheduledTasks) {
|
| - if (entry.endsWith(taskSuffix)) {
|
| + ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.parseEntry(entry);
|
| + if (parsed != null && parsed.getTaskId() == taskId) {
|
| entryToRemove = entry;
|
| break;
|
| }
|
| @@ -61,36 +119,59 @@ public class BackgroundTaskSchedulerPrefs {
|
| /** Gets a set of scheduled task class names. */
|
| public static Set<String> getScheduledTasks() {
|
| SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
|
| - Set<String> prefsEntries = prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1));
|
| - Set<String> scheduledTasksClassNames = new HashSet<>(prefsEntries.size());
|
| - for (String entry : prefsEntries) {
|
| - String[] entryParts = entry.split(ENTRY_SEPARATOR);
|
| - if (entryParts.length != 2 || entryParts[0] == null || entryParts[0].isEmpty()) {
|
| - continue;
|
| + Set<String> scheduledTask = getScheduledTaskEntries(prefs);
|
| + Set<String> scheduledTasksClassNames = new HashSet<>(scheduledTask.size());
|
| + for (String entry : scheduledTask) {
|
| + ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.parseEntry(entry);
|
| + if (parsed != null) {
|
| + scheduledTasksClassNames.add(parsed.getBackgroundTaskClass());
|
| }
|
| - scheduledTasksClassNames.add(entryParts[0]);
|
| }
|
|
|
| return scheduledTasksClassNames;
|
| }
|
|
|
| + /** Gets a set of scheduled task IDs. */
|
| + public static Set<Integer> getScheduledTaskIds() {
|
| + SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
|
| + Set<String> scheduledTasks = getScheduledTaskEntries(prefs);
|
| + Set<Integer> scheduledTaskIds = new HashSet<>(scheduledTasks.size());
|
| + for (String entry : scheduledTasks) {
|
| + ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.parseEntry(entry);
|
| + if (parsed != null) {
|
| + scheduledTaskIds.add(parsed.getTaskId());
|
| + }
|
| + }
|
| + return scheduledTaskIds;
|
| + }
|
| +
|
| /** Removes all scheduled tasks from shared preferences store. */
|
| public static void removeAllTasks() {
|
| ContextUtils.getAppSharedPreferences().edit().remove(KEY_SCHEDULED_TASKS).apply();
|
| }
|
|
|
| + /** Gets the last SDK version on which this instance ran. Defaults to current SDK version. */
|
| + public static int getLastSdkVersion() {
|
| + return ContextUtils.getAppSharedPreferences().getInt(
|
| + KEY_LAST_SDK_VERSION, Build.VERSION.SDK_INT);
|
| + }
|
| +
|
| + /** Gets the last SDK version on which this instance ran. */
|
| + public static void setLastSdkVersion(int sdkVersion) {
|
| + ContextUtils.getAppSharedPreferences()
|
| + .edit()
|
| + .putInt(KEY_LAST_SDK_VERSION, sdkVersion)
|
| + .apply();
|
| + }
|
| +
|
| private static void updateScheduledTasks(SharedPreferences prefs, Set<String> tasks) {
|
| SharedPreferences.Editor editor = prefs.edit();
|
| editor.putStringSet(KEY_SCHEDULED_TASKS, tasks);
|
| editor.apply();
|
| }
|
|
|
| - /**
|
| - * Converts a task info to a shared preference entry in the format:
|
| - * BACKGROUND_TASK_CLASS_NAME:TASK_ID.
|
| - * Task ID is necessary to be able to remove the entries from the shared preferences.
|
| - */
|
| - private static String toSharedPreferenceEntry(TaskInfo taskInfo) {
|
| - return taskInfo.getBackgroundTaskClass().getName() + ENTRY_SEPARATOR + taskInfo.getTaskId();
|
| + /** Gets the entries for scheduled tasks from shared preferences. */
|
| + private static Set<String> getScheduledTaskEntries(SharedPreferences prefs) {
|
| + return prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1));
|
| }
|
| }
|
|
|