Debian Codegraph @

apex/jobscheduler/framework/java/android/app/job/JobService.java

raw symbols SCIP
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.app.Service;
20 import android.content.Intent;
21 import android.os.IBinder;
22
23 /**
24 * <p>Entry point for the callback from the {@link android.app.job.JobScheduler}.</p>
25 * <p>This is the base class that handles asynchronous requests that were previously scheduled. You
26 * are responsible for overriding {@link JobService#onStartJob(JobParameters)}, which is where
27 * you will implement your job logic.</p>
28 * <p>This service executes each incoming job on a {@link android.os.Handler} running on your
29 * application's main thread. This means that you <b>must</b> offload your execution logic to
30 * another thread/handler/{@link android.os.AsyncTask} of your choosing. Not doing so will result
31 * in blocking any future callbacks from the JobManager - specifically
32 * {@link #onStopJob(android.app.job.JobParameters)}, which is meant to inform you that the
33 * scheduling requirements are no longer being met.</p>
34 *
35 * <p>As a subclass of {@link Service}, there will only be one active instance of any JobService
36 * subclasses, regardless of job ID. This means that if you schedule multiple jobs with different
37 * job IDs but using the same JobService class, that JobService may receive multiple calls to
38 * {@link #onStartJob(JobParameters)} and {@link #onStopJob(JobParameters)}, with each call being
39 * for the separate jobs.</p>
40 */
41 public abstract class JobService extends Service {
42 private static final String TAG = "JobService";
43
44 /**
45 * Job services must be protected with this permission:
46 *
47 * <pre class="prettyprint">
48 * &#60;service android:name="MyJobService"
49 * android:permission="android.permission.BIND_JOB_SERVICE" &#62;
50 * ...
51 * &#60;/service&#62;
52 * </pre>
53 *
54 * <p>If a job service is declared in the manifest but not protected with this
55 * permission, that service will be ignored by the system.
56 */
57 public static final String PERMISSION_BIND =
58 "android.permission.BIND_JOB_SERVICE";
59
60 private JobServiceEngine mEngine;
61
62 /** @hide */
63 public final IBinder onBind(Intent intent) {
64 if (mEngine == null) {
65 mEngine = new JobServiceEngine(this) {
66 @Override
67 public boolean onStartJob(JobParameters params) {
68 return JobService.this.onStartJob(params);
69 }
70
71 @Override
72 public boolean onStopJob(JobParameters params) {
73 return JobService.this.onStopJob(params);
74 }
75 };
76 }
77 return mEngine.getBinder();
78 }
79
80 /**
81 * Call this to inform the JobScheduler that the job has finished its work. When the
82 * system receives this message, it releases the wakelock being held for the job.
83 * This does not need to be called if {@link #onStopJob(JobParameters)} has been called.
84 * <p>
85 * You can request that the job be scheduled again by passing {@code true} as
86 * the <code>wantsReschedule</code> parameter. This will apply back-off policy
87 * for the job; this policy can be adjusted through the
88 * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)} method
89 * when the job is originally scheduled. The job's initial
90 * requirements are preserved when jobs are rescheduled, regardless of backed-off
91 * policy.
92 * <p class="note">
93 * A job running while the device is dozing will not be rescheduled with the normal back-off
94 * policy. Instead, the job will be re-added to the queue and executed again during
95 * a future idle maintenance window.
96 * </p>
97 *
98 * @param params The parameters identifying this job, as supplied to
99 * the job in the {@link #onStartJob(JobParameters)} callback.
100 * @param wantsReschedule {@code true} if this job should be rescheduled according
101 * to the back-off criteria specified when it was first scheduled; {@code false}
102 * otherwise.
103 */
104 public final void jobFinished(JobParameters params, boolean wantsReschedule) {
105 mEngine.jobFinished(params, wantsReschedule);
106 }
107
108 /**
109 * Called to indicate that the job has begun executing. Override this method with the
110 * logic for your job. Like all other component lifecycle callbacks, this method executes
111 * on your application's main thread.
112 * <p>
113 * Return {@code true} from this method if your job needs to continue running. If you
114 * do this, the job remains active until you call
115 * {@link #jobFinished(JobParameters, boolean)} to tell the system that it has completed
116 * its work, or until the job's required constraints are no longer satisfied. For
117 * example, if the job was scheduled using
118 * {@link JobInfo.Builder#setRequiresCharging(boolean) setRequiresCharging(true)},
119 * it will be immediately halted by the system if the user unplugs the device from power,
120 * the job's {@link #onStopJob(JobParameters)} callback will be invoked, and the app
121 * will be expected to shut down all ongoing work connected with that job.
122 * <p>
123 * The system holds a wakelock on behalf of your app as long as your job is executing.
124 * This wakelock is acquired before this method is invoked, and is not released until either
125 * you call {@link #jobFinished(JobParameters, boolean)}, or after the system invokes
126 * {@link #onStopJob(JobParameters)} to notify your job that it is being shut down
127 * prematurely.
128 * <p>
129 * Returning {@code false} from this method means your job is already finished. The
130 * system's wakelock for the job will be released, and {@link #onStopJob(JobParameters)}
131 * will not be invoked.
132 *
133 * @param params Parameters specifying info about this job, including the optional
134 * extras configured with {@link JobInfo.Builder#setExtras(android.os.PersistableBundle)}.
135 * This object serves to identify this specific running job instance when calling
136 * {@link #jobFinished(JobParameters, boolean)}.
137 * @return {@code true} if your service will continue running, using a separate thread
138 * when appropriate. {@code false} means that this job has completed its work.
139 */
140 public abstract boolean onStartJob(JobParameters params);
141
142 /**
143 * This method is called if the system has determined that you must stop execution of your job
144 * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}.
145 * Once this method is called, you no longer need to call
146 * {@link #jobFinished(JobParameters, boolean)}.
147 *
148 * <p>This may happen if the requirements specified at schedule time are no longer met. For
149 * example you may have requested WiFi with
150 * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your
151 * job was executing the user toggled WiFi. Another example is if you had specified
152 * {@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left
153 * its idle maintenance window. There are many other reasons a job can be stopped early besides
154 * constraints no longer being satisfied. {@link JobParameters#getStopReason()} will return the
155 * reason this method was called. You are solely responsible for the behavior of your
156 * application upon receipt of this message; your app will likely start to misbehave if you
157 * ignore it.
158 * <p>
159 * Once this method returns (or times out), the system releases the wakelock that it is holding
160 * on behalf of the job.</p>
161 *
162 * <p class="caution"><strong>Note:</strong> When a job is stopped and rescheduled via this
163 * method call, the deadline constraint is excluded from the rescheduled job's constraint set.
164 * The rescheduled job will run again once all remaining constraints are satisfied.
165 *
166 * @param params The parameters identifying this job, similar to what was supplied to the job in
167 * the {@link #onStartJob(JobParameters)} callback, but with the stop reason
168 * included.
169 * @return {@code true} to indicate to the JobManager whether you'd like to reschedule
170 * this job based on the retry criteria provided at job creation-time; or {@code false}
171 * to end the job entirely. Regardless of the value returned, your job must stop executing.
172 */
173 public abstract boolean onStopJob(JobParameters params);
174 }