Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import org.xmlpull.v1.XmlPullParserException; |
| 20 | |
| 21 | import android.annotation.SdkConstant; |
| 22 | import android.annotation.SdkConstant.SdkConstantType; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.pm.ActivityInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.os.Handler; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 29 | import android.os.RemoteCallback; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 30 | import android.os.RemoteException; |
| 31 | import android.os.ServiceManager; |
| 32 | import android.util.Log; |
| 33 | |
| 34 | import java.io.IOException; |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 35 | import java.util.List; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Public interface for managing policies enforced on a device. Most clients |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 39 | * of this class must have published a {@link DeviceAdminReceiver} that the user |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | * has currently enabled. |
| 41 | */ |
| 42 | public class DevicePolicyManager { |
| 43 | private static String TAG = "DevicePolicyManager"; |
| 44 | private static boolean DEBUG = false; |
| 45 | private static boolean localLOGV = DEBUG || android.util.Config.LOGV; |
| 46 | |
| 47 | private final Context mContext; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 48 | private final IDevicePolicyManager mService; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 49 | |
| 50 | private final Handler mHandler; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 51 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame^] | 52 | private DevicePolicyManager(Context context, Handler handler) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 53 | mContext = context; |
| 54 | mHandler = handler; |
| 55 | mService = IDevicePolicyManager.Stub.asInterface( |
| 56 | ServiceManager.getService(Context.DEVICE_POLICY_SERVICE)); |
| 57 | } |
| 58 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame^] | 59 | /*package*/ static DevicePolicyManager create(Context context, Handler handler) { |
| 60 | DevicePolicyManager me = new DevicePolicyManager(context, handler); |
| 61 | return me.mService != null ? me : null; |
| 62 | } |
| 63 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 64 | /** |
| 65 | * Activity action: ask the user to add a new device administrator to the system. |
| 66 | * The desired policy is the ComponentName of the policy in the |
| 67 | * {@link #EXTRA_DEVICE_ADMIN} extra field. This will invoke a UI to |
| 68 | * bring the user through adding the device administrator to the system (or |
| 69 | * allowing them to reject it). |
| 70 | * |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 71 | * <p>You can optionally include the {@link #EXTRA_ADD_EXPLANATION} |
| 72 | * field to provide the user with additional explanation (in addition |
| 73 | * to your component's description) about what is being added. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 74 | */ |
| 75 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 76 | public static final String ACTION_ADD_DEVICE_ADMIN |
| 77 | = "android.app.action.ADD_DEVICE_ADMIN"; |
| 78 | |
| 79 | /** |
| 80 | * The ComponentName of the administrator component. |
| 81 | * |
| 82 | * @see #ACTION_ADD_DEVICE_ADMIN |
| 83 | */ |
| 84 | public static final String EXTRA_DEVICE_ADMIN = "android.app.extra.DEVICE_ADMIN"; |
| 85 | |
| 86 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 87 | * An optional CharSequence providing additional explanation for why the |
| 88 | * admin is being added. |
| 89 | * |
| 90 | * @see #ACTION_ADD_DEVICE_ADMIN |
| 91 | */ |
| 92 | public static final String EXTRA_ADD_EXPLANATION = "android.app.extra.ADD_EXPLANATION"; |
| 93 | |
| 94 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 95 | * Activity action: have the user enter a new password. This activity |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 96 | * should be launched after using {@link #setPasswordQuality(ComponentName, int)} |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 97 | * or {@link #setPasswordMinimumLength(ComponentName, int)} to have the |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 98 | * user enter a new password that meets the current requirements. You can |
| 99 | * use {@link #isActivePasswordSufficient()} to determine whether you need |
| 100 | * to have the user select a new password in order to meet the current |
| 101 | * constraints. Upon being resumed from this activity, |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 102 | * you can check the new password characteristics to see if they are |
| 103 | * sufficient. |
| 104 | */ |
| 105 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 106 | public static final String ACTION_SET_NEW_PASSWORD |
| 107 | = "android.app.action.SET_NEW_PASSWORD"; |
| 108 | |
| 109 | /** |
| 110 | * Return true if the given administrator component is currently |
| 111 | * active (enabled) in the system. |
| 112 | */ |
| 113 | public boolean isAdminActive(ComponentName who) { |
| 114 | if (mService != null) { |
| 115 | try { |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 116 | return mService.isAdminActive(who); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 117 | } catch (RemoteException e) { |
| 118 | Log.w(TAG, "Failed talking with device policy service", e); |
| 119 | } |
| 120 | } |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | /** |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 125 | * Return a list of all currently active device administrator's component |
| 126 | * names. Note that if there are no administrators than null may be |
| 127 | * returned. |
| 128 | */ |
| 129 | public List<ComponentName> getActiveAdmins() { |
| 130 | if (mService != null) { |
| 131 | try { |
| 132 | return mService.getActiveAdmins(); |
| 133 | } catch (RemoteException e) { |
| 134 | Log.w(TAG, "Failed talking with device policy service", e); |
| 135 | } |
| 136 | } |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | /** |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame^] | 141 | * @hide |
| 142 | */ |
| 143 | public boolean packageHasActiveAdmins(String packageName) { |
| 144 | if (mService != null) { |
| 145 | try { |
| 146 | return mService.packageHasActiveAdmins(packageName); |
| 147 | } catch (RemoteException e) { |
| 148 | Log.w(TAG, "Failed talking with device policy service", e); |
| 149 | } |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 155 | * Remove a current administration component. This can only be called |
| 156 | * by the application that owns the administration component; if you |
| 157 | * try to remove someone else's component, a security exception will be |
| 158 | * thrown. |
| 159 | */ |
| 160 | public void removeActiveAdmin(ComponentName who) { |
| 161 | if (mService != null) { |
| 162 | try { |
| 163 | mService.removeActiveAdmin(who); |
| 164 | } catch (RemoteException e) { |
| 165 | Log.w(TAG, "Failed talking with device policy service", e); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 171 | * Constant for {@link #setPasswordQuality}: the policy has no requirements |
| 172 | * for the password. Note that quality constants are ordered so that higher |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 173 | * values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 174 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 175 | public static final int PASSWORD_QUALITY_UNSPECIFIED = 0; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 176 | |
| 177 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 178 | * Constant for {@link #setPasswordQuality}: the policy requires some kind |
| 179 | * of password, but doesn't care what it is. Note that quality constants |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 180 | * are ordered so that higher values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 181 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 182 | public static final int PASSWORD_QUALITY_SOMETHING = 0x10000; |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 185 | * Constant for {@link #setPasswordQuality}: the user must have entered a |
| 186 | * password containing at least numeric characters. Note that quality |
| 187 | * constants are ordered so that higher values are more restrictive. |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 188 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 189 | public static final int PASSWORD_QUALITY_NUMERIC = 0x20000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 190 | |
| 191 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 192 | * Constant for {@link #setPasswordQuality}: the user must have entered a |
| 193 | * password containing at least <em>both></em> numeric <em>and</em> |
| 194 | * alphabeter (or other symbol) characters. Note that quality constants are |
| 195 | * ordered so that higher values are more restrictive. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 196 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 197 | public static final int PASSWORD_QUALITY_ALPHANUMERIC = 0x30000; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 198 | |
| 199 | /** |
| 200 | * Called by an application that is administering the device to set the |
| 201 | * password restrictions it is imposing. After setting this, the user |
| 202 | * will not be able to enter a new password that is not at least as |
| 203 | * restrictive as what has been set. Note that the current password |
| 204 | * will remain until the user has set a new one, so the change does not |
| 205 | * take place immediately. To prompt the user for a new password, use |
| 206 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. |
| 207 | * |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 208 | * <p>Quality constants are ordered so that higher values are more restrictive; |
| 209 | * thus the highest requested quality constant (between the policy set here, |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 210 | * the user's preference, and any other considerations) is the one that |
| 211 | * is in effect. |
| 212 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 213 | * <p>The calling device admin must have requested |
| 214 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 215 | * this method; if it has not, a security exception will be thrown. |
| 216 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 217 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 218 | * @param quality The new desired quality. One of |
| 219 | * {@link #PASSWORD_QUALITY_UNSPECIFIED}, {@link #PASSWORD_QUALITY_SOMETHING}, |
| 220 | * {@link #PASSWORD_QUALITY_NUMERIC}, or {@link #PASSWORD_QUALITY_ALPHANUMERIC}. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 221 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 222 | public void setPasswordQuality(ComponentName admin, int quality) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 223 | if (mService != null) { |
| 224 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 225 | mService.setPasswordQuality(admin, quality); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 226 | } catch (RemoteException e) { |
| 227 | Log.w(TAG, "Failed talking with device policy service", e); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 233 | * Retrieve the current minimum password quality for all admins |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 234 | * or a particular one. |
| 235 | * @param admin The name of the admin component to check, or null to aggregate |
| 236 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 237 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 238 | public int getPasswordQuality(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 239 | if (mService != null) { |
| 240 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 241 | return mService.getPasswordQuality(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 242 | } catch (RemoteException e) { |
| 243 | Log.w(TAG, "Failed talking with device policy service", e); |
| 244 | } |
| 245 | } |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 246 | return PASSWORD_QUALITY_UNSPECIFIED; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 250 | * Called by an application that is administering the device to set the |
| 251 | * minimum allowed password length. After setting this, the user |
| 252 | * will not be able to enter a new password that is not at least as |
| 253 | * restrictive as what has been set. Note that the current password |
| 254 | * will remain until the user has set a new one, so the change does not |
| 255 | * take place immediately. To prompt the user for a new password, use |
| 256 | * {@link #ACTION_SET_NEW_PASSWORD} after setting this value. This |
| 257 | * constraint is only imposed if the administrator has also requested either |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 258 | * {@link #PASSWORD_QUALITY_NUMERIC} or {@link #PASSWORD_QUALITY_ALPHANUMERIC} |
| 259 | * with {@link #setPasswordQuality}. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 260 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 261 | * <p>The calling device admin must have requested |
| 262 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 263 | * this method; if it has not, a security exception will be thrown. |
| 264 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 265 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 266 | * @param length The new desired minimum password length. A value of 0 |
| 267 | * means there is no restriction. |
| 268 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 269 | public void setPasswordMinimumLength(ComponentName admin, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 270 | if (mService != null) { |
| 271 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 272 | mService.setPasswordMinimumLength(admin, length); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 273 | } catch (RemoteException e) { |
| 274 | Log.w(TAG, "Failed talking with device policy service", e); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 280 | * Retrieve the current minimum password length for all admins |
| 281 | * or a particular one. |
| 282 | * @param admin The name of the admin component to check, or null to aggregate |
| 283 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 284 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 285 | public int getPasswordMinimumLength(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 286 | if (mService != null) { |
| 287 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 288 | return mService.getPasswordMinimumLength(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 289 | } catch (RemoteException e) { |
| 290 | Log.w(TAG, "Failed talking with device policy service", e); |
| 291 | } |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 297 | * Return the maximum password length that the device supports for a |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 298 | * particular password quality. |
Dianne Hackborn | 364f6e3 | 2010-01-29 17:38:20 -0800 | [diff] [blame] | 299 | * @param quality The quality being interrogated. |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 300 | * @return Returns the maximum length that the user can enter. |
| 301 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 302 | public int getPasswordMaximumLength(int quality) { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 303 | // Kind-of arbitrary. |
| 304 | return 16; |
| 305 | } |
| 306 | |
| 307 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 308 | * Determine whether the current password the user has set is sufficient |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 309 | * to meet the policy requirements (quality, minimum length) that have been |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 310 | * requested. |
| 311 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 312 | * <p>The calling device admin must have requested |
| 313 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to be able to call |
| 314 | * this method; if it has not, a security exception will be thrown. |
| 315 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 316 | * @return Returns true if the password meets the current requirements, |
| 317 | * else false. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 318 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 319 | public boolean isActivePasswordSufficient() { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 320 | if (mService != null) { |
| 321 | try { |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 322 | return mService.isActivePasswordSufficient(); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 323 | } catch (RemoteException e) { |
| 324 | Log.w(TAG, "Failed talking with device policy service", e); |
| 325 | } |
| 326 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 327 | return false; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Retrieve the number of times the user has failed at entering a |
| 332 | * password since that last successful password entry. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 333 | * |
| 334 | * <p>The calling device admin must have requested |
| 335 | * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to be able to call |
| 336 | * this method; if it has not, a security exception will be thrown. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 337 | */ |
| 338 | public int getCurrentFailedPasswordAttempts() { |
| 339 | if (mService != null) { |
| 340 | try { |
| 341 | return mService.getCurrentFailedPasswordAttempts(); |
| 342 | } catch (RemoteException e) { |
| 343 | Log.w(TAG, "Failed talking with device policy service", e); |
| 344 | } |
| 345 | } |
| 346 | return -1; |
| 347 | } |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 348 | |
| 349 | /** |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 350 | * Setting this to a value greater than zero enables a built-in policy |
| 351 | * that will perform a device wipe after too many incorrect |
| 352 | * device-unlock passwords have been entered. This built-in policy combines |
| 353 | * watching for failed passwords and wiping the device, and requires |
| 354 | * that you request both {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} and |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 355 | * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA}}. |
| 356 | * |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 357 | * <p>To implement any other policy (e.g. wiping data for a particular |
| 358 | * application only, erasing or revoking credentials, or reporting the |
| 359 | * failure to a server), you should implement |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 360 | * {@link DeviceAdminReceiver#onPasswordFailed(Context, android.content.Intent)} |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 361 | * instead. Do not use this API, because if the maximum count is reached, |
| 362 | * the device will be wiped immediately, and your callback will not be invoked. |
| 363 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 364 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 365 | * @param num The number of failed password attempts at which point the |
| 366 | * device will wipe its data. |
| 367 | */ |
| 368 | public void setMaximumFailedPasswordsForWipe(ComponentName admin, int num) { |
| 369 | if (mService != null) { |
| 370 | try { |
| 371 | mService.setMaximumFailedPasswordsForWipe(admin, num); |
| 372 | } catch (RemoteException e) { |
| 373 | Log.w(TAG, "Failed talking with device policy service", e); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 379 | * Retrieve the current maximum number of login attempts that are allowed |
| 380 | * before the device wipes itself, for all admins |
| 381 | * or a particular one. |
| 382 | * @param admin The name of the admin component to check, or null to aggregate |
| 383 | * all admins. |
| 384 | */ |
| 385 | public int getMaximumFailedPasswordsForWipe(ComponentName admin) { |
| 386 | if (mService != null) { |
| 387 | try { |
| 388 | return mService.getMaximumFailedPasswordsForWipe(admin); |
| 389 | } catch (RemoteException e) { |
| 390 | Log.w(TAG, "Failed talking with device policy service", e); |
| 391 | } |
| 392 | } |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | /** |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 397 | * Force a new device unlock password (the password needed to access the |
| 398 | * entire device, not for individual accounts) on the user. This takes |
| 399 | * effect immediately. |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 400 | * The given password must be sufficient for the |
| 401 | * current password quality and length constraints as returned by |
| 402 | * {@link #getPasswordQuality(ComponentName)} and |
| 403 | * {@link #getPasswordMinimumLength(ComponentName)}; if it does not meet |
| 404 | * these constraints, then it will be rejected and false returned. Note |
| 405 | * that the password may be a stronger quality (containing alphanumeric |
| 406 | * characters when the requested quality is only numeric), in which case |
| 407 | * the currently active quality will be increased to match. |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 408 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 409 | * <p>The calling device admin must have requested |
| 410 | * {@link DeviceAdminInfo#USES_POLICY_RESET_PASSWORD} to be able to call |
| 411 | * this method; if it has not, a security exception will be thrown. |
| 412 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 413 | * @param password The new password for the user. |
| 414 | * @return Returns true if the password was applied, or false if it is |
| 415 | * not acceptable for the current constraints. |
| 416 | */ |
| 417 | public boolean resetPassword(String password) { |
| 418 | if (mService != null) { |
| 419 | try { |
| 420 | return mService.resetPassword(password); |
| 421 | } catch (RemoteException e) { |
| 422 | Log.w(TAG, "Failed talking with device policy service", e); |
| 423 | } |
| 424 | } |
| 425 | return false; |
| 426 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * Called by an application that is administering the device to set the |
| 430 | * maximum time for user activity until the device will lock. This limits |
| 431 | * the length that the user can set. It takes effect immediately. |
| 432 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 433 | * <p>The calling device admin must have requested |
Dianne Hackborn | 315ada7 | 2010-02-11 12:14:08 -0800 | [diff] [blame] | 434 | * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 435 | * this method; if it has not, a security exception will be thrown. |
| 436 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 437 | * @param admin Which {@link DeviceAdminReceiver} this request is associated with. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 438 | * @param timeMs The new desired maximum time to lock in milliseconds. |
| 439 | * A value of 0 means there is no restriction. |
| 440 | */ |
| 441 | public void setMaximumTimeToLock(ComponentName admin, long timeMs) { |
| 442 | if (mService != null) { |
| 443 | try { |
| 444 | mService.setMaximumTimeToLock(admin, timeMs); |
| 445 | } catch (RemoteException e) { |
| 446 | Log.w(TAG, "Failed talking with device policy service", e); |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | /** |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 452 | * Retrieve the current maximum time to unlock for all admins |
| 453 | * or a particular one. |
| 454 | * @param admin The name of the admin component to check, or null to aggregate |
| 455 | * all admins. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 456 | */ |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 457 | public long getMaximumTimeToLock(ComponentName admin) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 458 | if (mService != null) { |
| 459 | try { |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 460 | return mService.getMaximumTimeToLock(admin); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 461 | } catch (RemoteException e) { |
| 462 | Log.w(TAG, "Failed talking with device policy service", e); |
| 463 | } |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /** |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 469 | * Make the device lock immediately, as if the lock screen timeout has |
| 470 | * expired at the point of this call. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 471 | * |
| 472 | * <p>The calling device admin must have requested |
| 473 | * {@link DeviceAdminInfo#USES_POLICY_FORCE_LOCK} to be able to call |
| 474 | * this method; if it has not, a security exception will be thrown. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 475 | */ |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 476 | public void lockNow() { |
| 477 | if (mService != null) { |
| 478 | try { |
| 479 | mService.lockNow(); |
| 480 | } catch (RemoteException e) { |
| 481 | Log.w(TAG, "Failed talking with device policy service", e); |
| 482 | } |
| 483 | } |
| 484 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * Ask the user date be wiped. This will cause the device to reboot, |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 488 | * erasing all user data while next booting up. External storage such |
| 489 | * as SD cards will not be erased. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 490 | * |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 491 | * <p>The calling device admin must have requested |
| 492 | * {@link DeviceAdminInfo#USES_POLICY_WIPE_DATA} to be able to call |
| 493 | * this method; if it has not, a security exception will be thrown. |
| 494 | * |
Dianne Hackborn | df83afa | 2010-01-20 13:37:26 -0800 | [diff] [blame] | 495 | * @param flags Bit mask of additional options: currently must be 0. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 496 | */ |
| 497 | public void wipeData(int flags) { |
| 498 | if (mService != null) { |
| 499 | try { |
| 500 | mService.wipeData(flags); |
| 501 | } catch (RemoteException e) { |
| 502 | Log.w(TAG, "Failed talking with device policy service", e); |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * @hide |
| 509 | */ |
| 510 | public void setActiveAdmin(ComponentName policyReceiver) { |
| 511 | if (mService != null) { |
| 512 | try { |
| 513 | mService.setActiveAdmin(policyReceiver); |
| 514 | } catch (RemoteException e) { |
| 515 | Log.w(TAG, "Failed talking with device policy service", e); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /** |
| 521 | * @hide |
| 522 | */ |
Dianne Hackborn | d47c6ed | 2010-01-27 16:21:20 -0800 | [diff] [blame] | 523 | public DeviceAdminInfo getAdminInfo(ComponentName cn) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 524 | ActivityInfo ai; |
| 525 | try { |
| 526 | ai = mContext.getPackageManager().getReceiverInfo(cn, |
| 527 | PackageManager.GET_META_DATA); |
| 528 | } catch (PackageManager.NameNotFoundException e) { |
| 529 | Log.w(TAG, "Unable to retrieve device policy " + cn, e); |
| 530 | return null; |
| 531 | } |
| 532 | |
| 533 | ResolveInfo ri = new ResolveInfo(); |
| 534 | ri.activityInfo = ai; |
| 535 | |
| 536 | try { |
| 537 | return new DeviceAdminInfo(mContext, ri); |
| 538 | } catch (XmlPullParserException e) { |
| 539 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 540 | return null; |
| 541 | } catch (IOException e) { |
| 542 | Log.w(TAG, "Unable to parse device policy " + cn, e); |
| 543 | return null; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * @hide |
| 549 | */ |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 550 | public void getRemoveWarning(ComponentName admin, RemoteCallback result) { |
| 551 | if (mService != null) { |
| 552 | try { |
| 553 | mService.getRemoveWarning(admin, result); |
| 554 | } catch (RemoteException e) { |
| 555 | Log.w(TAG, "Failed talking with device policy service", e); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * @hide |
| 562 | */ |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 563 | public void setActivePasswordState(int quality, int length) { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 564 | if (mService != null) { |
| 565 | try { |
Dianne Hackborn | 9327f4f | 2010-01-29 10:38:29 -0800 | [diff] [blame] | 566 | mService.setActivePasswordState(quality, length); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 567 | } catch (RemoteException e) { |
| 568 | Log.w(TAG, "Failed talking with device policy service", e); |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * @hide |
| 575 | */ |
| 576 | public void reportFailedPasswordAttempt() { |
| 577 | if (mService != null) { |
| 578 | try { |
| 579 | mService.reportFailedPasswordAttempt(); |
| 580 | } catch (RemoteException e) { |
| 581 | Log.w(TAG, "Failed talking with device policy service", e); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * @hide |
| 588 | */ |
| 589 | public void reportSuccessfulPasswordAttempt() { |
| 590 | if (mService != null) { |
| 591 | try { |
| 592 | mService.reportSuccessfulPasswordAttempt(); |
| 593 | } catch (RemoteException e) { |
| 594 | Log.w(TAG, "Failed talking with device policy service", e); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |