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