JobParameters.java revision 76a0241effeb80cb3bdd15be3efbfa84b6172d6e
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; 2376a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkeyimport android.net.Network; 241a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackbornimport android.net.Uri; 25ba60473a6539d16bef8720d79b5559512303bddfDianne Hackbornimport android.os.Bundle; 266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.IBinder; 276e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcel; 286e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williamsimport android.os.Parcelable; 293d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williamsimport android.os.PersistableBundle; 307da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackbornimport android.os.RemoteException; 316e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 326e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams/** 337060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * Contains the parameters used to configure/identify your job. You do not create this object 346e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * yourself, instead it is handed in to your application by the System. 356e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */ 367060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tatepublic class JobParameters implements Parcelable { 376e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 385db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 395db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public static final int REASON_CANCELED = 0; 405db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 415db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public static final int REASON_CONSTRAINTS_NOT_SATISFIED = 1; 425db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 435db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public static final int REASON_PREEMPT = 2; 445db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 455db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public static final int REASON_TIMEOUT = 3; 465db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 475db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public static final int REASON_DEVICE_IDLE = 4; 485db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge 4994326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn /** @hide */ 5094326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn public static String getReasonName(int reason) { 5194326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn switch (reason) { 5294326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn case REASON_CANCELED: return "canceled"; 5394326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn case REASON_CONSTRAINTS_NOT_SATISFIED: return "constraints"; 5494326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn case REASON_PREEMPT: return "preempt"; 5594326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn case REASON_TIMEOUT: return "timeout"; 5694326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn case REASON_DEVICE_IDLE: return "device_idle"; 5794326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn default: return "unknown:" + reason; 5894326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn } 5994326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn } 6094326cb56aa0c7cee110d6781fb8b8f16fb09663Dianne Hackborn 617060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate private final int jobId; 623d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams private final PersistableBundle extras; 63ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn private final Bundle transientExtras; 64a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn private final ClipData clipData; 65a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn private final int clipGrantFlags; 666de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams private final IBinder callback; 6703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams private final boolean overrideDeadlineExpired; 681a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn private final Uri[] mTriggeredContentUris; 691a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn private final String[] mTriggeredContentAuthorities; 7076a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey private final Network network; 711a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn 725db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge private int stopReason; // Default value of stopReason is REASON_CANCELED 736de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams 746de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams /** @hide */ 7503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams public JobParameters(IBinder callback, int jobId, PersistableBundle extras, 76a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn Bundle transientExtras, ClipData clipData, int clipGrantFlags, 77a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn boolean overrideDeadlineExpired, Uri[] triggeredContentUris, 7876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey String[] triggeredContentAuthorities, Network network) { 797060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate this.jobId = jobId; 806de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams this.extras = extras; 81ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn this.transientExtras = transientExtras; 82a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn this.clipData = clipData; 83a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn this.clipGrantFlags = clipGrantFlags; 846de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams this.callback = callback; 8503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams this.overrideDeadlineExpired = overrideDeadlineExpired; 861a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn this.mTriggeredContentUris = triggeredContentUris; 871a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn this.mTriggeredContentAuthorities = triggeredContentAuthorities; 8876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey this.network = network; 896de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams } 906e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 916e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams /** 927060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * @return The unique id of this job, specified at creation time. 936e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */ 947060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public int getJobId() { 957060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate return jobId; 966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams /** 995db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge * Reason onStopJob() was called on this job. 1005db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge * @hide 1015db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge */ 1025db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public int getStopReason() { 1035db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge return stopReason; 1045db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge } 1055db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge 1065db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** 1077060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * @return The extras you passed in when constructing this job with 1087060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate * {@link android.app.job.JobInfo.Builder#setExtras(android.os.PersistableBundle)}. This will 1096e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams * never be null. If you did not set any extras this will be an empty bundle. 1106e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams */ 111f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @NonNull PersistableBundle getExtras() { 1126e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams return extras; 1136e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 1146e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 11503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams /** 116ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn * @return The transient extras you passed in when constructing this job with 117ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn * {@link android.app.job.JobInfo.Builder#setTransientExtras(android.os.Bundle)}. This will 118ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn * never be null. If you did not set any extras this will be an empty bundle. 119ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn */ 120f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @NonNull Bundle getTransientExtras() { 121ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn return transientExtras; 122ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn } 123ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn 124ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn /** 125a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * @return The clip you passed in when constructing this job with 126a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be null 127a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * if it was not set. 128a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn */ 129f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @Nullable ClipData getClipData() { 130a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn return clipData; 131a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } 132a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn 133a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn /** 134a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * @return The clip grant flags you passed in when constructing this job with 135a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * {@link android.app.job.JobInfo.Builder#setClipData(ClipData, int)}. Will be 0 136a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn * if it was not set. 137a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn */ 138a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn public int getClipGrantFlags() { 139a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn return clipGrantFlags; 140a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } 141a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn 142a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn /** 14303a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams * For jobs with {@link android.app.job.JobInfo.Builder#setOverrideDeadline(long)} set, this 14403a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams * provides an easy way to tell whether the job is being executed due to the deadline 14503a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams * expiring. Note: If the job is running because its deadline expired, it implies that its 14603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams * constraints will not be met. 14703a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams */ 14803a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams public boolean isOverrideDeadlineExpired() { 14903a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams return overrideDeadlineExpired; 15003a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams } 15103a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams 1521a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn /** 1531a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this 1541a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * reports which URIs have triggered the job. This will be null if either no URIs have 1551a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * triggered it (it went off due to a deadline or other reason), or the number of changed 1561a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * URIs is too large to report. Whether or not the number of URIs is too large, you can 1571a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * always use {@link #getTriggeredContentAuthorities()} to determine whether the job was 1581a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * triggered due to any content changes and the authorities they are associated with. 1591a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn */ 160f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @Nullable Uri[] getTriggeredContentUris() { 1611a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn return mTriggeredContentUris; 1621a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn } 1631a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn 1641a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn /** 1651a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * For jobs with {@link android.app.job.JobInfo.Builder#addTriggerContentUri} set, this 1661a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * reports which content authorities have triggered the job. It will only be null if no 1671a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * authorities have triggered it -- that is, the job executed for some other reason, such 1681a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * as a deadline expiring. If this is non-null, you can use {@link #getTriggeredContentUris()} 1691a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * to retrieve the details of which URIs changed (as long as that has not exceeded the maximum 1701a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn * number it can reported). 1711a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn */ 172f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @Nullable String[] getTriggeredContentAuthorities() { 1731a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn return mTriggeredContentAuthorities; 1741a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn } 1751a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn 1767da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn /** 17776a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * Return the network that should be used to perform any network requests 17876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * for this job. 17976a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * <p> 18076a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * Devices may have multiple active network connections simultaneously, or 18176a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * they may not have a default network route at all. To correctly handle all 18276a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * situations like this, your job should always use the network returned by 18376a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * this method instead of implicitly using the default network route. 18476a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * <p> 18576a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * Note that the system may relax the constraints you originally requested, 18676a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * such as allowing a {@link JobInfo#NETWORK_TYPE_UNMETERED} job to run over 18776a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * a metered network when there is a surplus of metered data available. 18876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * 18976a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * @return the network that should be used to perform any network requests 19076a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * for this job, or {@code null} if this job didn't set any required 19176a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * network type. 19276a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey * @see JobInfo.Builder#setRequiredNetworkType(int) 19376a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey */ 19476a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey public @Nullable Network getNetwork() { 19576a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey return network; 19676a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey } 19776a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey 19876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey /** 1997da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * Dequeue the next pending {@link JobWorkItem} from these JobParameters associated with their 2007da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * currently running job. Calling this method when there is no more work available and all 2017da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * previously dequeued work has been completed will result in the system taking care of 2027da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * stopping the job for you -- 2037da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * you should not call {@link JobService#jobFinished(JobParameters, boolean)} yourself 2047da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * (otherwise you risk losing an upcoming JobWorkItem that is being enqueued at the same time). 2057da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * 206fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * <p>Once you are done with the {@link JobWorkItem} returned by this method, you must call 207fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * {@link #completeWork(JobWorkItem)} with it to inform the system that you are done 208fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * executing the work. The job will not be finished until all dequeued work has been 209fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * completed. You do not, however, have to complete each returned work item before deqeueing 210fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * the next one -- you can use {@link #dequeueWork()} multiple times before completing 211fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * previous work if you want to process work in parallel, and you can complete the work 212fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * in whatever order you want.</p> 213fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * 214fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * <p>If the job runs to the end of its available time period before all work has been 215fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * completed, it will stop as normal. You should return true from 216fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * {@link JobService#onStopJob(JobParameters)} in order to have the job rescheduled, and by 217fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * doing so any pending as well as remaining uncompleted work will be re-queued 218fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * for the next time the job runs.</p> 219fd8807accf8ac811818e016fcd4c3dd6e165d379Dianne Hackborn * 22028d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * <p>This example shows how to construct a JobService that will serially dequeue and 22128d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * process work that is available for it:</p> 22228d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * 22328d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * {@sample development/samples/ApiDemos/src/com/example/android/apis/app/JobWorkService.java 22428d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * service} 22528d1b661347a6a7e05dc1004fd7e8436cace8953Dianne Hackborn * 2267da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * @return Returns a new {@link JobWorkItem} if there is one pending, otherwise null. 2277da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * If null is returned, the system will also stop the job if all work has also been completed. 2287da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * (This means that for correct operation, you must always call dequeueWork() after you have 2297da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * completed other work, to check either for more work or allow the system to stop the job.) 2307da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn */ 231f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public @Nullable JobWorkItem dequeueWork() { 2327da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn try { 2337da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn return getCallback().dequeueWork(getJobId()); 2347da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } catch (RemoteException e) { 2357da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn throw e.rethrowFromSystemServer(); 2367da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } 2377da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } 2387da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn 2397da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn /** 2407da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * Report the completion of executing a {@link JobWorkItem} previously returned by 2417da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * {@link #dequeueWork()}. This tells the system you are done with the 2427da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * work associated with that item, so it will not be returned again. Note that if this 2437da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * is the last work in the queue, completing it here will <em>not</em> finish the overall 2447da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * job -- for that to happen, you still need to call {@link #dequeueWork()} 2457da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * again. 2467da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * 2477da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * <p>If you are enqueueing work into a job, you must call this method for each piece 2487da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * of work you process. Do <em>not</em> call 2497da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * {@link JobService#jobFinished(JobParameters, boolean)} 2507da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * or else you can lose work in your queue.</p> 2517da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * 2527da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * @param work The work you have completed processing, as previously returned by 2537da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn * {@link #dequeueWork()} 2547da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn */ 255f9bac16d61db0fceb15484587ecf876c2b802c37Dianne Hackborn public void completeWork(@NonNull JobWorkItem work) { 2567da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn try { 2577da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn if (!getCallback().completeWork(getJobId(), work.getWorkId())) { 2587da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn throw new IllegalArgumentException("Given work is not active: " + work); 2597da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } 2607da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } catch (RemoteException e) { 2617da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn throw e.rethrowFromSystemServer(); 2627da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } 2637da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn } 2647da13d7c3e5b48c0410ae869c5679652de97e5aaDianne Hackborn 2656de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams /** @hide */ 2667060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public IJobCallback getCallback() { 2677060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate return IJobCallback.Stub.asInterface(callback); 2686e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 2696e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 2707060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate private JobParameters(Parcel in) { 2717060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate jobId = in.readInt(); 2723d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams extras = in.readPersistableBundle(); 273ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn transientExtras = in.readBundle(); 274a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn if (in.readInt() != 0) { 275a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn clipData = ClipData.CREATOR.createFromParcel(in); 276a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn clipGrantFlags = in.readInt(); 277a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } else { 278a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn clipData = null; 279a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn clipGrantFlags = 0; 280a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } 2816de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams callback = in.readStrongBinder(); 28203a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams overrideDeadlineExpired = in.readInt() == 1; 2831a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn mTriggeredContentUris = in.createTypedArray(Uri.CREATOR); 2841a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn mTriggeredContentAuthorities = in.createStringArray(); 28576a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey if (in.readInt() != 0) { 28676a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey network = Network.CREATOR.createFromParcel(in); 28776a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey } else { 28876a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey network = null; 28976a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey } 2905db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge stopReason = in.readInt(); 2915db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge } 2925db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge 2935db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge /** @hide */ 2945db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge public void setStopReason(int reason) { 2955db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge stopReason = reason; 2966e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 2976e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 2986e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams @Override 2996e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams public int describeContents() { 3006e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams return 0; 3016e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 3026e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 3036e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams @Override 3046e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams public void writeToParcel(Parcel dest, int flags) { 3057060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate dest.writeInt(jobId); 3063d86fd2bb9db6067c49634bc4c6cdb4d5235ad36Matthew Williams dest.writePersistableBundle(extras); 307ba60473a6539d16bef8720d79b5559512303bddfDianne Hackborn dest.writeBundle(transientExtras); 308a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn if (clipData != null) { 309a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn dest.writeInt(1); 310a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn clipData.writeToParcel(dest, flags); 311a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn dest.writeInt(clipGrantFlags); 312a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } else { 313a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn dest.writeInt(0); 314a47223f99b6b7ade4ae909c458d975eb487062b3Dianne Hackborn } 3156de79e2b17fa0796ea4d39fd9555b563c484248dMatthew Williams dest.writeStrongBinder(callback); 31603a4da6e8e92b19c1345016c06694cb3aabbfc27Matthew Williams dest.writeInt(overrideDeadlineExpired ? 1 : 0); 3171a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn dest.writeTypedArray(mTriggeredContentUris, flags); 3181a30bd9b13fd127d9bbfdc5fd4cb2f80ab7ece21Dianne Hackborn dest.writeStringArray(mTriggeredContentAuthorities); 31976a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey if (network != null) { 32076a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey dest.writeInt(1); 32176a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey network.writeToParcel(dest, flags); 32276a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey } else { 32376a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey dest.writeInt(0); 32476a0241effeb80cb3bdd15be3efbfa84b6172d6eJeff Sharkey } 3255db09084c8e4efc6311754243c39962fc8e7a766Shreyas Basarge dest.writeInt(stopReason); 3266e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 3276e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 3287060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public static final Creator<JobParameters> CREATOR = new Creator<JobParameters>() { 3296e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams @Override 3307060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public JobParameters createFromParcel(Parcel in) { 3317060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate return new JobParameters(in); 3326e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 3336e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams 3346e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams @Override 3357060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate public JobParameters[] newArray(int size) { 3367060b04f6d92351b67222e636ab378a0273bf3e7Christopher Tate return new JobParameters[size]; 3376e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams } 3386e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams }; 3396e31c5c82bc5c9bddf9c01d254067ea5bebbd96bMatthew Williams} 340