blob: d173f19501b1702997042820e323946246fab2fd [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;
Sander Alewijnsef475ca32014-02-17 15:13:58 +000025import android.content.IntentFilter;
Dianne Hackbornd6847842010-01-12 18:14:19 -080026import android.content.pm.ActivityInfo;
27import android.content.pm.PackageManager;
28import android.content.pm.ResolveInfo;
Robin Lee66e5d962014-04-09 16:44:21 +010029import android.os.Bundle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080030import android.os.Handler;
Adam Connors776c5552014-01-09 10:42:56 +000031import android.os.Process;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080032import android.os.RemoteCallback;
Dianne Hackbornd6847842010-01-12 18:14:19 -080033import android.os.RemoteException;
34import android.os.ServiceManager;
Amith Yamasani599dd7c2012-09-14 23:20:08 -070035import android.os.UserHandle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080036import android.util.Log;
37
Maggie Benthallda51e682013-08-08 22:35:44 -040038import com.android.org.conscrypt.TrustedCertificateStore;
39
40import java.io.ByteArrayInputStream;
Dianne Hackbornd6847842010-01-12 18:14:19 -080041import java.io.IOException;
Oscar Montemayor69238c62010-08-03 10:51:06 -070042import java.net.InetSocketAddress;
43import java.net.Proxy;
Maggie Benthallda51e682013-08-08 22:35:44 -040044import java.security.cert.CertificateException;
45import java.security.cert.CertificateFactory;
46import java.security.cert.X509Certificate;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080047import java.util.List;
Maggie Benthallda51e682013-08-08 22:35:44 -040048import java.util.Set;
Dianne Hackbornd6847842010-01-12 18:14:19 -080049
50/**
51 * Public interface for managing policies enforced on a device. Most clients
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080052 * of this class must have published a {@link DeviceAdminReceiver} that the user
Dianne Hackbornd6847842010-01-12 18:14:19 -080053 * has currently enabled.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080054 *
55 * <div class="special reference">
56 * <h3>Developer Guides</h3>
57 * <p>For more information about managing policies for device adminstration, read the
58 * <a href="{@docRoot}guide/topics/admin/device-admin.html">Device Administration</a>
59 * developer guide.</p>
60 * </div>
Dianne Hackbornd6847842010-01-12 18:14:19 -080061 */
62public class DevicePolicyManager {
63 private static String TAG = "DevicePolicyManager";
Dianne Hackbornd6847842010-01-12 18:14:19 -080064
65 private final Context mContext;
Dianne Hackbornd6847842010-01-12 18:14:19 -080066 private final IDevicePolicyManager mService;
Konstantin Lopyrev32558232010-05-20 16:18:05 -070067
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080068 private DevicePolicyManager(Context context, Handler handler) {
Dianne Hackbornd6847842010-01-12 18:14:19 -080069 mContext = context;
Dianne Hackbornd6847842010-01-12 18:14:19 -080070 mService = IDevicePolicyManager.Stub.asInterface(
71 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
72 }
73
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080074 /** @hide */
75 public static DevicePolicyManager create(Context context, Handler handler) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080076 DevicePolicyManager me = new DevicePolicyManager(context, handler);
77 return me.mService != null ? me : null;
78 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -070079
Dianne Hackbornd6847842010-01-12 18:14:19 -080080 /**
Jessica Hummelf72078b2014-03-06 16:13:12 +000081 * Activity action: Starts the provisioning flow which sets up a managed profile.
82 * This intent will typically be sent by a mobile device management application(mdm).
83 * Managed profile provisioning creates a profile, moves the mdm to the profile,
84 * sets the mdm as the profile owner and removes all non required applications from the profile.
85 * As a profile owner the mdm than has full control over the managed profile.
86 *
87 * <p>The intent must contain the extras {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME} and
88 * {@link #EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME}.
89 *
90 * <p> When managed provisioning has completed, an intent of the type
91 * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE} is broadcasted to the
92 * mdm app on the managed profile.
93 *
94 * <p>Input: Nothing.</p>
95 * <p>Output: Nothing</p>
96 */
97 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
98 public static final String ACTION_PROVISION_MANAGED_PROFILE
Jessica Hummel03dd2202014-03-13 16:05:26 +000099 = "android.app.action.ACTION_PROVISION_MANAGED_PROFILE";
Jessica Hummelf72078b2014-03-06 16:13:12 +0000100
101 /**
102 * A String extra holding the name of the package of the mobile device management application
103 * that starts the managed provisioning flow. This package will be set as the profile owner.
104 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}.
105 */
106 public static final String EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME
107 = "deviceAdminPackageName";
108
109 /**
110 * A String extra holding the default name of the profile that is created during managed profile
111 * provisioning.
112 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}
113 */
114 public static final String EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME
115 = "defaultManagedProfileName";
116
117 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800118 * Activity action: ask the user to add a new device administrator to the system.
119 * The desired policy is the ComponentName of the policy in the
120 * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to
121 * bring the user through adding the device administrator to the system (or
122 * allowing them to reject it).
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700123 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800124 * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
125 * field to provide the user with additional explanation (in addition
126 * to your component's description) about what is being added.
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800127 *
128 * <p>If your administrator is already active, this will ordinarily return immediately (without
129 * user intervention). However, if your administrator has been updated and is requesting
130 * additional uses-policy flags, the user will be presented with the new list. New policies
131 * will not be available to the updated administrator until the user has accepted the new list.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800132 */
133 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
134 public static final String ACTION_ADD_DEVICE_ADMIN
135 = "android.app.action.ADD_DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700136
Dianne Hackbornd6847842010-01-12 18:14:19 -0800137 /**
Jim Miller284b62e2010-06-08 14:27:42 -0700138 * Activity action: send when any policy admin changes a policy.
139 * This is generally used to find out when a new policy is in effect.
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700140 *
Jim Miller284b62e2010-06-08 14:27:42 -0700141 * @hide
142 */
143 public static final String ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
144 = "android.app.action.DEVICE_POLICY_MANAGER_STATE_CHANGED";
145
146 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800147 * The ComponentName of the administrator component.
148 *
149 * @see #ACTION_ADD_DEVICE_ADMIN
150 */
151 public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700152
Dianne Hackbornd6847842010-01-12 18:14:19 -0800153 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800154 * An optional CharSequence providing additional explanation for why the
155 * admin is being added.
156 *
157 * @see #ACTION_ADD_DEVICE_ADMIN
158 */
159 public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700160
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800161 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700162 * Activity action: have the user enter a new password. This activity should
163 * be launched after using {@link #setPasswordQuality(ComponentName, int)},
164 * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the user
165 * enter a new password that meets the current requirements. You can use
166 * {@link #isActivePasswordSufficient()} to determine whether you need to
167 * have the user select a new password in order to meet the current
168 * constraints. Upon being resumed from this activity, you can check the new
169 * password characteristics to see if they are sufficient.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800170 */
171 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
172 public static final String ACTION_SET_NEW_PASSWORD
173 = "android.app.action.SET_NEW_PASSWORD";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700174
Dianne Hackbornd6847842010-01-12 18:14:19 -0800175 /**
176 * Return true if the given administrator component is currently
177 * active (enabled) in the system.
178 */
179 public boolean isAdminActive(ComponentName who) {
180 if (mService != null) {
181 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700182 return mService.isAdminActive(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800183 } catch (RemoteException e) {
184 Log.w(TAG, "Failed talking with device policy service", e);
185 }
186 }
187 return false;
188 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700189
Dianne Hackbornd6847842010-01-12 18:14:19 -0800190 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800191 * Return a list of all currently active device administrator's component
192 * names. Note that if there are no administrators than null may be
193 * returned.
194 */
195 public List<ComponentName> getActiveAdmins() {
196 if (mService != null) {
197 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700198 return mService.getActiveAdmins(UserHandle.myUserId());
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800199 } catch (RemoteException e) {
200 Log.w(TAG, "Failed talking with device policy service", e);
201 }
202 }
203 return null;
204 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700205
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800206 /**
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700207 * Used by package administration code to determine if a package can be stopped
208 * or uninstalled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800209 * @hide
210 */
211 public boolean packageHasActiveAdmins(String packageName) {
212 if (mService != null) {
213 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700214 return mService.packageHasActiveAdmins(packageName, UserHandle.myUserId());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800215 } catch (RemoteException e) {
216 Log.w(TAG, "Failed talking with device policy service", e);
217 }
218 }
219 return false;
220 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700221
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800222 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800223 * Remove a current administration component. This can only be called
224 * by the application that owns the administration component; if you
225 * try to remove someone else's component, a security exception will be
226 * thrown.
227 */
228 public void removeActiveAdmin(ComponentName who) {
229 if (mService != null) {
230 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700231 mService.removeActiveAdmin(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800232 } catch (RemoteException e) {
233 Log.w(TAG, "Failed talking with device policy service", e);
234 }
235 }
236 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700237
Dianne Hackbornd6847842010-01-12 18:14:19 -0800238 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800239 * Returns true if an administrator has been granted a particular device policy. This can
240 * be used to check if the administrator was activated under an earlier set of policies,
241 * but requires additional policies after an upgrade.
242 *
243 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. Must be
244 * an active administrator, or an exception will be thrown.
245 * @param usesPolicy Which uses-policy to check, as defined in {@link DeviceAdminInfo}.
246 */
247 public boolean hasGrantedPolicy(ComponentName admin, int usesPolicy) {
248 if (mService != null) {
249 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700250 return mService.hasGrantedPolicy(admin, usesPolicy, UserHandle.myUserId());
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800251 } catch (RemoteException e) {
252 Log.w(TAG, "Failed talking with device policy service", e);
253 }
254 }
255 return false;
256 }
257
258 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800259 * Constant for {@link #setPasswordQuality}: the policy has no requirements
260 * for the password. Note that quality constants are ordered so that higher
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800261 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800262 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800263 public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700264
Dianne Hackbornd6847842010-01-12 18:14:19 -0800265 /**
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700266 * Constant for {@link #setPasswordQuality}: the policy allows for low-security biometric
267 * recognition technology. This implies technologies that can recognize the identity of
268 * an individual to about a 3 digit PIN (false detection is less than 1 in 1,000).
269 * Note that quality constants are ordered so that higher values are more restrictive.
270 */
271 public static final int PASSWORD_QUALITY_BIOMETRIC_WEAK = 0x8000;
272
273 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800274 * Constant for {@link #setPasswordQuality}: the policy requires some kind
275 * of password, but doesn't care what it is. Note that quality constants
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800276 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800277 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800278 public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700279
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800280 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800281 * Constant for {@link #setPasswordQuality}: the user must have entered a
282 * password containing at least numeric characters. Note that quality
283 * constants are ordered so that higher values are more restrictive.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800284 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800285 public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700286
Dianne Hackbornd6847842010-01-12 18:14:19 -0800287 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800288 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700289 * password containing at least alphabetic (or other symbol) characters.
290 * Note that quality constants are ordered so that higher values are more
291 * restrictive.
292 */
293 public static final int PASSWORD_QUALITY_ALPHABETIC = 0x40000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700294
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700295 /**
296 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800297 * password containing at least <em>both></em> numeric <em>and</em>
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700298 * alphabetic (or other symbol) characters. Note that quality constants are
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800299 * ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800300 */
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700301 public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x50000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700302
Dianne Hackbornd6847842010-01-12 18:14:19 -0800303 /**
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700304 * Constant for {@link #setPasswordQuality}: the user must have entered a
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700305 * password containing at least a letter, a numerical digit and a special
306 * symbol, by default. With this password quality, passwords can be
307 * restricted to contain various sets of characters, like at least an
308 * uppercase letter, etc. These are specified using various methods,
309 * like {@link #setPasswordMinimumLowerCase(ComponentName, int)}. Note
310 * that quality constants are ordered so that higher values are more
311 * restrictive.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700312 */
313 public static final int PASSWORD_QUALITY_COMPLEX = 0x60000;
314
315 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800316 * Called by an application that is administering the device to set the
317 * password restrictions it is imposing. After setting this, the user
318 * will not be able to enter a new password that is not at least as
319 * restrictive as what has been set. Note that the current password
320 * will remain until the user has set a new one, so the change does not
321 * take place immediately. To prompt the user for a new password, use
322 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700323 *
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800324 * <p>Quality constants are ordered so that higher values are more restrictive;
325 * thus the highest requested quality constant (between the policy set here,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800326 * the user's preference, and any other considerations) is the one that
327 * is in effect.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700328 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800329 * <p>The calling device admin must have requested
330 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
331 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700332 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800333 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800334 * @param quality The new desired quality. One of
335 * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING},
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700336 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC},
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700337 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} or {@link #PASSWORD_QUALITY_COMPLEX}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800338 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800339 public void setPasswordQuality(ComponentName admin, int quality) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800340 if (mService != null) {
341 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700342 mService.setPasswordQuality(admin, quality, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800343 } catch (RemoteException e) {
344 Log.w(TAG, "Failed talking with device policy service", e);
345 }
346 }
347 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700348
Dianne Hackbornd6847842010-01-12 18:14:19 -0800349 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800350 * Retrieve the current minimum password quality for all admins
Dianne Hackborn254cb442010-01-27 19:23:59 -0800351 * or a particular one.
352 * @param admin The name of the admin component to check, or null to aggregate
353 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800354 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800355 public int getPasswordQuality(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700356 return getPasswordQuality(admin, UserHandle.myUserId());
357 }
358
359 /** @hide per-user version */
360 public int getPasswordQuality(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800361 if (mService != null) {
362 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700363 return mService.getPasswordQuality(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800364 } catch (RemoteException e) {
365 Log.w(TAG, "Failed talking with device policy service", e);
366 }
367 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800368 return PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800369 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700370
Dianne Hackbornd6847842010-01-12 18:14:19 -0800371 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800372 * Called by an application that is administering the device to set the
373 * minimum allowed password length. After setting this, the user
374 * will not be able to enter a new password that is not at least as
375 * restrictive as what has been set. Note that the current password
376 * will remain until the user has set a new one, so the change does not
377 * take place immediately. To prompt the user for a new password, use
378 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
379 * constraint is only imposed if the administrator has also requested either
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700380 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC}
381 * {@link #PASSWORD_QUALITY_ALPHANUMERIC}, or {@link #PASSWORD_QUALITY_COMPLEX}
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800382 * with {@link #setPasswordQuality}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700383 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800384 * <p>The calling device admin must have requested
385 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
386 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700387 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800388 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800389 * @param length The new desired minimum password length. A value of 0
390 * means there is no restriction.
391 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800392 public void setPasswordMinimumLength(ComponentName admin, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800393 if (mService != null) {
394 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700395 mService.setPasswordMinimumLength(admin, length, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800396 } catch (RemoteException e) {
397 Log.w(TAG, "Failed talking with device policy service", e);
398 }
399 }
400 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700401
Dianne Hackbornd6847842010-01-12 18:14:19 -0800402 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800403 * Retrieve the current minimum password length for all admins
404 * or a particular one.
405 * @param admin The name of the admin component to check, or null to aggregate
406 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800407 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800408 public int getPasswordMinimumLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700409 return getPasswordMinimumLength(admin, UserHandle.myUserId());
410 }
411
412 /** @hide per-user version */
413 public int getPasswordMinimumLength(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800414 if (mService != null) {
415 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700416 return mService.getPasswordMinimumLength(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800417 } catch (RemoteException e) {
418 Log.w(TAG, "Failed talking with device policy service", e);
419 }
420 }
421 return 0;
422 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700423
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700424 /**
425 * Called by an application that is administering the device to set the
426 * minimum number of upper case letters required in the password. After
427 * setting this, the user will not be able to enter a new password that is
428 * not at least as restrictive as what has been set. Note that the current
429 * password will remain until the user has set a new one, so the change does
430 * not take place immediately. To prompt the user for a new password, use
431 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
432 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700433 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
434 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700435 * <p>
436 * The calling device admin must have requested
437 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
438 * this method; if it has not, a security exception will be thrown.
439 *
440 * @param admin Which {@link DeviceAdminReceiver} this request is associated
441 * with.
442 * @param length The new desired minimum number of upper case letters
443 * required in the password. A value of 0 means there is no
444 * restriction.
445 */
446 public void setPasswordMinimumUpperCase(ComponentName admin, int length) {
447 if (mService != null) {
448 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700449 mService.setPasswordMinimumUpperCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700450 } catch (RemoteException e) {
451 Log.w(TAG, "Failed talking with device policy service", e);
452 }
453 }
454 }
455
456 /**
457 * Retrieve the current number of upper case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700458 * password for all admins or a particular one. This is the same value as
459 * set by {#link {@link #setPasswordMinimumUpperCase(ComponentName, int)}
460 * and only applies when the password quality is
461 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700462 *
463 * @param admin The name of the admin component to check, or null to
464 * aggregate all admins.
465 * @return The minimum number of upper case letters required in the
466 * password.
467 */
468 public int getPasswordMinimumUpperCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700469 return getPasswordMinimumUpperCase(admin, UserHandle.myUserId());
470 }
471
472 /** @hide per-user version */
473 public int getPasswordMinimumUpperCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700474 if (mService != null) {
475 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700476 return mService.getPasswordMinimumUpperCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700477 } catch (RemoteException e) {
478 Log.w(TAG, "Failed talking with device policy service", e);
479 }
480 }
481 return 0;
482 }
483
484 /**
485 * Called by an application that is administering the device to set the
486 * minimum number of lower case letters required in the password. After
487 * setting this, the user will not be able to enter a new password that is
488 * not at least as restrictive as what has been set. Note that the current
489 * password will remain until the user has set a new one, so the change does
490 * not take place immediately. To prompt the user for a new password, use
491 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
492 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700493 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
494 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700495 * <p>
496 * The calling device admin must have requested
497 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
498 * this method; if it has not, a security exception will be thrown.
499 *
500 * @param admin Which {@link DeviceAdminReceiver} this request is associated
501 * with.
502 * @param length The new desired minimum number of lower case letters
503 * required in the password. A value of 0 means there is no
504 * restriction.
505 */
506 public void setPasswordMinimumLowerCase(ComponentName admin, int length) {
507 if (mService != null) {
508 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700509 mService.setPasswordMinimumLowerCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700510 } catch (RemoteException e) {
511 Log.w(TAG, "Failed talking with device policy service", e);
512 }
513 }
514 }
515
516 /**
517 * Retrieve the current number of lower case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700518 * password for all admins or a particular one. This is the same value as
519 * set by {#link {@link #setPasswordMinimumLowerCase(ComponentName, int)}
520 * and only applies when the password quality is
521 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700522 *
523 * @param admin The name of the admin component to check, or null to
524 * aggregate all admins.
525 * @return The minimum number of lower case letters required in the
526 * password.
527 */
528 public int getPasswordMinimumLowerCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700529 return getPasswordMinimumLowerCase(admin, UserHandle.myUserId());
530 }
531
532 /** @hide per-user version */
533 public int getPasswordMinimumLowerCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700534 if (mService != null) {
535 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700536 return mService.getPasswordMinimumLowerCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700537 } catch (RemoteException e) {
538 Log.w(TAG, "Failed talking with device policy service", e);
539 }
540 }
541 return 0;
542 }
543
544 /**
545 * Called by an application that is administering the device to set the
546 * minimum number of letters required in the password. After setting this,
547 * the user will not be able to enter a new password that is not at least as
548 * restrictive as what has been set. Note that the current password will
549 * remain until the user has set a new one, so the change does not take
550 * place immediately. To prompt the user for a new password, use
551 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
552 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700553 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
554 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700555 * <p>
556 * The calling device admin must have requested
557 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
558 * this method; if it has not, a security exception will be thrown.
559 *
560 * @param admin Which {@link DeviceAdminReceiver} this request is associated
561 * with.
562 * @param length The new desired minimum number of letters required in the
563 * password. A value of 0 means there is no restriction.
564 */
565 public void setPasswordMinimumLetters(ComponentName admin, int length) {
566 if (mService != null) {
567 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700568 mService.setPasswordMinimumLetters(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700569 } catch (RemoteException e) {
570 Log.w(TAG, "Failed talking with device policy service", e);
571 }
572 }
573 }
574
575 /**
576 * Retrieve the current number of letters required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700577 * admins or a particular one. This is the same value as
578 * set by {#link {@link #setPasswordMinimumLetters(ComponentName, int)}
579 * and only applies when the password quality is
580 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700581 *
582 * @param admin The name of the admin component to check, or null to
583 * aggregate all admins.
584 * @return The minimum number of letters required in the password.
585 */
586 public int getPasswordMinimumLetters(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700587 return getPasswordMinimumLetters(admin, UserHandle.myUserId());
588 }
589
590 /** @hide per-user version */
591 public int getPasswordMinimumLetters(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700592 if (mService != null) {
593 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700594 return mService.getPasswordMinimumLetters(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700595 } catch (RemoteException e) {
596 Log.w(TAG, "Failed talking with device policy service", e);
597 }
598 }
599 return 0;
600 }
601
602 /**
603 * Called by an application that is administering the device to set the
604 * minimum number of numerical digits required in the password. After
605 * setting this, the user will not be able to enter a new password that is
606 * not at least as restrictive as what has been set. Note that the current
607 * password will remain until the user has set a new one, so the change does
608 * not take place immediately. To prompt the user for a new password, use
609 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
610 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700611 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
612 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700613 * <p>
614 * The calling device admin must have requested
615 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
616 * this method; if it has not, a security exception will be thrown.
617 *
618 * @param admin Which {@link DeviceAdminReceiver} this request is associated
619 * with.
620 * @param length The new desired minimum number of numerical digits required
621 * in the password. A value of 0 means there is no restriction.
622 */
623 public void setPasswordMinimumNumeric(ComponentName admin, int length) {
624 if (mService != null) {
625 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700626 mService.setPasswordMinimumNumeric(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700627 } catch (RemoteException e) {
628 Log.w(TAG, "Failed talking with device policy service", e);
629 }
630 }
631 }
632
633 /**
634 * Retrieve the current number of numerical digits required in the password
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700635 * for all admins or a particular one. This is the same value as
636 * set by {#link {@link #setPasswordMinimumNumeric(ComponentName, int)}
637 * and only applies when the password quality is
638 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700639 *
640 * @param admin The name of the admin component to check, or null to
641 * aggregate all admins.
642 * @return The minimum number of numerical digits required in the password.
643 */
644 public int getPasswordMinimumNumeric(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700645 return getPasswordMinimumNumeric(admin, UserHandle.myUserId());
646 }
647
648 /** @hide per-user version */
649 public int getPasswordMinimumNumeric(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700650 if (mService != null) {
651 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700652 return mService.getPasswordMinimumNumeric(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700653 } catch (RemoteException e) {
654 Log.w(TAG, "Failed talking with device policy service", e);
655 }
656 }
657 return 0;
658 }
659
660 /**
661 * Called by an application that is administering the device to set the
662 * minimum number of symbols required in the password. After setting this,
663 * the user will not be able to enter a new password that is not at least as
664 * restrictive as what has been set. Note that the current password will
665 * remain until the user has set a new one, so the change does not take
666 * place immediately. To prompt the user for a new password, use
667 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
668 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700669 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
670 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700671 * <p>
672 * The calling device admin must have requested
673 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
674 * this method; if it has not, a security exception will be thrown.
675 *
676 * @param admin Which {@link DeviceAdminReceiver} this request is associated
677 * with.
678 * @param length The new desired minimum number of symbols required in the
679 * password. A value of 0 means there is no restriction.
680 */
681 public void setPasswordMinimumSymbols(ComponentName admin, int length) {
682 if (mService != null) {
683 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700684 mService.setPasswordMinimumSymbols(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700685 } catch (RemoteException e) {
686 Log.w(TAG, "Failed talking with device policy service", e);
687 }
688 }
689 }
690
691 /**
692 * Retrieve the current number of symbols required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700693 * admins or a particular one. This is the same value as
694 * set by {#link {@link #setPasswordMinimumSymbols(ComponentName, int)}
695 * and only applies when the password quality is
696 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700697 *
698 * @param admin The name of the admin component to check, or null to
699 * aggregate all admins.
700 * @return The minimum number of symbols required in the password.
701 */
702 public int getPasswordMinimumSymbols(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700703 return getPasswordMinimumSymbols(admin, UserHandle.myUserId());
704 }
705
706 /** @hide per-user version */
707 public int getPasswordMinimumSymbols(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700708 if (mService != null) {
709 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700710 return mService.getPasswordMinimumSymbols(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700711 } catch (RemoteException e) {
712 Log.w(TAG, "Failed talking with device policy service", e);
713 }
714 }
715 return 0;
716 }
717
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700718 /**
719 * Called by an application that is administering the device to set the
720 * minimum number of non-letter characters (numerical digits or symbols)
721 * required in the password. After setting this, the user will not be able
722 * to enter a new password that is not at least as restrictive as what has
723 * been set. Note that the current password will remain until the user has
724 * set a new one, so the change does not take place immediately. To prompt
725 * the user for a new password, use {@link #ACTION_SET_NEW_PASSWORD} after
726 * setting this value. This constraint is only imposed if the administrator
727 * has also requested {@link #PASSWORD_QUALITY_COMPLEX} with
728 * {@link #setPasswordQuality}. The default value is 0.
729 * <p>
730 * The calling device admin must have requested
731 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
732 * this method; if it has not, a security exception will be thrown.
733 *
734 * @param admin Which {@link DeviceAdminReceiver} this request is associated
735 * with.
736 * @param length The new desired minimum number of letters required in the
737 * password. A value of 0 means there is no restriction.
738 */
739 public void setPasswordMinimumNonLetter(ComponentName admin, int length) {
740 if (mService != null) {
741 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700742 mService.setPasswordMinimumNonLetter(admin, length, UserHandle.myUserId());
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700743 } catch (RemoteException e) {
744 Log.w(TAG, "Failed talking with device policy service", e);
745 }
746 }
747 }
748
749 /**
750 * Retrieve the current number of non-letter characters required in the
751 * password for all admins or a particular one. This is the same value as
752 * set by {#link {@link #setPasswordMinimumNonLetter(ComponentName, int)}
753 * and only applies when the password quality is
754 * {@link #PASSWORD_QUALITY_COMPLEX}.
755 *
756 * @param admin The name of the admin component to check, or null to
757 * aggregate all admins.
758 * @return The minimum number of letters required in the password.
759 */
760 public int getPasswordMinimumNonLetter(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700761 return getPasswordMinimumNonLetter(admin, UserHandle.myUserId());
762 }
763
764 /** @hide per-user version */
765 public int getPasswordMinimumNonLetter(ComponentName admin, int userHandle) {
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700766 if (mService != null) {
767 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700768 return mService.getPasswordMinimumNonLetter(admin, userHandle);
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700769 } catch (RemoteException e) {
770 Log.w(TAG, "Failed talking with device policy service", e);
771 }
772 }
773 return 0;
774 }
775
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700776 /**
777 * Called by an application that is administering the device to set the length
778 * of the password history. After setting this, the user will not be able to
779 * enter a new password that is the same as any password in the history. Note
780 * that the current password will remain until the user has set a new one, so
781 * the change does not take place immediately. To prompt the user for a new
782 * password, use {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
783 * This constraint is only imposed if the administrator has also requested
784 * either {@link #PASSWORD_QUALITY_NUMERIC},
785 * {@link #PASSWORD_QUALITY_ALPHABETIC}, or
786 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} with {@link #setPasswordQuality}.
787 *
788 * <p>
789 * The calling device admin must have requested
790 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call this
791 * method; if it has not, a security exception will be thrown.
792 *
793 * @param admin Which {@link DeviceAdminReceiver} this request is associated
794 * with.
795 * @param length The new desired length of password history. A value of 0
796 * means there is no restriction.
797 */
798 public void setPasswordHistoryLength(ComponentName admin, int length) {
799 if (mService != null) {
800 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700801 mService.setPasswordHistoryLength(admin, length, UserHandle.myUserId());
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700802 } catch (RemoteException e) {
803 Log.w(TAG, "Failed talking with device policy service", e);
804 }
805 }
806 }
807
808 /**
Jim Millera4e28d12010-11-08 16:15:47 -0800809 * Called by a device admin to set the password expiration timeout. Calling this method
810 * will restart the countdown for password expiration for the given admin, as will changing
811 * the device password (for all admins).
812 *
813 * <p>The provided timeout is the time delta in ms and will be added to the current time.
814 * For example, to have the password expire 5 days from now, timeout would be
815 * 5 * 86400 * 1000 = 432000000 ms for timeout.
816 *
817 * <p>To disable password expiration, a value of 0 may be used for timeout.
818 *
Jim Millera4e28d12010-11-08 16:15:47 -0800819 * <p>The calling device admin must have requested
820 * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to be able to call this
821 * method; if it has not, a security exception will be thrown.
822 *
823 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
824 * @param timeout The limit (in ms) that a password can remain in effect. A value of 0
825 * means there is no restriction (unlimited).
826 */
827 public void setPasswordExpirationTimeout(ComponentName admin, long timeout) {
828 if (mService != null) {
829 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700830 mService.setPasswordExpirationTimeout(admin, timeout, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800831 } catch (RemoteException e) {
832 Log.w(TAG, "Failed talking with device policy service", e);
833 }
834 }
835 }
836
837 /**
Jim Miller6b857682011-02-16 16:27:41 -0800838 * Get the password expiration timeout for the given admin. The expiration timeout is the
839 * recurring expiration timeout provided in the call to
840 * {@link #setPasswordExpirationTimeout(ComponentName, long)} for the given admin or the
841 * aggregate of all policy administrators if admin is null.
Jim Millera4e28d12010-11-08 16:15:47 -0800842 *
843 * @param admin The name of the admin component to check, or null to aggregate all admins.
844 * @return The timeout for the given admin or the minimum of all timeouts
845 */
846 public long getPasswordExpirationTimeout(ComponentName admin) {
847 if (mService != null) {
848 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700849 return mService.getPasswordExpirationTimeout(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800850 } catch (RemoteException e) {
851 Log.w(TAG, "Failed talking with device policy service", e);
852 }
853 }
854 return 0;
855 }
856
857 /**
858 * Get the current password expiration time for the given admin or an aggregate of
Jim Miller6b857682011-02-16 16:27:41 -0800859 * all admins if admin is null. If the password is expired, this will return the time since
860 * the password expired as a negative number. If admin is null, then a composite of all
861 * expiration timeouts is returned - which will be the minimum of all timeouts.
Jim Millera4e28d12010-11-08 16:15:47 -0800862 *
863 * @param admin The name of the admin component to check, or null to aggregate all admins.
864 * @return The password expiration time, in ms.
865 */
866 public long getPasswordExpiration(ComponentName admin) {
867 if (mService != null) {
868 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700869 return mService.getPasswordExpiration(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800870 } catch (RemoteException e) {
871 Log.w(TAG, "Failed talking with device policy service", e);
872 }
873 }
874 return 0;
875 }
876
877 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700878 * Retrieve the current password history length for all admins
879 * or a particular one.
880 * @param admin The name of the admin component to check, or null to aggregate
881 * all admins.
882 * @return The length of the password history
883 */
884 public int getPasswordHistoryLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700885 return getPasswordHistoryLength(admin, UserHandle.myUserId());
886 }
887
888 /** @hide per-user version */
889 public int getPasswordHistoryLength(ComponentName admin, int userHandle) {
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700890 if (mService != null) {
891 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700892 return mService.getPasswordHistoryLength(admin, userHandle);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700893 } catch (RemoteException e) {
894 Log.w(TAG, "Failed talking with device policy service", e);
895 }
896 }
897 return 0;
898 }
899
Dianne Hackbornd6847842010-01-12 18:14:19 -0800900 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800901 * Return the maximum password length that the device supports for a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800902 * particular password quality.
Dianne Hackborn364f6e32010-01-29 17:38:20 -0800903 * @param quality The quality being interrogated.
Dianne Hackborn254cb442010-01-27 19:23:59 -0800904 * @return Returns the maximum length that the user can enter.
905 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800906 public int getPasswordMaximumLength(int quality) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800907 // Kind-of arbitrary.
908 return 16;
909 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700910
Dianne Hackborn254cb442010-01-27 19:23:59 -0800911 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800912 * Determine whether the current password the user has set is sufficient
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800913 * to meet the policy requirements (quality, minimum length) that have been
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800914 * requested.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700915 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800916 * <p>The calling device admin must have requested
917 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
918 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700919 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800920 * @return Returns true if the password meets the current requirements,
921 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800922 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800923 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800924 if (mService != null) {
925 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700926 return mService.isActivePasswordSufficient(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800927 } catch (RemoteException e) {
928 Log.w(TAG, "Failed talking with device policy service", e);
929 }
930 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800931 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800932 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700933
Dianne Hackbornd6847842010-01-12 18:14:19 -0800934 /**
935 * Retrieve the number of times the user has failed at entering a
936 * password since that last successful password entry.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700937 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800938 * <p>The calling device admin must have requested
939 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
940 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800941 */
942 public int getCurrentFailedPasswordAttempts() {
943 if (mService != null) {
944 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700945 return mService.getCurrentFailedPasswordAttempts(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800946 } catch (RemoteException e) {
947 Log.w(TAG, "Failed talking with device policy service", e);
948 }
949 }
950 return -1;
951 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800952
953 /**
Andrew Stadler88209d12010-02-08 22:59:36 -0800954 * Setting this to a value greater than zero enables a built-in policy
955 * that will perform a device wipe after too many incorrect
956 * device-unlock passwords have been entered. This built-in policy combines
957 * watching for failed passwords and wiping the device, and requires
958 * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800959 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700960 *
Andrew Stadler88209d12010-02-08 22:59:36 -0800961 * <p>To implement any other policy (e.g. wiping data for a particular
962 * application only, erasing or revoking credentials, or reporting the
963 * failure to a server), you should implement
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800964 * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
Andrew Stadler88209d12010-02-08 22:59:36 -0800965 * instead. Do not use this API, because if the maximum count is reached,
966 * the device will be wiped immediately, and your callback will not be invoked.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700967 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800968 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800969 * @param num The number of failed password attempts at which point the
970 * device will wipe its data.
971 */
972 public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) {
973 if (mService != null) {
974 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700975 mService.setMaximumFailedPasswordsForWipe(admin, num, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800976 } catch (RemoteException e) {
977 Log.w(TAG, "Failed talking with device policy service", e);
978 }
979 }
980 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700981
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800982 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800983 * Retrieve the current maximum number of login attempts that are allowed
984 * before the device wipes itself, for all admins
985 * or a particular one.
986 * @param admin The name of the admin component to check, or null to aggregate
987 * all admins.
988 */
989 public int getMaximumFailedPasswordsForWipe(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700990 return getMaximumFailedPasswordsForWipe(admin, UserHandle.myUserId());
991 }
992
993 /** @hide per-user version */
994 public int getMaximumFailedPasswordsForWipe(ComponentName admin, int userHandle) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800995 if (mService != null) {
996 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700997 return mService.getMaximumFailedPasswordsForWipe(admin, userHandle);
Dianne Hackborn254cb442010-01-27 19:23:59 -0800998 } catch (RemoteException e) {
999 Log.w(TAG, "Failed talking with device policy service", e);
1000 }
1001 }
1002 return 0;
1003 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001004
Dianne Hackborn254cb442010-01-27 19:23:59 -08001005 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001006 * Flag for {@link #resetPassword}: don't allow other admins to change
1007 * the password again until the user has entered it.
1008 */
1009 public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001010
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001011 /**
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001012 * Force a new device unlock password (the password needed to access the
1013 * entire device, not for individual accounts) on the user. This takes
1014 * effect immediately.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001015 * The given password must be sufficient for the
1016 * current password quality and length constraints as returned by
1017 * {@link #getPasswordQuality(ComponentName)} and
1018 * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet
1019 * these constraints, then it will be rejected and false returned. Note
1020 * that the password may be a stronger quality (containing alphanumeric
1021 * characters when the requested quality is only numeric), in which case
1022 * the currently active quality will be increased to match.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001023 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001024 * <p>The calling device admin must have requested
1025 * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
1026 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001027 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001028 * @param password The new password for the user.
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001029 * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}.
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001030 * @return Returns true if the password was applied, or false if it is
1031 * not acceptable for the current constraints.
1032 */
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001033 public boolean resetPassword(String password, int flags) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001034 if (mService != null) {
1035 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001036 return mService.resetPassword(password, flags, UserHandle.myUserId());
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001037 } catch (RemoteException e) {
1038 Log.w(TAG, "Failed talking with device policy service", e);
1039 }
1040 }
1041 return false;
1042 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001043
Dianne Hackbornd6847842010-01-12 18:14:19 -08001044 /**
1045 * Called by an application that is administering the device to set the
1046 * maximum time for user activity until the device will lock. This limits
1047 * the length that the user can set. It takes effect immediately.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001048 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001049 * <p>The calling device admin must have requested
Dianne Hackborn315ada72010-02-11 12:14:08 -08001050 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001051 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001052 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001053 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001054 * @param timeMs The new desired maximum time to lock in milliseconds.
1055 * A value of 0 means there is no restriction.
1056 */
1057 public void setMaximumTimeToLock(ComponentName admin, long timeMs) {
1058 if (mService != null) {
1059 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001060 mService.setMaximumTimeToLock(admin, timeMs, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001061 } catch (RemoteException e) {
1062 Log.w(TAG, "Failed talking with device policy service", e);
1063 }
1064 }
1065 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001066
Dianne Hackbornd6847842010-01-12 18:14:19 -08001067 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -08001068 * Retrieve the current maximum time to unlock for all admins
1069 * or a particular one.
1070 * @param admin The name of the admin component to check, or null to aggregate
1071 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001072 */
Dianne Hackborn254cb442010-01-27 19:23:59 -08001073 public long getMaximumTimeToLock(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001074 return getMaximumTimeToLock(admin, UserHandle.myUserId());
1075 }
1076
1077 /** @hide per-user version */
1078 public long getMaximumTimeToLock(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001079 if (mService != null) {
1080 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001081 return mService.getMaximumTimeToLock(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001082 } catch (RemoteException e) {
1083 Log.w(TAG, "Failed talking with device policy service", e);
1084 }
1085 }
1086 return 0;
1087 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001088
Dianne Hackbornd6847842010-01-12 18:14:19 -08001089 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001090 * Make the device lock immediately, as if the lock screen timeout has
1091 * expired at the point of this call.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001092 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001093 * <p>The calling device admin must have requested
1094 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
1095 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001096 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001097 public void lockNow() {
1098 if (mService != null) {
1099 try {
1100 mService.lockNow();
1101 } catch (RemoteException e) {
1102 Log.w(TAG, "Failed talking with device policy service", e);
1103 }
1104 }
1105 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001106
Dianne Hackbornd6847842010-01-12 18:14:19 -08001107 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001108 * Flag for {@link #wipeData(int)}: also erase the device's external
1109 * storage.
1110 */
1111 public static final int WIPE_EXTERNAL_STORAGE = 0x0001;
1112
1113 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001114 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001115 * erasing all user data while next booting up. External storage such
Masanori Oginof535cb042012-02-15 19:25:50 +09001116 * as SD cards will be also erased if the flag {@link #WIPE_EXTERNAL_STORAGE}
1117 * is set.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001118 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001119 * <p>The calling device admin must have requested
1120 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
1121 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001122 *
Masanori Oginof535cb042012-02-15 19:25:50 +09001123 * @param flags Bit mask of additional options: currently 0 and
1124 * {@link #WIPE_EXTERNAL_STORAGE} are supported.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001125 */
1126 public void wipeData(int flags) {
1127 if (mService != null) {
1128 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001129 mService.wipeData(flags, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001130 } catch (RemoteException e) {
1131 Log.w(TAG, "Failed talking with device policy service", e);
1132 }
1133 }
1134 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001135
Dianne Hackbornd6847842010-01-12 18:14:19 -08001136 /**
Oscar Montemayor69238c62010-08-03 10:51:06 -07001137 * Called by an application that is administering the device to set the
1138 * global proxy and exclusion list.
1139 * <p>
1140 * The calling device admin must have requested
1141 * {@link DeviceAdminInfo#USES_POLICY_SETS_GLOBAL_PROXY} to be able to call
1142 * this method; if it has not, a security exception will be thrown.
1143 * Only the first device admin can set the proxy. If a second admin attempts
1144 * to set the proxy, the {@link ComponentName} of the admin originally setting the
1145 * proxy will be returned. If successful in setting the proxy, null will
1146 * be returned.
1147 * The method can be called repeatedly by the device admin alrady setting the
1148 * proxy to update the proxy and exclusion list.
1149 *
1150 * @param admin Which {@link DeviceAdminReceiver} this request is associated
1151 * with.
1152 * @param proxySpec the global proxy desired. Must be an HTTP Proxy.
1153 * Pass Proxy.NO_PROXY to reset the proxy.
1154 * @param exclusionList a list of domains to be excluded from the global proxy.
Oscar Montemayor69238c62010-08-03 10:51:06 -07001155 * @return returns null if the proxy was successfully set, or a {@link ComponentName}
1156 * of the device admin that sets thew proxy otherwise.
Andy Stadlerd2672722011-02-16 10:53:33 -08001157 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001158 */
1159 public ComponentName setGlobalProxy(ComponentName admin, Proxy proxySpec,
1160 List<String> exclusionList ) {
1161 if (proxySpec == null) {
1162 throw new NullPointerException();
1163 }
1164 if (mService != null) {
1165 try {
1166 String hostSpec;
1167 String exclSpec;
1168 if (proxySpec.equals(Proxy.NO_PROXY)) {
1169 hostSpec = null;
1170 exclSpec = null;
1171 } else {
1172 if (!proxySpec.type().equals(Proxy.Type.HTTP)) {
1173 throw new IllegalArgumentException();
1174 }
1175 InetSocketAddress sa = (InetSocketAddress)proxySpec.address();
1176 String hostName = sa.getHostName();
1177 int port = sa.getPort();
1178 StringBuilder hostBuilder = new StringBuilder();
1179 hostSpec = hostBuilder.append(hostName)
1180 .append(":").append(Integer.toString(port)).toString();
1181 if (exclusionList == null) {
1182 exclSpec = "";
1183 } else {
1184 StringBuilder listBuilder = new StringBuilder();
1185 boolean firstDomain = true;
1186 for (String exclDomain : exclusionList) {
1187 if (!firstDomain) {
1188 listBuilder = listBuilder.append(",");
1189 } else {
1190 firstDomain = false;
1191 }
1192 listBuilder = listBuilder.append(exclDomain.trim());
1193 }
1194 exclSpec = listBuilder.toString();
1195 }
Yuhao Zheng90704842014-02-28 17:22:45 -08001196 if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
1197 != android.net.Proxy.PROXY_VALID)
1198 throw new IllegalArgumentException();
Oscar Montemayor69238c62010-08-03 10:51:06 -07001199 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001200 return mService.setGlobalProxy(admin, hostSpec, exclSpec, UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001201 } catch (RemoteException e) {
1202 Log.w(TAG, "Failed talking with device policy service", e);
1203 }
1204 }
1205 return null;
1206 }
1207
1208 /**
1209 * Returns the component name setting the global proxy.
1210 * @return ComponentName object of the device admin that set the global proxy, or
1211 * null if no admin has set the proxy.
Andy Stadlerd2672722011-02-16 10:53:33 -08001212 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001213 */
1214 public ComponentName getGlobalProxyAdmin() {
1215 if (mService != null) {
1216 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001217 return mService.getGlobalProxyAdmin(UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001218 } catch (RemoteException e) {
1219 Log.w(TAG, "Failed talking with device policy service", e);
1220 }
1221 }
1222 return null;
1223 }
1224
1225 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001226 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001227 * indicating that encryption is not supported.
1228 */
1229 public static final int ENCRYPTION_STATUS_UNSUPPORTED = 0;
1230
1231 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001232 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001233 * indicating that encryption is supported, but is not currently active.
1234 */
1235 public static final int ENCRYPTION_STATUS_INACTIVE = 1;
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 currently active, but is currently
1240 * being activated. This is only reported by devices that support
1241 * encryption of data and only when the storage is currently
1242 * undergoing a process of becoming encrypted. A device that must reboot and/or wipe data
1243 * to become encrypted will never return this value.
1244 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001245 public static final int ENCRYPTION_STATUS_ACTIVATING = 2;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001246
1247 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001248 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001249 * indicating that encryption is active.
1250 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001251 public static final int ENCRYPTION_STATUS_ACTIVE = 3;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001252
1253 /**
1254 * Activity action: begin the process of encrypting data on the device. This activity should
1255 * be launched after using {@link #setStorageEncryption} to request encryption be activated.
1256 * After resuming from this activity, use {@link #getStorageEncryption}
1257 * to check encryption status. However, on some devices this activity may never return, as
1258 * it may trigger a reboot and in some cases a complete data wipe of the device.
1259 */
1260 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
1261 public static final String ACTION_START_ENCRYPTION
1262 = "android.app.action.START_ENCRYPTION";
1263
1264 /**
Jim Millerb8ec4702012-08-31 17:19:10 -07001265 * Widgets are enabled in keyguard
1266 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001267 public static final int KEYGUARD_DISABLE_FEATURES_NONE = 0;
Jim Millerb8ec4702012-08-31 17:19:10 -07001268
1269 /**
1270 * Disable all keyguard widgets
1271 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001272 public static final int KEYGUARD_DISABLE_WIDGETS_ALL = 1 << 0;
1273
1274 /**
1275 * Disable the camera on secure keyguard screens (e.g. PIN/Pattern/Password)
1276 */
1277 public static final int KEYGUARD_DISABLE_SECURE_CAMERA = 1 << 1;
1278
1279 /**
Jim Miller35207742012-11-02 15:33:20 -07001280 * Disable all current and future keyguard customizations.
Jim Miller48b9b0d2012-09-19 23:16:50 -07001281 */
1282 public static final int KEYGUARD_DISABLE_FEATURES_ALL = 0x7fffffff;
Jim Millerb8ec4702012-08-31 17:19:10 -07001283
1284 /**
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001285 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001286 * request that the storage system be encrypted.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001287 *
1288 * <p>When multiple device administrators attempt to control device
1289 * encryption, the most secure, supported setting will always be
1290 * used. If any device administrator requests device encryption,
1291 * it will be enabled; Conversely, if a device administrator
1292 * attempts to disable device encryption while another
1293 * device administrator has enabled it, the call to disable will
1294 * fail (most commonly returning {@link #ENCRYPTION_STATUS_ACTIVE}).
1295 *
1296 * <p>This policy controls encryption of the secure (application data) storage area. Data
Andy Stadler50c294f2011-03-07 19:13:42 -08001297 * written to other storage areas may or may not be encrypted, and this policy does not require
1298 * or control the encryption of any other storage areas.
1299 * There is one exception: If {@link android.os.Environment#isExternalStorageEmulated()} is
1300 * {@code true}, then the directory returned by
1301 * {@link android.os.Environment#getExternalStorageDirectory()} must be written to disk
1302 * within the encrypted storage area.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001303 *
1304 * <p>Important Note: On some devices, it is possible to encrypt storage without requiring
1305 * the user to create a device PIN or Password. In this case, the storage is encrypted, but
1306 * the encryption key may not be fully secured. For maximum security, the administrator should
1307 * also require (and check for) a pattern, PIN, or password.
1308 *
1309 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1310 * @param encrypt true to request encryption, false to release any previous request
Andy Stadler22dbfda2011-01-17 12:47:31 -08001311 * @return the new request status (for all active admins) - will be one of
1312 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE}, or
1313 * {@link #ENCRYPTION_STATUS_ACTIVE}. This is the value of the requests; Use
1314 * {@link #getStorageEncryptionStatus()} to query the actual device state.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001315 */
1316 public int setStorageEncryption(ComponentName admin, boolean encrypt) {
1317 if (mService != null) {
1318 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001319 return mService.setStorageEncryption(admin, encrypt, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001320 } catch (RemoteException e) {
1321 Log.w(TAG, "Failed talking with device policy service", e);
1322 }
1323 }
1324 return ENCRYPTION_STATUS_UNSUPPORTED;
1325 }
1326
1327 /**
1328 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001329 * determine the requested setting for secure storage.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001330 *
Andy Stadler22dbfda2011-01-17 12:47:31 -08001331 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. If null,
1332 * this will return the requested encryption setting as an aggregate of all active
1333 * administrators.
1334 * @return true if the admin(s) are requesting encryption, false if not.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001335 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001336 public boolean getStorageEncryption(ComponentName admin) {
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001337 if (mService != null) {
1338 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001339 return mService.getStorageEncryption(admin, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001340 } catch (RemoteException e) {
1341 Log.w(TAG, "Failed talking with device policy service", e);
1342 }
1343 }
Andy Stadler22dbfda2011-01-17 12:47:31 -08001344 return false;
1345 }
1346
1347 /**
1348 * Called by an application that is administering the device to
1349 * determine the current encryption status of the device.
1350 *
1351 * Depending on the returned status code, the caller may proceed in different
1352 * ways. If the result is {@link #ENCRYPTION_STATUS_UNSUPPORTED}, the
1353 * storage system does not support encryption. If the
1354 * result is {@link #ENCRYPTION_STATUS_INACTIVE}, use {@link
1355 * #ACTION_START_ENCRYPTION} to begin the process of encrypting or decrypting the
1356 * storage. If the result is {@link #ENCRYPTION_STATUS_ACTIVATING} or
1357 * {@link #ENCRYPTION_STATUS_ACTIVE}, no further action is required.
1358 *
1359 * @return current status of encryption. The value will be one of
1360 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE},
1361 * {@link #ENCRYPTION_STATUS_ACTIVATING}, or{@link #ENCRYPTION_STATUS_ACTIVE}.
1362 */
1363 public int getStorageEncryptionStatus() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001364 return getStorageEncryptionStatus(UserHandle.myUserId());
1365 }
1366
1367 /** @hide per-user version */
1368 public int getStorageEncryptionStatus(int userHandle) {
Andy Stadler22dbfda2011-01-17 12:47:31 -08001369 if (mService != null) {
1370 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001371 return mService.getStorageEncryptionStatus(userHandle);
Andy Stadler22dbfda2011-01-17 12:47:31 -08001372 } catch (RemoteException e) {
1373 Log.w(TAG, "Failed talking with device policy service", e);
1374 }
1375 }
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001376 return ENCRYPTION_STATUS_UNSUPPORTED;
1377 }
1378
1379 /**
Maggie Benthallda51e682013-08-08 22:35:44 -04001380 * Installs the given certificate as a User CA.
1381 *
1382 * @return false if the certBuffer cannot be parsed or installation is
1383 * interrupted, otherwise true
1384 * @hide
1385 */
1386 public boolean installCaCert(byte[] certBuffer) {
1387 if (mService != null) {
1388 try {
1389 return mService.installCaCert(certBuffer);
1390 } catch (RemoteException e) {
1391 Log.w(TAG, "Failed talking with device policy service", e);
1392 }
1393 }
1394 return false;
1395 }
1396
1397 /**
1398 * Uninstalls the given certificate from the list of User CAs, if present.
1399 *
1400 * @hide
1401 */
1402 public void uninstallCaCert(byte[] certBuffer) {
1403 if (mService != null) {
1404 try {
1405 mService.uninstallCaCert(certBuffer);
1406 } catch (RemoteException e) {
1407 Log.w(TAG, "Failed talking with device policy service", e);
1408 }
1409 }
1410 }
1411
1412 /**
1413 * Returns whether there are any user-installed CA certificates.
1414 *
1415 * @hide
1416 */
Maggie Benthall0469f412013-09-05 15:30:26 -04001417 public static boolean hasAnyCaCertsInstalled() {
Maggie Benthallda51e682013-08-08 22:35:44 -04001418 TrustedCertificateStore certStore = new TrustedCertificateStore();
1419 Set<String> aliases = certStore.userAliases();
1420 return aliases != null && !aliases.isEmpty();
1421 }
1422
1423 /**
1424 * Returns whether this certificate has been installed as a User CA.
1425 *
1426 * @hide
1427 */
1428 public boolean hasCaCertInstalled(byte[] certBuffer) {
1429 TrustedCertificateStore certStore = new TrustedCertificateStore();
1430 String alias;
1431 byte[] pemCert;
1432 try {
1433 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
1434 X509Certificate cert = (X509Certificate) certFactory.generateCertificate(
1435 new ByteArrayInputStream(certBuffer));
1436 return certStore.getCertificateAlias(cert) != null;
1437 } catch (CertificateException ce) {
1438 Log.w(TAG, "Could not parse certificate", ce);
1439 }
1440 return false;
1441 }
1442
1443 /**
Ben Komalo2447edd2011-05-09 16:05:33 -07001444 * Called by an application that is administering the device to disable all cameras
1445 * on the device. After setting this, no applications will be able to access any cameras
1446 * on the device.
1447 *
1448 * <p>The calling device admin must have requested
1449 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_CAMERA} to be able to call
1450 * this method; if it has not, a security exception will be thrown.
1451 *
1452 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1453 * @param disabled Whether or not the camera should be disabled.
1454 */
1455 public void setCameraDisabled(ComponentName admin, boolean disabled) {
1456 if (mService != null) {
1457 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001458 mService.setCameraDisabled(admin, disabled, UserHandle.myUserId());
Ben Komalo2447edd2011-05-09 16:05:33 -07001459 } catch (RemoteException e) {
1460 Log.w(TAG, "Failed talking with device policy service", e);
1461 }
1462 }
1463 }
1464
1465 /**
1466 * Determine whether or not the device's cameras have been disabled either by the current
1467 * admin, if specified, or all admins.
1468 * @param admin The name of the admin component to check, or null to check if any admins
1469 * have disabled the camera
1470 */
1471 public boolean getCameraDisabled(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001472 return getCameraDisabled(admin, UserHandle.myUserId());
1473 }
1474
1475 /** @hide per-user version */
1476 public boolean getCameraDisabled(ComponentName admin, int userHandle) {
Ben Komalo2447edd2011-05-09 16:05:33 -07001477 if (mService != null) {
1478 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001479 return mService.getCameraDisabled(admin, userHandle);
Ben Komalo2447edd2011-05-09 16:05:33 -07001480 } catch (RemoteException e) {
1481 Log.w(TAG, "Failed talking with device policy service", e);
1482 }
1483 }
1484 return false;
1485 }
1486
1487 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001488 * Called by an application that is administering the device to disable keyguard customizations,
1489 * such as widgets. After setting this, keyguard features will be disabled according to the
1490 * provided feature list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001491 *
1492 * <p>The calling device admin must have requested
Jim Miller48b9b0d2012-09-19 23:16:50 -07001493 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_KEYGUARD_FEATURES} to be able to call
Jim Millerb8ec4702012-08-31 17:19:10 -07001494 * this method; if it has not, a security exception will be thrown.
1495 *
1496 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Jim Miller35207742012-11-02 15:33:20 -07001497 * @param which {@link #KEYGUARD_DISABLE_FEATURES_NONE} (default),
1498 * {@link #KEYGUARD_DISABLE_WIDGETS_ALL}, {@link #KEYGUARD_DISABLE_SECURE_CAMERA},
1499 * {@link #KEYGUARD_DISABLE_FEATURES_ALL}
Jim Millerb8ec4702012-08-31 17:19:10 -07001500 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001501 public void setKeyguardDisabledFeatures(ComponentName admin, int which) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001502 if (mService != null) {
1503 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001504 mService.setKeyguardDisabledFeatures(admin, which, UserHandle.myUserId());
Jim Millerb8ec4702012-08-31 17:19:10 -07001505 } catch (RemoteException e) {
1506 Log.w(TAG, "Failed talking with device policy service", e);
1507 }
1508 }
1509 }
1510
1511 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001512 * Determine whether or not features have been disabled in keyguard either by the current
Jim Millerb8ec4702012-08-31 17:19:10 -07001513 * admin, if specified, or all admins.
1514 * @param admin The name of the admin component to check, or null to check if any admins
Jim Miller48b9b0d2012-09-19 23:16:50 -07001515 * have disabled features in keyguard.
Jim Miller35207742012-11-02 15:33:20 -07001516 * @return bitfield of flags. See {@link #setKeyguardDisabledFeatures(ComponentName, int)}
1517 * for a list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001518 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001519 public int getKeyguardDisabledFeatures(ComponentName admin) {
1520 return getKeyguardDisabledFeatures(admin, UserHandle.myUserId());
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001521 }
1522
1523 /** @hide per-user version */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001524 public int getKeyguardDisabledFeatures(ComponentName admin, int userHandle) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001525 if (mService != null) {
1526 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001527 return mService.getKeyguardDisabledFeatures(admin, userHandle);
Jim Millerb8ec4702012-08-31 17:19:10 -07001528 } catch (RemoteException e) {
1529 Log.w(TAG, "Failed talking with device policy service", e);
1530 }
1531 }
Jim Miller48b9b0d2012-09-19 23:16:50 -07001532 return KEYGUARD_DISABLE_FEATURES_NONE;
Jim Millerb8ec4702012-08-31 17:19:10 -07001533 }
1534
1535 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001536 * @hide
1537 */
Jessica Hummel6d36b602014-04-04 12:42:17 +01001538 public void setActiveAdmin(ComponentName policyReceiver, boolean refreshing, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001539 if (mService != null) {
1540 try {
Jessica Hummel6d36b602014-04-04 12:42:17 +01001541 mService.setActiveAdmin(policyReceiver, refreshing, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001542 } catch (RemoteException e) {
1543 Log.w(TAG, "Failed talking with device policy service", e);
1544 }
1545 }
1546 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001547
Dianne Hackbornd6847842010-01-12 18:14:19 -08001548 /**
Jessica Hummel6d36b602014-04-04 12:42:17 +01001549 * @hide
1550 */
1551 public void setActiveAdmin(ComponentName policyReceiver, boolean refreshing) {
1552 setActiveAdmin(policyReceiver, refreshing, UserHandle.myUserId());
1553 }
1554
1555 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -08001556 * Returns the DeviceAdminInfo as defined by the administrator's package info & meta-data
Dianne Hackbornd6847842010-01-12 18:14:19 -08001557 * @hide
1558 */
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001559 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001560 ActivityInfo ai;
1561 try {
1562 ai = mContext.getPackageManager().getReceiverInfo(cn,
1563 PackageManager.GET_META_DATA);
1564 } catch (PackageManager.NameNotFoundException e) {
1565 Log.w(TAG, "Unable to retrieve device policy " + cn, e);
1566 return null;
1567 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001568
Dianne Hackbornd6847842010-01-12 18:14:19 -08001569 ResolveInfo ri = new ResolveInfo();
1570 ri.activityInfo = ai;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001571
Dianne Hackbornd6847842010-01-12 18:14:19 -08001572 try {
1573 return new DeviceAdminInfo(mContext, ri);
1574 } catch (XmlPullParserException e) {
1575 Log.w(TAG, "Unable to parse device policy " + cn, e);
1576 return null;
1577 } catch (IOException e) {
1578 Log.w(TAG, "Unable to parse device policy " + cn, e);
1579 return null;
1580 }
1581 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001582
Dianne Hackbornd6847842010-01-12 18:14:19 -08001583 /**
1584 * @hide
1585 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001586 public void getRemoveWarning(ComponentName admin, RemoteCallback result) {
1587 if (mService != null) {
1588 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001589 mService.getRemoveWarning(admin, result, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001590 } catch (RemoteException e) {
1591 Log.w(TAG, "Failed talking with device policy service", e);
1592 }
1593 }
1594 }
1595
1596 /**
1597 * @hide
1598 */
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001599 public void setActivePasswordState(int quality, int length, int letters, int uppercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001600 int lowercase, int numbers, int symbols, int nonletter, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001601 if (mService != null) {
1602 try {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001603 mService.setActivePasswordState(quality, length, letters, uppercase, lowercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001604 numbers, symbols, nonletter, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001605 } catch (RemoteException e) {
1606 Log.w(TAG, "Failed talking with device policy service", e);
1607 }
1608 }
1609 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001610
Dianne Hackbornd6847842010-01-12 18:14:19 -08001611 /**
1612 * @hide
1613 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001614 public void reportFailedPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001615 if (mService != null) {
1616 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001617 mService.reportFailedPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001618 } catch (RemoteException e) {
1619 Log.w(TAG, "Failed talking with device policy service", e);
1620 }
1621 }
1622 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001623
Dianne Hackbornd6847842010-01-12 18:14:19 -08001624 /**
1625 * @hide
1626 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001627 public void reportSuccessfulPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001628 if (mService != null) {
1629 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001630 mService.reportSuccessfulPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001631 } catch (RemoteException e) {
1632 Log.w(TAG, "Failed talking with device policy service", e);
1633 }
1634 }
1635 }
Amith Yamasani71e6c692013-03-24 17:39:28 -07001636
1637 /**
1638 * @hide
1639 * Sets the given package as the device owner. The package must already be installed and there
1640 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1641 * method must be called before the device is provisioned.
1642 * @param packageName the package name of the application to be registered as the device owner.
1643 * @return whether the package was successfully registered as the device owner.
1644 * @throws IllegalArgumentException if the package name is null or invalid
1645 * @throws IllegalStateException if a device owner is already registered or the device has
1646 * already been provisioned.
1647 */
1648 public boolean setDeviceOwner(String packageName) throws IllegalArgumentException,
1649 IllegalStateException {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001650 return setDeviceOwner(packageName, null);
1651 }
1652
1653 /**
1654 * @hide
1655 * Sets the given package as the device owner. The package must already be installed and there
1656 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1657 * method must be called before the device is provisioned.
1658 * @param packageName the package name of the application to be registered as the device owner.
1659 * @param ownerName the human readable name of the institution that owns this device.
1660 * @return whether the package was successfully registered as the device owner.
1661 * @throws IllegalArgumentException if the package name is null or invalid
1662 * @throws IllegalStateException if a device owner is already registered or the device has
1663 * already been provisioned.
1664 */
1665 public boolean setDeviceOwner(String packageName, String ownerName)
1666 throws IllegalArgumentException, IllegalStateException {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001667 if (mService != null) {
1668 try {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001669 return mService.setDeviceOwner(packageName, ownerName);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001670 } catch (RemoteException re) {
1671 Log.w(TAG, "Failed to set device owner");
1672 }
1673 }
1674 return false;
1675 }
1676
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001677
Amith Yamasani71e6c692013-03-24 17:39:28 -07001678 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001679 * Used to determine if a particular package has been registered as a Device Owner app.
1680 * A device owner app is a special device admin that cannot be deactivated by the user, once
1681 * activated as a device admin. It also cannot be uninstalled. To check if a particular
1682 * package is currently registered as the device owner app, pass in the package name from
1683 * {@link Context#getPackageName()} to this method.<p/>This is useful for device
1684 * admin apps that want to check if they are also registered as the device owner app. The
1685 * exact mechanism by which a device admin app is registered as a device owner app is defined by
1686 * the setup process.
1687 * @param packageName the package name of the app, to compare with the registered device owner
1688 * app, if any.
1689 * @return whether or not the package is registered as the device owner app.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001690 */
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001691 public boolean isDeviceOwnerApp(String packageName) {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001692 if (mService != null) {
1693 try {
1694 return mService.isDeviceOwner(packageName);
1695 } catch (RemoteException re) {
1696 Log.w(TAG, "Failed to check device owner");
1697 }
1698 }
1699 return false;
1700 }
1701
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001702 /**
1703 * @hide
1704 * Redirect to isDeviceOwnerApp.
1705 */
1706 public boolean isDeviceOwner(String packageName) {
1707 return isDeviceOwnerApp(packageName);
1708 }
1709
Amith Yamasani71e6c692013-03-24 17:39:28 -07001710 /** @hide */
1711 public String getDeviceOwner() {
1712 if (mService != null) {
1713 try {
1714 return mService.getDeviceOwner();
1715 } catch (RemoteException re) {
1716 Log.w(TAG, "Failed to get device owner");
1717 }
1718 }
1719 return null;
1720 }
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001721
1722 /** @hide */
1723 public String getDeviceOwnerName() {
1724 if (mService != null) {
1725 try {
1726 return mService.getDeviceOwnerName();
1727 } catch (RemoteException re) {
1728 Log.w(TAG, "Failed to get device owner");
1729 }
1730 }
1731 return null;
1732 }
Adam Connors776c5552014-01-09 10:42:56 +00001733
1734 /**
1735 * @hide
1736 * Sets the given package as the profile owner of the given user profile. The package must
1737 * already be installed and there shouldn't be an existing profile owner registered for this
1738 * user. Also, this method must be called before the user has been used for the first time.
1739 * @param packageName the package name of the application to be registered as profile owner.
1740 * @param ownerName the human readable name of the organisation associated with this DPM.
Adam Connors661ec472014-02-11 13:59:46 +00001741 * @param userHandle the userId to set the profile owner for.
Adam Connors776c5552014-01-09 10:42:56 +00001742 * @return whether the package was successfully registered as the profile owner.
1743 * @throws IllegalArgumentException if packageName is null, the package isn't installed, or
1744 * the user has already been set up.
1745 */
Adam Connors661ec472014-02-11 13:59:46 +00001746 public boolean setProfileOwner(String packageName, String ownerName, int userHandle)
Adam Connors776c5552014-01-09 10:42:56 +00001747 throws IllegalArgumentException {
1748 if (mService != null) {
1749 try {
Adam Connors661ec472014-02-11 13:59:46 +00001750 return mService.setProfileOwner(packageName, ownerName, userHandle);
Adam Connors776c5552014-01-09 10:42:56 +00001751 } catch (RemoteException re) {
1752 Log.w(TAG, "Failed to set profile owner", re);
1753 throw new IllegalArgumentException("Couldn't set profile owner.", re);
1754 }
1755 }
1756 return false;
1757 }
1758
1759 /**
Alexandra Gherghina512675b2014-04-02 11:23:54 +01001760 * Sets the enabled state of the profile. A profile should be enabled only once it is ready to
1761 * be used. Only the profile owner can call this.
1762 *
1763 * @see #isPRofileOwnerApp
1764 *
1765 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1766 */
1767 public void setProfileEnabled(ComponentName admin) {
1768 if (mService != null) {
1769 try {
1770 mService.setProfileEnabled(admin);
1771 } catch (RemoteException e) {
1772 Log.w(TAG, "Failed talking with device policy service", e);
1773 }
1774 }
1775 }
1776
1777 /**
Adam Connors776c5552014-01-09 10:42:56 +00001778 * Used to determine if a particular package is registered as the Profile Owner for the
Alexandra Gherghina512675b2014-04-02 11:23:54 +01001779 * current user. A profile owner is a special device admin that has additional privileges
Adam Connors776c5552014-01-09 10:42:56 +00001780 * within the managed profile.
1781 *
1782 * @param packageName The package name of the app to compare with the registered profile owner.
1783 * @return Whether or not the package is registered as the profile owner.
1784 */
1785 public boolean isProfileOwnerApp(String packageName) {
1786 if (mService != null) {
1787 try {
1788 String profileOwnerPackage = mService.getProfileOwner(
1789 Process.myUserHandle().getIdentifier());
1790 return profileOwnerPackage != null && profileOwnerPackage.equals(packageName);
1791 } catch (RemoteException re) {
1792 Log.w(TAG, "Failed to check profile owner");
1793 }
1794 }
1795 return false;
1796 }
1797
1798 /**
1799 * @hide
1800 * @return the packageName of the owner of the given user profile or null if no profile
1801 * owner has been set for that user.
1802 * @throws IllegalArgumentException if the userId is invalid.
1803 */
1804 public String getProfileOwner() throws IllegalArgumentException {
1805 if (mService != null) {
1806 try {
1807 return mService.getProfileOwner(Process.myUserHandle().getIdentifier());
1808 } catch (RemoteException re) {
1809 Log.w(TAG, "Failed to get profile owner");
1810 throw new IllegalArgumentException(
1811 "Requested profile owner for invalid userId", re);
1812 }
1813 }
1814 return null;
1815 }
1816
1817 /**
1818 * @hide
Alexandra Gherghina385124d2014-04-03 13:37:39 +01001819 * @param userId the userId of a managed profile profile.
1820 *
1821 * @return whether or not the managed profile is enabled.
1822 * @throws IllegalArgumentException if the userId is invalid.
1823 */
1824 public boolean isProfileEnabled(int userId) throws IllegalArgumentException {
1825 if (mService != null) {
1826 try {
1827 return mService.isProfileEnabled(userId);
1828 } catch (RemoteException re) {
1829 Log.w(TAG, "Failed to get status for owner profile.");
1830 throw new IllegalArgumentException(
1831 "Failed to get status for owner profile.", re);
1832 }
1833 }
1834 return true;
1835 }
1836
1837
1838 /**
1839 * @hide
Adam Connors776c5552014-01-09 10:42:56 +00001840 * @return the human readable name of the organisation associated with this DPM or null if
1841 * one is not set.
1842 * @throws IllegalArgumentException if the userId is invalid.
1843 */
1844 public String getProfileOwnerName() throws IllegalArgumentException {
1845 if (mService != null) {
1846 try {
1847 return mService.getProfileOwnerName(Process.myUserHandle().getIdentifier());
1848 } catch (RemoteException re) {
1849 Log.w(TAG, "Failed to get profile owner");
1850 throw new IllegalArgumentException(
1851 "Requested profile owner for invalid userId", re);
1852 }
1853 }
1854 return null;
1855 }
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001856
1857 /**
1858 * Called by a profile owner or device owner to add a default intent handler activity for
1859 * intents that match a certain intent filter. This activity will remain the default intent
1860 * handler even if the set of potential event handlers for the intent filter changes and if
1861 * the intent preferences are reset.
1862 *
1863 * <p>The default disambiguation mechanism takes over if the activity is not installed
1864 * (anymore). When the activity is (re)installed, it is automatically reset as default
1865 * intent handler for the filter.
1866 *
1867 * <p>The calling device admin must be a profile owner or device owner. If it is not, a
1868 * security exception will be thrown.
1869 *
1870 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1871 * @param filter The IntentFilter for which a default handler is added.
1872 * @param activity The Activity that is added as default intent handler.
1873 */
1874 public void addPersistentPreferredActivity(ComponentName admin, IntentFilter filter,
1875 ComponentName activity) {
1876 if (mService != null) {
1877 try {
1878 mService.addPersistentPreferredActivity(admin, filter, activity);
1879 } catch (RemoteException e) {
1880 Log.w(TAG, "Failed talking with device policy service", e);
1881 }
1882 }
1883 }
1884
1885 /**
1886 * Called by a profile owner or device owner to remove all persistent intent handler preferences
Torne (Richard Coles)875e2102014-02-24 14:11:56 +00001887 * associated with the given package that were set by {@link #addPersistentPreferredActivity}.
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001888 *
1889 * <p>The calling device admin must be a profile owner. If it is not, a security
1890 * exception will be thrown.
1891 *
1892 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1893 * @param packageName The name of the package for which preferences are removed.
1894 */
1895 public void clearPackagePersistentPreferredActivities(ComponentName admin,
1896 String packageName) {
1897 if (mService != null) {
1898 try {
1899 mService.clearPackagePersistentPreferredActivities(admin, packageName);
1900 } catch (RemoteException e) {
1901 Log.w(TAG, "Failed talking with device policy service", e);
1902 }
1903 }
1904 }
Robin Lee66e5d962014-04-09 16:44:21 +01001905
1906 /**
1907 * Called by a profile or device owner to set the application restrictions for a given target
1908 * application running in the managed profile.
1909 *
1910 * <p>The provided {@link Bundle} consists of key-value pairs, where the types of values may be
1911 * {@link Boolean}, {@link String}, or {@link String}[]. The recommended format for key strings
1912 * is "com.example.packagename/example-setting" to avoid naming conflicts with library
1913 * components such as {@link android.webkit.WebView}.
1914 *
1915 * <p>The application restrictions are only made visible to the target application and the
1916 * profile or device owner.
1917 *
1918 * <p>The calling device admin must be a profile or device owner; if it is not, a security
1919 * exception will be thrown.
1920 *
1921 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1922 * @param packageName The name of the package to update restricted settings for.
1923 * @param settings A {@link Bundle} to be parsed by the receiving application, conveying a new
1924 * set of active restrictions.
1925 */
1926 public void setApplicationRestrictions(ComponentName admin, String packageName,
1927 Bundle settings) {
1928 if (mService != null) {
1929 try {
1930 mService.setApplicationRestrictions(admin, packageName, settings);
1931 } catch (RemoteException e) {
1932 Log.w(TAG, "Failed talking with device policy service", e);
1933 }
1934 }
1935 }
1936
1937 /**
1938 * Called by a profile or device owner to get the application restrictions for a given target
1939 * application running in the managed profile.
1940 *
1941 * <p>The calling device admin must be a profile or device owner; if it is not, a security
1942 * exception will be thrown.
1943 *
1944 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1945 * @param packageName The name of the package to fetch restricted settings of.
1946 * @return {@link Bundle} of settings corresponding to what was set last time
1947 * {@link DevicePolicyManager#setApplicationRestrictions} was called, or an empty {@link Bundle}
1948 * if no restrictions have been set.
1949 */
1950 public Bundle getApplicationRestrictions(ComponentName admin, String packageName) {
1951 if (mService != null) {
1952 try {
1953 return mService.getApplicationRestrictions(admin, packageName);
1954 } catch (RemoteException e) {
1955 Log.w(TAG, "Failed talking with device policy service", e);
1956 }
1957 }
1958 return null;
1959 }
Amith Yamasanibe465322014-04-24 13:45:17 -07001960
1961 /**
1962 * Called by a profile or device owner to set a user restriction specified
1963 * by the key.
1964 * <p>
1965 * The calling device admin must be a profile or device owner; if it is not,
1966 * a security exception will be thrown.
1967 *
1968 * @param admin Which {@link DeviceAdminReceiver} this request is associated
1969 * with.
1970 * @param key The key of the restriction. See the constants in
1971 * {@link android.os.UserManager} for the list of keys.
1972 */
1973 public void addUserRestriction(ComponentName admin, String key) {
1974 if (mService != null) {
1975 try {
1976 mService.setUserRestriction(admin, key, true);
1977 } catch (RemoteException e) {
1978 Log.w(TAG, "Failed talking with device policy service", e);
1979 }
1980 }
1981 }
1982
1983 /**
1984 * Called by a profile or device owner to clear a user restriction specified
1985 * by the key.
1986 * <p>
1987 * The calling device admin must be a profile or device owner; if it is not,
1988 * a security exception will be thrown.
1989 *
1990 * @param admin Which {@link DeviceAdminReceiver} this request is associated
1991 * with.
1992 * @param key The key of the restriction. See the constants in
1993 * {@link android.os.UserManager} for the list of keys.
1994 */
1995 public void clearUserRestriction(ComponentName admin, String key) {
1996 if (mService != null) {
1997 try {
1998 mService.setUserRestriction(admin, key, false);
1999 } catch (RemoteException e) {
2000 Log.w(TAG, "Failed talking with device policy service", e);
2001 }
2002 }
2003 }
Dianne Hackbornd6847842010-01-12 18:14:19 -08002004}