Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, 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 | package com.android.managedprovisioning; |
| 17 | |
Alexandra Gherghina | 7cca927 | 2014-11-26 20:57:13 +0000 | [diff] [blame] | 18 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE; |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 19 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE; |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 20 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME; |
| 21 | |
| 22 | import android.app.Notification; |
| 23 | import android.app.NotificationManager; |
| 24 | import android.app.PendingIntent; |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 29 | import android.os.Bundle; |
| 30 | |
| 31 | /** |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 32 | * Class that handles the resuming process that takes place after a reboot for encryption |
Jessica Hummel | 5c99553 | 2014-08-26 16:29:22 +0100 | [diff] [blame] | 33 | * during the provisioning process. |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 34 | */ |
| 35 | public class BootReminder extends BroadcastReceiver { |
| 36 | private static final int NOTIFY_ID = 1; |
| 37 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 38 | /* |
| 39 | * Profile owner parameters that are stored in the IntentStore for resuming provisioning. |
| 40 | */ |
| 41 | private static final String PROFILE_OWNER_PREFERENCES_NAME = |
| 42 | "profile-owner-provisioning-resume"; |
| 43 | |
| 44 | private static final String[] PROFILE_OWNER_STRING_EXTRAS = { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 45 | // Key for the device admin package name |
| 46 | EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME |
| 47 | }; |
| 48 | |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 49 | private static final String[] PROFILE_OWNER_PERSISTABLE_BUNDLE_EXTRAS = { |
| 50 | // Key for the admin extras bundle |
| 51 | EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE |
| 52 | }; |
| 53 | |
Alexandra Gherghina | 7cca927 | 2014-11-26 20:57:13 +0000 | [diff] [blame] | 54 | private static final String[] PROFILE_OWNER_ACCOUNT_EXTRAS = { |
| 55 | // Key for the account extras |
| 56 | EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE |
| 57 | }; |
| 58 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 59 | private static final ComponentName PROFILE_OWNER_INTENT_TARGET = |
Sander Alewijnse | 606780d | 2014-10-29 10:51:39 +0000 | [diff] [blame] | 60 | ProfileOwnerPreProvisioningActivity.ALIAS_NO_CHECK_CALLER; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Device owner parameters that are stored in the IntentStore for resuming provisioning. |
| 64 | */ |
| 65 | private static final String DEVICE_OWNER_PREFERENCES_NAME = |
| 66 | "device-owner-provisioning-resume"; |
| 67 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 68 | private static final ComponentName DEVICE_OWNER_INTENT_TARGET = |
| 69 | new ComponentName("com.android.managedprovisioning", |
| 70 | "com.android.managedprovisioning.DeviceOwnerProvisioningActivity"); |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public void onReceive(Context context, Intent intent) { |
| 74 | if (android.content.Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 75 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 76 | // Resume profile owner provisioning if applicable. |
| 77 | IntentStore profileOwnerIntentStore = getProfileOwnerIntentStore(context); |
| 78 | final Intent resumeProfileOwnerPrvIntent = profileOwnerIntentStore.load(); |
Robin Lee | 4af4ed0 | 2014-08-04 12:29:31 +0100 | [diff] [blame] | 79 | if (resumeProfileOwnerPrvIntent != null) { |
Robin Lee | 7691d19 | 2014-08-29 21:29:38 +0100 | [diff] [blame] | 80 | if (EncryptDeviceActivity.isDeviceEncrypted()) { |
| 81 | // Show reminder notification and then forget about it for next boot |
| 82 | profileOwnerIntentStore.clear(); |
| 83 | setNotification(context, resumeProfileOwnerPrvIntent); |
| 84 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Resume device owner provisioning if applicable. |
| 88 | IntentStore deviceOwnerIntentStore = getDeviceOwnerIntentStore(context); |
| 89 | Intent resumeDeviceOwnerPrvIntent = deviceOwnerIntentStore.load(); |
| 90 | if (resumeDeviceOwnerPrvIntent != null) { |
| 91 | deviceOwnerIntentStore.clear(); |
| 92 | resumeDeviceOwnerPrvIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 93 | context.startActivity(resumeDeviceOwnerPrvIntent); |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Schedule a provisioning reminder notification for the next reboot. |
| 100 | * |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 101 | * {@code extras} should be a Bundle containing the |
| 102 | * {@link EncryptDeviceActivity.EXTRA_RESUME_TARGET}. |
Jessica Hummel | 2e9cec6 | 2014-06-20 11:50:01 +0100 | [diff] [blame] | 103 | * This field has only two supported values {@link EncryptDeviceActivity.TARGET_PROFILE_OWNER} |
| 104 | * and {@link EncryptDeviceActivity.TARGET_DEVICE_OWNER} |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 105 | * |
Jessica Hummel | 2e9cec6 | 2014-06-20 11:50:01 +0100 | [diff] [blame] | 106 | * <p> In case of TARGET_PROFILE_OWNER {@code extras} should further contain a value for at |
| 107 | * least the key: {@link EXTRA_PROVISIONING_DEVICE_ADMIN_PACKAGE_NAME}, a {@link String} which |
| 108 | * specifies the package to set as profile owner. |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 109 | * |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 110 | * <p> |
Sander Alewijnse | d8dcb1f | 2014-07-24 15:24:50 +0100 | [diff] [blame] | 111 | * See {@link MessageParser} for the TARGET_DEVICE_OWNER case. |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 112 | * </ul> |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 113 | * |
Jessica Hummel | 2e9cec6 | 2014-06-20 11:50:01 +0100 | [diff] [blame] | 114 | * <p> These fields will be persisted and restored to the provisioner after rebooting. Any other |
| 115 | * key/value pairs will be ignored. |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 116 | */ |
| 117 | public static void setProvisioningReminder(Context context, Bundle extras) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 118 | IntentStore intentStore; |
| 119 | String resumeTarget = extras.getString(EncryptDeviceActivity.EXTRA_RESUME_TARGET, null); |
| 120 | if (resumeTarget == null) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | if (resumeTarget.equals(EncryptDeviceActivity.TARGET_PROFILE_OWNER)) { |
| 124 | intentStore = getProfileOwnerIntentStore(context); |
| 125 | } else if (resumeTarget.equals(EncryptDeviceActivity.TARGET_DEVICE_OWNER)) { |
| 126 | intentStore = getDeviceOwnerIntentStore(context); |
| 127 | } else { |
| 128 | ProvisionLogger.loge("Unknown resume target for bootreminder."); |
| 129 | return; |
| 130 | } |
| 131 | intentStore.save(extras); |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 132 | } |
| 133 | |
Robin Lee | 563ea17 | 2014-06-20 18:55:31 +0100 | [diff] [blame] | 134 | /** |
| 135 | * Cancel all active provisioning reminders. |
| 136 | */ |
| 137 | public static void cancelProvisioningReminder(Context context) { |
| 138 | getProfileOwnerIntentStore(context).clear(); |
| 139 | getDeviceOwnerIntentStore(context).clear(); |
| 140 | setNotification(context, null); |
| 141 | } |
| 142 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 143 | private static IntentStore getProfileOwnerIntentStore(Context context) { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 144 | return new IntentStore(context,PROFILE_OWNER_INTENT_TARGET, PROFILE_OWNER_PREFERENCES_NAME) |
| 145 | .setStringKeys(PROFILE_OWNER_STRING_EXTRAS) |
Alexandra Gherghina | 7cca927 | 2014-11-26 20:57:13 +0000 | [diff] [blame] | 146 | .setPersistableBundleKeys(PROFILE_OWNER_PERSISTABLE_BUNDLE_EXTRAS) |
| 147 | .setAccountKeys(PROFILE_OWNER_ACCOUNT_EXTRAS); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 148 | } |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 149 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 150 | private static IntentStore getDeviceOwnerIntentStore(Context context) { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 151 | return new IntentStore(context, DEVICE_OWNER_INTENT_TARGET, DEVICE_OWNER_PREFERENCES_NAME) |
| 152 | .setStringKeys(MessageParser.DEVICE_OWNER_STRING_EXTRAS) |
| 153 | .setLongKeys(MessageParser.DEVICE_OWNER_LONG_EXTRAS) |
| 154 | .setIntKeys(MessageParser.DEVICE_OWNER_INT_EXTRAS) |
| 155 | .setBooleanKeys(MessageParser.DEVICE_OWNER_BOOLEAN_EXTRAS) |
| 156 | .setPersistableBundleKeys(MessageParser.DEVICE_OWNER_PERSISTABLE_BUNDLE_EXTRAS); |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** Create and show the provisioning reminder notification. */ |
| 160 | private static void setNotification(Context context, Intent intent) { |
| 161 | final NotificationManager notificationManager = (NotificationManager) |
| 162 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
Robin Lee | 563ea17 | 2014-06-20 18:55:31 +0100 | [diff] [blame] | 163 | if (intent == null) { |
| 164 | notificationManager.cancel(NOTIFY_ID); |
| 165 | return; |
| 166 | } |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 167 | final PendingIntent resumePendingIntent = PendingIntent.getActivity( |
| 168 | context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 169 | final Notification.Builder notify = new Notification.Builder(context) |
| 170 | .setContentIntent(resumePendingIntent) |
| 171 | .setContentTitle(context.getString(R.string.continue_provisioning_notify_title)) |
| 172 | .setContentText(context.getString(R.string.continue_provisioning_notify_text)) |
Alexandra Gherghina | dd5bb0b | 2014-09-18 16:19:15 +0100 | [diff] [blame] | 173 | .setSmallIcon(com.android.internal.R.drawable.ic_corp_statusbar_icon) |
Robin Lee | 4af4ed0 | 2014-08-04 12:29:31 +0100 | [diff] [blame] | 174 | .setVisibility(Notification.VISIBILITY_PUBLIC) |
Selim Cinek | ee3ce60 | 2014-08-27 16:59:20 +0200 | [diff] [blame] | 175 | .setColor(context.getResources().getColor( |
| 176 | com.android.internal.R.color.system_notification_accent_color)) |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 177 | .setAutoCancel(true); |
Robin Lee | 3ab2b57 | 2014-05-06 18:39:07 +0100 | [diff] [blame] | 178 | notificationManager.notify(NOTIFY_ID, notify.build()); |
| 179 | } |
| 180 | } |