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

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

Issue 2714463002: [android] Add JobScheduler-based BackgroundTaskScheduler. (Closed)
Patch Set: FindBugs wants the real Pi, but I won't give it. Created 3 years, 9 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
(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 import java.lang.reflect.Constructor;
16
17 /**
18 * 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 * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older pl atforms.
21 *
22 * To get an instance of this class, use {@link BackgroundTaskSchedulerFactory#g etScheduler()}.
23 */
24 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
25 public class BackgroundTaskScheduler {
26 private static final String TAG = "BkgrdTaskScheduler";
27
28 @Nullable
29 static BackgroundTask getBackgroundTaskFromClassName(String backgroundTaskCl assName) {
30 if (backgroundTaskClassName == null) return null;
31
32 Class<?> clazz;
33 try {
34 clazz = Class.forName(backgroundTaskClassName);
35 } catch (ClassNotFoundException e) {
36 Log.w(TAG, "Unable to find BackgroundTask class with name " + backgr oundTaskClassName);
37 return null;
38 }
39
40 if (!BackgroundTask.class.isAssignableFrom(clazz)) {
41 Log.w(TAG, "Class " + clazz + " is not a BackgroundTask");
42 return null;
43 }
44
45 try {
46 return (BackgroundTask) clazz.newInstance();
47 } catch (InstantiationException | IllegalAccessException e) {
48 Log.w(TAG, "Unable to instantiate class " + clazz);
49 return null;
50 }
51 }
52
53 static boolean hasParameterlessPublicConstructor(Class<? extends BackgroundT ask> clazz) {
54 for (Constructor<?> constructor : clazz.getConstructors()) {
55 if (constructor.getParameterTypes().length == 0) return true;
56 }
57 return false;
58 }
59
60 private final BackgroundTaskSchedulerDelegate mSchedulerDelegate;
61
62 BackgroundTaskScheduler(BackgroundTaskSchedulerDelegate schedulerDelegate) {
63 assert schedulerDelegate != null;
64 mSchedulerDelegate = schedulerDelegate;
65 }
66
67 /**
68 * Schedules a background task. See {@link TaskInfo} for information on what types of tasks that
69 * can be scheduled.
70 *
71 * @param context the current context.
72 * @param taskInfo the information about the task to be scheduled.
73 * @return true if the schedule operation succeeded, and false otherwise.
74 * @see TaskInfo
75 */
76 public boolean schedule(Context context, TaskInfo taskInfo) {
77 ThreadUtils.assertOnUiThread();
78 return mSchedulerDelegate.schedule(context, taskInfo);
79 }
80
81 /**
82 * Cancels the task specified by the task ID.
83 *
84 * @param context the current context.
85 * @param taskId the ID of the task to cancel. See {@link TaskIds} for a lis t.
86 */
87 public void cancel(Context context, int taskId) {
88 ThreadUtils.assertOnUiThread();
89 mSchedulerDelegate.cancel(context, taskId);
90 }
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698