blob: eb7ef1f5d7cc817f1fbd8f2d05d74f1803422f9d [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;
Jessica Hummel81fe1042014-06-23 17:10:38 +010034import android.support.v4.content.LocalBroadcastManager;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010035import android.text.TextUtils;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000036import android.view.LayoutInflater;
37import android.view.View;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010038import android.widget.TextView;
Sander Alewijnsec7757382014-03-18 17:09:45 +000039
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010040import com.android.managedprovisioning.task.AddWifiNetworkTask;
Rubin Xu44cdbdf2014-11-28 15:19:14 +000041import com.android.setupwizard.navigationbar.SetupWizardNavBar;
42import com.android.setupwizard.navigationbar.SetupWizardNavBar.NavigationBarListener;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010043
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000044import java.util.ArrayList;
Julia Reynolds3f4eb372015-02-11 13:46:42 -050045import java.util.List;
Sander Alewijnse56f71572014-06-23 16:21:33 +010046
Sander Alewijnseed0883b2014-03-18 15:01:13 +000047/**
48 * This activity starts device owner provisioning:
49 * It downloads a mobile device management application(mdm) from a given url and installs it,
50 * or a given mdm is already present on the device. The mdm is set as the owner of the device so
51 * that it has full control over the device:
52 * TODO: put link here with documentation on how a device owner has control over the device
53 * The mdm can then execute further setup steps.
54 *
55 * <p>
56 * An example use case might be when a company wants to set up a device for a single use case
57 * (such as giving instructions).
58 * </p>
59 *
60 * <p>
61 * Provisioning is triggered by a programmer device that sends required provisioning parameters via
62 * nfc. For an example of a programmer app see:
63 * com.example.android.apis.app.DeviceProvisioningProgrammerSample.
64 * </p>
65 *
Sander Alewijnse4c4badf2014-03-20 14:12:49 +000066 * <p>
67 * In the unlikely case that this activity is killed the whole provisioning process so far is
68 * repeated. We made sure that all tasks can be done twice without causing any problems.
69 * </p>
Sander Alewijnseed0883b2014-03-18 15:01:13 +000070 */
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010071public class DeviceOwnerProvisioningActivity extends Activity
Rubin Xu44cdbdf2014-11-28 15:19:14 +000072 implements UserConsentDialog.ConsentCallback, NavigationBarListener {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000073 private static final boolean DEBUG = false; // To control logging.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010074
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010075 private static final String KEY_USER_CONSENTED = "user_consented";
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000076 private static final String KEY_CANCEL_DIALOG_SHOWN = "cancel_dialog_shown";
77 private static final String KEY_PENDING_INTENTS = "pending_intents";
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010078
Sander Alewijnse28bffd62014-06-05 10:54:26 +010079 private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010080 private static final int WIFI_REQUEST_CODE = 2;
Sander Alewijnse28bffd62014-06-05 10:54:26 +010081
Rubin Xu44cdbdf2014-11-28 15:19:14 +000082 // Hide default system navigation bar.
83 protected static final int IMMERSIVE_FLAGS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
84 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
85
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010086 private BroadcastReceiver mServiceMessageReceiver;
87 private TextView mProgressTextView;
Sander Alewijnsef88f7092014-08-20 16:26:09 +010088
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010089 // Indicates whether user consented by clicking on positive button of interstitial.
90 private boolean mUserConsented = false;
91
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010092 // Params that will be used after user consent.
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000093 // Extracted from the starting intent.
94 private ProvisioningParams mParams;
95
96 // Indicates that the cancel dialog is shown.
97 private boolean mCancelDialogShown = false;
98
99 // List of intents received while cancel dialog is shown.
100 private ArrayList<Intent> mPendingProvisioningIntents = new ArrayList<Intent>();
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100101
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000102 @Override
103 public void onCreate(Bundle savedInstanceState) {
104 super.onCreate(savedInstanceState);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000105 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONCREATE");
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000106
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100107 if (savedInstanceState != null) {
108 mUserConsented = savedInstanceState.getBoolean(KEY_USER_CONSENTED, false);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000109 mCancelDialogShown = savedInstanceState.getBoolean(KEY_CANCEL_DIALOG_SHOWN, false);
110 mPendingProvisioningIntents = savedInstanceState
111 .getParcelableArrayList(KEY_PENDING_INTENTS);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100112 }
113
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000114 // Setup the UI.
115 final LayoutInflater inflater = getLayoutInflater();
116 final View contentView = inflater.inflate(R.layout.progress, null);
117 setContentView(contentView);
118 mProgressTextView = (TextView) findViewById(R.id.prog_text);
119 TextView titleText = (TextView) findViewById(R.id.title);
Sander Alewijnsea36dd992015-01-15 16:41:43 +0000120 if (titleText != null) titleText.setText(getString(R.string.setup_work_device));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000121 if (mCancelDialogShown) showCancelResetDialog();
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000122
Joe Delfino84e56f52015-03-27 09:56:18 -0400123 // Check whether we have already provisioned this user.
124 if (Utils.isCurrentUserOwner()) {
125 int provisioned =
126 Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, 0 /*default*/);
127 if (provisioned != 0) {
128 ProvisionLogger.loge("Device already provisioned.");
129 error(R.string.device_owner_error_already_provisioned,
130 false /* no factory reset */);
131 return;
132 }
133 } else {
134 int provisioned =
135 Secure.getInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 0 /*default*/);
136 if (provisioned != 0) {
137 ProvisionLogger.loge("User already provisioned.");
138 error(R.string.device_owner_error_already_provisioned_user,
139 false /* no factory reset */);
140 return;
141 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000142 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000143
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100144 // 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 Hummel81fe1042014-06-23 17:10:38 +0100150 LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100151
Sander Alewijnsed7043852014-06-17 15:50:48 +0100152 // Parse the incoming intent.
153 MessageParser parser = new MessageParser();
Sander Alewijnsed7043852014-06-17 15:50:48 +0100154 try {
Nicolas Prevot668d65f2015-03-10 17:58:15 +0000155 mParams = parser.parseIntent(getIntent());
Nicolas Prevot0b447252015-03-09 14:59:02 +0000156 } catch (Utils.IllegalProvisioningArgumentException e) {
Sander Alewijnsed7043852014-06-17 15:50:48 +0100157 ProvisionLogger.loge("Could not read data from intent", e);
Nicolas Prevot0b447252015-03-09 14:59:02 +0000158 error(R.string.device_owner_error_general, false /* no factory reset */);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100159 return;
160 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100161
162 // Ask to encrypt the device before proceeding
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100163 if (!(EncryptDeviceActivity.isDeviceEncrypted()
Julia Reynolds5d47b652015-02-02 06:47:25 -0500164 || SystemProperties.getBoolean("persist.sys.no_req_encrypt", false)
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100165 || mParams.skipEncryption)) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000166 requestEncryption(parser, mParams);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100167 finish();
168 return;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100169 // System will reboot. Bootreminder will restart this activity.
Sander Alewijnsed7043852014-06-17 15:50:48 +0100170 }
171
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100172 // Have the user pick a wifi network if necessary.
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100173 if (!AddWifiNetworkTask.isConnectedToWifi(this) && TextUtils.isEmpty(mParams.wifiInfo.ssid)
174 && !mParams.bluetoothInfo.useProxy) {
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100175 requestWifiPick();
176 return;
177 // Wait for onActivityResult.
178 }
179
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000180 showInterstitialAndProvision(mParams);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100181 }
182
183 private void showInterstitialAndProvision(final ProvisioningParams params) {
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100184 if (mUserConsented || params.startedByNfc || !Utils.isCurrentUserOwner()) {
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100185 startDeviceOwnerProvisioningService(params);
186 } else {
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100187 // Notify the user that the admin will have full control over the device,
188 // then start provisioning.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100189 UserConsentDialog.newInstance(UserConsentDialog.DEVICE_OWNER)
190 .show(getFragmentManager(), "UserConsentDialogFragment");
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100191 }
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100192 }
193
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100194 @Override
195 public void onDialogConsent() {
196 mUserConsented = true;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000197 startDeviceOwnerProvisioningService(mParams);
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100198 }
199
200 @Override
201 public void onDialogCancel() {
202 finish();
203 }
204
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100205 private void startDeviceOwnerProvisioningService(ProvisioningParams params) {
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100206 Intent intent = new Intent(this, DeviceOwnerProvisioningService.class);
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100207 intent.putExtra(DeviceOwnerProvisioningService.EXTRA_PROVISIONING_PARAMS, params);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100208 intent.putExtras(getIntent());
209 startService(intent);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000210 }
211
Jessica Hummel14eeef92014-06-16 11:06:20 +0100212 class ServiceMessageReceiver extends BroadcastReceiver
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100213 {
214 @Override
215 public void onReceive(Context context, Intent intent)
216 {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000217 if (mCancelDialogShown) {
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100218
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000219 // Postpone handling the intent.
220 mPendingProvisioningIntents.add(intent);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100221 return;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000222 }
223 handleProvisioningIntent(intent);
224 }
225 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100226
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000227 private void handleProvisioningIntent(Intent intent) {
228 String action = intent.getAction();
229 if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS)) {
230 if (DEBUG) ProvisionLogger.logd("Successfully provisioned");
231 onProvisioningSuccess();
232 } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR)) {
233 int errorMessageId = intent.getIntExtra(
234 DeviceOwnerProvisioningService.EXTRA_USER_VISIBLE_ERROR_ID_KEY,
235 R.string.device_owner_error_general);
236
237 if (DEBUG) {
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100238 ProvisionLogger.logd("Error reported with code "
Sander Alewijnsed7043852014-06-17 15:50:48 +0100239 + getResources().getString(errorMessageId));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000240 }
241 error(errorMessageId, true /* always factory reset */);
242 } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE)) {
243 int progressMessage = intent.getIntExtra(
244 DeviceOwnerProvisioningService.EXTRA_PROGRESS_MESSAGE_ID_KEY, -1);
245 if (DEBUG) {
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100246 ProvisionLogger.logd("Progress update reported with code "
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000247 + getResources().getString(progressMessage));
248 }
249 if (progressMessage >= 0) {
250 progressUpdate(progressMessage);
Sander Alewijnsec7757382014-03-18 17:09:45 +0000251 }
Sander Alewijnse63254f42014-03-21 15:31:12 +0000252 }
253 }
254
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000255
Sander Alewijnsed7043852014-06-17 15:50:48 +0100256 private void onProvisioningSuccess() {
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100257 if (mParams.deviceInitializerComponentName != null) {
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500258 Intent result = new Intent(ACTION_READY_FOR_USER_INITIALIZATION);
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100259 result.setComponent(mParams.deviceInitializerComponentName);
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500260 result.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
261 Intent.FLAG_RECEIVER_FOREGROUND);
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100262 if (mParams.adminExtrasBundle != null) {
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500263 result.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,
Sander Alewijnse74d6c142015-04-13 11:51:19 +0100264 mParams.adminExtrasBundle);
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500265 }
266 List<ResolveInfo> matchingReceivers =
267 getPackageManager().queryBroadcastReceivers(result, 0);
268 if (matchingReceivers.size() > 0) {
269 // Notify the device initializer that it can now perform pre-user-setup tasks.
270 sendBroadcast(result);
271 } else {
272 ProvisionLogger.logi("Initializer component doesn't have a receiver for "
273 + "android.app.action.READY_FOR_USER_INITIALIZATION. Skipping broadcast "
274 + "and finishing user initialization.");
275 provisionDevice();
276 }
277 } else {
278 // No initializer, set the device provisioned ourselves.
279 provisionDevice();
280 }
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100281 // Note: the DeviceOwnerProvisioningService will stop itself.
Sander Alewijnse1c8d9312014-08-18 20:00:49 +0100282 setResult(Activity.RESULT_OK);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100283 finish();
284 }
285
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500286 private void provisionDevice() {
Joe Delfino84e56f52015-03-27 09:56:18 -0400287 if (Utils.isCurrentUserOwner()) {
288 // This only needs to be set once per device
289 Global.putInt(getContentResolver(), Global.DEVICE_PROVISIONED, 1);
290 }
291
292 // Setting this flag will either cause Setup Wizard to finish immediately when it starts (if
293 // it is not already running), or when its next activity starts (if it is already running,
294 // e.g. the non-NFC flow).
295 // When either of these things happen, a home intent is fired. We catch that in
296 // HomeReceiverActivity before sending the intent to notify the mdm that provisioning is
297 // complete.
298 // Note that, in the NFC flow or for secondary users, setting this flag will prevent the
299 // user from seeing SUW, even if no other device initialization app was specified.
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500300 Secure.putInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 1);
301 }
302
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100303 private void requestEncryption(MessageParser messageParser, ProvisioningParams params) {
Sander Alewijnse56f71572014-06-23 16:21:33 +0100304 Intent encryptIntent = new Intent(DeviceOwnerProvisioningActivity.this,
305 EncryptDeviceActivity.class);
306
307 Bundle resumeExtras = new Bundle();
308 resumeExtras.putString(EncryptDeviceActivity.EXTRA_RESUME_TARGET,
309 EncryptDeviceActivity.TARGET_DEVICE_OWNER);
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100310 messageParser.addProvisioningParamsToBundle(resumeExtras, params);
Sander Alewijnse56f71572014-06-23 16:21:33 +0100311
312 encryptIntent.putExtra(EncryptDeviceActivity.EXTRA_RESUME, resumeExtras);
313
314 startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE);
315 }
316
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100317 private void requestWifiPick() {
318 startActivityForResult(AddWifiNetworkTask.getWifiPickIntent(), WIFI_REQUEST_CODE);
319 }
320
Sander Alewijnse63254f42014-03-21 15:31:12 +0000321 @Override
322 public void onBackPressed() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000323 if (mCancelDialogShown) {
324 return;
325 }
326
327 mCancelDialogShown = true;
Sander Alewijnsed7043852014-06-17 15:50:48 +0100328 showCancelResetDialog();
329 }
330
331 private void showCancelResetDialog() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000332 new AlertDialog.Builder(DeviceOwnerProvisioningActivity.this)
333 .setCancelable(false)
334 .setTitle(R.string.device_owner_cancel_title)
335 .setMessage(R.string.device_owner_cancel_message)
336 .setNegativeButton(R.string.device_owner_cancel_cancel,
337 new DialogInterface.OnClickListener() {
338 @Override
339 public void onClick(DialogInterface dialog,int id) {
340 dialog.dismiss();
341 handlePendingIntents();
342 mCancelDialogShown = false;
343 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100344 })
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000345 .setPositiveButton(R.string.device_owner_error_reset,
346 new DialogInterface.OnClickListener() {
347 @Override
348 public void onClick(DialogInterface dialog,int id) {
349 dialog.dismiss();
Sander Alewijnsed7043852014-06-17 15:50:48 +0100350
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000351 // Factory reset the device.
352 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
353 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
354 intent.putExtra(Intent.EXTRA_REASON,
355 "DeviceOwnerProvisioningActivity.showCancelResetDialog()");
356 sendBroadcast(intent);
357 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
358 DeviceOwnerProvisioningService.class));
359 finish();
360 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000361 })
362 .show()
363 .getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000364 }
365
366 private void handlePendingIntents() {
367 for (Intent intent : mPendingProvisioningIntents) {
368 if (DEBUG) ProvisionLogger.logd("Handling pending intent " + intent.getAction());
369 handleProvisioningIntent(intent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100370 }
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000371 mPendingProvisioningIntents.clear();
Sander Alewijnse63254f42014-03-21 15:31:12 +0000372 }
373
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100374 private void progressUpdate(int progressMessage) {
375 mProgressTextView.setText(progressMessage);
376 }
377
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100378 @Override
379 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
380 if (requestCode == ENCRYPT_DEVICE_REQUEST_CODE) {
381 if (resultCode == RESULT_CANCELED) {
382 ProvisionLogger.loge("User canceled device encryption.");
383 finish();
384 }
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100385 } else if (requestCode == WIFI_REQUEST_CODE) {
386 if (resultCode == RESULT_CANCELED) {
387 ProvisionLogger.loge("User canceled wifi picking.");
388 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
389 DeviceOwnerProvisioningService.class));
390 finish();
391 } else if (resultCode == RESULT_OK) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000392 if (DEBUG) ProvisionLogger.logd("Wifi request result is OK");
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100393 if (AddWifiNetworkTask.isConnectedToWifi(this)) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000394 showInterstitialAndProvision(mParams);
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100395 } else {
396 requestWifiPick();
397 }
398 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100399 }
400 }
401
Sander Alewijnsed7043852014-06-17 15:50:48 +0100402 private void error(int dialogMessage, boolean resetRequired) {
403 AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
Jessica Hummelc3fc45e2014-09-01 19:01:12 +0100404 .setTitle(R.string.provisioning_error_title)
405 .setMessage(dialogMessage)
406 .setCancelable(false);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100407 if (resetRequired) {
408 alertBuilder.setPositiveButton(R.string.device_owner_error_reset,
409 new DialogInterface.OnClickListener() {
410 @Override
411 public void onClick(DialogInterface dialog,int id) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000412 dialog.dismiss();
413
Sander Alewijnsed7043852014-06-17 15:50:48 +0100414 // Factory reset the device.
Jeff Sharkey56ebde02014-09-24 13:58:43 -0700415 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
416 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
417 intent.putExtra(Intent.EXTRA_REASON,
418 "DeviceOwnerProvisioningActivity.error()");
419 sendBroadcast(intent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100420 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
421 DeviceOwnerProvisioningService.class));
422 finish();
423 }
424 });
425 } else {
426 alertBuilder.setPositiveButton(R.string.device_owner_error_ok,
427 new DialogInterface.OnClickListener() {
428 @Override
429 public void onClick(DialogInterface dialog,int id) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000430 dialog.dismiss();
431
Sander Alewijnsed7043852014-06-17 15:50:48 +0100432 // Close activity.
433 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
434 DeviceOwnerProvisioningService.class));
435 finish();
436 }
437 });
438 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000439 alertBuilder.show().getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000440 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100441
Jessica Hummel14eeef92014-06-16 11:06:20 +0100442 @Override
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100443 protected void onSaveInstanceState(Bundle outState) {
444 outState.putBoolean(KEY_USER_CONSENTED, mUserConsented);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000445 outState.putBoolean(KEY_CANCEL_DIALOG_SHOWN, mCancelDialogShown);
446 outState.putParcelableArrayList(KEY_PENDING_INTENTS, mPendingProvisioningIntents);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100447 }
448
449 @Override
Sander Alewijnse56f71572014-06-23 16:21:33 +0100450 public void onDestroy() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000451 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONDESTROY");
Sander Alewijnse56f71572014-06-23 16:21:33 +0100452 if (mServiceMessageReceiver != null) {
453 LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver);
454 mServiceMessageReceiver = null;
455 }
Sander Alewijnse56f71572014-06-23 16:21:33 +0100456 super.onDestroy();
457 }
458
459 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100460 protected void onRestart() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000461 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESTART");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100462 super.onRestart();
463 }
464
Jessica Hummel14eeef92014-06-16 11:06:20 +0100465 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100466 protected void onResume() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000467 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESUME");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100468 super.onResume();
469 }
470
Jessica Hummel14eeef92014-06-16 11:06:20 +0100471 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100472 protected void onPause() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000473 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONPAUSE");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100474 super.onPause();
475 }
476
Jessica Hummel14eeef92014-06-16 11:06:20 +0100477 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100478 protected void onStop() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000479 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONSTOP");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100480 super.onStop();
481 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000482
483 @Override
484 public void onNavigationBarCreated(SetupWizardNavBar bar) {
485 bar.getNextButton().setVisibility(View.INVISIBLE);
486 }
487
488 @Override
489 public void onNavigateBack() {
490 onBackPressed();
491 }
492
493 @Override
494 public void onNavigateNext() {
495 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000496}
497