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

Side by Side Diff: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskSchedulerPrefs.java

Issue 2819703002: [Android] Implements OS upgrade check and rescheduling (Closed)
Patch Set: Calling upgrade task from DeferredStartupHandler Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.components.background_task_scheduler; 5 package org.chromium.components.background_task_scheduler;
6 6
7 import android.content.SharedPreferences; 7 import android.content.SharedPreferences;
8 import android.os.Build;
8 9
9 import org.chromium.base.ContextUtils; 10 import org.chromium.base.ContextUtils;
10 11
11 import java.util.HashSet; 12 import java.util.HashSet;
12 import java.util.Set; 13 import java.util.Set;
13 14
14 /** 15 /**
15 * Class handling shared preference entries for BackgroundTaskScheduler. 16 * Class handling shared preference entries for BackgroundTaskScheduler.
16 */ 17 */
17 public class BackgroundTaskSchedulerPrefs { 18 public class BackgroundTaskSchedulerPrefs {
18 private static final String TAG = "BTSPrefs"; 19 private static final String TAG = "BTSPrefs";
19 private static final String KEY_SCHEDULED_TASKS = "bts_scheduled_tasks"; 20 private static final String KEY_SCHEDULED_TASKS = "bts_scheduled_tasks";
20 private static final String KEY_LAST_OS_VERSION = "bts_last_os_version"; 21 private static final String KEY_LAST_SDK_VERSION = "bts_last_sdk_version";
21 private static final String KEY_LAST_APP_VERSION = "bts_last_app_version"; 22
22 private static final String ENTRY_SEPARATOR = ":"; 23 /**
24 * Class abstracting conversions between a string kept in shared preferences and actual values
25 * held there.
26 */
27 private static class ScheduledTaskPreferenceEntry {
28 private static final String ENTRY_SEPARATOR = ":";
29 private String mBackgroundTaskClass;
30 private int mTaskId;
31
32 /** Creates a scheduled task shared preference entry from task info. */
33 public static ScheduledTaskPreferenceEntry createForTaskInfo(TaskInfo ta skInfo) {
34 return new ScheduledTaskPreferenceEntry(
35 taskInfo.getBackgroundTaskClass().getName(), taskInfo.getTas kId());
36 }
37
38 /**
39 * Parses a preference entry from input string.
40 *
41 * @param entry An input string to parse.
42 * @return A parsed entry or null if the input is not valid.
43 */
44 public static ScheduledTaskPreferenceEntry parseEntry(String entry) {
45 String[] entryParts = entry.split(ENTRY_SEPARATOR);
46 if (entryParts.length != 2 || entryParts[0].isEmpty() || entryParts[ 1].isEmpty()) {
47 return null;
48 }
49 int taskId = 0;
50 try {
51 taskId = Integer.parseInt(entryParts[1]);
52 } catch (NumberFormatException e) {
53 return null;
54 }
55 return new ScheduledTaskPreferenceEntry(entryParts[0], taskId);
56 }
57
58 public ScheduledTaskPreferenceEntry(String className, int taskId) {
59 mBackgroundTaskClass = className;
60 mTaskId = taskId;
61 }
62
63 /**
64 * Converts a task info to a shared preference entry in the format:
65 * BACKGROUND_TASK_CLASS_NAME:TASK_ID.
66 */
67 public String toString() {
68 return mBackgroundTaskClass + ENTRY_SEPARATOR + mTaskId;
69 }
70
71 /** Gets the name of background task class in this entry. */
72 public String getBackgroundTaskClass() {
73 return mBackgroundTaskClass;
74 }
75
76 /** Gets the ID of the task in this entry. */
77 public int getTaskId() {
78 return mTaskId;
79 }
80 }
23 81
24 /** Adds a task to scheduler's preferences, so that it can be rescheduled wi th OS upgrade. */ 82 /** Adds a task to scheduler's preferences, so that it can be rescheduled wi th OS upgrade. */
25 public static void addScheduledTask(TaskInfo taskInfo) { 83 public static void addScheduledTask(TaskInfo taskInfo) {
26 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); 84 SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
27 Set<String> scheduledTasks = 85 Set<String> scheduledTasks =
28 prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1)); 86 prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1));
29 String prefsEntry = toSharedPreferenceEntry(taskInfo); 87 String prefsEntry = ScheduledTaskPreferenceEntry.createForTaskInfo(taskI nfo).toString();
30 if (scheduledTasks.contains(prefsEntry)) return; 88 if (scheduledTasks.contains(prefsEntry)) return;
31 89
32 // Set returned from getStringSet() should not be modified. 90 // Set returned from getStringSet() should not be modified.
33 scheduledTasks = new HashSet<>(scheduledTasks); 91 scheduledTasks = new HashSet<>(scheduledTasks);
34 scheduledTasks.add(prefsEntry); 92 scheduledTasks.add(prefsEntry);
35 updateScheduledTasks(prefs, scheduledTasks); 93 updateScheduledTasks(prefs, scheduledTasks);
36 } 94 }
37 95
38 /** Removes a task from scheduler's preferences. */ 96 /** Removes a task from scheduler's preferences. */
39 public static void removeScheduledTask(int taskId) { 97 public static void removeScheduledTask(int taskId) {
40 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); 98 SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
41 Set<String> scheduledTasks = prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>()); 99 Set<String> scheduledTasks = getScheduledTaskEntries(prefs);
42 100
43 String entryToRemove = null; 101 String entryToRemove = null;
44 String taskSuffix = ENTRY_SEPARATOR + taskId;
45 for (String entry : scheduledTasks) { 102 for (String entry : scheduledTasks) {
46 if (entry.endsWith(taskSuffix)) { 103 ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.p arseEntry(entry);
104 if (parsed != null && parsed.getTaskId() == taskId) {
47 entryToRemove = entry; 105 entryToRemove = entry;
48 break; 106 break;
49 } 107 }
50 } 108 }
51 109
52 // Entry matching taskId was not found. 110 // Entry matching taskId was not found.
53 if (entryToRemove == null) return; 111 if (entryToRemove == null) return;
54 112
55 // Set returned from getStringSet() should not be modified. 113 // Set returned from getStringSet() should not be modified.
56 scheduledTasks = new HashSet<>(scheduledTasks); 114 scheduledTasks = new HashSet<>(scheduledTasks);
57 scheduledTasks.remove(entryToRemove); 115 scheduledTasks.remove(entryToRemove);
58 updateScheduledTasks(prefs, scheduledTasks); 116 updateScheduledTasks(prefs, scheduledTasks);
59 } 117 }
60 118
61 /** Gets a set of scheduled task class names. */ 119 /** Gets a set of scheduled task class names. */
62 public static Set<String> getScheduledTasks() { 120 public static Set<String> getScheduledTasks() {
63 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); 121 SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
64 Set<String> prefsEntries = prefs.getStringSet(KEY_SCHEDULED_TASKS, new H ashSet<String>(1)); 122 Set<String> scheduledTask = getScheduledTaskEntries(prefs);
65 Set<String> scheduledTasksClassNames = new HashSet<>(prefsEntries.size() ); 123 Set<String> scheduledTasksClassNames = new HashSet<>(scheduledTask.size( ));
66 for (String entry : prefsEntries) { 124 for (String entry : scheduledTask) {
67 String[] entryParts = entry.split(ENTRY_SEPARATOR); 125 ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.p arseEntry(entry);
68 if (entryParts.length != 2 || entryParts[0] == null || entryParts[0] .isEmpty()) { 126 if (parsed != null) {
69 continue; 127 scheduledTasksClassNames.add(parsed.getBackgroundTaskClass());
70 } 128 }
71 scheduledTasksClassNames.add(entryParts[0]);
72 } 129 }
73 130
74 return scheduledTasksClassNames; 131 return scheduledTasksClassNames;
75 } 132 }
76 133
134 /** Gets a set of scheduled task IDs. */
135 public static Set<Integer> getScheduledTaskIds() {
136 SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
137 Set<String> scheduledTasks = getScheduledTaskEntries(prefs);
138 Set<Integer> scheduledTaskIds = new HashSet<>(scheduledTasks.size());
139 for (String entry : scheduledTasks) {
140 ScheduledTaskPreferenceEntry parsed = ScheduledTaskPreferenceEntry.p arseEntry(entry);
141 if (parsed != null) {
142 scheduledTaskIds.add(parsed.getTaskId());
143 }
144 }
145 return scheduledTaskIds;
146 }
147
77 /** Removes all scheduled tasks from shared preferences store. */ 148 /** Removes all scheduled tasks from shared preferences store. */
78 public static void removeAllTasks() { 149 public static void removeAllTasks() {
79 ContextUtils.getAppSharedPreferences().edit().remove(KEY_SCHEDULED_TASKS ).apply(); 150 ContextUtils.getAppSharedPreferences().edit().remove(KEY_SCHEDULED_TASKS ).apply();
80 } 151 }
81 152
153 /** Gets the last SDK version on which this instance ran. Defaults to curren t SDK version. */
154 public static int getLastSdkVersion() {
155 return ContextUtils.getAppSharedPreferences().getInt(
156 KEY_LAST_SDK_VERSION, Build.VERSION.SDK_INT);
157 }
158
159 /** Gets the last SDK version on which this instance ran. */
160 public static void setLastSdkVersion(int sdkVersion) {
161 ContextUtils.getAppSharedPreferences()
162 .edit()
163 .putInt(KEY_LAST_SDK_VERSION, sdkVersion)
164 .apply();
165 }
166
82 private static void updateScheduledTasks(SharedPreferences prefs, Set<String > tasks) { 167 private static void updateScheduledTasks(SharedPreferences prefs, Set<String > tasks) {
83 SharedPreferences.Editor editor = prefs.edit(); 168 SharedPreferences.Editor editor = prefs.edit();
84 editor.putStringSet(KEY_SCHEDULED_TASKS, tasks); 169 editor.putStringSet(KEY_SCHEDULED_TASKS, tasks);
85 editor.apply(); 170 editor.apply();
86 } 171 }
87 172
88 /** 173 /** Gets the entries for scheduled tasks from shared preferences. */
89 * Converts a task info to a shared preference entry in the format: 174 private static Set<String> getScheduledTaskEntries(SharedPreferences prefs) {
90 * BACKGROUND_TASK_CLASS_NAME:TASK_ID. 175 return prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1));
91 * Task ID is necessary to be able to remove the entries from the shared pre ferences.
92 */
93 private static String toSharedPreferenceEntry(TaskInfo taskInfo) {
94 return taskInfo.getBackgroundTaskClass().getName() + ENTRY_SEPARATOR + t askInfo.getTaskId();
95 } 176 }
96 } 177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698