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 * Create a new piece of work, which can be submitted to 59 * {@link JobScheduler#enqueue JobScheduler.enqueue}. 60 * <p> 61 * See {@link JobInfo.Builder#setEstimatedNetworkBytes(long, long)} for 62 * details about how to estimate network traffic. 63 * 64 * @param intent The general Intent describing this work. 65 * @param downloadBytes The estimated size of network traffic that will be 66 * downloaded by this job work item, in bytes. 67 * @param uploadBytes The estimated size of network traffic that will be 68 * uploaded by this job work item, in bytes. 69 */ JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes)70 public JobWorkItem(Intent intent, @BytesLong long downloadBytes, @BytesLong long uploadBytes) { 71 mIntent = intent; 72 mNetworkDownloadBytes = downloadBytes; 73 mNetworkUploadBytes = uploadBytes; 74 } 75 76 /** 77 * Return the Intent associated with this work. 78 */ getIntent()79 public Intent getIntent() { 80 return mIntent; 81 } 82 83 /** 84 * Return the estimated size of download traffic that will be performed by 85 * this job, in bytes. 86 * 87 * @return Estimated size of download traffic, or 88 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. 89 */ getEstimatedNetworkDownloadBytes()90 public @BytesLong long getEstimatedNetworkDownloadBytes() { 91 return mNetworkDownloadBytes; 92 } 93 94 /** 95 * Return the estimated size of upload traffic that will be performed by 96 * this job work item, in bytes. 97 * 98 * @return Estimated size of upload traffic, or 99 * {@link JobInfo#NETWORK_BYTES_UNKNOWN} when unknown. 100 */ getEstimatedNetworkUploadBytes()101 public @BytesLong long getEstimatedNetworkUploadBytes() { 102 return mNetworkUploadBytes; 103 } 104 105 /** 106 * Return the count of the number of times this work item has been delivered 107 * to the job. The value will be > 1 if it has been redelivered because the job 108 * was stopped or crashed while it had previously been delivered but before the 109 * job had called {@link JobParameters#completeWork JobParameters.completeWork} for it. 110 */ getDeliveryCount()111 public int getDeliveryCount() { 112 return mDeliveryCount; 113 } 114 115 /** 116 * @hide 117 */ bumpDeliveryCount()118 public void bumpDeliveryCount() { 119 mDeliveryCount++; 120 } 121 122 /** 123 * @hide 124 */ setWorkId(int id)125 public void setWorkId(int id) { 126 mWorkId = id; 127 } 128 129 /** 130 * @hide 131 */ getWorkId()132 public int getWorkId() { 133 return mWorkId; 134 } 135 136 /** 137 * @hide 138 */ setGrants(Object grants)139 public void setGrants(Object grants) { 140 mGrants = grants; 141 } 142 143 /** 144 * @hide 145 */ getGrants()146 public Object getGrants() { 147 return mGrants; 148 } 149 toString()150 public String toString() { 151 StringBuilder sb = new StringBuilder(64); 152 sb.append("JobWorkItem{id="); 153 sb.append(mWorkId); 154 sb.append(" intent="); 155 sb.append(mIntent); 156 if (mNetworkDownloadBytes != NETWORK_BYTES_UNKNOWN) { 157 sb.append(" downloadBytes="); 158 sb.append(mNetworkDownloadBytes); 159 } 160 if (mNetworkUploadBytes != NETWORK_BYTES_UNKNOWN) { 161 sb.append(" uploadBytes="); 162 sb.append(mNetworkUploadBytes); 163 } 164 if (mDeliveryCount != 0) { 165 sb.append(" dcount="); 166 sb.append(mDeliveryCount); 167 } 168 sb.append("}"); 169 return sb.toString(); 170 } 171 describeContents()172 public int describeContents() { 173 return 0; 174 } 175 writeToParcel(Parcel out, int flags)176 public void writeToParcel(Parcel out, int flags) { 177 if (mIntent != null) { 178 out.writeInt(1); 179 mIntent.writeToParcel(out, 0); 180 } else { 181 out.writeInt(0); 182 } 183 out.writeLong(mNetworkDownloadBytes); 184 out.writeLong(mNetworkUploadBytes); 185 out.writeInt(mDeliveryCount); 186 out.writeInt(mWorkId); 187 } 188 189 public static final @android.annotation.NonNull Parcelable.Creator<JobWorkItem> CREATOR 190 = new Parcelable.Creator<JobWorkItem>() { 191 public JobWorkItem createFromParcel(Parcel in) { 192 return new JobWorkItem(in); 193 } 194 195 public JobWorkItem[] newArray(int size) { 196 return new JobWorkItem[size]; 197 } 198 }; 199 200 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) JobWorkItem(Parcel in)201 JobWorkItem(Parcel in) { 202 if (in.readInt() != 0) { 203 mIntent = Intent.CREATOR.createFromParcel(in); 204 } else { 205 mIntent = null; 206 } 207 mNetworkDownloadBytes = in.readLong(); 208 mNetworkUploadBytes = in.readLong(); 209 mDeliveryCount = in.readInt(); 210 mWorkId = in.readInt(); 211 } 212 } 213