JobParameters.java revision 28d1b661347a6a7e05dc1004fd7e8436cace8953
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
487060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private final int jobId;
493d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams    private final PersistableBundle extras;
50ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    private final Bundle transientExtras;
51a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    private final ClipData clipData;
52a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    private final int clipGrantFlags;
536de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    private final IBinder callback;
5403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    private final boolean overrideDeadlineExpired;
551a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    private final Uri[] mTriggeredContentUris;
561a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    private final String[] mTriggeredContentAuthorities;
571a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
585db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    private int stopReason; // Default value of stopReason is REASON_CANCELED
596de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams
606de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    /** @hide */
6103a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    public JobParameters(IBinder callback, int jobId, PersistableBundle extras,
62a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            Bundle transientExtras, ClipData clipData, int clipGrantFlags,
63a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            boolean overrideDeadlineExpired, Uri[] triggeredContentUris,
641a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn            String[] triggeredContentAuthorities) {
657060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        this.jobId = jobId;
666de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        this.extras = extras;
67ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        this.transientExtras = transientExtras;
68a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        this.clipData = clipData;
69a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        this.clipGrantFlags = clipGrantFlags;
706de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        this.callback = callback;
7103a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        this.overrideDeadlineExpired = overrideDeadlineExpired;
721a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        this.mTriggeredContentUris = triggeredContentUris;
731a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        this.mTriggeredContentAuthorities = triggeredContentAuthorities;
746de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    }
756e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
766e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
777060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * @return The unique id of this job, specified at creation time.
786e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
797060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public int getJobId() {
807060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        return jobId;
816e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
826e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
836e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    /**
845db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     * Reason onStopJob() was called on this job.
855db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     * @hide
865db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge     */
875db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public int getStopReason() {
885db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        return stopReason;
895db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    }
905db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge
915db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /**
927060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * @return The extras you passed in when constructing this job with
937060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate     * {@link android.app.job.JobInfo.Builder#setExtras(android.os.PersistableBundle)}. This will
946e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     * never be null. If you did not set any extras this will be an empty bundle.
956e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams     */
96f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @NonNull PersistableBundle getExtras() {
976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return extras;
986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
996e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
10003a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    /**
101ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * @return The transient extras you passed in when constructing this job with
102ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * {@link android.app.job.JobInfo.Builder#setTransientExtras(android.os.Bundle)}. This will
103ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     * never be null. If you did not set any extras this will be an empty bundle.
104ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn     */
105f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @NonNull Bundle getTransientExtras() {
106ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        return transientExtras;
107ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    }
108ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn
109ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn    /**
110a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * @return The clip you passed in when constructing this job with
111a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be null
112a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * if it was not set.
113a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     */
114f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable ClipData getClipData() {
115a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        return clipData;
116a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    }
117a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn
118a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    /**
119a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * @return The clip grant flags you passed in when constructing this job with
120a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be 0
121a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     * if it was not set.
122a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn     */
123a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    public int getClipGrantFlags() {
124a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        return clipGrantFlags;
125a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    }
126a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn
127a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn    /**
12803a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * For jobs with {@link android.app.job.JobInfo.Builder#setOverrideDeadline(long)} set, this
12903a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * provides an easy way to tell whether the job is being executed due to the deadline
13003a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * expiring. Note: If the job is running because its deadline expired, it implies that its
13103a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     * constraints will not be met.
13203a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams     */
13303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    public boolean isOverrideDeadlineExpired() {
13403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        return overrideDeadlineExpired;
13503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams    }
13603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams
1371a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    /**
1381a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this
1391a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * reports which URIs have triggered the job.  This will be null if either no URIs have
1401a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * triggered it (it went off due to a deadline or other reason), or the number of changed
1411a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * URIs is too large to report.  Whether or not the number of URIs is too large, you can
1421a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * always use {@link #getTriggeredContentAuthorities()} to determine whether the job was
1431a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * triggered due to any content changes and the authorities they are associated with.
1441a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     */
145f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable Uri[] getTriggeredContentUris() {
1461a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        return mTriggeredContentUris;
1471a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    }
1481a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
1491a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    /**
1501a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this
1511a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * reports which content authorities have triggered the job.  It will only be null if no
1521a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * authorities have triggered it -- that is, the job executed for some other reason, such
1531a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * as a deadline expiring.  If this is non-null, you can use {@link #getTriggeredContentUris()}
1541a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * to retrieve the details of which URIs changed (as long as that has not exceeded the maximum
1551a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     * number it can reported).
1561a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn     */
157f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable String[] getTriggeredContentAuthorities() {
1581a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        return mTriggeredContentAuthorities;
1591a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn    }
1601a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn
1617da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    /**
1627da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * Dequeue the next pending {@link JobWorkItem} from these JobParameters associated with their
1637da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * currently running job.  Calling this method when there is no more work available and all
1647da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * previously dequeued work has been completed will result in the system taking care of
1657da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * stopping the job for you --
1667da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * you should not call {@link JobService#jobFinished(JobParameters, boolean)} yourself
1677da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * (otherwise you risk losing an upcoming JobWorkItem that is being enqueued at the same time).
1687da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
169fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * <p>Once you are done with the {@link JobWorkItem} returned by this method, you must call
170fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * {@link #completeWork(JobWorkItem)} with it to inform the system that you are done
171fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * executing the work.  The job will not be finished until all dequeued work has been
172fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * completed.  You do not, however, have to complete each returned work item before deqeueing
173fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * the next one -- you can use {@link #dequeueWork()} multiple times before completing
174fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * previous work if you want to process work in parallel, and you can complete the work
175fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * in whatever order you want.</p>
176fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     *
177fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * <p>If the job runs to the end of its available time period before all work has been
178fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * completed, it will stop as normal.  You should return true from
179fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * {@link JobService#onStopJob(JobParameters)} in order to have the job rescheduled, and by
180fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * doing so any pending as well as remaining uncompleted work will be re-queued
181fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     * for the next time the job runs.</p>
182fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn     *
18328d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * <p>This example shows how to construct a JobService that will serially dequeue and
18428d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * process work that is available for it:</p>
18528d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *
18628d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/JobWorkService.java
18728d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *      service}
18828d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn     *
1897da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * @return Returns a new {@link JobWorkItem} if there is one pending, otherwise null.
1907da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * If null is returned, the system will also stop the job if all work has also been completed.
1917da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * (This means that for correct operation, you must always call dequeueWork() after you have
1927da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * completed other work, to check either for more work or allow the system to stop the job.)
1937da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     */
194f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public @Nullable JobWorkItem dequeueWork() {
1957da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        try {
1967da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            return getCallback().dequeueWork(getJobId());
1977da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        } catch (RemoteException e) {
1987da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            throw e.rethrowFromSystemServer();
1997da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        }
2007da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    }
2017da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn
2027da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    /**
2037da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * Report the completion of executing a {@link JobWorkItem} previously returned by
2047da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link #dequeueWork()}.  This tells the system you are done with the
2057da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * work associated with that item, so it will not be returned again.  Note that if this
2067da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * is the last work in the queue, completing it here will <em>not</em> finish the overall
2077da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * job -- for that to happen, you still need to call {@link #dequeueWork()}
2087da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * again.
2097da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
2107da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * <p>If you are enqueueing work into a job, you must call this method for each piece
2117da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * of work you process.  Do <em>not</em> call
2127da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link JobService#jobFinished(JobParameters, boolean)}
2137da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * or else you can lose work in your queue.</p>
2147da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     *
2157da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * @param work The work you have completed processing, as previously returned by
2167da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     * {@link #dequeueWork()}
2177da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn     */
218f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn    public void completeWork(@NonNull JobWorkItem work) {
2197da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        try {
2207da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            if (!getCallback().completeWork(getJobId(), work.getWorkId())) {
2217da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn                throw new IllegalArgumentException("Given work is not active: " + work);
2227da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            }
2237da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        } catch (RemoteException e) {
2247da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn            throw e.rethrowFromSystemServer();
2257da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn        }
2267da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn    }
2277da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn
2286de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams    /** @hide */
2297060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public IJobCallback getCallback() {
2307060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        return IJobCallback.Stub.asInterface(callback);
2316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2326e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2337060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    private JobParameters(Parcel in) {
2347060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        jobId = in.readInt();
2353d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        extras = in.readPersistableBundle();
236ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        transientExtras = in.readBundle();
237a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        if (in.readInt() != 0) {
238a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData = ClipData.CREATOR.createFromParcel(in);
239a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipGrantFlags = in.readInt();
240a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        } else {
241a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData = null;
242a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipGrantFlags = 0;
243a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        }
2446de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        callback = in.readStrongBinder();
24503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        overrideDeadlineExpired = in.readInt() == 1;
2461a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        mTriggeredContentUris = in.createTypedArray(Uri.CREATOR);
2471a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        mTriggeredContentAuthorities = in.createStringArray();
2485db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        stopReason = in.readInt();
2495db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    }
2505db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge
2515db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    /** @hide */
2525db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge    public void setStopReason(int reason) {
2535db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        stopReason = reason;
2546e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2556e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2566e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2576e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public int describeContents() {
2586e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        return 0;
2596e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2606e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2616e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    @Override
2626e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    public void writeToParcel(Parcel dest, int flags) {
2637060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        dest.writeInt(jobId);
2643d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams        dest.writePersistableBundle(extras);
265ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn        dest.writeBundle(transientExtras);
266a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        if (clipData != null) {
267a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(1);
268a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            clipData.writeToParcel(dest, flags);
269a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(clipGrantFlags);
270a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        } else {
271a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn            dest.writeInt(0);
272a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn        }
2736de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams        dest.writeStrongBinder(callback);
27403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams        dest.writeInt(overrideDeadlineExpired ? 1 : 0);
2751a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        dest.writeTypedArray(mTriggeredContentUris, flags);
2761a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn        dest.writeStringArray(mTriggeredContentAuthorities);
2775db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge        dest.writeInt(stopReason);
2786e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    }
2796e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2807060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate    public static final Creator<JobParameters> CREATOR = new Creator<JobParameters>() {
2816e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2827060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobParameters createFromParcel(Parcel in) {
2837060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobParameters(in);
2846e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
2856e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams
2866e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        @Override
2877060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate        public JobParameters[] newArray(int size) {
2887060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate            return new JobParameters[size];
2896e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams        }
2906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams    };
2916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams}
292