blob: 0e8c1ab07aa661d2f56deb0321fd8c9ac7e9f71a [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
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080039 * of this class must have published a {@link DeviceAdminReceiver} that the user
Dianne Hackbornd6847842010-01-12 18:14:19 -080040 * 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
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080052 private DevicePolicyManager(Context context, Handler handler) {
Dianne Hackbornd6847842010-01-12 18:14:19 -080053 mContext = context;
54 mHandler = handler;
55 mService = IDevicePolicyManager.Stub.asInterface(
56 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
57 }
58
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080059 /*package*/ static DevicePolicyManager create(Context context, Handler handler) {
60 DevicePolicyManager me = new DevicePolicyManager(context, handler);
61 return me.mService != null ? me : null;
62 }
63
Dianne Hackbornd6847842010-01-12 18:14:19 -080064 /**
65 * Activity action: ask the user to add a new device administrator to the system.
66 * The desired policy is the ComponentName of the policy in the
67 * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to
68 * bring the user through adding the device administrator to the system (or
69 * allowing them to reject it).
70 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080071 * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
72 * field to provide the user with additional explanation (in addition
73 * to your component's description) about what is being added.
Dianne Hackbornd6847842010-01-12 18:14:19 -080074 */
75 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
76 public static final String ACTION_ADD_DEVICE_ADMIN
77 = "android.app.action.ADD_DEVICE_ADMIN";
78
79 /**
80 * The ComponentName of the administrator component.
81 *
82 * @see #ACTION_ADD_DEVICE_ADMIN
83 */
84 public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
85
86 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080087 * An optional CharSequence providing additional explanation for why the
88 * admin is being added.
89 *
90 * @see #ACTION_ADD_DEVICE_ADMIN
91 */
92 public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
93
94 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -080095 * Activity action: have the user enter a new password. This activity
Dianne Hackborn9327f4f2010-01-29 10:38:29 -080096 * should be launched after using {@link #setPasswordQuality(ComponentName, int)}
Dianne Hackborn254cb442010-01-27 19:23:59 -080097 * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the
Dianne Hackborndf83afa2010-01-20 13:37:26 -080098 * user enter a new password that meets the current requirements. You can
99 * use {@link #isActivePasswordSufficient()} to determine whether you need
100 * to have the user select a new password in order to meet the current
101 * constraints. Upon being resumed from this activity,
Dianne Hackbornd6847842010-01-12 18:14:19 -0800102 * you can check the new password characteristics to see if they are
103 * sufficient.
104 */
105 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
106 public static final String ACTION_SET_NEW_PASSWORD
107 = "android.app.action.SET_NEW_PASSWORD";
108
109 /**
110 * Return true if the given administrator component is currently
111 * active (enabled) in the system.
112 */
113 public boolean isAdminActive(ComponentName who) {
114 if (mService != null) {
115 try {
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800116 return mService.isAdminActive(who);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800117 } catch (RemoteException e) {
118 Log.w(TAG, "Failed talking with device policy service", e);
119 }
120 }
121 return false;
122 }
123
124 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800125 * Return a list of all currently active device administrator's component
126 * names. Note that if there are no administrators than null may be
127 * returned.
128 */
129 public List<ComponentName> getActiveAdmins() {
130 if (mService != null) {
131 try {
132 return mService.getActiveAdmins();
133 } catch (RemoteException e) {
134 Log.w(TAG, "Failed talking with device policy service", e);
135 }
136 }
137 return null;
138 }
139
140 /**
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800141 * @hide
142 */
143 public boolean packageHasActiveAdmins(String packageName) {
144 if (mService != null) {
145 try {
146 return mService.packageHasActiveAdmins(packageName);
147 } catch (RemoteException e) {
148 Log.w(TAG, "Failed talking with device policy service", e);
149 }
150 }
151 return false;
152 }
153
154 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800155 * Remove a current administration component. This can only be called
156 * by the application that owns the administration component; if you
157 * try to remove someone else's component, a security exception will be
158 * thrown.
159 */
160 public void removeActiveAdmin(ComponentName who) {
161 if (mService != null) {
162 try {
163 mService.removeActiveAdmin(who);
164 } catch (RemoteException e) {
165 Log.w(TAG, "Failed talking with device policy service", e);
166 }
167 }
168 }
169
170 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800171 * Constant for {@link #setPasswordQuality}: the policy has no requirements
172 * for the password. Note that quality constants are ordered so that higher
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800173 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800174 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800175 public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800176
177 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800178 * Constant for {@link #setPasswordQuality}: the policy requires some kind
179 * of password, but doesn't care what it is. Note that quality constants
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800180 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800181 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800182 public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800183
184 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800185 * Constant for {@link #setPasswordQuality}: the user must have entered a
186 * password containing at least numeric characters. Note that quality
187 * constants are ordered so that higher values are more restrictive.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800188 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800189 public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800190
191 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800192 * Constant for {@link #setPasswordQuality}: the user must have entered a
193 * password containing at least <em>both></em> numeric <em>and</em>
194 * alphabeter (or other symbol) characters. Note that quality constants are
195 * ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800196 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800197 public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x30000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800198
199 /**
200 * Called by an application that is administering the device to set the
201 * password restrictions it is imposing. After setting this, the user
202 * will not be able to enter a new password that is not at least as
203 * restrictive as what has been set. Note that the current password
204 * will remain until the user has set a new one, so the change does not
205 * take place immediately. To prompt the user for a new password, use
206 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
207 *
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800208 * <p>Quality constants are ordered so that higher values are more restrictive;
209 * thus the highest requested quality constant (between the policy set here,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800210 * the user's preference, and any other considerations) is the one that
211 * is in effect.
212 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800213 * <p>The calling device admin must have requested
214 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
215 * this method; if it has not, a security exception will be thrown.
216 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800217 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800218 * @param quality The new desired quality. One of
219 * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING},
220 * {@link #PASSWORD_QUALITY_NUMERIC}, or {@link #PASSWORD_QUALITY_ALPHANUMERIC}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800221 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800222 public void setPasswordQuality(ComponentName admin, int quality) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800223 if (mService != null) {
224 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800225 mService.setPasswordQuality(admin, quality);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800226 } catch (RemoteException e) {
227 Log.w(TAG, "Failed talking with device policy service", e);
228 }
229 }
230 }
231
232 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800233 * Retrieve the current minimum password quality for all admins
Dianne Hackborn254cb442010-01-27 19:23:59 -0800234 * or a particular one.
235 * @param admin The name of the admin component to check, or null to aggregate
236 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800237 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800238 public int getPasswordQuality(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800239 if (mService != null) {
240 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800241 return mService.getPasswordQuality(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800242 } catch (RemoteException e) {
243 Log.w(TAG, "Failed talking with device policy service", e);
244 }
245 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800246 return PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800247 }
248
249 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800250 * Called by an application that is administering the device to set the
251 * minimum allowed password length. After setting this, the user
252 * will not be able to enter a new password that is not at least as
253 * restrictive as what has been set. Note that the current password
254 * will remain until the user has set a new one, so the change does not
255 * take place immediately. To prompt the user for a new password, use
256 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
257 * constraint is only imposed if the administrator has also requested either
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800258 * {@link #PASSWORD_QUALITY_NUMERIC} or {@link #PASSWORD_QUALITY_ALPHANUMERIC}
259 * with {@link #setPasswordQuality}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800260 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800261 * <p>The calling device admin must have requested
262 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
263 * this method; if it has not, a security exception will be thrown.
264 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800265 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800266 * @param length The new desired minimum password length. A value of 0
267 * means there is no restriction.
268 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800269 public void setPasswordMinimumLength(ComponentName admin, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800270 if (mService != null) {
271 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800272 mService.setPasswordMinimumLength(admin, length);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800273 } catch (RemoteException e) {
274 Log.w(TAG, "Failed talking with device policy service", e);
275 }
276 }
277 }
278
279 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800280 * Retrieve the current minimum password length for all admins
281 * or a particular one.
282 * @param admin The name of the admin component to check, or null to aggregate
283 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800284 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800285 public int getPasswordMinimumLength(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800286 if (mService != null) {
287 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800288 return mService.getPasswordMinimumLength(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800289 } catch (RemoteException e) {
290 Log.w(TAG, "Failed talking with device policy service", e);
291 }
292 }
293 return 0;
294 }
295
296 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800297 * Return the maximum password length that the device supports for a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800298 * particular password quality.
Dianne Hackborn364f6e32010-01-29 17:38:20 -0800299 * @param quality The quality being interrogated.
Dianne Hackborn254cb442010-01-27 19:23:59 -0800300 * @return Returns the maximum length that the user can enter.
301 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800302 public int getPasswordMaximumLength(int quality) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800303 // Kind-of arbitrary.
304 return 16;
305 }
306
307 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800308 * Determine whether the current password the user has set is sufficient
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800309 * to meet the policy requirements (quality, minimum length) that have been
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800310 * requested.
311 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800312 * <p>The calling device admin must have requested
313 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
314 * this method; if it has not, a security exception will be thrown.
315 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800316 * @return Returns true if the password meets the current requirements,
317 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800318 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800319 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800320 if (mService != null) {
321 try {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800322 return mService.isActivePasswordSufficient();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800323 } catch (RemoteException e) {
324 Log.w(TAG, "Failed talking with device policy service", e);
325 }
326 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800327 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800328 }
329
330 /**
331 * Retrieve the number of times the user has failed at entering a
332 * password since that last successful password entry.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800333 *
334 * <p>The calling device admin must have requested
335 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
336 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800337 */
338 public int getCurrentFailedPasswordAttempts() {
339 if (mService != null) {
340 try {
341 return mService.getCurrentFailedPasswordAttempts();
342 } catch (RemoteException e) {
343 Log.w(TAG, "Failed talking with device policy service", e);
344 }
345 }
346 return -1;
347 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800348
349 /**
Andrew Stadler88209d12010-02-08 22:59:36 -0800350 * Setting this to a value greater than zero enables a built-in policy
351 * that will perform a device wipe after too many incorrect
352 * device-unlock passwords have been entered. This built-in policy combines
353 * watching for failed passwords and wiping the device, and requires
354 * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800355 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
356 *
Andrew Stadler88209d12010-02-08 22:59:36 -0800357 * <p>To implement any other policy (e.g. wiping data for a particular
358 * application only, erasing or revoking credentials, or reporting the
359 * failure to a server), you should implement
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800360 * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
Andrew Stadler88209d12010-02-08 22:59:36 -0800361 * instead. Do not use this API, because if the maximum count is reached,
362 * the device will be wiped immediately, and your callback will not be invoked.
363 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800364 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800365 * @param num The number of failed password attempts at which point the
366 * device will wipe its data.
367 */
368 public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) {
369 if (mService != null) {
370 try {
371 mService.setMaximumFailedPasswordsForWipe(admin, num);
372 } catch (RemoteException e) {
373 Log.w(TAG, "Failed talking with device policy service", e);
374 }
375 }
376 }
377
378 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800379 * Retrieve the current maximum number of login attempts that are allowed
380 * before the device wipes itself, for all admins
381 * or a particular one.
382 * @param admin The name of the admin component to check, or null to aggregate
383 * all admins.
384 */
385 public int getMaximumFailedPasswordsForWipe(ComponentName admin) {
386 if (mService != null) {
387 try {
388 return mService.getMaximumFailedPasswordsForWipe(admin);
389 } catch (RemoteException e) {
390 Log.w(TAG, "Failed talking with device policy service", e);
391 }
392 }
393 return 0;
394 }
395
396 /**
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800397 * Force a new device unlock password (the password needed to access the
398 * entire device, not for individual accounts) on the user. This takes
399 * effect immediately.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800400 * The given password must be sufficient for the
401 * current password quality and length constraints as returned by
402 * {@link #getPasswordQuality(ComponentName)} and
403 * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet
404 * these constraints, then it will be rejected and false returned. Note
405 * that the password may be a stronger quality (containing alphanumeric
406 * characters when the requested quality is only numeric), in which case
407 * the currently active quality will be increased to match.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800408 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800409 * <p>The calling device admin must have requested
410 * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
411 * this method; if it has not, a security exception will be thrown.
412 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800413 * @param password The new password for the user.
414 * @return Returns true if the password was applied, or false if it is
415 * not acceptable for the current constraints.
416 */
417 public boolean resetPassword(String password) {
418 if (mService != null) {
419 try {
420 return mService.resetPassword(password);
421 } catch (RemoteException e) {
422 Log.w(TAG, "Failed talking with device policy service", e);
423 }
424 }
425 return false;
426 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800427
428 /**
429 * Called by an application that is administering the device to set the
430 * maximum time for user activity until the device will lock. This limits
431 * the length that the user can set. It takes effect immediately.
432 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800433 * <p>The calling device admin must have requested
Dianne Hackborn315ada72010-02-11 12:14:08 -0800434 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800435 * this method; if it has not, a security exception will be thrown.
436 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800437 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800438 * @param timeMs The new desired maximum time to lock in milliseconds.
439 * A value of 0 means there is no restriction.
440 */
441 public void setMaximumTimeToLock(ComponentName admin, long timeMs) {
442 if (mService != null) {
443 try {
444 mService.setMaximumTimeToLock(admin, timeMs);
445 } catch (RemoteException e) {
446 Log.w(TAG, "Failed talking with device policy service", e);
447 }
448 }
449 }
450
451 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800452 * Retrieve the current maximum time to unlock for all admins
453 * or a particular one.
454 * @param admin The name of the admin component to check, or null to aggregate
455 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800456 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800457 public long getMaximumTimeToLock(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800458 if (mService != null) {
459 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800460 return mService.getMaximumTimeToLock(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800461 } catch (RemoteException e) {
462 Log.w(TAG, "Failed talking with device policy service", e);
463 }
464 }
465 return 0;
466 }
467
468 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800469 * Make the device lock immediately, as if the lock screen timeout has
470 * expired at the point of this call.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800471 *
472 * <p>The calling device admin must have requested
473 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
474 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800475 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800476 public void lockNow() {
477 if (mService != null) {
478 try {
479 mService.lockNow();
480 } catch (RemoteException e) {
481 Log.w(TAG, "Failed talking with device policy service", e);
482 }
483 }
484 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800485
486 /**
487 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800488 * erasing all user data while next booting up. External storage such
489 * as SD cards will not be erased.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800490 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800491 * <p>The calling device admin must have requested
492 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
493 * this method; if it has not, a security exception will be thrown.
494 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800495 * @param flags Bit mask of additional options: currently must be 0.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800496 */
497 public void wipeData(int flags) {
498 if (mService != null) {
499 try {
500 mService.wipeData(flags);
501 } catch (RemoteException e) {
502 Log.w(TAG, "Failed talking with device policy service", e);
503 }
504 }
505 }
506
507 /**
508 * @hide
509 */
510 public void setActiveAdmin(ComponentName policyReceiver) {
511 if (mService != null) {
512 try {
513 mService.setActiveAdmin(policyReceiver);
514 } catch (RemoteException e) {
515 Log.w(TAG, "Failed talking with device policy service", e);
516 }
517 }
518 }
519
520 /**
521 * @hide
522 */
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800523 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800524 ActivityInfo ai;
525 try {
526 ai = mContext.getPackageManager().getReceiverInfo(cn,
527 PackageManager.GET_META_DATA);
528 } catch (PackageManager.NameNotFoundException e) {
529 Log.w(TAG, "Unable to retrieve device policy " + cn, e);
530 return null;
531 }
532
533 ResolveInfo ri = new ResolveInfo();
534 ri.activityInfo = ai;
535
536 try {
537 return new DeviceAdminInfo(mContext, ri);
538 } catch (XmlPullParserException e) {
539 Log.w(TAG, "Unable to parse device policy " + cn, e);
540 return null;
541 } catch (IOException e) {
542 Log.w(TAG, "Unable to parse device policy " + cn, e);
543 return null;
544 }
545 }
546
547 /**
548 * @hide
549 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800550 public void getRemoveWarning(ComponentName admin, RemoteCallback result) {
551 if (mService != null) {
552 try {
553 mService.getRemoveWarning(admin, result);
554 } catch (RemoteException e) {
555 Log.w(TAG, "Failed talking with device policy service", e);
556 }
557 }
558 }
559
560 /**
561 * @hide
562 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800563 public void setActivePasswordState(int quality, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800564 if (mService != null) {
565 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800566 mService.setActivePasswordState(quality, length);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800567 } catch (RemoteException e) {
568 Log.w(TAG, "Failed talking with device policy service", e);
569 }
570 }
571 }
572
573 /**
574 * @hide
575 */
576 public void reportFailedPasswordAttempt() {
577 if (mService != null) {
578 try {
579 mService.reportFailedPasswordAttempt();
580 } catch (RemoteException e) {
581 Log.w(TAG, "Failed talking with device policy service", e);
582 }
583 }
584 }
585
586 /**
587 * @hide
588 */
589 public void reportSuccessfulPasswordAttempt() {
590 if (mService != null) {
591 try {
592 mService.reportSuccessfulPasswordAttempt();
593 } catch (RemoteException e) {
594 Log.w(TAG, "Failed talking with device policy service", e);
595 }
596 }
597 }
598}