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