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

Unified 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, 10 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 side-by-side diff with in-line comments
Download patch
Index: components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
diff --git a/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
new file mode 100644
index 0000000000000000000000000000000000000000..ea633c0188c861299b59385da950629a075ce072
--- /dev/null
+++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java
@@ -0,0 +1,91 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.components.background_task_scheduler;
+
+import android.annotation.TargetApi;
+import android.content.Context;
+import android.os.Build;
+import android.support.annotation.Nullable;
+
+import org.chromium.base.Log;
+import org.chromium.base.ThreadUtils;
+
+import java.lang.reflect.Constructor;
+
+/**
+ * A BackgroundTaskScheduler which is used to schedule jobs that run in the background.
+ * It is backed by system APIs ({@link android.app.job.JobScheduler}) on newer platforms
+ * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older platforms.
+ *
+ * To get an instance of this class, use {@link BackgroundTaskSchedulerFactory#getScheduler()}.
+ */
+@TargetApi(Build.VERSION_CODES.LOLLIPOP)
+public class BackgroundTaskScheduler {
+ private static final String TAG = "BkgrdTaskScheduler";
+
+ @Nullable
+ static BackgroundTask getBackgroundTaskFromClassName(String backgroundTaskClassName) {
+ if (backgroundTaskClassName == null) return null;
+
+ Class<?> clazz;
+ try {
+ clazz = Class.forName(backgroundTaskClassName);
+ } catch (ClassNotFoundException e) {
+ Log.w(TAG, "Unable to find BackgroundTask class with name " + backgroundTaskClassName);
+ return null;
+ }
+
+ if (!BackgroundTask.class.isAssignableFrom(clazz)) {
+ Log.w(TAG, "Class " + clazz + " is not a BackgroundTask");
+ return null;
+ }
+
+ try {
+ return (BackgroundTask) clazz.newInstance();
+ } catch (InstantiationException | IllegalAccessException e) {
+ Log.w(TAG, "Unable to instantiate class " + clazz);
+ return null;
+ }
+ }
+
+ static boolean hasParameterlessPublicConstructor(Class<? extends BackgroundTask> clazz) {
+ for (Constructor<?> constructor : clazz.getConstructors()) {
+ if (constructor.getParameterTypes().length == 0) return true;
+ }
+ return false;
+ }
+
+ private final BackgroundTaskSchedulerDelegate mSchedulerDelegate;
+
+ BackgroundTaskScheduler(BackgroundTaskSchedulerDelegate schedulerDelegate) {
+ assert schedulerDelegate != null;
+ mSchedulerDelegate = schedulerDelegate;
+ }
+
+ /**
+ * Schedules a background task. See {@link TaskInfo} for information on what types of tasks that
+ * can be scheduled.
+ *
+ * @param context the current context.
+ * @param taskInfo the information about the task to be scheduled.
+ * @return true if the schedule operation succeeded, and false otherwise.
+ * @see TaskInfo
+ */
+ public boolean schedule(Context context, TaskInfo taskInfo) {
+ ThreadUtils.assertOnUiThread();
+ return mSchedulerDelegate.schedule(context, taskInfo);
+ }
+
+ /**
+ * Cancels the task specified by the task ID.
+ *
+ * @param context the current context.
+ * @param taskId the ID of the task to cancel. See {@link TaskIds} for a list.
+ */
+ public void cancel(Context context, int taskId) {
+ ThreadUtils.assertOnUiThread();
+ mSchedulerDelegate.cancel(context, taskId);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698