1 /*
2  * Copyright (C) 2018 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.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * Current-state snapshot of a scheduled job.  These snapshots are not used in apps;
24  * they exist only within the system process across the local call surface where JobStatus
25  * is not directly accessible at build time.
26  *
27  * Constraints that the underlying job does not require are always reported as
28  * being currently satisfied.
29  * @hide
30  */
31 public class JobSnapshot implements Parcelable {
32     private final JobInfo mJob;
33     private final int mSatisfiedConstraints;
34     private final boolean mIsRunnable;
35 
JobSnapshot(JobInfo info, int satisfiedMask, boolean runnable)36     public JobSnapshot(JobInfo info, int satisfiedMask, boolean runnable) {
37         mJob = info;
38         mSatisfiedConstraints = satisfiedMask;
39         mIsRunnable = runnable;
40     }
41 
JobSnapshot(Parcel in)42     public JobSnapshot(Parcel in) {
43         mJob = JobInfo.CREATOR.createFromParcel(in);
44         mSatisfiedConstraints = in.readInt();
45         mIsRunnable = in.readBoolean();
46     }
47 
satisfied(int flag)48     private boolean satisfied(int flag) {
49         return (mSatisfiedConstraints & flag) != 0;
50     }
51 
52     /**
53      * Returning JobInfo bound to this snapshot
54      * @return JobInfo of this snapshot
55      */
getJobInfo()56     public JobInfo getJobInfo() {
57         return mJob;
58     }
59 
60     /**
61      * Is this job actually runnable at this moment?
62      */
isRunnable()63     public boolean isRunnable() {
64         return mIsRunnable;
65     }
66 
67     /**
68      * @see JobInfo.Builder#setRequiresCharging(boolean)
69      */
isChargingSatisfied()70     public boolean isChargingSatisfied() {
71         return !mJob.isRequireCharging()
72                 || satisfied(JobInfo.CONSTRAINT_FLAG_CHARGING);
73     }
74 
75     /**
76      * @see JobInfo.Builder#setRequiresBatteryNotLow(boolean)
77      */
isBatteryNotLowSatisfied()78     public boolean isBatteryNotLowSatisfied() {
79         return !mJob.isRequireBatteryNotLow()
80                 || satisfied(JobInfo.CONSTRAINT_FLAG_BATTERY_NOT_LOW);
81     }
82 
83     /**
84      * @see JobInfo.Builder#setRequiresDeviceIdle(boolean)
85      */
isRequireDeviceIdleSatisfied()86     public boolean isRequireDeviceIdleSatisfied() {
87         return !mJob.isRequireDeviceIdle()
88                 || satisfied(JobInfo.CONSTRAINT_FLAG_DEVICE_IDLE);
89     }
90 
isRequireStorageNotLowSatisfied()91     public boolean isRequireStorageNotLowSatisfied() {
92         return !mJob.isRequireStorageNotLow()
93                 || satisfied(JobInfo.CONSTRAINT_FLAG_STORAGE_NOT_LOW);
94     }
95 
96     @Override
describeContents()97     public int describeContents() {
98         return 0;
99     }
100 
101     @Override
writeToParcel(Parcel out, int flags)102     public void writeToParcel(Parcel out, int flags) {
103         mJob.writeToParcel(out, flags);
104         out.writeInt(mSatisfiedConstraints);
105         out.writeBoolean(mIsRunnable);
106     }
107 
108     public static final @android.annotation.NonNull Creator<JobSnapshot> CREATOR = new Creator<JobSnapshot>() {
109         @Override
110         public JobSnapshot createFromParcel(Parcel in) {
111             return new JobSnapshot(in);
112         }
113 
114         @Override
115         public JobSnapshot[] newArray(int size) {
116             return new JobSnapshot[size];
117         }
118     };
119 }
120