blob: 646fc836e944fdc448a5b9b9431537d9ebf78072 [file] [log] [blame]
Sander Alewijnseed0883b2014-03-18 15:01:13 +00001/*
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
17package com.android.managedprovisioning;
18
Julia Reynolds3f4eb372015-02-11 13:46:42 -050019import static android.app.admin.DeviceAdminReceiver.ACTION_READY_FOR_USER_INITIALIZATION;
20import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21
Sander Alewijnseed0883b2014-03-18 15:01:13 +000022import android.app.Activity;
23import android.app.AlertDialog;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010024import android.content.BroadcastReceiver;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000025import android.content.Context;
26import android.content.DialogInterface;
27import android.content.Intent;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010028import android.content.IntentFilter;
Julia Reynolds3f4eb372015-02-11 13:46:42 -050029import android.content.pm.ResolveInfo;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000030import android.os.Bundle;
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010031import android.os.SystemProperties;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000032import android.provider.Settings.Global;
Sander Alewijnsed7043852014-06-17 15:50:48 +010033import android.provider.Settings.Secure;
Paul Crowleyc86542b2014-11-25 12:34:13 +000034import android.service.persistentdata.PersistentDataBlockManager;
Jessica Hummel81fe1042014-06-23 17:10:38 +010035import android.support.v4.content.LocalBroadcastManager;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010036import android.text.TextUtils;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000037import android.view.LayoutInflater;
38import android.view.View;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010039import android.widget.TextView;
Sander Alewijnsec7757382014-03-18 17:09:45 +000040
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010041import com.android.managedprovisioning.task.AddWifiNetworkTask;
Rubin Xu44cdbdf2014-11-28 15:19:14 +000042import com.android.setupwizard.navigationbar.SetupWizardNavBar;
43import com.android.setupwizard.navigationbar.SetupWizardNavBar.NavigationBarListener;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010044
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000045import java.util.ArrayList;
Julia Reynolds3f4eb372015-02-11 13:46:42 -050046import java.util.List;
Sander Alewijnse56f71572014-06-23 16:21:33 +010047
Sander Alewijnseed0883b2014-03-18 15:01:13 +000048/**
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 Alewijnse4c4badf2014-03-20 14:12:49 +000067 * <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 Alewijnseed0883b2014-03-18 15:01:13 +000071 */
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010072public class DeviceOwnerProvisioningActivity extends Activity
Rubin Xu44cdbdf2014-11-28 15:19:14 +000073 implements UserConsentDialog.ConsentCallback, NavigationBarListener {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000074 private static final boolean DEBUG = false; // To control logging.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010075
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010076 private static final String KEY_USER_CONSENTED = "user_consented";
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000077 private static final String KEY_CANCEL_DIALOG_SHOWN = "cancel_dialog_shown";
78 private static final String KEY_PENDING_INTENTS = "pending_intents";
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010079
Sander Alewijnse28bffd62014-06-05 10:54:26 +010080 private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010081 private static final int WIFI_REQUEST_CODE = 2;
Sander Alewijnse28bffd62014-06-05 10:54:26 +010082
Rubin Xu44cdbdf2014-11-28 15:19:14 +000083 // 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 Alewijnse04ab6fe2014-04-29 10:53:54 +010087 private BroadcastReceiver mServiceMessageReceiver;
88 private TextView mProgressTextView;
Sander Alewijnsef88f7092014-08-20 16:26:09 +010089
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010090 // Indicates whether user consented by clicking on positive button of interstitial.
91 private boolean mUserConsented = false;
92
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010093 // Params that will be used after user consent.
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000094 // 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 Alewijnse3bad02d2014-10-02 14:57:05 +0100102
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000103 @Override
104 public void onCreate(Bundle savedInstanceState) {
105 super.onCreate(savedInstanceState);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000106 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONCREATE");
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000107
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100108 if (savedInstanceState != null) {
109 mUserConsented = savedInstanceState.getBoolean(KEY_USER_CONSENTED, false);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000110 mCancelDialogShown = savedInstanceState.getBoolean(KEY_CANCEL_DIALOG_SHOWN, false);
111 mPendingProvisioningIntents = savedInstanceState
112 .getParcelableArrayList(KEY_PENDING_INTENTS);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100113 }
114
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000115 // 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 Alewijnsea36dd992015-01-15 16:41:43 +0000121 if (titleText != null) titleText.setText(getString(R.string.setup_work_device));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000122 if (mCancelDialogShown) showCancelResetDialog();
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000123
124 // Check whether we can provision.
Sander Alewijnsed7043852014-06-17 15:50:48 +0100125 if (Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, 0 /* default */) != 0) {
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000126 ProvisionLogger.loge("Device already provisioned.");
Sander Alewijnsed7043852014-06-17 15:50:48 +0100127 error(R.string.device_owner_error_already_provisioned, false /* no factory reset */);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000128 return;
129 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000130
Joe Delfinod0f29282015-03-19 08:26:09 -0400131 if (!Utils.isCurrentUserOwner()) {
Sander Alewijnse8de999b2014-08-27 17:42:48 +0100132 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 Crowleyc86542b2014-11-25 12:34:13 +0000137 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 Alewijnse04ab6fe2014-04-29 10:53:54 +0100143 // 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 Hummel81fe1042014-06-23 17:10:38 +0100149 LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100150
Sander Alewijnsed7043852014-06-17 15:50:48 +0100151 // Parse the incoming intent.
152 MessageParser parser = new MessageParser();
Sander Alewijnsed7043852014-06-17 15:50:48 +0100153 try {
Nicolas Prevot668d65f2015-03-10 17:58:15 +0000154 mParams = parser.parseIntent(getIntent());
Nicolas Prevot0b447252015-03-09 14:59:02 +0000155 } catch (Utils.IllegalProvisioningArgumentException e) {
Sander Alewijnsed7043852014-06-17 15:50:48 +0100156 ProvisionLogger.loge("Could not read data from intent", e);
Nicolas Prevot0b447252015-03-09 14:59:02 +0000157 error(R.string.device_owner_error_general, false /* no factory reset */);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100158 return;
159 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100160
161 // Ask to encrypt the device before proceeding
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100162 if (!(EncryptDeviceActivity.isDeviceEncrypted()
Julia Reynolds5d47b652015-02-02 06:47:25 -0500163 || SystemProperties.getBoolean("persist.sys.no_req_encrypt", false)
164 || mParams.mSkipEncryption)) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000165 requestEncryption(parser, mParams);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100166 finish();
167 return;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100168 // System will reboot. Bootreminder will restart this activity.
Sander Alewijnsed7043852014-06-17 15:50:48 +0100169 }
170
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100171 // Have the user pick a wifi network if necessary.
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000172 if (!AddWifiNetworkTask.isConnectedToWifi(this) && TextUtils.isEmpty(mParams.mWifiSsid)) {
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100173 requestWifiPick();
174 return;
175 // Wait for onActivityResult.
176 }
177
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000178 showInterstitialAndProvision(mParams);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100179 }
180
Paul Crowleyc86542b2014-11-25 12:34:13 +0000181 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 Alewijnse8f9bd132014-09-08 12:21:26 +0100193 private void showInterstitialAndProvision(final ProvisioningParams params) {
194 if (mUserConsented || params.mStartedByNfc) {
195 startDeviceOwnerProvisioningService(params);
196 } else {
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100197 // Notify the user that the admin will have full control over the device,
198 // then start provisioning.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100199 UserConsentDialog.newInstance(UserConsentDialog.DEVICE_OWNER)
200 .show(getFragmentManager(), "UserConsentDialogFragment");
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100201 }
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100202 }
203
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100204 @Override
205 public void onDialogConsent() {
206 mUserConsented = true;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000207 startDeviceOwnerProvisioningService(mParams);
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100208 }
209
210 @Override
211 public void onDialogCancel() {
212 finish();
213 }
214
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100215 private void startDeviceOwnerProvisioningService(ProvisioningParams params) {
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100216 Intent intent = new Intent(this, DeviceOwnerProvisioningService.class);
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100217 intent.putExtra(DeviceOwnerProvisioningService.EXTRA_PROVISIONING_PARAMS, params);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100218 intent.putExtras(getIntent());
219 startService(intent);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000220 }
221
Jessica Hummel14eeef92014-06-16 11:06:20 +0100222 class ServiceMessageReceiver extends BroadcastReceiver
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100223 {
224 @Override
225 public void onReceive(Context context, Intent intent)
226 {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000227 if (mCancelDialogShown) {
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100228
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000229 // Postpone handling the intent.
230 mPendingProvisioningIntents.add(intent);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100231 return;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000232 }
233 handleProvisioningIntent(intent);
234 }
235 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100236
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000237 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 Alewijnse28bffd62014-06-05 10:54:26 +0100248 ProvisionLogger.logd("Error reported with code "
Sander Alewijnsed7043852014-06-17 15:50:48 +0100249 + getResources().getString(errorMessageId));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000250 }
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 Alewijnse28bffd62014-06-05 10:54:26 +0100256 ProvisionLogger.logd("Progress update reported with code "
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000257 + getResources().getString(progressMessage));
258 }
259 if (progressMessage >= 0) {
260 progressUpdate(progressMessage);
Sander Alewijnsec7757382014-03-18 17:09:45 +0000261 }
Sander Alewijnse63254f42014-03-21 15:31:12 +0000262 }
263 }
264
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000265
Sander Alewijnsed7043852014-06-17 15:50:48 +0100266 private void onProvisioningSuccess() {
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500267 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 Alewijnseab18ea72014-09-11 15:15:23 +0100291 // Note: the DeviceOwnerProvisioningService will stop itself.
Sander Alewijnse1c8d9312014-08-18 20:00:49 +0100292 setResult(Activity.RESULT_OK);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100293 finish();
294 }
295
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500296 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 Alewijnsef88f7092014-08-20 16:26:09 +0100304 private void requestEncryption(MessageParser messageParser, ProvisioningParams params) {
Sander Alewijnse56f71572014-06-23 16:21:33 +0100305 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 Alewijnsef88f7092014-08-20 16:26:09 +0100311 messageParser.addProvisioningParamsToBundle(resumeExtras, params);
Sander Alewijnse56f71572014-06-23 16:21:33 +0100312
313 encryptIntent.putExtra(EncryptDeviceActivity.EXTRA_RESUME, resumeExtras);
314
315 startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE);
316 }
317
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100318 private void requestWifiPick() {
319 startActivityForResult(AddWifiNetworkTask.getWifiPickIntent(), WIFI_REQUEST_CODE);
320 }
321
Sander Alewijnse63254f42014-03-21 15:31:12 +0000322 @Override
323 public void onBackPressed() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000324 if (mCancelDialogShown) {
325 return;
326 }
327
328 mCancelDialogShown = true;
Sander Alewijnsed7043852014-06-17 15:50:48 +0100329 showCancelResetDialog();
330 }
331
332 private void showCancelResetDialog() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000333 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 Alewijnsed7043852014-06-17 15:50:48 +0100345 })
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000346 .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 Alewijnsed7043852014-06-17 15:50:48 +0100351
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000352 // 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 Xu44cdbdf2014-11-28 15:19:14 +0000362 })
363 .show()
364 .getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000365 }
366
367 private void handlePendingIntents() {
368 for (Intent intent : mPendingProvisioningIntents) {
369 if (DEBUG) ProvisionLogger.logd("Handling pending intent " + intent.getAction());
370 handleProvisioningIntent(intent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100371 }
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000372 mPendingProvisioningIntents.clear();
Sander Alewijnse63254f42014-03-21 15:31:12 +0000373 }
374
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100375 private void progressUpdate(int progressMessage) {
376 mProgressTextView.setText(progressMessage);
377 }
378
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100379 @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 Alewijnse326bcfd2014-06-25 15:24:03 +0100386 } 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 Alewijnse9a40ab02014-10-27 18:36:56 +0000393 if (DEBUG) ProvisionLogger.logd("Wifi request result is OK");
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100394 if (AddWifiNetworkTask.isConnectedToWifi(this)) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000395 showInterstitialAndProvision(mParams);
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100396 } else {
397 requestWifiPick();
398 }
399 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100400 }
401 }
402
Sander Alewijnsed7043852014-06-17 15:50:48 +0100403 private void error(int dialogMessage, boolean resetRequired) {
404 AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
Jessica Hummelc3fc45e2014-09-01 19:01:12 +0100405 .setTitle(R.string.provisioning_error_title)
406 .setMessage(dialogMessage)
407 .setCancelable(false);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100408 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 Alewijnse9a40ab02014-10-27 18:36:56 +0000413 dialog.dismiss();
414
Sander Alewijnsed7043852014-06-17 15:50:48 +0100415 // Factory reset the device.
Jeff Sharkey56ebde02014-09-24 13:58:43 -0700416 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 Alewijnsed7043852014-06-17 15:50:48 +0100421 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 Alewijnse9a40ab02014-10-27 18:36:56 +0000431 dialog.dismiss();
432
Sander Alewijnsed7043852014-06-17 15:50:48 +0100433 // Close activity.
434 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
435 DeviceOwnerProvisioningService.class));
436 finish();
437 }
438 });
439 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000440 alertBuilder.show().getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000441 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100442
Jessica Hummel14eeef92014-06-16 11:06:20 +0100443 @Override
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100444 protected void onSaveInstanceState(Bundle outState) {
445 outState.putBoolean(KEY_USER_CONSENTED, mUserConsented);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000446 outState.putBoolean(KEY_CANCEL_DIALOG_SHOWN, mCancelDialogShown);
447 outState.putParcelableArrayList(KEY_PENDING_INTENTS, mPendingProvisioningIntents);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100448 }
449
450 @Override
Sander Alewijnse56f71572014-06-23 16:21:33 +0100451 public void onDestroy() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000452 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONDESTROY");
Sander Alewijnse56f71572014-06-23 16:21:33 +0100453 if (mServiceMessageReceiver != null) {
454 LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver);
455 mServiceMessageReceiver = null;
456 }
Sander Alewijnse56f71572014-06-23 16:21:33 +0100457 super.onDestroy();
458 }
459
460 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100461 protected void onRestart() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000462 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESTART");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100463 super.onRestart();
464 }
465
Jessica Hummel14eeef92014-06-16 11:06:20 +0100466 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100467 protected void onResume() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000468 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESUME");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100469 super.onResume();
470 }
471
Jessica Hummel14eeef92014-06-16 11:06:20 +0100472 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100473 protected void onPause() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000474 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONPAUSE");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100475 super.onPause();
476 }
477
Jessica Hummel14eeef92014-06-16 11:06:20 +0100478 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100479 protected void onStop() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000480 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONSTOP");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100481 super.onStop();
482 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000483
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 Alewijnseed0883b2014-03-18 15:01:13 +0000497}
498