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