blob: 9096b47b8d4d9d47c91df4a3135df969aa167cc4 [file] [log] [blame]
Christopher Tate7060b042014-06-09 19:50:00 -07001/*
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
17package android.app.job;
18
19import android.app.Service;
Christopher Tate7060b042014-06-09 19:50:00 -070020import android.content.Intent;
21import android.os.Handler;
22import android.os.IBinder;
23import android.os.Looper;
24import android.os.Message;
25import android.os.RemoteException;
26import android.util.Log;
27
28import com.android.internal.annotations.GuardedBy;
29
Dianne Hackborn0796e9f2016-04-07 17:29:36 -070030import java.lang.ref.WeakReference;
31
Christopher Tate7060b042014-06-09 19:50:00 -070032/**
33 * <p>Entry point for the callback from the {@link android.app.job.JobScheduler}.</p>
34 * <p>This is the base class that handles asynchronous requests that were previously scheduled. You
35 * are responsible for overriding {@link JobService#onStartJob(JobParameters)}, which is where
36 * you will implement your job logic.</p>
37 * <p>This service executes each incoming job on a {@link android.os.Handler} running on your
38 * application's main thread. This means that you <b>must</b> offload your execution logic to
39 * another thread/handler/{@link android.os.AsyncTask} of your choosing. Not doing so will result
40 * in blocking any future callbacks from the JobManager - specifically
41 * {@link #onStopJob(android.app.job.JobParameters)}, which is meant to inform you that the
42 * scheduling requirements are no longer being met.</p>
43 */
44public abstract class JobService extends Service {
45 private static final String TAG = "JobService";
46
47 /**
48 * Job services must be protected with this permission:
49 *
50 * <pre class="prettyprint">
Christopher Tatea8e24f22016-02-25 16:18:32 -080051 * &#60;service android:name="MyJobService"
52 * android:permission="android.permission.BIND_JOB_SERVICE" &#62;
Christopher Tate7060b042014-06-09 19:50:00 -070053 * ...
Christopher Tatea8e24f22016-02-25 16:18:32 -080054 * &#60;/service&#62;
Christopher Tate7060b042014-06-09 19:50:00 -070055 * </pre>
56 *
57 * <p>If a job service is declared in the manifest but not protected with this
58 * permission, that service will be ignored by the OS.
59 */
60 public static final String PERMISSION_BIND =
61 "android.permission.BIND_JOB_SERVICE";
62
Dianne Hackborn579f75c2017-04-14 15:46:00 -070063 private JobServiceEngine mEngine;
Christopher Tate7060b042014-06-09 19:50:00 -070064
65 /** @hide */
66 public final IBinder onBind(Intent intent) {
Dianne Hackborn579f75c2017-04-14 15:46:00 -070067 if (mEngine == null) {
68 mEngine = new JobServiceEngine(this) {
69 @Override
70 public boolean onStartJob(JobParameters params) {
71 return JobService.this.onStartJob(params);
72 }
73
74 @Override
75 public boolean onStopJob(JobParameters params) {
76 return JobService.this.onStopJob(params);
77 }
78 };
Dianne Hackborn0796e9f2016-04-07 17:29:36 -070079 }
Dianne Hackborn579f75c2017-04-14 15:46:00 -070080 return mEngine.getBinder();
Christopher Tate7060b042014-06-09 19:50:00 -070081 }
82
83 /**
84 * Override this method with the callback logic for your job. Any such logic needs to be
85 * performed on a separate thread, as this function is executed on your application's main
86 * thread.
87 *
88 * @param params Parameters specifying info about this job, including the extras bundle you
89 * optionally provided at job-creation time.
90 * @return True if your service needs to process the work (on a separate thread). False if
91 * there's no more work to be done for this job.
92 */
93 public abstract boolean onStartJob(JobParameters params);
94
95 /**
96 * This method is called if the system has determined that you must stop execution of your job
97 * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}.
98 *
99 * <p>This will happen if the requirements specified at schedule time are no longer met. For
100 * example you may have requested WiFi with
Matthew Williamsd1c06752014-08-22 14:15:28 -0700101 * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your
Christopher Tate7060b042014-06-09 19:50:00 -0700102 * job was executing the user toggled WiFi. Another example is if you had specified
103 * {@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left its
104 * idle maintenance window. You are solely responsible for the behaviour of your application
105 * upon receipt of this message; your app will likely start to misbehave if you ignore it. One
106 * immediate repercussion is that the system will cease holding a wakelock for you.</p>
107 *
108 * @param params Parameters specifying info about this job.
109 * @return True to indicate to the JobManager whether you'd like to reschedule this job based
110 * on the retry criteria provided at job creation-time. False to drop the job. Regardless of
111 * the value returned, your job must stop executing.
112 */
113 public abstract boolean onStopJob(JobParameters params);
114
115 /**
Dianne Hackborn532ea262017-03-17 17:50:55 -0700116 * Call this to inform the JobManager you've finished executing. This can be called from any
Christopher Tate7060b042014-06-09 19:50:00 -0700117 * thread, as it will ultimately be run on your application's main thread. When the system
118 * receives this message it will release the wakelock being held.
119 * <p>
120 * You can specify post-execution behaviour to the scheduler here with
121 * <code>needsReschedule </code>. This will apply a back-off timer to your job based on
122 * the default, or what was set with
123 * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)}. The original
124 * requirements are always honoured even for a backed-off job. Note that a job running in
125 * idle mode will not be backed-off. Instead what will happen is the job will be re-added
126 * to the queue and re-executed within a future idle maintenance window.
127 * </p>
128 *
129 * @param params Parameters specifying system-provided info about this job, this was given to
130 * your application in {@link #onStartJob(JobParameters)}.
Matthew Williamsd1c06752014-08-22 14:15:28 -0700131 * @param needsReschedule True if this job should be rescheduled according to the back-off
132 * criteria specified at schedule-time. False otherwise.
Christopher Tate7060b042014-06-09 19:50:00 -0700133 */
134 public final void jobFinished(JobParameters params, boolean needsReschedule) {
Dianne Hackborn579f75c2017-04-14 15:46:00 -0700135 mEngine.jobFinished(params, needsReschedule);
Christopher Tate7060b042014-06-09 19:50:00 -0700136 }
137}