• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package android.app.job;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.RequiresPermission;
23 import android.annotation.SystemApi;
24 import android.annotation.SystemService;
25 import android.content.ClipData;
26 import android.content.Context;
27 import android.os.Bundle;
28 import android.os.PersistableBundle;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.List;
33 
34 /**
35  * This is an API for scheduling various types of jobs against the framework that will be executed
36  * in your application's own process.
37  * <p>
38  * See {@link android.app.job.JobInfo} for more description of the types of jobs that can be run
39  * and how to construct them. You will construct these JobInfo objects and pass them to the
40  * JobScheduler with {@link #schedule(JobInfo)}. When the criteria declared are met, the
41  * system will execute this job on your application's {@link android.app.job.JobService}.
42  * You identify the service component that implements the logic for your job when you
43  * construct the JobInfo using
44  * {@link android.app.job.JobInfo.Builder#JobInfo.Builder(int,android.content.ComponentName)}.
45  * </p>
46  * <p>
47  * The framework will be intelligent about when it executes jobs, and attempt to batch
48  * and defer them as much as possible. Typically if you don't specify a deadline on a job, it
49  * can be run at any moment depending on the current state of the JobScheduler's internal queue.
50  * <p>
51  * While a job is running, the system holds a wakelock on behalf of your app.  For this reason,
52  * you do not need to take any action to guarantee that the device stays awake for the
53  * duration of the job.
54  * </p>
55  * <p>You do not
56  * instantiate this class directly; instead, retrieve it through
57  * {@link android.content.Context#getSystemService
58  * Context.getSystemService(Context.JOB_SCHEDULER_SERVICE)}.
59  */
60 @SystemService(Context.JOB_SCHEDULER_SERVICE)
61 public abstract class JobScheduler {
62     /** @hide */
63     @IntDef(prefix = { "RESULT_" }, value = {
64             RESULT_FAILURE,
65             RESULT_SUCCESS,
66     })
67     @Retention(RetentionPolicy.SOURCE)
68     public @interface Result {}
69 
70     /**
71      * Returned from {@link #schedule(JobInfo)} when an invalid parameter was supplied. This can occur
72      * if the run-time for your job is too short, or perhaps the system can't resolve the
73      * requisite {@link JobService} in your package.
74      */
75     public static final int RESULT_FAILURE = 0;
76     /**
77      * Returned from {@link #schedule(JobInfo)} if this job has been successfully scheduled.
78      */
79     public static final int RESULT_SUCCESS = 1;
80 
81     /**
82      * Schedule a job to be executed.  Will replace any currently scheduled job with the same
83      * ID with the new information in the {@link JobInfo}.  If a job with the given ID is currently
84      * running, it will be stopped.
85      *
86      * @param job The job you wish scheduled. See
87      * {@link android.app.job.JobInfo.Builder JobInfo.Builder} for more detail on the sorts of jobs
88      * you can schedule.
89      * @return the result of the schedule request.
90      */
schedule(@onNull JobInfo job)91     public abstract @Result int schedule(@NonNull JobInfo job);
92 
93     /**
94      * Similar to {@link #schedule}, but allows you to enqueue work for a new <em>or existing</em>
95      * job.  If a job with the same ID is already scheduled, it will be replaced with the
96      * new {@link JobInfo}, but any previously enqueued work will remain and be dispatched the
97      * next time it runs.  If a job with the same ID is already running, the new work will be
98      * enqueued for it.
99      *
100      * <p>The work you enqueue is later retrieved through
101      * {@link JobParameters#dequeueWork() JobParameters.dequeueWork}.  Be sure to see there
102      * about how to process work; the act of enqueueing work changes how you should handle the
103      * overall lifecycle of an executing job.</p>
104      *
105      * <p>It is strongly encouraged that you use the same {@link JobInfo} for all work you
106      * enqueue.  This will allow the system to optimally schedule work along with any pending
107      * and/or currently running work.  If the JobInfo changes from the last time the job was
108      * enqueued, the system will need to update the associated JobInfo, which can cause a disruption
109      * in execution.  In particular, this can result in any currently running job that is processing
110      * previous work to be stopped and restarted with the new JobInfo.</p>
111      *
112      * <p>It is recommended that you avoid using
113      * {@link JobInfo.Builder#setExtras(PersistableBundle)} or
114      * {@link JobInfo.Builder#setTransientExtras(Bundle)} with a JobInfo you are using to
115      * enqueue work.  The system will try to compare these extras with the previous JobInfo,
116      * but there are situations where it may get this wrong and count the JobInfo as changing.
117      * (That said, you should be relatively safe with a simple set of consistent data in these
118      * fields.)  You should never use {@link JobInfo.Builder#setClipData(ClipData, int)} with
119      * work you are enqueue, since currently this will always be treated as a different JobInfo,
120      * even if the ClipData contents are exactly the same.</p>
121      *
122      * @param job The job you wish to enqueue work for. See
123      * {@link android.app.job.JobInfo.Builder JobInfo.Builder} for more detail on the sorts of jobs
124      * you can schedule.
125      * @param work New work to enqueue.  This will be available later when the job starts running.
126      * @return the result of the enqueue request.
127      */
enqueue(@onNull JobInfo job, @NonNull JobWorkItem work)128     public abstract @Result int enqueue(@NonNull JobInfo job, @NonNull JobWorkItem work);
129 
130     /**
131      *
132      * @param job The job to be scheduled.
133      * @param packageName The package on behalf of which the job is to be scheduled. This will be
134      *                    used to track battery usage and appIdleState.
135      * @param userId    User on behalf of whom this job is to be scheduled.
136      * @param tag Debugging tag for dumps associated with this job (instead of the service class)
137      * @hide
138      */
139     @SystemApi
140     @RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_STATS)
scheduleAsPackage(@onNull JobInfo job, @NonNull String packageName, int userId, String tag)141     public abstract @Result int scheduleAsPackage(@NonNull JobInfo job, @NonNull String packageName,
142             int userId, String tag);
143 
144     /**
145      * Cancel the specified job.  If the job is currently executing, it is stopped
146      * immediately and the return value from its {@link JobService#onStopJob(JobParameters)}
147      * method is ignored.
148      *
149      * @param jobId unique identifier for the job to be canceled, as supplied to
150      *     {@link JobInfo.Builder#JobInfo.Builder(int, android.content.ComponentName)
151      *     JobInfo.Builder(int, android.content.ComponentName)}.
152      */
cancel(int jobId)153     public abstract void cancel(int jobId);
154 
155     /**
156      * Cancel <em>all</em> jobs that have been scheduled by the calling application.
157      */
cancelAll()158     public abstract void cancelAll();
159 
160     /**
161      * Retrieve all jobs that have been scheduled by the calling application.
162      *
163      * @return a list of all of the app's scheduled jobs.  This includes jobs that are
164      *     currently started as well as those that are still waiting to run.
165      */
getAllPendingJobs()166     public abstract @NonNull List<JobInfo> getAllPendingJobs();
167 
168     /**
169      * Look up the description of a scheduled job.
170      *
171      * @return The {@link JobInfo} description of the given scheduled job, or {@code null}
172      *     if the supplied job ID does not correspond to any job.
173      */
getPendingJob(int jobId)174     public abstract @Nullable JobInfo getPendingJob(int jobId);
175 
176     /**
177      * <b>For internal system callers only!</b>
178      * Returns a list of all currently-executing jobs.
179      * @hide
180      */
getStartedJobs()181     public abstract List<JobInfo> getStartedJobs();
182 
183     /**
184      * <b>For internal system callers only!</b>
185      * Returns a snapshot of the state of all jobs known to the system.
186      *
187      * <p class="note">This is a slow operation, so it should be called sparingly.
188      * @hide
189      */
getAllJobSnapshots()190     public abstract List<JobSnapshot> getAllJobSnapshots();
191 }