blob: e06cf38c6be0604e8687c026be5fd75b13d9926b [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;
29import android.os.Handler;
Adam Connors776c5552014-01-09 10:42:56 +000030import android.os.Process;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080031import android.os.RemoteCallback;
Dianne Hackbornd6847842010-01-12 18:14:19 -080032import android.os.RemoteException;
33import android.os.ServiceManager;
Amith Yamasani599dd7c2012-09-14 23:20:08 -070034import android.os.UserHandle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080035import android.util.Log;
36
Maggie Benthallda51e682013-08-08 22:35:44 -040037import com.android.org.conscrypt.TrustedCertificateStore;
38
39import java.io.ByteArrayInputStream;
Dianne Hackbornd6847842010-01-12 18:14:19 -080040import java.io.IOException;
Oscar Montemayor69238c62010-08-03 10:51:06 -070041import java.net.InetSocketAddress;
42import java.net.Proxy;
Maggie Benthallda51e682013-08-08 22:35:44 -040043import java.security.cert.CertificateException;
44import java.security.cert.CertificateFactory;
45import java.security.cert.X509Certificate;
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -080046import java.util.List;
Maggie Benthallda51e682013-08-08 22:35:44 -040047import java.util.Set;
Dianne Hackbornd6847842010-01-12 18:14:19 -080048
49/**
50 * Public interface for managing policies enforced on a device. Most clients
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080051 * of this class must have published a {@link DeviceAdminReceiver} that the user
Dianne Hackbornd6847842010-01-12 18:14:19 -080052 * has currently enabled.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080053 *
54 * <div class="special reference">
55 * <h3>Developer Guides</h3>
56 * <p>For more information about managing policies for device adminstration, read the
57 * <a href="{@docRoot}guide/topics/admin/device-admin.html">Device Administration</a>
58 * developer guide.</p>
59 * </div>
Dianne Hackbornd6847842010-01-12 18:14:19 -080060 */
61public class DevicePolicyManager {
62 private static String TAG = "DevicePolicyManager";
Dianne Hackbornd6847842010-01-12 18:14:19 -080063
64 private final Context mContext;
Dianne Hackbornd6847842010-01-12 18:14:19 -080065 private final IDevicePolicyManager mService;
Konstantin Lopyrev32558232010-05-20 16:18:05 -070066
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080067 private DevicePolicyManager(Context context, Handler handler) {
Dianne Hackbornd6847842010-01-12 18:14:19 -080068 mContext = context;
Dianne Hackbornd6847842010-01-12 18:14:19 -080069 mService = IDevicePolicyManager.Stub.asInterface(
70 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
71 }
72
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080073 /** @hide */
74 public static DevicePolicyManager create(Context context, Handler handler) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -080075 DevicePolicyManager me = new DevicePolicyManager(context, handler);
76 return me.mService != null ? me : null;
77 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -070078
Dianne Hackbornd6847842010-01-12 18:14:19 -080079 /**
Jessica Hummelf72078b2014-03-06 16:13:12 +000080 * Activity action: Starts the provisioning flow which sets up a managed profile.
81 * This intent will typically be sent by a mobile device management application(mdm).
82 * Managed profile provisioning creates a profile, moves the mdm to the profile,
83 * sets the mdm as the profile owner and removes all non required applications from the profile.
84 * As a profile owner the mdm than has full control over the managed profile.
85 *
86 * <p>The intent must contain the extras {@link #EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME} and
87 * {@link #EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME}.
88 *
89 * <p> When managed provisioning has completed, an intent of the type
90 * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE} is broadcasted to the
91 * mdm app on the managed profile.
92 *
93 * <p>Input: Nothing.</p>
94 * <p>Output: Nothing</p>
95 */
96 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
97 public static final String ACTION_PROVISION_MANAGED_PROFILE
98 = "android.managedprovisioning.ACTION_PROVISION_MANAGED_PROFILE";
99
100 /**
101 * A String extra holding the name of the package of the mobile device management application
102 * that starts the managed provisioning flow. This package will be set as the profile owner.
103 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}.
104 */
105 public static final String EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME
106 = "deviceAdminPackageName";
107
108 /**
109 * A String extra holding the default name of the profile that is created during managed profile
110 * provisioning.
111 * <p>Use with {@link #ACTION_PROVISION_MANAGED_PROFILE}
112 */
113 public static final String EXTRA_PROVISIONING_DEFAULT_MANAGED_PROFILE_NAME
114 = "defaultManagedProfileName";
115
116 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800117 * Activity action: ask the user to add a new device administrator to the system.
118 * The desired policy is the ComponentName of the policy in the
119 * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to
120 * bring the user through adding the device administrator to the system (or
121 * allowing them to reject it).
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700122 *
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800123 * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION}
124 * field to provide the user with additional explanation (in addition
125 * to your component's description) about what is being added.
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800126 *
127 * <p>If your administrator is already active, this will ordinarily return immediately (without
128 * user intervention). However, if your administrator has been updated and is requesting
129 * additional uses-policy flags, the user will be presented with the new list. New policies
130 * will not be available to the updated administrator until the user has accepted the new list.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800131 */
132 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
133 public static final String ACTION_ADD_DEVICE_ADMIN
134 = "android.app.action.ADD_DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700135
Dianne Hackbornd6847842010-01-12 18:14:19 -0800136 /**
Jim Miller284b62e2010-06-08 14:27:42 -0700137 * Activity action: send when any policy admin changes a policy.
138 * This is generally used to find out when a new policy is in effect.
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700139 *
Jim Miller284b62e2010-06-08 14:27:42 -0700140 * @hide
141 */
142 public static final String ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED
143 = "android.app.action.DEVICE_POLICY_MANAGER_STATE_CHANGED";
144
145 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800146 * The ComponentName of the administrator component.
147 *
148 * @see #ACTION_ADD_DEVICE_ADMIN
149 */
150 public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700151
Dianne Hackbornd6847842010-01-12 18:14:19 -0800152 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800153 * An optional CharSequence providing additional explanation for why the
154 * admin is being added.
155 *
156 * @see #ACTION_ADD_DEVICE_ADMIN
157 */
158 public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700159
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800160 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700161 * Activity action: have the user enter a new password. This activity should
162 * be launched after using {@link #setPasswordQuality(ComponentName, int)},
163 * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the user
164 * enter a new password that meets the current requirements. You can use
165 * {@link #isActivePasswordSufficient()} to determine whether you need to
166 * have the user select a new password in order to meet the current
167 * constraints. Upon being resumed from this activity, you can check the new
168 * password characteristics to see if they are sufficient.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800169 */
170 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
171 public static final String ACTION_SET_NEW_PASSWORD
172 = "android.app.action.SET_NEW_PASSWORD";
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700173
Dianne Hackbornd6847842010-01-12 18:14:19 -0800174 /**
175 * Return true if the given administrator component is currently
176 * active (enabled) in the system.
177 */
178 public boolean isAdminActive(ComponentName who) {
179 if (mService != null) {
180 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700181 return mService.isAdminActive(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800182 } catch (RemoteException e) {
183 Log.w(TAG, "Failed talking with device policy service", e);
184 }
185 }
186 return false;
187 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700188
Dianne Hackbornd6847842010-01-12 18:14:19 -0800189 /**
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800190 * Return a list of all currently active device administrator's component
191 * names. Note that if there are no administrators than null may be
192 * returned.
193 */
194 public List<ComponentName> getActiveAdmins() {
195 if (mService != null) {
196 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700197 return mService.getActiveAdmins(UserHandle.myUserId());
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800198 } catch (RemoteException e) {
199 Log.w(TAG, "Failed talking with device policy service", e);
200 }
201 }
202 return null;
203 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700204
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -0800205 /**
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700206 * Used by package administration code to determine if a package can be stopped
207 * or uninstalled.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800208 * @hide
209 */
210 public boolean packageHasActiveAdmins(String packageName) {
211 if (mService != null) {
212 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700213 return mService.packageHasActiveAdmins(packageName, UserHandle.myUserId());
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800214 } catch (RemoteException e) {
215 Log.w(TAG, "Failed talking with device policy service", e);
216 }
217 }
218 return false;
219 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700220
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800221 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800222 * Remove a current administration component. This can only be called
223 * by the application that owns the administration component; if you
224 * try to remove someone else's component, a security exception will be
225 * thrown.
226 */
227 public void removeActiveAdmin(ComponentName who) {
228 if (mService != null) {
229 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700230 mService.removeActiveAdmin(who, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800231 } catch (RemoteException e) {
232 Log.w(TAG, "Failed talking with device policy service", e);
233 }
234 }
235 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700236
Dianne Hackbornd6847842010-01-12 18:14:19 -0800237 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800238 * Returns true if an administrator has been granted a particular device policy. This can
239 * be used to check if the administrator was activated under an earlier set of policies,
240 * but requires additional policies after an upgrade.
241 *
242 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. Must be
243 * an active administrator, or an exception will be thrown.
244 * @param usesPolicy Which uses-policy to check, as defined in {@link DeviceAdminInfo}.
245 */
246 public boolean hasGrantedPolicy(ComponentName admin, int usesPolicy) {
247 if (mService != null) {
248 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700249 return mService.hasGrantedPolicy(admin, usesPolicy, UserHandle.myUserId());
Andy Stadlerc25f70a2010-12-08 15:56:45 -0800250 } catch (RemoteException e) {
251 Log.w(TAG, "Failed talking with device policy service", e);
252 }
253 }
254 return false;
255 }
256
257 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800258 * Constant for {@link #setPasswordQuality}: the policy has no requirements
259 * for the password. Note that quality constants are ordered so that higher
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800260 * values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800261 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800262 public static final int PASSWORD_QUALITY_UNSPECIFIED = 0;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700263
Dianne Hackbornd6847842010-01-12 18:14:19 -0800264 /**
Jim Miller3e5d3fd2011-09-02 17:30:35 -0700265 * Constant for {@link #setPasswordQuality}: the policy allows for low-security biometric
266 * recognition technology. This implies technologies that can recognize the identity of
267 * an individual to about a 3 digit PIN (false detection is less than 1 in 1,000).
268 * Note that quality constants are ordered so that higher values are more restrictive.
269 */
270 public static final int PASSWORD_QUALITY_BIOMETRIC_WEAK = 0x8000;
271
272 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800273 * Constant for {@link #setPasswordQuality}: the policy requires some kind
274 * of password, but doesn't care what it is. Note that quality constants
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800275 * are ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800276 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800277 public static final int PASSWORD_QUALITY_SOMETHING = 0x10000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700278
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800279 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800280 * Constant for {@link #setPasswordQuality}: the user must have entered a
281 * password containing at least numeric characters. Note that quality
282 * constants are ordered so that higher values are more restrictive.
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800283 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800284 public static final int PASSWORD_QUALITY_NUMERIC = 0x20000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700285
Dianne Hackbornd6847842010-01-12 18:14:19 -0800286 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800287 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700288 * password containing at least alphabetic (or other symbol) characters.
289 * Note that quality constants are ordered so that higher values are more
290 * restrictive.
291 */
292 public static final int PASSWORD_QUALITY_ALPHABETIC = 0x40000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700293
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700294 /**
295 * Constant for {@link #setPasswordQuality}: the user must have entered a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800296 * password containing at least <em>both></em> numeric <em>and</em>
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700297 * alphabetic (or other symbol) characters. Note that quality constants are
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800298 * ordered so that higher values are more restrictive.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800299 */
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700300 public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x50000;
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700301
Dianne Hackbornd6847842010-01-12 18:14:19 -0800302 /**
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700303 * Constant for {@link #setPasswordQuality}: the user must have entered a
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700304 * password containing at least a letter, a numerical digit and a special
305 * symbol, by default. With this password quality, passwords can be
306 * restricted to contain various sets of characters, like at least an
307 * uppercase letter, etc. These are specified using various methods,
308 * like {@link #setPasswordMinimumLowerCase(ComponentName, int)}. Note
309 * that quality constants are ordered so that higher values are more
310 * restrictive.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700311 */
312 public static final int PASSWORD_QUALITY_COMPLEX = 0x60000;
313
314 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800315 * Called by an application that is administering the device to set the
316 * password restrictions it is imposing. After setting this, the user
317 * will not be able to enter a new password that is not at least as
318 * restrictive as what has been set. Note that the current password
319 * will remain until the user has set a new one, so the change does not
320 * take place immediately. To prompt the user for a new password, use
321 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700322 *
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800323 * <p>Quality constants are ordered so that higher values are more restrictive;
324 * thus the highest requested quality constant (between the policy set here,
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800325 * the user's preference, and any other considerations) is the one that
326 * is in effect.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700327 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800328 * <p>The calling device admin must have requested
329 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
330 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700331 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800332 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800333 * @param quality The new desired quality. One of
334 * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING},
Dianne Hackborn85f2c9c2010-03-22 11:12:48 -0700335 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC},
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700336 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} or {@link #PASSWORD_QUALITY_COMPLEX}.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800337 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800338 public void setPasswordQuality(ComponentName admin, int quality) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800339 if (mService != null) {
340 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700341 mService.setPasswordQuality(admin, quality, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800342 } catch (RemoteException e) {
343 Log.w(TAG, "Failed talking with device policy service", e);
344 }
345 }
346 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700347
Dianne Hackbornd6847842010-01-12 18:14:19 -0800348 /**
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800349 * Retrieve the current minimum password quality for all admins
Dianne Hackborn254cb442010-01-27 19:23:59 -0800350 * or a particular one.
351 * @param admin The name of the admin component to check, or null to aggregate
352 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800353 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800354 public int getPasswordQuality(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700355 return getPasswordQuality(admin, UserHandle.myUserId());
356 }
357
358 /** @hide per-user version */
359 public int getPasswordQuality(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800360 if (mService != null) {
361 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700362 return mService.getPasswordQuality(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800363 } catch (RemoteException e) {
364 Log.w(TAG, "Failed talking with device policy service", e);
365 }
366 }
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800367 return PASSWORD_QUALITY_UNSPECIFIED;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800368 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700369
Dianne Hackbornd6847842010-01-12 18:14:19 -0800370 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800371 * Called by an application that is administering the device to set the
372 * minimum allowed password length. After setting this, the user
373 * will not be able to enter a new password that is not at least as
374 * restrictive as what has been set. Note that the current password
375 * will remain until the user has set a new one, so the change does not
376 * take place immediately. To prompt the user for a new password, use
377 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
378 * constraint is only imposed if the administrator has also requested either
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700379 * {@link #PASSWORD_QUALITY_NUMERIC}, {@link #PASSWORD_QUALITY_ALPHABETIC}
380 * {@link #PASSWORD_QUALITY_ALPHANUMERIC}, or {@link #PASSWORD_QUALITY_COMPLEX}
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800381 * with {@link #setPasswordQuality}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700382 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800383 * <p>The calling device admin must have requested
384 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
385 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700386 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800387 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800388 * @param length The new desired minimum password length. A value of 0
389 * means there is no restriction.
390 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800391 public void setPasswordMinimumLength(ComponentName admin, int length) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800392 if (mService != null) {
393 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700394 mService.setPasswordMinimumLength(admin, length, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800395 } catch (RemoteException e) {
396 Log.w(TAG, "Failed talking with device policy service", e);
397 }
398 }
399 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700400
Dianne Hackbornd6847842010-01-12 18:14:19 -0800401 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800402 * Retrieve the current minimum password length for all admins
403 * or a particular one.
404 * @param admin The name of the admin component to check, or null to aggregate
405 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800406 */
Dianne Hackborn254cb442010-01-27 19:23:59 -0800407 public int getPasswordMinimumLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700408 return getPasswordMinimumLength(admin, UserHandle.myUserId());
409 }
410
411 /** @hide per-user version */
412 public int getPasswordMinimumLength(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800413 if (mService != null) {
414 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700415 return mService.getPasswordMinimumLength(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800416 } catch (RemoteException e) {
417 Log.w(TAG, "Failed talking with device policy service", e);
418 }
419 }
420 return 0;
421 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700422
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700423 /**
424 * Called by an application that is administering the device to set the
425 * minimum number of upper case letters required in the password. After
426 * setting this, the user will not be able to enter a new password that is
427 * not at least as restrictive as what has been set. Note that the current
428 * password will remain until the user has set a new one, so the change does
429 * not take place immediately. To prompt the user for a new password, use
430 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
431 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700432 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
433 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700434 * <p>
435 * The calling device admin must have requested
436 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
437 * this method; if it has not, a security exception will be thrown.
438 *
439 * @param admin Which {@link DeviceAdminReceiver} this request is associated
440 * with.
441 * @param length The new desired minimum number of upper case letters
442 * required in the password. A value of 0 means there is no
443 * restriction.
444 */
445 public void setPasswordMinimumUpperCase(ComponentName admin, int length) {
446 if (mService != null) {
447 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700448 mService.setPasswordMinimumUpperCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700449 } catch (RemoteException e) {
450 Log.w(TAG, "Failed talking with device policy service", e);
451 }
452 }
453 }
454
455 /**
456 * Retrieve the current number of upper case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700457 * password for all admins or a particular one. This is the same value as
458 * set by {#link {@link #setPasswordMinimumUpperCase(ComponentName, int)}
459 * and only applies when the password quality is
460 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700461 *
462 * @param admin The name of the admin component to check, or null to
463 * aggregate all admins.
464 * @return The minimum number of upper case letters required in the
465 * password.
466 */
467 public int getPasswordMinimumUpperCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700468 return getPasswordMinimumUpperCase(admin, UserHandle.myUserId());
469 }
470
471 /** @hide per-user version */
472 public int getPasswordMinimumUpperCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700473 if (mService != null) {
474 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700475 return mService.getPasswordMinimumUpperCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700476 } catch (RemoteException e) {
477 Log.w(TAG, "Failed talking with device policy service", e);
478 }
479 }
480 return 0;
481 }
482
483 /**
484 * Called by an application that is administering the device to set the
485 * minimum number of lower case letters required in the password. After
486 * setting this, the user will not be able to enter a new password that is
487 * not at least as restrictive as what has been set. Note that the current
488 * password will remain until the user has set a new one, so the change does
489 * not take place immediately. To prompt the user for a new password, use
490 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
491 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700492 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
493 * default value is 0.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700494 * <p>
495 * The calling device admin must have requested
496 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
497 * this method; if it has not, a security exception will be thrown.
498 *
499 * @param admin Which {@link DeviceAdminReceiver} this request is associated
500 * with.
501 * @param length The new desired minimum number of lower case letters
502 * required in the password. A value of 0 means there is no
503 * restriction.
504 */
505 public void setPasswordMinimumLowerCase(ComponentName admin, int length) {
506 if (mService != null) {
507 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700508 mService.setPasswordMinimumLowerCase(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700509 } catch (RemoteException e) {
510 Log.w(TAG, "Failed talking with device policy service", e);
511 }
512 }
513 }
514
515 /**
516 * Retrieve the current number of lower case letters required in the
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700517 * password for all admins or a particular one. This is the same value as
518 * set by {#link {@link #setPasswordMinimumLowerCase(ComponentName, int)}
519 * and only applies when the password quality is
520 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700521 *
522 * @param admin The name of the admin component to check, or null to
523 * aggregate all admins.
524 * @return The minimum number of lower case letters required in the
525 * password.
526 */
527 public int getPasswordMinimumLowerCase(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700528 return getPasswordMinimumLowerCase(admin, UserHandle.myUserId());
529 }
530
531 /** @hide per-user version */
532 public int getPasswordMinimumLowerCase(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700533 if (mService != null) {
534 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700535 return mService.getPasswordMinimumLowerCase(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700536 } catch (RemoteException e) {
537 Log.w(TAG, "Failed talking with device policy service", e);
538 }
539 }
540 return 0;
541 }
542
543 /**
544 * Called by an application that is administering the device to set the
545 * minimum number of letters required in the password. After setting this,
546 * the user will not be able to enter a new password that is not at least as
547 * restrictive as what has been set. Note that the current password will
548 * remain until the user has set a new one, so the change does not take
549 * place immediately. To prompt the user for a new password, use
550 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
551 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700552 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
553 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700554 * <p>
555 * The calling device admin must have requested
556 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
557 * this method; if it has not, a security exception will be thrown.
558 *
559 * @param admin Which {@link DeviceAdminReceiver} this request is associated
560 * with.
561 * @param length The new desired minimum number of letters required in the
562 * password. A value of 0 means there is no restriction.
563 */
564 public void setPasswordMinimumLetters(ComponentName admin, int length) {
565 if (mService != null) {
566 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700567 mService.setPasswordMinimumLetters(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700568 } catch (RemoteException e) {
569 Log.w(TAG, "Failed talking with device policy service", e);
570 }
571 }
572 }
573
574 /**
575 * Retrieve the current number of letters required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700576 * admins or a particular one. This is the same value as
577 * set by {#link {@link #setPasswordMinimumLetters(ComponentName, int)}
578 * and only applies when the password quality is
579 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700580 *
581 * @param admin The name of the admin component to check, or null to
582 * aggregate all admins.
583 * @return The minimum number of letters required in the password.
584 */
585 public int getPasswordMinimumLetters(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700586 return getPasswordMinimumLetters(admin, UserHandle.myUserId());
587 }
588
589 /** @hide per-user version */
590 public int getPasswordMinimumLetters(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700591 if (mService != null) {
592 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700593 return mService.getPasswordMinimumLetters(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700594 } catch (RemoteException e) {
595 Log.w(TAG, "Failed talking with device policy service", e);
596 }
597 }
598 return 0;
599 }
600
601 /**
602 * Called by an application that is administering the device to set the
603 * minimum number of numerical digits required in the password. After
604 * setting this, the user will not be able to enter a new password that is
605 * not at least as restrictive as what has been set. Note that the current
606 * password will remain until the user has set a new one, so the change does
607 * not take place immediately. To prompt the user for a new password, use
608 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
609 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700610 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
611 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700612 * <p>
613 * The calling device admin must have requested
614 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
615 * this method; if it has not, a security exception will be thrown.
616 *
617 * @param admin Which {@link DeviceAdminReceiver} this request is associated
618 * with.
619 * @param length The new desired minimum number of numerical digits required
620 * in the password. A value of 0 means there is no restriction.
621 */
622 public void setPasswordMinimumNumeric(ComponentName admin, int length) {
623 if (mService != null) {
624 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700625 mService.setPasswordMinimumNumeric(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700626 } catch (RemoteException e) {
627 Log.w(TAG, "Failed talking with device policy service", e);
628 }
629 }
630 }
631
632 /**
633 * Retrieve the current number of numerical digits required in the password
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700634 * for all admins or a particular one. This is the same value as
635 * set by {#link {@link #setPasswordMinimumNumeric(ComponentName, int)}
636 * and only applies when the password quality is
637 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700638 *
639 * @param admin The name of the admin component to check, or null to
640 * aggregate all admins.
641 * @return The minimum number of numerical digits required in the password.
642 */
643 public int getPasswordMinimumNumeric(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700644 return getPasswordMinimumNumeric(admin, UserHandle.myUserId());
645 }
646
647 /** @hide per-user version */
648 public int getPasswordMinimumNumeric(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700649 if (mService != null) {
650 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700651 return mService.getPasswordMinimumNumeric(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700652 } catch (RemoteException e) {
653 Log.w(TAG, "Failed talking with device policy service", e);
654 }
655 }
656 return 0;
657 }
658
659 /**
660 * Called by an application that is administering the device to set the
661 * minimum number of symbols required in the password. After setting this,
662 * the user will not be able to enter a new password that is not at least as
663 * restrictive as what has been set. Note that the current password will
664 * remain until the user has set a new one, so the change does not take
665 * place immediately. To prompt the user for a new password, use
666 * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This
667 * constraint is only imposed if the administrator has also requested
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700668 * {@link #PASSWORD_QUALITY_COMPLEX} with {@link #setPasswordQuality}. The
669 * default value is 1.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700670 * <p>
671 * The calling device admin must have requested
672 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
673 * this method; if it has not, a security exception will be thrown.
674 *
675 * @param admin Which {@link DeviceAdminReceiver} this request is associated
676 * with.
677 * @param length The new desired minimum number of symbols required in the
678 * password. A value of 0 means there is no restriction.
679 */
680 public void setPasswordMinimumSymbols(ComponentName admin, int length) {
681 if (mService != null) {
682 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700683 mService.setPasswordMinimumSymbols(admin, length, UserHandle.myUserId());
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700684 } catch (RemoteException e) {
685 Log.w(TAG, "Failed talking with device policy service", e);
686 }
687 }
688 }
689
690 /**
691 * Retrieve the current number of symbols required in the password for all
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700692 * admins or a particular one. This is the same value as
693 * set by {#link {@link #setPasswordMinimumSymbols(ComponentName, int)}
694 * and only applies when the password quality is
695 * {@link #PASSWORD_QUALITY_COMPLEX}.
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700696 *
697 * @param admin The name of the admin component to check, or null to
698 * aggregate all admins.
699 * @return The minimum number of symbols required in the password.
700 */
701 public int getPasswordMinimumSymbols(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700702 return getPasswordMinimumSymbols(admin, UserHandle.myUserId());
703 }
704
705 /** @hide per-user version */
706 public int getPasswordMinimumSymbols(ComponentName admin, int userHandle) {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700707 if (mService != null) {
708 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700709 return mService.getPasswordMinimumSymbols(admin, userHandle);
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -0700710 } catch (RemoteException e) {
711 Log.w(TAG, "Failed talking with device policy service", e);
712 }
713 }
714 return 0;
715 }
716
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700717 /**
718 * Called by an application that is administering the device to set the
719 * minimum number of non-letter characters (numerical digits or symbols)
720 * required in the password. After setting this, the user will not be able
721 * to enter a new password that is not at least as restrictive as what has
722 * been set. Note that the current password will remain until the user has
723 * set a new one, so the change does not take place immediately. To prompt
724 * the user for a new password, use {@link #ACTION_SET_NEW_PASSWORD} after
725 * setting this value. This constraint is only imposed if the administrator
726 * has also requested {@link #PASSWORD_QUALITY_COMPLEX} with
727 * {@link #setPasswordQuality}. The default value is 0.
728 * <p>
729 * The calling device admin must have requested
730 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
731 * this method; if it has not, a security exception will be thrown.
732 *
733 * @param admin Which {@link DeviceAdminReceiver} this request is associated
734 * with.
735 * @param length The new desired minimum number of letters required in the
736 * password. A value of 0 means there is no restriction.
737 */
738 public void setPasswordMinimumNonLetter(ComponentName admin, int length) {
739 if (mService != null) {
740 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700741 mService.setPasswordMinimumNonLetter(admin, length, UserHandle.myUserId());
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700742 } catch (RemoteException e) {
743 Log.w(TAG, "Failed talking with device policy service", e);
744 }
745 }
746 }
747
748 /**
749 * Retrieve the current number of non-letter characters required in the
750 * password for all admins or a particular one. This is the same value as
751 * set by {#link {@link #setPasswordMinimumNonLetter(ComponentName, int)}
752 * and only applies when the password quality is
753 * {@link #PASSWORD_QUALITY_COMPLEX}.
754 *
755 * @param admin The name of the admin component to check, or null to
756 * aggregate all admins.
757 * @return The minimum number of letters required in the password.
758 */
759 public int getPasswordMinimumNonLetter(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700760 return getPasswordMinimumNonLetter(admin, UserHandle.myUserId());
761 }
762
763 /** @hide per-user version */
764 public int getPasswordMinimumNonLetter(ComponentName admin, int userHandle) {
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700765 if (mService != null) {
766 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700767 return mService.getPasswordMinimumNonLetter(admin, userHandle);
Konstantin Lopyrevc8577402010-06-04 17:15:02 -0700768 } catch (RemoteException e) {
769 Log.w(TAG, "Failed talking with device policy service", e);
770 }
771 }
772 return 0;
773 }
774
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700775 /**
776 * Called by an application that is administering the device to set the length
777 * of the password history. After setting this, the user will not be able to
778 * enter a new password that is the same as any password in the history. Note
779 * that the current password will remain until the user has set a new one, so
780 * the change does not take place immediately. To prompt the user for a new
781 * password, use {@link #ACTION_SET_NEW_PASSWORD} after setting this value.
782 * This constraint is only imposed if the administrator has also requested
783 * either {@link #PASSWORD_QUALITY_NUMERIC},
784 * {@link #PASSWORD_QUALITY_ALPHABETIC}, or
785 * {@link #PASSWORD_QUALITY_ALPHANUMERIC} with {@link #setPasswordQuality}.
786 *
787 * <p>
788 * The calling device admin must have requested
789 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call this
790 * method; if it has not, a security exception will be thrown.
791 *
792 * @param admin Which {@link DeviceAdminReceiver} this request is associated
793 * with.
794 * @param length The new desired length of password history. A value of 0
795 * means there is no restriction.
796 */
797 public void setPasswordHistoryLength(ComponentName admin, int length) {
798 if (mService != null) {
799 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700800 mService.setPasswordHistoryLength(admin, length, UserHandle.myUserId());
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700801 } catch (RemoteException e) {
802 Log.w(TAG, "Failed talking with device policy service", e);
803 }
804 }
805 }
806
807 /**
Jim Millera4e28d12010-11-08 16:15:47 -0800808 * Called by a device admin to set the password expiration timeout. Calling this method
809 * will restart the countdown for password expiration for the given admin, as will changing
810 * the device password (for all admins).
811 *
812 * <p>The provided timeout is the time delta in ms and will be added to the current time.
813 * For example, to have the password expire 5 days from now, timeout would be
814 * 5 * 86400 * 1000 = 432000000 ms for timeout.
815 *
816 * <p>To disable password expiration, a value of 0 may be used for timeout.
817 *
Jim Millera4e28d12010-11-08 16:15:47 -0800818 * <p>The calling device admin must have requested
819 * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to be able to call this
820 * method; if it has not, a security exception will be thrown.
821 *
822 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
823 * @param timeout The limit (in ms) that a password can remain in effect. A value of 0
824 * means there is no restriction (unlimited).
825 */
826 public void setPasswordExpirationTimeout(ComponentName admin, long timeout) {
827 if (mService != null) {
828 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700829 mService.setPasswordExpirationTimeout(admin, timeout, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800830 } catch (RemoteException e) {
831 Log.w(TAG, "Failed talking with device policy service", e);
832 }
833 }
834 }
835
836 /**
Jim Miller6b857682011-02-16 16:27:41 -0800837 * Get the password expiration timeout for the given admin. The expiration timeout is the
838 * recurring expiration timeout provided in the call to
839 * {@link #setPasswordExpirationTimeout(ComponentName, long)} for the given admin or the
840 * aggregate of all policy administrators if admin is null.
Jim Millera4e28d12010-11-08 16:15:47 -0800841 *
842 * @param admin The name of the admin component to check, or null to aggregate all admins.
843 * @return The timeout for the given admin or the minimum of all timeouts
844 */
845 public long getPasswordExpirationTimeout(ComponentName admin) {
846 if (mService != null) {
847 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700848 return mService.getPasswordExpirationTimeout(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800849 } catch (RemoteException e) {
850 Log.w(TAG, "Failed talking with device policy service", e);
851 }
852 }
853 return 0;
854 }
855
856 /**
857 * Get the current password expiration time for the given admin or an aggregate of
Jim Miller6b857682011-02-16 16:27:41 -0800858 * all admins if admin is null. If the password is expired, this will return the time since
859 * the password expired as a negative number. If admin is null, then a composite of all
860 * expiration timeouts is returned - which will be the minimum of all timeouts.
Jim Millera4e28d12010-11-08 16:15:47 -0800861 *
862 * @param admin The name of the admin component to check, or null to aggregate all admins.
863 * @return The password expiration time, in ms.
864 */
865 public long getPasswordExpiration(ComponentName admin) {
866 if (mService != null) {
867 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700868 return mService.getPasswordExpiration(admin, UserHandle.myUserId());
Jim Millera4e28d12010-11-08 16:15:47 -0800869 } catch (RemoteException e) {
870 Log.w(TAG, "Failed talking with device policy service", e);
871 }
872 }
873 return 0;
874 }
875
876 /**
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700877 * Retrieve the current password history length for all admins
878 * or a particular one.
879 * @param admin The name of the admin component to check, or null to aggregate
880 * all admins.
881 * @return The length of the password history
882 */
883 public int getPasswordHistoryLength(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700884 return getPasswordHistoryLength(admin, UserHandle.myUserId());
885 }
886
887 /** @hide per-user version */
888 public int getPasswordHistoryLength(ComponentName admin, int userHandle) {
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700889 if (mService != null) {
890 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700891 return mService.getPasswordHistoryLength(admin, userHandle);
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700892 } catch (RemoteException e) {
893 Log.w(TAG, "Failed talking with device policy service", e);
894 }
895 }
896 return 0;
897 }
898
Dianne Hackbornd6847842010-01-12 18:14:19 -0800899 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800900 * Return the maximum password length that the device supports for a
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800901 * particular password quality.
Dianne Hackborn364f6e32010-01-29 17:38:20 -0800902 * @param quality The quality being interrogated.
Dianne Hackborn254cb442010-01-27 19:23:59 -0800903 * @return Returns the maximum length that the user can enter.
904 */
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800905 public int getPasswordMaximumLength(int quality) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800906 // Kind-of arbitrary.
907 return 16;
908 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700909
Dianne Hackborn254cb442010-01-27 19:23:59 -0800910 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800911 * Determine whether the current password the user has set is sufficient
Dianne Hackborn9327f4f2010-01-29 10:38:29 -0800912 * to meet the policy requirements (quality, minimum length) that have been
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800913 * requested.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700914 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800915 * <p>The calling device admin must have requested
916 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call
917 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700918 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800919 * @return Returns true if the password meets the current requirements,
920 * else false.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800921 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800922 public boolean isActivePasswordSufficient() {
Dianne Hackbornd6847842010-01-12 18:14:19 -0800923 if (mService != null) {
924 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700925 return mService.isActivePasswordSufficient(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800926 } catch (RemoteException e) {
927 Log.w(TAG, "Failed talking with device policy service", e);
928 }
929 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800930 return false;
Dianne Hackbornd6847842010-01-12 18:14:19 -0800931 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700932
Dianne Hackbornd6847842010-01-12 18:14:19 -0800933 /**
934 * Retrieve the number of times the user has failed at entering a
935 * password since that last successful password entry.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700936 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800937 * <p>The calling device admin must have requested
938 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call
939 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800940 */
941 public int getCurrentFailedPasswordAttempts() {
942 if (mService != null) {
943 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700944 return mService.getCurrentFailedPasswordAttempts(UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -0800945 } catch (RemoteException e) {
946 Log.w(TAG, "Failed talking with device policy service", e);
947 }
948 }
949 return -1;
950 }
Dianne Hackborndf83afa2010-01-20 13:37:26 -0800951
952 /**
Andrew Stadler88209d12010-02-08 22:59:36 -0800953 * Setting this to a value greater than zero enables a built-in policy
954 * that will perform a device wipe after too many incorrect
955 * device-unlock passwords have been entered. This built-in policy combines
956 * watching for failed passwords and wiping the device, and requires
957 * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800958 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700959 *
Andrew Stadler88209d12010-02-08 22:59:36 -0800960 * <p>To implement any other policy (e.g. wiping data for a particular
961 * application only, erasing or revoking credentials, or reporting the
962 * failure to a server), you should implement
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800963 * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)}
Andrew Stadler88209d12010-02-08 22:59:36 -0800964 * instead. Do not use this API, because if the maximum count is reached,
965 * the device will be wiped immediately, and your callback will not be invoked.
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700966 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800967 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800968 * @param num The number of failed password attempts at which point the
969 * device will wipe its data.
970 */
971 public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) {
972 if (mService != null) {
973 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700974 mService.setMaximumFailedPasswordsForWipe(admin, num, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800975 } catch (RemoteException e) {
976 Log.w(TAG, "Failed talking with device policy service", e);
977 }
978 }
979 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -0700980
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800981 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -0800982 * Retrieve the current maximum number of login attempts that are allowed
983 * before the device wipes itself, for all admins
984 * or a particular one.
985 * @param admin The name of the admin component to check, or null to aggregate
986 * all admins.
987 */
988 public int getMaximumFailedPasswordsForWipe(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700989 return getMaximumFailedPasswordsForWipe(admin, UserHandle.myUserId());
990 }
991
992 /** @hide per-user version */
993 public int getMaximumFailedPasswordsForWipe(ComponentName admin, int userHandle) {
Dianne Hackborn254cb442010-01-27 19:23:59 -0800994 if (mService != null) {
995 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -0700996 return mService.getMaximumFailedPasswordsForWipe(admin, userHandle);
Dianne Hackborn254cb442010-01-27 19:23:59 -0800997 } catch (RemoteException e) {
998 Log.w(TAG, "Failed talking with device policy service", e);
999 }
1000 }
1001 return 0;
1002 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001003
Dianne Hackborn254cb442010-01-27 19:23:59 -08001004 /**
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001005 * Flag for {@link #resetPassword}: don't allow other admins to change
1006 * the password again until the user has entered it.
1007 */
1008 public static final int RESET_PASSWORD_REQUIRE_ENTRY = 0x0001;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001009
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001010 /**
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001011 * Force a new device unlock password (the password needed to access the
1012 * entire device, not for individual accounts) on the user. This takes
1013 * effect immediately.
Dianne Hackborn9327f4f2010-01-29 10:38:29 -08001014 * The given password must be sufficient for the
1015 * current password quality and length constraints as returned by
1016 * {@link #getPasswordQuality(ComponentName)} and
1017 * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet
1018 * these constraints, then it will be rejected and false returned. Note
1019 * that the password may be a stronger quality (containing alphanumeric
1020 * characters when the requested quality is only numeric), in which case
1021 * the currently active quality will be increased to match.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001022 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001023 * <p>The calling device admin must have requested
1024 * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call
1025 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001026 *
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001027 * @param password The new password for the user.
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001028 * @param flags May be 0 or {@link #RESET_PASSWORD_REQUIRE_ENTRY}.
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001029 * @return Returns true if the password was applied, or false if it is
1030 * not acceptable for the current constraints.
1031 */
Dianne Hackborn87bba1e2010-02-26 17:25:54 -08001032 public boolean resetPassword(String password, int flags) {
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001033 if (mService != null) {
1034 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001035 return mService.resetPassword(password, flags, UserHandle.myUserId());
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001036 } catch (RemoteException e) {
1037 Log.w(TAG, "Failed talking with device policy service", e);
1038 }
1039 }
1040 return false;
1041 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001042
Dianne Hackbornd6847842010-01-12 18:14:19 -08001043 /**
1044 * Called by an application that is administering the device to set the
1045 * maximum time for user activity until the device will lock. This limits
1046 * the length that the user can set. It takes effect immediately.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001047 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001048 * <p>The calling device admin must have requested
Dianne Hackborn315ada72010-02-11 12:14:08 -08001049 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001050 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001051 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -08001052 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001053 * @param timeMs The new desired maximum time to lock in milliseconds.
1054 * A value of 0 means there is no restriction.
1055 */
1056 public void setMaximumTimeToLock(ComponentName admin, long timeMs) {
1057 if (mService != null) {
1058 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001059 mService.setMaximumTimeToLock(admin, timeMs, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001060 } catch (RemoteException e) {
1061 Log.w(TAG, "Failed talking with device policy service", e);
1062 }
1063 }
1064 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001065
Dianne Hackbornd6847842010-01-12 18:14:19 -08001066 /**
Dianne Hackborn254cb442010-01-27 19:23:59 -08001067 * Retrieve the current maximum time to unlock for all admins
1068 * or a particular one.
1069 * @param admin The name of the admin component to check, or null to aggregate
1070 * all admins.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001071 */
Dianne Hackborn254cb442010-01-27 19:23:59 -08001072 public long getMaximumTimeToLock(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001073 return getMaximumTimeToLock(admin, UserHandle.myUserId());
1074 }
1075
1076 /** @hide per-user version */
1077 public long getMaximumTimeToLock(ComponentName admin, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001078 if (mService != null) {
1079 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001080 return mService.getMaximumTimeToLock(admin, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001081 } catch (RemoteException e) {
1082 Log.w(TAG, "Failed talking with device policy service", e);
1083 }
1084 }
1085 return 0;
1086 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001087
Dianne Hackbornd6847842010-01-12 18:14:19 -08001088 /**
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001089 * Make the device lock immediately, as if the lock screen timeout has
1090 * expired at the point of this call.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001091 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001092 * <p>The calling device admin must have requested
1093 * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call
1094 * this method; if it has not, a security exception will be thrown.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001095 */
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001096 public void lockNow() {
1097 if (mService != null) {
1098 try {
1099 mService.lockNow();
1100 } catch (RemoteException e) {
1101 Log.w(TAG, "Failed talking with device policy service", e);
1102 }
1103 }
1104 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001105
Dianne Hackbornd6847842010-01-12 18:14:19 -08001106 /**
Dianne Hackborn42499172010-10-15 18:45:07 -07001107 * Flag for {@link #wipeData(int)}: also erase the device's external
1108 * storage.
1109 */
1110 public static final int WIPE_EXTERNAL_STORAGE = 0x0001;
1111
1112 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001113 * Ask the user date be wiped. This will cause the device to reboot,
Dianne Hackborndf83afa2010-01-20 13:37:26 -08001114 * erasing all user data while next booting up. External storage such
Masanori Oginof535cb042012-02-15 19:25:50 +09001115 * as SD cards will be also erased if the flag {@link #WIPE_EXTERNAL_STORAGE}
1116 * is set.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001117 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -08001118 * <p>The calling device admin must have requested
1119 * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call
1120 * this method; if it has not, a security exception will be thrown.
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001121 *
Masanori Oginof535cb042012-02-15 19:25:50 +09001122 * @param flags Bit mask of additional options: currently 0 and
1123 * {@link #WIPE_EXTERNAL_STORAGE} are supported.
Dianne Hackbornd6847842010-01-12 18:14:19 -08001124 */
1125 public void wipeData(int flags) {
1126 if (mService != null) {
1127 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001128 mService.wipeData(flags, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001129 } catch (RemoteException e) {
1130 Log.w(TAG, "Failed talking with device policy service", e);
1131 }
1132 }
1133 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001134
Dianne Hackbornd6847842010-01-12 18:14:19 -08001135 /**
Oscar Montemayor69238c62010-08-03 10:51:06 -07001136 * Called by an application that is administering the device to set the
1137 * global proxy and exclusion list.
1138 * <p>
1139 * The calling device admin must have requested
1140 * {@link DeviceAdminInfo#USES_POLICY_SETS_GLOBAL_PROXY} to be able to call
1141 * this method; if it has not, a security exception will be thrown.
1142 * Only the first device admin can set the proxy. If a second admin attempts
1143 * to set the proxy, the {@link ComponentName} of the admin originally setting the
1144 * proxy will be returned. If successful in setting the proxy, null will
1145 * be returned.
1146 * The method can be called repeatedly by the device admin alrady setting the
1147 * proxy to update the proxy and exclusion list.
1148 *
1149 * @param admin Which {@link DeviceAdminReceiver} this request is associated
1150 * with.
1151 * @param proxySpec the global proxy desired. Must be an HTTP Proxy.
1152 * Pass Proxy.NO_PROXY to reset the proxy.
1153 * @param exclusionList a list of domains to be excluded from the global proxy.
Oscar Montemayor69238c62010-08-03 10:51:06 -07001154 * @return returns null if the proxy was successfully set, or a {@link ComponentName}
1155 * of the device admin that sets thew proxy otherwise.
Andy Stadlerd2672722011-02-16 10:53:33 -08001156 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001157 */
1158 public ComponentName setGlobalProxy(ComponentName admin, Proxy proxySpec,
1159 List<String> exclusionList ) {
1160 if (proxySpec == null) {
1161 throw new NullPointerException();
1162 }
1163 if (mService != null) {
1164 try {
1165 String hostSpec;
1166 String exclSpec;
1167 if (proxySpec.equals(Proxy.NO_PROXY)) {
1168 hostSpec = null;
1169 exclSpec = null;
1170 } else {
1171 if (!proxySpec.type().equals(Proxy.Type.HTTP)) {
1172 throw new IllegalArgumentException();
1173 }
1174 InetSocketAddress sa = (InetSocketAddress)proxySpec.address();
1175 String hostName = sa.getHostName();
1176 int port = sa.getPort();
1177 StringBuilder hostBuilder = new StringBuilder();
1178 hostSpec = hostBuilder.append(hostName)
1179 .append(":").append(Integer.toString(port)).toString();
1180 if (exclusionList == null) {
1181 exclSpec = "";
1182 } else {
1183 StringBuilder listBuilder = new StringBuilder();
1184 boolean firstDomain = true;
1185 for (String exclDomain : exclusionList) {
1186 if (!firstDomain) {
1187 listBuilder = listBuilder.append(",");
1188 } else {
1189 firstDomain = false;
1190 }
1191 listBuilder = listBuilder.append(exclDomain.trim());
1192 }
1193 exclSpec = listBuilder.toString();
1194 }
Yuhao Zheng90704842014-02-28 17:22:45 -08001195 if (android.net.Proxy.validate(hostName, Integer.toString(port), exclSpec)
1196 != android.net.Proxy.PROXY_VALID)
1197 throw new IllegalArgumentException();
Oscar Montemayor69238c62010-08-03 10:51:06 -07001198 }
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001199 return mService.setGlobalProxy(admin, hostSpec, exclSpec, UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001200 } catch (RemoteException e) {
1201 Log.w(TAG, "Failed talking with device policy service", e);
1202 }
1203 }
1204 return null;
1205 }
1206
1207 /**
1208 * Returns the component name setting the global proxy.
1209 * @return ComponentName object of the device admin that set the global proxy, or
1210 * null if no admin has set the proxy.
Andy Stadlerd2672722011-02-16 10:53:33 -08001211 * @hide
Oscar Montemayor69238c62010-08-03 10:51:06 -07001212 */
1213 public ComponentName getGlobalProxyAdmin() {
1214 if (mService != null) {
1215 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001216 return mService.getGlobalProxyAdmin(UserHandle.myUserId());
Oscar Montemayor69238c62010-08-03 10:51:06 -07001217 } catch (RemoteException e) {
1218 Log.w(TAG, "Failed talking with device policy service", e);
1219 }
1220 }
1221 return null;
1222 }
1223
1224 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001225 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001226 * indicating that encryption is not supported.
1227 */
1228 public static final int ENCRYPTION_STATUS_UNSUPPORTED = 0;
1229
1230 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001231 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001232 * indicating that encryption is supported, but is not currently active.
1233 */
1234 public static final int ENCRYPTION_STATUS_INACTIVE = 1;
1235
1236 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001237 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001238 * indicating that encryption is not currently active, but is currently
1239 * being activated. This is only reported by devices that support
1240 * encryption of data and only when the storage is currently
1241 * undergoing a process of becoming encrypted. A device that must reboot and/or wipe data
1242 * to become encrypted will never return this value.
1243 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001244 public static final int ENCRYPTION_STATUS_ACTIVATING = 2;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001245
1246 /**
Andy Stadler22dbfda2011-01-17 12:47:31 -08001247 * Result code for {@link #setStorageEncryption} and {@link #getStorageEncryptionStatus}:
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001248 * indicating that encryption is active.
1249 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001250 public static final int ENCRYPTION_STATUS_ACTIVE = 3;
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001251
1252 /**
1253 * Activity action: begin the process of encrypting data on the device. This activity should
1254 * be launched after using {@link #setStorageEncryption} to request encryption be activated.
1255 * After resuming from this activity, use {@link #getStorageEncryption}
1256 * to check encryption status. However, on some devices this activity may never return, as
1257 * it may trigger a reboot and in some cases a complete data wipe of the device.
1258 */
1259 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
1260 public static final String ACTION_START_ENCRYPTION
1261 = "android.app.action.START_ENCRYPTION";
1262
1263 /**
Jim Millerb8ec4702012-08-31 17:19:10 -07001264 * Widgets are enabled in keyguard
1265 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001266 public static final int KEYGUARD_DISABLE_FEATURES_NONE = 0;
Jim Millerb8ec4702012-08-31 17:19:10 -07001267
1268 /**
1269 * Disable all keyguard widgets
1270 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001271 public static final int KEYGUARD_DISABLE_WIDGETS_ALL = 1 << 0;
1272
1273 /**
1274 * Disable the camera on secure keyguard screens (e.g. PIN/Pattern/Password)
1275 */
1276 public static final int KEYGUARD_DISABLE_SECURE_CAMERA = 1 << 1;
1277
1278 /**
Jim Miller35207742012-11-02 15:33:20 -07001279 * Disable all current and future keyguard customizations.
Jim Miller48b9b0d2012-09-19 23:16:50 -07001280 */
1281 public static final int KEYGUARD_DISABLE_FEATURES_ALL = 0x7fffffff;
Jim Millerb8ec4702012-08-31 17:19:10 -07001282
1283 /**
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001284 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001285 * request that the storage system be encrypted.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001286 *
1287 * <p>When multiple device administrators attempt to control device
1288 * encryption, the most secure, supported setting will always be
1289 * used. If any device administrator requests device encryption,
1290 * it will be enabled; Conversely, if a device administrator
1291 * attempts to disable device encryption while another
1292 * device administrator has enabled it, the call to disable will
1293 * fail (most commonly returning {@link #ENCRYPTION_STATUS_ACTIVE}).
1294 *
1295 * <p>This policy controls encryption of the secure (application data) storage area. Data
Andy Stadler50c294f2011-03-07 19:13:42 -08001296 * written to other storage areas may or may not be encrypted, and this policy does not require
1297 * or control the encryption of any other storage areas.
1298 * There is one exception: If {@link android.os.Environment#isExternalStorageEmulated()} is
1299 * {@code true}, then the directory returned by
1300 * {@link android.os.Environment#getExternalStorageDirectory()} must be written to disk
1301 * within the encrypted storage area.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001302 *
1303 * <p>Important Note: On some devices, it is possible to encrypt storage without requiring
1304 * the user to create a device PIN or Password. In this case, the storage is encrypted, but
1305 * the encryption key may not be fully secured. For maximum security, the administrator should
1306 * also require (and check for) a pattern, PIN, or password.
1307 *
1308 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1309 * @param encrypt true to request encryption, false to release any previous request
Andy Stadler22dbfda2011-01-17 12:47:31 -08001310 * @return the new request status (for all active admins) - will be one of
1311 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE}, or
1312 * {@link #ENCRYPTION_STATUS_ACTIVE}. This is the value of the requests; Use
1313 * {@link #getStorageEncryptionStatus()} to query the actual device state.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001314 */
1315 public int setStorageEncryption(ComponentName admin, boolean encrypt) {
1316 if (mService != null) {
1317 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001318 return mService.setStorageEncryption(admin, encrypt, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001319 } catch (RemoteException e) {
1320 Log.w(TAG, "Failed talking with device policy service", e);
1321 }
1322 }
1323 return ENCRYPTION_STATUS_UNSUPPORTED;
1324 }
1325
1326 /**
1327 * Called by an application that is administering the device to
Andy Stadler22dbfda2011-01-17 12:47:31 -08001328 * determine the requested setting for secure storage.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001329 *
Andy Stadler22dbfda2011-01-17 12:47:31 -08001330 * @param admin Which {@link DeviceAdminReceiver} this request is associated with. If null,
1331 * this will return the requested encryption setting as an aggregate of all active
1332 * administrators.
1333 * @return true if the admin(s) are requesting encryption, false if not.
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001334 */
Andy Stadler22dbfda2011-01-17 12:47:31 -08001335 public boolean getStorageEncryption(ComponentName admin) {
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001336 if (mService != null) {
1337 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001338 return mService.getStorageEncryption(admin, UserHandle.myUserId());
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001339 } catch (RemoteException e) {
1340 Log.w(TAG, "Failed talking with device policy service", e);
1341 }
1342 }
Andy Stadler22dbfda2011-01-17 12:47:31 -08001343 return false;
1344 }
1345
1346 /**
1347 * Called by an application that is administering the device to
1348 * determine the current encryption status of the device.
1349 *
1350 * Depending on the returned status code, the caller may proceed in different
1351 * ways. If the result is {@link #ENCRYPTION_STATUS_UNSUPPORTED}, the
1352 * storage system does not support encryption. If the
1353 * result is {@link #ENCRYPTION_STATUS_INACTIVE}, use {@link
1354 * #ACTION_START_ENCRYPTION} to begin the process of encrypting or decrypting the
1355 * storage. If the result is {@link #ENCRYPTION_STATUS_ACTIVATING} or
1356 * {@link #ENCRYPTION_STATUS_ACTIVE}, no further action is required.
1357 *
1358 * @return current status of encryption. The value will be one of
1359 * {@link #ENCRYPTION_STATUS_UNSUPPORTED}, {@link #ENCRYPTION_STATUS_INACTIVE},
1360 * {@link #ENCRYPTION_STATUS_ACTIVATING}, or{@link #ENCRYPTION_STATUS_ACTIVE}.
1361 */
1362 public int getStorageEncryptionStatus() {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001363 return getStorageEncryptionStatus(UserHandle.myUserId());
1364 }
1365
1366 /** @hide per-user version */
1367 public int getStorageEncryptionStatus(int userHandle) {
Andy Stadler22dbfda2011-01-17 12:47:31 -08001368 if (mService != null) {
1369 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001370 return mService.getStorageEncryptionStatus(userHandle);
Andy Stadler22dbfda2011-01-17 12:47:31 -08001371 } catch (RemoteException e) {
1372 Log.w(TAG, "Failed talking with device policy service", e);
1373 }
1374 }
Andy Stadler7b0f8f02011-01-12 14:59:52 -08001375 return ENCRYPTION_STATUS_UNSUPPORTED;
1376 }
1377
1378 /**
Maggie Benthallda51e682013-08-08 22:35:44 -04001379 * Installs the given certificate as a User CA.
1380 *
1381 * @return false if the certBuffer cannot be parsed or installation is
1382 * interrupted, otherwise true
1383 * @hide
1384 */
1385 public boolean installCaCert(byte[] certBuffer) {
1386 if (mService != null) {
1387 try {
1388 return mService.installCaCert(certBuffer);
1389 } catch (RemoteException e) {
1390 Log.w(TAG, "Failed talking with device policy service", e);
1391 }
1392 }
1393 return false;
1394 }
1395
1396 /**
1397 * Uninstalls the given certificate from the list of User CAs, if present.
1398 *
1399 * @hide
1400 */
1401 public void uninstallCaCert(byte[] certBuffer) {
1402 if (mService != null) {
1403 try {
1404 mService.uninstallCaCert(certBuffer);
1405 } catch (RemoteException e) {
1406 Log.w(TAG, "Failed talking with device policy service", e);
1407 }
1408 }
1409 }
1410
1411 /**
1412 * Returns whether there are any user-installed CA certificates.
1413 *
1414 * @hide
1415 */
Maggie Benthall0469f412013-09-05 15:30:26 -04001416 public static boolean hasAnyCaCertsInstalled() {
Maggie Benthallda51e682013-08-08 22:35:44 -04001417 TrustedCertificateStore certStore = new TrustedCertificateStore();
1418 Set<String> aliases = certStore.userAliases();
1419 return aliases != null && !aliases.isEmpty();
1420 }
1421
1422 /**
1423 * Returns whether this certificate has been installed as a User CA.
1424 *
1425 * @hide
1426 */
1427 public boolean hasCaCertInstalled(byte[] certBuffer) {
1428 TrustedCertificateStore certStore = new TrustedCertificateStore();
1429 String alias;
1430 byte[] pemCert;
1431 try {
1432 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
1433 X509Certificate cert = (X509Certificate) certFactory.generateCertificate(
1434 new ByteArrayInputStream(certBuffer));
1435 return certStore.getCertificateAlias(cert) != null;
1436 } catch (CertificateException ce) {
1437 Log.w(TAG, "Could not parse certificate", ce);
1438 }
1439 return false;
1440 }
1441
1442 /**
Ben Komalo2447edd2011-05-09 16:05:33 -07001443 * Called by an application that is administering the device to disable all cameras
1444 * on the device. After setting this, no applications will be able to access any cameras
1445 * on the device.
1446 *
1447 * <p>The calling device admin must have requested
1448 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_CAMERA} to be able to call
1449 * this method; if it has not, a security exception will be thrown.
1450 *
1451 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1452 * @param disabled Whether or not the camera should be disabled.
1453 */
1454 public void setCameraDisabled(ComponentName admin, boolean disabled) {
1455 if (mService != null) {
1456 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001457 mService.setCameraDisabled(admin, disabled, UserHandle.myUserId());
Ben Komalo2447edd2011-05-09 16:05:33 -07001458 } catch (RemoteException e) {
1459 Log.w(TAG, "Failed talking with device policy service", e);
1460 }
1461 }
1462 }
1463
1464 /**
1465 * Determine whether or not the device's cameras have been disabled either by the current
1466 * admin, if specified, or all admins.
1467 * @param admin The name of the admin component to check, or null to check if any admins
1468 * have disabled the camera
1469 */
1470 public boolean getCameraDisabled(ComponentName admin) {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001471 return getCameraDisabled(admin, UserHandle.myUserId());
1472 }
1473
1474 /** @hide per-user version */
1475 public boolean getCameraDisabled(ComponentName admin, int userHandle) {
Ben Komalo2447edd2011-05-09 16:05:33 -07001476 if (mService != null) {
1477 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001478 return mService.getCameraDisabled(admin, userHandle);
Ben Komalo2447edd2011-05-09 16:05:33 -07001479 } catch (RemoteException e) {
1480 Log.w(TAG, "Failed talking with device policy service", e);
1481 }
1482 }
1483 return false;
1484 }
1485
1486 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001487 * Called by an application that is administering the device to disable keyguard customizations,
1488 * such as widgets. After setting this, keyguard features will be disabled according to the
1489 * provided feature list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001490 *
1491 * <p>The calling device admin must have requested
Jim Miller48b9b0d2012-09-19 23:16:50 -07001492 * {@link DeviceAdminInfo#USES_POLICY_DISABLE_KEYGUARD_FEATURES} to be able to call
Jim Millerb8ec4702012-08-31 17:19:10 -07001493 * this method; if it has not, a security exception will be thrown.
1494 *
1495 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
Jim Miller35207742012-11-02 15:33:20 -07001496 * @param which {@link #KEYGUARD_DISABLE_FEATURES_NONE} (default),
1497 * {@link #KEYGUARD_DISABLE_WIDGETS_ALL}, {@link #KEYGUARD_DISABLE_SECURE_CAMERA},
1498 * {@link #KEYGUARD_DISABLE_FEATURES_ALL}
Jim Millerb8ec4702012-08-31 17:19:10 -07001499 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001500 public void setKeyguardDisabledFeatures(ComponentName admin, int which) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001501 if (mService != null) {
1502 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001503 mService.setKeyguardDisabledFeatures(admin, which, UserHandle.myUserId());
Jim Millerb8ec4702012-08-31 17:19:10 -07001504 } catch (RemoteException e) {
1505 Log.w(TAG, "Failed talking with device policy service", e);
1506 }
1507 }
1508 }
1509
1510 /**
Jim Miller48b9b0d2012-09-19 23:16:50 -07001511 * Determine whether or not features have been disabled in keyguard either by the current
Jim Millerb8ec4702012-08-31 17:19:10 -07001512 * admin, if specified, or all admins.
1513 * @param admin The name of the admin component to check, or null to check if any admins
Jim Miller48b9b0d2012-09-19 23:16:50 -07001514 * have disabled features in keyguard.
Jim Miller35207742012-11-02 15:33:20 -07001515 * @return bitfield of flags. See {@link #setKeyguardDisabledFeatures(ComponentName, int)}
1516 * for a list.
Jim Millerb8ec4702012-08-31 17:19:10 -07001517 */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001518 public int getKeyguardDisabledFeatures(ComponentName admin) {
1519 return getKeyguardDisabledFeatures(admin, UserHandle.myUserId());
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001520 }
1521
1522 /** @hide per-user version */
Jim Miller48b9b0d2012-09-19 23:16:50 -07001523 public int getKeyguardDisabledFeatures(ComponentName admin, int userHandle) {
Jim Millerb8ec4702012-08-31 17:19:10 -07001524 if (mService != null) {
1525 try {
Jim Miller48b9b0d2012-09-19 23:16:50 -07001526 return mService.getKeyguardDisabledFeatures(admin, userHandle);
Jim Millerb8ec4702012-08-31 17:19:10 -07001527 } catch (RemoteException e) {
1528 Log.w(TAG, "Failed talking with device policy service", e);
1529 }
1530 }
Jim Miller48b9b0d2012-09-19 23:16:50 -07001531 return KEYGUARD_DISABLE_FEATURES_NONE;
Jim Millerb8ec4702012-08-31 17:19:10 -07001532 }
1533
1534 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -08001535 * @hide
1536 */
Andy Stadlerc25f70a2010-12-08 15:56:45 -08001537 public void setActiveAdmin(ComponentName policyReceiver, boolean refreshing) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001538 if (mService != null) {
1539 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001540 mService.setActiveAdmin(policyReceiver, refreshing, UserHandle.myUserId());
Dianne Hackbornd6847842010-01-12 18:14:19 -08001541 } catch (RemoteException e) {
1542 Log.w(TAG, "Failed talking with device policy service", e);
1543 }
1544 }
1545 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001546
Dianne Hackbornd6847842010-01-12 18:14:19 -08001547 /**
Andy Stadlerc25f70a2010-12-08 15:56:45 -08001548 * Returns the DeviceAdminInfo as defined by the administrator's package info & meta-data
Dianne Hackbornd6847842010-01-12 18:14:19 -08001549 * @hide
1550 */
Dianne Hackbornd47c6ed2010-01-27 16:21:20 -08001551 public DeviceAdminInfo getAdminInfo(ComponentName cn) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001552 ActivityInfo ai;
1553 try {
1554 ai = mContext.getPackageManager().getReceiverInfo(cn,
1555 PackageManager.GET_META_DATA);
1556 } catch (PackageManager.NameNotFoundException e) {
1557 Log.w(TAG, "Unable to retrieve device policy " + cn, e);
1558 return null;
1559 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001560
Dianne Hackbornd6847842010-01-12 18:14:19 -08001561 ResolveInfo ri = new ResolveInfo();
1562 ri.activityInfo = ai;
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001563
Dianne Hackbornd6847842010-01-12 18:14:19 -08001564 try {
1565 return new DeviceAdminInfo(mContext, ri);
1566 } catch (XmlPullParserException e) {
1567 Log.w(TAG, "Unable to parse device policy " + cn, e);
1568 return null;
1569 } catch (IOException e) {
1570 Log.w(TAG, "Unable to parse device policy " + cn, e);
1571 return null;
1572 }
1573 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001574
Dianne Hackbornd6847842010-01-12 18:14:19 -08001575 /**
1576 * @hide
1577 */
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001578 public void getRemoveWarning(ComponentName admin, RemoteCallback result) {
1579 if (mService != null) {
1580 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001581 mService.getRemoveWarning(admin, result, UserHandle.myUserId());
Dianne Hackborn8ea138c2010-01-26 18:01:04 -08001582 } catch (RemoteException e) {
1583 Log.w(TAG, "Failed talking with device policy service", e);
1584 }
1585 }
1586 }
1587
1588 /**
1589 * @hide
1590 */
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001591 public void setActivePasswordState(int quality, int length, int letters, int uppercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001592 int lowercase, int numbers, int symbols, int nonletter, int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001593 if (mService != null) {
1594 try {
Konstantin Lopyreva15dcfa2010-05-24 17:10:56 -07001595 mService.setActivePasswordState(quality, length, letters, uppercase, lowercase,
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001596 numbers, symbols, nonletter, userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001597 } catch (RemoteException e) {
1598 Log.w(TAG, "Failed talking with device policy service", e);
1599 }
1600 }
1601 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001602
Dianne Hackbornd6847842010-01-12 18:14:19 -08001603 /**
1604 * @hide
1605 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001606 public void reportFailedPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001607 if (mService != null) {
1608 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001609 mService.reportFailedPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001610 } catch (RemoteException e) {
1611 Log.w(TAG, "Failed talking with device policy service", e);
1612 }
1613 }
1614 }
Konstantin Lopyrev32558232010-05-20 16:18:05 -07001615
Dianne Hackbornd6847842010-01-12 18:14:19 -08001616 /**
1617 * @hide
1618 */
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001619 public void reportSuccessfulPasswordAttempt(int userHandle) {
Dianne Hackbornd6847842010-01-12 18:14:19 -08001620 if (mService != null) {
1621 try {
Amith Yamasani599dd7c2012-09-14 23:20:08 -07001622 mService.reportSuccessfulPasswordAttempt(userHandle);
Dianne Hackbornd6847842010-01-12 18:14:19 -08001623 } catch (RemoteException e) {
1624 Log.w(TAG, "Failed talking with device policy service", e);
1625 }
1626 }
1627 }
Amith Yamasani71e6c692013-03-24 17:39:28 -07001628
1629 /**
1630 * @hide
1631 * Sets the given package as the device owner. The package must already be installed and there
1632 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1633 * method must be called before the device is provisioned.
1634 * @param packageName the package name of the application to be registered as the device owner.
1635 * @return whether the package was successfully registered as the device owner.
1636 * @throws IllegalArgumentException if the package name is null or invalid
1637 * @throws IllegalStateException if a device owner is already registered or the device has
1638 * already been provisioned.
1639 */
1640 public boolean setDeviceOwner(String packageName) throws IllegalArgumentException,
1641 IllegalStateException {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001642 return setDeviceOwner(packageName, null);
1643 }
1644
1645 /**
1646 * @hide
1647 * Sets the given package as the device owner. The package must already be installed and there
1648 * shouldn't be an existing device owner registered, for this call to succeed. Also, this
1649 * method must be called before the device is provisioned.
1650 * @param packageName the package name of the application to be registered as the device owner.
1651 * @param ownerName the human readable name of the institution that owns this device.
1652 * @return whether the package was successfully registered as the device owner.
1653 * @throws IllegalArgumentException if the package name is null or invalid
1654 * @throws IllegalStateException if a device owner is already registered or the device has
1655 * already been provisioned.
1656 */
1657 public boolean setDeviceOwner(String packageName, String ownerName)
1658 throws IllegalArgumentException, IllegalStateException {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001659 if (mService != null) {
1660 try {
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001661 return mService.setDeviceOwner(packageName, ownerName);
Amith Yamasani71e6c692013-03-24 17:39:28 -07001662 } catch (RemoteException re) {
1663 Log.w(TAG, "Failed to set device owner");
1664 }
1665 }
1666 return false;
1667 }
1668
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001669
Amith Yamasani71e6c692013-03-24 17:39:28 -07001670 /**
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001671 * Used to determine if a particular package has been registered as a Device Owner app.
1672 * A device owner app is a special device admin that cannot be deactivated by the user, once
1673 * activated as a device admin. It also cannot be uninstalled. To check if a particular
1674 * package is currently registered as the device owner app, pass in the package name from
1675 * {@link Context#getPackageName()} to this method.<p/>This is useful for device
1676 * admin apps that want to check if they are also registered as the device owner app. The
1677 * exact mechanism by which a device admin app is registered as a device owner app is defined by
1678 * the setup process.
1679 * @param packageName the package name of the app, to compare with the registered device owner
1680 * app, if any.
1681 * @return whether or not the package is registered as the device owner app.
Amith Yamasani71e6c692013-03-24 17:39:28 -07001682 */
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001683 public boolean isDeviceOwnerApp(String packageName) {
Amith Yamasani71e6c692013-03-24 17:39:28 -07001684 if (mService != null) {
1685 try {
1686 return mService.isDeviceOwner(packageName);
1687 } catch (RemoteException re) {
1688 Log.w(TAG, "Failed to check device owner");
1689 }
1690 }
1691 return false;
1692 }
1693
Amith Yamasani3b458ad2013-04-18 18:40:07 -07001694 /**
1695 * @hide
1696 * Redirect to isDeviceOwnerApp.
1697 */
1698 public boolean isDeviceOwner(String packageName) {
1699 return isDeviceOwnerApp(packageName);
1700 }
1701
Amith Yamasani71e6c692013-03-24 17:39:28 -07001702 /** @hide */
1703 public String getDeviceOwner() {
1704 if (mService != null) {
1705 try {
1706 return mService.getDeviceOwner();
1707 } catch (RemoteException re) {
1708 Log.w(TAG, "Failed to get device owner");
1709 }
1710 }
1711 return null;
1712 }
Geoffrey Borggaard334c7e32013-08-08 14:31:36 -04001713
1714 /** @hide */
1715 public String getDeviceOwnerName() {
1716 if (mService != null) {
1717 try {
1718 return mService.getDeviceOwnerName();
1719 } catch (RemoteException re) {
1720 Log.w(TAG, "Failed to get device owner");
1721 }
1722 }
1723 return null;
1724 }
Adam Connors776c5552014-01-09 10:42:56 +00001725
1726 /**
1727 * @hide
1728 * Sets the given package as the profile owner of the given user profile. The package must
1729 * already be installed and there shouldn't be an existing profile owner registered for this
1730 * user. Also, this method must be called before the user has been used for the first time.
1731 * @param packageName the package name of the application to be registered as profile owner.
1732 * @param ownerName the human readable name of the organisation associated with this DPM.
Adam Connors661ec472014-02-11 13:59:46 +00001733 * @param userHandle the userId to set the profile owner for.
Adam Connors776c5552014-01-09 10:42:56 +00001734 * @return whether the package was successfully registered as the profile owner.
1735 * @throws IllegalArgumentException if packageName is null, the package isn't installed, or
1736 * the user has already been set up.
1737 */
Adam Connors661ec472014-02-11 13:59:46 +00001738 public boolean setProfileOwner(String packageName, String ownerName, int userHandle)
Adam Connors776c5552014-01-09 10:42:56 +00001739 throws IllegalArgumentException {
1740 if (mService != null) {
1741 try {
Adam Connors661ec472014-02-11 13:59:46 +00001742 return mService.setProfileOwner(packageName, ownerName, userHandle);
Adam Connors776c5552014-01-09 10:42:56 +00001743 } catch (RemoteException re) {
1744 Log.w(TAG, "Failed to set profile owner", re);
1745 throw new IllegalArgumentException("Couldn't set profile owner.", re);
1746 }
1747 }
1748 return false;
1749 }
1750
1751 /**
1752 * Used to determine if a particular package is registered as the Profile Owner for the
1753 * current user. A profile owner is a special device admin that has additional priviledges
1754 * within the managed profile.
1755 *
1756 * @param packageName The package name of the app to compare with the registered profile owner.
1757 * @return Whether or not the package is registered as the profile owner.
1758 */
1759 public boolean isProfileOwnerApp(String packageName) {
1760 if (mService != null) {
1761 try {
1762 String profileOwnerPackage = mService.getProfileOwner(
1763 Process.myUserHandle().getIdentifier());
1764 return profileOwnerPackage != null && profileOwnerPackage.equals(packageName);
1765 } catch (RemoteException re) {
1766 Log.w(TAG, "Failed to check profile owner");
1767 }
1768 }
1769 return false;
1770 }
1771
1772 /**
1773 * @hide
1774 * @return the packageName of the owner of the given user profile or null if no profile
1775 * owner has been set for that user.
1776 * @throws IllegalArgumentException if the userId is invalid.
1777 */
1778 public String getProfileOwner() throws IllegalArgumentException {
1779 if (mService != null) {
1780 try {
1781 return mService.getProfileOwner(Process.myUserHandle().getIdentifier());
1782 } catch (RemoteException re) {
1783 Log.w(TAG, "Failed to get profile owner");
1784 throw new IllegalArgumentException(
1785 "Requested profile owner for invalid userId", re);
1786 }
1787 }
1788 return null;
1789 }
1790
1791 /**
1792 * @hide
1793 * @return the human readable name of the organisation associated with this DPM or null if
1794 * one is not set.
1795 * @throws IllegalArgumentException if the userId is invalid.
1796 */
1797 public String getProfileOwnerName() throws IllegalArgumentException {
1798 if (mService != null) {
1799 try {
1800 return mService.getProfileOwnerName(Process.myUserHandle().getIdentifier());
1801 } catch (RemoteException re) {
1802 Log.w(TAG, "Failed to get profile owner");
1803 throw new IllegalArgumentException(
1804 "Requested profile owner for invalid userId", re);
1805 }
1806 }
1807 return null;
1808 }
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001809
1810 /**
1811 * Called by a profile owner or device owner to add a default intent handler activity for
1812 * intents that match a certain intent filter. This activity will remain the default intent
1813 * handler even if the set of potential event handlers for the intent filter changes and if
1814 * the intent preferences are reset.
1815 *
1816 * <p>The default disambiguation mechanism takes over if the activity is not installed
1817 * (anymore). When the activity is (re)installed, it is automatically reset as default
1818 * intent handler for the filter.
1819 *
1820 * <p>The calling device admin must be a profile owner or device owner. If it is not, a
1821 * security exception will be thrown.
1822 *
1823 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1824 * @param filter The IntentFilter for which a default handler is added.
1825 * @param activity The Activity that is added as default intent handler.
1826 */
1827 public void addPersistentPreferredActivity(ComponentName admin, IntentFilter filter,
1828 ComponentName activity) {
1829 if (mService != null) {
1830 try {
1831 mService.addPersistentPreferredActivity(admin, filter, activity);
1832 } catch (RemoteException e) {
1833 Log.w(TAG, "Failed talking with device policy service", e);
1834 }
1835 }
1836 }
1837
1838 /**
1839 * Called by a profile owner or device owner to remove all persistent intent handler preferences
Torne (Richard Coles)875e2102014-02-24 14:11:56 +00001840 * associated with the given package that were set by {@link #addPersistentPreferredActivity}.
Sander Alewijnsef475ca32014-02-17 15:13:58 +00001841 *
1842 * <p>The calling device admin must be a profile owner. If it is not, a security
1843 * exception will be thrown.
1844 *
1845 * @param admin Which {@link DeviceAdminReceiver} this request is associated with.
1846 * @param packageName The name of the package for which preferences are removed.
1847 */
1848 public void clearPackagePersistentPreferredActivities(ComponentName admin,
1849 String packageName) {
1850 if (mService != null) {
1851 try {
1852 mService.clearPackagePersistentPreferredActivities(admin, packageName);
1853 } catch (RemoteException e) {
1854 Log.w(TAG, "Failed talking with device policy service", e);
1855 }
1856 }
1857 }
Dianne Hackbornd6847842010-01-12 18:14:19 -08001858}