blob: d5ca0e94e2d3f44e8f8c3f327c5569c0196ec9d4 [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
Jessica Hummel9da60392014-05-21 12:32:57 +010019import android.accounts.AccountManager;
Michal Karpinski3fc437e2015-12-15 10:09:00 +000020import android.annotation.IntDef;
Dianne Hackbornd6847842010-01-12 18:14:19 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Julia Reynolds94e7bf62015-06-10 14:44:56 -040023import android.annotation.SystemApi;
Dianne Hackborn87bba1e2010-02-26 17:25:54 -080024import android.app.Service;
Dianne Hackbornd6847842010-01-12 18:14:19 -080025import android.content.BroadcastReceiver;
26import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
Robin Lee39087b12015-05-05 15:57:17 +010029import android.net.Uri;
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080030import android.os.Bundle;
Robin Lee3798ed52015-02-03 17:55:31 +000031import android.security.KeyChain;
Dianne Hackbornd6847842010-01-12 18:14:19 -080032
Michal Karpinski3fc437e2015-12-15 10:09:00 +000033import java.lang.annotation.Retention;
34import java.lang.annotation.RetentionPolicy;
35
Dianne Hackbornd6847842010-01-12 18:14:19 -080036/**
37 * Base class for implementing a device administration component. This
38 * class provides a convenience for interpreting the raw intent actions
39 * that are sent by the system.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000040 *
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080041 * <p>The callback methods, like the base
42 * {@link BroadcastReceiver#onReceive(Context, Intent) BroadcastReceiver.onReceive()}
43 * method, happen on the main thread of the process. Thus long running
44 * operations must be done on another thread. Note that because a receiver
45 * is done once returning from its receive function, such long-running operations
46 * should probably be done in a {@link Service}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000047 *
Dianne Hackbornd6847842010-01-12 18:14:19 -080048 * <p>When publishing your DeviceAdmin subclass as a receiver, it must
49 * handle {@link #ACTION_DEVICE_ADMIN_ENABLED} and require the
50 * {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission. A typical
51 * manifest entry would look like:</p>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000052 *
Dianne Hackbornab8a8ed2010-01-29 19:03:06 -080053 * {@sample development/samples/ApiDemos/AndroidManifest.xml device_admin_declaration}
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000054 *
Dianne Hackbornd6847842010-01-12 18:14:19 -080055 * <p>The meta-data referenced here provides addition information specific
56 * to the device administrator, as parsed by the {@link DeviceAdminInfo} class.
57 * A typical file would be:</p>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000058 *
Andrew Stadler88209d12010-02-08 22:59:36 -080059 * {@sample development/samples/ApiDemos/res/xml/device_admin_sample.xml meta_data}
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080060 *
61 * <div class="special reference">
62 * <h3>Developer Guides</h3>
63 * <p>For more information about device administration, read the
64 * <a href="{@docRoot}guide/topics/admin/device-admin.html">Device Administration</a>
65 * developer guide.</p>
66 * </div>
Dianne Hackbornd6847842010-01-12 18:14:19 -080067 */
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080068public class DeviceAdminReceiver extends BroadcastReceiver {
Dianne Hackbornd6847842010-01-12 18:14:19 -080069 private static String TAG = "DevicePolicy";
Joe Onorato43a17652011-04-06 19:22:23 -070070 private static boolean localLOGV = false;
Dianne Hackbornd6847842010-01-12 18:14:19 -080071
72 /**
73 * This is the primary action that a device administrator must implement to be
74 * allowed to manage a device. This will be set to the receiver
75 * when the user enables it for administration. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -080076 * handle this in {@link DeviceAdminReceiver#onEnabled(Context, Intent)}. To be
Dianne Hackbornd6847842010-01-12 18:14:19 -080077 * supported, the receiver must also require the
78 * {@link android.Manifest.permission#BIND_DEVICE_ADMIN} permission so
79 * that other applications can not abuse it.
80 */
81 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
82 public static final String ACTION_DEVICE_ADMIN_ENABLED
83 = "android.app.action.DEVICE_ADMIN_ENABLED";
84
85 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080086 * Action sent to a device administrator when the user has requested to
87 * disable it, but before this has actually been done. This gives you
88 * a chance to supply a message to the user about the impact of
89 * disabling your admin, by setting the extra field
90 * {@link #EXTRA_DISABLE_WARNING} in the result Intent. If not set,
91 * no warning will be displayed. If set, the given text will be shown
92 * to the user before they disable your admin.
93 */
94 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
95 public static final String ACTION_DEVICE_ADMIN_DISABLE_REQUESTED
96 = "android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +000097
Dianne Hackborn8ea138c2010-01-26 18:01:04 -080098 /**
99 * A CharSequence that can be shown to the user informing them of the
100 * impact of disabling your admin.
101 *
102 * @see #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED
103 */
104 public static final String EXTRA_DISABLE_WARNING = "android.app.extra.DISABLE_WARNING";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000105
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800106 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800107 * Action sent to a device administrator when the user has disabled
108 * it. Upon return, the application no longer has access to the
109 * protected device policy manager APIs. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800110 * handle this in {@link DeviceAdminReceiver#onDisabled(Context, Intent)}. Note
Dianne Hackbornd6847842010-01-12 18:14:19 -0800111 * that this action will be
112 * sent the receiver regardless of whether it is explicitly listed in
113 * its intent filter.
114 */
115 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
116 public static final String ACTION_DEVICE_ADMIN_DISABLED
117 = "android.app.action.DEVICE_ADMIN_DISABLED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000118
Dianne Hackbornd6847842010-01-12 18:14:19 -0800119 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000120 * Action sent to a device administrator when the user has changed the password of their device
121 * or profile challenge. You can at this point check the characteristics
Dianne Hackborn254cb442010-01-27 19:23:59 -0800122 * of the new password with {@link DevicePolicyManager#isActivePasswordSufficient()
123 * DevicePolicyManager.isActivePasswordSufficient()}.
124 * You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800125 * handle this in {@link DeviceAdminReceiver#onPasswordChanged}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000126 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800127 * <p>The calling device admin must have requested
128 * {@link DeviceAdminInfo#USES_POLICY_LIMIT_PASSWORD} to receive
129 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800130 */
131 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
132 public static final String ACTION_PASSWORD_CHANGED
133 = "android.app.action.ACTION_PASSWORD_CHANGED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000134
Dianne Hackbornd6847842010-01-12 18:14:19 -0800135 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000136 * Action sent to a device administrator when the user has entered an incorrect device
137 * or profile challenge password. You can at this point check the
Dianne Hackbornd6847842010-01-12 18:14:19 -0800138 * number of failed password attempts there have been with
Dianne Hackborn254cb442010-01-27 19:23:59 -0800139 * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts
Dianne Hackbornd6847842010-01-12 18:14:19 -0800140 * DevicePolicyManager.getCurrentFailedPasswordAttempts()}. You will generally
Dianne Hackbornef6b22f2010-02-16 20:38:49 -0800141 * handle this in {@link DeviceAdminReceiver#onPasswordFailed}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000142 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800143 * <p>The calling device admin must have requested
144 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive
145 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800146 */
147 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
148 public static final String ACTION_PASSWORD_FAILED
149 = "android.app.action.ACTION_PASSWORD_FAILED";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000150
Dianne Hackbornd6847842010-01-12 18:14:19 -0800151 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000152 * Action sent to a device administrator when the user has successfully entered their device
153 * or profile challenge password, after failing one or more times. You will generally
154 * handle this in {@link DeviceAdminReceiver#onPasswordSucceeded}.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000155 *
Dianne Hackborn8aa2e892010-01-22 11:31:30 -0800156 * <p>The calling device admin must have requested
157 * {@link DeviceAdminInfo#USES_POLICY_WATCH_LOGIN} to receive
158 * this broadcast.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800159 */
160 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
161 public static final String ACTION_PASSWORD_SUCCEEDED
162 = "android.app.action.ACTION_PASSWORD_SUCCEEDED";
Jim Millera4e28d12010-11-08 16:15:47 -0800163
164 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000165 * Action periodically sent to a device administrator when the device or profile challenge
166 * password is expiring. You will generally
167 * handle this in {@link DeviceAdminReceiver#onPasswordExpiring}.
Jim Millera4e28d12010-11-08 16:15:47 -0800168 *
169 * <p>The calling device admin must have requested
170 * {@link DeviceAdminInfo#USES_POLICY_EXPIRE_PASSWORD} to receive
171 * this broadcast.
172 */
173 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
174 public static final String ACTION_PASSWORD_EXPIRING
175 = "android.app.action.ACTION_PASSWORD_EXPIRING";
176
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000177 /**
Jason Monk35c62a42014-06-17 10:24:47 -0400178 * Action sent to a device administrator to notify that the device is entering
Benjamin Franz43261142015-02-11 15:59:44 +0000179 * lock task mode. The extra {@link #EXTRA_LOCK_TASK_PACKAGE}
180 * will describe the package using lock task mode.
Jason Monk35c62a42014-06-17 10:24:47 -0400181 *
Jason Monk35c62a42014-06-17 10:24:47 -0400182 * <p>The calling device admin must be the device owner or profile
183 * owner to receive this broadcast.
Benjamin Franzc70d0e72015-02-12 16:12:58 +0000184 *
185 * @see DevicePolicyManager#isLockTaskPermitted(String)
Jason Monk35c62a42014-06-17 10:24:47 -0400186 */
187 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jason Monk48aacba2014-08-13 16:29:08 -0400188 public static final String ACTION_LOCK_TASK_ENTERING
Jason Monk5503a552014-09-04 14:14:37 -0400189 = "android.app.action.LOCK_TASK_ENTERING";
Jason Monk35c62a42014-06-17 10:24:47 -0400190
191 /**
Jason Monk48aacba2014-08-13 16:29:08 -0400192 * Action sent to a device administrator to notify that the device is exiting
Benjamin Franz43261142015-02-11 15:59:44 +0000193 * lock task mode.
Jason Monk35c62a42014-06-17 10:24:47 -0400194 *
Jason Monk48aacba2014-08-13 16:29:08 -0400195 * <p>The calling device admin must be the device owner or profile
196 * owner to receive this broadcast.
Benjamin Franzc70d0e72015-02-12 16:12:58 +0000197 *
198 * @see DevicePolicyManager#isLockTaskPermitted(String)
Jason Monk35c62a42014-06-17 10:24:47 -0400199 */
Jason Monk48aacba2014-08-13 16:29:08 -0400200 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
201 public static final String ACTION_LOCK_TASK_EXITING
Jason Monk5503a552014-09-04 14:14:37 -0400202 = "android.app.action.LOCK_TASK_EXITING";
Jason Monk35c62a42014-06-17 10:24:47 -0400203
204 /**
Benjamin Franzc70d0e72015-02-12 16:12:58 +0000205 * A string containing the name of the package entering lock task mode.
Jason Monk35c62a42014-06-17 10:24:47 -0400206 *
Benjamin Franzc70d0e72015-02-12 16:12:58 +0000207 * @see #ACTION_LOCK_TASK_ENTERING
Jason Monk35c62a42014-06-17 10:24:47 -0400208 */
209 public static final String EXTRA_LOCK_TASK_PACKAGE =
210 "android.app.extra.LOCK_TASK_PACKAGE";
211
212 /**
Jessica Hummel9da60392014-05-21 12:32:57 +0100213 * Broadcast Action: This broadcast is sent to indicate that provisioning of a managed profile
214 * or managed device has completed successfully.
Jessica Hummelf72078b2014-03-06 16:13:12 +0000215 *
Jessica Hummel9da60392014-05-21 12:32:57 +0100216 * <p>The broadcast is limited to the profile that will be managed by the application that
217 * requested provisioning. In the device owner case the profile is the primary user.
218 * The broadcast will also be limited to the {@link DeviceAdminReceiver} component
219 * specified in the original intent or NFC bump that started the provisioning process
Andrew Solovay27f53372015-03-02 16:37:59 -0800220 * (see {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE
221 * DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE}).
Nicolas Prevot07ac20b2014-05-27 15:37:45 +0100222 *
Sander Alewijnse1cc4ecc2014-06-23 19:56:52 +0100223 * <p>A device admin application which listens to this intent can find out if the device was
224 * provisioned for the device owner or profile owner case by calling respectively
225 * {@link android.app.admin.DevicePolicyManager#isDeviceOwnerApp} and
Julia Reynolds20118f12015-02-11 12:34:08 -0500226 * {@link android.app.admin.DevicePolicyManager#isProfileOwnerApp}. You will generally handle
227 * this in {@link DeviceAdminReceiver#onProfileProvisioningComplete}.
Sander Alewijnse1cc4ecc2014-06-23 19:56:52 +0100228 *
Jessica Hummelf72078b2014-03-06 16:13:12 +0000229 * <p>Input: Nothing.</p>
230 * <p>Output: Nothing</p>
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000231 */
232 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jessica Hummelf72078b2014-03-06 16:13:12 +0000233 public static final String ACTION_PROFILE_PROVISIONING_COMPLETE =
Jessica Hummel56692162014-09-10 15:12:11 +0100234 "android.app.action.PROFILE_PROVISIONING_COMPLETE";
235
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000236 /**
237 * Action sent to a device administrator to notify that the device user
238 * has declined sharing a bugreport.
239 *
240 * <p>The calling device admin must be the device owner to receive this broadcast.
241 * @see DevicePolicyManager#requestBugreport
242 * @hide
243 */
244 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
245 public static final String ACTION_BUGREPORT_SHARING_DECLINED =
246 "android.app.action.BUGREPORT_SHARING_DECLINED";
247
248 /**
249 * Action sent to a device administrator to notify that the collection of a bugreport
250 * has failed.
251 *
252 * <p>The calling device admin must be the device owner to receive this broadcast.
253 * @see DevicePolicyManager#requestBugreport
254 * @hide
255 */
256 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
257 public static final String ACTION_BUGREPORT_FAILED = "android.app.action.BUGREPORT_FAILED";
258
259 /**
260 * Action sent to a device administrator to share the bugreport.
261 *
262 * <p>The calling device admin must be the device owner to receive this broadcast.
263 * @see DevicePolicyManager#requestBugreport
264 * @hide
265 */
266 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
267 public static final String ACTION_BUGREPORT_SHARE =
268 "android.app.action.BUGREPORT_SHARE";
269
270 /**
Rubin Xuc3cd05f2016-01-11 12:11:35 +0000271 * Broadcast action: notify that a new batch of device logs is ready to be collected.
272 * @hide
273 */
274 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
275 public static final String ACTION_SECURITY_LOGS_AVAILABLE
276 = "android.app.action.SECURITY_LOGS_AVAILABLE";
277
278 /**
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000279 * A string containing the SHA-256 hash of the bugreport file.
280 *
281 * @see #ACTION_BUGREPORT_SHARE
282 * @hide
283 */
284 public static final String EXTRA_BUGREPORT_HASH = "android.app.extra.BUGREPORT_HASH";
285
286 /**
287 * An {@code int} failure code representing the reason of the bugreport failure.
288 *
289 * @see #ACTION_BUGREPORT_FAILED
290 * @see #BUGREPORT_FAILURE_FAILED_COMPLETING, #BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE
291 * @hide
292 */
293 public static final String EXTRA_BUGREPORT_FAILURE_REASON =
294 "android.app.extra.BUGREPORT_FAILURE_REASON";
295
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000296 /**
297 * An interface representing reason of bugreport failure.
298 *
299 * @see #EXTRA_BUGREPORT_FAILURE_REASON
300 * @hide
301 */
Felipe Lemedc7af962016-01-22 18:27:03 -0800302 @Retention(RetentionPolicy.SOURCE)
303 @IntDef({
304 BUGREPORT_FAILURE_FAILED_COMPLETING,
305 BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE
306 })
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000307 public @interface BugreportFailureCode {}
308 /** Bugreport completion process failed. */
309 public static final int BUGREPORT_FAILURE_FAILED_COMPLETING = 0;
310 /** Bugreport is no longer available for collection. */
311 public static final int BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE = 1;
312
Robin Lee3798ed52015-02-03 17:55:31 +0000313 /** @hide */
314 public static final String ACTION_CHOOSE_PRIVATE_KEY_ALIAS = "android.app.action.CHOOSE_PRIVATE_KEY_ALIAS";
315
316 /** @hide */
317 public static final String EXTRA_CHOOSE_PRIVATE_KEY_SENDER_UID = "android.app.extra.CHOOSE_PRIVATE_KEY_SENDER_UID";
318
319 /** @hide */
Robin Lee39087b12015-05-05 15:57:17 +0100320 public static final String EXTRA_CHOOSE_PRIVATE_KEY_URI = "android.app.extra.CHOOSE_PRIVATE_KEY_URI";
Robin Lee3798ed52015-02-03 17:55:31 +0000321
322 /** @hide */
323 public static final String EXTRA_CHOOSE_PRIVATE_KEY_ALIAS = "android.app.extra.CHOOSE_PRIVATE_KEY_ALIAS";
324
325 /** @hide */
326 public static final String EXTRA_CHOOSE_PRIVATE_KEY_RESPONSE = "android.app.extra.CHOOSE_PRIVATE_KEY_RESPONSE";
327
Julia Reynolds20118f12015-02-11 12:34:08 -0500328 /**
Rubin Xudc105cc2015-04-14 23:38:01 +0100329 * Broadcast action: notify device owner that there is a pending system update.
330 * @hide
331 */
332 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
333 public static final String ACTION_NOTIFY_PENDING_SYSTEM_UPDATE = "android.app.action.NOTIFY_PENDING_SYSTEM_UPDATE";
334
335 /**
336 * A long type extra for {@link #onSystemUpdatePending} recording the system time as given by
337 * {@link System#currentTimeMillis()} when the current pending system update is first available.
338 * @hide
339 */
340 public static final String EXTRA_SYSTEM_UPDATE_RECEIVED_TIME = "android.app.extra.SYSTEM_UPDATE_RECEIVED_TIME";
341
342 /**
Ken Wakasaf76a50c2012-03-09 19:56:35 +0900343 * Name under which a DevicePolicy component publishes information
Dianne Hackbornd6847842010-01-12 18:14:19 -0800344 * about itself. This meta-data must reference an XML resource containing
Andrew Solovay27f53372015-03-02 16:37:59 -0800345 * a device-admin tag.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800346 */
Andrew Solovay27f53372015-03-02 16:37:59 -0800347 // TO DO: describe syntax.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800348 public static final String DEVICE_ADMIN_META_DATA = "android.app.device_admin";
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000349
Dianne Hackbornd6847842010-01-12 18:14:19 -0800350 private DevicePolicyManager mManager;
351 private ComponentName mWho;
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000352
Dianne Hackbornd6847842010-01-12 18:14:19 -0800353 /**
354 * Retrieve the DevicePolicyManager interface for this administrator to work
355 * with the system.
356 */
357 public DevicePolicyManager getManager(Context context) {
358 if (mManager != null) {
359 return mManager;
360 }
361 mManager = (DevicePolicyManager)context.getSystemService(
362 Context.DEVICE_POLICY_SERVICE);
363 return mManager;
364 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000365
Dianne Hackbornd6847842010-01-12 18:14:19 -0800366 /**
367 * Retrieve the ComponentName describing who this device administrator is, for
368 * use in {@link DevicePolicyManager} APIs that require the administrator to
369 * identify itself.
370 */
371 public ComponentName getWho(Context context) {
372 if (mWho != null) {
373 return mWho;
374 }
375 mWho = new ComponentName(context, getClass());
376 return mWho;
377 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000378
Dianne Hackbornd6847842010-01-12 18:14:19 -0800379 /**
380 * Called after the administrator is first enabled, as a result of
381 * receiving {@link #ACTION_DEVICE_ADMIN_ENABLED}. At this point you
382 * can use {@link DevicePolicyManager} to set your desired policies.
Jason Monk03978a42014-06-10 15:05:30 -0400383 *
384 * <p> If the admin is activated by a device owner, then the intent
385 * may contain private extras that are relevant to user setup.
386 * {@see DevicePolicyManager#createAndInitializeUser(ComponentName, String, String,
387 * ComponentName, Intent)}
388 *
Dianne Hackbornd6847842010-01-12 18:14:19 -0800389 * @param context The running context as per {@link #onReceive}.
390 * @param intent The received intent as per {@link #onReceive}.
391 */
392 public void onEnabled(Context context, Intent intent) {
393 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000394
Dianne Hackbornd6847842010-01-12 18:14:19 -0800395 /**
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800396 * Called when the user has asked to disable the administrator, as a result of
397 * receiving {@link #ACTION_DEVICE_ADMIN_DISABLE_REQUESTED}, giving you
398 * a chance to present a warning message to them. The message is returned
399 * as the result; if null is returned (the default implementation), no
400 * message will be displayed.
401 * @param context The running context as per {@link #onReceive}.
402 * @param intent The received intent as per {@link #onReceive}.
403 * @return Return the warning message to display to the user before
404 * being disabled; if null is returned, no message is displayed.
405 */
406 public CharSequence onDisableRequested(Context context, Intent intent) {
407 return null;
408 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000409
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800410 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800411 * Called prior to the administrator being disabled, as a result of
412 * receiving {@link #ACTION_DEVICE_ADMIN_DISABLED}. Upon return, you
413 * can no longer use the protected parts of the {@link DevicePolicyManager}
414 * API.
415 * @param context The running context as per {@link #onReceive}.
416 * @param intent The received intent as per {@link #onReceive}.
417 */
418 public void onDisabled(Context context, Intent intent) {
419 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000420
Dianne Hackbornd6847842010-01-12 18:14:19 -0800421 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000422 * Called after the user has changed their device or profile challenge password, as a result of
Dianne Hackbornd6847842010-01-12 18:14:19 -0800423 * receiving {@link #ACTION_PASSWORD_CHANGED}. At this point you
Robin Lee9983aca2016-01-20 17:15:08 +0000424 * can use {@link DevicePolicyManager#getPasswordQuality(android.content.ComponentName)}
Dianne Hackbornd6847842010-01-12 18:14:19 -0800425 * to retrieve the active password characteristics.
426 * @param context The running context as per {@link #onReceive}.
427 * @param intent The received intent as per {@link #onReceive}.
428 */
429 public void onPasswordChanged(Context context, Intent intent) {
430 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000431
Dianne Hackbornd6847842010-01-12 18:14:19 -0800432 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000433 * Called after the user has failed at entering their device or profile challenge password,
434 * as a result of receiving {@link #ACTION_PASSWORD_FAILED}. At this point you can use
435 * {@link DevicePolicyManager#getCurrentFailedPasswordAttempts()} to retrieve the number of
436 * failed password attempts.
Dianne Hackbornd6847842010-01-12 18:14:19 -0800437 * @param context The running context as per {@link #onReceive}.
438 * @param intent The received intent as per {@link #onReceive}.
439 */
440 public void onPasswordFailed(Context context, Intent intent) {
441 }
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000442
Dianne Hackbornd6847842010-01-12 18:14:19 -0800443 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000444 * Called after the user has succeeded at entering their device or profile challenge password,
Dianne Hackbornd6847842010-01-12 18:14:19 -0800445 * as a result of receiving {@link #ACTION_PASSWORD_SUCCEEDED}. This will
446 * only be received the first time they succeed after having previously
447 * failed.
448 * @param context The running context as per {@link #onReceive}.
449 * @param intent The received intent as per {@link #onReceive}.
450 */
451 public void onPasswordSucceeded(Context context, Intent intent) {
452 }
Jim Millera4e28d12010-11-08 16:15:47 -0800453
454 /**
Esteban Talaverac1c83592016-02-17 17:56:15 +0000455 * Called periodically when the device or profile challenge password is about to expire
456 * or has expired. It will typically be called at these times: on device boot, once per day
457 * before the password expires, and at the time when the password expires.
Jim Millera4e28d12010-11-08 16:15:47 -0800458 *
459 * <p>If the password is not updated by the user, this method will continue to be called
460 * once per day until the password is changed or the device admin disables password expiration.
461 *
462 * <p>The admin will typically post a notification requesting the user to change their password
463 * in response to this call. The actual password expiration time can be obtained by calling
464 * {@link DevicePolicyManager#getPasswordExpiration(ComponentName) }
465 *
466 * <p>The admin should be sure to take down any notifications it posted in response to this call
467 * when it receives {@link DeviceAdminReceiver#onPasswordChanged(Context, Intent) }.
468 *
469 * @param context The running context as per {@link #onReceive}.
470 * @param intent The received intent as per {@link #onReceive}.
471 */
472 public void onPasswordExpiring(Context context, Intent intent) {
473 }
474
Dianne Hackbornd6847842010-01-12 18:14:19 -0800475 /**
Jessica Hummel9da60392014-05-21 12:32:57 +0100476 * Called when provisioning of a managed profile or managed device has completed successfully.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000477 *
Julia Reynolds20118f12015-02-11 12:34:08 -0500478 * <p> As a prerequisite for the execution of this callback the {@link DeviceAdminReceiver} has
Jessica Hummel9da60392014-05-21 12:32:57 +0100479 * to declare an intent filter for {@link #ACTION_PROFILE_PROVISIONING_COMPLETE}.
480 * Its component must also be specified in the {@link DevicePolicyManager#EXTRA_DEVICE_ADMIN}
481 * of the {@link DevicePolicyManager#ACTION_PROVISION_MANAGED_PROFILE} intent that started the
482 * managed provisioning.
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000483 *
Julia Reynolds20118f12015-02-11 12:34:08 -0500484 * <p>When provisioning of a managed profile is complete, the managed profile is hidden until
485 * the profile owner calls {DevicePolicyManager#setProfileEnabled(ComponentName admin)}.
486 * Typically a profile owner will enable the profile when it has finished any additional setup
487 * such as adding an account by using the {@link AccountManager} and calling apis to bring the
488 * profile into the desired state.
Jessica Hummel9da60392014-05-21 12:32:57 +0100489 *
490 * <p> Note that provisioning completes without waiting for any server interactions, so the
Julia Reynolds20118f12015-02-11 12:34:08 -0500491 * profile owner needs to wait for data to be available if required (e.g. android device ids or
Jessica Hummel9da60392014-05-21 12:32:57 +0100492 * other data that is set as a result of server interactions).
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000493 *
494 * @param context The running context as per {@link #onReceive}.
495 * @param intent The received intent as per {@link #onReceive}.
496 */
497 public void onProfileProvisioningComplete(Context context, Intent intent) {
498 }
499
500 /**
Julia Reynolds20118f12015-02-11 12:34:08 -0500501 * Called during provisioning of a managed device to allow the device initializer to perform
Craig Lafayettee7ee54e2015-09-21 13:48:53 -0400502 * user setup steps.
Julia Reynolds20118f12015-02-11 12:34:08 -0500503 *
504 * @param context The running context as per {@link #onReceive}.
505 * @param intent The received intent as per {@link #onReceive}.
Craig Lafayettee7ee54e2015-09-21 13:48:53 -0400506 * @deprecated Do not use
Julia Reynolds20118f12015-02-11 12:34:08 -0500507 */
Craig Lafayettee7ee54e2015-09-21 13:48:53 -0400508 @Deprecated
Julia Reynolds94e7bf62015-06-10 14:44:56 -0400509 @SystemApi
Julia Reynolds20118f12015-02-11 12:34:08 -0500510 public void onReadyForUserInitialization(Context context, Intent intent) {
511 }
512
513 /**
Benjamin Franz43261142015-02-11 15:59:44 +0000514 * Called when a device is entering lock task mode.
Jason Monk35c62a42014-06-17 10:24:47 -0400515 *
516 * @param context The running context as per {@link #onReceive}.
517 * @param intent The received intent as per {@link #onReceive}.
Jason Monk35c62a42014-06-17 10:24:47 -0400518 * @param pkg If entering, the authorized package using lock task mode, otherwise null.
519 */
Jason Monk48aacba2014-08-13 16:29:08 -0400520 public void onLockTaskModeEntering(Context context, Intent intent, String pkg) {
521 }
522
523 /**
Benjamin Franz43261142015-02-11 15:59:44 +0000524 * Called when a device is exiting lock task mode.
Jason Monk48aacba2014-08-13 16:29:08 -0400525 *
526 * @param context The running context as per {@link #onReceive}.
527 * @param intent The received intent as per {@link #onReceive}.
528 */
529 public void onLockTaskModeExiting(Context context, Intent intent) {
Jason Monk35c62a42014-06-17 10:24:47 -0400530 }
531
532 /**
Robin Lee3798ed52015-02-03 17:55:31 +0000533 * Allows this receiver to select the alias for a private key and certificate pair for
534 * authentication. If this method returns null, the default {@link android.app.Activity} will be
535 * shown that lets the user pick a private key and certificate pair.
536 *
537 * @param context The running context as per {@link #onReceive}.
538 * @param intent The received intent as per {@link #onReceive}.
539 * @param uid The uid asking for the private key and certificate pair.
Robin Lee39087b12015-05-05 15:57:17 +0100540 * @param uri The URI to authenticate, may be null.
Robin Lee3798ed52015-02-03 17:55:31 +0000541 * @param alias The alias preselected by the client, or null.
542 * @return The private key alias to return and grant access to.
543 * @see KeyChain#choosePrivateKeyAlias
544 */
Robin Lee39087b12015-05-05 15:57:17 +0100545 public String onChoosePrivateKeyAlias(Context context, Intent intent, int uid, Uri uri,
546 String alias) {
Robin Lee3798ed52015-02-03 17:55:31 +0000547 return null;
548 }
549
550 /**
Rubin Xudc105cc2015-04-14 23:38:01 +0100551 * Allows the receiver to be notified when information about a pending system update is
552 * available from the system update service. The same pending system update can trigger multiple
553 * calls to this method, so it is necessary to examine the incoming parameters for details about
554 * the update.
555 * <p>
556 * This callback is only applicable to device owners.
557 *
558 * @param context The running context as per {@link #onReceive}.
559 * @param intent The received intent as per {@link #onReceive}.
560 * @param receivedTime The time as given by {@link System#currentTimeMillis()} indicating when
561 * the current pending update was first available. -1 if no pending update is available.
562 */
563 public void onSystemUpdatePending(Context context, Intent intent, long receivedTime) {
564 }
565
566 /**
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000567 * Called when sharing a bugreport has been cancelled by the user of the device.
568 *
569 * <p>This callback is only applicable to device owners.
570 *
571 * @param context The running context as per {@link #onReceive}.
572 * @param intent The received intent as per {@link #onReceive}.
573 * @see DevicePolicyManager#requestBugreport
574 */
575 public void onBugreportSharingDeclined(Context context, Intent intent) {
576 }
577
578 /**
579 * Called when the bugreport has been shared with the device administrator app.
580 *
581 * <p>This callback is only applicable to device owners.
582 *
583 * @param context The running context as per {@link #onReceive}.
584 * @param intent The received intent as per {@link #onReceive}. Contains the URI of
585 * the bugreport file (with MIME type "application/vnd.android.bugreport"), that can be accessed
586 * by calling {@link Intent#getData()}
587 * @param bugreportHash SHA-256 hash of the bugreport file.
588 * @see DevicePolicyManager#requestBugreport
589 */
590 public void onBugreportShared(Context context, Intent intent, String bugreportHash) {
591 }
592
593 /**
594 * Called when the bugreport collection flow has failed.
595 *
596 * <p>This callback is only applicable to device owners.
597 *
598 * @param context The running context as per {@link #onReceive}.
599 * @param intent The received intent as per {@link #onReceive}.
600 * @param failureCode int containing failure code. One of
601 * #BUGREPORT_FAILURE_FAILED_COMPLETING or #BUGREPORT_FAILURE_FILE_NO_LONGER_AVAILABLE
602 * @see DevicePolicyManager#requestBugreport
603 */
604 public void onBugreportFailed(Context context, Intent intent,
605 @BugreportFailureCode int failureCode) {
606 }
607
608 /**
Rubin Xuc3cd05f2016-01-11 12:11:35 +0000609 * Called when a new batch of device logs can be retrieved.
610 *
611 * <p>This callback is only applicable to device owners.
612 *
613 * @param context The running context as per {@link #onReceive}.
614 * @param intent The received intent as per {@link #onReceive}.
615 * @see DevicePolicyManager#retrieveDeviceLogs(ComponentName)
616 */
617 public void onSecurityLogsAvailable(Context context, Intent intent) {
618 }
619
620 /**
Dianne Hackbornd6847842010-01-12 18:14:19 -0800621 * Intercept standard device administrator broadcasts. Implementations
622 * should not override this method; it is better to implement the
623 * convenience callbacks for each action.
624 */
625 @Override
626 public void onReceive(Context context, Intent intent) {
627 String action = intent.getAction();
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000628
Dianne Hackbornd6847842010-01-12 18:14:19 -0800629 if (ACTION_PASSWORD_CHANGED.equals(action)) {
630 onPasswordChanged(context, intent);
631 } else if (ACTION_PASSWORD_FAILED.equals(action)) {
632 onPasswordFailed(context, intent);
633 } else if (ACTION_PASSWORD_SUCCEEDED.equals(action)) {
634 onPasswordSucceeded(context, intent);
635 } else if (ACTION_DEVICE_ADMIN_ENABLED.equals(action)) {
636 onEnabled(context, intent);
Dianne Hackborn8ea138c2010-01-26 18:01:04 -0800637 } else if (ACTION_DEVICE_ADMIN_DISABLE_REQUESTED.equals(action)) {
638 CharSequence res = onDisableRequested(context, intent);
639 if (res != null) {
640 Bundle extras = getResultExtras(true);
641 extras.putCharSequence(EXTRA_DISABLE_WARNING, res);
642 }
Dianne Hackbornd6847842010-01-12 18:14:19 -0800643 } else if (ACTION_DEVICE_ADMIN_DISABLED.equals(action)) {
644 onDisabled(context, intent);
Jim Millera4e28d12010-11-08 16:15:47 -0800645 } else if (ACTION_PASSWORD_EXPIRING.equals(action)) {
646 onPasswordExpiring(context, intent);
Jessica Hummel8cdb6fc2014-03-03 14:14:51 +0000647 } else if (ACTION_PROFILE_PROVISIONING_COMPLETE.equals(action)) {
648 onProfileProvisioningComplete(context, intent);
Robin Lee3798ed52015-02-03 17:55:31 +0000649 } else if (ACTION_CHOOSE_PRIVATE_KEY_ALIAS.equals(action)) {
Robin Leeabf35702015-02-17 14:12:48 +0000650 int uid = intent.getIntExtra(EXTRA_CHOOSE_PRIVATE_KEY_SENDER_UID, -1);
Robin Lee39087b12015-05-05 15:57:17 +0100651 Uri uri = intent.getParcelableExtra(EXTRA_CHOOSE_PRIVATE_KEY_URI);
Robin Lee3798ed52015-02-03 17:55:31 +0000652 String alias = intent.getStringExtra(EXTRA_CHOOSE_PRIVATE_KEY_ALIAS);
Robin Lee39087b12015-05-05 15:57:17 +0100653 String chosenAlias = onChoosePrivateKeyAlias(context, intent, uid, uri, alias);
Robin Lee3798ed52015-02-03 17:55:31 +0000654 setResultData(chosenAlias);
Jason Monk48aacba2014-08-13 16:29:08 -0400655 } else if (ACTION_LOCK_TASK_ENTERING.equals(action)) {
Jason Monk35c62a42014-06-17 10:24:47 -0400656 String pkg = intent.getStringExtra(EXTRA_LOCK_TASK_PACKAGE);
Jason Monk48aacba2014-08-13 16:29:08 -0400657 onLockTaskModeEntering(context, intent, pkg);
658 } else if (ACTION_LOCK_TASK_EXITING.equals(action)) {
659 onLockTaskModeExiting(context, intent);
Rubin Xudc105cc2015-04-14 23:38:01 +0100660 } else if (ACTION_NOTIFY_PENDING_SYSTEM_UPDATE.equals(action)) {
661 long receivedTime = intent.getLongExtra(EXTRA_SYSTEM_UPDATE_RECEIVED_TIME, -1);
662 onSystemUpdatePending(context, intent, receivedTime);
Michal Karpinski3fc437e2015-12-15 10:09:00 +0000663 } else if (ACTION_BUGREPORT_SHARING_DECLINED.equals(action)) {
664 onBugreportSharingDeclined(context, intent);
665 } else if (ACTION_BUGREPORT_SHARE.equals(action)) {
666 String bugreportFileHash = intent.getStringExtra(EXTRA_BUGREPORT_HASH);
667 onBugreportShared(context, intent, bugreportFileHash);
668 } else if (ACTION_BUGREPORT_FAILED.equals(action)) {
669 int failureCode = intent.getIntExtra(EXTRA_BUGREPORT_FAILURE_REASON,
670 BUGREPORT_FAILURE_FAILED_COMPLETING);
671 onBugreportFailed(context, intent, failureCode);
Rubin Xuc3cd05f2016-01-11 12:11:35 +0000672 } else if (ACTION_SECURITY_LOGS_AVAILABLE.equals(action)) {
673 onSecurityLogsAvailable(context, intent);
Dianne Hackbornd6847842010-01-12 18:14:19 -0800674 }
675 }
676}