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.content.Intent; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 26 /** 27 * A unit of work that can be enqueued for a job using 28 * {@link JobScheduler#enqueue JobScheduler.enqueue}. See 29 * {@link JobParameters#dequeueWork() JobParameters.dequeueWork} for more details. 30 */ 31 final public class JobWorkItem implements Parcelable { 32 final Intent mIntent; 33 final long mNetworkDownloadBytes; 34 final long mNetworkUploadBytes; 35 int mDeliveryCount; 36 int mWorkId; 37 Object mGrants; 38 39 /** 40 * Create a new piece of work, which can be submitted to 41 * {@link JobScheduler#enqueue JobScheduler.enqueue}. 42 * 43 * @param intent The general Intent describing this work. 44 */ JobWorkItem(Intent intent)45 public JobWorkItem(Intent intent) { 46 mIntent = intent; 47 mNetworkDownloadBytes = NETWORK_BYTES_UNKNOWN; 48 mNetworkUploadBytes = NETWORK_BYTES_UNKNOWN; 49 } 50 51 /** 52 * @deprecated replaced by {@link #JobWorkItem(Intent, long, long)} 53 * @removed 54 */ 55 @Deprecated JobWorkItem(Intent intent, @BytesLong long networkBytes)56 public JobWorkItem(Intent intent, @BytesLong long networkBytes) { 57 this(intent, networkBytes, NETWORK_BYTES_UNKNOWN); 58 } 59 60 /** 61 * Create a new piece of work, which can be submitted to 62 * {@link JobScheduler#enqueue JobScheduler.enqueue}. 63 * <p> 64 * See {@link JobInfo.Builder#setEstimatedNetworkBytes(long, long)} for 65 * details about how to estimate network traffic. 66 * 67 * @param intent The general Intent describing this work. 68 * @param downloadBytes The estimated size of network traffic that will be 69 * downloaded by this job work item, in bytes. 70 * @param uploadBytes The estimated size of network traffic that will be 71 * uploaded by this job work item, in bytes. 72 */ JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes)73 public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) { 74 mIntent = intent; 75 mNetworkDownloadBytes = downloadBytes; 76 mNetworkUploadBytes = uploadBytes; 77 } 78 79 /** 80 * Return the Intent associated with this work. 81 */ getIntent()82 public Intent getIntent() { 83 return mIntent; 84 } 85 86 /** 87 * @deprecated replaced by {@link #getEstimatedNetworkDownloadBytes()} and 88 * {@link #getEstimatedNetworkUploadBytes()}. 89 * @removed 90 */ 91 @Deprecated getEstimatedNetworkBytes()92 public @BytesLong long getEstimatedNetworkBytes() { 93 if (mNetworkDownloadBytes == NETWORK_BYTES_UNKNOWN 94 && mNetworkUploadBytes == NETWORK_BYTES_UNKNOWN) { 95 return NETWORK_BYTES_UNKNOWN; 96 } else if (mNetworkDownloadBytes == NETWORK_BYTES_UNKNOWN) { 97 return mNetworkUploadBytes; 98 } else if (mNetworkUploadBytes == NETWORK_BYTES_UNKNOWN) { 99 return mNetworkDownloadBytes; 100 } else { 101 return mNetworkDownloadBytes + mNetworkUploadBytes; 102 } 103 } 104 105 /** 106 * Return the estimated size of download traffic that will be performed by 107 * this job, in bytes. 108 * 109 * @return Estimated size of download traffic, or 110 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. 111 */ getEstimatedNetworkDownloadBytes()112 public @BytesLong long getEstimatedNetworkDownloadBytes() { 113 return mNetworkDownloadBytes; 114 } 115 116 /** 117 * Return the estimated size of upload traffic that will be performed by 118 * this job work item, in bytes. 119 * 120 * @return Estimated size of upload traffic, or 121 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. 122 */ getEstimatedNetworkUploadBytes()123 public @BytesLong long getEstimatedNetworkUploadBytes() { 124 return mNetworkUploadBytes; 125 } 126 127 /** 128 * Return the count of the number of times this work item has been delivered 129 * to the job. The value will be > 1 if it has been redelivered because the job 130 * was stopped or crashed while it had previously been delivered but before the 131 * job had called {@link JobParameters#completeWork JobParameters.completeWork} for it. 132 */ getDeliveryCount()133 public int getDeliveryCount() { 134 return mDeliveryCount; 135 } 136 137 /** 138 * @hide 139 */ bumpDeliveryCount()140 public void bumpDeliveryCount() { 141 mDeliveryCount++; 142 } 143 144 /** 145 * @hide 146 */ setWorkId(int id)147 public void setWorkId(int id) { 148 mWorkId = id; 149 } 150 151 /** 152 * @hide 153 */ getWorkId()154 public int getWorkId() { 155 return mWorkId; 156 } 157 158 /** 159 * @hide 160 */ setGrants(Object grants)161 public void setGrants(Object grants) { 162 mGrants = grants; 163 } 164 165 /** 166 * @hide 167 */ getGrants()168 public Object getGrants() { 169 return mGrants; 170 } 171 toString()172 public String toString() { 173 StringBuilder sb = new StringBuilder(64); 174 sb.append("JobWorkItem{id="); 175 sb.append(mWorkId); 176 sb.append(" intent="); 177 sb.append(mIntent); 178 if (mNetworkDownloadBytes != NETWORK_BYTES_UNKNOWN) { 179 sb.append(" downloadBytes="); 180 sb.append(mNetworkDownloadBytes); 181 } 182 if (mNetworkUploadBytes != NETWORK_BYTES_UNKNOWN) { 183 sb.append(" uploadBytes="); 184 sb.append(mNetworkUploadBytes); 185 } 186 if (mDeliveryCount != 0) { 187 sb.append(" dcount="); 188 sb.append(mDeliveryCount); 189 } 190 sb.append("}"); 191 return sb.toString(); 192 } 193 describeContents()194 public int describeContents() { 195 return 0; 196 } 197 writeToParcel(Parcel out, int flags)198 public void writeToParcel(Parcel out, int flags) { 199 if (mIntent != null) { 200 out.writeInt(1); 201 mIntent.writeToParcel(out, 0); 202 } else { 203 out.writeInt(0); 204 } 205 out.writeLong(mNetworkDownloadBytes); 206 out.writeLong(mNetworkUploadBytes); 207 out.writeInt(mDeliveryCount); 208 out.writeInt(mWorkId); 209 } 210 211 public static final Parcelable.Creator<JobWorkItem> CREATOR 212 = new Parcelable.Creator<JobWorkItem>() { 213 public JobWorkItem createFromParcel(Parcel in) { 214 return new JobWorkItem(in); 215 } 216 217 public JobWorkItem[] newArray(int size) { 218 return new JobWorkItem[size]; 219 } 220 }; 221 JobWorkItem(Parcel in)222 JobWorkItem(Parcel in) { 223 if (in.readInt() != 0) { 224 mIntent = Intent.CREATOR.createFromParcel(in); 225 } else { 226 mIntent = null; 227 } 228 mNetworkDownloadBytes = in.readLong(); 229 mNetworkUploadBytes = in.readLong(); 230 mDeliveryCount = in.readInt(); 231 mWorkId = in.readInt(); 232 } 233 } 234