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 19 import static android.app.job.JobInfo.NETWORK_BYTES_UNKNOWN; 20 21 import android.annotation.BytesLong; 22 import android.compat.annotation.UnsupportedAppUsage; 23 import android.content.Intent; 24 import android.os.Build; 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 30 * {@link JobScheduler#enqueue JobScheduler.enqueue}. See 31 * {@link JobParameters#dequeueWork() JobParameters.dequeueWork} for more details. 32 */ 33 final public class JobWorkItem implements Parcelable { 34 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 35 final Intent mIntent; 36 final long mNetworkDownloadBytes; 37 final long mNetworkUploadBytes; 38 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 39 int mDeliveryCount; 40 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 41 int mWorkId; 42 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 43 Object mGrants; 44 45 /** 46 * Create a new piece of work, which can be submitted to 47 * {@link JobScheduler#enqueue JobScheduler.enqueue}. 48 * 49 * @param intent The general Intent describing this work. 50 */ JobWorkItem(Intent intent)51 public JobWorkItem(Intent intent) { 52 mIntent = intent; 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 JobWorkItem(Intent intent, @BytesLong long networkBytes)62 public JobWorkItem(Intent intent, @BytesLong long networkBytes) { 63 this(intent, networkBytes, NETWORK_BYTES_UNKNOWN); 64 } 65 66 /** 67 * Create a new piece of work, which can be submitted to 68 * {@link JobScheduler#enqueue JobScheduler.enqueue}. 69 * <p> 70 * See {@link JobInfo.Builder#setEstimatedNetworkBytes(long, long)} for 71 * details about how to estimate network traffic. 72 * 73 * @param intent The general Intent describing this work. 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. 78 */ JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes)79 public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) { 80 mIntent = intent; 81 mNetworkDownloadBytes = downloadBytes; 82 mNetworkUploadBytes = uploadBytes; 83 } 84 85 /** 86 * Return the Intent associated with this work. 87 */ getIntent()88 public Intent getIntent() { 89 return mIntent; 90 } 91 92 /** 93 * @deprecated replaced by {@link #getEstimatedNetworkDownloadBytes()} and 94 * {@link #getEstimatedNetworkUploadBytes()}. 95 * @removed 96 */ 97 @Deprecated getEstimatedNetworkBytes()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 */ getEstimatedNetworkDownloadBytes()118 public @BytesLong long getEstimatedNetworkDownloadBytes() { 119 return mNetworkDownloadBytes; 120 } 121 122 /** 123 * Return the estimated size of upload traffic that will be performed by 124 * this job work item, in bytes. 125 * 126 * @return Estimated size of upload traffic, or 127 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. 128 */ getEstimatedNetworkUploadBytes()129 public @BytesLong long getEstimatedNetworkUploadBytes() { 130 return mNetworkUploadBytes; 131 } 132 133 /** 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 */ getDeliveryCount()139 public int getDeliveryCount() { 140 return mDeliveryCount; 141 } 142 143 /** 144 * @hide 145 */ bumpDeliveryCount()146 public void bumpDeliveryCount() { 147 mDeliveryCount++; 148 } 149 150 /** 151 * @hide 152 */ setWorkId(int id)153 public void setWorkId(int id) { 154 mWorkId = id; 155 } 156 157 /** 158 * @hide 159 */ getWorkId()160 public int getWorkId() { 161 return mWorkId; 162 } 163 164 /** 165 * @hide 166 */ setGrants(Object grants)167 public void setGrants(Object grants) { 168 mGrants = grants; 169 } 170 171 /** 172 * @hide 173 */ getGrants()174 public Object getGrants() { 175 return mGrants; 176 } 177 toString()178 public String toString() { 179 StringBuilder sb = new StringBuilder(64); 180 sb.append("JobWorkItem{id="); 181 sb.append(mWorkId); 182 sb.append(" intent="); 183 sb.append(mIntent); 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); 191 } 192 if (mDeliveryCount != 0) { 193 sb.append(" dcount="); 194 sb.append(mDeliveryCount); 195 } 196 sb.append("}"); 197 return sb.toString(); 198 } 199 describeContents()200 public int describeContents() { 201 return 0; 202 } 203 writeToParcel(Parcel out, int flags)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 } 211 out.writeLong(mNetworkDownloadBytes); 212 out.writeLong(mNetworkUploadBytes); 213 out.writeInt(mDeliveryCount); 214 out.writeInt(mWorkId); 215 } 216 217 public static final @android.annotation.NonNull Parcelable.Creator<JobWorkItem> CREATOR 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 228 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) JobWorkItem(Parcel in)229 JobWorkItem(Parcel in) { 230 if (in.readInt() != 0) { 231 mIntent = Intent.CREATOR.createFromParcel(in); 232 } else { 233 mIntent = null; 234 } 235 mNetworkDownloadBytes = in.readLong(); 236 mNetworkUploadBytes = in.readLong(); 237 mDeliveryCount = in.readInt(); 238 mWorkId = in.readInt(); 239 } 240 } 241