blob: 2cc15e21293fc81edfa88e7e47f47b568f16a9e6 [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 android.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080021import android.app.Service;
Dianne Hackbornd6847842010-01-12 18:14:19 -080022import android.content.BroadcastReceiver;
23import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080026import android.os.Bundle;
Dianne Hackbornd6847842010-01-12 18:14:19 -080027
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.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000032 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080033 * <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}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000039 *
Dianne Hackbornd6847842010-01-12 18:14:19 -080040 * <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>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000044 *
Dianne Hackbornab8a8ed2010-01-29 19:03:06 -080045 * {@sample development/samples/ApiDemos/AndroidManifest.xml device_admin_declaration}
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000046 *
Dianne Hackbornd6847842010-01-12 18:14:19 -080047 * <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>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000050 *
Andrew Stadler88209d12010-02-08 22:59:36 -080051 * {@sample development/samples/ApiDemos/res/xml/device_admin_sample.xml meta_data}
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080052 *
53 * <div class="special reference">
54 * <h3>Developer Guides</h3>
55 * <p>For more information about device administration, read the
56 * <a href="{@docRoot}guide/topics/admin/device-admin.html">Device Administration</a>
57 * developer guide.</p>
58 * </div>
Dianne Hackbornd6847842010-01-12 18:14:19 -080059 */
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080060public class DeviceAdminReceiver extends BroadcastReceiver {
Dianne Hackbornd6847842010-01-12 18:14:19 -080061 private static String TAG = "DevicePolicy";
Joe Onorato43a17652011-04-06 19:22:23 -070062 private static boolean localLOGV = false;
Dianne Hackbornd6847842010-01-12 18:14:19 -080063
64 /**
65 * This is the primary action that a device administrator must implement to be
66 * allowed to manage a device. This will be set to the receiver
67 * when the user enables it for administration. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080068 * handle this in {@link DeviceAdminReceiver#onEnabled(Context, Intent)}. To be
Dianne Hackbornd6847842010-01-12 18:14:19 -080069 * supported, the receiver must also require the
70 * {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so
71 * that other applications can not abuse it.
72 */
73 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
74 public static final String ACTION_DEVICE_ADMIN_ENABLED
75 = "android.app.action.DEVICE_ADMIN_ENABLED";
76
77 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080078 * Action sent to a device administrator when the user has requested to
79 * disable it, but before this has actually been done. This gives you
80 * a chance to supply a message to the user about the impact of
81 * disabling your admin, by setting the extra field
82 * {@link #EXTRA_DISABLE_WARNING} in the result Intent. If not set,
83 * no warning will be displayed. If set, the given text will be shown
84 * to the user before they disable your admin.
85 */
86 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
87 public static final String ACTION_DEVICE_ADMIN_DISABLE_REQUESTED
88 = "android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000089
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080090 /**
91 * A CharSequence that can be shown to the user informing them of the
92 * impact of disabling your admin.
93 *
94 * @see #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED
95 */
96 public static final String EXTRA_DISABLE_WARNING = "android.app.extra.DISABLE_WARNING";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000097
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080098 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -080099 * Action sent to a device administrator when the user has disabled
100 * it. Upon return, the application no longer has access to the
101 * protected device policy manager APIs. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800102 * handle this in {@link DeviceAdminReceiver#onDisabled(Context, Intent)}. Note
Dianne Hackbornd6847842010-01-12 18:14:19 -0800103 * that this action will be
104 * sent the receiver regardless of whether it is explicitly listed in
105 * its intent filter.
106 */
107 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
108 public static final String ACTION_DEVICE_ADMIN_DISABLED
109 = "android.app.action.DEVICE_ADMIN_DISABLED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000110
Dianne Hackbornd6847842010-01-12 18:14:19 -0800111 /**
112 * Action sent to a device administrator when the user has changed the
113 * password of their device. You can at this point check the characteristics
Dianne Hackborn254cb442010-01-27 19:23:59 -0800114 * of the new password with {@link DevicePolicyManager#isActivePasswordSufficient()
115 * DevicePolicyManager.isActivePasswordSufficient()}.
116 * You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800117 * handle this in {@link DeviceAdminReceiver#onPasswordChanged}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000118 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800119 * <p>The calling device admin must have requested
120 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to receive
121 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800122 */
123 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
124 public static final String ACTION_PASSWORD_CHANGED
125 = "android.app.action.ACTION_PASSWORD_CHANGED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000126
Dianne Hackbornd6847842010-01-12 18:14:19 -0800127 /**
128 * Action sent to a device administrator when the user has failed at
129 * attempted to enter the password. You can at this point check the
130 * number of failed password attempts there have been with
Dianne Hackborn254cb442010-01-27 19:23:59 -0800131 * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts
Dianne Hackbornd6847842010-01-12 18:14:19 -0800132 * DevicePolicyManager.getCurrentFailedPasswordAttempts()}. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800133 * handle this in {@link DeviceAdminReceiver#onPasswordFailed}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000134 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800135 * <p>The calling device admin must have requested
136 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive
137 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800138 */
139 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
140 public static final String ACTION_PASSWORD_FAILED
141 = "android.app.action.ACTION_PASSWORD_FAILED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000142
Dianne Hackbornd6847842010-01-12 18:14:19 -0800143 /**
144 * Action sent to a device administrator when the user has successfully
145 * entered their password, after failing one or more times.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000146 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800147 * <p>The calling device admin must have requested
148 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive
149 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800150 */
151 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
152 public static final String ACTION_PASSWORD_SUCCEEDED
153 = "android.app.action.ACTION_PASSWORD_SUCCEEDED";
Jim Millera4e28d12010-11-08 16:15:47 -0800154
155 /**
156 * Action periodically sent to a device administrator when the device password
Jim Miller6b857682011-02-16 16:27:41 -0800157 * is expiring.
Jim Millera4e28d12010-11-08 16:15:47 -0800158 *
159 * <p>The calling device admin must have requested
160 * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to receive
161 * this broadcast.
162 */
163 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
164 public static final String ACTION_PASSWORD_EXPIRING
165 = "android.app.action.ACTION_PASSWORD_EXPIRING";
166
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000167 /**
Jessica Hummelf72078b2014-03-06 16:13:12 +0000168 * Broadcast Action: This broadcast is sent to the newly created profile when
Sander Alewijnsec7ae2ad2014-05-20 15:26:48 +0100169 * the provisioning of a managed profile has completed successfully. It is used in both the
170 * Profile Owner and the Device Owner provisioning.
Jessica Hummelf72078b2014-03-06 16:13:12 +0000171 *
Sander Alewijnsec7ae2ad2014-05-20 15:26:48 +0100172 * <p>The broadcast is limited to the DeviceAdminReceiver component specified in the message
173 * that started the provisioning. It is also limited to the managed profile.
Jessica Hummelf72078b2014-03-06 16:13:12 +0000174 *
Nicolas Prevot07ac20b2014-05-27 15:37:45 +0100175 * <p> The intent may contain the extra
176 * {@link DevicePolicyManager#EXTRA_PROVISIONING_EMAIL_ADDRESS}.
177 *
Jessica Hummelf72078b2014-03-06 16:13:12 +0000178 * <p>Input: Nothing.</p>
179 * <p>Output: Nothing</p>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000180 */
181 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jessica Hummelf72078b2014-03-06 16:13:12 +0000182 public static final String ACTION_PROFILE_PROVISIONING_COMPLETE =
183 "android.app.action.ACTION_PROFILE_PROVISIONING_COMPLETE";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000184
Dianne Hackbornd6847842010-01-12 18:14:19 -0800185 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900186 * Name under which a DevicePolicy component publishes information
Dianne Hackbornd6847842010-01-12 18:14:19 -0800187 * about itself. This meta-data must reference an XML resource containing
188 * a device-admin tag. XXX TO DO: describe syntax.
189 */
190 public static final String DEVICE_ADMIN_META_DATA = "android.app.device_admin";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000191
Dianne Hackbornd6847842010-01-12 18:14:19 -0800192 private DevicePolicyManager mManager;
193 private ComponentName mWho;
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000194
Dianne Hackbornd6847842010-01-12 18:14:19 -0800195 /**
196 * Retrieve the DevicePolicyManager interface for this administrator to work
197 * with the system.
198 */
199 public DevicePolicyManager getManager(Context context) {
200 if (mManager != null) {
201 return mManager;
202 }
203 mManager = (DevicePolicyManager)context.getSystemService(
204 Context.DEVICE_POLICY_SERVICE);
205 return mManager;
206 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000207
Dianne Hackbornd6847842010-01-12 18:14:19 -0800208 /**
209 * Retrieve the ComponentName describing who this device administrator is, for
210 * use in {@link DevicePolicyManager} APIs that require the administrator to
211 * identify itself.
212 */
213 public ComponentName getWho(Context context) {
214 if (mWho != null) {
215 return mWho;
216 }
217 mWho = new ComponentName(context, getClass());
218 return mWho;
219 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000220
Dianne Hackbornd6847842010-01-12 18:14:19 -0800221 /**
222 * Called after the administrator is first enabled, as a result of
223 * receiving {@link #ACTION_DEVICE_ADMIN_ENABLED}. At this point you
224 * can use {@link DevicePolicyManager} to set your desired policies.
225 * @param context The running context as per {@link #onReceive}.
226 * @param intent The received intent as per {@link #onReceive}.
227 */
228 public void onEnabled(Context context, Intent intent) {
229 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000230
Dianne Hackbornd6847842010-01-12 18:14:19 -0800231 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800232 * Called when the user has asked to disable the administrator, as a result of
233 * receiving {@link #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED}, giving you
234 * a chance to present a warning message to them. The message is returned
235 * as the result; if null is returned (the default implementation), no
236 * message will be displayed.
237 * @param context The running context as per {@link #onReceive}.
238 * @param intent The received intent as per {@link #onReceive}.
239 * @return Return the warning message to display to the user before
240 * being disabled; if null is returned, no message is displayed.
241 */
242 public CharSequence onDisableRequested(Context context, Intent intent) {
243 return null;
244 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000245
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800246 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800247 * Called prior to the administrator being disabled, as a result of
248 * receiving {@link #ACTION_DEVICE_ADMIN_DISABLED}. Upon return, you
249 * can no longer use the protected parts of the {@link DevicePolicyManager}
250 * API.
251 * @param context The running context as per {@link #onReceive}.
252 * @param intent The received intent as per {@link #onReceive}.
253 */
254 public void onDisabled(Context context, Intent intent) {
255 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000256
Dianne Hackbornd6847842010-01-12 18:14:19 -0800257 /**
258 * Called after the user has changed their password, as a result of
259 * receiving {@link #ACTION_PASSWORD_CHANGED}. At this point you
260 * can use {@link DevicePolicyManager#getCurrentFailedPasswordAttempts()
261 * DevicePolicyManager.getCurrentFailedPasswordAttempts()}
262 * to retrieve the active password characteristics.
263 * @param context The running context as per {@link #onReceive}.
264 * @param intent The received intent as per {@link #onReceive}.
265 */
266 public void onPasswordChanged(Context context, Intent intent) {
267 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000268
Dianne Hackbornd6847842010-01-12 18:14:19 -0800269 /**
270 * Called after the user has failed at entering their current password, as a result of
271 * receiving {@link #ACTION_PASSWORD_FAILED}. At this point you
272 * can use {@link DevicePolicyManager} to retrieve the number of failed
273 * password attempts.
274 * @param context The running context as per {@link #onReceive}.
275 * @param intent The received intent as per {@link #onReceive}.
276 */
277 public void onPasswordFailed(Context context, Intent intent) {
278 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000279
Dianne Hackbornd6847842010-01-12 18:14:19 -0800280 /**
281 * Called after the user has succeeded at entering their current password,
282 * as a result of receiving {@link #ACTION_PASSWORD_SUCCEEDED}. This will
283 * only be received the first time they succeed after having previously
284 * failed.
285 * @param context The running context as per {@link #onReceive}.
286 * @param intent The received intent as per {@link #onReceive}.
287 */
288 public void onPasswordSucceeded(Context context, Intent intent) {
289 }
Jim Millera4e28d12010-11-08 16:15:47 -0800290
291 /**
292 * Called periodically when the password is about to expire or has expired. It will typically
Jim Miller6b857682011-02-16 16:27:41 -0800293 * be called at these times: on device boot, once per day before the password expires,
294 * and at the time when the password expires.
Jim Millera4e28d12010-11-08 16:15:47 -0800295 *
296 * <p>If the password is not updated by the user, this method will continue to be called
297 * once per day until the password is changed or the device admin disables password expiration.
298 *
299 * <p>The admin will typically post a notification requesting the user to change their password
300 * in response to this call. The actual password expiration time can be obtained by calling
301 * {@link DevicePolicyManager#getPasswordExpiration(ComponentName) }
302 *
303 * <p>The admin should be sure to take down any notifications it posted in response to this call
304 * when it receives {@link DeviceAdminReceiver#onPasswordChanged(Context, Intent) }.
305 *
306 * @param context The running context as per {@link #onReceive}.
307 * @param intent The received intent as per {@link #onReceive}.
308 */
309 public void onPasswordExpiring(Context context, Intent intent) {
310 }
311
Dianne Hackbornd6847842010-01-12 18:14:19 -0800312 /**
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000313 * Called on the new profile when managed profile provisioning has completed.
314 * Managed profile provisioning is the process of setting up the device so that it has a
315 * separate profile which is managed by the mobile device management(mdm) application that
316 * triggered the provisioning.
317 *
318 * <p>As part of provisioning a new profile is created, the mdm is moved to the new profile and
319 * set as the owner of the profile so that it has full control over it.
320 * This intent is only received by the mdm package that is set as profile owner during
321 * provisioning.
322 *
323 * <p>Provisioning can be triggered via an intent with the action
324 * android.managedprovisioning.ACTION_PROVISION_MANAGED_PROFILE.
325 *
326 * @param context The running context as per {@link #onReceive}.
327 * @param intent The received intent as per {@link #onReceive}.
328 */
329 public void onProfileProvisioningComplete(Context context, Intent intent) {
330 }
331
332 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800333 * Intercept standard device administrator broadcasts. Implementations
334 * should not override this method; it is better to implement the
335 * convenience callbacks for each action.
336 */
337 @Override
338 public void onReceive(Context context, Intent intent) {
339 String action = intent.getAction();
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000340
Dianne Hackbornd6847842010-01-12 18:14:19 -0800341 if (ACTION_PASSWORD_CHANGED.equals(action)) {
342 onPasswordChanged(context, intent);
343 } else if (ACTION_PASSWORD_FAILED.equals(action)) {
344 onPasswordFailed(context, intent);
345 } else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) {
346 onPasswordSucceeded(context, intent);
347 } else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) {
348 onEnabled(context, intent);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800349 } else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) {
350 CharSequence res = onDisableRequested(context, intent);
351 if (res != null) {
352 Bundle extras = getResultExtras(true);
353 extras.putCharSequence(EXTRA_DISABLE_WARNING, res);
354 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800355 } else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) {
356 onDisabled(context, intent);
Jim Millera4e28d12010-11-08 16:15:47 -0800357 } else if (ACTION_PASSWORD_EXPIRING.equals(action)) {
358 onPasswordExpiring(context, intent);
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000359 } else if (ACTION_PROFILE_PROVISIONING_COMPLETE.equals(action)) {
360 onProfileProvisioningComplete(context, intent);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800361 }
362 }
363}