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 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 21 | import android.app.Dialog; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 26 | import android.content.IntentFilter; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 27 | import android.os.Bundle; |
Sander Alewijnse | 8de999b | 2014-08-27 17:42:48 +0100 | [diff] [blame] | 28 | import android.os.UserHandle; |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 29 | import android.os.SystemProperties; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 30 | import android.provider.Settings.Global; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 31 | import android.provider.Settings.Secure; |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 32 | import android.support.v4.content.LocalBroadcastManager; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 33 | import android.text.TextUtils; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 34 | import android.view.LayoutInflater; |
| 35 | import android.view.View; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 36 | import android.widget.TextView; |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 37 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 38 | import com.android.managedprovisioning.task.AddWifiNetworkTask; |
| 39 | |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 40 | import java.util.Locale; |
| 41 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 42 | /** |
| 43 | * This activity starts device owner provisioning: |
| 44 | * It downloads a mobile device management application(mdm) from a given url and installs it, |
| 45 | * or a given mdm is already present on the device. The mdm is set as the owner of the device so |
| 46 | * that it has full control over the device: |
| 47 | * TODO: put link here with documentation on how a device owner has control over the device |
| 48 | * The mdm can then execute further setup steps. |
| 49 | * |
| 50 | * <p> |
| 51 | * An example use case might be when a company wants to set up a device for a single use case |
| 52 | * (such as giving instructions). |
| 53 | * </p> |
| 54 | * |
| 55 | * <p> |
| 56 | * Provisioning is triggered by a programmer device that sends required provisioning parameters via |
| 57 | * nfc. For an example of a programmer app see: |
| 58 | * com.example.android.apis.app.DeviceProvisioningProgrammerSample. |
| 59 | * </p> |
| 60 | * |
Sander Alewijnse | 4c4badf | 2014-03-20 14:12:49 +0000 | [diff] [blame] | 61 | * <p> |
| 62 | * In the unlikely case that this activity is killed the whole provisioning process so far is |
| 63 | * repeated. We made sure that all tasks can be done twice without causing any problems. |
| 64 | * </p> |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 65 | */ |
| 66 | public class DeviceOwnerProvisioningActivity extends Activity { |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 67 | private static final String KEY_USER_CONSENTED = "user_consented"; |
| 68 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 69 | private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 70 | private static final int WIFI_REQUEST_CODE = 2; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 71 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 72 | private BroadcastReceiver mServiceMessageReceiver; |
| 73 | private TextView mProgressTextView; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 74 | private Dialog mDialog; // The cancel or error dialog that is currently shown. |
| 75 | private boolean mDone; // Indicates whether the service has sent ACTION_PROVISIONING_SUCCESS. |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 76 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 77 | // Run when wifi picker activity reports success. |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 78 | private Runnable mOnWifiConnectedRunnable; |
| 79 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 80 | // Indicates whether user consented by clicking on positive button of interstitial. |
| 81 | private boolean mUserConsented = false; |
| 82 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 83 | @Override |
| 84 | public void onCreate(Bundle savedInstanceState) { |
| 85 | super.onCreate(savedInstanceState); |
| 86 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 87 | if (savedInstanceState != null) { |
| 88 | mUserConsented = savedInstanceState.getBoolean(KEY_USER_CONSENTED, false); |
| 89 | } |
| 90 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 91 | ProvisionLogger.logd("Device owner provisioning activity ONCREATE"); |
| 92 | |
| 93 | // Check whether we can provision. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 94 | if (Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, 0 /* default */) != 0) { |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 95 | ProvisionLogger.loge("Device already provisioned."); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 96 | error(R.string.device_owner_error_already_provisioned, false /* no factory reset */); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 99 | |
Sander Alewijnse | 8de999b | 2014-08-27 17:42:48 +0100 | [diff] [blame] | 100 | if (UserHandle.myUserId() != UserHandle.USER_OWNER) { |
| 101 | ProvisionLogger.loge("Device owner can only be set up for USER_OWNER."); |
| 102 | error(R.string.device_owner_error_general, false /* no factory reset */); |
| 103 | return; |
| 104 | } |
| 105 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 106 | // Setup the UI. |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 107 | final LayoutInflater inflater = getLayoutInflater(); |
Sander Alewijnse | 8ce0c51 | 2014-06-03 17:49:02 +0100 | [diff] [blame] | 108 | final View contentView = inflater.inflate(R.layout.progress, null); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 109 | setContentView(contentView); |
Sander Alewijnse | 8ce0c51 | 2014-06-03 17:49:02 +0100 | [diff] [blame] | 110 | mProgressTextView = (TextView) findViewById(R.id.prog_text); |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 111 | TextView titleText = (TextView) findViewById(R.id.title); |
| 112 | if (titleText != null) titleText.setText(getString(R.string.setup_device)); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 113 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 114 | // Setup broadcast receiver for feedback from service. |
| 115 | mServiceMessageReceiver = new ServiceMessageReceiver(); |
| 116 | IntentFilter filter = new IntentFilter(); |
| 117 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS); |
| 118 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR); |
| 119 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 120 | LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 121 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 122 | // Parse the incoming intent. |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 123 | final ProvisioningParams params; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 124 | MessageParser parser = new MessageParser(); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 125 | try { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 126 | params = parser.parseIntent(getIntent()); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 127 | mOnWifiConnectedRunnable = new Runnable() { |
| 128 | public void run() { |
| 129 | showInterstitialAndProvision(params); |
| 130 | } |
| 131 | }; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 132 | } catch (MessageParser.ParseException e) { |
| 133 | ProvisionLogger.loge("Could not read data from intent", e); |
| 134 | error(e.getErrorMessageId(), false /* no factory reset */); |
| 135 | return; |
| 136 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 137 | |
| 138 | // Ask to encrypt the device before proceeding |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 139 | if (!(EncryptDeviceActivity.isDeviceEncrypted() |
| 140 | || SystemProperties.getBoolean("persist.sys.no_req_encrypt", false))) { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 141 | requestEncryption(parser, params); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 142 | finish(); |
| 143 | return; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 144 | // System will reboot. Bootreminder will restart this activity. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 147 | // Have the user pick a wifi network if necessary. |
Sander Alewijnse | ab2e3ea | 2014-09-23 18:00:26 +0100 | [diff] [blame] | 148 | if (!AddWifiNetworkTask.isConnectedToWifi(this) && TextUtils.isEmpty(params.mWifiSsid)) { |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 149 | requestWifiPick(); |
| 150 | return; |
| 151 | // Wait for onActivityResult. |
| 152 | } |
| 153 | |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 154 | showInterstitialAndProvision(params); |
| 155 | } |
| 156 | |
| 157 | private void showInterstitialAndProvision(final ProvisioningParams params) { |
| 158 | if (mUserConsented || params.mStartedByNfc) { |
| 159 | startDeviceOwnerProvisioningService(params); |
| 160 | } else { |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 161 | // Notify the user that the admin will have full control over the device, |
| 162 | // then start provisioning. |
Sander Alewijnse | 7a85694 | 2014-09-08 20:46:40 +0100 | [diff] [blame] | 163 | new UserConsentDialog(this, UserConsentDialog.DEVICE_OWNER, new Runnable() { |
| 164 | @Override |
| 165 | public void run() { |
| 166 | mUserConsented = true; |
| 167 | startDeviceOwnerProvisioningService(params); |
| 168 | } |
| 169 | } /* onUserConsented */ , new Runnable() { |
| 170 | @Override |
| 171 | public void run() { |
| 172 | finish(); |
| 173 | } |
| 174 | } /* onCancel */).show(getFragmentManager(), |
| 175 | "UserConsentDialogFragment"); |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 176 | } |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 179 | private void startDeviceOwnerProvisioningService(ProvisioningParams params) { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 180 | Intent intent = new Intent(this, DeviceOwnerProvisioningService.class); |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 181 | intent.putExtra(DeviceOwnerProvisioningService.EXTRA_PROVISIONING_PARAMS, params); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 182 | intent.putExtras(getIntent()); |
| 183 | startService(intent); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 186 | class ServiceMessageReceiver extends BroadcastReceiver |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 187 | { |
| 188 | @Override |
| 189 | public void onReceive(Context context, Intent intent) |
| 190 | { |
| 191 | String action = intent.getAction(); |
| 192 | if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS)) { |
| 193 | ProvisionLogger.logd("Successfully provisioned"); |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 194 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 195 | synchronized(this) { |
| 196 | if (mDialog == null) { |
| 197 | onProvisioningSuccess(); |
| 198 | } else { |
| 199 | // Postpone finishing this activity till the user has decided whether |
| 200 | // he/she wants to reset or not. |
| 201 | mDone = true; |
| 202 | } |
| 203 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 204 | return; |
| 205 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR)) { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 206 | int errorMessageId = intent.getIntExtra( |
| 207 | DeviceOwnerProvisioningService.EXTRA_USER_VISIBLE_ERROR_ID_KEY, |
| 208 | R.string.device_owner_error_general); |
| 209 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 210 | ProvisionLogger.logd("Error reported with code " |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 211 | + getResources().getString(errorMessageId)); |
| 212 | error(errorMessageId, true /* always factory reset */); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 213 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE)) { |
| 214 | int progressMessage = intent.getIntExtra( |
| 215 | DeviceOwnerProvisioningService.EXTRA_PROGRESS_MESSAGE_ID_KEY, -1); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 216 | ProvisionLogger.logd("Progress update reported with code " |
| 217 | + getResources().getString(progressMessage)); |
| 218 | if (progressMessage >= 0) { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 219 | progressUpdate(progressMessage); |
Sander Alewijnse | af8413e | 2014-03-19 11:37:44 +0000 | [diff] [blame] | 220 | } |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 221 | } |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 225 | private void onProvisioningSuccess() { |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 226 | // The Setup wizards listens to this flag and finishes itself when it is set. |
| 227 | // It then fires a home intent, which we catch in the HomeReceiverActivity before sending |
| 228 | // the intent to notify the mdm that provisioning is complete. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 229 | Global.putInt(getContentResolver(), Global.DEVICE_PROVISIONED, 1); |
| 230 | Secure.putInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 1); |
| 231 | |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 232 | // Note: the DeviceOwnerProvisioningService will stop itself. |
Sander Alewijnse | 1c8d931 | 2014-08-18 20:00:49 +0100 | [diff] [blame] | 233 | setResult(Activity.RESULT_OK); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 234 | finish(); |
| 235 | } |
| 236 | |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 237 | private void requestEncryption(MessageParser messageParser, ProvisioningParams params) { |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 238 | Intent encryptIntent = new Intent(DeviceOwnerProvisioningActivity.this, |
| 239 | EncryptDeviceActivity.class); |
| 240 | |
| 241 | Bundle resumeExtras = new Bundle(); |
| 242 | resumeExtras.putString(EncryptDeviceActivity.EXTRA_RESUME_TARGET, |
| 243 | EncryptDeviceActivity.TARGET_DEVICE_OWNER); |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 244 | messageParser.addProvisioningParamsToBundle(resumeExtras, params); |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 245 | |
| 246 | encryptIntent.putExtra(EncryptDeviceActivity.EXTRA_RESUME, resumeExtras); |
| 247 | |
| 248 | startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE); |
| 249 | } |
| 250 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 251 | private void requestWifiPick() { |
| 252 | startActivityForResult(AddWifiNetworkTask.getWifiPickIntent(), WIFI_REQUEST_CODE); |
| 253 | } |
| 254 | |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 255 | @Override |
| 256 | public void onBackPressed() { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 257 | showCancelResetDialog(); |
| 258 | } |
| 259 | |
| 260 | private void showCancelResetDialog() { |
| 261 | AlertDialog.Builder alertBuilder = |
| 262 | new AlertDialog.Builder(DeviceOwnerProvisioningActivity.this) |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 263 | .setCancelable(false) |
| 264 | .setTitle(R.string.device_owner_cancel_title) |
| 265 | .setMessage(R.string.device_owner_cancel_message) |
| 266 | .setNegativeButton(R.string.device_owner_cancel_cancel, |
| 267 | new DialogInterface.OnClickListener() { |
| 268 | @Override |
| 269 | public void onClick(DialogInterface dialog,int id) { |
| 270 | dialog.dismiss(); |
| 271 | synchronized(this) { |
| 272 | mDialog = null; |
| 273 | if (mDone) { |
| 274 | onProvisioningSuccess(); |
| 275 | } |
| 276 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 277 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 278 | }) |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 279 | .setPositiveButton(R.string.device_owner_error_reset, |
| 280 | new DialogInterface.OnClickListener() { |
| 281 | @Override |
| 282 | public void onClick(DialogInterface dialog,int id) { |
| 283 | // Factory reset the device. |
Jeff Sharkey | 56ebde0 | 2014-09-24 13:58:43 -0700 | [diff] [blame] | 284 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 285 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 286 | intent.putExtra(Intent.EXTRA_REASON, |
| 287 | "DeviceOwnerProvisioningActivity.showCancelResetDialog()"); |
| 288 | sendBroadcast(intent); |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 289 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 290 | DeviceOwnerProvisioningService.class)); |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 291 | finish(); |
| 292 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 293 | }); |
| 294 | |
| 295 | if (mDialog != null) { |
| 296 | mDialog.dismiss(); |
| 297 | } |
| 298 | mDialog = alertBuilder.create(); |
| 299 | mDialog.show(); |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 302 | private void progressUpdate(int progressMessage) { |
| 303 | mProgressTextView.setText(progressMessage); |
| 304 | } |
| 305 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 306 | @Override |
| 307 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 308 | if (requestCode == ENCRYPT_DEVICE_REQUEST_CODE) { |
| 309 | if (resultCode == RESULT_CANCELED) { |
| 310 | ProvisionLogger.loge("User canceled device encryption."); |
| 311 | finish(); |
| 312 | } |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 313 | } else if (requestCode == WIFI_REQUEST_CODE) { |
| 314 | if (resultCode == RESULT_CANCELED) { |
| 315 | ProvisionLogger.loge("User canceled wifi picking."); |
| 316 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 317 | DeviceOwnerProvisioningService.class)); |
| 318 | finish(); |
| 319 | } else if (resultCode == RESULT_OK) { |
| 320 | ProvisionLogger.logd("Wifi request result is OK"); |
| 321 | if (AddWifiNetworkTask.isConnectedToWifi(this)) { |
Sander Alewijnse | f88f709 | 2014-08-20 16:26:09 +0100 | [diff] [blame] | 322 | mOnWifiConnectedRunnable.run(); |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 323 | } else { |
| 324 | requestWifiPick(); |
| 325 | } |
| 326 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 330 | private void error(int dialogMessage, boolean resetRequired) { |
| 331 | AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this) |
Jessica Hummel | c3fc45e | 2014-09-01 19:01:12 +0100 | [diff] [blame] | 332 | .setTitle(R.string.provisioning_error_title) |
| 333 | .setMessage(dialogMessage) |
| 334 | .setCancelable(false); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 335 | if (resetRequired) { |
| 336 | alertBuilder.setPositiveButton(R.string.device_owner_error_reset, |
| 337 | new DialogInterface.OnClickListener() { |
| 338 | @Override |
| 339 | public void onClick(DialogInterface dialog,int id) { |
| 340 | // Factory reset the device. |
Jeff Sharkey | 56ebde0 | 2014-09-24 13:58:43 -0700 | [diff] [blame] | 341 | Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR); |
| 342 | intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
| 343 | intent.putExtra(Intent.EXTRA_REASON, |
| 344 | "DeviceOwnerProvisioningActivity.error()"); |
| 345 | sendBroadcast(intent); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 346 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 347 | DeviceOwnerProvisioningService.class)); |
| 348 | finish(); |
| 349 | } |
| 350 | }); |
| 351 | } else { |
| 352 | alertBuilder.setPositiveButton(R.string.device_owner_error_ok, |
| 353 | new DialogInterface.OnClickListener() { |
| 354 | @Override |
| 355 | public void onClick(DialogInterface dialog,int id) { |
| 356 | // Close activity. |
| 357 | stopService(new Intent(DeviceOwnerProvisioningActivity.this, |
| 358 | DeviceOwnerProvisioningService.class)); |
| 359 | finish(); |
| 360 | } |
| 361 | }); |
| 362 | } |
| 363 | mDialog = alertBuilder.create(); |
| 364 | mDialog.show(); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 365 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 366 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 367 | @Override |
Sander Alewijnse | 8f9bd13 | 2014-09-08 12:21:26 +0100 | [diff] [blame] | 368 | protected void onSaveInstanceState(Bundle outState) { |
| 369 | outState.putBoolean(KEY_USER_CONSENTED, mUserConsented); |
| 370 | } |
| 371 | |
| 372 | @Override |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 373 | public void onDestroy() { |
| 374 | ProvisionLogger.logd("Device owner provisioning activity ONDESTROY"); |
| 375 | if (mServiceMessageReceiver != null) { |
| 376 | LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver); |
| 377 | mServiceMessageReceiver = null; |
| 378 | } |
| 379 | if (mDialog != null) { |
| 380 | mDialog.dismiss(); |
| 381 | mDialog = null; |
| 382 | } |
| 383 | super.onDestroy(); |
| 384 | } |
| 385 | |
| 386 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 387 | protected void onRestart() { |
| 388 | ProvisionLogger.logd("Device owner provisioning activity ONRESTART"); |
| 389 | super.onRestart(); |
| 390 | } |
| 391 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 392 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 393 | protected void onResume() { |
| 394 | ProvisionLogger.logd("Device owner provisioning activity ONRESUME"); |
| 395 | super.onResume(); |
| 396 | } |
| 397 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 398 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 399 | protected void onPause() { |
| 400 | ProvisionLogger.logd("Device owner provisioning activity ONPAUSE"); |
| 401 | super.onPause(); |
| 402 | } |
| 403 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 404 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 405 | protected void onStop() { |
| 406 | ProvisionLogger.logd("Device owner provisioning activity ONSTOP"); |
| 407 | super.onStop(); |
| 408 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 409 | } |
| 410 | |