1 /*
2  * Copyright (C) 2023 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.admin;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.annotation.SystemApi;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import java.util.HashSet;
26 import java.util.Objects;
27 import java.util.Set;
28 
29 /**
30  * Class to represent a lock task policy, this is a combination of lock task packages (see
31  * {@link DevicePolicyManager#setLockTaskPackages}) and lock task features (see
32  * {@link DevicePolicyManager#setLockTaskFeatures}.
33  *
34  * @hide
35  */
36 @SystemApi
37 public final class LockTaskPolicy extends PolicyValue<LockTaskPolicy> {
38     /**
39      * @hide
40      */
41     // We default on the power button menu, in order to be consistent with pre-P behaviour
42     public static final int DEFAULT_LOCK_TASK_FLAG =
43             DevicePolicyManager.LOCK_TASK_FEATURE_GLOBAL_ACTIONS;
44 
45     private Set<String> mPackages = new HashSet<>();
46     private int mFlags = DEFAULT_LOCK_TASK_FLAG;
47 
48     /**
49      * Returns the list of packages allowed to start the lock task mode.
50      */
51     @NonNull
getPackages()52     public Set<String> getPackages() {
53         return mPackages;
54     }
55 
56     /**
57      * Returns which system features are enabled for LockTask mode.
58      */
getFlags()59     public @DevicePolicyManager.LockTaskFeature int getFlags() {
60         return mFlags;
61     }
62 
63     // Overriding to hide
64     /**
65      * @hide
66      */
67     @Override
68     @NonNull
getValue()69     public LockTaskPolicy getValue() {
70         return this;
71     }
72 
73     /**
74      * @hide
75      */
LockTaskPolicy(@ullable Set<String> packages)76     public LockTaskPolicy(@Nullable Set<String> packages) {
77         if (packages != null) {
78             mPackages.addAll(packages);
79         }
80         setValue(this);
81     }
82 
83     /**
84      * @hide
85      */
LockTaskPolicy(int flags)86     public LockTaskPolicy(int flags) {
87         mFlags = flags;
88         setValue(this);
89     }
90 
91     /**
92      * @hide
93      */
LockTaskPolicy(@ullable Set<String> packages, int flags)94     public LockTaskPolicy(@Nullable Set<String> packages, int flags) {
95         if (packages != null) {
96             mPackages.addAll(packages);
97         }
98         mFlags = flags;
99         setValue(this);
100     }
101 
LockTaskPolicy(Parcel source)102     private LockTaskPolicy(Parcel source) {
103         int size = source.readInt();
104         mPackages = new HashSet<>();
105         for (int i = 0; i < size; i++) {
106             mPackages.add(source.readString());
107         }
108         mFlags = source.readInt();
109         setValue(this);
110     }
111 
112     /**
113      * @hide
114      */
LockTaskPolicy(LockTaskPolicy policy)115     public LockTaskPolicy(LockTaskPolicy policy) {
116         mPackages = new HashSet<>(policy.mPackages);
117         mFlags = policy.mFlags;
118         setValue(this);
119     }
120 
121     /**
122      * @hide
123      */
setPackages(@onNull Set<String> packages)124     public void setPackages(@NonNull Set<String> packages) {
125         Objects.requireNonNull(packages);
126         mPackages = new HashSet<>(packages);
127     }
128 
129     /**
130      * @hide
131      */
setFlags(int flags)132     public void setFlags(int flags) {
133         mFlags = flags;
134     }
135 
136     @Override
equals(@ullable Object o)137     public boolean equals(@Nullable Object o) {
138         if (this == o) return true;
139         if (o == null || getClass() != o.getClass()) return false;
140         LockTaskPolicy other = (LockTaskPolicy) o;
141         return Objects.equals(mPackages, other.mPackages)
142                 && mFlags == other.mFlags;
143     }
144 
145     @Override
hashCode()146     public int hashCode() {
147         return Objects.hash(mPackages, mFlags);
148     }
149 
150     @Override
toString()151     public String toString() {
152         return "LockTaskPolicy {mPackages= " + String.join(", ", mPackages) + "; mFlags= "
153                 + mFlags + " }";
154     }
155 
156     @Override
describeContents()157     public int describeContents() {
158         return 0;
159     }
160 
161     @Override
writeToParcel(@onNull Parcel dest, int flags)162     public void writeToParcel(@NonNull Parcel dest, int flags) {
163         dest.writeInt(mPackages.size());
164         for (String p : mPackages) {
165             dest.writeString(p);
166         }
167         dest.writeInt(mFlags);
168     }
169 
170     @NonNull
171     public static final Parcelable.Creator<LockTaskPolicy> CREATOR =
172             new Parcelable.Creator<LockTaskPolicy>() {
173                 @Override
174                 public LockTaskPolicy createFromParcel(Parcel source) {
175                     return new LockTaskPolicy(source);
176                 }
177 
178                 @Override
179                 public LockTaskPolicy[] newArray(int size) {
180                     return new LockTaskPolicy[size];
181                 }
182             };
183 }
184