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 | |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 17 | package android.app.admin; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 18 | |
| 19 | import android.annotation.SdkConstant; |
| 20 | import android.annotation.SdkConstant.SdkConstantType; |
Dianne Hackborn | 87bba1e | 2010-02-26 17:25:54 -0800 | [diff] [blame] | 21 | import android.app.Service; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 26 | import android.os.Bundle; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Base class for implementing a device administration component. This |
| 30 | * class provides a convenience for interpreting the raw intent actions |
| 31 | * that are sent by the system. |
| 32 | * |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 33 | * <p>The callback methods, like the base |
| 34 | * {@link BroadcastReceiver#onReceive(Context, Intent) BroadcastReceiver.onReceive()} |
| 35 | * method, happen on the main thread of the process. Thus long running |
| 36 | * operations must be done on another thread. Note that because a receiver |
| 37 | * is done once returning from its receive function, such long-running operations |
| 38 | * should probably be done in a {@link Service}. |
| 39 | * |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 40 | * <p>When publishing your DeviceAdmin subclass as a receiver, it must |
| 41 | * handle {@link #ACTION_DEVICE_ADMIN_ENABLED} and require the |
| 42 | * {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission. A typical |
| 43 | * manifest entry would look like:</p> |
| 44 | * |
Dianne Hackborn | ab8a8ed | 2010-01-29 19:03:06 -0800 | [diff] [blame] | 45 | * {@sample development/samples/ApiDemos/AndroidManifest.xml device_admin_declaration} |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 46 | * |
| 47 | * <p>The meta-data referenced here provides addition information specific |
| 48 | * to the device administrator, as parsed by the {@link DeviceAdminInfo} class. |
| 49 | * A typical file would be:</p> |
| 50 | * |
Andrew Stadler | 88209d1 | 2010-02-08 22:59:36 -0800 | [diff] [blame] | 51 | * {@sample development/samples/ApiDemos/res/xml/device_admin_sample.xml meta_data} |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 52 | */ |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 53 | public class DeviceAdminReceiver extends BroadcastReceiver { |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 54 | private static String TAG = "DevicePolicy"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 55 | private static boolean localLOGV = false; |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * This is the primary action that a device administrator must implement to be |
| 59 | * allowed to manage a device. This will be set to the receiver |
| 60 | * when the user enables it for administration. You will generally |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 61 | * handle this in {@link DeviceAdminReceiver#onEnabled(Context, Intent)}. To be |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 62 | * supported, the receiver must also require the |
| 63 | * {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so |
| 64 | * that other applications can not abuse it. |
| 65 | */ |
| 66 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 67 | public static final String ACTION_DEVICE_ADMIN_ENABLED |
| 68 | = "android.app.action.DEVICE_ADMIN_ENABLED"; |
| 69 | |
| 70 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 71 | * Action sent to a device administrator when the user has requested to |
| 72 | * disable it, but before this has actually been done. This gives you |
| 73 | * a chance to supply a message to the user about the impact of |
| 74 | * disabling your admin, by setting the extra field |
| 75 | * {@link #EXTRA_DISABLE_WARNING} in the result Intent. If not set, |
| 76 | * no warning will be displayed. If set, the given text will be shown |
| 77 | * to the user before they disable your admin. |
| 78 | */ |
| 79 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 80 | public static final String ACTION_DEVICE_ADMIN_DISABLE_REQUESTED |
| 81 | = "android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED"; |
| 82 | |
| 83 | /** |
| 84 | * A CharSequence that can be shown to the user informing them of the |
| 85 | * impact of disabling your admin. |
| 86 | * |
| 87 | * @see #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED |
| 88 | */ |
| 89 | public static final String EXTRA_DISABLE_WARNING = "android.app.extra.DISABLE_WARNING"; |
| 90 | |
| 91 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 92 | * Action sent to a device administrator when the user has disabled |
| 93 | * it. Upon return, the application no longer has access to the |
| 94 | * protected device policy manager APIs. You will generally |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 95 | * handle this in {@link DeviceAdminReceiver#onDisabled(Context, Intent)}. Note |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 96 | * that this action will be |
| 97 | * sent the receiver regardless of whether it is explicitly listed in |
| 98 | * its intent filter. |
| 99 | */ |
| 100 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 101 | public static final String ACTION_DEVICE_ADMIN_DISABLED |
| 102 | = "android.app.action.DEVICE_ADMIN_DISABLED"; |
| 103 | |
| 104 | /** |
| 105 | * Action sent to a device administrator when the user has changed the |
| 106 | * password of their device. You can at this point check the characteristics |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 107 | * of the new password with {@link DevicePolicyManager#isActivePasswordSufficient() |
| 108 | * DevicePolicyManager.isActivePasswordSufficient()}. |
| 109 | * You will generally |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 110 | * handle this in {@link DeviceAdminReceiver#onPasswordChanged}. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 111 | * |
| 112 | * <p>The calling device admin must have requested |
| 113 | * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to receive |
| 114 | * this broadcast. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 115 | */ |
| 116 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 117 | public static final String ACTION_PASSWORD_CHANGED |
| 118 | = "android.app.action.ACTION_PASSWORD_CHANGED"; |
| 119 | |
| 120 | /** |
| 121 | * Action sent to a device administrator when the user has failed at |
| 122 | * attempted to enter the password. You can at this point check the |
| 123 | * number of failed password attempts there have been with |
Dianne Hackborn | 254cb44 | 2010-01-27 19:23:59 -0800 | [diff] [blame] | 124 | * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 125 | * DevicePolicyManager.getCurrentFailedPasswordAttempts()}. You will generally |
Dianne Hackborn | ef6b22f | 2010-02-16 20:38:49 -0800 | [diff] [blame] | 126 | * handle this in {@link DeviceAdminReceiver#onPasswordFailed}. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 127 | * |
| 128 | * <p>The calling device admin must have requested |
| 129 | * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive |
| 130 | * this broadcast. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 131 | */ |
| 132 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 133 | public static final String ACTION_PASSWORD_FAILED |
| 134 | = "android.app.action.ACTION_PASSWORD_FAILED"; |
| 135 | |
| 136 | /** |
| 137 | * Action sent to a device administrator when the user has successfully |
| 138 | * entered their password, after failing one or more times. |
Dianne Hackborn | 8aa2e89 | 2010-01-22 11:31:30 -0800 | [diff] [blame] | 139 | * |
| 140 | * <p>The calling device admin must have requested |
| 141 | * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive |
| 142 | * this broadcast. |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 143 | */ |
| 144 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 145 | public static final String ACTION_PASSWORD_SUCCEEDED |
| 146 | = "android.app.action.ACTION_PASSWORD_SUCCEEDED"; |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * Action periodically sent to a device administrator when the device password |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 150 | * is expiring. |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 151 | * |
| 152 | * <p>The calling device admin must have requested |
| 153 | * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to receive |
| 154 | * this broadcast. |
| 155 | */ |
| 156 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 157 | public static final String ACTION_PASSWORD_EXPIRING |
| 158 | = "android.app.action.ACTION_PASSWORD_EXPIRING"; |
| 159 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 160 | /** |
| 161 | * Name under which an DevicePolicy component publishes information |
| 162 | * about itself. This meta-data must reference an XML resource containing |
| 163 | * a device-admin tag. XXX TO DO: describe syntax. |
| 164 | */ |
| 165 | public static final String DEVICE_ADMIN_META_DATA = "android.app.device_admin"; |
| 166 | |
| 167 | private DevicePolicyManager mManager; |
| 168 | private ComponentName mWho; |
| 169 | |
| 170 | /** |
| 171 | * Retrieve the DevicePolicyManager interface for this administrator to work |
| 172 | * with the system. |
| 173 | */ |
| 174 | public DevicePolicyManager getManager(Context context) { |
| 175 | if (mManager != null) { |
| 176 | return mManager; |
| 177 | } |
| 178 | mManager = (DevicePolicyManager)context.getSystemService( |
| 179 | Context.DEVICE_POLICY_SERVICE); |
| 180 | return mManager; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Retrieve the ComponentName describing who this device administrator is, for |
| 185 | * use in {@link DevicePolicyManager} APIs that require the administrator to |
| 186 | * identify itself. |
| 187 | */ |
| 188 | public ComponentName getWho(Context context) { |
| 189 | if (mWho != null) { |
| 190 | return mWho; |
| 191 | } |
| 192 | mWho = new ComponentName(context, getClass()); |
| 193 | return mWho; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Called after the administrator is first enabled, as a result of |
| 198 | * receiving {@link #ACTION_DEVICE_ADMIN_ENABLED}. At this point you |
| 199 | * can use {@link DevicePolicyManager} to set your desired policies. |
| 200 | * @param context The running context as per {@link #onReceive}. |
| 201 | * @param intent The received intent as per {@link #onReceive}. |
| 202 | */ |
| 203 | public void onEnabled(Context context, Intent intent) { |
| 204 | } |
| 205 | |
| 206 | /** |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 207 | * Called when the user has asked to disable the administrator, as a result of |
| 208 | * receiving {@link #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED}, giving you |
| 209 | * a chance to present a warning message to them. The message is returned |
| 210 | * as the result; if null is returned (the default implementation), no |
| 211 | * message will be displayed. |
| 212 | * @param context The running context as per {@link #onReceive}. |
| 213 | * @param intent The received intent as per {@link #onReceive}. |
| 214 | * @return Return the warning message to display to the user before |
| 215 | * being disabled; if null is returned, no message is displayed. |
| 216 | */ |
| 217 | public CharSequence onDisableRequested(Context context, Intent intent) { |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | /** |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 222 | * Called prior to the administrator being disabled, as a result of |
| 223 | * receiving {@link #ACTION_DEVICE_ADMIN_DISABLED}. Upon return, you |
| 224 | * can no longer use the protected parts of the {@link DevicePolicyManager} |
| 225 | * API. |
| 226 | * @param context The running context as per {@link #onReceive}. |
| 227 | * @param intent The received intent as per {@link #onReceive}. |
| 228 | */ |
| 229 | public void onDisabled(Context context, Intent intent) { |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Called after the user has changed their password, as a result of |
| 234 | * receiving {@link #ACTION_PASSWORD_CHANGED}. At this point you |
| 235 | * can use {@link DevicePolicyManager#getCurrentFailedPasswordAttempts() |
| 236 | * DevicePolicyManager.getCurrentFailedPasswordAttempts()} |
| 237 | * to retrieve the active password characteristics. |
| 238 | * @param context The running context as per {@link #onReceive}. |
| 239 | * @param intent The received intent as per {@link #onReceive}. |
| 240 | */ |
| 241 | public void onPasswordChanged(Context context, Intent intent) { |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Called after the user has failed at entering their current password, as a result of |
| 246 | * receiving {@link #ACTION_PASSWORD_FAILED}. At this point you |
| 247 | * can use {@link DevicePolicyManager} to retrieve the number of failed |
| 248 | * password attempts. |
| 249 | * @param context The running context as per {@link #onReceive}. |
| 250 | * @param intent The received intent as per {@link #onReceive}. |
| 251 | */ |
| 252 | public void onPasswordFailed(Context context, Intent intent) { |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Called after the user has succeeded at entering their current password, |
| 257 | * as a result of receiving {@link #ACTION_PASSWORD_SUCCEEDED}. This will |
| 258 | * only be received the first time they succeed after having previously |
| 259 | * failed. |
| 260 | * @param context The running context as per {@link #onReceive}. |
| 261 | * @param intent The received intent as per {@link #onReceive}. |
| 262 | */ |
| 263 | public void onPasswordSucceeded(Context context, Intent intent) { |
| 264 | } |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Called periodically when the password is about to expire or has expired. It will typically |
Jim Miller | 6b85768 | 2011-02-16 16:27:41 -0800 | [diff] [blame] | 268 | * be called at these times: on device boot, once per day before the password expires, |
| 269 | * and at the time when the password expires. |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 270 | * |
| 271 | * <p>If the password is not updated by the user, this method will continue to be called |
| 272 | * once per day until the password is changed or the device admin disables password expiration. |
| 273 | * |
| 274 | * <p>The admin will typically post a notification requesting the user to change their password |
| 275 | * in response to this call. The actual password expiration time can be obtained by calling |
| 276 | * {@link DevicePolicyManager#getPasswordExpiration(ComponentName) } |
| 277 | * |
| 278 | * <p>The admin should be sure to take down any notifications it posted in response to this call |
| 279 | * when it receives {@link DeviceAdminReceiver#onPasswordChanged(Context, Intent) }. |
| 280 | * |
| 281 | * @param context The running context as per {@link #onReceive}. |
| 282 | * @param intent The received intent as per {@link #onReceive}. |
| 283 | */ |
| 284 | public void onPasswordExpiring(Context context, Intent intent) { |
| 285 | } |
| 286 | |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 287 | /** |
| 288 | * Intercept standard device administrator broadcasts. Implementations |
| 289 | * should not override this method; it is better to implement the |
| 290 | * convenience callbacks for each action. |
| 291 | */ |
| 292 | @Override |
| 293 | public void onReceive(Context context, Intent intent) { |
| 294 | String action = intent.getAction(); |
| 295 | if (ACTION_PASSWORD_CHANGED.equals(action)) { |
| 296 | onPasswordChanged(context, intent); |
| 297 | } else if (ACTION_PASSWORD_FAILED.equals(action)) { |
| 298 | onPasswordFailed(context, intent); |
| 299 | } else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) { |
| 300 | onPasswordSucceeded(context, intent); |
| 301 | } else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) { |
| 302 | onEnabled(context, intent); |
Dianne Hackborn | 8ea138c | 2010-01-26 18:01:04 -0800 | [diff] [blame] | 303 | } else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) { |
| 304 | CharSequence res = onDisableRequested(context, intent); |
| 305 | if (res != null) { |
| 306 | Bundle extras = getResultExtras(true); |
| 307 | extras.putCharSequence(EXTRA_DISABLE_WARNING, res); |
| 308 | } |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 309 | } else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) { |
| 310 | onDisabled(context, intent); |
Jim Miller | a4e28d1 | 2010-11-08 16:15:47 -0800 | [diff] [blame] | 311 | } else if (ACTION_PASSWORD_EXPIRING.equals(action)) { |
| 312 | onPasswordExpiring(context, intent); |
Dianne Hackborn | d684784 | 2010-01-12 18:14:19 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } |