JobParameters.java revision 94326cb56aa0c7cee110d6781fb8b8f16fb09663
16e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams/*
26e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Copyright (C) 2014 The Android Open Source Project
36e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
46e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Licensed under the Apache License, Version 2.0 (the "License");
56e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * you may not use this file except in compliance with the License.
66e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * You may obtain a copy of the License at
76e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
86e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *      http://www.apache.org/licenses/LICENSE-2.0
96e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams *
106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * Unless required by applicable law or agreed to in writing, software
116e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * distributed under the License is distributed on an "AS IS" BASIS,
126e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * See the License for the specific language governing permissions and
146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * limitations under the License
156e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */
166e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
177060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepackage android.app.job;
186e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
19f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackbornimport android.annotation.NonNull;
20f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackbornimport android.annotation.Nullable;
217060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tateimport android.app.job.IJobCallback;
22a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackbornimport android.content.ClipData;
231a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackbornimport android.net.Uri;
24ba60473a6539d16bef8720d79b5559512303bddfDianne Hackbornimport android.os.Bundle;
256e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.IBinder;
266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcel;
276e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcelable;
283d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williamsimport android.os.PersistableBundle;
297da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackbornimport android.os.RemoteException;
306e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams/**
327060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * Contains the parameters used to configure/identify your job. You do not create this object
336e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * yourself, instead it is handed in to your application by the System.
346e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */
357060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepublic class JobParameters implements Parcelable {
366e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
375db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
385db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public static final int REASON_CANCELED = 0;
395db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
405db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public static final int REASON_CONSTRAINTS_NOT_SATISFIED = 1;
415db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
425db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public static final int REASON_PREEMPT = 2;
435db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
445db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public static final int REASON_TIMEOUT = 3;
455db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
465db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public static final int REASON_DEVICE_IDLE = 4;
475db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge
4894326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn    /** @hide */
4994326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn    public static String getReasonName(int reason) {
5094326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn        switch (reason) {
5194326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            case REASON_CANCELED: return "canceled";
5294326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            case REASON_CONSTRAINTS_NOT_SATISFIED: return "constraints";
5394326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            case REASON_PREEMPT: return "preempt";
5494326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            case REASON_TIMEOUT: return "timeout";
5594326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            case REASON_DEVICE_IDLE: return "device_idle";
5694326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn            default: return "unknown:" + reason;
5794326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn        }
5894326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn    }
5994326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn
607060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private final int jobId;
613d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    private final PersistableBundle extras;
62ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    private final Bundle transientExtras;
63a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    private final ClipData clipData;
64a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    private final int clipGrantFlags;
656de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final IBinder callback;
6603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    private final boolean overrideDeadlineExpired;
671a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    private final Uri[] mTriggeredContentUris;
681a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    private final String[] mTriggeredContentAuthorities;
691a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
705db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    private int stopReason; // Default value of stopReason is REASON_CANCELED
716de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams
726de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    /** @hide */
7303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    public JobParameters(IBinder callback, int jobId, PersistableBundle extras,
74a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            Bundle transientExtras, ClipData clipData, int clipGrantFlags,
75a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            boolean overrideDeadlineExpired, Uri[] triggeredContentUris,
761a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn            String[] triggeredContentAuthorities) {
777060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        this.jobId = jobId;
786de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        this.extras = extras;
79ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        this.transientExtras = transientExtras;
80a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        this.clipData = clipData;
81a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        this.clipGrantFlags = clipGrantFlags;
826de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        this.callback = callback;
8303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        this.overrideDeadlineExpired = overrideDeadlineExpired;
841a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        this.mTriggeredContentUris = triggeredContentUris;
851a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        this.mTriggeredContentAuthorities = triggeredContentAuthorities;
866de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    }
876e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
886e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
897060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * @return The unique id of this job, specified at creation time.
906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
917060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public int getJobId() {
927060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        return jobId;
936e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
946e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
956e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
965db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     * Reason onStopJob() was called on this job.
975db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     * @hide
985db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     */
995db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public int getStopReason() {
1005db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        return stopReason;
1015db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    }
1025db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge
1035db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /**
1047060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * @return The extras you passed in when constructing this job with
1057060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * {@link android.app.job.JobInfo.Builder#setExtras(android.os.PersistableBundle)}. This will
1066e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     * never be null. If you did not set any extras this will be an empty bundle.
1076e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
108f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @NonNull PersistableBundle getExtras() {
1096e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return extras;
1106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
1116e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
11203a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    /**
113ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * @return The transient extras you passed in when constructing this job with
114ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * {@link android.app.job.JobInfo.Builder#setTransientExtras(android.os.Bundle)}. This will
115ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * never be null. If you did not set any extras this will be an empty bundle.
116ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     */
117f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @NonNull Bundle getTransientExtras() {
118ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        return transientExtras;
119ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    }
120ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn
121ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    /**
122a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * @return The clip you passed in when constructing this job with
123a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be null
124a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * if it was not set.
125a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     */
126f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable ClipData getClipData() {
127a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        return clipData;
128a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    }
129a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn
130a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    /**
131a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * @return The clip grant flags you passed in when constructing this job with
132a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be 0
133a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * if it was not set.
134a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     */
135a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    public int getClipGrantFlags() {
136a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        return clipGrantFlags;
137a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    }
138a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn
139a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    /**
14003a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * For jobs with {@link android.app.job.JobInfo.Builder#setOverrideDeadline(long)} set, this
14103a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * provides an easy way to tell whether the job is being executed due to the deadline
14203a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * expiring. Note: If the job is running because its deadline expired, it implies that its
14303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * constraints will not be met.
14403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     */
14503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    public boolean isOverrideDeadlineExpired() {
14603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        return overrideDeadlineExpired;
14703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    }
14803a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams
1491a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    /**
1501a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this
1511a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * reports which URIs have triggered the job.  This will be null if either no URIs have
1521a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * triggered it (it went off due to a deadline or other reason), or the number of changed
1531a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * URIs is too large to report.  Whether or not the number of URIs is too large, you can
1541a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * always use {@link #getTriggeredContentAuthorities()} to determine whether the job was
1551a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * triggered due to any content changes and the authorities they are associated with.
1561a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     */
157f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable Uri[] getTriggeredContentUris() {
1581a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        return mTriggeredContentUris;
1591a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    }
1601a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
1611a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    /**
1621a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this
1631a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * reports which content authorities have triggered the job.  It will only be null if no
1641a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * authorities have triggered it -- that is, the job executed for some other reason, such
1651a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * as a deadline expiring.  If this is non-null, you can use {@link #getTriggeredContentUris()}
1661a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * to retrieve the details of which URIs changed (as long as that has not exceeded the maximum
1671a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * number it can reported).
1681a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     */
169f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable String[] getTriggeredContentAuthorities() {
1701a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        return mTriggeredContentAuthorities;
1711a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    }
1721a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
1737da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    /**
1747da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * Dequeue the next pending {@link JobWorkItem} from these JobParameters associated with their
1757da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * currently running job.  Calling this method when there is no more work available and all
1767da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * previously dequeued work has been completed will result in the system taking care of
1777da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * stopping the job for you --
1787da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * you should not call {@link JobService#jobFinished(JobParameters, boolean)} yourself
1797da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * (otherwise you risk losing an upcoming JobWorkItem that is being enqueued at the same time).
1807da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
181fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * <p>Once you are done with the {@link JobWorkItem} returned by this method, you must call
182fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * {@link #completeWork(JobWorkItem)} with it to inform the system that you are done
183fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * executing the work.  The job will not be finished until all dequeued work has been
184fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * completed.  You do not, however, have to complete each returned work item before deqeueing
185fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * the next one -- you can use {@link #dequeueWork()} multiple times before completing
186fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * previous work if you want to process work in parallel, and you can complete the work
187fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * in whatever order you want.</p>
188fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     *
189fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * <p>If the job runs to the end of its available time period before all work has been
190fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * completed, it will stop as normal.  You should return true from
191fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * {@link JobService#onStopJob(JobParameters)} in order to have the job rescheduled, and by
192fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * doing so any pending as well as remaining uncompleted work will be re-queued
193fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * for the next time the job runs.</p>
194fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     *
19528d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * <p>This example shows how to construct a JobService that will serially dequeue and
19628d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * process work that is available for it:</p>
19728d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *
19828d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/JobWorkService.java
19928d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *      service}
20028d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *
2017da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * @return Returns a new {@link JobWorkItem} if there is one pending, otherwise null.
2027da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * If null is returned, the system will also stop the job if all work has also been completed.
2037da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * (This means that for correct operation, you must always call dequeueWork() after you have
2047da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * completed other work, to check either for more work or allow the system to stop the job.)
2057da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     */
206f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable JobWorkItem dequeueWork() {
2077da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        try {
2087da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            return getCallback().dequeueWork(getJobId());
2097da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        } catch (RemoteException e) {
2107da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            throw e.rethrowFromSystemServer();
2117da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        }
2127da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    }
2137da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn
2147da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    /**
2157da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * Report the completion of executing a {@link JobWorkItem} previously returned by
2167da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link #dequeueWork()}.  This tells the system you are done with the
2177da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * work associated with that item, so it will not be returned again.  Note that if this
2187da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * is the last work in the queue, completing it here will <em>not</em> finish the overall
2197da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * job -- for that to happen, you still need to call {@link #dequeueWork()}
2207da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * again.
2217da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
2227da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * <p>If you are enqueueing work into a job, you must call this method for each piece
2237da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * of work you process.  Do <em>not</em> call
2247da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link JobService#jobFinished(JobParameters, boolean)}
2257da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * or else you can lose work in your queue.</p>
2267da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
2277da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * @param work The work you have completed processing, as previously returned by
2287da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link #dequeueWork()}
2297da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     */
230f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public void completeWork(@NonNull JobWorkItem work) {
2317da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        try {
2327da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            if (!getCallback().completeWork(getJobId(), work.getWorkId())) {
2337da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn                throw new IllegalArgumentException("Given work is not active: " + work);
2347da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            }
2357da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        } catch (RemoteException e) {
2367da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            throw e.rethrowFromSystemServer();
2377da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        }
2387da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    }
2397da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn
2406de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    /** @hide */
2417060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public IJobCallback getCallback() {
2427060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        return IJobCallback.Stub.asInterface(callback);
2436e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2446e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2457060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private JobParameters(Parcel in) {
2467060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        jobId = in.readInt();
2473d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        extras = in.readPersistableBundle();
248ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        transientExtras = in.readBundle();
249a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        if (in.readInt() != 0) {
250a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData = ClipData.CREATOR.createFromParcel(in);
251a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipGrantFlags = in.readInt();
252a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        } else {
253a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData = null;
254a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipGrantFlags = 0;
255a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        }
2566de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        callback = in.readStrongBinder();
25703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        overrideDeadlineExpired = in.readInt() == 1;
2581a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        mTriggeredContentUris = in.createTypedArray(Uri.CREATOR);
2591a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        mTriggeredContentAuthorities = in.createStringArray();
2605db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        stopReason = in.readInt();
2615db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    }
2625db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge
2635db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
2645db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public void setStopReason(int reason) {
2655db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        stopReason = reason;
2666e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2676e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2686e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2696e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public int describeContents() {
2706e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return 0;
2716e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2726e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2736e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2746e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public void writeToParcel(Parcel dest, int flags) {
2757060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        dest.writeInt(jobId);
2763d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        dest.writePersistableBundle(extras);
277ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        dest.writeBundle(transientExtras);
278a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        if (clipData != null) {
279a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(1);
280a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData.writeToParcel(dest, flags);
281a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(clipGrantFlags);
282a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        } else {
283a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(0);
284a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        }
2856de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        dest.writeStrongBinder(callback);
28603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        dest.writeInt(overrideDeadlineExpired ? 1 : 0);
2871a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        dest.writeTypedArray(mTriggeredContentUris, flags);
2881a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        dest.writeStringArray(mTriggeredContentAuthorities);
2895db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        dest.writeInt(stopReason);
2906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2927060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public static final Creator<JobParameters> CREATOR = new Creator<JobParameters>() {
2936e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2947060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobParameters createFromParcel(Parcel in) {
2957060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobParameters(in);
2966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
2976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2997060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobParameters[] newArray(int size) {
3007060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobParameters[size];
3016e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
3026e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    };
3036e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams}
304