Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1 | /* |
| 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 Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 17 | package android.app.admin; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 18 | |
Xiaohui Chen | f85c977 | 2015-09-22 15:08:57 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.pm.ActivityInfo; |
| 24 | import android.content.pm.PackageManager; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 25 | import android.content.pm.PackageManager.NameNotFoundException; |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 26 | import android.content.pm.ResolveInfo; |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 27 | import android.content.res.Resources; |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 28 | import android.content.res.Resources.NotFoundException; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 29 | import android.content.res.TypedArray; |
| 30 | import android.content.res.XmlResourceParser; |
| 31 | import android.graphics.drawable.Drawable; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 32 | import android.os.Build; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 33 | import android.os.Parcel; |
| 34 | import android.os.Parcelable; |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 35 | import android.os.PersistableBundle; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 36 | import android.util.AttributeSet; |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 37 | import android.util.Log; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 38 | import android.util.Printer; |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 39 | import android.util.SparseArray; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | import android.util.Xml; |
| 41 | |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 42 | import org.xmlpull.v1.XmlPullParser; |
| 43 | import org.xmlpull.v1.XmlPullParserException; |
| 44 | import org.xmlpull.v1.XmlSerializer; |
| 45 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 46 | import java.io.IOException; |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.HashMap; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * This class is used to specify meta information of a device administrator |
| 52 | * component. |
| 53 | */ |
| 54 | public final class DeviceAdminInfo implements Parcelable { |
| 55 | static final String TAG = "DeviceAdminInfo"; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 56 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 57 | /** |
Robin Lee | 5c921da | 2014-03-24 15:11:35 +0000 | [diff] [blame] | 58 | * 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 74 | * A type of policy that this device admin can use: limit the passwords |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 75 | * that the user can select, via {@link DevicePolicyManager#setPasswordQuality} |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 76 | * and {@link DevicePolicyManager#setPasswordMinimumLength}. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 77 | * |
Eric Sandness | ca5969d | 2018-08-10 13:28:46 +0100 | [diff] [blame] | 78 | * <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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 82 | */ |
| 83 | public static final int USES_POLICY_LIMIT_PASSWORD = 0; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 84 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 85 | /** |
| 86 | * A type of policy that this device admin can use: able to watch login |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 87 | * attempts from the user, via {@link DeviceAdminReceiver#ACTION_PASSWORD_FAILED}, |
| 88 | * {@link DeviceAdminReceiver#ACTION_PASSWORD_SUCCEEDED}, and |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 89 | * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts}. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 90 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 91 | * <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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 100 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 101 | * <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 Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 107 | * 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 109 | * maximum lock timeout for the device via |
| 110 | * {@link DevicePolicyManager#setMaximumTimeToLock}. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 111 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 112 | * <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 Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 115 | public static final int USES_POLICY_FORCE_LOCK = 3; |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 116 | |
| 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 121 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 122 | * <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 Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 125 | public static final int USES_POLICY_WIPE_DATA = 4; |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 126 | |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 127 | /** |
| 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 Stadler | d267272 | 2011-02-16 10:53:33 -0800 | [diff] [blame] | 133 | * @hide |
Oscar Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 134 | */ |
| 135 | public static final int USES_POLICY_SETS_GLOBAL_PROXY = 5; |
| 136 | |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 137 | /** |
| 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 Sandness | ca5969d | 2018-08-10 13:28:46 +0100 | [diff] [blame] | 141 | * <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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 145 | */ |
| 146 | public static final int USES_POLICY_EXPIRE_PASSWORD = 6; |
| 147 | |
Andy Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 148 | /** |
| 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 Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 156 | /** |
| 157 | * A type of policy that this device admin can use: disables use of all device cameras. |
| 158 | * |
Eric Sandness | ca5969d | 2018-08-10 13:28:46 +0100 | [diff] [blame] | 159 | * <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 Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 162 | */ |
| 163 | public static final int USES_POLICY_DISABLE_CAMERA = 8; |
| 164 | |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 165 | /** |
Scott Kennedy | 7ed189e | 2013-01-11 22:31:43 -0800 | [diff] [blame] | 166 | * A type of policy that this device admin can use: disables use of keyguard features. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 167 | * |
Eric Sandness | ca5969d | 2018-08-10 13:28:46 +0100 | [diff] [blame] | 168 | * <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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 172 | */ |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 173 | public static final int USES_POLICY_DISABLE_KEYGUARD_FEATURES = 9; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 174 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 175 | /** @hide */ |
| 176 | public static class PolicyInfo { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 177 | public final int ident; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 178 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Fyodor Kupolov | b343d6d | 2015-02-19 10:51:48 -0800 | [diff] [blame] | 179 | 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 184 | |
Fyodor Kupolov | b343d6d | 2015-02-19 10:51:48 -0800 | [diff] [blame] | 185 | 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 199 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 200 | static ArrayList<PolicyInfo> sPoliciesDisplayOrder = new ArrayList<PolicyInfo>(); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 201 | static HashMap<String, Integer> sKnownPolicies = new HashMap<String, Integer>(); |
| 202 | static SparseArray<PolicyInfo> sRevKnownPolicies = new SparseArray<PolicyInfo>(); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 203 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 204 | static { |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 205 | sPoliciesDisplayOrder.add(new PolicyInfo(USES_POLICY_WIPE_DATA, "wipe-data", |
| 206 | com.android.internal.R.string.policylab_wipeData, |
Fyodor Kupolov | b343d6d | 2015-02-19 10:51:48 -0800 | [diff] [blame] | 207 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 211 | 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 Kupolov | b343d6d | 2015-02-19 10:51:48 -0800 | [diff] [blame] | 219 | 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 Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 223 | 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 Montemayor | 69238c6 | 2010-08-03 10:51:06 -0700 | [diff] [blame] | 226 | 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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 229 | 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 Stadler | 7b0f8f0 | 2011-01-12 14:59:52 -0800 | [diff] [blame] | 232 | 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 Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 235 | 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 238 | sPoliciesDisplayOrder.add(new PolicyInfo( |
Jim Miller | 48b9b0d | 2012-09-19 23:16:50 -0700 | [diff] [blame] | 239 | 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 Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 242 | |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 243 | 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 247 | } |
| 248 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 249 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 250 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 251 | * The BroadcastReceiver that implements this device admin component. |
| 252 | */ |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 253 | final ActivityInfo mActivityInfo; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 254 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 255 | /** |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 256 | * Whether this should be visible to the user. |
| 257 | */ |
| 258 | boolean mVisible; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 259 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 260 | /** |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 261 | * The policies this administrator needs access to. |
| 262 | */ |
| 263 | int mUsesPolicies; |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 264 | |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 265 | /** |
| 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 Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 271 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 272 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 273 | * Constructor. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 274 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 275 | * @param context The Context in which we are parsing the device admin. |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 276 | * @param resolveInfo The ResolveInfo returned from the package manager about |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 277 | * this device admin's component. |
| 278 | */ |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 279 | public DeviceAdminInfo(Context context, ResolveInfo resolveInfo) |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 280 | throws XmlPullParserException, IOException { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 281 | 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 295 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 296 | PackageManager pm = context.getPackageManager(); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 297 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 298 | XmlResourceParser parser = null; |
| 299 | try { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 300 | parser = mActivityInfo.loadXmlMetaData(pm, DeviceAdminReceiver.DEVICE_ADMIN_META_DATA); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 301 | if (parser == null) { |
| 302 | throw new XmlPullParserException("No " |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 303 | + DeviceAdminReceiver.DEVICE_ADMIN_META_DATA + " meta-data"); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 304 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 305 | |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 306 | Resources res = pm.getResourcesForApplication(mActivityInfo.applicationInfo); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 307 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 308 | AttributeSet attrs = Xml.asAttributeSet(parser); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 309 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 310 | int type; |
| 311 | while ((type=parser.next()) != XmlPullParser.END_DOCUMENT |
| 312 | && type != XmlPullParser.START_TAG) { |
| 313 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 314 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 315 | 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 320 | |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 321 | TypedArray sa = res.obtainAttributes(attrs, |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 322 | com.android.internal.R.styleable.DeviceAdmin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 323 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 324 | mVisible = sa.getBoolean( |
| 325 | com.android.internal.R.styleable.DeviceAdmin_visible, true); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 326 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 327 | sa.recycle(); |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 328 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 329 | 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 | } |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 352 | } 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
Dianne Hackborn | 20cb56e | 2010-03-04 00:58:29 -0800 | [diff] [blame] | 360 | } catch (NameNotFoundException e) { |
| 361 | throw new XmlPullParserException( |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 362 | "Unable to create context for: " + mActivityInfo.packageName); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 363 | } finally { |
| 364 | if (parser != null) parser.close(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | DeviceAdminInfo(Parcel source) { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 369 | mActivityInfo = ActivityInfo.CREATOR.createFromParcel(source); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 370 | mUsesPolicies = source.readInt(); |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 371 | mSupportsTransferOwnership = source.readBoolean(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 372 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 373 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 374 | /** |
| 375 | * Return the .apk package that implements this device admin. |
| 376 | */ |
| 377 | public String getPackageName() { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 378 | return mActivityInfo.packageName; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 379 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 380 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 381 | /** |
| 382 | * Return the class name of the receiver component that implements |
| 383 | * this device admin. |
| 384 | */ |
| 385 | public String getReceiverName() { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 386 | return mActivityInfo.name; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 387 | } |
| 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 Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 394 | return mActivityInfo; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Return the component of the receiver that implements this device admin. |
| 399 | */ |
Xiaohui Chen | f85c977 | 2015-09-22 15:08:57 -0700 | [diff] [blame] | 400 | @NonNull |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 401 | public ComponentName getComponent() { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 402 | return new ComponentName(mActivityInfo.packageName, |
| 403 | mActivityInfo.name); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 404 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 405 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 406 | /** |
| 407 | * Load the user-displayed label for this device admin. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 408 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 409 | * @param pm Supply a PackageManager used to load the device admin's |
| 410 | * resources. |
| 411 | */ |
| 412 | public CharSequence loadLabel(PackageManager pm) { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 413 | return mActivityInfo.loadLabel(pm); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 414 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 415 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 416 | /** |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 417 | * Load user-visible description associated with this device admin. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 418 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 419 | * @param pm Supply a PackageManager used to load the device admin's |
| 420 | * resources. |
| 421 | */ |
| 422 | public CharSequence loadDescription(PackageManager pm) throws NotFoundException { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 423 | if (mActivityInfo.descriptionRes != 0) { |
| 424 | return pm.getText(mActivityInfo.packageName, |
| 425 | mActivityInfo.descriptionRes, mActivityInfo.applicationInfo); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 426 | } |
| 427 | throw new NotFoundException(); |
| 428 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 429 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 430 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 431 | * Load the user-displayed icon for this device admin. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 432 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 433 | * @param pm Supply a PackageManager used to load the device admin's |
| 434 | * resources. |
| 435 | */ |
| 436 | public Drawable loadIcon(PackageManager pm) { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 437 | return mActivityInfo.loadIcon(pm); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 438 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 439 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 440 | /** |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 441 | * 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 447 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 448 | /** |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 449 | * 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}, |
Raphael | dc2df32 | 2010-02-11 17:01:16 -0800 | [diff] [blame] | 452 | * {@link #USES_POLICY_RESET_PASSWORD}, {@link #USES_POLICY_FORCE_LOCK}, |
Andy Stadler | d267272 | 2011-02-16 10:53:33 -0800 | [diff] [blame] | 453 | * {@link #USES_POLICY_WIPE_DATA}, |
Ben Komalo | 2447edd | 2011-05-09 16:05:33 -0700 | [diff] [blame] | 454 | * {@link #USES_POLICY_EXPIRE_PASSWORD}, {@link #USES_ENCRYPTED_STORAGE}, |
| 455 | * {@link #USES_POLICY_DISABLE_CAMERA}. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 456 | */ |
| 457 | public boolean usesPolicy(int policyIdent) { |
| 458 | return (mUsesPolicies & (1<<policyIdent)) != 0; |
| 459 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 460 | |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 461 | /** |
| 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 Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 469 | |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 470 | /** |
| 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 Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 477 | /** @hide */ |
Mathew Inwood | 61e8ae6 | 2018-08-14 14:17:44 +0100 | [diff] [blame] | 478 | @UnsupportedAppUsage |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 479 | public ArrayList<PolicyInfo> getUsedPolicies() { |
| 480 | ArrayList<PolicyInfo> res = new ArrayList<PolicyInfo>(); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 481 | for (int i=0; i<sPoliciesDisplayOrder.size(); i++) { |
| 482 | PolicyInfo pi = sPoliciesDisplayOrder.get(i); |
| 483 | if (usesPolicy(pi.ident)) { |
| 484 | res.add(pi); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | return res; |
| 488 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 489 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 490 | /** @hide */ |
| 491 | public void writePoliciesToXml(XmlSerializer out) |
| 492 | throws IllegalArgumentException, IllegalStateException, IOException { |
| 493 | out.attribute(null, "flags", Integer.toString(mUsesPolicies)); |
| 494 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 495 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 496 | /** @hide */ |
| 497 | public void readPoliciesFromXml(XmlPullParser parser) |
| 498 | throws XmlPullParserException, IOException { |
| 499 | mUsesPolicies = Integer.parseInt( |
| 500 | parser.getAttributeValue(null, "flags")); |
| 501 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 502 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 503 | public void dump(Printer pw, String prefix) { |
| 504 | pw.println(prefix + "Receiver:"); |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 505 | mActivityInfo.dump(pw, prefix + " "); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 506 | } |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 507 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 508 | @Override |
| 509 | public String toString() { |
Makoto Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 510 | return "DeviceAdminInfo{" + mActivityInfo.name + "}"; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | /** |
| 514 | * Used to package this object into a {@link Parcel}. |
Jim Miller | b8ec470 | 2012-08-31 17:19:10 -0700 | [diff] [blame] | 515 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 516 | * @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 Onuki | 889c088 | 2016-02-16 15:20:30 -0800 | [diff] [blame] | 520 | mActivityInfo.writeToParcel(dest, flags); |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 521 | dest.writeInt(mUsesPolicies); |
arangelov | c8ee263 | 2018-02-23 16:45:53 +0000 | [diff] [blame] | 522 | dest.writeBoolean(mSupportsTransferOwnership); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Used to make this class parcelable. |
| 527 | */ |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 528 | public static final @android.annotation.NonNull Parcelable.Creator<DeviceAdminInfo> CREATOR = |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 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 | } |