Chromium Code Reviews| 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..a25d418db8e2b880c180e7b77acf0f85f9f84f5d |
| --- /dev/null |
| +++ b/components/background_task_scheduler/android/java/src/org/chromium/components/background_task_scheduler/BackgroundTaskScheduler.java |
| @@ -0,0 +1,78 @@ |
| +// 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; |
| + |
| +/** |
| + * 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 |
|
David Trainor- moved to gerrit
2017/02/24 07:19:49
missing ) after }.
nyquist
2017/02/24 23:41:11
Done.
|
| + * and by GCM ({@link com.google.android.gms.gcm.GcmNetworkManager}) on older platforms. |
| + */ |
| +@TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| +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.
|
| + 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; |
| + } |
| + |
| + 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); |
| + } |
| +} |