OLD | NEW |
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.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.os.Build; | 9 import android.os.Build; |
10 import android.support.annotation.Nullable; | 10 import android.support.annotation.Nullable; |
11 | 11 |
12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
13 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
14 | 14 |
15 import java.lang.reflect.Constructor; | 15 import java.lang.reflect.Constructor; |
| 16 import java.util.Set; |
16 | 17 |
17 /** | 18 /** |
18 * A BackgroundTaskScheduler which is used to schedule jobs that run in the back
ground. | 19 * A BackgroundTaskScheduler which is used to schedule jobs that run in the back
ground. |
19 * It is backed by system APIs ({@link android.app.job.JobScheduler}) on newer p
latforms | 20 * It is backed by system APIs ({@link android.app.job.JobScheduler}) on newer p
latforms |
20 * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older pl
atforms. | 21 * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older pl
atforms. |
21 * | 22 * |
22 * To get an instance of this class, use {@link BackgroundTaskSchedulerFactory#g
etScheduler()}. | 23 * To get an instance of this class, use {@link BackgroundTaskSchedulerFactory#g
etScheduler()}. |
23 */ | 24 */ |
24 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | 25 @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
25 public class BackgroundTaskScheduler { | 26 public class BackgroundTaskScheduler { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 * Schedules a background task. See {@link TaskInfo} for information on what
types of tasks that | 69 * Schedules a background task. See {@link TaskInfo} for information on what
types of tasks that |
69 * can be scheduled. | 70 * can be scheduled. |
70 * | 71 * |
71 * @param context the current context. | 72 * @param context the current context. |
72 * @param taskInfo the information about the task to be scheduled. | 73 * @param taskInfo the information about the task to be scheduled. |
73 * @return true if the schedule operation succeeded, and false otherwise. | 74 * @return true if the schedule operation succeeded, and false otherwise. |
74 * @see TaskInfo | 75 * @see TaskInfo |
75 */ | 76 */ |
76 public boolean schedule(Context context, TaskInfo taskInfo) { | 77 public boolean schedule(Context context, TaskInfo taskInfo) { |
77 ThreadUtils.assertOnUiThread(); | 78 ThreadUtils.assertOnUiThread(); |
78 BackgroundTaskSchedulerPrefs.addScheduledTask(taskInfo); | 79 boolean success = mSchedulerDelegate.schedule(context, taskInfo); |
79 return mSchedulerDelegate.schedule(context, taskInfo); | 80 if (success) { |
| 81 BackgroundTaskSchedulerPrefs.addScheduledTask(taskInfo); |
| 82 } |
| 83 return success; |
80 } | 84 } |
81 | 85 |
82 /** | 86 /** |
83 * Cancels the task specified by the task ID. | 87 * Cancels the task specified by the task ID. |
84 * | 88 * |
85 * @param context the current context. | 89 * @param context the current context. |
86 * @param taskId the ID of the task to cancel. See {@link TaskIds} for a lis
t. | 90 * @param taskId the ID of the task to cancel. See {@link TaskIds} for a lis
t. |
87 */ | 91 */ |
88 public void cancel(Context context, int taskId) { | 92 public void cancel(Context context, int taskId) { |
89 ThreadUtils.assertOnUiThread(); | 93 ThreadUtils.assertOnUiThread(); |
90 BackgroundTaskSchedulerPrefs.removeScheduledTask(taskId); | 94 BackgroundTaskSchedulerPrefs.removeScheduledTask(taskId); |
91 mSchedulerDelegate.cancel(context, taskId); | 95 mSchedulerDelegate.cancel(context, taskId); |
92 } | 96 } |
| 97 |
| 98 public void reschedule(Context context) { |
| 99 Set<String> scheduledTasksClassNames = BackgroundTaskSchedulerPrefs.getS
cheduledTasks(); |
| 100 BackgroundTaskSchedulerPrefs.removeAllTasks(); |
| 101 for (String className : scheduledTasksClassNames) { |
| 102 BackgroundTask task = getBackgroundTaskFromClassName(className); |
| 103 if (task == null) { |
| 104 Log.w(TAG, "Cannot reschedule task for: " + className); |
| 105 continue; |
| 106 } |
| 107 |
| 108 task.reschedule(context); |
| 109 } |
| 110 } |
93 } | 111 } |
OLD | NEW |