• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             setPackagesInternal(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             setPackagesInternal(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         setPackagesInternal(packages);
127     }
128 
129     /**
130      * @hide
131      */
setFlags(int flags)132     public void setFlags(int flags) {
133         mFlags = flags;
134     }
135 
setPackagesInternal(Set<String> packages)136     private void setPackagesInternal(Set<String> packages) {
137         for (String p : packages) {
138             PolicySizeVerifier.enforceMaxPackageNameLength(p);
139         }
140         mPackages = new HashSet<>(packages);
141     }
142 
143     @Override
equals(@ullable Object o)144     public boolean equals(@Nullable Object o) {
145         if (this == o) return true;
146         if (o == null || getClass() != o.getClass()) return false;
147         LockTaskPolicy other = (LockTaskPolicy) o;
148         return Objects.equals(mPackages, other.mPackages)
149                 && mFlags == other.mFlags;
150     }
151 
152     @Override
hashCode()153     public int hashCode() {
154         return Objects.hash(mPackages, mFlags);
155     }
156 
157     @Override
toString()158     public String toString() {
159         return "LockTaskPolicy {mPackages= " + String.join(", ", mPackages) + "; mFlags= "
160                 + mFlags + " }";
161     }
162 
163     @Override
describeContents()164     public int describeContents() {
165         return 0;
166     }
167 
168     @Override
writeToParcel(@onNull Parcel dest, int flags)169     public void writeToParcel(@NonNull Parcel dest, int flags) {
170         dest.writeInt(mPackages.size());
171         for (String p : mPackages) {
172             dest.writeString(p);
173         }
174         dest.writeInt(mFlags);
175     }
176 
177     @NonNull
178     public static final Parcelable.Creator<LockTaskPolicy> CREATOR =
179             new Parcelable.Creator<LockTaskPolicy>() {
180                 @Override
181                 public LockTaskPolicy createFromParcel(Parcel source) {
182                     return new LockTaskPolicy(source);
183                 }
184 
185                 @Override
186                 public LockTaskPolicy[] newArray(int size) {
187                     return new LockTaskPolicy[size];
188                 }
189             };
190 }
191