blob: 135851f824da8861489755ae0da48efb55a6450a [file] [log] [blame]
Dianne Hackbornd6847842010-01-12 18:14:19 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
19import org.xmlpull.v1.XmlPullParserException;
20
21import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.pm.ActivityInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.ResolveInfo;
28import android.os.Handler;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080029import android.os.RemoteCallback;
Dianne Hackbornd6847842010-01-12 18:14:19 -080030import android.os.RemoteException;
31import android.os.ServiceManager;
32import android.util.Log;
33
34import java.io.IOException;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080035import java.util.List;
Dianne Hackbornd6847842010-01-12 18:14:19 -080036
37/**
38 * Public interface for managing policies enforced on a device. Most clients
39 * of this class must have published a {@link DeviceAdmin} that the user
40 * has currently enabled.
41 */
42public class DevicePolicyManager {
43 private static String TAG = "DevicePolicyManager";
44 private static boolean DEBUG = false;
45 private static boolean localLOGV = DEBUG || android.util.Config.LOGV;
46
47 private final Context mContext;
Dianne Hackbornd6847842010-01-12 18:14:19 -080048 private final IDevicePolicyManager mService;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080049
50 private final Handler mHandler;
Dianne Hackbornd6847842010-01-12 18:14:19 -080051
52 /*package*/ DevicePolicyManager(Context context, Handler handler) {
53 mContext = context;
54 mHandler = handler;
55 mService = IDevicePolicyManager.Stub.asInterface(
56 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
57 }
58
59 /**
60 * Activity action: ask the user to add a new device administrator to the system.
61 * The desired policy is the ComponentName of the policy in the
62 * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to
63 * bring the user through adding the device administrator to the system (or
64 * allowing them to reject it).
65 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080066 * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
67 * field to provide the user with additional explanation (in addition
68 * to your component's description) about what is being added.
Dianne Hackbornd6847842010-01-12 18:14:19 -080069 */
70 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
71 public static final String ACTION_ADD_DEVICE_ADMIN
72 = "android.app.action.ADD_DEVICE_ADMIN";
73
74 /**
75 * The ComponentName of the administrator component.
76 *
77 * @see #ACTION_ADD_DEVICE_ADMIN
78 */
79 public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
80
81 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080082 * An optional CharSequence providing additional explanation for why the
83 * admin is being added.
84 *
85 * @see #ACTION_ADD_DEVICE_ADMIN
86 */
87 public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
88
89 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -080090 * Activity action: have the user enter a new password. This activity
91 * should be launched after using {@link #setPasswordMode(ComponentName, int)}
92 * or {@link #setMinimumPasswordLength(ComponentName, int)} to have the
Dianne Hackborndf83afa2010-01-20 13:37:26 -080093 * user enter a new password that meets the current requirements. You can
94 * use {@link #isActivePasswordSufficient()} to determine whether you need
95 * to have the user select a new password in order to meet the current
96 * constraints. Upon being resumed from this activity,
Dianne Hackbornd6847842010-01-12 18:14:19 -080097 * you can check the new password characteristics to see if they are
98 * sufficient.
99 */
100 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
101 public static final String ACTION_SET_NEW_PASSWORD
102 = "android.app.action.SET_NEW_PASSWORD";
103
104 /**
105 * Return true if the given administrator component is currently
106 * active (enabled) in the system.
107 */
108 public boolean isAdminActive(ComponentName who) {
109 if (mService != null) {
110 try {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800111 return mService.isAdminActive(who);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800112 } catch (RemoteException e) {
113 Log.w(TAG, "Failed talking with device policy service", e);
114 }
115 }
116 return false;
117 }
118
119 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800120 * Return a list of all currently active device administrator's component
121 * names. Note that if there are no administrators than null may be
122 * returned.
123 */
124 public List<ComponentName> getActiveAdmins() {
125 if (mService != null) {
126 try {
127 return mService.getActiveAdmins();
128 } catch (RemoteException e) {
129 Log.w(TAG, "Failed talking with device policy service", e);
130 }
131 }
132 return null;
133 }
134
135 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800136 * Remove a current administration component. This can only be called
137 * by the application that owns the administration component; if you
138 * try to remove someone else's component, a security exception will be
139 * thrown.
140 */
141 public void removeActiveAdmin(ComponentName who) {
142 if (mService != null) {
143 try {
144 mService.removeActiveAdmin(who);
145 } catch (RemoteException e) {
146 Log.w(TAG, "Failed talking with device policy service", e);
147 }
148 }
149 }
150
151 /**
152 * Constant for {@link #setPasswordMode}: the policy has no requirements
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800153 * for the password. Note that mode constants are ordered so that higher
154 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800155 */
156 public static final int PASSWORD_MODE_UNSPECIFIED = 0;
157
158 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800159 * Constant for {@link #setPasswordMode}: the policy requires some kind
160 * of password, but doesn't care what it is. Note that mode constants
161 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800162 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800163 public static final int PASSWORD_MODE_SOMETHING = 1000;
164
165 /**
166 * Constant for {@link #setPasswordMode}: the user must have at least a
167 * numeric password. Note that mode constants are ordered so that higher
168 * values are more restrictive.
169 */
170 public static final int PASSWORD_MODE_NUMERIC = 2000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800171
172 /**
173 * Constant for {@link #setPasswordMode}: the user must have at least an
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800174 * alphanumeric password. Note that mode constants are ordered so that higher
175 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800176 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800177 public static final int PASSWORD_MODE_ALPHANUMERIC = 3000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800178
179 /**
180 * Called by an application that is administering the device to set the
181 * password restrictions it is imposing. After setting this, the user
182 * will not be able to enter a new password that is not at least as
183 * restrictive as what has been set. Note that the current password
184 * will remain until the user has set a new one, so the change does not
185 * take place immediately. To prompt the user for a new password, use
186 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
187 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800188 * <p>Mode constants are ordered so that higher values are more restrictive;
189 * thus the highest requested mode constant (between the policy set here,
190 * the user's preference, and any other considerations) is the one that
191 * is in effect.
192 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800193 * <p>The calling device admin must have requested
194 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
195 * this method; if it has not, a security exception will be thrown.
196 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800197 * @param admin Which {@link DeviceAdmin} this request is associated with.
198 * @param mode The new desired mode. One of
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800199 * {@link #PASSWORD_MODE_UNSPECIFIED}, {@link #PASSWORD_MODE_SOMETHING},
200 * {@link #PASSWORD_MODE_NUMERIC}, or {@link #PASSWORD_MODE_ALPHANUMERIC}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800201 */
202 public void setPasswordMode(ComponentName admin, int mode) {
203 if (mService != null) {
204 try {
205 mService.setPasswordMode(admin, mode);
206 } catch (RemoteException e) {
207 Log.w(TAG, "Failed talking with device policy service", e);
208 }
209 }
210 }
211
212 /**
213 * Retrieve the current password mode that is in effect due to all
214 * device admins.
215 */
216 public int getPasswordMode() {
217 if (mService != null) {
218 try {
219 return mService.getPasswordMode();
220 } catch (RemoteException e) {
221 Log.w(TAG, "Failed talking with device policy service", e);
222 }
223 }
224 return PASSWORD_MODE_UNSPECIFIED;
225 }
226
227 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800228 * Called by an application that is administering the device to set the
229 * minimum allowed password length. After setting this, the user
230 * will not be able to enter a new password that is not at least as
231 * restrictive as what has been set. Note that the current password
232 * will remain until the user has set a new one, so the change does not
233 * take place immediately. To prompt the user for a new password, use
234 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
235 * constraint is only imposed if the administrator has also requested either
236 * {@link #PASSWORD_MODE_NUMERIC} or {@link #PASSWORD_MODE_ALPHANUMERIC}
237 * with {@link #setPasswordMode}.
238 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800239 * <p>The calling device admin must have requested
240 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
241 * this method; if it has not, a security exception will be thrown.
242 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800243 * @param admin Which {@link DeviceAdmin} this request is associated with.
244 * @param length The new desired minimum password length. A value of 0
245 * means there is no restriction.
246 */
247 public void setMinimumPasswordLength(ComponentName admin, int length) {
248 if (mService != null) {
249 try {
250 mService.setMinimumPasswordLength(admin, length);
251 } catch (RemoteException e) {
252 Log.w(TAG, "Failed talking with device policy service", e);
253 }
254 }
255 }
256
257 /**
258 * Retrieve the current minimum password length that is in effect due to all
259 * device admins.
260 */
261 public int getMinimumPasswordLength() {
262 if (mService != null) {
263 try {
264 return mService.getMinimumPasswordLength();
265 } catch (RemoteException e) {
266 Log.w(TAG, "Failed talking with device policy service", e);
267 }
268 }
269 return 0;
270 }
271
272 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800273 * Determine whether the current password the user has set is sufficient
274 * to meet the policy requirements (mode, minimum length) that have been
275 * requested.
276 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800277 * <p>The calling device admin must have requested
278 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
279 * this method; if it has not, a security exception will be thrown.
280 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800281 * @return Returns true if the password meets the current requirements,
282 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800283 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800284 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800285 if (mService != null) {
286 try {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800287 return mService.isActivePasswordSufficient();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800288 } catch (RemoteException e) {
289 Log.w(TAG, "Failed talking with device policy service", e);
290 }
291 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800292 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800293 }
294
295 /**
296 * Retrieve the number of times the user has failed at entering a
297 * password since that last successful password entry.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800298 *
299 * <p>The calling device admin must have requested
300 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
301 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800302 */
303 public int getCurrentFailedPasswordAttempts() {
304 if (mService != null) {
305 try {
306 return mService.getCurrentFailedPasswordAttempts();
307 } catch (RemoteException e) {
308 Log.w(TAG, "Failed talking with device policy service", e);
309 }
310 }
311 return -1;
312 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800313
314 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800315 * Set the maximum number of failed password attempts that are allowed
316 * before the device wipes its data. This is convenience for implementing
317 * the corresponding functionality with a combination of watching failed
318 * password attempts and calling {@link #wipeData} upon reaching a certain
319 * count, and as such requires that you request both
320 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
321 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
322 *
323 * @param admin Which {@link DeviceAdmin} this request is associated with.
324 * @param num The number of failed password attempts at which point the
325 * device will wipe its data.
326 */
327 public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) {
328 if (mService != null) {
329 try {
330 mService.setMaximumFailedPasswordsForWipe(admin, num);
331 } catch (RemoteException e) {
332 Log.w(TAG, "Failed talking with device policy service", e);
333 }
334 }
335 }
336
337 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800338 * Force a new password on the user. This takes effect immediately. The
339 * given password must meet the current password minimum length constraint
340 * or it will be rejected. The given password will be accepted regardless
341 * of the current password mode, automatically adjusting the password mode
342 * higher if needed. (The string you give here is acceptable for any mode;
343 * if it contains only digits, that is still an acceptable alphanumeric
344 * password.)
345 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800346 * <p>The calling device admin must have requested
347 * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
348 * this method; if it has not, a security exception will be thrown.
349 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800350 * @param password The new password for the user.
351 * @return Returns true if the password was applied, or false if it is
352 * not acceptable for the current constraints.
353 */
354 public boolean resetPassword(String password) {
355 if (mService != null) {
356 try {
357 return mService.resetPassword(password);
358 } catch (RemoteException e) {
359 Log.w(TAG, "Failed talking with device policy service", e);
360 }
361 }
362 return false;
363 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800364
365 /**
366 * Called by an application that is administering the device to set the
367 * maximum time for user activity until the device will lock. This limits
368 * the length that the user can set. It takes effect immediately.
369 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800370 * <p>The calling device admin must have requested
371 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_UNLOCK} to be able to call
372 * this method; if it has not, a security exception will be thrown.
373 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800374 * @param admin Which {@link DeviceAdmin} this request is associated with.
375 * @param timeMs The new desired maximum time to lock in milliseconds.
376 * A value of 0 means there is no restriction.
377 */
378 public void setMaximumTimeToLock(ComponentName admin, long timeMs) {
379 if (mService != null) {
380 try {
381 mService.setMaximumTimeToLock(admin, timeMs);
382 } catch (RemoteException e) {
383 Log.w(TAG, "Failed talking with device policy service", e);
384 }
385 }
386 }
387
388 /**
389 * Retrieve the current maximum time to lock that is in effect due to all
390 * device admins. Returns 0 if no maximum is set.
391 */
392 public long getMaximumTimeToLock() {
393 if (mService != null) {
394 try {
395 return mService.getMaximumTimeToLock();
396 } catch (RemoteException e) {
397 Log.w(TAG, "Failed talking with device policy service", e);
398 }
399 }
400 return 0;
401 }
402
403 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800404 * Make the device lock immediately, as if the lock screen timeout has
405 * expired at the point of this call.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800406 *
407 * <p>The calling device admin must have requested
408 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
409 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800410 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800411 public void lockNow() {
412 if (mService != null) {
413 try {
414 mService.lockNow();
415 } catch (RemoteException e) {
416 Log.w(TAG, "Failed talking with device policy service", e);
417 }
418 }
419 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800420
421 /**
422 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800423 * erasing all user data while next booting up. External storage such
424 * as SD cards will not be erased.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800425 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800426 * <p>The calling device admin must have requested
427 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
428 * this method; if it has not, a security exception will be thrown.
429 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800430 * @param flags Bit mask of additional options: currently must be 0.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800431 */
432 public void wipeData(int flags) {
433 if (mService != null) {
434 try {
435 mService.wipeData(flags);
436 } catch (RemoteException e) {
437 Log.w(TAG, "Failed talking with device policy service", e);
438 }
439 }
440 }
441
442 /**
443 * @hide
444 */
445 public void setActiveAdmin(ComponentName policyReceiver) {
446 if (mService != null) {
447 try {
448 mService.setActiveAdmin(policyReceiver);
449 } catch (RemoteException e) {
450 Log.w(TAG, "Failed talking with device policy service", e);
451 }
452 }
453 }
454
455 /**
456 * @hide
457 */
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800458 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800459 ActivityInfo ai;
460 try {
461 ai = mContext.getPackageManager().getReceiverInfo(cn,
462 PackageManager.GET_META_DATA);
463 } catch (PackageManager.NameNotFoundException e) {
464 Log.w(TAG, "Unable to retrieve device policy " + cn, e);
465 return null;
466 }
467
468 ResolveInfo ri = new ResolveInfo();
469 ri.activityInfo = ai;
470
471 try {
472 return new DeviceAdminInfo(mContext, ri);
473 } catch (XmlPullParserException e) {
474 Log.w(TAG, "Unable to parse device policy " + cn, e);
475 return null;
476 } catch (IOException e) {
477 Log.w(TAG, "Unable to parse device policy " + cn, e);
478 return null;
479 }
480 }
481
482 /**
483 * @hide
484 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800485 public void getRemoveWarning(ComponentName admin, RemoteCallback result) {
486 if (mService != null) {
487 try {
488 mService.getRemoveWarning(admin, result);
489 } catch (RemoteException e) {
490 Log.w(TAG, "Failed talking with device policy service", e);
491 }
492 }
493 }
494
495 /**
496 * @hide
497 */
Dianne Hackbornd6847842010-01-12 18:14:19 -0800498 public void setActivePasswordState(int mode, int length) {
499 if (mService != null) {
500 try {
501 mService.setActivePasswordState(mode, length);
502 } catch (RemoteException e) {
503 Log.w(TAG, "Failed talking with device policy service", e);
504 }
505 }
506 }
507
508 /**
509 * @hide
510 */
511 public void reportFailedPasswordAttempt() {
512 if (mService != null) {
513 try {
514 mService.reportFailedPasswordAttempt();
515 } catch (RemoteException e) {
516 Log.w(TAG, "Failed talking with device policy service", e);
517 }
518 }
519 }
520
521 /**
522 * @hide
523 */
524 public void reportSuccessfulPasswordAttempt() {
525 if (mService != null) {
526 try {
527 mService.reportSuccessfulPasswordAttempt();
528 } catch (RemoteException e) {
529 Log.w(TAG, "Failed talking with device policy service", e);
530 }
531 }
532 }
533}