Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [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 | |
| 17 | package com.android.managedprovisioning; |
| 18 | |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 19 | import static android.app.admin.DeviceAdminReceiver.ACTION_READY_FOR_USER_INITIALIZATION; |
| 20 | import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE; |
| 21 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 22 | import android.app.Activity; |
| 23 | import android.app.AlertDialog; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 24 | import android.content.BroadcastReceiver; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
| 27 | import android.content.Intent; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 28 | import android.content.IntentFilter; |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 29 | import android.content.pm.ResolveInfo; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 30 | import android.os.Bundle; |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 31 | import android.os.SystemProperties; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 32 | import android.provider.Settings.Global; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 33 | import android.provider.Settings.Secure; |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 34 | import android.support.v4.content.LocalBroadcastManager; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 35 | import android.text.TextUtils; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 36 | import android.view.LayoutInflater; |
| 37 | import android.view.View; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 38 | import android.widget.TextView; |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 39 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 40 | import com.android.managedprovisioning.task.AddWifiNetworkTask; |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 41 | import com.android.setupwizard.navigationbar.SetupWizardNavBar; |
| 42 | import com.android.setupwizard.navigationbar.SetupWizardNavBar.NavigationBarListener; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 43 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 44 | import java.util.ArrayList; |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 45 | import java.util.List; |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 46 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 47 | /** |
| 48 | * This activity starts device owner provisioning: |
| 49 | * It downloads a mobile device management application(mdm) from a given url and installs it, |
| 50 | * or a given mdm is already present on the device. The mdm is set as the owner of the device so |
| 51 | * that it has full control over the device: |
| 52 | * TODO: put link here with documentation on how a device owner has control over the device |
| 53 | * The mdm can then execute further setup steps. |
| 54 | * |
| 55 | * <p> |
| 56 | * An example use case might be when a company wants to set up a device for a single use case |
| 57 | * (such as giving instructions). |
| 58 | * </p> |
| 59 | * |
| 60 | * <p> |
| 61 | * Provisioning is triggered by a programmer device that sends required provisioning parameters via |
| 62 | * nfc. For an example of a programmer app see: |
| 63 | * com.example.android.apis.app.DeviceProvisioningProgrammerSample. |
| 64 | * </p> |
| 65 | * |
Sander Alewijnse | 4c4badf | 2014-03-20 14:12:49 +0000 | [diff] [blame] | 66 | * <p> |
| 67 | * In the unlikely case that this activity is killed the whole provisioning process so far is |
| 68 | * repeated. We made sure that all tasks can be done twice without causing any problems. |
| 69 | * </p> |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 70 | */ |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 71 | public class DeviceOwnerProvisioningActivity extends Activity |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 72 | implements UserConsentDialog.ConsentCallback, NavigationBarListener { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 73 | private static final boolean DEBUG = false; // To control logging. |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 74 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 75 | private static final String KEY_USER_CONSENTED = "user_consented"; |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 76 | private static final String KEY_CANCEL_DIALOG_SHOWN = "cancel_dialog_shown"; |
| 77 | private static final String KEY_PENDING_INTENTS = "pending_intents"; |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 78 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 79 | private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 80 | private static final int WIFI_REQUEST_CODE = 2; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 81 | |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 82 | // Hide default system navigation bar. |
| 83 | protected static final int IMMERSIVE_FLAGS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
| 84 | | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| 85 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 86 | private BroadcastReceiver mServiceMessageReceiver; |
| 87 | private TextView mProgressTextView; |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 88 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 89 | // Indicates whether user consented by clicking on positive button of interstitial. |
| 90 | private boolean mUserConsented = false; |
| 91 | |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 92 | // Params that will be used after user consent. |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 93 | // Extracted from the starting intent. |
| 94 | private ProvisioningParams mParams; |
| 95 | |
| 96 | // Indicates that the cancel dialog is shown. |
| 97 | private boolean mCancelDialogShown = false; |
| 98 | |
| 99 | // List of intents received while cancel dialog is shown. |
| 100 | private ArrayList<Intent> mPendingProvisioningIntents = new ArrayList<Intent>(); |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 101 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 102 | @Override |
| 103 | public void onCreate(Bundle savedInstanceState) { |
| 104 | super.onCreate(savedInstanceState); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 105 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONCREATE"); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 106 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 107 | if (savedInstanceState != null) { |
| 108 | mUserConsented = savedInstanceState.getBoolean(KEY_USER_CONSENTED, false); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 109 | mCancelDialogShown = savedInstanceState.getBoolean(KEY_CANCEL_DIALOG_SHOWN, false); |
| 110 | mPendingProvisioningIntents = savedInstanceState |
| 111 | .getParcelableArrayList(KEY_PENDING_INTENTS); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 114 | // Setup the UI. |
| 115 | final LayoutInflater inflater = getLayoutInflater(); |
| 116 | final View contentView = inflater.inflate(R.layout.progress, null); |
| 117 | setContentView(contentView); |
| 118 | mProgressTextView = (TextView) findViewById(R.id.prog_text); |
| 119 | TextView titleText = (TextView) findViewById(R.id.title); |
Sander Alewijnse | a36dd99 | 2015-01-15 16:41:43 +0000 | [diff] [blame] | 120 | if (titleText != null) titleText.setText(getString(R.string.setup_work_device)); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 121 | if (mCancelDialogShown) showCancelResetDialog(); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 122 | |
Joe Delfino | 84e56f5 | 2015-03-27 09:56:18 -0400 | [diff] [blame] | 123 | // Check whether we have already provisioned this user. |
| 124 | if (Utils.isCurrentUserOwner()) { |
| 125 | int provisioned = |
| 126 | Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, 0 /*default*/); |
| 127 | if (provisioned != 0) { |
| 128 | ProvisionLogger.loge("Device already provisioned."); |
| 129 | error(R.string.device_owner_error_already_provisioned, |
| 130 | false /* no factory reset */); |
| 131 | return; |
| 132 | } |
| 133 | } else { |
| 134 | int provisioned = |
| 135 | Secure.getInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 0 /*default*/); |
| 136 | if (provisioned != 0) { |
| 137 | ProvisionLogger.loge("User already provisioned."); |
| 138 | error(R.string.device_owner_error_already_provisioned_user, |
| 139 | false /* no factory reset */); |
| 140 | return; |
| 141 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 142 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 143 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 144 | // Setup broadcast receiver for feedback from service. |
| 145 | mServiceMessageReceiver = new ServiceMessageReceiver(); |
| 146 | IntentFilter filter = new IntentFilter(); |
| 147 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS); |
| 148 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR); |
| 149 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 150 | LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 151 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 152 | // Parse the incoming intent. |
| 153 | MessageParser parser = new MessageParser(); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 154 | try { |
Nicolas Prevot | 668d65f | 2015-03-10 17:58:15 +0000 | [diff] [blame] | 155 | mParams = parser.parseIntent(getIntent()); |
Nicolas Prevot | 0b44725 | 2015-03-09 14:59:02 +0000 | [diff] [blame] | 156 | } catch (Utils.IllegalProvisioningArgumentException e) { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 157 | ProvisionLogger.loge("Could not read data from intent", e); |
Nicolas Prevot | 0b44725 | 2015-03-09 14:59:02 +0000 | [diff] [blame] | 158 | error(R.string.device_owner_error_general, false /* no factory reset */); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 159 | return; |
| 160 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 161 | |
| 162 | // Ask to encrypt the device before proceeding |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 163 | if (!(EncryptDeviceActivity.isDeviceEncrypted() |
Julia Reynolds | 5d47b65 | 2015-02-02 06:47:25 -0500 | [diff] [blame] | 164 | || SystemProperties.getBoolean("persist.sys.no_req_encrypt", false) |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 165 | || mParams.skipEncryption)) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 166 | requestEncryption(parser, mParams); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 167 | finish(); |
| 168 | return; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 169 | // System will reboot. Bootreminder will restart this activity. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 172 | // Have the user pick a wifi network if necessary. |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 173 | if (!AddWifiNetworkTask.isConnectedToWifi(this) && TextUtils.isEmpty(mParams.wifiInfo.ssid) |
| 174 | && !mParams.bluetoothInfo.useProxy) { |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 175 | requestWifiPick(); |
| 176 | return; |
| 177 | // Wait for onActivityResult. |
| 178 | } |
| 179 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 180 | showInterstitialAndProvision(mParams); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | private void showInterstitialAndProvision(final ProvisioningParams params) { |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 184 | if (mUserConsented || params.startedByNfc || !Utils.isCurrentUserOwner()) { |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 185 | startDeviceOwnerProvisioningService(params); |
| 186 | } else { |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 187 | // Notify the user that the admin will have full control over the device, |
| 188 | // then start provisioning. |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 189 | UserConsentDialog.newInstance(UserConsentDialog.DEVICE_OWNER) |
| 190 | .show(getFragmentManager(), "UserConsentDialogFragment"); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 191 | } |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 194 | @Override |
| 195 | public void onDialogConsent() { |
| 196 | mUserConsented = true; |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 197 | startDeviceOwnerProvisioningService(mParams); |
Sander Alewijnse | 3bad02d | 2014-10-02 14:57:05 +0100 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void onDialogCancel() { |
| 202 | finish(); |
| 203 | } |
| 204 | |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 205 | private void startDeviceOwnerProvisioningService(ProvisioningParams params) { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 206 | Intent intent = new Intent(this, DeviceOwnerProvisioningService.class); |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 207 | intent.putExtra(DeviceOwnerProvisioningService.EXTRA_PROVISIONING_PARAMS, params); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 208 | intent.putExtras(getIntent()); |
| 209 | startService(intent); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 212 | class ServiceMessageReceiver extends BroadcastReceiver |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 213 | { |
| 214 | @Override |
| 215 | public void onReceive(Context context, Intent intent) |
| 216 | { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 217 | if (mCancelDialogShown) { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 218 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 219 | // Postpone handling the intent. |
| 220 | mPendingProvisioningIntents.add(intent); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 221 | return; |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 222 | } |
| 223 | handleProvisioningIntent(intent); |
| 224 | } |
| 225 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 226 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 227 | private void handleProvisioningIntent(Intent intent) { |
| 228 | String action = intent.getAction(); |
| 229 | if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS)) { |
| 230 | if (DEBUG) ProvisionLogger.logd("Successfully provisioned"); |
| 231 | onProvisioningSuccess(); |
| 232 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR)) { |
| 233 | int errorMessageId = intent.getIntExtra( |
| 234 | DeviceOwnerProvisioningService.EXTRA_USER_VISIBLE_ERROR_ID_KEY, |
| 235 | R.string.device_owner_error_general); |
| 236 | |
| 237 | if (DEBUG) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 238 | ProvisionLogger.logd("Error reported with code " |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 239 | + getResources().getString(errorMessageId)); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 240 | } |
| 241 | error(errorMessageId, true /* always factory reset */); |
| 242 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE)) { |
| 243 | int progressMessage = intent.getIntExtra( |
| 244 | DeviceOwnerProvisioningService.EXTRA_PROGRESS_MESSAGE_ID_KEY, -1); |
| 245 | if (DEBUG) { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 246 | ProvisionLogger.logd("Progress update reported with code " |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 247 | + getResources().getString(progressMessage)); |
| 248 | } |
| 249 | if (progressMessage >= 0) { |
| 250 | progressUpdate(progressMessage); |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 251 | } |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 255 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 256 | private void onProvisioningSuccess() { |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 257 | if (mParams.deviceInitializerComponentName != null) { |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 258 | Intent result = new Intent(ACTION_READY_FOR_USER_INITIALIZATION); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 259 | result.setComponent(mParams.deviceInitializerComponentName); |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 260 | result.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES | |
| 261 | Intent.FLAG_RECEIVER_FOREGROUND); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 262 | if (mParams.adminExtrasBundle != null) { |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 263 | result.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 264 | mParams.adminExtrasBundle); |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 265 | } |
| 266 | List<ResolveInfo> matchingReceivers = |
| 267 | getPackageManager().queryBroadcastReceivers(result, 0); |
| 268 | if (matchingReceivers.size() > 0) { |
| 269 | // Notify the device initializer that it can now perform pre-user-setup tasks. |
| 270 | sendBroadcast(result); |
| 271 | } else { |
| 272 | ProvisionLogger.logi("Initializer component doesn't have a receiver for " |
| 273 | + "android.app.action.READY_FOR_USER_INITIALIZATION. Skipping broadcast " |
| 274 | + "and finishing user initialization."); |
| 275 | provisionDevice(); |
| 276 | } |
| 277 | } else { |
| 278 | // No initializer, set the device provisioned ourselves. |
| 279 | provisionDevice(); |
| 280 | } |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 281 | // Note: the DeviceOwnerProvisioningService will stop itself. |
Sander Alewijnse | 1c8d931 | 2014-08-18 20:00:49 +0100 | [diff] [blame] | 282 | setResult(Activity.RESULT_OK); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 283 | finish(); |
| 284 | } |
| 285 | |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 286 | private void provisionDevice() { |
Joe Delfino | 84e56f5 | 2015-03-27 09:56:18 -0400 | [diff] [blame] | 287 | if (Utils.isCurrentUserOwner()) { |
| 288 | // This only needs to be set once per device |
| 289 | Global.putInt(getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 290 | } |
| 291 | |
| 292 | // Setting this flag will either cause Setup Wizard to finish immediately when it starts (if |
| 293 | // it is not already running), or when its next activity starts (if it is already running, |
| 294 | // e.g. the non-NFC flow). |
| 295 | // When either of these things happen, a home intent is fired. We catch that in |
| 296 | // HomeReceiverActivity before sending the intent to notify the mdm that provisioning is |
| 297 | // complete. |
| 298 | // Note that, in the NFC flow or for secondary users, setting this flag will prevent the |
| 299 | // user from seeing SUW, even if no other device initialization app was specified. |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 300 | Secure.putInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 1); |
| 301 | } |
| 302 | |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 303 | private void requestEncryption(MessageParser messageParser, ProvisioningParams params) { |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 304 | Intent encryptIntent = new Intent(DeviceOwnerProvisioningActivity.this, |
| 305 | EncryptDeviceActivity.class); |
| 306 | |
| 307 | Bundle resumeExtras = new Bundle(); |
| 308 | resumeExtras.putString(EncryptDeviceActivity.EXTRA_RESUME_TARGET, |
| 309 | EncryptDeviceActivity.TARGET_DEVICE_OWNER); |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 310 | messageParser.addProvisioningParamsToBundle(resumeExtras, params); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 311 | |
| 312 | encryptIntent.putExtra(EncryptDeviceActivity.EXTRA_RESUME, resumeExtras); |
| 313 | |
| 314 | startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE); |
| 315 | } |
| 316 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 317 | private void requestWifiPick() { |
| 318 | startActivityForResult(AddWifiNetworkTask.getWifiPickIntent(), WIFI_REQUEST_CODE); |
| 319 | } |
| 320 | |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 321 | @Override |
| 322 | public void onBackPressed() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 323 | if (mCancelDialogShown) { |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | mCancelDialogShown = true; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 328 | showCancelResetDialog(); |
| 329 | } |
| 330 | |
| 331 | private void showCancelResetDialog() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 332 | new AlertDialog.Builder(DeviceOwnerProvisioningActivity.this) |
| 333 | .setCancelable(false) |
| 334 | .setTitle(R.string.device_owner_cancel_title) |
| 335 | .setMessage(R.string.device_owner_cancel_message) |
| 336 | .setNegativeButton(R.string.device_owner_cancel_cancel, |
| 337 | new DialogInterface.OnClickListener() { |
| 338 | @Override |
| 339 | public void onClick(DialogInterface dialog,int id) { |
| 340 | dialog.dismiss(); |
| 341 | handlePendingIntents(); |
| 342 | mCancelDialogShown = false; |
| 343 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 344 | }) |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 345 | .setPositiveButton(R.string.device_owner_error_reset, |
| 346 | new DialogInterface.OnClickListener() { |
| 347 | @Override |
| 348 | public void onClick(DialogInterface dialog,int id) { |
| 349 | dialog.dismiss(); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 350 | |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 351 | // Factory reset the device. |
| 352 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 353 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 354 | intent.putExtra(Intent.EXTRA_REASON, |
| 355 | "DeviceOwnerProvisioningActivity.showCancelResetDialog()"); |
| 356 | sendBroadcast(intent); |
| 357 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 358 | DeviceOwnerProvisioningService.class)); |
| 359 | finish(); |
| 360 | } |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 361 | }) |
| 362 | .show() |
| 363 | .getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | private void handlePendingIntents() { |
| 367 | for (Intent intent : mPendingProvisioningIntents) { |
| 368 | if (DEBUG) ProvisionLogger.logd("Handling pending intent " + intent.getAction()); |
| 369 | handleProvisioningIntent(intent); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 370 | } |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 371 | mPendingProvisioningIntents.clear(); |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 374 | private void progressUpdate(int progressMessage) { |
| 375 | mProgressTextView.setText(progressMessage); |
| 376 | } |
| 377 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 378 | @Override |
| 379 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 380 | if (requestCode == ENCRYPT_DEVICE_REQUEST_CODE) { |
| 381 | if (resultCode == RESULT_CANCELED) { |
| 382 | ProvisionLogger.loge("User canceled device encryption."); |
| 383 | finish(); |
| 384 | } |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 385 | } else if (requestCode == WIFI_REQUEST_CODE) { |
| 386 | if (resultCode == RESULT_CANCELED) { |
| 387 | ProvisionLogger.loge("User canceled wifi picking."); |
| 388 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 389 | DeviceOwnerProvisioningService.class)); |
| 390 | finish(); |
| 391 | } else if (resultCode == RESULT_OK) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 392 | if (DEBUG) ProvisionLogger.logd("Wifi request result is OK"); |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 393 | if (AddWifiNetworkTask.isConnectedToWifi(this)) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 394 | showInterstitialAndProvision(mParams); |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 395 | } else { |
| 396 | requestWifiPick(); |
| 397 | } |
| 398 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 402 | private void error(int dialogMessage, boolean resetRequired) { |
| 403 | AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this) |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 404 | .setTitle(R.string.provisioning_error_title) |
| 405 | .setMessage(dialogMessage) |
| 406 | .setCancelable(false); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 407 | if (resetRequired) { |
| 408 | alertBuilder.setPositiveButton(R.string.device_owner_error_reset, |
| 409 | new DialogInterface.OnClickListener() { |
| 410 | @Override |
| 411 | public void onClick(DialogInterface dialog,int id) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 412 | dialog.dismiss(); |
| 413 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 414 | // Factory reset the device. |
Jeff Sharkey | 56ebde0 | 2014-09-24 13:58:43 -0700 | [diff] [blame] | 415 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 416 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 417 | intent.putExtra(Intent.EXTRA_REASON, |
| 418 | "DeviceOwnerProvisioningActivity.error()"); |
| 419 | sendBroadcast(intent); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 420 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 421 | DeviceOwnerProvisioningService.class)); |
| 422 | finish(); |
| 423 | } |
| 424 | }); |
| 425 | } else { |
| 426 | alertBuilder.setPositiveButton(R.string.device_owner_error_ok, |
| 427 | new DialogInterface.OnClickListener() { |
| 428 | @Override |
| 429 | public void onClick(DialogInterface dialog,int id) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 430 | dialog.dismiss(); |
| 431 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 432 | // Close activity. |
| 433 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 434 | DeviceOwnerProvisioningService.class)); |
| 435 | finish(); |
| 436 | } |
| 437 | }); |
| 438 | } |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 439 | alertBuilder.show().getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 440 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 441 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 442 | @Override |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 443 | protected void onSaveInstanceState(Bundle outState) { |
| 444 | outState.putBoolean(KEY_USER_CONSENTED, mUserConsented); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 445 | outState.putBoolean(KEY_CANCEL_DIALOG_SHOWN, mCancelDialogShown); |
| 446 | outState.putParcelableArrayList(KEY_PENDING_INTENTS, mPendingProvisioningIntents); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 450 | public void onDestroy() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 451 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONDESTROY"); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 452 | if (mServiceMessageReceiver != null) { |
| 453 | LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver); |
| 454 | mServiceMessageReceiver = null; |
| 455 | } |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 456 | super.onDestroy(); |
| 457 | } |
| 458 | |
| 459 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 460 | protected void onRestart() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 461 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESTART"); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 462 | super.onRestart(); |
| 463 | } |
| 464 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 465 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 466 | protected void onResume() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 467 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESUME"); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 468 | super.onResume(); |
| 469 | } |
| 470 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 471 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 472 | protected void onPause() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 473 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONPAUSE"); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 474 | super.onPause(); |
| 475 | } |
| 476 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 477 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 478 | protected void onStop() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 479 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONSTOP"); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 480 | super.onStop(); |
| 481 | } |
Rubin Xu | 44cdbdf | 2014-11-28 15:19:14 +0000 | [diff] [blame] | 482 | |
| 483 | @Override |
| 484 | public void onNavigationBarCreated(SetupWizardNavBar bar) { |
| 485 | bar.getNextButton().setVisibility(View.INVISIBLE); |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public void onNavigateBack() { |
| 490 | onBackPressed(); |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | public void onNavigateNext() { |
| 495 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 496 | } |
| 497 | |