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 android.content.Intent;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 /**
24  * A unit of work that can be enqueued for a job using
25  * {@link JobScheduler#enqueue JobScheduler.enqueue}.  See
26  * {@link JobParameters#dequeueWork() JobParameters.dequeueWork} for more details.
27  */
28 final public class JobWorkItem implements Parcelable {
29     final Intent mIntent;
30     int mDeliveryCount;
31     int mWorkId;
32     Object mGrants;
33 
34     /**
35      * Create a new piece of work, which can be submitted to
36      * {@link JobScheduler#enqueue JobScheduler.enqueue}.
37      *
38      * @param intent The general Intent describing this work.
39      */
JobWorkItem(Intent intent)40     public JobWorkItem(Intent intent) {
41         mIntent = intent;
42     }
43 
44     /**
45      * Return the Intent associated with this work.
46      */
getIntent()47     public Intent getIntent() {
48         return mIntent;
49     }
50 
51     /**
52      * Return the count of the number of times this work item has been delivered
53      * to the job.  The value will be > 1 if it has been redelivered because the job
54      * was stopped or crashed while it had previously been delivered but before the
55      * job had called {@link JobParameters#completeWork JobParameters.completeWork} for it.
56      */
getDeliveryCount()57     public int getDeliveryCount() {
58         return mDeliveryCount;
59     }
60 
61     /**
62      * @hide
63      */
bumpDeliveryCount()64     public void bumpDeliveryCount() {
65         mDeliveryCount++;
66     }
67 
68     /**
69      * @hide
70      */
setWorkId(int id)71     public void setWorkId(int id) {
72         mWorkId = id;
73     }
74 
75     /**
76      * @hide
77      */
getWorkId()78     public int getWorkId() {
79         return mWorkId;
80     }
81 
82     /**
83      * @hide
84      */
setGrants(Object grants)85     public void setGrants(Object grants) {
86         mGrants = grants;
87     }
88 
89     /**
90      * @hide
91      */
getGrants()92     public Object getGrants() {
93         return mGrants;
94     }
95 
toString()96     public String toString() {
97         StringBuilder sb = new StringBuilder(64);
98         sb.append("JobWorkItem{id=");
99         sb.append(mWorkId);
100         sb.append(" intent=");
101         sb.append(mIntent);
102         if (mDeliveryCount != 0) {
103             sb.append(" dcount=");
104             sb.append(mDeliveryCount);
105         }
106         sb.append("}");
107         return sb.toString();
108     }
109 
describeContents()110     public int describeContents() {
111         return 0;
112     }
113 
writeToParcel(Parcel out, int flags)114     public void writeToParcel(Parcel out, int flags) {
115         if (mIntent != null) {
116             out.writeInt(1);
117             mIntent.writeToParcel(out, 0);
118         } else {
119             out.writeInt(0);
120         }
121         out.writeInt(mDeliveryCount);
122         out.writeInt(mWorkId);
123     }
124 
125     public static final Parcelable.Creator<JobWorkItem> CREATOR
126             = new Parcelable.Creator<JobWorkItem>() {
127         public JobWorkItem createFromParcel(Parcel in) {
128             return new JobWorkItem(in);
129         }
130 
131         public JobWorkItem[] newArray(int size) {
132             return new JobWorkItem[size];
133         }
134     };
135 
JobWorkItem(Parcel in)136     JobWorkItem(Parcel in) {
137         if (in.readInt() != 0) {
138             mIntent = Intent.CREATOR.createFromParcel(in);
139         } else {
140             mIntent = null;
141         }
142         mDeliveryCount = in.readInt();
143         mWorkId = in.readInt();
144     }
145 }
146