blob: a055ab48c76623806e333ae962885a80e4833742 [file] [log] [blame]
Dianne Hackborn7da13d72017-04-04 17:17:35 -07001/*
2 * Copyright (C) 2017 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
17package android.app.job;
18
Jeff Sharkey8474ca02018-03-26 19:10:02 -060019import static android.app.job.JobInfo.NETWORK_BYTES_UNKNOWN;
20
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -060021import android.annotation.BytesLong;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010022import android.annotation.UnsupportedAppUsage;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070023import android.content.Intent;
Mathew Inwood31755f92018-12-20 13:53:36 +000024import android.os.Build;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070025import android.os.Parcel;
26import android.os.Parcelable;
27
28/**
29 * A unit of work that can be enqueued for a job using
Dianne Hackborn28d1b662017-04-21 14:17:23 -070030 * {@link JobScheduler#enqueue JobScheduler.enqueue}. See
31 * {@link JobParameters#dequeueWork() JobParameters.dequeueWork} for more details.
Dianne Hackborn7da13d72017-04-04 17:17:35 -070032 */
33final public class JobWorkItem implements Parcelable {
Mathew Inwood31755f92018-12-20 13:53:36 +000034 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn7da13d72017-04-04 17:17:35 -070035 final Intent mIntent;
Jeff Sharkey8474ca02018-03-26 19:10:02 -060036 final long mNetworkDownloadBytes;
37 final long mNetworkUploadBytes;
Mathew Inwood31755f92018-12-20 13:53:36 +000038 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn28d1b662017-04-21 14:17:23 -070039 int mDeliveryCount;
Mathew Inwood31755f92018-12-20 13:53:36 +000040 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn7da13d72017-04-04 17:17:35 -070041 int mWorkId;
Mathew Inwood31755f92018-12-20 13:53:36 +000042 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn342e6032017-04-13 18:04:31 -070043 Object mGrants;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070044
45 /**
Dianne Hackborn28d1b662017-04-21 14:17:23 -070046 * Create a new piece of work, which can be submitted to
47 * {@link JobScheduler#enqueue JobScheduler.enqueue}.
48 *
Dianne Hackborn7da13d72017-04-04 17:17:35 -070049 * @param intent The general Intent describing this work.
50 */
51 public JobWorkItem(Intent intent) {
52 mIntent = intent;
Jeff Sharkey8474ca02018-03-26 19:10:02 -060053 mNetworkDownloadBytes = NETWORK_BYTES_UNKNOWN;
54 mNetworkUploadBytes = NETWORK_BYTES_UNKNOWN;
55 }
56
57 /**
58 * @deprecated replaced by {@link #JobWorkItem(Intent, long, long)}
59 * @removed
60 */
61 @Deprecated
62 public JobWorkItem(Intent intent, @BytesLong long networkBytes) {
63 this(intent, networkBytes, NETWORK_BYTES_UNKNOWN);
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -060064 }
65
66 /**
67 * Create a new piece of work, which can be submitted to
68 * {@link JobScheduler#enqueue JobScheduler.enqueue}.
Jeff Sharkey8474ca02018-03-26 19:10:02 -060069 * <p>
70 * See {@link JobInfo.Builder#setEstimatedNetworkBytes(long, long)} for
71 * details about how to estimate network traffic.
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -060072 *
73 * @param intent The general Intent describing this work.
Jeff Sharkey8474ca02018-03-26 19:10:02 -060074 * @param downloadBytes The estimated size of network traffic that will be
75 * downloaded by this job work item, in bytes.
76 * @param uploadBytes The estimated size of network traffic that will be
77 * uploaded by this job work item, in bytes.
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -060078 */
Jeff Sharkey8474ca02018-03-26 19:10:02 -060079 public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) {
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -060080 mIntent = intent;
Jeff Sharkey8474ca02018-03-26 19:10:02 -060081 mNetworkDownloadBytes = downloadBytes;
82 mNetworkUploadBytes = uploadBytes;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070083 }
84
85 /**
86 * Return the Intent associated with this work.
87 */
88 public Intent getIntent() {
89 return mIntent;
90 }
91
92 /**
Jeff Sharkey8474ca02018-03-26 19:10:02 -060093 * @deprecated replaced by {@link #getEstimatedNetworkDownloadBytes()} and
94 * {@link #getEstimatedNetworkUploadBytes()}.
95 * @removed
96 */
97 @Deprecated
98 public @BytesLong long getEstimatedNetworkBytes() {
99 if (mNetworkDownloadBytes == NETWORK_BYTES_UNKNOWN
100 && mNetworkUploadBytes == NETWORK_BYTES_UNKNOWN) {
101 return NETWORK_BYTES_UNKNOWN;
102 } else if (mNetworkDownloadBytes == NETWORK_BYTES_UNKNOWN) {
103 return mNetworkUploadBytes;
104 } else if (mNetworkUploadBytes == NETWORK_BYTES_UNKNOWN) {
105 return mNetworkDownloadBytes;
106 } else {
107 return mNetworkDownloadBytes + mNetworkUploadBytes;
108 }
109 }
110
111 /**
112 * Return the estimated size of download traffic that will be performed by
113 * this job, in bytes.
114 *
115 * @return Estimated size of download traffic, or
116 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown.
117 */
118 public @BytesLong long getEstimatedNetworkDownloadBytes() {
119 return mNetworkDownloadBytes;
120 }
121
122 /**
123 * Return the estimated size of upload traffic that will be performed by
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600124 * this job work item, in bytes.
125 *
Jeff Sharkey8474ca02018-03-26 19:10:02 -0600126 * @return Estimated size of upload traffic, or
127 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown.
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600128 */
Jeff Sharkey8474ca02018-03-26 19:10:02 -0600129 public @BytesLong long getEstimatedNetworkUploadBytes() {
130 return mNetworkUploadBytes;
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600131 }
132
133 /**
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700134 * Return the count of the number of times this work item has been delivered
135 * to the job. The value will be > 1 if it has been redelivered because the job
136 * was stopped or crashed while it had previously been delivered but before the
137 * job had called {@link JobParameters#completeWork JobParameters.completeWork} for it.
138 */
139 public int getDeliveryCount() {
140 return mDeliveryCount;
141 }
142
143 /**
144 * @hide
145 */
146 public void bumpDeliveryCount() {
147 mDeliveryCount++;
148 }
149
150 /**
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700151 * @hide
152 */
153 public void setWorkId(int id) {
154 mWorkId = id;
155 }
156
157 /**
158 * @hide
159 */
160 public int getWorkId() {
161 return mWorkId;
162 }
163
Dianne Hackborn342e6032017-04-13 18:04:31 -0700164 /**
165 * @hide
166 */
167 public void setGrants(Object grants) {
168 mGrants = grants;
169 }
170
171 /**
172 * @hide
173 */
174 public Object getGrants() {
175 return mGrants;
176 }
177
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700178 public String toString() {
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700179 StringBuilder sb = new StringBuilder(64);
180 sb.append("JobWorkItem{id=");
181 sb.append(mWorkId);
182 sb.append(" intent=");
183 sb.append(mIntent);
Jeff Sharkey8474ca02018-03-26 19:10:02 -0600184 if (mNetworkDownloadBytes != NETWORK_BYTES_UNKNOWN) {
185 sb.append(" downloadBytes=");
186 sb.append(mNetworkDownloadBytes);
187 }
188 if (mNetworkUploadBytes != NETWORK_BYTES_UNKNOWN) {
189 sb.append(" uploadBytes=");
190 sb.append(mNetworkUploadBytes);
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600191 }
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700192 if (mDeliveryCount != 0) {
193 sb.append(" dcount=");
194 sb.append(mDeliveryCount);
195 }
196 sb.append("}");
197 return sb.toString();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700198 }
199
200 public int describeContents() {
201 return 0;
202 }
203
204 public void writeToParcel(Parcel out, int flags) {
205 if (mIntent != null) {
206 out.writeInt(1);
207 mIntent.writeToParcel(out, 0);
208 } else {
209 out.writeInt(0);
210 }
Jeff Sharkey8474ca02018-03-26 19:10:02 -0600211 out.writeLong(mNetworkDownloadBytes);
212 out.writeLong(mNetworkUploadBytes);
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700213 out.writeInt(mDeliveryCount);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700214 out.writeInt(mWorkId);
215 }
216
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700217 public static final @android.annotation.NonNull Parcelable.Creator<JobWorkItem> CREATOR
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700218 = new Parcelable.Creator<JobWorkItem>() {
219 public JobWorkItem createFromParcel(Parcel in) {
220 return new JobWorkItem(in);
221 }
222
223 public JobWorkItem[] newArray(int size) {
224 return new JobWorkItem[size];
225 }
226 };
227
Mathew Inwood31755f92018-12-20 13:53:36 +0000228 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700229 JobWorkItem(Parcel in) {
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700230 if (in.readInt() != 0) {
231 mIntent = Intent.CREATOR.createFromParcel(in);
232 } else {
233 mIntent = null;
234 }
Jeff Sharkey8474ca02018-03-26 19:10:02 -0600235 mNetworkDownloadBytes = in.readLong();
236 mNetworkUploadBytes = in.readLong();
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700237 mDeliveryCount = in.readInt();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700238 mWorkId = in.readInt();
239 }
240}