blob: 61ff60ab2bf07474e61dd5d0a04776647a0110ad [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;
23import android.content.ComponentName;
24import android.content.Context;
Adam Connors010cfd42014-04-16 12:48:13 +010025import android.content.Intent;
Sander Alewijnsef475ca32014-02-17 15:13:58 +000026import android.content.IntentFilter;
Dianne Hackbornd6847842010-01-12 18:14:19 -080027import android.content.pm.ActivityInfo;
28import android.content.pm.PackageManager;
29import android.content.pm.ResolveInfo;
Robin Lee66e5d962014-04-09 16:44:21 +010030import android.os.Bundle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080031import android.os.Handler;
Adam Connors776c5552014-01-09 10:42:56 +000032import android.os.Process;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080033import android.os.RemoteCallback;
Dianne Hackbornd6847842010-01-12 18:14:19 -080034import android.os.RemoteException;
35import android.os.ServiceManager;
Amith Yamasani599dd7c2012-09-14 23:20:08 -070036import android.os.UserHandle;
Jim Miller50e62182014-04-23 17:25:00 -070037import android.service.trust.TrustAgentService;
Dianne Hackbornd6847842010-01-12 18:14:19 -080038import android.util.Log;
39
Maggie Benthallda51e682013-08-08 22:35:44 -040040import com.android.org.conscrypt.TrustedCertificateStore;
41
42import java.io.ByteArrayInputStream;
Dianne Hackbornd6847842010-01-12 18:14:19 -080043import java.io.IOException;
Oscar Montemayor69238c62010-08-03 10:51:06 -070044import java.net.InetSocketAddress;
45import java.net.Proxy;
Maggie Benthallda51e682013-08-08 22:35:44 -040046import java.security.cert.CertificateException;
47import java.security.cert.CertificateFactory;
48import java.security.cert.X509Certificate;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080049import java.util.List;
Maggie Benthallda51e682013-08-08 22:35:44 -040050import java.util.Set;
Dianne Hackbornd6847842010-01-12 18:14:19 -080051
52/**
53 * Public interface for managing policies enforced on a device. Most clients
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080054 * of this class must have published a {@link DeviceAdminReceiver} that the user
Dianne Hackbornd6847842010-01-12 18:14:19 -080055 * has currently enabled.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080056 *
57 * <div class="special reference">
58 * <h3>Developer Guides</h3>
59 * <p>For more information about managing policies for device adminstration, read the
60 * <a href="{@docRoot}guide/topics/admin/device-admin.html">Device Administration</a>
61 * developer guide.</p>
62 * </div>
Dianne Hackbornd6847842010-01-12 18:14:19 -080063 */
64public class DevicePolicyManager {
65 private static String TAG = "DevicePolicyManager";
Dianne Hackbornd6847842010-01-12 18:14:19 -080066
67 private final Context mContext;
Dianne Hackbornd6847842010-01-12 18:14:19 -080068 private final IDevicePolicyManager mService;
Konstantin Lopyrev32558232010-05-20 16:18:05 -070069
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080070 private DevicePolicyManager(Context context, Handler handler) {
Dianne Hackbornd6847842010-01-12 18:14:19 -080071 mContext = context;
Dianne Hackbornd6847842010-01-12 18:14:19 -080072 mService = IDevicePolicyManager.Stub.asInterface(
73 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
74 }
75
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080076 /** @hide */
77 public static DevicePolicyManager create(Context context, Handler handler) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080078 DevicePolicyManager me = new DevicePolicyManager(context, handler);
79 return me.mService != null ? me : null;
80 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -070081
Dianne Hackbornd6847842010-01-12 18:14:19 -080082 /**
Jessica Hummelf72078b2014-03-06 16:13:12 +000083 * Activity action: Starts the provisioning flow which sets up a managed profile.
84 * This intent will typically be sent by a mobile device management application(mdm).
85 * Managed profile provisioning creates a profile, moves the mdm to the profile,
86 * sets the mdm as the profile owner and removes all non required applications from the profile.
87 * As a profile owner the mdm than has full control over the managed profile.
88 *
89 * <p>The intent must contain the extras {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME} and
90 * {@link #EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME}.
91 *
92 * <p> When managed provisioning has completed, an intent of the type
93 * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE} is broadcasted to the
94 * mdm app on the managed profile.
95 *
96 * <p>Input: Nothing.</p>
97 * <p>Output: Nothing</p>
98 */
99 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
100 public static final String ACTION_PROVISION_MANAGED_PROFILE
Jessica Hummel03dd2202014-03-13 16:05:26 +0000101 = "android.app.action.ACTION_PROVISION_MANAGED_PROFILE";
Jessica Hummelf72078b2014-03-06 16:13:12 +0000102
103 /**
104 * A String extra holding the name of the package of the mobile device management application
105 * that starts the managed provisioning flow. This package will be set as the profile owner.
106 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}.
107 */
108 public static final String EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME
109 = "deviceAdminPackageName";
110
111 /**
112 * A String extra holding the default name of the profile that is created during managed profile
113 * provisioning.
114 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}
115 */
116 public static final String EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME
117 = "defaultManagedProfileName";
118
119 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800120 * Activity action: ask the user to add a new device administrator to the system.
121 * The desired policy is the ComponentName of the policy in the
122 * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to
123 * bring the user through adding the device administrator to the system (or
124 * allowing them to reject it).
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700125 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800126 * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
127 * field to provide the user with additional explanation (in addition
128 * to your component's description) about what is being added.
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800129 *
130 * <p>If your administrator is already active, this will ordinarily return immediately (without
131 * user intervention). However, if your administrator has been updated and is requesting
132 * additional uses-policy flags, the user will be presented with the new list. New policies
133 * will not be available to the updated administrator until the user has accepted the new list.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800134 */
135 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
136 public static final String ACTION_ADD_DEVICE_ADMIN
137 = "android.app.action.ADD_DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700138
Dianne Hackbornd6847842010-01-12 18:14:19 -0800139 /**
Jim Miller284b62e2010-06-08 14:27:42 -0700140 * Activity action: send when any policy admin changes a policy.
141 * This is generally used to find out when a new policy is in effect.
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700142 *
Jim Miller284b62e2010-06-08 14:27:42 -0700143 * @hide
144 */
145 public static final String ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
146 = "android.app.action.DEVICE_POLICY_MANAGER_STATE_CHANGED";
147
148 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800149 * The ComponentName of the administrator component.
150 *
151 * @see #ACTION_ADD_DEVICE_ADMIN
152 */
153 public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700154
Dianne Hackbornd6847842010-01-12 18:14:19 -0800155 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800156 * An optional CharSequence providing additional explanation for why the
157 * admin is being added.
158 *
159 * @see #ACTION_ADD_DEVICE_ADMIN
160 */
161 public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700162
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800163 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700164 * Activity action: have the user enter a new password. This activity should
165 * be launched after using {@link #setPasswordQuality(ComponentName, int)},
166 * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the user
167 * enter a new password that meets the current requirements. You can use
168 * {@link #isActivePasswordSufficient()} to determine whether you need to
169 * have the user select a new password in order to meet the current
170 * constraints. Upon being resumed from this activity, you can check the new
171 * password characteristics to see if they are sufficient.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800172 */
173 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
174 public static final String ACTION_SET_NEW_PASSWORD
175 = "android.app.action.SET_NEW_PASSWORD";
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000176 /**
Nicolas Prevotc79586e2014-05-06 12:47:57 +0100177 * Flag for {@link #addForwardingIntentFilter}: the intents will forwarded to the primary user.
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000178 */
179 public static int FLAG_TO_PRIMARY_USER = 0x0001;
180
181 /**
Nicolas Prevotc79586e2014-05-06 12:47:57 +0100182 * Flag for {@link #addForwardingIntentFilter}: the intents will be forwarded to the managed
Nicolas Prevot10fa67c2014-03-24 13:44:38 +0000183 * profile.
184 */
185 public static int FLAG_TO_MANAGED_PROFILE = 0x0002;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700186
Dianne Hackbornd6847842010-01-12 18:14:19 -0800187 /**
188 * Return true if the given administrator component is currently
189 * active (enabled) in the system.
190 */
191 public boolean isAdminActive(ComponentName who) {
192 if (mService != null) {
193 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700194 return mService.isAdminActive(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800195 } catch (RemoteException e) {
196 Log.w(TAG, "Failed talking with device policy service", e);
197 }
198 }
199 return false;
200 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700201
Dianne Hackbornd6847842010-01-12 18:14:19 -0800202 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800203 * Return a list of all currently active device administrator's component
204 * names. Note that if there are no administrators than null may be
205 * returned.
206 */
207 public List<ComponentName> getActiveAdmins() {
208 if (mService != null) {
209 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700210 return mService.getActiveAdmins(UserHandle.myUserId());
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800211 } catch (RemoteException e) {
212 Log.w(TAG, "Failed talking with device policy service", e);
213 }
214 }
215 return null;
216 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700217
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800218 /**
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700219 * Used by package administration code to determine if a package can be stopped
220 * or uninstalled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800221 * @hide
222 */
223 public boolean packageHasActiveAdmins(String packageName) {
224 if (mService != null) {
225 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700226 return mService.packageHasActiveAdmins(packageName, UserHandle.myUserId());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800227 } catch (RemoteException e) {
228 Log.w(TAG, "Failed talking with device policy service", e);
229 }
230 }
231 return false;
232 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700233
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800234 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800235 * Remove a current administration component. This can only be called
236 * by the application that owns the administration component; if you
237 * try to remove someone else's component, a security exception will be
238 * thrown.
239 */
240 public void removeActiveAdmin(ComponentName who) {
241 if (mService != null) {
242 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700243 mService.removeActiveAdmin(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800244 } catch (RemoteException e) {
245 Log.w(TAG, "Failed talking with device policy service", e);
246 }
247 }
248 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700249
Dianne Hackbornd6847842010-01-12 18:14:19 -0800250 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800251 * Returns true if an administrator has been granted a particular device policy. This can
252 * be used to check if the administrator was activated under an earlier set of policies,
253 * but requires additional policies after an upgrade.
254 *
255 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. Must be
256 * an active administrator, or an exception will be thrown.
257 * @param usesPolicy Which uses-policy to check, as defined in {@link DeviceAdminInfo}.
258 */
259 public boolean hasGrantedPolicy(ComponentName admin, int usesPolicy) {
260 if (mService != null) {
261 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700262 return mService.hasGrantedPolicy(admin, usesPolicy, UserHandle.myUserId());
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800263 } catch (RemoteException e) {
264 Log.w(TAG, "Failed talking with device policy service", e);
265 }
266 }
267 return false;
268 }
269
270 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800271 * Constant for {@link #setPasswordQuality}: the policy has no requirements
272 * for the password. Note that quality constants are ordered so that higher
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800273 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800274 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800275 public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700276
Dianne Hackbornd6847842010-01-12 18:14:19 -0800277 /**
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700278 * Constant for {@link #setPasswordQuality}: the policy allows for low-security biometric
279 * recognition technology. This implies technologies that can recognize the identity of
280 * an individual to about a 3 digit PIN (false detection is less than 1 in 1,000).
281 * Note that quality constants are ordered so that higher values are more restrictive.
282 */
283 public static final int PASSWORD_QUALITY_BIOMETRIC_WEAK = 0x8000;
284
285 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800286 * Constant for {@link #setPasswordQuality}: the policy requires some kind
287 * of password, but doesn't care what it is. Note that quality constants
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800288 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800289 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800290 public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700291
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800292 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800293 * Constant for {@link #setPasswordQuality}: the user must have entered a
294 * password containing at least numeric characters. Note that quality
295 * constants are ordered so that higher values are more restrictive.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800296 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800297 public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700298
Dianne Hackbornd6847842010-01-12 18:14:19 -0800299 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800300 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700301 * password containing at least alphabetic (or other symbol) characters.
302 * Note that quality constants are ordered so that higher values are more
303 * restrictive.
304 */
305 public static final int PASSWORD_QUALITY_ALPHABETIC = 0x40000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700306
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700307 /**
308 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800309 * password containing at least <em>both></em> numeric <em>and</em>
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700310 * alphabetic (or other symbol) characters. Note that quality constants are
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800311 * ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800312 */
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700313 public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x50000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700314
Dianne Hackbornd6847842010-01-12 18:14:19 -0800315 /**
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700316 * Constant for {@link #setPasswordQuality}: the user must have entered a
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700317 * password containing at least a letter, a numerical digit and a special
318 * symbol, by default. With this password quality, passwords can be
319 * restricted to contain various sets of characters, like at least an
320 * uppercase letter, etc. These are specified using various methods,
321 * like {@link #setPasswordMinimumLowerCase(ComponentName, int)}. Note
322 * that quality constants are ordered so that higher values are more
323 * restrictive.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700324 */
325 public static final int PASSWORD_QUALITY_COMPLEX = 0x60000;
326
327 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800328 * Called by an application that is administering the device to set the
329 * password restrictions it is imposing. After setting this, the user
330 * will not be able to enter a new password that is not at least as
331 * restrictive as what has been set. Note that the current password
332 * will remain until the user has set a new one, so the change does not
333 * take place immediately. To prompt the user for a new password, use
334 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700335 *
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800336 * <p>Quality constants are ordered so that higher values are more restrictive;
337 * thus the highest requested quality constant (between the policy set here,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800338 * the user's preference, and any other considerations) is the one that
339 * is in effect.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700340 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800341 * <p>The calling device admin must have requested
342 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
343 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700344 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800345 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800346 * @param quality The new desired quality. One of
347 * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING},
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700348 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC},
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700349 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} or {@link #PASSWORD_QUALITY_COMPLEX}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800350 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800351 public void setPasswordQuality(ComponentName admin, int quality) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800352 if (mService != null) {
353 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700354 mService.setPasswordQuality(admin, quality, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800355 } catch (RemoteException e) {
356 Log.w(TAG, "Failed talking with device policy service", e);
357 }
358 }
359 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700360
Dianne Hackbornd6847842010-01-12 18:14:19 -0800361 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800362 * Retrieve the current minimum password quality for all admins
Dianne Hackborn254cb442010-01-27 19:23:59 -0800363 * or a particular one.
364 * @param admin The name of the admin component to check, or null to aggregate
365 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800366 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800367 public int getPasswordQuality(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700368 return getPasswordQuality(admin, UserHandle.myUserId());
369 }
370
371 /** @hide per-user version */
372 public int getPasswordQuality(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800373 if (mService != null) {
374 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700375 return mService.getPasswordQuality(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800376 } catch (RemoteException e) {
377 Log.w(TAG, "Failed talking with device policy service", e);
378 }
379 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800380 return PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800381 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700382
Dianne Hackbornd6847842010-01-12 18:14:19 -0800383 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800384 * Called by an application that is administering the device to set the
385 * minimum allowed password length. After setting this, the user
386 * will not be able to enter a new password that is not at least as
387 * restrictive as what has been set. Note that the current password
388 * will remain until the user has set a new one, so the change does not
389 * take place immediately. To prompt the user for a new password, use
390 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
391 * constraint is only imposed if the administrator has also requested either
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700392 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC}
393 * {@link #PASSWORD_QUALITY_ALPHANUMERIC}, or {@link #PASSWORD_QUALITY_COMPLEX}
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800394 * with {@link #setPasswordQuality}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700395 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800396 * <p>The calling device admin must have requested
397 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
398 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700399 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800400 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800401 * @param length The new desired minimum password length. A value of 0
402 * means there is no restriction.
403 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800404 public void setPasswordMinimumLength(ComponentName admin, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800405 if (mService != null) {
406 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700407 mService.setPasswordMinimumLength(admin, length, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800408 } catch (RemoteException e) {
409 Log.w(TAG, "Failed talking with device policy service", e);
410 }
411 }
412 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700413
Dianne Hackbornd6847842010-01-12 18:14:19 -0800414 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800415 * Retrieve the current minimum password length for all admins
416 * or a particular one.
417 * @param admin The name of the admin component to check, or null to aggregate
418 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800419 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800420 public int getPasswordMinimumLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700421 return getPasswordMinimumLength(admin, UserHandle.myUserId());
422 }
423
424 /** @hide per-user version */
425 public int getPasswordMinimumLength(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800426 if (mService != null) {
427 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700428 return mService.getPasswordMinimumLength(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800429 } catch (RemoteException e) {
430 Log.w(TAG, "Failed talking with device policy service", e);
431 }
432 }
433 return 0;
434 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700435
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700436 /**
437 * Called by an application that is administering the device to set the
438 * minimum number of upper case letters required in the password. After
439 * setting this, the user will not be able to enter a new password that is
440 * not at least as restrictive as what has been set. Note that the current
441 * password will remain until the user has set a new one, so the change does
442 * not take place immediately. To prompt the user for a new password, use
443 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
444 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700445 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
446 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700447 * <p>
448 * The calling device admin must have requested
449 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
450 * this method; if it has not, a security exception will be thrown.
451 *
452 * @param admin Which {@link DeviceAdminReceiver} this request is associated
453 * with.
454 * @param length The new desired minimum number of upper case letters
455 * required in the password. A value of 0 means there is no
456 * restriction.
457 */
458 public void setPasswordMinimumUpperCase(ComponentName admin, int length) {
459 if (mService != null) {
460 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700461 mService.setPasswordMinimumUpperCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700462 } catch (RemoteException e) {
463 Log.w(TAG, "Failed talking with device policy service", e);
464 }
465 }
466 }
467
468 /**
469 * Retrieve the current number of upper case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700470 * password for all admins or a particular one. This is the same value as
471 * set by {#link {@link #setPasswordMinimumUpperCase(ComponentName, int)}
472 * and only applies when the password quality is
473 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700474 *
475 * @param admin The name of the admin component to check, or null to
476 * aggregate all admins.
477 * @return The minimum number of upper case letters required in the
478 * password.
479 */
480 public int getPasswordMinimumUpperCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700481 return getPasswordMinimumUpperCase(admin, UserHandle.myUserId());
482 }
483
484 /** @hide per-user version */
485 public int getPasswordMinimumUpperCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700486 if (mService != null) {
487 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700488 return mService.getPasswordMinimumUpperCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700489 } catch (RemoteException e) {
490 Log.w(TAG, "Failed talking with device policy service", e);
491 }
492 }
493 return 0;
494 }
495
496 /**
497 * Called by an application that is administering the device to set the
498 * minimum number of lower case letters required in the password. After
499 * setting this, the user will not be able to enter a new password that is
500 * not at least as restrictive as what has been set. Note that the current
501 * password will remain until the user has set a new one, so the change does
502 * not take place immediately. To prompt the user for a new password, use
503 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
504 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700505 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
506 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700507 * <p>
508 * The calling device admin must have requested
509 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
510 * this method; if it has not, a security exception will be thrown.
511 *
512 * @param admin Which {@link DeviceAdminReceiver} this request is associated
513 * with.
514 * @param length The new desired minimum number of lower case letters
515 * required in the password. A value of 0 means there is no
516 * restriction.
517 */
518 public void setPasswordMinimumLowerCase(ComponentName admin, int length) {
519 if (mService != null) {
520 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700521 mService.setPasswordMinimumLowerCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700522 } catch (RemoteException e) {
523 Log.w(TAG, "Failed talking with device policy service", e);
524 }
525 }
526 }
527
528 /**
529 * Retrieve the current number of lower case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700530 * password for all admins or a particular one. This is the same value as
531 * set by {#link {@link #setPasswordMinimumLowerCase(ComponentName, int)}
532 * and only applies when the password quality is
533 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700534 *
535 * @param admin The name of the admin component to check, or null to
536 * aggregate all admins.
537 * @return The minimum number of lower case letters required in the
538 * password.
539 */
540 public int getPasswordMinimumLowerCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700541 return getPasswordMinimumLowerCase(admin, UserHandle.myUserId());
542 }
543
544 /** @hide per-user version */
545 public int getPasswordMinimumLowerCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700546 if (mService != null) {
547 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700548 return mService.getPasswordMinimumLowerCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700549 } catch (RemoteException e) {
550 Log.w(TAG, "Failed talking with device policy service", e);
551 }
552 }
553 return 0;
554 }
555
556 /**
557 * Called by an application that is administering the device to set the
558 * minimum number of letters required in the password. After setting this,
559 * the user will not be able to enter a new password that is not at least as
560 * restrictive as what has been set. Note that the current password will
561 * remain until the user has set a new one, so the change does not take
562 * place immediately. To prompt the user for a new password, use
563 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
564 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700565 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
566 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700567 * <p>
568 * The calling device admin must have requested
569 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
570 * this method; if it has not, a security exception will be thrown.
571 *
572 * @param admin Which {@link DeviceAdminReceiver} this request is associated
573 * with.
574 * @param length The new desired minimum number of letters required in the
575 * password. A value of 0 means there is no restriction.
576 */
577 public void setPasswordMinimumLetters(ComponentName admin, int length) {
578 if (mService != null) {
579 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700580 mService.setPasswordMinimumLetters(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700581 } catch (RemoteException e) {
582 Log.w(TAG, "Failed talking with device policy service", e);
583 }
584 }
585 }
586
587 /**
588 * Retrieve the current number of letters required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700589 * admins or a particular one. This is the same value as
590 * set by {#link {@link #setPasswordMinimumLetters(ComponentName, int)}
591 * and only applies when the password quality is
592 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700593 *
594 * @param admin The name of the admin component to check, or null to
595 * aggregate all admins.
596 * @return The minimum number of letters required in the password.
597 */
598 public int getPasswordMinimumLetters(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700599 return getPasswordMinimumLetters(admin, UserHandle.myUserId());
600 }
601
602 /** @hide per-user version */
603 public int getPasswordMinimumLetters(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700604 if (mService != null) {
605 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700606 return mService.getPasswordMinimumLetters(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700607 } catch (RemoteException e) {
608 Log.w(TAG, "Failed talking with device policy service", e);
609 }
610 }
611 return 0;
612 }
613
614 /**
615 * Called by an application that is administering the device to set the
616 * minimum number of numerical digits required in the password. After
617 * setting this, the user will not be able to enter a new password that is
618 * not at least as restrictive as what has been set. Note that the current
619 * password will remain until the user has set a new one, so the change does
620 * not take place immediately. To prompt the user for a new password, use
621 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
622 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700623 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
624 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700625 * <p>
626 * The calling device admin must have requested
627 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
628 * this method; if it has not, a security exception will be thrown.
629 *
630 * @param admin Which {@link DeviceAdminReceiver} this request is associated
631 * with.
632 * @param length The new desired minimum number of numerical digits required
633 * in the password. A value of 0 means there is no restriction.
634 */
635 public void setPasswordMinimumNumeric(ComponentName admin, int length) {
636 if (mService != null) {
637 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700638 mService.setPasswordMinimumNumeric(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700639 } catch (RemoteException e) {
640 Log.w(TAG, "Failed talking with device policy service", e);
641 }
642 }
643 }
644
645 /**
646 * Retrieve the current number of numerical digits required in the password
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700647 * for all admins or a particular one. This is the same value as
648 * set by {#link {@link #setPasswordMinimumNumeric(ComponentName, int)}
649 * and only applies when the password quality is
650 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700651 *
652 * @param admin The name of the admin component to check, or null to
653 * aggregate all admins.
654 * @return The minimum number of numerical digits required in the password.
655 */
656 public int getPasswordMinimumNumeric(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700657 return getPasswordMinimumNumeric(admin, UserHandle.myUserId());
658 }
659
660 /** @hide per-user version */
661 public int getPasswordMinimumNumeric(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700662 if (mService != null) {
663 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700664 return mService.getPasswordMinimumNumeric(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700665 } catch (RemoteException e) {
666 Log.w(TAG, "Failed talking with device policy service", e);
667 }
668 }
669 return 0;
670 }
671
672 /**
673 * Called by an application that is administering the device to set the
674 * minimum number of symbols required in the password. After setting this,
675 * the user will not be able to enter a new password that is not at least as
676 * restrictive as what has been set. Note that the current password will
677 * remain until the user has set a new one, so the change does not take
678 * place immediately. To prompt the user for a new password, use
679 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
680 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700681 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
682 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700683 * <p>
684 * The calling device admin must have requested
685 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
686 * this method; if it has not, a security exception will be thrown.
687 *
688 * @param admin Which {@link DeviceAdminReceiver} this request is associated
689 * with.
690 * @param length The new desired minimum number of symbols required in the
691 * password. A value of 0 means there is no restriction.
692 */
693 public void setPasswordMinimumSymbols(ComponentName admin, int length) {
694 if (mService != null) {
695 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700696 mService.setPasswordMinimumSymbols(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700697 } catch (RemoteException e) {
698 Log.w(TAG, "Failed talking with device policy service", e);
699 }
700 }
701 }
702
703 /**
704 * Retrieve the current number of symbols required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700705 * admins or a particular one. This is the same value as
706 * set by {#link {@link #setPasswordMinimumSymbols(ComponentName, int)}
707 * and only applies when the password quality is
708 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700709 *
710 * @param admin The name of the admin component to check, or null to
711 * aggregate all admins.
712 * @return The minimum number of symbols required in the password.
713 */
714 public int getPasswordMinimumSymbols(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700715 return getPasswordMinimumSymbols(admin, UserHandle.myUserId());
716 }
717
718 /** @hide per-user version */
719 public int getPasswordMinimumSymbols(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700720 if (mService != null) {
721 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700722 return mService.getPasswordMinimumSymbols(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700723 } catch (RemoteException e) {
724 Log.w(TAG, "Failed talking with device policy service", e);
725 }
726 }
727 return 0;
728 }
729
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700730 /**
731 * Called by an application that is administering the device to set the
732 * minimum number of non-letter characters (numerical digits or symbols)
733 * required in the password. After setting this, the user will not be able
734 * to enter a new password that is not at least as restrictive as what has
735 * been set. Note that the current password will remain until the user has
736 * set a new one, so the change does not take place immediately. To prompt
737 * the user for a new password, use {@link #ACTION_SET_NEW_PASSWORD} after
738 * setting this value. This constraint is only imposed if the administrator
739 * has also requested {@link #PASSWORD_QUALITY_COMPLEX} with
740 * {@link #setPasswordQuality}. The default value is 0.
741 * <p>
742 * The calling device admin must have requested
743 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
744 * this method; if it has not, a security exception will be thrown.
745 *
746 * @param admin Which {@link DeviceAdminReceiver} this request is associated
747 * with.
748 * @param length The new desired minimum number of letters required in the
749 * password. A value of 0 means there is no restriction.
750 */
751 public void setPasswordMinimumNonLetter(ComponentName admin, int length) {
752 if (mService != null) {
753 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700754 mService.setPasswordMinimumNonLetter(admin, length, UserHandle.myUserId());
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700755 } catch (RemoteException e) {
756 Log.w(TAG, "Failed talking with device policy service", e);
757 }
758 }
759 }
760
761 /**
762 * Retrieve the current number of non-letter characters required in the
763 * password for all admins or a particular one. This is the same value as
764 * set by {#link {@link #setPasswordMinimumNonLetter(ComponentName, int)}
765 * and only applies when the password quality is
766 * {@link #PASSWORD_QUALITY_COMPLEX}.
767 *
768 * @param admin The name of the admin component to check, or null to
769 * aggregate all admins.
770 * @return The minimum number of letters required in the password.
771 */
772 public int getPasswordMinimumNonLetter(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700773 return getPasswordMinimumNonLetter(admin, UserHandle.myUserId());
774 }
775
776 /** @hide per-user version */
777 public int getPasswordMinimumNonLetter(ComponentName admin, int userHandle) {
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700778 if (mService != null) {
779 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700780 return mService.getPasswordMinimumNonLetter(admin, userHandle);
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700781 } catch (RemoteException e) {
782 Log.w(TAG, "Failed talking with device policy service", e);
783 }
784 }
785 return 0;
786 }
787
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700788 /**
789 * Called by an application that is administering the device to set the length
790 * of the password history. After setting this, the user will not be able to
791 * enter a new password that is the same as any password in the history. Note
792 * that the current password will remain until the user has set a new one, so
793 * the change does not take place immediately. To prompt the user for a new
794 * password, use {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
795 * This constraint is only imposed if the administrator has also requested
796 * either {@link #PASSWORD_QUALITY_NUMERIC},
797 * {@link #PASSWORD_QUALITY_ALPHABETIC}, or
798 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} with {@link #setPasswordQuality}.
799 *
800 * <p>
801 * The calling device admin must have requested
802 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call this
803 * method; if it has not, a security exception will be thrown.
804 *
805 * @param admin Which {@link DeviceAdminReceiver} this request is associated
806 * with.
807 * @param length The new desired length of password history. A value of 0
808 * means there is no restriction.
809 */
810 public void setPasswordHistoryLength(ComponentName admin, int length) {
811 if (mService != null) {
812 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700813 mService.setPasswordHistoryLength(admin, length, UserHandle.myUserId());
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700814 } catch (RemoteException e) {
815 Log.w(TAG, "Failed talking with device policy service", e);
816 }
817 }
818 }
819
820 /**
Jim Millera4e28d12010-11-08 16:15:47 -0800821 * Called by a device admin to set the password expiration timeout. Calling this method
822 * will restart the countdown for password expiration for the given admin, as will changing
823 * the device password (for all admins).
824 *
825 * <p>The provided timeout is the time delta in ms and will be added to the current time.
826 * For example, to have the password expire 5 days from now, timeout would be
827 * 5 * 86400 * 1000 = 432000000 ms for timeout.
828 *
829 * <p>To disable password expiration, a value of 0 may be used for timeout.
830 *
Jim Millera4e28d12010-11-08 16:15:47 -0800831 * <p>The calling device admin must have requested
832 * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to be able to call this
833 * method; if it has not, a security exception will be thrown.
834 *
835 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
836 * @param timeout The limit (in ms) that a password can remain in effect. A value of 0
837 * means there is no restriction (unlimited).
838 */
839 public void setPasswordExpirationTimeout(ComponentName admin, long timeout) {
840 if (mService != null) {
841 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700842 mService.setPasswordExpirationTimeout(admin, timeout, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800843 } catch (RemoteException e) {
844 Log.w(TAG, "Failed talking with device policy service", e);
845 }
846 }
847 }
848
849 /**
Jim Miller6b857682011-02-16 16:27:41 -0800850 * Get the password expiration timeout for the given admin. The expiration timeout is the
851 * recurring expiration timeout provided in the call to
852 * {@link #setPasswordExpirationTimeout(ComponentName, long)} for the given admin or the
853 * aggregate of all policy administrators if admin is null.
Jim Millera4e28d12010-11-08 16:15:47 -0800854 *
855 * @param admin The name of the admin component to check, or null to aggregate all admins.
856 * @return The timeout for the given admin or the minimum of all timeouts
857 */
858 public long getPasswordExpirationTimeout(ComponentName admin) {
859 if (mService != null) {
860 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700861 return mService.getPasswordExpirationTimeout(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800862 } catch (RemoteException e) {
863 Log.w(TAG, "Failed talking with device policy service", e);
864 }
865 }
866 return 0;
867 }
868
869 /**
870 * Get the current password expiration time for the given admin or an aggregate of
Jim Miller6b857682011-02-16 16:27:41 -0800871 * all admins if admin is null. If the password is expired, this will return the time since
872 * the password expired as a negative number. If admin is null, then a composite of all
873 * expiration timeouts is returned - which will be the minimum of all timeouts.
Jim Millera4e28d12010-11-08 16:15:47 -0800874 *
875 * @param admin The name of the admin component to check, or null to aggregate all admins.
876 * @return The password expiration time, in ms.
877 */
878 public long getPasswordExpiration(ComponentName admin) {
879 if (mService != null) {
880 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700881 return mService.getPasswordExpiration(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800882 } catch (RemoteException e) {
883 Log.w(TAG, "Failed talking with device policy service", e);
884 }
885 }
886 return 0;
887 }
888
889 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700890 * Retrieve the current password history length for all admins
891 * or a particular one.
892 * @param admin The name of the admin component to check, or null to aggregate
893 * all admins.
894 * @return The length of the password history
895 */
896 public int getPasswordHistoryLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700897 return getPasswordHistoryLength(admin, UserHandle.myUserId());
898 }
899
900 /** @hide per-user version */
901 public int getPasswordHistoryLength(ComponentName admin, int userHandle) {
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700902 if (mService != null) {
903 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700904 return mService.getPasswordHistoryLength(admin, userHandle);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700905 } catch (RemoteException e) {
906 Log.w(TAG, "Failed talking with device policy service", e);
907 }
908 }
909 return 0;
910 }
911
Dianne Hackbornd6847842010-01-12 18:14:19 -0800912 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800913 * Return the maximum password length that the device supports for a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800914 * particular password quality.
Dianne Hackborn364f6e32010-01-29 17:38:20 -0800915 * @param quality The quality being interrogated.
Dianne Hackborn254cb442010-01-27 19:23:59 -0800916 * @return Returns the maximum length that the user can enter.
917 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800918 public int getPasswordMaximumLength(int quality) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800919 // Kind-of arbitrary.
920 return 16;
921 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700922
Dianne Hackborn254cb442010-01-27 19:23:59 -0800923 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800924 * Determine whether the current password the user has set is sufficient
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800925 * to meet the policy requirements (quality, minimum length) that have been
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800926 * requested.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700927 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800928 * <p>The calling device admin must have requested
929 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
930 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700931 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800932 * @return Returns true if the password meets the current requirements,
933 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800934 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800935 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800936 if (mService != null) {
937 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700938 return mService.isActivePasswordSufficient(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800939 } catch (RemoteException e) {
940 Log.w(TAG, "Failed talking with device policy service", e);
941 }
942 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800943 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800944 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700945
Dianne Hackbornd6847842010-01-12 18:14:19 -0800946 /**
947 * Retrieve the number of times the user has failed at entering a
948 * password since that last successful password entry.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700949 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800950 * <p>The calling device admin must have requested
951 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
952 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800953 */
954 public int getCurrentFailedPasswordAttempts() {
955 if (mService != null) {
956 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700957 return mService.getCurrentFailedPasswordAttempts(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800958 } catch (RemoteException e) {
959 Log.w(TAG, "Failed talking with device policy service", e);
960 }
961 }
962 return -1;
963 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800964
965 /**
Andrew Stadler88209d12010-02-08 22:59:36 -0800966 * Setting this to a value greater than zero enables a built-in policy
967 * that will perform a device wipe after too many incorrect
968 * device-unlock passwords have been entered. This built-in policy combines
969 * watching for failed passwords and wiping the device, and requires
970 * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800971 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700972 *
Andrew Stadler88209d12010-02-08 22:59:36 -0800973 * <p>To implement any other policy (e.g. wiping data for a particular
974 * application only, erasing or revoking credentials, or reporting the
975 * failure to a server), you should implement
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800976 * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
Andrew Stadler88209d12010-02-08 22:59:36 -0800977 * instead. Do not use this API, because if the maximum count is reached,
978 * the device will be wiped immediately, and your callback will not be invoked.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700979 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800980 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800981 * @param num The number of failed password attempts at which point the
982 * device will wipe its data.
983 */
984 public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) {
985 if (mService != null) {
986 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700987 mService.setMaximumFailedPasswordsForWipe(admin, num, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800988 } catch (RemoteException e) {
989 Log.w(TAG, "Failed talking with device policy service", e);
990 }
991 }
992 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700993
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800994 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800995 * Retrieve the current maximum number of login attempts that are allowed
996 * before the device wipes itself, for all admins
997 * or a particular one.
998 * @param admin The name of the admin component to check, or null to aggregate
999 * all admins.
1000 */
1001 public int getMaximumFailedPasswordsForWipe(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001002 return getMaximumFailedPasswordsForWipe(admin, UserHandle.myUserId());
1003 }
1004
1005 /** @hide per-user version */
1006 public int getMaximumFailedPasswordsForWipe(ComponentName admin, int userHandle) {
Dianne Hackborn254cb442010-01-27 19:23:59 -08001007 if (mService != null) {
1008 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001009 return mService.getMaximumFailedPasswordsForWipe(admin, userHandle);
Dianne Hackborn254cb442010-01-27 19:23:59 -08001010 } catch (RemoteException e) {
1011 Log.w(TAG, "Failed talking with device policy service", e);
1012 }
1013 }
1014 return 0;
1015 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001016
Dianne Hackborn254cb442010-01-27 19:23:59 -08001017 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001018 * Flag for {@link #resetPassword}: don't allow other admins to change
1019 * the password again until the user has entered it.
1020 */
1021 public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001022
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001023 /**
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001024 * Force a new device unlock password (the password needed to access the
1025 * entire device, not for individual accounts) on the user. This takes
1026 * effect immediately.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001027 * The given password must be sufficient for the
1028 * current password quality and length constraints as returned by
1029 * {@link #getPasswordQuality(ComponentName)} and
1030 * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet
1031 * these constraints, then it will be rejected and false returned. Note
1032 * that the password may be a stronger quality (containing alphanumeric
1033 * characters when the requested quality is only numeric), in which case
1034 * the currently active quality will be increased to match.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001035 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001036 * <p>The calling device admin must have requested
1037 * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
1038 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001039 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001040 * @param password The new password for the user.
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001041 * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}.
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001042 * @return Returns true if the password was applied, or false if it is
1043 * not acceptable for the current constraints.
1044 */
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001045 public boolean resetPassword(String password, int flags) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001046 if (mService != null) {
1047 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001048 return mService.resetPassword(password, flags, UserHandle.myUserId());
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001049 } catch (RemoteException e) {
1050 Log.w(TAG, "Failed talking with device policy service", e);
1051 }
1052 }
1053 return false;
1054 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001055
Dianne Hackbornd6847842010-01-12 18:14:19 -08001056 /**
1057 * Called by an application that is administering the device to set the
1058 * maximum time for user activity until the device will lock. This limits
1059 * the length that the user can set. It takes effect immediately.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001060 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001061 * <p>The calling device admin must have requested
Dianne Hackborn315ada72010-02-11 12:14:08 -08001062 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001063 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001064 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001065 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001066 * @param timeMs The new desired maximum time to lock in milliseconds.
1067 * A value of 0 means there is no restriction.
1068 */
1069 public void setMaximumTimeToLock(ComponentName admin, long timeMs) {
1070 if (mService != null) {
1071 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001072 mService.setMaximumTimeToLock(admin, timeMs, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001073 } catch (RemoteException e) {
1074 Log.w(TAG, "Failed talking with device policy service", e);
1075 }
1076 }
1077 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001078
Dianne Hackbornd6847842010-01-12 18:14:19 -08001079 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -08001080 * Retrieve the current maximum time to unlock for all admins
1081 * or a particular one.
1082 * @param admin The name of the admin component to check, or null to aggregate
1083 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001084 */
Dianne Hackborn254cb442010-01-27 19:23:59 -08001085 public long getMaximumTimeToLock(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001086 return getMaximumTimeToLock(admin, UserHandle.myUserId());
1087 }
1088
1089 /** @hide per-user version */
1090 public long getMaximumTimeToLock(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001091 if (mService != null) {
1092 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001093 return mService.getMaximumTimeToLock(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001094 } catch (RemoteException e) {
1095 Log.w(TAG, "Failed talking with device policy service", e);
1096 }
1097 }
1098 return 0;
1099 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001100
Dianne Hackbornd6847842010-01-12 18:14:19 -08001101 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001102 * Make the device lock immediately, as if the lock screen timeout has
1103 * expired at the point of this call.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001104 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001105 * <p>The calling device admin must have requested
1106 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
1107 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001108 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001109 public void lockNow() {
1110 if (mService != null) {
1111 try {
1112 mService.lockNow();
1113 } catch (RemoteException e) {
1114 Log.w(TAG, "Failed talking with device policy service", e);
1115 }
1116 }
1117 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001118
Dianne Hackbornd6847842010-01-12 18:14:19 -08001119 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001120 * Flag for {@link #wipeData(int)}: also erase the device's external
1121 * storage.
1122 */
1123 public static final int WIPE_EXTERNAL_STORAGE = 0x0001;
1124
1125 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001126 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001127 * erasing all user data while next booting up. External storage such
Masanori Oginof535cb042012-02-15 19:25:50 +09001128 * as SD cards will be also erased if the flag {@link #WIPE_EXTERNAL_STORAGE}
1129 * is set.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001130 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001131 * <p>The calling device admin must have requested
1132 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
1133 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001134 *
Masanori Oginof535cb042012-02-15 19:25:50 +09001135 * @param flags Bit mask of additional options: currently 0 and
1136 * {@link #WIPE_EXTERNAL_STORAGE} are supported.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001137 */
1138 public void wipeData(int flags) {
1139 if (mService != null) {
1140 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001141 mService.wipeData(flags, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001142 } catch (RemoteException e) {
1143 Log.w(TAG, "Failed talking with device policy service", e);
1144 }
1145 }
1146 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001147
Dianne Hackbornd6847842010-01-12 18:14:19 -08001148 /**
Oscar Montemayor69238c62010-08-03 10:51:06 -07001149 * Called by an application that is administering the device to set the
1150 * global proxy and exclusion list.
1151 * <p>
1152 * The calling device admin must have requested
1153 * {@link DeviceAdminInfo#USES_POLICY_SETS_GLOBAL_PROXY} to be able to call
1154 * this method; if it has not, a security exception will be thrown.
1155 * Only the first device admin can set the proxy. If a second admin attempts
1156 * to set the proxy, the {@link ComponentName} of the admin originally setting the
1157 * proxy will be returned. If successful in setting the proxy, null will
1158 * be returned.
1159 * The method can be called repeatedly by the device admin alrady setting the
1160 * proxy to update the proxy and exclusion list.
1161 *
1162 * @param admin Which {@link DeviceAdminReceiver} this request is associated
1163 * with.
1164 * @param proxySpec the global proxy desired. Must be an HTTP Proxy.
1165 * Pass Proxy.NO_PROXY to reset the proxy.
1166 * @param exclusionList a list of domains to be excluded from the global proxy.
Oscar Montemayor69238c62010-08-03 10:51:06 -07001167 * @return returns null if the proxy was successfully set, or a {@link ComponentName}
1168 * of the device admin that sets thew proxy otherwise.
Andy Stadlerd2672722011-02-16 10:53:33 -08001169 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001170 */
1171 public ComponentName setGlobalProxy(ComponentName admin, Proxy proxySpec,
1172 List<String> exclusionList ) {
1173 if (proxySpec == null) {
1174 throw new NullPointerException();
1175 }
1176 if (mService != null) {
1177 try {
1178 String hostSpec;
1179 String exclSpec;
1180 if (proxySpec.equals(Proxy.NO_PROXY)) {
1181 hostSpec = null;
1182 exclSpec = null;
1183 } else {
1184 if (!proxySpec.type().equals(Proxy.Type.HTTP)) {
1185 throw new IllegalArgumentException();
1186 }
1187 InetSocketAddress sa = (InetSocketAddress)proxySpec.address();
1188 String hostName = sa.getHostName();
1189 int port = sa.getPort();
1190 StringBuilder hostBuilder = new StringBuilder();
1191 hostSpec = hostBuilder.append(hostName)
1192 .append(":").append(Integer.toString(port)).toString();
1193 if (exclusionList == null) {
1194 exclSpec = "";
1195 } else {
1196 StringBuilder listBuilder = new StringBuilder();
1197 boolean firstDomain = true;
1198 for (String exclDomain : exclusionList) {
1199 if (!firstDomain) {
1200 listBuilder = listBuilder.append(",");
1201 } else {
1202 firstDomain = false;
1203 }
1204 listBuilder = listBuilder.append(exclDomain.trim());
1205 }
1206 exclSpec = listBuilder.toString();
1207 }
Yuhao Zheng90704842014-02-28 17:22:45 -08001208 if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
1209 != android.net.Proxy.PROXY_VALID)
1210 throw new IllegalArgumentException();
Oscar Montemayor69238c62010-08-03 10:51:06 -07001211 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001212 return mService.setGlobalProxy(admin, hostSpec, exclSpec, UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001213 } catch (RemoteException e) {
1214 Log.w(TAG, "Failed talking with device policy service", e);
1215 }
1216 }
1217 return null;
1218 }
1219
1220 /**
1221 * Returns the component name setting the global proxy.
1222 * @return ComponentName object of the device admin that set the global proxy, or
1223 * null if no admin has set the proxy.
Andy Stadlerd2672722011-02-16 10:53:33 -08001224 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001225 */
1226 public ComponentName getGlobalProxyAdmin() {
1227 if (mService != null) {
1228 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001229 return mService.getGlobalProxyAdmin(UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001230 } catch (RemoteException e) {
1231 Log.w(TAG, "Failed talking with device policy service", e);
1232 }
1233 }
1234 return null;
1235 }
1236
1237 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001238 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001239 * indicating that encryption is not supported.
1240 */
1241 public static final int ENCRYPTION_STATUS_UNSUPPORTED = 0;
1242
1243 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001244 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001245 * indicating that encryption is supported, but is not currently active.
1246 */
1247 public static final int ENCRYPTION_STATUS_INACTIVE = 1;
1248
1249 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001250 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001251 * indicating that encryption is not currently active, but is currently
1252 * being activated. This is only reported by devices that support
1253 * encryption of data and only when the storage is currently
1254 * undergoing a process of becoming encrypted. A device that must reboot and/or wipe data
1255 * to become encrypted will never return this value.
1256 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001257 public static final int ENCRYPTION_STATUS_ACTIVATING = 2;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001258
1259 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001260 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001261 * indicating that encryption is active.
1262 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001263 public static final int ENCRYPTION_STATUS_ACTIVE = 3;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001264
1265 /**
1266 * Activity action: begin the process of encrypting data on the device. This activity should
1267 * be launched after using {@link #setStorageEncryption} to request encryption be activated.
1268 * After resuming from this activity, use {@link #getStorageEncryption}
1269 * to check encryption status. However, on some devices this activity may never return, as
1270 * it may trigger a reboot and in some cases a complete data wipe of the device.
1271 */
1272 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
1273 public static final String ACTION_START_ENCRYPTION
1274 = "android.app.action.START_ENCRYPTION";
1275
1276 /**
Jim Millerb8ec4702012-08-31 17:19:10 -07001277 * Widgets are enabled in keyguard
1278 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001279 public static final int KEYGUARD_DISABLE_FEATURES_NONE = 0;
Jim Millerb8ec4702012-08-31 17:19:10 -07001280
1281 /**
Jim Miller50e62182014-04-23 17:25:00 -07001282 * Disable all keyguard widgets. Has no effect.
Jim Millerb8ec4702012-08-31 17:19:10 -07001283 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001284 public static final int KEYGUARD_DISABLE_WIDGETS_ALL = 1 << 0;
1285
1286 /**
1287 * Disable the camera on secure keyguard screens (e.g. PIN/Pattern/Password)
1288 */
1289 public static final int KEYGUARD_DISABLE_SECURE_CAMERA = 1 << 1;
1290
1291 /**
Jim Miller50e62182014-04-23 17:25:00 -07001292 * Disable showing all notifications on secure keyguard screens (e.g. PIN/Pattern/Password)
1293 */
1294 public static final int KEYGUARD_DISABLE_SECURE_NOTIFICATIONS = 1 << 2;
1295
1296 /**
1297 * Only allow redacted notifications on secure keyguard screens (e.g. PIN/Pattern/Password)
1298 */
1299 public static final int KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS = 1 << 3;
1300
1301 /**
1302 * Ignore {@link TrustAgentService} state on secure keyguard screens
1303 * (e.g. PIN/Pattern/Password).
1304 */
1305 public static final int KEYGUARD_DISABLE_TRUST_AGENTS = 1 << 4;
1306
1307 /**
Jim Miller35207742012-11-02 15:33:20 -07001308 * Disable all current and future keyguard customizations.
Jim Miller48b9b0d2012-09-19 23:16:50 -07001309 */
1310 public static final int KEYGUARD_DISABLE_FEATURES_ALL = 0x7fffffff;
Jim Millerb8ec4702012-08-31 17:19:10 -07001311
1312 /**
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001313 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001314 * request that the storage system be encrypted.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001315 *
1316 * <p>When multiple device administrators attempt to control device
1317 * encryption, the most secure, supported setting will always be
1318 * used. If any device administrator requests device encryption,
1319 * it will be enabled; Conversely, if a device administrator
1320 * attempts to disable device encryption while another
1321 * device administrator has enabled it, the call to disable will
1322 * fail (most commonly returning {@link #ENCRYPTION_STATUS_ACTIVE}).
1323 *
1324 * <p>This policy controls encryption of the secure (application data) storage area. Data
Andy Stadler50c294f2011-03-07 19:13:42 -08001325 * written to other storage areas may or may not be encrypted, and this policy does not require
1326 * or control the encryption of any other storage areas.
1327 * There is one exception: If {@link android.os.Environment#isExternalStorageEmulated()} is
1328 * {@code true}, then the directory returned by
1329 * {@link android.os.Environment#getExternalStorageDirectory()} must be written to disk
1330 * within the encrypted storage area.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001331 *
1332 * <p>Important Note: On some devices, it is possible to encrypt storage without requiring
1333 * the user to create a device PIN or Password. In this case, the storage is encrypted, but
1334 * the encryption key may not be fully secured. For maximum security, the administrator should
1335 * also require (and check for) a pattern, PIN, or password.
1336 *
1337 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1338 * @param encrypt true to request encryption, false to release any previous request
Andy Stadler22dbfda2011-01-17 12:47:31 -08001339 * @return the new request status (for all active admins) - will be one of
1340 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE}, or
1341 * {@link #ENCRYPTION_STATUS_ACTIVE}. This is the value of the requests; Use
1342 * {@link #getStorageEncryptionStatus()} to query the actual device state.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001343 */
1344 public int setStorageEncryption(ComponentName admin, boolean encrypt) {
1345 if (mService != null) {
1346 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001347 return mService.setStorageEncryption(admin, encrypt, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001348 } catch (RemoteException e) {
1349 Log.w(TAG, "Failed talking with device policy service", e);
1350 }
1351 }
1352 return ENCRYPTION_STATUS_UNSUPPORTED;
1353 }
1354
1355 /**
1356 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001357 * determine the requested setting for secure storage.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001358 *
Andy Stadler22dbfda2011-01-17 12:47:31 -08001359 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. If null,
1360 * this will return the requested encryption setting as an aggregate of all active
1361 * administrators.
1362 * @return true if the admin(s) are requesting encryption, false if not.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001363 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001364 public boolean getStorageEncryption(ComponentName admin) {
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001365 if (mService != null) {
1366 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001367 return mService.getStorageEncryption(admin, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001368 } catch (RemoteException e) {
1369 Log.w(TAG, "Failed talking with device policy service", e);
1370 }
1371 }
Andy Stadler22dbfda2011-01-17 12:47:31 -08001372 return false;
1373 }
1374
1375 /**
1376 * Called by an application that is administering the device to
1377 * determine the current encryption status of the device.
1378 *
1379 * Depending on the returned status code, the caller may proceed in different
1380 * ways. If the result is {@link #ENCRYPTION_STATUS_UNSUPPORTED}, the
1381 * storage system does not support encryption. If the
1382 * result is {@link #ENCRYPTION_STATUS_INACTIVE}, use {@link
1383 * #ACTION_START_ENCRYPTION} to begin the process of encrypting or decrypting the
1384 * storage. If the result is {@link #ENCRYPTION_STATUS_ACTIVATING} or
1385 * {@link #ENCRYPTION_STATUS_ACTIVE}, no further action is required.
1386 *
1387 * @return current status of encryption. The value will be one of
1388 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE},
1389 * {@link #ENCRYPTION_STATUS_ACTIVATING}, or{@link #ENCRYPTION_STATUS_ACTIVE}.
1390 */
1391 public int getStorageEncryptionStatus() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001392 return getStorageEncryptionStatus(UserHandle.myUserId());
1393 }
1394
1395 /** @hide per-user version */
1396 public int getStorageEncryptionStatus(int userHandle) {
Andy Stadler22dbfda2011-01-17 12:47:31 -08001397 if (mService != null) {
1398 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001399 return mService.getStorageEncryptionStatus(userHandle);
Andy Stadler22dbfda2011-01-17 12:47:31 -08001400 } catch (RemoteException e) {
1401 Log.w(TAG, "Failed talking with device policy service", e);
1402 }
1403 }
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001404 return ENCRYPTION_STATUS_UNSUPPORTED;
1405 }
1406
1407 /**
Maggie Benthallda51e682013-08-08 22:35:44 -04001408 * Installs the given certificate as a User CA.
1409 *
1410 * @return false if the certBuffer cannot be parsed or installation is
1411 * interrupted, otherwise true
1412 * @hide
1413 */
1414 public boolean installCaCert(byte[] certBuffer) {
1415 if (mService != null) {
1416 try {
1417 return mService.installCaCert(certBuffer);
1418 } catch (RemoteException e) {
1419 Log.w(TAG, "Failed talking with device policy service", e);
1420 }
1421 }
1422 return false;
1423 }
1424
1425 /**
1426 * Uninstalls the given certificate from the list of User CAs, if present.
1427 *
1428 * @hide
1429 */
1430 public void uninstallCaCert(byte[] certBuffer) {
1431 if (mService != null) {
1432 try {
1433 mService.uninstallCaCert(certBuffer);
1434 } catch (RemoteException e) {
1435 Log.w(TAG, "Failed talking with device policy service", e);
1436 }
1437 }
1438 }
1439
1440 /**
1441 * Returns whether there are any user-installed CA certificates.
1442 *
1443 * @hide
1444 */
Maggie Benthall0469f412013-09-05 15:30:26 -04001445 public static boolean hasAnyCaCertsInstalled() {
Maggie Benthallda51e682013-08-08 22:35:44 -04001446 TrustedCertificateStore certStore = new TrustedCertificateStore();
1447 Set<String> aliases = certStore.userAliases();
1448 return aliases != null && !aliases.isEmpty();
1449 }
1450
1451 /**
1452 * Returns whether this certificate has been installed as a User CA.
1453 *
1454 * @hide
1455 */
1456 public boolean hasCaCertInstalled(byte[] certBuffer) {
1457 TrustedCertificateStore certStore = new TrustedCertificateStore();
1458 String alias;
1459 byte[] pemCert;
1460 try {
1461 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
1462 X509Certificate cert = (X509Certificate) certFactory.generateCertificate(
1463 new ByteArrayInputStream(certBuffer));
1464 return certStore.getCertificateAlias(cert) != null;
1465 } catch (CertificateException ce) {
1466 Log.w(TAG, "Could not parse certificate", ce);
1467 }
1468 return false;
1469 }
1470
1471 /**
Ben Komalo2447edd2011-05-09 16:05:33 -07001472 * Called by an application that is administering the device to disable all cameras
1473 * on the device. After setting this, no applications will be able to access any cameras
1474 * on the device.
1475 *
1476 * <p>The calling device admin must have requested
1477 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_CAMERA} to be able to call
1478 * this method; if it has not, a security exception will be thrown.
1479 *
1480 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1481 * @param disabled Whether or not the camera should be disabled.
1482 */
1483 public void setCameraDisabled(ComponentName admin, boolean disabled) {
1484 if (mService != null) {
1485 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001486 mService.setCameraDisabled(admin, disabled, UserHandle.myUserId());
Ben Komalo2447edd2011-05-09 16:05:33 -07001487 } catch (RemoteException e) {
1488 Log.w(TAG, "Failed talking with device policy service", e);
1489 }
1490 }
1491 }
1492
1493 /**
1494 * Determine whether or not the device's cameras have been disabled either by the current
1495 * admin, if specified, or all admins.
1496 * @param admin The name of the admin component to check, or null to check if any admins
1497 * have disabled the camera
1498 */
1499 public boolean getCameraDisabled(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001500 return getCameraDisabled(admin, UserHandle.myUserId());
1501 }
1502
1503 /** @hide per-user version */
1504 public boolean getCameraDisabled(ComponentName admin, int userHandle) {
Ben Komalo2447edd2011-05-09 16:05:33 -07001505 if (mService != null) {
1506 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001507 return mService.getCameraDisabled(admin, userHandle);
Ben Komalo2447edd2011-05-09 16:05:33 -07001508 } catch (RemoteException e) {
1509 Log.w(TAG, "Failed talking with device policy service", e);
1510 }
1511 }
1512 return false;
1513 }
1514
1515 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001516 * Called by an application that is administering the device to disable keyguard customizations,
1517 * such as widgets. After setting this, keyguard features will be disabled according to the
1518 * provided feature list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001519 *
1520 * <p>The calling device admin must have requested
Jim Miller48b9b0d2012-09-19 23:16:50 -07001521 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_KEYGUARD_FEATURES} to be able to call
Jim Millerb8ec4702012-08-31 17:19:10 -07001522 * this method; if it has not, a security exception will be thrown.
1523 *
1524 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Jim Miller35207742012-11-02 15:33:20 -07001525 * @param which {@link #KEYGUARD_DISABLE_FEATURES_NONE} (default),
1526 * {@link #KEYGUARD_DISABLE_WIDGETS_ALL}, {@link #KEYGUARD_DISABLE_SECURE_CAMERA},
Jim Miller50e62182014-04-23 17:25:00 -07001527 * {@link #KEYGUARD_DISABLE_SECURE_NOTIFICATIONS}, {@link #KEYGUARD_DISABLE_TRUST_AGENTS},
1528 * {@link #KEYGUARD_DISABLE_UNREDACTED_NOTIFICATIONS}, {@link #KEYGUARD_DISABLE_FEATURES_ALL}
Jim Millerb8ec4702012-08-31 17:19:10 -07001529 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001530 public void setKeyguardDisabledFeatures(ComponentName admin, int which) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001531 if (mService != null) {
1532 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001533 mService.setKeyguardDisabledFeatures(admin, which, UserHandle.myUserId());
Jim Millerb8ec4702012-08-31 17:19:10 -07001534 } catch (RemoteException e) {
1535 Log.w(TAG, "Failed talking with device policy service", e);
1536 }
1537 }
1538 }
1539
1540 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001541 * Determine whether or not features have been disabled in keyguard either by the current
Jim Millerb8ec4702012-08-31 17:19:10 -07001542 * admin, if specified, or all admins.
1543 * @param admin The name of the admin component to check, or null to check if any admins
Jim Miller48b9b0d2012-09-19 23:16:50 -07001544 * have disabled features in keyguard.
Jim Miller35207742012-11-02 15:33:20 -07001545 * @return bitfield of flags. See {@link #setKeyguardDisabledFeatures(ComponentName, int)}
1546 * for a list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001547 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001548 public int getKeyguardDisabledFeatures(ComponentName admin) {
1549 return getKeyguardDisabledFeatures(admin, UserHandle.myUserId());
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001550 }
1551
1552 /** @hide per-user version */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001553 public int getKeyguardDisabledFeatures(ComponentName admin, int userHandle) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001554 if (mService != null) {
1555 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001556 return mService.getKeyguardDisabledFeatures(admin, userHandle);
Jim Millerb8ec4702012-08-31 17:19:10 -07001557 } catch (RemoteException e) {
1558 Log.w(TAG, "Failed talking with device policy service", e);
1559 }
1560 }
Jim Miller48b9b0d2012-09-19 23:16:50 -07001561 return KEYGUARD_DISABLE_FEATURES_NONE;
Jim Millerb8ec4702012-08-31 17:19:10 -07001562 }
1563
1564 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001565 * @hide
1566 */
Jessica Hummel6d36b602014-04-04 12:42:17 +01001567 public void setActiveAdmin(ComponentName policyReceiver, boolean refreshing, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001568 if (mService != null) {
1569 try {
Jessica Hummel6d36b602014-04-04 12:42:17 +01001570 mService.setActiveAdmin(policyReceiver, refreshing, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001571 } catch (RemoteException e) {
1572 Log.w(TAG, "Failed talking with device policy service", e);
1573 }
1574 }
1575 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001576
Dianne Hackbornd6847842010-01-12 18:14:19 -08001577 /**
Jessica Hummel6d36b602014-04-04 12:42:17 +01001578 * @hide
1579 */
1580 public void setActiveAdmin(ComponentName policyReceiver, boolean refreshing) {
1581 setActiveAdmin(policyReceiver, refreshing, UserHandle.myUserId());
1582 }
1583
1584 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -08001585 * Returns the DeviceAdminInfo as defined by the administrator's package info & meta-data
Dianne Hackbornd6847842010-01-12 18:14:19 -08001586 * @hide
1587 */
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001588 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001589 ActivityInfo ai;
1590 try {
1591 ai = mContext.getPackageManager().getReceiverInfo(cn,
1592 PackageManager.GET_META_DATA);
1593 } catch (PackageManager.NameNotFoundException e) {
1594 Log.w(TAG, "Unable to retrieve device policy " + cn, e);
1595 return null;
1596 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001597
Dianne Hackbornd6847842010-01-12 18:14:19 -08001598 ResolveInfo ri = new ResolveInfo();
1599 ri.activityInfo = ai;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001600
Dianne Hackbornd6847842010-01-12 18:14:19 -08001601 try {
1602 return new DeviceAdminInfo(mContext, ri);
1603 } catch (XmlPullParserException e) {
1604 Log.w(TAG, "Unable to parse device policy " + cn, e);
1605 return null;
1606 } catch (IOException e) {
1607 Log.w(TAG, "Unable to parse device policy " + cn, e);
1608 return null;
1609 }
1610 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001611
Dianne Hackbornd6847842010-01-12 18:14:19 -08001612 /**
1613 * @hide
1614 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001615 public void getRemoveWarning(ComponentName admin, RemoteCallback result) {
1616 if (mService != null) {
1617 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001618 mService.getRemoveWarning(admin, result, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001619 } catch (RemoteException e) {
1620 Log.w(TAG, "Failed talking with device policy service", e);
1621 }
1622 }
1623 }
1624
1625 /**
1626 * @hide
1627 */
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001628 public void setActivePasswordState(int quality, int length, int letters, int uppercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001629 int lowercase, int numbers, int symbols, int nonletter, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001630 if (mService != null) {
1631 try {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001632 mService.setActivePasswordState(quality, length, letters, uppercase, lowercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001633 numbers, symbols, nonletter, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001634 } catch (RemoteException e) {
1635 Log.w(TAG, "Failed talking with device policy service", e);
1636 }
1637 }
1638 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001639
Dianne Hackbornd6847842010-01-12 18:14:19 -08001640 /**
1641 * @hide
1642 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001643 public void reportFailedPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001644 if (mService != null) {
1645 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001646 mService.reportFailedPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001647 } catch (RemoteException e) {
1648 Log.w(TAG, "Failed talking with device policy service", e);
1649 }
1650 }
1651 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001652
Dianne Hackbornd6847842010-01-12 18:14:19 -08001653 /**
1654 * @hide
1655 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001656 public void reportSuccessfulPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001657 if (mService != null) {
1658 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001659 mService.reportSuccessfulPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001660 } catch (RemoteException e) {
1661 Log.w(TAG, "Failed talking with device policy service", e);
1662 }
1663 }
1664 }
Amith Yamasani71e6c692013-03-24 17:39:28 -07001665
1666 /**
1667 * @hide
1668 * Sets the given package as the device owner. The package must already be installed and there
1669 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1670 * method must be called before the device is provisioned.
1671 * @param packageName the package name of the application to be registered as the device owner.
1672 * @return whether the package was successfully registered as the device owner.
1673 * @throws IllegalArgumentException if the package name is null or invalid
1674 * @throws IllegalStateException if a device owner is already registered or the device has
1675 * already been provisioned.
1676 */
1677 public boolean setDeviceOwner(String packageName) throws IllegalArgumentException,
1678 IllegalStateException {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001679 return setDeviceOwner(packageName, null);
1680 }
1681
1682 /**
1683 * @hide
1684 * Sets the given package as the device owner. The package must already be installed and there
1685 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1686 * method must be called before the device is provisioned.
1687 * @param packageName the package name of the application to be registered as the device owner.
1688 * @param ownerName the human readable name of the institution that owns this device.
1689 * @return whether the package was successfully registered as the device owner.
1690 * @throws IllegalArgumentException if the package name is null or invalid
1691 * @throws IllegalStateException if a device owner is already registered or the device has
1692 * already been provisioned.
1693 */
1694 public boolean setDeviceOwner(String packageName, String ownerName)
1695 throws IllegalArgumentException, IllegalStateException {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001696 if (mService != null) {
1697 try {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001698 return mService.setDeviceOwner(packageName, ownerName);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001699 } catch (RemoteException re) {
1700 Log.w(TAG, "Failed to set device owner");
1701 }
1702 }
1703 return false;
1704 }
1705
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001706
Amith Yamasani71e6c692013-03-24 17:39:28 -07001707 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001708 * Used to determine if a particular package has been registered as a Device Owner app.
1709 * A device owner app is a special device admin that cannot be deactivated by the user, once
1710 * activated as a device admin. It also cannot be uninstalled. To check if a particular
1711 * package is currently registered as the device owner app, pass in the package name from
1712 * {@link Context#getPackageName()} to this method.<p/>This is useful for device
1713 * admin apps that want to check if they are also registered as the device owner app. The
1714 * exact mechanism by which a device admin app is registered as a device owner app is defined by
1715 * the setup process.
1716 * @param packageName the package name of the app, to compare with the registered device owner
1717 * app, if any.
1718 * @return whether or not the package is registered as the device owner app.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001719 */
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001720 public boolean isDeviceOwnerApp(String packageName) {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001721 if (mService != null) {
1722 try {
1723 return mService.isDeviceOwner(packageName);
1724 } catch (RemoteException re) {
1725 Log.w(TAG, "Failed to check device owner");
1726 }
1727 }
1728 return false;
1729 }
1730
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001731 /**
1732 * @hide
1733 * Redirect to isDeviceOwnerApp.
1734 */
1735 public boolean isDeviceOwner(String packageName) {
1736 return isDeviceOwnerApp(packageName);
1737 }
1738
Amith Yamasani71e6c692013-03-24 17:39:28 -07001739 /** @hide */
1740 public String getDeviceOwner() {
1741 if (mService != null) {
1742 try {
1743 return mService.getDeviceOwner();
1744 } catch (RemoteException re) {
1745 Log.w(TAG, "Failed to get device owner");
1746 }
1747 }
1748 return null;
1749 }
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001750
1751 /** @hide */
1752 public String getDeviceOwnerName() {
1753 if (mService != null) {
1754 try {
1755 return mService.getDeviceOwnerName();
1756 } catch (RemoteException re) {
1757 Log.w(TAG, "Failed to get device owner");
1758 }
1759 }
1760 return null;
1761 }
Adam Connors776c5552014-01-09 10:42:56 +00001762
1763 /**
1764 * @hide
1765 * Sets the given package as the profile owner of the given user profile. The package must
1766 * already be installed and there shouldn't be an existing profile owner registered for this
1767 * user. Also, this method must be called before the user has been used for the first time.
1768 * @param packageName the package name of the application to be registered as profile owner.
1769 * @param ownerName the human readable name of the organisation associated with this DPM.
Adam Connors661ec472014-02-11 13:59:46 +00001770 * @param userHandle the userId to set the profile owner for.
Adam Connors776c5552014-01-09 10:42:56 +00001771 * @return whether the package was successfully registered as the profile owner.
1772 * @throws IllegalArgumentException if packageName is null, the package isn't installed, or
1773 * the user has already been set up.
1774 */
Adam Connors661ec472014-02-11 13:59:46 +00001775 public boolean setProfileOwner(String packageName, String ownerName, int userHandle)
Adam Connors776c5552014-01-09 10:42:56 +00001776 throws IllegalArgumentException {
1777 if (mService != null) {
1778 try {
Adam Connors661ec472014-02-11 13:59:46 +00001779 return mService.setProfileOwner(packageName, ownerName, userHandle);
Adam Connors776c5552014-01-09 10:42:56 +00001780 } catch (RemoteException re) {
1781 Log.w(TAG, "Failed to set profile owner", re);
1782 throw new IllegalArgumentException("Couldn't set profile owner.", re);
1783 }
1784 }
1785 return false;
1786 }
1787
1788 /**
Alexandra Gherghina512675b2014-04-02 11:23:54 +01001789 * Sets the enabled state of the profile. A profile should be enabled only once it is ready to
1790 * be used. Only the profile owner can call this.
1791 *
Alexandra Gherghinadf35d572014-04-09 13:54:39 +01001792 * @see #isProfileOwnerApp
Alexandra Gherghina512675b2014-04-02 11:23:54 +01001793 *
1794 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1795 */
1796 public void setProfileEnabled(ComponentName admin) {
1797 if (mService != null) {
1798 try {
1799 mService.setProfileEnabled(admin);
1800 } catch (RemoteException e) {
1801 Log.w(TAG, "Failed talking with device policy service", e);
1802 }
1803 }
1804 }
1805
1806 /**
Adam Connors776c5552014-01-09 10:42:56 +00001807 * Used to determine if a particular package is registered as the Profile Owner for the
Alexandra Gherghina512675b2014-04-02 11:23:54 +01001808 * current user. A profile owner is a special device admin that has additional privileges
Adam Connors776c5552014-01-09 10:42:56 +00001809 * within the managed profile.
1810 *
1811 * @param packageName The package name of the app to compare with the registered profile owner.
1812 * @return Whether or not the package is registered as the profile owner.
1813 */
1814 public boolean isProfileOwnerApp(String packageName) {
1815 if (mService != null) {
1816 try {
1817 String profileOwnerPackage = mService.getProfileOwner(
1818 Process.myUserHandle().getIdentifier());
1819 return profileOwnerPackage != null && profileOwnerPackage.equals(packageName);
1820 } catch (RemoteException re) {
1821 Log.w(TAG, "Failed to check profile owner");
1822 }
1823 }
1824 return false;
1825 }
1826
1827 /**
1828 * @hide
1829 * @return the packageName of the owner of the given user profile or null if no profile
1830 * owner has been set for that user.
1831 * @throws IllegalArgumentException if the userId is invalid.
1832 */
1833 public String getProfileOwner() throws IllegalArgumentException {
1834 if (mService != null) {
1835 try {
1836 return mService.getProfileOwner(Process.myUserHandle().getIdentifier());
1837 } catch (RemoteException re) {
1838 Log.w(TAG, "Failed to get profile owner");
1839 throw new IllegalArgumentException(
1840 "Requested profile owner for invalid userId", re);
1841 }
1842 }
1843 return null;
1844 }
1845
1846 /**
1847 * @hide
1848 * @return the human readable name of the organisation associated with this DPM or null if
1849 * one is not set.
1850 * @throws IllegalArgumentException if the userId is invalid.
1851 */
1852 public String getProfileOwnerName() throws IllegalArgumentException {
1853 if (mService != null) {
1854 try {
1855 return mService.getProfileOwnerName(Process.myUserHandle().getIdentifier());
1856 } catch (RemoteException re) {
1857 Log.w(TAG, "Failed to get profile owner");
1858 throw new IllegalArgumentException(
1859 "Requested profile owner for invalid userId", re);
1860 }
1861 }
1862 return null;
1863 }
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001864
1865 /**
1866 * Called by a profile owner or device owner to add a default intent handler activity for
1867 * intents that match a certain intent filter. This activity will remain the default intent
1868 * handler even if the set of potential event handlers for the intent filter changes and if
1869 * the intent preferences are reset.
1870 *
1871 * <p>The default disambiguation mechanism takes over if the activity is not installed
1872 * (anymore). When the activity is (re)installed, it is automatically reset as default
1873 * intent handler for the filter.
1874 *
1875 * <p>The calling device admin must be a profile owner or device owner. If it is not, a
1876 * security exception will be thrown.
1877 *
1878 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1879 * @param filter The IntentFilter for which a default handler is added.
1880 * @param activity The Activity that is added as default intent handler.
1881 */
1882 public void addPersistentPreferredActivity(ComponentName admin, IntentFilter filter,
1883 ComponentName activity) {
1884 if (mService != null) {
1885 try {
1886 mService.addPersistentPreferredActivity(admin, filter, activity);
1887 } catch (RemoteException e) {
1888 Log.w(TAG, "Failed talking with device policy service", e);
1889 }
1890 }
1891 }
1892
1893 /**
1894 * Called by a profile owner or device owner to remove all persistent intent handler preferences
Torne (Richard Coles)875e2102014-02-24 14:11:56 +00001895 * associated with the given package that were set by {@link #addPersistentPreferredActivity}.
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001896 *
1897 * <p>The calling device admin must be a profile owner. If it is not, a security
1898 * exception will be thrown.
1899 *
1900 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1901 * @param packageName The name of the package for which preferences are removed.
1902 */
1903 public void clearPackagePersistentPreferredActivities(ComponentName admin,
1904 String packageName) {
1905 if (mService != null) {
1906 try {
1907 mService.clearPackagePersistentPreferredActivities(admin, packageName);
1908 } catch (RemoteException e) {
1909 Log.w(TAG, "Failed talking with device policy service", e);
1910 }
1911 }
1912 }
Robin Lee66e5d962014-04-09 16:44:21 +01001913
1914 /**
1915 * Called by a profile or device owner to set the application restrictions for a given target
1916 * application running in the managed profile.
1917 *
1918 * <p>The provided {@link Bundle} consists of key-value pairs, where the types of values may be
1919 * {@link Boolean}, {@link String}, or {@link String}[]. The recommended format for key strings
1920 * is "com.example.packagename/example-setting" to avoid naming conflicts with library
1921 * components such as {@link android.webkit.WebView}.
1922 *
1923 * <p>The application restrictions are only made visible to the target application and the
1924 * profile or device owner.
1925 *
1926 * <p>The calling device admin must be a profile or device owner; if it is not, a security
1927 * exception will be thrown.
1928 *
1929 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1930 * @param packageName The name of the package to update restricted settings for.
1931 * @param settings A {@link Bundle} to be parsed by the receiving application, conveying a new
1932 * set of active restrictions.
1933 */
1934 public void setApplicationRestrictions(ComponentName admin, String packageName,
1935 Bundle settings) {
1936 if (mService != null) {
1937 try {
1938 mService.setApplicationRestrictions(admin, packageName, settings);
1939 } catch (RemoteException e) {
1940 Log.w(TAG, "Failed talking with device policy service", e);
1941 }
1942 }
1943 }
1944
1945 /**
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001946 * Called by a profile owner to forward intents sent from the managed profile to the owner, or
1947 * from the owner to the managed profile.
1948 * If an intent matches this intent filter, then activities belonging to the other user can
1949 * respond to this intent.
1950 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1951 * @param filter if an intent matches this IntentFilter, then it can be forwarded.
1952 */
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001953 public void addForwardingIntentFilter(ComponentName admin, IntentFilter filter, int flags) {
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001954 if (mService != null) {
1955 try {
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001956 mService.addForwardingIntentFilter(admin, filter, flags);
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001957 } catch (RemoteException e) {
1958 Log.w(TAG, "Failed talking with device policy service", e);
1959 }
1960 }
1961 }
1962
1963 /**
Nicolas Prevotc79586e2014-05-06 12:47:57 +01001964 * Called by a profile owner to remove the forwarding intent filters from the current user
Nicolas Prevot10fa67c2014-03-24 13:44:38 +00001965 * and from the owner.
1966 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1967 */
1968 public void clearForwardingIntentFilters(ComponentName admin) {
1969 if (mService != null) {
1970 try {
1971 mService.clearForwardingIntentFilters(admin);
1972 } catch (RemoteException e) {
1973 Log.w(TAG, "Failed talking with device policy service", e);
1974 }
1975 }
1976 }
1977
1978 /**
Robin Lee66e5d962014-04-09 16:44:21 +01001979 * Called by a profile or device owner to get the application restrictions for a given target
1980 * application running in the managed profile.
1981 *
1982 * <p>The calling device admin must be a profile or device owner; if it is not, a security
1983 * exception will be thrown.
1984 *
1985 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1986 * @param packageName The name of the package to fetch restricted settings of.
1987 * @return {@link Bundle} of settings corresponding to what was set last time
1988 * {@link DevicePolicyManager#setApplicationRestrictions} was called, or an empty {@link Bundle}
1989 * if no restrictions have been set.
1990 */
1991 public Bundle getApplicationRestrictions(ComponentName admin, String packageName) {
1992 if (mService != null) {
1993 try {
1994 return mService.getApplicationRestrictions(admin, packageName);
1995 } catch (RemoteException e) {
1996 Log.w(TAG, "Failed talking with device policy service", e);
1997 }
1998 }
1999 return null;
2000 }
Amith Yamasanibe465322014-04-24 13:45:17 -07002001
2002 /**
2003 * Called by a profile or device owner to set a user restriction specified
2004 * by the key.
2005 * <p>
2006 * The calling device admin must be a profile or device owner; if it is not,
2007 * a security exception will be thrown.
Jim Millerdf2258b2014-04-27 20:10:26 -07002008 *
Amith Yamasanibe465322014-04-24 13:45:17 -07002009 * @param admin Which {@link DeviceAdminReceiver} this request is associated
2010 * with.
2011 * @param key The key of the restriction. See the constants in
2012 * {@link android.os.UserManager} for the list of keys.
2013 */
2014 public void addUserRestriction(ComponentName admin, String key) {
2015 if (mService != null) {
2016 try {
2017 mService.setUserRestriction(admin, key, true);
2018 } catch (RemoteException e) {
2019 Log.w(TAG, "Failed talking with device policy service", e);
2020 }
2021 }
2022 }
2023
2024 /**
2025 * Called by a profile or device owner to clear a user restriction specified
2026 * by the key.
2027 * <p>
2028 * The calling device admin must be a profile or device owner; if it is not,
2029 * a security exception will be thrown.
Jim Millerdf2258b2014-04-27 20:10:26 -07002030 *
Amith Yamasanibe465322014-04-24 13:45:17 -07002031 * @param admin Which {@link DeviceAdminReceiver} this request is associated
2032 * with.
2033 * @param key The key of the restriction. See the constants in
2034 * {@link android.os.UserManager} for the list of keys.
2035 */
2036 public void clearUserRestriction(ComponentName admin, String key) {
2037 if (mService != null) {
2038 try {
2039 mService.setUserRestriction(admin, key, false);
2040 } catch (RemoteException e) {
2041 Log.w(TAG, "Failed talking with device policy service", e);
2042 }
2043 }
2044 }
Adam Connors010cfd42014-04-16 12:48:13 +01002045
2046 /**
2047 * Called by profile or device owner to re-enable a system app that was disabled by default
2048 * when the managed profile was created. This should only be called from a profile or device
2049 * owner running within a managed profile.
2050 *
2051 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
2052 * @param packageName The package to be re-enabled in the current profile.
2053 */
2054 public void enableSystemApp(ComponentName admin, String packageName) {
2055 if (mService != null) {
2056 try {
2057 mService.enableSystemApp(admin, packageName);
2058 } catch (RemoteException e) {
2059 Log.w(TAG, "Failed to install package: " + packageName);
2060 }
2061 }
2062 }
2063
2064 /**
Sander Alewijnse5c02db62014-05-07 10:46:57 +01002065 * Called by a profile owner to disable account management for a specific type of account.
2066 *
2067 * <p>The calling device admin must be a profile owner. If it is not, a
2068 * security exception will be thrown.
2069 *
2070 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
2071 * @param accountType For which account management is disabled or enabled.
2072 * @param disabled The boolean indicating that account management will be disabled (true) or
2073 * enabled (false).
2074 */
2075 public void setAccountManagementDisabled(ComponentName admin, String accountType,
2076 boolean disabled) {
2077 if (mService != null) {
2078 try {
2079 mService.setAccountManagementDisabled(admin, accountType, disabled);
2080 } catch (RemoteException e) {
2081 Log.w(TAG, "Failed talking with device policy service", e);
2082 }
2083 }
2084 }
2085
2086 /**
Adam Connors010cfd42014-04-16 12:48:13 +01002087 * Called by profile or device owner to re-enable system apps by intent that were disabled
2088 * by default when the managed profile was created. This should only be called from a profile
2089 * or device owner running within a managed profile.
2090 *
2091 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
2092 * @param intent An intent matching the app(s) to be installed. All apps that resolve for this
2093 * intent will be re-enabled in the current profile.
Amith Yamasani007bbc02014-05-07 10:23:44 -07002094 * @return int The number of activities that matched the intent and were installed.
Adam Connors010cfd42014-04-16 12:48:13 +01002095 */
2096 public int enableSystemApp(ComponentName admin, Intent intent) {
2097 if (mService != null) {
2098 try {
2099 return mService.enableSystemAppWithIntent(admin, intent);
2100 } catch (RemoteException e) {
2101 Log.w(TAG, "Failed to install packages matching filter: " + intent);
2102 }
2103 }
2104 return 0;
2105 }
Sander Alewijnse5c02db62014-05-07 10:46:57 +01002106
2107 /**
2108 * Gets the array of accounts for which account management is disabled by the profile owner.
2109 *
2110 * <p> Account management can be disabled/enabled by calling
2111 * {@link #setAccountManagementDisabled}.
2112 *
2113 * @return a list of account types for which account management has been disabled.
2114 *
2115 * @see #setAccountManagementDisabled
2116 */
2117 public String[] getAccountTypesWithManagementDisabled() {
2118 if (mService != null) {
2119 try {
2120 return mService.getAccountTypesWithManagementDisabled();
2121 } catch (RemoteException e) {
2122 Log.w(TAG, "Failed talking with device policy service", e);
2123 }
2124 }
2125
2126 return null;
2127 }
Dianne Hackbornd6847842010-01-12 18:14:19 -08002128}