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