blob: 2d9b4154d0c4bd0ddc9bc0a429976d830260d4ba [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
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080017package android.app.admin;
Dianne Hackbornd6847842010-01-12 18:14:19 -080018
19import org.xmlpull.v1.XmlPullParserException;
20
21import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080023import android.app.admin.IDevicePolicyManager.Stub;
Dianne Hackbornd6847842010-01-12 18:14:19 -080024import android.content.ComponentName;
25import android.content.Context;
26import android.content.pm.ActivityInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
29import android.os.Handler;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080030import android.os.RemoteCallback;
Dianne Hackbornd6847842010-01-12 18:14:19 -080031import android.os.RemoteException;
32import android.os.ServiceManager;
33import android.util.Log;
34
35import java.io.IOException;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080036import java.util.List;
Dianne Hackbornd6847842010-01-12 18:14:19 -080037
38/**
39 * Public interface for managing policies enforced on a device. Most clients
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080040 * of this class must have published a {@link DeviceAdminReceiver} that the user
Dianne Hackbornd6847842010-01-12 18:14:19 -080041 * has currently enabled.
42 */
43public 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 Hackbornd6847842010-01-12 18:14:19 -080049 private final IDevicePolicyManager mService;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080050
51 private final Handler mHandler;
Dianne Hackbornd6847842010-01-12 18:14:19 -080052
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080053 private DevicePolicyManager(Context context, Handler handler) {
Dianne Hackbornd6847842010-01-12 18:14:19 -080054 mContext = context;
55 mHandler = handler;
56 mService = IDevicePolicyManager.Stub.asInterface(
57 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
58 }
59
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080060 /** @hide */
61 public static DevicePolicyManager create(Context context, Handler handler) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080062 DevicePolicyManager me = new DevicePolicyManager(context, handler);
63 return me.mService != null ? me : null;
64 }
65
Dianne Hackbornd6847842010-01-12 18:14:19 -080066 /**
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 Hackborn8ea138c2010-01-26 18:01:04 -080073 * <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 Hackbornd6847842010-01-12 18:14:19 -080076 */
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 Hackborn8ea138c2010-01-26 18:01:04 -080089 * 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 Hackbornd6847842010-01-12 18:14:19 -080097 * Activity action: have the user enter a new password. This activity
Dianne Hackborn9327f4f2010-01-29 10:38:29 -080098 * should be launched after using {@link #setPasswordQuality(ComponentName, int)}
Dianne Hackborn254cb442010-01-27 19:23:59 -080099 * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800100 * 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 Hackbornd6847842010-01-12 18:14:19 -0800104 * 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 Hackbornd47c6ed2010-01-27 16:21:20 -0800118 return mService.isAdminActive(who);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800119 } catch (RemoteException e) {
120 Log.w(TAG, "Failed talking with device policy service", e);
121 }
122 }
123 return false;
124 }
125
126 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800127 * 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 Hackborn21f1bd12010-02-19 17:02:21 -0800143 * @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 Hackbornd6847842010-01-12 18:14:19 -0800157 * 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 Hackborn9327f4f2010-01-29 10:38:29 -0800173 * Constant for {@link #setPasswordQuality}: the policy has no requirements
174 * for the password. Note that quality constants are ordered so that higher
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800175 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800176 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800177 public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800178
179 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800180 * Constant for {@link #setPasswordQuality}: the policy requires some kind
181 * of password, but doesn't care what it is. Note that quality constants
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800182 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800183 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800184 public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800185
186 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800187 * 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 Hackborndf83afa2010-01-20 13:37:26 -0800190 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800191 public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800192
193 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800194 * 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 Hackbornd6847842010-01-12 18:14:19 -0800198 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800199 public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x30000;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800200
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 Hackborn9327f4f2010-01-29 10:38:29 -0800210 * <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 Hackborndf83afa2010-01-20 13:37:26 -0800212 * the user's preference, and any other considerations) is the one that
213 * is in effect.
214 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800215 * <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 Hackbornef6b22f2010-02-16 20:38:49 -0800219 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800220 * @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 Hackbornd6847842010-01-12 18:14:19 -0800223 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800224 public void setPasswordQuality(ComponentName admin, int quality) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800225 if (mService != null) {
226 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800227 mService.setPasswordQuality(admin, quality);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800228 } catch (RemoteException e) {
229 Log.w(TAG, "Failed talking with device policy service", e);
230 }
231 }
232 }
233
234 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800235 * Retrieve the current minimum password quality for all admins
Dianne Hackborn254cb442010-01-27 19:23:59 -0800236 * or a particular one.
237 * @param admin The name of the admin component to check, or null to aggregate
238 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800239 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800240 public int getPasswordQuality(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800241 if (mService != null) {
242 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800243 return mService.getPasswordQuality(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800244 } catch (RemoteException e) {
245 Log.w(TAG, "Failed talking with device policy service", e);
246 }
247 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800248 return PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800249 }
250
251 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800252 * 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 Hackborn9327f4f2010-01-29 10:38:29 -0800260 * {@link #PASSWORD_QUALITY_NUMERIC} or {@link #PASSWORD_QUALITY_ALPHANUMERIC}
261 * with {@link #setPasswordQuality}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800262 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800263 * <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 Hackbornef6b22f2010-02-16 20:38:49 -0800267 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800268 * @param length The new desired minimum password length. A value of 0
269 * means there is no restriction.
270 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800271 public void setPasswordMinimumLength(ComponentName admin, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800272 if (mService != null) {
273 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800274 mService.setPasswordMinimumLength(admin, length);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800275 } catch (RemoteException e) {
276 Log.w(TAG, "Failed talking with device policy service", e);
277 }
278 }
279 }
280
281 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800282 * 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 Hackbornd6847842010-01-12 18:14:19 -0800286 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800287 public int getPasswordMinimumLength(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800288 if (mService != null) {
289 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800290 return mService.getPasswordMinimumLength(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800291 } catch (RemoteException e) {
292 Log.w(TAG, "Failed talking with device policy service", e);
293 }
294 }
295 return 0;
296 }
297
298 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800299 * Return the maximum password length that the device supports for a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800300 * particular password quality.
Dianne Hackborn364f6e32010-01-29 17:38:20 -0800301 * @param quality The quality being interrogated.
Dianne Hackborn254cb442010-01-27 19:23:59 -0800302 * @return Returns the maximum length that the user can enter.
303 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800304 public int getPasswordMaximumLength(int quality) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800305 // Kind-of arbitrary.
306 return 16;
307 }
308
309 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800310 * Determine whether the current password the user has set is sufficient
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800311 * to meet the policy requirements (quality, minimum length) that have been
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800312 * requested.
313 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800314 * <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 Hackborndf83afa2010-01-20 13:37:26 -0800318 * @return Returns true if the password meets the current requirements,
319 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800320 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800321 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800322 if (mService != null) {
323 try {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800324 return mService.isActivePasswordSufficient();
Dianne Hackbornd6847842010-01-12 18:14:19 -0800325 } catch (RemoteException e) {
326 Log.w(TAG, "Failed talking with device policy service", e);
327 }
328 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800329 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800330 }
331
332 /**
333 * Retrieve the number of times the user has failed at entering a
334 * password since that last successful password entry.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800335 *
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 Hackbornd6847842010-01-12 18:14:19 -0800339 */
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 Hackborndf83afa2010-01-20 13:37:26 -0800350
351 /**
Andrew Stadler88209d12010-02-08 22:59:36 -0800352 * 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 Hackborn8ea138c2010-01-26 18:01:04 -0800357 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
358 *
Andrew Stadler88209d12010-02-08 22:59:36 -0800359 * <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 Hackbornef6b22f2010-02-16 20:38:49 -0800362 * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
Andrew Stadler88209d12010-02-08 22:59:36 -0800363 * 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 Hackbornef6b22f2010-02-16 20:38:49 -0800366 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800367 * @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 Hackborn254cb442010-01-27 19:23:59 -0800381 * 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 Hackborn87bba1e2010-02-26 17:25:54 -0800399 * 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 Hackbornef6b22f2010-02-16 20:38:49 -0800405 * 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 Hackborn9327f4f2010-01-29 10:38:29 -0800408 * 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 Hackborndf83afa2010-01-20 13:37:26 -0800416 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800417 * <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 Hackborndf83afa2010-01-20 13:37:26 -0800421 * @param password The new password for the user.
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800422 * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800423 * @return Returns true if the password was applied, or false if it is
424 * not acceptable for the current constraints.
425 */
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800426 public boolean resetPassword(String password, int flags) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800427 if (mService != null) {
428 try {
Dianne Hackborn87bba1e2010-02-26 17:25:54 -0800429 return mService.resetPassword(password, flags);
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800430 } catch (RemoteException e) {
431 Log.w(TAG, "Failed talking with device policy service", e);
432 }
433 }
434 return false;
435 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800436
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 Hackborn8aa2e892010-01-22 11:31:30 -0800442 * <p>The calling device admin must have requested
Dianne Hackborn315ada72010-02-11 12:14:08 -0800443 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800444 * this method; if it has not, a security exception will be thrown.
445 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800446 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800447 * @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 Hackborn254cb442010-01-27 19:23:59 -0800461 * 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 Hackbornd6847842010-01-12 18:14:19 -0800465 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800466 public long getMaximumTimeToLock(ComponentName admin) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800467 if (mService != null) {
468 try {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800469 return mService.getMaximumTimeToLock(admin);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800470 } catch (RemoteException e) {
471 Log.w(TAG, "Failed talking with device policy service", e);
472 }
473 }
474 return 0;
475 }
476
477 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800478 * Make the device lock immediately, as if the lock screen timeout has
479 * expired at the point of this call.
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800480 *
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 Hackbornd6847842010-01-12 18:14:19 -0800484 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800485 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 Hackbornd6847842010-01-12 18:14:19 -0800494
495 /**
496 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800497 * erasing all user data while next booting up. External storage such
498 * as SD cards will not be erased.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800499 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800500 * <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 Hackborndf83afa2010-01-20 13:37:26 -0800504 * @param flags Bit mask of additional options: currently must be 0.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800505 */
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 Hackbornd47c6ed2010-01-27 16:21:20 -0800532 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800533 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 Hackborn8ea138c2010-01-26 18:01:04 -0800559 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 Hackborn9327f4f2010-01-29 10:38:29 -0800572 public void setActivePasswordState(int quality, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800573 if (mService != null) {
574 try {
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800575 mService.setActivePasswordState(quality, length);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800576 } 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}