Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app.job; |
| 18 | |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 19 | import static android.app.job.JobInfo.NETWORK_BYTES_UNKNOWN; |
| 20 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 21 | import android.annotation.BytesLong; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 24 | import android.os.Build; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 25 | import android.os.Parcel; |
| 26 | import android.os.Parcelable; |
| 27 | |
| 28 | /** |
| 29 | * A unit of work that can be enqueued for a job using |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 30 | * {@link JobScheduler#enqueue JobScheduler.enqueue}. See |
| 31 | * {@link JobParameters#dequeueWork() JobParameters.dequeueWork} for more details. |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 32 | */ |
| 33 | final public class JobWorkItem implements Parcelable { |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 34 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 35 | final Intent mIntent; |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 36 | final long mNetworkDownloadBytes; |
| 37 | final long mNetworkUploadBytes; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 38 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 39 | int mDeliveryCount; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 40 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 41 | int mWorkId; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 42 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 43 | Object mGrants; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 46 | * Create a new piece of work, which can be submitted to |
| 47 | * {@link JobScheduler#enqueue JobScheduler.enqueue}. |
| 48 | * |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 49 | * @param intent The general Intent describing this work. |
| 50 | */ |
| 51 | public JobWorkItem(Intent intent) { |
| 52 | mIntent = intent; |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 53 | 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 Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Create a new piece of work, which can be submitted to |
| 68 | * {@link JobScheduler#enqueue JobScheduler.enqueue}. |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 69 | * <p> |
| 70 | * See {@link JobInfo.Builder#setEstimatedNetworkBytes(long, long)} for |
| 71 | * details about how to estimate network traffic. |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 72 | * |
| 73 | * @param intent The general Intent describing this work. |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 74 | * @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 Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 78 | */ |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 79 | public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) { |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 80 | mIntent = intent; |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 81 | mNetworkDownloadBytes = downloadBytes; |
| 82 | mNetworkUploadBytes = uploadBytes; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Return the Intent associated with this work. |
| 87 | */ |
| 88 | public Intent getIntent() { |
| 89 | return mIntent; |
| 90 | } |
| 91 | |
| 92 | /** |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 93 | * @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 Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 124 | * this job work item, in bytes. |
| 125 | * |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 126 | * @return Estimated size of upload traffic, or |
| 127 | * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 128 | */ |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 129 | public @BytesLong long getEstimatedNetworkUploadBytes() { |
| 130 | return mNetworkUploadBytes; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 134 | * 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 Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 151 | * @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 Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 164 | /** |
| 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 Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 178 | public String toString() { |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 179 | StringBuilder sb = new StringBuilder(64); |
| 180 | sb.append("JobWorkItem{id="); |
| 181 | sb.append(mWorkId); |
| 182 | sb.append(" intent="); |
| 183 | sb.append(mIntent); |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 184 | 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 Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 191 | } |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 192 | if (mDeliveryCount != 0) { |
| 193 | sb.append(" dcount="); |
| 194 | sb.append(mDeliveryCount); |
| 195 | } |
| 196 | sb.append("}"); |
| 197 | return sb.toString(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 198 | } |
| 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 Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 211 | out.writeLong(mNetworkDownloadBytes); |
| 212 | out.writeLong(mNetworkUploadBytes); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 213 | out.writeInt(mDeliveryCount); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 214 | out.writeInt(mWorkId); |
| 215 | } |
| 216 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 217 | public static final @android.annotation.NonNull Parcelable.Creator<JobWorkItem> CREATOR |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 218 | = 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 Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 228 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 229 | JobWorkItem(Parcel in) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 230 | if (in.readInt() != 0) { |
| 231 | mIntent = Intent.CREATOR.createFromParcel(in); |
| 232 | } else { |
| 233 | mIntent = null; |
| 234 | } |
Jeff Sharkey | 8474ca0 | 2018-03-26 19:10:02 -0600 | [diff] [blame] | 235 | mNetworkDownloadBytes = in.readLong(); |
| 236 | mNetworkUploadBytes = in.readLong(); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 237 | mDeliveryCount = in.readInt(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 238 | mWorkId = in.readInt(); |
| 239 | } |
| 240 | } |