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.annotation.TargetApi; | |
8 import android.content.Context; | |
9 import android.os.Build; | |
10 import android.support.annotation.Nullable; | |
11 | |
12 import org.chromium.base.Log; | |
13 import org.chromium.base.ThreadUtils; | |
14 | |
15 /** | |
16 * A BackgroundTaskScheduler which is used to schedule jobs that run in the back ground. | |
17 * It is backed by system APIs ({@link android.app.job.JobScheduler} on newer pl atforms | |
David Trainor- moved to gerrit
2017/02/24 07:19:49
missing ) after }.
nyquist
2017/02/24 23:41:11
Done.
| |
18 * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older pl atforms. | |
19 */ | |
20 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
21 public class BackgroundTaskScheduler { | |
David Trainor- moved to gerrit
2017/02/24 07:19:49
Should you mention that people should access this
nyquist
2017/02/24 23:41:11
Good idea! Done.
| |
22 private static final String TAG = "BkgrdTaskScheduler"; | |
23 | |
24 @Nullable | |
25 static BackgroundTask getBackgroundTaskFromClassName(String backgroundTaskCl assName) { | |
26 if (backgroundTaskClassName == null) return null; | |
27 | |
28 Class<?> clazz; | |
29 try { | |
30 clazz = Class.forName(backgroundTaskClassName); | |
31 } catch (ClassNotFoundException e) { | |
32 Log.w(TAG, "Unable to find BackgroundTask class with name " + backgr oundTaskClassName); | |
33 return null; | |
34 } | |
35 | |
36 if (!BackgroundTask.class.isAssignableFrom(clazz)) { | |
37 Log.w(TAG, "Class " + clazz + " is not a BackgroundTask"); | |
38 return null; | |
39 } | |
40 | |
41 try { | |
42 return (BackgroundTask) clazz.newInstance(); | |
43 } catch (InstantiationException | IllegalAccessException e) { | |
44 Log.w(TAG, "Unable to instantiate class " + clazz); | |
45 } | |
46 return null; | |
47 } | |
48 | |
49 private final BackgroundTaskSchedulerDelegate mSchedulerDelegate; | |
50 | |
51 BackgroundTaskScheduler(BackgroundTaskSchedulerDelegate schedulerDelegate) { | |
52 assert schedulerDelegate != null; | |
53 mSchedulerDelegate = schedulerDelegate; | |
54 } | |
55 | |
56 /** | |
57 * Schedules a background task. See {@link TaskInfo} for information on what types of tasks that | |
58 * can be scheduled. | |
59 * @param context the current context. | |
60 * @param taskInfo the information about the task to be scheduled. | |
61 * @return true if the schedule operation succeeded, and false otherwise. | |
62 * @see TaskInfo | |
63 */ | |
64 public boolean schedule(Context context, TaskInfo taskInfo) { | |
65 ThreadUtils.assertOnUiThread(); | |
66 return mSchedulerDelegate.schedule(context, taskInfo); | |
67 } | |
68 | |
69 /** | |
70 * Cancels the task specified by the task ID. | |
71 * @param context the current context. | |
72 * @param taskId the ID of the task to cancel. See {@link TaskIds} for a lis t. | |
73 */ | |
74 public void cancel(Context context, int taskId) { | |
75 ThreadUtils.assertOnUiThread(); | |
76 mSchedulerDelegate.cancel(context, taskId); | |
77 } | |
78 } | |
OLD | NEW |