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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import org.xmlpull.v1.XmlPullParserException; |
| 20 | |
| 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.pm.ActivityInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.os.Handler; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | import java.io.IOException; |
| 34 | |
| 35 | /** |
| 36 | * Public interface for managing policies enforced on a device. Most clients |
| 37 | * of this class must have published a {@link DeviceAdmin} that the user |
| 38 | * has currently enabled. |
| 39 | */ |
| 40 | public class DevicePolicyManager { |
| 41 | private static String TAG = "DevicePolicyManager"; |
| 42 | private static boolean DEBUG = false; |
| 43 | private static boolean localLOGV = DEBUG || android.util.Config.LOGV; |
| 44 | |
| 45 | private final Context mContext; |
| 46 | private final Handler mHandler; |
| 47 | private final IDevicePolicyManager mService; |
| 48 | |
| 49 | /*package*/ DevicePolicyManager(Context context, Handler handler) { |
| 50 | mContext = context; |
| 51 | mHandler = handler; |
| 52 | mService = IDevicePolicyManager.Stub.asInterface( |
| 53 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Activity action: ask the user to add a new device administrator to the system. |
| 58 | * The desired policy is the ComponentName of the policy in the |
| 59 | * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to |
| 60 | * bring the user through adding the device administrator to the system (or |
| 61 | * allowing them to reject it). |
| 62 | * |
| 63 | * <p>Note: the current platform can only have one device administrator |
| 64 | * active at a time. If you make this request while there is already |
| 65 | * an active administrator, this new request will be canceled automatically. |
| 66 | */ |
| 67 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 68 | public static final String ACTION_ADD_DEVICE_ADMIN |
| 69 | = "android.app.action.ADD_DEVICE_ADMIN"; |
| 70 | |
| 71 | /** |
| 72 | * The ComponentName of the administrator component. |
| 73 | * |
| 74 | * @see #ACTION_ADD_DEVICE_ADMIN |
| 75 | */ |
| 76 | public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN"; |
| 77 | |
| 78 | /** |
| 79 | * Activity action: have the user enter a new password. This activity |
| 80 | * should be launched after using {@link #setPasswordMode(ComponentName, int)} |
| 81 | * or {@link #setMinimumPasswordLength(ComponentName, int)} to have the |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 82 | * user enter a new password that meets the current requirements. You can |
| 83 | * use {@link #isActivePasswordSufficient()} to determine whether you need |
| 84 | * to have the user select a new password in order to meet the current |
| 85 | * constraints. Upon being resumed from this activity, |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 86 | * you can check the new password characteristics to see if they are |
| 87 | * sufficient. |
| 88 | */ |
| 89 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 90 | public static final String ACTION_SET_NEW_PASSWORD |
| 91 | = "android.app.action.SET_NEW_PASSWORD"; |
| 92 | |
| 93 | /** |
| 94 | * Return true if the given administrator component is currently |
| 95 | * active (enabled) in the system. |
| 96 | */ |
| 97 | public boolean isAdminActive(ComponentName who) { |
| 98 | if (mService != null) { |
| 99 | try { |
| 100 | return who.equals(mService.getActiveAdmin()); |
| 101 | } catch (RemoteException e) { |
| 102 | Log.w(TAG, "Failed talking with device policy service", e); |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Remove a current administration component. This can only be called |
| 110 | * by the application that owns the administration component; if you |
| 111 | * try to remove someone else's component, a security exception will be |
| 112 | * thrown. |
| 113 | */ |
| 114 | public void removeActiveAdmin(ComponentName who) { |
| 115 | if (mService != null) { |
| 116 | try { |
| 117 | mService.removeActiveAdmin(who); |
| 118 | } catch (RemoteException e) { |
| 119 | Log.w(TAG, "Failed talking with device policy service", e); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Constant for {@link #setPasswordMode}: the policy has no requirements |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 126 | * for the password. Note that mode constants are ordered so that higher |
| 127 | * values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 128 | */ |
| 129 | public static final int PASSWORD_MODE_UNSPECIFIED = 0; |
| 130 | |
| 131 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 132 | * Constant for {@link #setPasswordMode}: the policy requires some kind |
| 133 | * of password, but doesn't care what it is. Note that mode constants |
| 134 | * are ordered so that higher values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 135 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 136 | public static final int PASSWORD_MODE_SOMETHING = 1000; |
| 137 | |
| 138 | /** |
| 139 | * Constant for {@link #setPasswordMode}: the user must have at least a |
| 140 | * numeric password. Note that mode constants are ordered so that higher |
| 141 | * values are more restrictive. |
| 142 | */ |
| 143 | public static final int PASSWORD_MODE_NUMERIC = 2000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Constant for {@link #setPasswordMode}: the user must have at least an |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 147 | * alphanumeric password. Note that mode constants are ordered so that higher |
| 148 | * values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 149 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 150 | public static final int PASSWORD_MODE_ALPHANUMERIC = 3000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Called by an application that is administering the device to set the |
| 154 | * password restrictions it is imposing. After setting this, the user |
| 155 | * will not be able to enter a new password that is not at least as |
| 156 | * restrictive as what has been set. Note that the current password |
| 157 | * will remain until the user has set a new one, so the change does not |
| 158 | * take place immediately. To prompt the user for a new password, use |
| 159 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. |
| 160 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 161 | * <p>Mode constants are ordered so that higher values are more restrictive; |
| 162 | * thus the highest requested mode constant (between the policy set here, |
| 163 | * the user's preference, and any other considerations) is the one that |
| 164 | * is in effect. |
| 165 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 166 | * @param admin Which {@link DeviceAdmin} this request is associated with. |
| 167 | * @param mode The new desired mode. One of |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 168 | * {@link #PASSWORD_MODE_UNSPECIFIED}, {@link #PASSWORD_MODE_SOMETHING}, |
| 169 | * {@link #PASSWORD_MODE_NUMERIC}, or {@link #PASSWORD_MODE_ALPHANUMERIC}. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 170 | */ |
| 171 | public void setPasswordMode(ComponentName admin, int mode) { |
| 172 | if (mService != null) { |
| 173 | try { |
| 174 | mService.setPasswordMode(admin, mode); |
| 175 | } catch (RemoteException e) { |
| 176 | Log.w(TAG, "Failed talking with device policy service", e); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Retrieve the current password mode that is in effect due to all |
| 183 | * device admins. |
| 184 | */ |
| 185 | public int getPasswordMode() { |
| 186 | if (mService != null) { |
| 187 | try { |
| 188 | return mService.getPasswordMode(); |
| 189 | } catch (RemoteException e) { |
| 190 | Log.w(TAG, "Failed talking with device policy service", e); |
| 191 | } |
| 192 | } |
| 193 | return PASSWORD_MODE_UNSPECIFIED; |
| 194 | } |
| 195 | |
| 196 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 197 | * Called by an application that is administering the device to set the |
| 198 | * minimum allowed password length. After setting this, the user |
| 199 | * will not be able to enter a new password that is not at least as |
| 200 | * restrictive as what has been set. Note that the current password |
| 201 | * will remain until the user has set a new one, so the change does not |
| 202 | * take place immediately. To prompt the user for a new password, use |
| 203 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This |
| 204 | * constraint is only imposed if the administrator has also requested either |
| 205 | * {@link #PASSWORD_MODE_NUMERIC} or {@link #PASSWORD_MODE_ALPHANUMERIC} |
| 206 | * with {@link #setPasswordMode}. |
| 207 | * |
| 208 | * @param admin Which {@link DeviceAdmin} this request is associated with. |
| 209 | * @param length The new desired minimum password length. A value of 0 |
| 210 | * means there is no restriction. |
| 211 | */ |
| 212 | public void setMinimumPasswordLength(ComponentName admin, int length) { |
| 213 | if (mService != null) { |
| 214 | try { |
| 215 | mService.setMinimumPasswordLength(admin, length); |
| 216 | } catch (RemoteException e) { |
| 217 | Log.w(TAG, "Failed talking with device policy service", e); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Retrieve the current minimum password length that is in effect due to all |
| 224 | * device admins. |
| 225 | */ |
| 226 | public int getMinimumPasswordLength() { |
| 227 | if (mService != null) { |
| 228 | try { |
| 229 | return mService.getMinimumPasswordLength(); |
| 230 | } catch (RemoteException e) { |
| 231 | Log.w(TAG, "Failed talking with device policy service", e); |
| 232 | } |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 238 | * Determine whether the current password the user has set is sufficient |
| 239 | * to meet the policy requirements (mode, minimum length) that have been |
| 240 | * requested. |
| 241 | * |
| 242 | * @return Returns true if the password meets the current requirements, |
| 243 | * else false. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 244 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 245 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 246 | if (mService != null) { |
| 247 | try { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 248 | return mService.isActivePasswordSufficient(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 249 | } catch (RemoteException e) { |
| 250 | Log.w(TAG, "Failed talking with device policy service", e); |
| 251 | } |
| 252 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 253 | return false; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Retrieve the number of times the user has failed at entering a |
| 258 | * password since that last successful password entry. |
| 259 | */ |
| 260 | public int getCurrentFailedPasswordAttempts() { |
| 261 | if (mService != null) { |
| 262 | try { |
| 263 | return mService.getCurrentFailedPasswordAttempts(); |
| 264 | } catch (RemoteException e) { |
| 265 | Log.w(TAG, "Failed talking with device policy service", e); |
| 266 | } |
| 267 | } |
| 268 | return -1; |
| 269 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 270 | |
| 271 | /** |
| 272 | * Force a new password on the user. This takes effect immediately. The |
| 273 | * given password must meet the current password minimum length constraint |
| 274 | * or it will be rejected. The given password will be accepted regardless |
| 275 | * of the current password mode, automatically adjusting the password mode |
| 276 | * higher if needed. (The string you give here is acceptable for any mode; |
| 277 | * if it contains only digits, that is still an acceptable alphanumeric |
| 278 | * password.) |
| 279 | * |
| 280 | * @param password The new password for the user. |
| 281 | * @return Returns true if the password was applied, or false if it is |
| 282 | * not acceptable for the current constraints. |
| 283 | */ |
| 284 | public boolean resetPassword(String password) { |
| 285 | if (mService != null) { |
| 286 | try { |
| 287 | return mService.resetPassword(password); |
| 288 | } catch (RemoteException e) { |
| 289 | Log.w(TAG, "Failed talking with device policy service", e); |
| 290 | } |
| 291 | } |
| 292 | return false; |
| 293 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * Called by an application that is administering the device to set the |
| 297 | * maximum time for user activity until the device will lock. This limits |
| 298 | * the length that the user can set. It takes effect immediately. |
| 299 | * |
| 300 | * @param admin Which {@link DeviceAdmin} this request is associated with. |
| 301 | * @param timeMs The new desired maximum time to lock in milliseconds. |
| 302 | * A value of 0 means there is no restriction. |
| 303 | */ |
| 304 | public void setMaximumTimeToLock(ComponentName admin, long timeMs) { |
| 305 | if (mService != null) { |
| 306 | try { |
| 307 | mService.setMaximumTimeToLock(admin, timeMs); |
| 308 | } catch (RemoteException e) { |
| 309 | Log.w(TAG, "Failed talking with device policy service", e); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Retrieve the current maximum time to lock that is in effect due to all |
| 316 | * device admins. Returns 0 if no maximum is set. |
| 317 | */ |
| 318 | public long getMaximumTimeToLock() { |
| 319 | if (mService != null) { |
| 320 | try { |
| 321 | return mService.getMaximumTimeToLock(); |
| 322 | } catch (RemoteException e) { |
| 323 | Log.w(TAG, "Failed talking with device policy service", e); |
| 324 | } |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 330 | * Make the device lock immediately, as if the lock screen timeout has |
| 331 | * expired at the point of this call. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 332 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 333 | public void lockNow() { |
| 334 | if (mService != null) { |
| 335 | try { |
| 336 | mService.lockNow(); |
| 337 | } catch (RemoteException e) { |
| 338 | Log.w(TAG, "Failed talking with device policy service", e); |
| 339 | } |
| 340 | } |
| 341 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * 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^] | 345 | * erasing all user data while next booting up. External storage such |
| 346 | * as SD cards will not be erased. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 347 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame^] | 348 | * @param flags Bit mask of additional options: currently must be 0. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 349 | */ |
| 350 | public void wipeData(int flags) { |
| 351 | if (mService != null) { |
| 352 | try { |
| 353 | mService.wipeData(flags); |
| 354 | } catch (RemoteException e) { |
| 355 | Log.w(TAG, "Failed talking with device policy service", e); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * @hide |
| 362 | */ |
| 363 | public void setActiveAdmin(ComponentName policyReceiver) { |
| 364 | if (mService != null) { |
| 365 | try { |
| 366 | mService.setActiveAdmin(policyReceiver); |
| 367 | } catch (RemoteException e) { |
| 368 | Log.w(TAG, "Failed talking with device policy service", e); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @hide |
| 375 | */ |
| 376 | public ComponentName getActiveAdmin() { |
| 377 | if (mService != null) { |
| 378 | try { |
| 379 | return mService.getActiveAdmin(); |
| 380 | } catch (RemoteException e) { |
| 381 | Log.w(TAG, "Failed talking with device policy service", e); |
| 382 | } |
| 383 | } |
| 384 | return null; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * @hide |
| 389 | */ |
| 390 | public DeviceAdminInfo getActiveAdminInfo() { |
| 391 | ComponentName cn = getActiveAdmin(); |
| 392 | if (cn == null) { |
| 393 | return null; |
| 394 | } |
| 395 | |
| 396 | ActivityInfo ai; |
| 397 | try { |
| 398 | ai = mContext.getPackageManager().getReceiverInfo(cn, |
| 399 | PackageManager.GET_META_DATA); |
| 400 | } catch (PackageManager.NameNotFoundException e) { |
| 401 | Log.w(TAG, "Unable to retrieve device policy " + cn, e); |
| 402 | return null; |
| 403 | } |
| 404 | |
| 405 | ResolveInfo ri = new ResolveInfo(); |
| 406 | ri.activityInfo = ai; |
| 407 | |
| 408 | try { |
| 409 | return new DeviceAdminInfo(mContext, ri); |
| 410 | } catch (XmlPullParserException e) { |
| 411 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 412 | return null; |
| 413 | } catch (IOException e) { |
| 414 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 415 | return null; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * @hide |
| 421 | */ |
| 422 | public void setActivePasswordState(int mode, int length) { |
| 423 | if (mService != null) { |
| 424 | try { |
| 425 | mService.setActivePasswordState(mode, length); |
| 426 | } catch (RemoteException e) { |
| 427 | Log.w(TAG, "Failed talking with device policy service", e); |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * @hide |
| 434 | */ |
| 435 | public void reportFailedPasswordAttempt() { |
| 436 | if (mService != null) { |
| 437 | try { |
| 438 | mService.reportFailedPasswordAttempt(); |
| 439 | } catch (RemoteException e) { |
| 440 | Log.w(TAG, "Failed talking with device policy service", e); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * @hide |
| 447 | */ |
| 448 | public void reportSuccessfulPasswordAttempt() { |
| 449 | if (mService != null) { |
| 450 | try { |
| 451 | mService.reportSuccessfulPasswordAttempt(); |
| 452 | } catch (RemoteException e) { |
| 453 | Log.w(TAG, "Failed talking with device policy service", e); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |