Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 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; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
| 21 | import android.os.Handler; |
| 22 | import android.os.IBinder; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
| 25 | import android.os.RemoteException; |
| 26 | import android.util.Log; |
| 27 | |
| 28 | import com.android.internal.annotations.GuardedBy; |
| 29 | |
Dianne Hackborn | 0796e9f | 2016-04-07 17:29:36 -0700 | [diff] [blame] | 30 | import java.lang.ref.WeakReference; |
| 31 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | /** |
| 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 | */ |
| 44 | public 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 Tate | a8e24f2 | 2016-02-25 16:18:32 -0800 | [diff] [blame] | 51 | * <service android:name="MyJobService" |
| 52 | * android:permission="android.permission.BIND_JOB_SERVICE" > |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 53 | * ... |
Christopher Tate | a8e24f2 | 2016-02-25 16:18:32 -0800 | [diff] [blame] | 54 | * </service> |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 55 | * </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 Hackborn | 579f75c | 2017-04-14 15:46:00 -0700 | [diff] [blame] | 63 | private JobServiceEngine mEngine; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 64 | |
| 65 | /** @hide */ |
| 66 | public final IBinder onBind(Intent intent) { |
Dianne Hackborn | 579f75c | 2017-04-14 15:46:00 -0700 | [diff] [blame] | 67 | 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 Hackborn | 0796e9f | 2016-04-07 17:29:36 -0700 | [diff] [blame] | 79 | } |
Dianne Hackborn | 579f75c | 2017-04-14 15:46:00 -0700 | [diff] [blame] | 80 | return mEngine.getBinder(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 81 | } |
| 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 Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 101 | * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 102 | * 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 Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 116 | * Call this to inform the JobManager you've finished executing. This can be called from any |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 117 | * 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 Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 131 | * @param needsReschedule True if this job should be rescheduled according to the back-off |
| 132 | * criteria specified at schedule-time. False otherwise. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 133 | */ |
| 134 | public final void jobFinished(JobParameters params, boolean needsReschedule) { |
Dianne Hackborn | 579f75c | 2017-04-14 15:46:00 -0700 | [diff] [blame] | 135 | mEngine.jobFinished(params, needsReschedule); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 136 | } |
| 137 | } |