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.content.Context; |
| 8 import android.support.annotation.AnyThread; |
| 9 import android.support.annotation.MainThread; |
| 10 |
| 11 /** |
| 12 * Entry point for callbacks from {@link BackgroundTaskScheduler}. Any classes i
mplementing |
| 13 * this interface must have a public constructor which takes no arguments. |
| 14 * The callbacks will be executed on the main thread, which means that execution
logic must be |
| 15 * offloaded to another {@link Thread}, {@link android.os.Handler} or {@link and
roid.os.AsyncTask}. |
| 16 */ |
| 17 public interface BackgroundTask { |
| 18 /** |
| 19 * Callback to invoke whenever background processing has finished after firs
t returning true |
| 20 * from {@link #onStartTask(Context, TaskParameters, TaskFinishedCallback)}. |
| 21 */ |
| 22 interface TaskFinishedCallback { |
| 23 /** |
| 24 * Callback to inform the {@link BackgroundTaskScheduler} that the backg
round processing |
| 25 * now has finished. When this callback is invoked, the system will stop
holding a wakelock. |
| 26 * |
| 27 * @param needsReschedule whether this task must be rescheduled. |
| 28 */ |
| 29 @AnyThread |
| 30 void taskFinished(boolean needsReschedule); |
| 31 } |
| 32 |
| 33 /** |
| 34 * Callback from {@link BackgroundTaskScheduler} when your task should start
processing. |
| 35 * It is invoked on the main thread, and if your task finishes quickly, you
should return false |
| 36 * from this method when you are done processing. If this is a long-running
task, you should |
| 37 * return true from this method, and instead invoke the {@link TaskFinishedC
allback} when the |
| 38 * processing is finished on some other {@link Thread}, {@link android.os.Ha
ndler} or |
| 39 * {@link android.os.AsyncTask}. While this method is running the system hol
ds a wakelock. If |
| 40 * false is returned from this method, the wakelock is immediately released,
but if this method |
| 41 * returns true, the wakelock is not released until either the {@link TaskFi
nishedCallback} is |
| 42 * invoked, or the system calls {@link #onStopTask(Context, TaskParameters)}
. |
| 43 * |
| 44 * @param context the current context. |
| 45 * @param taskParameters the data passed in as {@link TaskInfo} when the tas
k was scheduled. |
| 46 * @param callback if the task needs to continue processing after returning
from the call to |
| 47 * {@link #onStartTask(Context, TaskParameters, TaskFinished
Callback)}, this |
| 48 * callback must be invoked when the processing has finished
. |
| 49 * @return true if the task needs to continue processing work. False if ther
e is no more work. |
| 50 * @see TaskInfo |
| 51 */ |
| 52 @MainThread |
| 53 boolean onStartTask( |
| 54 Context context, TaskParameters taskParameters, TaskFinishedCallback
callback); |
| 55 |
| 56 /** |
| 57 * Callback from {@link BackgroundTaskScheduler} when the system has determi
ned that the |
| 58 * execution of the task must stop immediately, even before the {@link TaskF
inishedCallback} |
| 59 * has been invoked. This will typically happen whenever the required condit
ions for the task |
| 60 * are no longer met. See {@link TaskInfo}. A wakelock is held by the system
while this callback |
| 61 * is invoked, and immediately released after this method returns. |
| 62 * |
| 63 * @param context the current context. |
| 64 * @param taskParameters the data passed in as {@link TaskInfo} when the tas
k was scheduled. |
| 65 * @return true if the task needs to be rescheduled according to the resched
uling criteria |
| 66 * specified when the task was scheduled initially. False if the taskshould
not be rescheduled. |
| 67 * Regardless of the value returned, your task must stop executing. |
| 68 * @see TaskInfo |
| 69 */ |
| 70 @MainThread |
| 71 boolean onStopTask(Context context, TaskParameters taskParameters); |
| 72 } |
OLD | NEW |