OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.components.background_task_scheduler; |
| 6 |
| 7 import android.content.SharedPreferences; |
| 8 |
| 9 import org.chromium.base.ContextUtils; |
| 10 |
| 11 import java.util.HashSet; |
| 12 import java.util.Set; |
| 13 |
| 14 /** |
| 15 * Class handling shared preference entries for BackgroundTaskScheduler. |
| 16 */ |
| 17 public class BackgroundTaskSchedulerPrefs { |
| 18 private static final String TAG = "BTSPrefs"; |
| 19 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_APP_VERSION = "bts_last_app_version"; |
| 22 private static final String ENTRY_SEPARATOR = ":"; |
| 23 |
| 24 /** Adds a task to scheduler's preferences, so that it can be rescheduled wi
th OS upgrade. */ |
| 25 public static void addScheduledTask(TaskInfo taskInfo) { |
| 26 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); |
| 27 Set<String> scheduledTasks = |
| 28 prefs.getStringSet(KEY_SCHEDULED_TASKS, new HashSet<String>(1)); |
| 29 String prefsEntry = toSharedPreferenceEntry(taskInfo); |
| 30 if (scheduledTasks.contains(prefsEntry)) return; |
| 31 |
| 32 // Set returned from getStringSet() should not be modified. |
| 33 scheduledTasks = new HashSet<>(scheduledTasks); |
| 34 scheduledTasks.add(prefsEntry); |
| 35 updateScheduledTasks(prefs, scheduledTasks); |
| 36 } |
| 37 |
| 38 /** Removes a task from scheduler's preferences. */ |
| 39 public static void removeScheduledTask(int taskId) { |
| 40 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); |
| 41 Set<String> scheduledTasks = prefs.getStringSet(KEY_SCHEDULED_TASKS, new
HashSet<String>()); |
| 42 |
| 43 String entryToRemove = null; |
| 44 String taskSuffix = ENTRY_SEPARATOR + taskId; |
| 45 for (String entry : scheduledTasks) { |
| 46 if (entry.endsWith(taskSuffix)) { |
| 47 entryToRemove = entry; |
| 48 break; |
| 49 } |
| 50 } |
| 51 |
| 52 // Entry matching taskId was not found. |
| 53 if (entryToRemove == null) return; |
| 54 |
| 55 // Set returned from getStringSet() should not be modified. |
| 56 scheduledTasks = new HashSet<>(scheduledTasks); |
| 57 scheduledTasks.remove(entryToRemove); |
| 58 updateScheduledTasks(prefs, scheduledTasks); |
| 59 } |
| 60 |
| 61 /** Gets a set of scheduled task class names. */ |
| 62 public static Set<String> getScheduledTasks() { |
| 63 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); |
| 64 Set<String> prefsEntries = prefs.getStringSet(KEY_SCHEDULED_TASKS, new H
ashSet<String>(1)); |
| 65 Set<String> scheduledTasksClassNames = new HashSet<>(prefsEntries.size()
); |
| 66 for (String entry : prefsEntries) { |
| 67 String[] entryParts = entry.split(ENTRY_SEPARATOR); |
| 68 if (entryParts.length != 2 || entryParts[0] == null || entryParts[0]
.isEmpty()) { |
| 69 continue; |
| 70 } |
| 71 scheduledTasksClassNames.add(entryParts[0]); |
| 72 } |
| 73 |
| 74 return scheduledTasksClassNames; |
| 75 } |
| 76 |
| 77 private static void updateScheduledTasks(SharedPreferences prefs, Set<String
> tasks) { |
| 78 SharedPreferences.Editor editor = prefs.edit(); |
| 79 editor.putStringSet(KEY_SCHEDULED_TASKS, tasks); |
| 80 editor.apply(); |
| 81 } |
| 82 |
| 83 /** |
| 84 * Converts a task info to a shared preference entry in the format: |
| 85 * BACKGROUND_TASK_CLASS_NAME:TASK_ID. |
| 86 * Task ID is necessary to be able to remove the entries from the shared pre
ferences. |
| 87 */ |
| 88 private static String toSharedPreferenceEntry(TaskInfo taskInfo) { |
| 89 return taskInfo.getBackgroundTaskClass().getName() + ENTRY_SEPARATOR + t
askInfo.getTaskId(); |
| 90 } |
| 91 } |
OLD | NEW |