Home | History | Annotate | Download | only in job
      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 public abstract class JobService extends Service {
     36     private static final String TAG = "JobService";
     37 
     38     /**
     39      * Job services must be protected with this permission:
     40      *
     41      * <pre class="prettyprint">
     42      *     &#60;service android:name="MyJobService"
     43      *              android:permission="android.permission.BIND_JOB_SERVICE" &#62;
     44      *         ...
     45      *     &#60;/service&#62;
     46      * </pre>
     47      *
     48      * <p>If a job service is declared in the manifest but not protected with this
     49      * permission, that service will be ignored by the system.
     50      */
     51     public static final String PERMISSION_BIND =
     52             "android.permission.BIND_JOB_SERVICE";
     53 
     54     private JobServiceEngine mEngine;
     55 
     56     /** @hide */
     57     public final IBinder onBind(Intent intent) {
     58         if (mEngine == null) {
     59             mEngine = new JobServiceEngine(this) {
     60                 @Override
     61                 public boolean onStartJob(JobParameters params) {
     62                     return JobService.this.onStartJob(params);
     63                 }
     64 
     65                 @Override
     66                 public boolean onStopJob(JobParameters params) {
     67                     return JobService.this.onStopJob(params);
     68                 }
     69             };
     70         }
     71         return mEngine.getBinder();
     72     }
     73 
     74     /**
     75      * Call this to inform the JobScheduler that the job has finished its work.  When the
     76      * system receives this message, it releases the wakelock being held for the job.
     77      * <p>
     78      * You can request that the job be scheduled again by passing {@code true} as
     79      * the <code>wantsReschedule</code> parameter. This will apply back-off policy
     80      * for the job; this policy can be adjusted through the
     81      * {@link android.app.job.JobInfo.Builder#setBackoffCriteria(long, int)} method
     82      * when the job is originally scheduled.  The job's initial
     83      * requirements are preserved when jobs are rescheduled, regardless of backed-off
     84      * policy.
     85      * <p class="note">
     86      * A job running while the device is dozing will not be rescheduled with the normal back-off
     87      * policy.  Instead, the job will be re-added to the queue and executed again during
     88      * a future idle maintenance window.
     89      * </p>
     90      *
     91      * @param params The parameters identifying this job, as supplied to
     92      *               the job in the {@link #onStartJob(JobParameters)} callback.
     93      * @param wantsReschedule {@code true} if this job should be rescheduled according
     94      *     to the back-off criteria specified when it was first scheduled; {@code false}
     95      *     otherwise.
     96      */
     97     public final void jobFinished(JobParameters params, boolean wantsReschedule) {
     98         mEngine.jobFinished(params, wantsReschedule);
     99     }
    100 
    101     /**
    102      * Called to indicate that the job has begun executing.  Override this method with the
    103      * logic for your job.  Like all other component lifecycle callbacks, this method executes
    104      * on your application's main thread.
    105      * <p>
    106      * Return {@code true} from this method if your job needs to continue running.  If you
    107      * do this, the job remains active until you call
    108      * {@link #jobFinished(JobParameters, boolean)} to tell the system that it has completed
    109      * its work, or until the job's required constraints are no longer satisfied.  For
    110      * example, if the job was scheduled using
    111      * {@link JobInfo.Builder#setRequiresCharging(boolean) setRequiresCharging(true)},
    112      * it will be immediately halted by the system if the user unplugs the device from power,
    113      * the job's {@link #onStopJob(JobParameters)} callback will be invoked, and the app
    114      * will be expected to shut down all ongoing work connected with that job.
    115      * <p>
    116      * The system holds a wakelock on behalf of your app as long as your job is executing.
    117      * This wakelock is acquired before this method is invoked, and is not released until either
    118      * you call {@link #jobFinished(JobParameters, boolean)}, or after the system invokes
    119      * {@link #onStopJob(JobParameters)} to notify your job that it is being shut down
    120      * prematurely.
    121      * <p>
    122      * Returning {@code false} from this method means your job is already finished.  The
    123      * system's wakelock for the job will be released, and {@link #onStopJob(JobParameters)}
    124      * will not be invoked.
    125      *
    126      * @param params Parameters specifying info about this job, including the optional
    127      *     extras configured with {@link JobInfo.Builder#setExtras(android.os.PersistableBundle).
    128      *     This object serves to identify this specific running job instance when calling
    129      *     {@link #jobFinished(JobParameters, boolean)}.
    130      * @return {@code true} if your service will continue running, using a separate thread
    131      *     when appropriate.  {@code false} means that this job has completed its work.
    132      */
    133     public abstract boolean onStartJob(JobParameters params);
    134 
    135     /**
    136      * This method is called if the system has determined that you must stop execution of your job
    137      * even before you've had a chance to call {@link #jobFinished(JobParameters, boolean)}.
    138      *
    139      * <p>This will happen if the requirements specified at schedule time are no longer met. For
    140      * example you may have requested WiFi with
    141      * {@link android.app.job.JobInfo.Builder#setRequiredNetworkType(int)}, yet while your
    142      * job was executing the user toggled WiFi. Another example is if you had specified
    143      * {@link android.app.job.JobInfo.Builder#setRequiresDeviceIdle(boolean)}, and the phone left its
    144      * idle maintenance window. You are solely responsible for the behavior of your application
    145      * upon receipt of this message; your app will likely start to misbehave if you ignore it.
    146      * <p>
    147      * Once this method returns, the system releases the wakelock that it is holding on
    148      * behalf of the job.</p>
    149      *
    150      * @param params The parameters identifying this job, as supplied to
    151      *               the job in the {@link #onStartJob(JobParameters)} callback.
    152      * @return {@code true} to indicate to the JobManager whether you'd like to reschedule
    153      * this job based on the retry criteria provided at job creation-time; or {@code false}
    154      * to end the job entirely.  Regardless of the value returned, your job must stop executing.
    155      */
    156     public abstract boolean onStopJob(JobParameters params);
    157 }
    158