blob: 121161e1a2f01b0a1b4cb809d627efa5b3cd21fd [file] [log] [blame]
Dianne Hackbornd6847842010-01-12 18:14:19 -08001/*
2 * Copyright (C) 2010 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
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080017package android.app.admin;
Dianne Hackbornd6847842010-01-12 18:14:19 -080018
Xiaohui Chenf85c9772015-09-22 15:08:57 -070019import android.annotation.NonNull;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010020import android.annotation.UnsupportedAppUsage;
Dianne Hackbornd6847842010-01-12 18:14:19 -080021import android.content.ComponentName;
22import android.content.Context;
23import android.content.pm.ActivityInfo;
24import android.content.pm.PackageManager;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080025import android.content.pm.PackageManager.NameNotFoundException;
arangelovc8ee2632018-02-23 16:45:53 +000026import android.content.pm.ResolveInfo;
Dianne Hackborn20cb56e2010-03-04 00:58:29 -080027import android.content.res.Resources;
arangelovc8ee2632018-02-23 16:45:53 +000028import android.content.res.Resources.NotFoundException;
Dianne Hackbornd6847842010-01-12 18:14:19 -080029import android.content.res.TypedArray;
30import android.content.res.XmlResourceParser;
31import android.graphics.drawable.Drawable;
Mathew Inwood31755f92018-12-20 13:53:36 +000032import android.os.Build;
Dianne Hackbornd6847842010-01-12 18:14:19 -080033import android.os.Parcel;
34import android.os.Parcelable;
arangelovc8ee2632018-02-23 16:45:53 +000035import android.os.PersistableBundle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080036import android.util.AttributeSet;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080037import android.util.Log;
Dianne Hackbornd6847842010-01-12 18:14:19 -080038import android.util.Printer;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080039import android.util.SparseArray;
Dianne Hackbornd6847842010-01-12 18:14:19 -080040import android.util.Xml;
41
arangelovc8ee2632018-02-23 16:45:53 +000042import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlPullParserException;
44import org.xmlpull.v1.XmlSerializer;
45
Dianne Hackbornd6847842010-01-12 18:14:19 -080046import java.io.IOException;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080047import java.util.ArrayList;
48import java.util.HashMap;
Dianne Hackbornd6847842010-01-12 18:14:19 -080049
50/**
51 * This class is used to specify meta information of a device administrator
52 * component.
53 */
54public final class DeviceAdminInfo implements Parcelable {
55 static final String TAG = "DeviceAdminInfo";
Jim Millerb8ec4702012-08-31 17:19:10 -070056
Dianne Hackbornd6847842010-01-12 18:14:19 -080057 /**
Robin Lee5c921da2014-03-24 15:11:35 +000058 * A type of policy that this device admin can use: device owner meta-policy
59 * for an admin that is designated as owner of the device.
60 *
61 * @hide
62 */
63 public static final int USES_POLICY_DEVICE_OWNER = -2;
64
65 /**
66 * A type of policy that this device admin can use: profile owner meta-policy
67 * for admins that have been installed as owner of some user profile.
68 *
69 * @hide
70 */
71 public static final int USES_POLICY_PROFILE_OWNER = -1;
72
73 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080074 * A type of policy that this device admin can use: limit the passwords
Dianne Hackborn9327f4f2010-01-29 10:38:29 -080075 * that the user can select, via {@link DevicePolicyManager#setPasswordQuality}
Dianne Hackborn254cb442010-01-27 19:23:59 -080076 * and {@link DevicePolicyManager#setPasswordMinimumLength}.
Jim Millerb8ec4702012-08-31 17:19:10 -070077 *
Eric Sandnessca5969d2018-08-10 13:28:46 +010078 * <p>To control this policy, the device admin must be a device owner or profile owner,
79 * and must have a "limit-password" tag in the "uses-policies" section of its meta-data.
80 * If used by a device owner, the policy only affects the primary user and its profiles,
81 * but not any secondary users on the device.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080082 */
83 public static final int USES_POLICY_LIMIT_PASSWORD = 0;
Jim Millerb8ec4702012-08-31 17:19:10 -070084
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080085 /**
86 * A type of policy that this device admin can use: able to watch login
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080087 * attempts from the user, via {@link DeviceAdminReceiver#ACTION_PASSWORD_FAILED},
88 * {@link DeviceAdminReceiver#ACTION_PASSWORD_SUCCEEDED}, and
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080089 * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts}.
Jim Millerb8ec4702012-08-31 17:19:10 -070090 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -080091 * <p>To control this policy, the device admin must have a "watch-login"
92 * tag in the "uses-policies" section of its meta-data.
93 */
94 public static final int USES_POLICY_WATCH_LOGIN = 1;
95
96 /**
97 * A type of policy that this device admin can use: able to reset the
98 * user's password via
99 * {@link DevicePolicyManager#resetPassword}.
Jim Millerb8ec4702012-08-31 17:19:10 -0700100 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800101 * <p>To control this policy, the device admin must have a "reset-password"
102 * tag in the "uses-policies" section of its meta-data.
103 */
104 public static final int USES_POLICY_RESET_PASSWORD = 2;
105
106 /**
Dianne Hackborn315ada72010-02-11 12:14:08 -0800107 * A type of policy that this device admin can use: able to force the device
108 * to lock via{@link DevicePolicyManager#lockNow} or limit the
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800109 * maximum lock timeout for the device via
110 * {@link DevicePolicyManager#setMaximumTimeToLock}.
Jim Millerb8ec4702012-08-31 17:19:10 -0700111 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800112 * <p>To control this policy, the device admin must have a "force-lock"
113 * tag in the "uses-policies" section of its meta-data.
114 */
Dianne Hackborn315ada72010-02-11 12:14:08 -0800115 public static final int USES_POLICY_FORCE_LOCK = 3;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800116
117 /**
118 * A type of policy that this device admin can use: able to factory
119 * reset the device, erasing all of the user's data, via
120 * {@link DevicePolicyManager#wipeData}.
Jim Millerb8ec4702012-08-31 17:19:10 -0700121 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800122 * <p>To control this policy, the device admin must have a "wipe-data"
123 * tag in the "uses-policies" section of its meta-data.
124 */
Dianne Hackborn315ada72010-02-11 12:14:08 -0800125 public static final int USES_POLICY_WIPE_DATA = 4;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800126
Oscar Montemayor69238c62010-08-03 10:51:06 -0700127 /**
128 * A type of policy that this device admin can use: able to specify the
129 * device Global Proxy, via {@link DevicePolicyManager#setGlobalProxy}.
130 *
131 * <p>To control this policy, the device admin must have a "set-global-proxy"
132 * tag in the "uses-policies" section of its meta-data.
Andy Stadlerd2672722011-02-16 10:53:33 -0800133 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -0700134 */
135 public static final int USES_POLICY_SETS_GLOBAL_PROXY = 5;
136
Jim Millera4e28d12010-11-08 16:15:47 -0800137 /**
138 * A type of policy that this device admin can use: force the user to
139 * change their password after an administrator-defined time limit.
140 *
Eric Sandnessca5969d2018-08-10 13:28:46 +0100141 * <p>To control this policy, the device admin must be a device owner or profile owner,
142 * and must have an "expire-password" tag in the "uses-policies" section of its meta-data.
143 * If used by a device owner, the policy only affects the primary user and its profiles,
144 * but not any secondary users on the device.
Jim Millera4e28d12010-11-08 16:15:47 -0800145 */
146 public static final int USES_POLICY_EXPIRE_PASSWORD = 6;
147
Andy Stadler7b0f8f02011-01-12 14:59:52 -0800148 /**
149 * A type of policy that this device admin can use: require encryption of stored data.
150 *
151 * <p>To control this policy, the device admin must have a "encrypted-storage"
152 * tag in the "uses-policies" section of its meta-data.
153 */
154 public static final int USES_ENCRYPTED_STORAGE = 7;
155
Ben Komalo2447edd2011-05-09 16:05:33 -0700156 /**
157 * A type of policy that this device admin can use: disables use of all device cameras.
158 *
Eric Sandnessca5969d2018-08-10 13:28:46 +0100159 * <p>To control this policy, the device admin must be a device owner or profile owner,
160 * and must have a "disable-camera" tag in the "uses-policies" section of its meta-data.
161 * If used by a device owner, the policy affects all users on the device.
Ben Komalo2447edd2011-05-09 16:05:33 -0700162 */
163 public static final int USES_POLICY_DISABLE_CAMERA = 8;
164
Jim Millerb8ec4702012-08-31 17:19:10 -0700165 /**
Scott Kennedy7ed189e2013-01-11 22:31:43 -0800166 * A type of policy that this device admin can use: disables use of keyguard features.
Jim Millerb8ec4702012-08-31 17:19:10 -0700167 *
Eric Sandnessca5969d2018-08-10 13:28:46 +0100168 * <p>To control this policy, the device admin must be a device owner or profile owner,
169 * and must have a "disable-keyguard-features" tag in the "uses-policies" section of its
170 * meta-data. If used by a device owner, the policy only affects the primary user and
171 * its profiles, but not any secondary users on the device.
Jim Millerb8ec4702012-08-31 17:19:10 -0700172 */
Jim Miller48b9b0d2012-09-19 23:16:50 -0700173 public static final int USES_POLICY_DISABLE_KEYGUARD_FEATURES = 9;
Jim Millerb8ec4702012-08-31 17:19:10 -0700174
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800175 /** @hide */
176 public static class PolicyInfo {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800177 public final int ident;
Mathew Inwood31755f92018-12-20 13:53:36 +0000178 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Fyodor Kupolovb343d6d2015-02-19 10:51:48 -0800179 public final String tag;
180 public final int label;
181 public final int description;
182 public final int labelForSecondaryUsers;
183 public final int descriptionForSecondaryUsers;
Jim Millerb8ec4702012-08-31 17:19:10 -0700184
Fyodor Kupolovb343d6d2015-02-19 10:51:48 -0800185 public PolicyInfo(int ident, String tag, int label, int description) {
186 this(ident, tag, label, description, label, description);
187 }
188
189 public PolicyInfo(int ident, String tag, int label, int description,
190 int labelForSecondaryUsers, int descriptionForSecondaryUsers) {
191 this.ident = ident;
192 this.tag = tag;
193 this.label = label;
194 this.description = description;
195 this.labelForSecondaryUsers = labelForSecondaryUsers;
196 this.descriptionForSecondaryUsers = descriptionForSecondaryUsers;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800197 }
198 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700199
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800200 static ArrayList<PolicyInfo> sPoliciesDisplayOrder = new ArrayList<PolicyInfo>();
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800201 static HashMap<String, Integer> sKnownPolicies = new HashMap<String, Integer>();
202 static SparseArray<PolicyInfo> sRevKnownPolicies = new SparseArray<PolicyInfo>();
Jim Millerb8ec4702012-08-31 17:19:10 -0700203
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800204 static {
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800205 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WIPE_DATA, "wipe-data",
206 com.android.internal.R.string.policylab_wipeData,
Fyodor Kupolovb343d6d2015-02-19 10:51:48 -0800207 com.android.internal.R.string.policydesc_wipeData,
208 com.android.internal.R.string.policylab_wipeData_secondaryUser,
209 com.android.internal.R.string.policydesc_wipeData_secondaryUser
210 ));
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800211 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_RESET_PASSWORD, "reset-password",
212 com.android.internal.R.string.policylab_resetPassword,
213 com.android.internal.R.string.policydesc_resetPassword));
214 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_LIMIT_PASSWORD, "limit-password",
215 com.android.internal.R.string.policylab_limitPassword,
216 com.android.internal.R.string.policydesc_limitPassword));
217 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WATCH_LOGIN, "watch-login",
218 com.android.internal.R.string.policylab_watchLogin,
Fyodor Kupolovb343d6d2015-02-19 10:51:48 -0800219 com.android.internal.R.string.policydesc_watchLogin,
220 com.android.internal.R.string.policylab_watchLogin,
221 com.android.internal.R.string.policydesc_watchLogin_secondaryUser
222 ));
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800223 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_FORCE_LOCK, "force-lock",
224 com.android.internal.R.string.policylab_forceLock,
225 com.android.internal.R.string.policydesc_forceLock));
Oscar Montemayor69238c62010-08-03 10:51:06 -0700226 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_SETS_GLOBAL_PROXY, "set-global-proxy",
227 com.android.internal.R.string.policylab_setGlobalProxy,
228 com.android.internal.R.string.policydesc_setGlobalProxy));
Jim Millera4e28d12010-11-08 16:15:47 -0800229 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_EXPIRE_PASSWORD, "expire-password",
230 com.android.internal.R.string.policylab_expirePassword,
231 com.android.internal.R.string.policydesc_expirePassword));
Andy Stadler7b0f8f02011-01-12 14:59:52 -0800232 sPoliciesDisplayOrder.add(new PolicyInfo(USES_ENCRYPTED_STORAGE, "encrypted-storage",
233 com.android.internal.R.string.policylab_encryptedStorage,
234 com.android.internal.R.string.policydesc_encryptedStorage));
Ben Komalo2447edd2011-05-09 16:05:33 -0700235 sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_DISABLE_CAMERA, "disable-camera",
236 com.android.internal.R.string.policylab_disableCamera,
237 com.android.internal.R.string.policydesc_disableCamera));
Jim Millerb8ec4702012-08-31 17:19:10 -0700238 sPoliciesDisplayOrder.add(new PolicyInfo(
Jim Miller48b9b0d2012-09-19 23:16:50 -0700239 USES_POLICY_DISABLE_KEYGUARD_FEATURES, "disable-keyguard-features",
240 com.android.internal.R.string.policylab_disableKeyguardFeatures,
241 com.android.internal.R.string.policydesc_disableKeyguardFeatures));
Jim Millera4e28d12010-11-08 16:15:47 -0800242
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800243 for (int i=0; i<sPoliciesDisplayOrder.size(); i++) {
244 PolicyInfo pi = sPoliciesDisplayOrder.get(i);
245 sRevKnownPolicies.put(pi.ident, pi);
246 sKnownPolicies.put(pi.tag, pi.ident);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800247 }
248 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700249
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800250 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800251 * The BroadcastReceiver that implements this device admin component.
252 */
Makoto Onuki889c0882016-02-16 15:20:30 -0800253 final ActivityInfo mActivityInfo;
Jim Millerb8ec4702012-08-31 17:19:10 -0700254
Dianne Hackbornd6847842010-01-12 18:14:19 -0800255 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800256 * Whether this should be visible to the user.
257 */
258 boolean mVisible;
Jim Millerb8ec4702012-08-31 17:19:10 -0700259
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800260 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800261 * The policies this administrator needs access to.
262 */
263 int mUsesPolicies;
Jim Millerb8ec4702012-08-31 17:19:10 -0700264
arangelovc8ee2632018-02-23 16:45:53 +0000265 /**
266 * Whether this administrator can be a target in an ownership transfer.
267 *
268 * @see DevicePolicyManager#transferOwnership(ComponentName, ComponentName, PersistableBundle)
269 */
270 boolean mSupportsTransferOwnership;
Makoto Onuki889c0882016-02-16 15:20:30 -0800271
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800272 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800273 * Constructor.
Jim Millerb8ec4702012-08-31 17:19:10 -0700274 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800275 * @param context The Context in which we are parsing the device admin.
Makoto Onuki889c0882016-02-16 15:20:30 -0800276 * @param resolveInfo The ResolveInfo returned from the package manager about
Dianne Hackbornd6847842010-01-12 18:14:19 -0800277 * this device admin's component.
278 */
Makoto Onuki889c0882016-02-16 15:20:30 -0800279 public DeviceAdminInfo(Context context, ResolveInfo resolveInfo)
Dianne Hackbornd6847842010-01-12 18:14:19 -0800280 throws XmlPullParserException, IOException {
Makoto Onuki889c0882016-02-16 15:20:30 -0800281 this(context, resolveInfo.activityInfo);
282 }
283 /**
284 * Constructor.
285 *
286 * @param context The Context in which we are parsing the device admin.
287 * @param activityInfo The ActivityInfo returned from the package manager about
288 * this device admin's component.
289 *
290 * @hide
291 */
292 public DeviceAdminInfo(Context context, ActivityInfo activityInfo)
293 throws XmlPullParserException, IOException {
294 mActivityInfo = activityInfo;
Jim Millerb8ec4702012-08-31 17:19:10 -0700295
Dianne Hackbornd6847842010-01-12 18:14:19 -0800296 PackageManager pm = context.getPackageManager();
Jim Millerb8ec4702012-08-31 17:19:10 -0700297
Dianne Hackbornd6847842010-01-12 18:14:19 -0800298 XmlResourceParser parser = null;
299 try {
Makoto Onuki889c0882016-02-16 15:20:30 -0800300 parser = mActivityInfo.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800301 if (parser == null) {
302 throw new XmlPullParserException("No "
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800303 + DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data");
Dianne Hackbornd6847842010-01-12 18:14:19 -0800304 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700305
Makoto Onuki889c0882016-02-16 15:20:30 -0800306 Resources res = pm.getResourcesForApplication(mActivityInfo.applicationInfo);
Jim Millerb8ec4702012-08-31 17:19:10 -0700307
Dianne Hackbornd6847842010-01-12 18:14:19 -0800308 AttributeSet attrs = Xml.asAttributeSet(parser);
Jim Millerb8ec4702012-08-31 17:19:10 -0700309
Dianne Hackbornd6847842010-01-12 18:14:19 -0800310 int type;
311 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
312 && type != XmlPullParser.START_TAG) {
313 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700314
Dianne Hackbornd6847842010-01-12 18:14:19 -0800315 String nodeName = parser.getName();
316 if (!"device-admin".equals(nodeName)) {
317 throw new XmlPullParserException(
318 "Meta-data does not start with device-admin tag");
319 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700320
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800321 TypedArray sa = res.obtainAttributes(attrs,
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800322 com.android.internal.R.styleable.DeviceAdmin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800323
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800324 mVisible = sa.getBoolean(
325 com.android.internal.R.styleable.DeviceAdmin_visible, true);
Jim Millerb8ec4702012-08-31 17:19:10 -0700326
Dianne Hackbornd6847842010-01-12 18:14:19 -0800327 sa.recycle();
Jim Millerb8ec4702012-08-31 17:19:10 -0700328
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800329 int outerDepth = parser.getDepth();
330 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
331 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
332 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
333 continue;
334 }
335 String tagName = parser.getName();
336 if (tagName.equals("uses-policies")) {
337 int innerDepth = parser.getDepth();
338 while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
339 && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
340 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
341 continue;
342 }
343 String policyName = parser.getName();
344 Integer val = sKnownPolicies.get(policyName);
345 if (val != null) {
346 mUsesPolicies |= 1 << val.intValue();
347 } else {
348 Log.w(TAG, "Unknown tag under uses-policies of "
349 + getComponent() + ": " + policyName);
350 }
351 }
arangelovc8ee2632018-02-23 16:45:53 +0000352 } else if (tagName.equals("support-transfer-ownership")) {
353 if (parser.next() != XmlPullParser.END_TAG) {
354 throw new XmlPullParserException(
355 "support-transfer-ownership tag must be empty.");
356 }
357 mSupportsTransferOwnership = true;
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800358 }
359 }
Dianne Hackborn20cb56e2010-03-04 00:58:29 -0800360 } catch (NameNotFoundException e) {
361 throw new XmlPullParserException(
Makoto Onuki889c0882016-02-16 15:20:30 -0800362 "Unable to create context for: " + mActivityInfo.packageName);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800363 } finally {
364 if (parser != null) parser.close();
365 }
366 }
367
368 DeviceAdminInfo(Parcel source) {
Makoto Onuki889c0882016-02-16 15:20:30 -0800369 mActivityInfo = ActivityInfo.CREATOR.createFromParcel(source);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800370 mUsesPolicies = source.readInt();
arangelovc8ee2632018-02-23 16:45:53 +0000371 mSupportsTransferOwnership = source.readBoolean();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800372 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700373
Dianne Hackbornd6847842010-01-12 18:14:19 -0800374 /**
375 * Return the .apk package that implements this device admin.
376 */
377 public String getPackageName() {
Makoto Onuki889c0882016-02-16 15:20:30 -0800378 return mActivityInfo.packageName;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800379 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700380
Dianne Hackbornd6847842010-01-12 18:14:19 -0800381 /**
382 * Return the class name of the receiver component that implements
383 * this device admin.
384 */
385 public String getReceiverName() {
Makoto Onuki889c0882016-02-16 15:20:30 -0800386 return mActivityInfo.name;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800387 }
388
389 /**
390 * Return the raw information about the receiver implementing this
391 * device admin. Do not modify the returned object.
392 */
393 public ActivityInfo getActivityInfo() {
Makoto Onuki889c0882016-02-16 15:20:30 -0800394 return mActivityInfo;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800395 }
396
397 /**
398 * Return the component of the receiver that implements this device admin.
399 */
Xiaohui Chenf85c9772015-09-22 15:08:57 -0700400 @NonNull
Dianne Hackbornd6847842010-01-12 18:14:19 -0800401 public ComponentName getComponent() {
Makoto Onuki889c0882016-02-16 15:20:30 -0800402 return new ComponentName(mActivityInfo.packageName,
403 mActivityInfo.name);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800404 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700405
Dianne Hackbornd6847842010-01-12 18:14:19 -0800406 /**
407 * Load the user-displayed label for this device admin.
Jim Millerb8ec4702012-08-31 17:19:10 -0700408 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800409 * @param pm Supply a PackageManager used to load the device admin's
410 * resources.
411 */
412 public CharSequence loadLabel(PackageManager pm) {
Makoto Onuki889c0882016-02-16 15:20:30 -0800413 return mActivityInfo.loadLabel(pm);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800414 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700415
Dianne Hackbornd6847842010-01-12 18:14:19 -0800416 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800417 * Load user-visible description associated with this device admin.
Jim Millerb8ec4702012-08-31 17:19:10 -0700418 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800419 * @param pm Supply a PackageManager used to load the device admin's
420 * resources.
421 */
422 public CharSequence loadDescription(PackageManager pm) throws NotFoundException {
Makoto Onuki889c0882016-02-16 15:20:30 -0800423 if (mActivityInfo.descriptionRes != 0) {
424 return pm.getText(mActivityInfo.packageName,
425 mActivityInfo.descriptionRes, mActivityInfo.applicationInfo);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800426 }
427 throw new NotFoundException();
428 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700429
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800430 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800431 * Load the user-displayed icon for this device admin.
Jim Millerb8ec4702012-08-31 17:19:10 -0700432 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800433 * @param pm Supply a PackageManager used to load the device admin's
434 * resources.
435 */
436 public Drawable loadIcon(PackageManager pm) {
Makoto Onuki889c0882016-02-16 15:20:30 -0800437 return mActivityInfo.loadIcon(pm);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800438 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700439
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800440 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800441 * Returns whether this device admin would like to be visible to the
442 * user, even when it is not enabled.
443 */
444 public boolean isVisible() {
445 return mVisible;
446 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700447
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800448 /**
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800449 * Return true if the device admin has requested that it be able to use
450 * the given policy control. The possible policy identifier inputs are:
451 * {@link #USES_POLICY_LIMIT_PASSWORD}, {@link #USES_POLICY_WATCH_LOGIN},
Raphaeldc2df322010-02-11 17:01:16 -0800452 * {@link #USES_POLICY_RESET_PASSWORD}, {@link #USES_POLICY_FORCE_LOCK},
Andy Stadlerd2672722011-02-16 10:53:33 -0800453 * {@link #USES_POLICY_WIPE_DATA},
Ben Komalo2447edd2011-05-09 16:05:33 -0700454 * {@link #USES_POLICY_EXPIRE_PASSWORD}, {@link #USES_ENCRYPTED_STORAGE},
455 * {@link #USES_POLICY_DISABLE_CAMERA}.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800456 */
457 public boolean usesPolicy(int policyIdent) {
458 return (mUsesPolicies & (1<<policyIdent)) != 0;
459 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700460
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800461 /**
462 * Return the XML tag name for the given policy identifier. Valid identifiers
463 * are as per {@link #usesPolicy(int)}. If the given identifier is not
464 * known, null is returned.
465 */
466 public String getTagForPolicy(int policyIdent) {
467 return sRevKnownPolicies.get(policyIdent).tag;
468 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700469
arangelovc8ee2632018-02-23 16:45:53 +0000470 /**
471 * Return true if this administrator can be a target in an ownership transfer.
472 */
473 public boolean supportsTransferOwnership() {
474 return mSupportsTransferOwnership;
475 }
476
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800477 /** @hide */
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100478 @UnsupportedAppUsage
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800479 public ArrayList<PolicyInfo> getUsedPolicies() {
480 ArrayList<PolicyInfo> res = new ArrayList<PolicyInfo>();
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800481 for (int i=0; i<sPoliciesDisplayOrder.size(); i++) {
482 PolicyInfo pi = sPoliciesDisplayOrder.get(i);
483 if (usesPolicy(pi.ident)) {
484 res.add(pi);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800485 }
486 }
487 return res;
488 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700489
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800490 /** @hide */
491 public void writePoliciesToXml(XmlSerializer out)
492 throws IllegalArgumentException, IllegalStateException, IOException {
493 out.attribute(null, "flags", Integer.toString(mUsesPolicies));
494 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700495
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800496 /** @hide */
497 public void readPoliciesFromXml(XmlPullParser parser)
498 throws XmlPullParserException, IOException {
499 mUsesPolicies = Integer.parseInt(
500 parser.getAttributeValue(null, "flags"));
501 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700502
Dianne Hackbornd6847842010-01-12 18:14:19 -0800503 public void dump(Printer pw, String prefix) {
504 pw.println(prefix + "Receiver:");
Makoto Onuki889c0882016-02-16 15:20:30 -0800505 mActivityInfo.dump(pw, prefix + " ");
Dianne Hackbornd6847842010-01-12 18:14:19 -0800506 }
Jim Millerb8ec4702012-08-31 17:19:10 -0700507
Dianne Hackbornd6847842010-01-12 18:14:19 -0800508 @Override
509 public String toString() {
Makoto Onuki889c0882016-02-16 15:20:30 -0800510 return "DeviceAdminInfo{" + mActivityInfo.name + "}";
Dianne Hackbornd6847842010-01-12 18:14:19 -0800511 }
512
513 /**
514 * Used to package this object into a {@link Parcel}.
Jim Millerb8ec4702012-08-31 17:19:10 -0700515 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800516 * @param dest The {@link Parcel} to be written.
517 * @param flags The flags used for parceling.
518 */
519 public void writeToParcel(Parcel dest, int flags) {
Makoto Onuki889c0882016-02-16 15:20:30 -0800520 mActivityInfo.writeToParcel(dest, flags);
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800521 dest.writeInt(mUsesPolicies);
arangelovc8ee2632018-02-23 16:45:53 +0000522 dest.writeBoolean(mSupportsTransferOwnership);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800523 }
524
525 /**
526 * Used to make this class parcelable.
527 */
528 public static final Parcelable.Creator<DeviceAdminInfo> CREATOR =
529 new Parcelable.Creator<DeviceAdminInfo>() {
530 public DeviceAdminInfo createFromParcel(Parcel source) {
531 return new DeviceAdminInfo(source);
532 }
533
534 public DeviceAdminInfo[] newArray(int size) {
535 return new DeviceAdminInfo[size];
536 }
537 };
538
539 public int describeContents() {
540 return 0;
541 }
542}