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 | |
| 19 | import org.xmlpull.v1.XmlPullParserException; |
| 20 | |
| 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 23 | import android.app.admin.IDevicePolicyManager.Stub; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.pm.ActivityInfo; |
| 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
| 29 | import android.os.Handler; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 30 | import android.os.RemoteCallback; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.ServiceManager; |
| 33 | import android.util.Log; |
| 34 | |
| 35 | import java.io.IOException; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 36 | import java.util.List; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Public interface for managing policies enforced on a device. Most clients |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 40 | * of this class must have published a {@link DeviceAdminReceiver} that the user |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 41 | * has currently enabled. |
| 42 | */ |
| 43 | public class DevicePolicyManager { |
| 44 | private static String TAG = "DevicePolicyManager"; |
| 45 | private static boolean DEBUG = false; |
| 46 | private static boolean localLOGV = DEBUG || android.util.Config.LOGV; |
| 47 | |
| 48 | private final Context mContext; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 49 | private final IDevicePolicyManager mService; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 50 | |
| 51 | private final Handler mHandler; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 52 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 53 | private DevicePolicyManager(Context context, Handler handler) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 54 | mContext = context; |
| 55 | mHandler = handler; |
| 56 | mService = IDevicePolicyManager.Stub.asInterface( |
| 57 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 58 | } |
| 59 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 60 | /** @hide */ |
| 61 | public static DevicePolicyManager create(Context context, Handler handler) { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 62 | DevicePolicyManager me = new DevicePolicyManager(context, handler); |
| 63 | return me.mService != null ? me : null; |
| 64 | } |
| 65 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Activity action: ask the user to add a new device administrator to the system. |
| 68 | * The desired policy is the ComponentName of the policy in the |
| 69 | * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to |
| 70 | * bring the user through adding the device administrator to the system (or |
| 71 | * allowing them to reject it). |
| 72 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 73 | * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION} |
| 74 | * field to provide the user with additional explanation (in addition |
| 75 | * to your component's description) about what is being added. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 76 | */ |
| 77 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 78 | public static final String ACTION_ADD_DEVICE_ADMIN |
| 79 | = "android.app.action.ADD_DEVICE_ADMIN"; |
| 80 | |
| 81 | /** |
| 82 | * The ComponentName of the administrator component. |
| 83 | * |
| 84 | * @see #ACTION_ADD_DEVICE_ADMIN |
| 85 | */ |
| 86 | public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN"; |
| 87 | |
| 88 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 89 | * An optional CharSequence providing additional explanation for why the |
| 90 | * admin is being added. |
| 91 | * |
| 92 | * @see #ACTION_ADD_DEVICE_ADMIN |
| 93 | */ |
| 94 | public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION"; |
| 95 | |
| 96 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 97 | * Activity action: have the user enter a new password. This activity |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 98 | * should be launched after using {@link #setPasswordQuality(ComponentName, int)} |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 99 | * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 100 | * user enter a new password that meets the current requirements. You can |
| 101 | * use {@link #isActivePasswordSufficient()} to determine whether you need |
| 102 | * to have the user select a new password in order to meet the current |
| 103 | * constraints. Upon being resumed from this activity, |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 104 | * you can check the new password characteristics to see if they are |
| 105 | * sufficient. |
| 106 | */ |
| 107 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 108 | public static final String ACTION_SET_NEW_PASSWORD |
| 109 | = "android.app.action.SET_NEW_PASSWORD"; |
| 110 | |
| 111 | /** |
| 112 | * Return true if the given administrator component is currently |
| 113 | * active (enabled) in the system. |
| 114 | */ |
| 115 | public boolean isAdminActive(ComponentName who) { |
| 116 | if (mService != null) { |
| 117 | try { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 118 | return mService.isAdminActive(who); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 119 | } catch (RemoteException e) { |
| 120 | Log.w(TAG, "Failed talking with device policy service", e); |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | /** |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 127 | * Return a list of all currently active device administrator's component |
| 128 | * names. Note that if there are no administrators than null may be |
| 129 | * returned. |
| 130 | */ |
| 131 | public List<ComponentName> getActiveAdmins() { |
| 132 | if (mService != null) { |
| 133 | try { |
| 134 | return mService.getActiveAdmins(); |
| 135 | } catch (RemoteException e) { |
| 136 | Log.w(TAG, "Failed talking with device policy service", e); |
| 137 | } |
| 138 | } |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | /** |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 143 | * @hide |
| 144 | */ |
| 145 | public boolean packageHasActiveAdmins(String packageName) { |
| 146 | if (mService != null) { |
| 147 | try { |
| 148 | return mService.packageHasActiveAdmins(packageName); |
| 149 | } catch (RemoteException e) { |
| 150 | Log.w(TAG, "Failed talking with device policy service", e); |
| 151 | } |
| 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 157 | * Remove a current administration component. This can only be called |
| 158 | * by the application that owns the administration component; if you |
| 159 | * try to remove someone else's component, a security exception will be |
| 160 | * thrown. |
| 161 | */ |
| 162 | public void removeActiveAdmin(ComponentName who) { |
| 163 | if (mService != null) { |
| 164 | try { |
| 165 | mService.removeActiveAdmin(who); |
| 166 | } catch (RemoteException e) { |
| 167 | Log.w(TAG, "Failed talking with device policy service", e); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 173 | * Constant for {@link #setPasswordQuality}: the policy has no requirements |
| 174 | * for the password. Note that quality constants are ordered so that higher |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 175 | * values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 176 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 177 | public static final int PASSWORD_QUALITY_UNSPECIFIED = 0; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 178 | |
| 179 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 180 | * Constant for {@link #setPasswordQuality}: the policy requires some kind |
| 181 | * of password, but doesn't care what it is. Note that quality constants |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 182 | * are ordered so that higher values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 183 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 184 | public static final int PASSWORD_QUALITY_SOMETHING = 0x10000; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 187 | * Constant for {@link #setPasswordQuality}: the user must have entered a |
| 188 | * password containing at least numeric characters. Note that quality |
| 189 | * constants are ordered so that higher values are more restrictive. |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 190 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 191 | public static final int PASSWORD_QUALITY_NUMERIC = 0x20000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 194 | * Constant for {@link #setPasswordQuality}: the user must have entered a |
| 195 | * password containing at least <em>both></em> numeric <em>and</em> |
| 196 | * alphabeter (or other symbol) characters. Note that quality constants are |
| 197 | * ordered so that higher values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 198 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 199 | public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x30000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * Called by an application that is administering the device to set the |
| 203 | * password restrictions it is imposing. After setting this, the user |
| 204 | * will not be able to enter a new password that is not at least as |
| 205 | * restrictive as what has been set. Note that the current password |
| 206 | * will remain until the user has set a new one, so the change does not |
| 207 | * take place immediately. To prompt the user for a new password, use |
| 208 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. |
| 209 | * |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 210 | * <p>Quality constants are ordered so that higher values are more restrictive; |
| 211 | * thus the highest requested quality constant (between the policy set here, |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 212 | * the user's preference, and any other considerations) is the one that |
| 213 | * is in effect. |
| 214 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 215 | * <p>The calling device admin must have requested |
| 216 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 217 | * this method; if it has not, a security exception will be thrown. |
| 218 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 219 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 220 | * @param quality The new desired quality. One of |
| 221 | * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING}, |
| 222 | * {@link #PASSWORD_QUALITY_NUMERIC}, or {@link #PASSWORD_QUALITY_ALPHANUMERIC}. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 223 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 224 | public void setPasswordQuality(ComponentName admin, int quality) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 225 | if (mService != null) { |
| 226 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 227 | mService.setPasswordQuality(admin, quality); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 228 | } catch (RemoteException e) { |
| 229 | Log.w(TAG, "Failed talking with device policy service", e); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 235 | * Retrieve the current minimum password quality for all admins |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 236 | * or a particular one. |
| 237 | * @param admin The name of the admin component to check, or null to aggregate |
| 238 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 239 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 240 | public int getPasswordQuality(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 241 | if (mService != null) { |
| 242 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 243 | return mService.getPasswordQuality(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 244 | } catch (RemoteException e) { |
| 245 | Log.w(TAG, "Failed talking with device policy service", e); |
| 246 | } |
| 247 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 248 | return PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 252 | * Called by an application that is administering the device to set the |
| 253 | * minimum allowed password length. After setting this, the user |
| 254 | * will not be able to enter a new password that is not at least as |
| 255 | * restrictive as what has been set. Note that the current password |
| 256 | * will remain until the user has set a new one, so the change does not |
| 257 | * take place immediately. To prompt the user for a new password, use |
| 258 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This |
| 259 | * constraint is only imposed if the administrator has also requested either |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 260 | * {@link #PASSWORD_QUALITY_NUMERIC} or {@link #PASSWORD_QUALITY_ALPHANUMERIC} |
| 261 | * with {@link #setPasswordQuality}. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 262 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 263 | * <p>The calling device admin must have requested |
| 264 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 265 | * this method; if it has not, a security exception will be thrown. |
| 266 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 267 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 268 | * @param length The new desired minimum password length. A value of 0 |
| 269 | * means there is no restriction. |
| 270 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 271 | public void setPasswordMinimumLength(ComponentName admin, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 272 | if (mService != null) { |
| 273 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 274 | mService.setPasswordMinimumLength(admin, length); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 275 | } catch (RemoteException e) { |
| 276 | Log.w(TAG, "Failed talking with device policy service", e); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 282 | * Retrieve the current minimum password length for all admins |
| 283 | * or a particular one. |
| 284 | * @param admin The name of the admin component to check, or null to aggregate |
| 285 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 286 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 287 | public int getPasswordMinimumLength(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 288 | if (mService != null) { |
| 289 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 290 | return mService.getPasswordMinimumLength(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 291 | } catch (RemoteException e) { |
| 292 | Log.w(TAG, "Failed talking with device policy service", e); |
| 293 | } |
| 294 | } |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 299 | * Return the maximum password length that the device supports for a |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 300 | * particular password quality. |
Dianne Hackborn | 364f6e3 | 2010-01-29 17:38:20 -0800 | [diff] [blame] | 301 | * @param quality The quality being interrogated. |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 302 | * @return Returns the maximum length that the user can enter. |
| 303 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 304 | public int getPasswordMaximumLength(int quality) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 305 | // Kind-of arbitrary. |
| 306 | return 16; |
| 307 | } |
| 308 | |
| 309 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 310 | * Determine whether the current password the user has set is sufficient |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 311 | * to meet the policy requirements (quality, minimum length) that have been |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 312 | * requested. |
| 313 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 314 | * <p>The calling device admin must have requested |
| 315 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 316 | * this method; if it has not, a security exception will be thrown. |
| 317 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 318 | * @return Returns true if the password meets the current requirements, |
| 319 | * else false. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 320 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 321 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 322 | if (mService != null) { |
| 323 | try { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 324 | return mService.isActivePasswordSufficient(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 325 | } catch (RemoteException e) { |
| 326 | Log.w(TAG, "Failed talking with device policy service", e); |
| 327 | } |
| 328 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 329 | return false; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Retrieve the number of times the user has failed at entering a |
| 334 | * password since that last successful password entry. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 335 | * |
| 336 | * <p>The calling device admin must have requested |
| 337 | * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call |
| 338 | * this method; if it has not, a security exception will be thrown. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 339 | */ |
| 340 | public int getCurrentFailedPasswordAttempts() { |
| 341 | if (mService != null) { |
| 342 | try { |
| 343 | return mService.getCurrentFailedPasswordAttempts(); |
| 344 | } catch (RemoteException e) { |
| 345 | Log.w(TAG, "Failed talking with device policy service", e); |
| 346 | } |
| 347 | } |
| 348 | return -1; |
| 349 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 350 | |
| 351 | /** |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 352 | * Setting this to a value greater than zero enables a built-in policy |
| 353 | * that will perform a device wipe after too many incorrect |
| 354 | * device-unlock passwords have been entered. This built-in policy combines |
| 355 | * watching for failed passwords and wiping the device, and requires |
| 356 | * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 357 | * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}. |
| 358 | * |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 359 | * <p>To implement any other policy (e.g. wiping data for a particular |
| 360 | * application only, erasing or revoking credentials, or reporting the |
| 361 | * failure to a server), you should implement |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 362 | * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)} |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 363 | * instead. Do not use this API, because if the maximum count is reached, |
| 364 | * the device will be wiped immediately, and your callback will not be invoked. |
| 365 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 366 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 367 | * @param num The number of failed password attempts at which point the |
| 368 | * device will wipe its data. |
| 369 | */ |
| 370 | public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) { |
| 371 | if (mService != null) { |
| 372 | try { |
| 373 | mService.setMaximumFailedPasswordsForWipe(admin, num); |
| 374 | } catch (RemoteException e) { |
| 375 | Log.w(TAG, "Failed talking with device policy service", e); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 381 | * Retrieve the current maximum number of login attempts that are allowed |
| 382 | * before the device wipes itself, for all admins |
| 383 | * or a particular one. |
| 384 | * @param admin The name of the admin component to check, or null to aggregate |
| 385 | * all admins. |
| 386 | */ |
| 387 | public int getMaximumFailedPasswordsForWipe(ComponentName admin) { |
| 388 | if (mService != null) { |
| 389 | try { |
| 390 | return mService.getMaximumFailedPasswordsForWipe(admin); |
| 391 | } catch (RemoteException e) { |
| 392 | Log.w(TAG, "Failed talking with device policy service", e); |
| 393 | } |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /** |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 399 | * Flag for {@link #resetPassword}: don't allow other admins to change |
| 400 | * the password again until the user has entered it. |
| 401 | */ |
| 402 | public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001; |
| 403 | |
| 404 | /** |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 405 | * Force a new device unlock password (the password needed to access the |
| 406 | * entire device, not for individual accounts) on the user. This takes |
| 407 | * effect immediately. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 408 | * The given password must be sufficient for the |
| 409 | * current password quality and length constraints as returned by |
| 410 | * {@link #getPasswordQuality(ComponentName)} and |
| 411 | * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet |
| 412 | * these constraints, then it will be rejected and false returned. Note |
| 413 | * that the password may be a stronger quality (containing alphanumeric |
| 414 | * characters when the requested quality is only numeric), in which case |
| 415 | * the currently active quality will be increased to match. |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 416 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 417 | * <p>The calling device admin must have requested |
| 418 | * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call |
| 419 | * this method; if it has not, a security exception will be thrown. |
| 420 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 421 | * @param password The new password for the user. |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 422 | * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}. |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 423 | * @return Returns true if the password was applied, or false if it is |
| 424 | * not acceptable for the current constraints. |
| 425 | */ |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 426 | public boolean resetPassword(String password, int flags) { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 427 | if (mService != null) { |
| 428 | try { |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame^] | 429 | return mService.resetPassword(password, flags); |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 430 | } catch (RemoteException e) { |
| 431 | Log.w(TAG, "Failed talking with device policy service", e); |
| 432 | } |
| 433 | } |
| 434 | return false; |
| 435 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * Called by an application that is administering the device to set the |
| 439 | * maximum time for user activity until the device will lock. This limits |
| 440 | * the length that the user can set. It takes effect immediately. |
| 441 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 442 | * <p>The calling device admin must have requested |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 443 | * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 444 | * this method; if it has not, a security exception will be thrown. |
| 445 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 446 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 447 | * @param timeMs The new desired maximum time to lock in milliseconds. |
| 448 | * A value of 0 means there is no restriction. |
| 449 | */ |
| 450 | public void setMaximumTimeToLock(ComponentName admin, long timeMs) { |
| 451 | if (mService != null) { |
| 452 | try { |
| 453 | mService.setMaximumTimeToLock(admin, timeMs); |
| 454 | } catch (RemoteException e) { |
| 455 | Log.w(TAG, "Failed talking with device policy service", e); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 461 | * Retrieve the current maximum time to unlock for all admins |
| 462 | * or a particular one. |
| 463 | * @param admin The name of the admin component to check, or null to aggregate |
| 464 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 465 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 466 | public long getMaximumTimeToLock(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 467 | if (mService != null) { |
| 468 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 469 | return mService.getMaximumTimeToLock(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 470 | } catch (RemoteException e) { |
| 471 | Log.w(TAG, "Failed talking with device policy service", e); |
| 472 | } |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 478 | * Make the device lock immediately, as if the lock screen timeout has |
| 479 | * expired at the point of this call. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 480 | * |
| 481 | * <p>The calling device admin must have requested |
| 482 | * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call |
| 483 | * this method; if it has not, a security exception will be thrown. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 484 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 485 | public void lockNow() { |
| 486 | if (mService != null) { |
| 487 | try { |
| 488 | mService.lockNow(); |
| 489 | } catch (RemoteException e) { |
| 490 | Log.w(TAG, "Failed talking with device policy service", e); |
| 491 | } |
| 492 | } |
| 493 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Ask the user date be wiped. This will cause the device to reboot, |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 497 | * erasing all user data while next booting up. External storage such |
| 498 | * as SD cards will not be erased. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 499 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 500 | * <p>The calling device admin must have requested |
| 501 | * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call |
| 502 | * this method; if it has not, a security exception will be thrown. |
| 503 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 504 | * @param flags Bit mask of additional options: currently must be 0. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 505 | */ |
| 506 | public void wipeData(int flags) { |
| 507 | if (mService != null) { |
| 508 | try { |
| 509 | mService.wipeData(flags); |
| 510 | } catch (RemoteException e) { |
| 511 | Log.w(TAG, "Failed talking with device policy service", e); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @hide |
| 518 | */ |
| 519 | public void setActiveAdmin(ComponentName policyReceiver) { |
| 520 | if (mService != null) { |
| 521 | try { |
| 522 | mService.setActiveAdmin(policyReceiver); |
| 523 | } catch (RemoteException e) { |
| 524 | Log.w(TAG, "Failed talking with device policy service", e); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * @hide |
| 531 | */ |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 532 | public DeviceAdminInfo getAdminInfo(ComponentName cn) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 533 | ActivityInfo ai; |
| 534 | try { |
| 535 | ai = mContext.getPackageManager().getReceiverInfo(cn, |
| 536 | PackageManager.GET_META_DATA); |
| 537 | } catch (PackageManager.NameNotFoundException e) { |
| 538 | Log.w(TAG, "Unable to retrieve device policy " + cn, e); |
| 539 | return null; |
| 540 | } |
| 541 | |
| 542 | ResolveInfo ri = new ResolveInfo(); |
| 543 | ri.activityInfo = ai; |
| 544 | |
| 545 | try { |
| 546 | return new DeviceAdminInfo(mContext, ri); |
| 547 | } catch (XmlPullParserException e) { |
| 548 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 549 | return null; |
| 550 | } catch (IOException e) { |
| 551 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 552 | return null; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @hide |
| 558 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 559 | public void getRemoveWarning(ComponentName admin, RemoteCallback result) { |
| 560 | if (mService != null) { |
| 561 | try { |
| 562 | mService.getRemoveWarning(admin, result); |
| 563 | } catch (RemoteException e) { |
| 564 | Log.w(TAG, "Failed talking with device policy service", e); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * @hide |
| 571 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 572 | public void setActivePasswordState(int quality, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 573 | if (mService != null) { |
| 574 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 575 | mService.setActivePasswordState(quality, length); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 576 | } catch (RemoteException e) { |
| 577 | Log.w(TAG, "Failed talking with device policy service", e); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * @hide |
| 584 | */ |
| 585 | public void reportFailedPasswordAttempt() { |
| 586 | if (mService != null) { |
| 587 | try { |
| 588 | mService.reportFailedPasswordAttempt(); |
| 589 | } catch (RemoteException e) { |
| 590 | Log.w(TAG, "Failed talking with device policy service", e); |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * @hide |
| 597 | */ |
| 598 | public void reportSuccessfulPasswordAttempt() { |
| 599 | if (mService != null) { |
| 600 | try { |
| 601 | mService.reportSuccessfulPasswordAttempt(); |
| 602 | } catch (RemoteException e) { |
| 603 | Log.w(TAG, "Failed talking with device policy service", e); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | } |