blob: 78c555705ff90dd9f7a9198f2c3fd9a7aebe712d [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 Alewijnse8de999b2014-08-27 17:42:48 +010031import android.os.UserHandle;
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010032import android.os.SystemProperties;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000033import android.provider.Settings.Global;
Sander Alewijnsed7043852014-06-17 15:50:48 +010034import android.provider.Settings.Secure;
Paul Crowleyc86542b2014-11-25 12:34:13 +000035import android.service.persistentdata.PersistentDataBlockManager;
Jessica Hummel81fe1042014-06-23 17:10:38 +010036import android.support.v4.content.LocalBroadcastManager;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010037import android.text.TextUtils;
Sander Alewijnseed0883b2014-03-18 15:01:13 +000038import android.view.LayoutInflater;
39import android.view.View;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010040import android.widget.TextView;
Sander Alewijnsec7757382014-03-18 17:09:45 +000041
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010042import com.android.managedprovisioning.task.AddWifiNetworkTask;
Rubin Xu44cdbdf2014-11-28 15:19:14 +000043import com.android.setupwizard.navigationbar.SetupWizardNavBar;
44import com.android.setupwizard.navigationbar.SetupWizardNavBar.NavigationBarListener;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010045
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000046import java.util.ArrayList;
Julia Reynolds3f4eb372015-02-11 13:46:42 -050047import java.util.List;
Sander Alewijnse56f71572014-06-23 16:21:33 +010048
Sander Alewijnseed0883b2014-03-18 15:01:13 +000049/**
50 * This activity starts device owner provisioning:
51 * It downloads a mobile device management application(mdm) from a given url and installs it,
52 * or a given mdm is already present on the device. The mdm is set as the owner of the device so
53 * that it has full control over the device:
54 * TODO: put link here with documentation on how a device owner has control over the device
55 * The mdm can then execute further setup steps.
56 *
57 * <p>
58 * An example use case might be when a company wants to set up a device for a single use case
59 * (such as giving instructions).
60 * </p>
61 *
62 * <p>
63 * Provisioning is triggered by a programmer device that sends required provisioning parameters via
64 * nfc. For an example of a programmer app see:
65 * com.example.android.apis.app.DeviceProvisioningProgrammerSample.
66 * </p>
67 *
Sander Alewijnse4c4badf2014-03-20 14:12:49 +000068 * <p>
69 * In the unlikely case that this activity is killed the whole provisioning process so far is
70 * repeated. We made sure that all tasks can be done twice without causing any problems.
71 * </p>
Sander Alewijnseed0883b2014-03-18 15:01:13 +000072 */
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010073public class DeviceOwnerProvisioningActivity extends Activity
Rubin Xu44cdbdf2014-11-28 15:19:14 +000074 implements UserConsentDialog.ConsentCallback, NavigationBarListener {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000075 private static final boolean DEBUG = false; // To control logging.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010076
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010077 private static final String KEY_USER_CONSENTED = "user_consented";
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000078 private static final String KEY_CANCEL_DIALOG_SHOWN = "cancel_dialog_shown";
79 private static final String KEY_PENDING_INTENTS = "pending_intents";
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010080
Sander Alewijnse28bffd62014-06-05 10:54:26 +010081 private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010082 private static final int WIFI_REQUEST_CODE = 2;
Sander Alewijnse28bffd62014-06-05 10:54:26 +010083
Rubin Xu44cdbdf2014-11-28 15:19:14 +000084 // Hide default system navigation bar.
85 protected static final int IMMERSIVE_FLAGS = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
86 | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
87
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010088 private BroadcastReceiver mServiceMessageReceiver;
89 private TextView mProgressTextView;
Sander Alewijnsef88f7092014-08-20 16:26:09 +010090
Sander Alewijnse8f9bd132014-09-08 12:21:26 +010091 // Indicates whether user consented by clicking on positive button of interstitial.
92 private boolean mUserConsented = false;
93
Sander Alewijnse3bad02d2014-10-02 14:57:05 +010094 // Params that will be used after user consent.
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000095 // Extracted from the starting intent.
96 private ProvisioningParams mParams;
97
98 // Indicates that the cancel dialog is shown.
99 private boolean mCancelDialogShown = false;
100
101 // List of intents received while cancel dialog is shown.
102 private ArrayList<Intent> mPendingProvisioningIntents = new ArrayList<Intent>();
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100103
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000104 @Override
105 public void onCreate(Bundle savedInstanceState) {
106 super.onCreate(savedInstanceState);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000107 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONCREATE");
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000108
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100109 if (savedInstanceState != null) {
110 mUserConsented = savedInstanceState.getBoolean(KEY_USER_CONSENTED, false);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000111 mCancelDialogShown = savedInstanceState.getBoolean(KEY_CANCEL_DIALOG_SHOWN, false);
112 mPendingProvisioningIntents = savedInstanceState
113 .getParcelableArrayList(KEY_PENDING_INTENTS);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100114 }
115
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000116 // Setup the UI.
117 final LayoutInflater inflater = getLayoutInflater();
118 final View contentView = inflater.inflate(R.layout.progress, null);
119 setContentView(contentView);
120 mProgressTextView = (TextView) findViewById(R.id.prog_text);
121 TextView titleText = (TextView) findViewById(R.id.title);
Sander Alewijnsea36dd992015-01-15 16:41:43 +0000122 if (titleText != null) titleText.setText(getString(R.string.setup_work_device));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000123 if (mCancelDialogShown) showCancelResetDialog();
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000124
125 // Check whether we can provision.
Sander Alewijnsed7043852014-06-17 15:50:48 +0100126 if (Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, 0 /* default */) != 0) {
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000127 ProvisionLogger.loge("Device already provisioned.");
Sander Alewijnsed7043852014-06-17 15:50:48 +0100128 error(R.string.device_owner_error_already_provisioned, false /* no factory reset */);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000129 return;
130 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000131
Sander Alewijnse8de999b2014-08-27 17:42:48 +0100132 if (UserHandle.myUserId() != UserHandle.USER_OWNER) {
133 ProvisionLogger.loge("Device owner can only be set up for USER_OWNER.");
134 error(R.string.device_owner_error_general, false /* no factory reset */);
135 return;
136 }
137
Paul Crowleyc86542b2014-11-25 12:34:13 +0000138 if (factoryResetProtected()) {
139 ProvisionLogger.loge("Factory reset protection blocks provisioning.");
140 error(R.string.device_owner_error_already_provisioned, false /* no factory reset */);
141 return;
142 }
143
Sander 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)
165 || mParams.mSkipEncryption)) {
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 Alewijnse9a40ab02014-10-27 18:36:56 +0000173 if (!AddWifiNetworkTask.isConnectedToWifi(this) && TextUtils.isEmpty(mParams.mWifiSsid)) {
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100174 requestWifiPick();
175 return;
176 // Wait for onActivityResult.
177 }
178
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000179 showInterstitialAndProvision(mParams);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100180 }
181
Paul Crowleyc86542b2014-11-25 12:34:13 +0000182 private boolean factoryResetProtected() {
183 // Can't refer to type directly here and API is hidden, so
184 // get it via reflection.
185 PersistentDataBlockManager pdbManager = (PersistentDataBlockManager)
186 getSystemService(Context.PERSISTENT_DATA_BLOCK_SERVICE);
187 if (pdbManager == null) {
188 ProvisionLogger.loge("Unable to get persistent data block service");
189 return false;
190 }
191 return pdbManager.getDataBlockSize() > 0;
192 }
193
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100194 private void showInterstitialAndProvision(final ProvisioningParams params) {
195 if (mUserConsented || params.mStartedByNfc) {
196 startDeviceOwnerProvisioningService(params);
197 } else {
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100198 // Notify the user that the admin will have full control over the device,
199 // then start provisioning.
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100200 UserConsentDialog.newInstance(UserConsentDialog.DEVICE_OWNER)
201 .show(getFragmentManager(), "UserConsentDialogFragment");
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100202 }
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100203 }
204
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100205 @Override
206 public void onDialogConsent() {
207 mUserConsented = true;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000208 startDeviceOwnerProvisioningService(mParams);
Sander Alewijnse3bad02d2014-10-02 14:57:05 +0100209 }
210
211 @Override
212 public void onDialogCancel() {
213 finish();
214 }
215
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100216 private void startDeviceOwnerProvisioningService(ProvisioningParams params) {
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100217 Intent intent = new Intent(this, DeviceOwnerProvisioningService.class);
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100218 intent.putExtra(DeviceOwnerProvisioningService.EXTRA_PROVISIONING_PARAMS, params);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100219 intent.putExtras(getIntent());
220 startService(intent);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000221 }
222
Jessica Hummel14eeef92014-06-16 11:06:20 +0100223 class ServiceMessageReceiver extends BroadcastReceiver
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100224 {
225 @Override
226 public void onReceive(Context context, Intent intent)
227 {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000228 if (mCancelDialogShown) {
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100229
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000230 // Postpone handling the intent.
231 mPendingProvisioningIntents.add(intent);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100232 return;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000233 }
234 handleProvisioningIntent(intent);
235 }
236 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100237
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000238 private void handleProvisioningIntent(Intent intent) {
239 String action = intent.getAction();
240 if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS)) {
241 if (DEBUG) ProvisionLogger.logd("Successfully provisioned");
242 onProvisioningSuccess();
243 } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR)) {
244 int errorMessageId = intent.getIntExtra(
245 DeviceOwnerProvisioningService.EXTRA_USER_VISIBLE_ERROR_ID_KEY,
246 R.string.device_owner_error_general);
247
248 if (DEBUG) {
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100249 ProvisionLogger.logd("Error reported with code "
Sander Alewijnsed7043852014-06-17 15:50:48 +0100250 + getResources().getString(errorMessageId));
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000251 }
252 error(errorMessageId, true /* always factory reset */);
253 } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE)) {
254 int progressMessage = intent.getIntExtra(
255 DeviceOwnerProvisioningService.EXTRA_PROGRESS_MESSAGE_ID_KEY, -1);
256 if (DEBUG) {
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100257 ProvisionLogger.logd("Progress update reported with code "
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000258 + getResources().getString(progressMessage));
259 }
260 if (progressMessage >= 0) {
261 progressUpdate(progressMessage);
Sander Alewijnsec7757382014-03-18 17:09:45 +0000262 }
Sander Alewijnse63254f42014-03-21 15:31:12 +0000263 }
264 }
265
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000266
Sander Alewijnsed7043852014-06-17 15:50:48 +0100267 private void onProvisioningSuccess() {
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500268 if (mParams.mDeviceInitializerComponentName != null) {
269 Intent result = new Intent(ACTION_READY_FOR_USER_INITIALIZATION);
270 result.setComponent(mParams.mDeviceInitializerComponentName);
271 result.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES |
272 Intent.FLAG_RECEIVER_FOREGROUND);
273 if (mParams.mAdminExtrasBundle != null) {
274 result.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE,
275 mParams.mAdminExtrasBundle);
276 }
277 List<ResolveInfo> matchingReceivers =
278 getPackageManager().queryBroadcastReceivers(result, 0);
279 if (matchingReceivers.size() > 0) {
280 // Notify the device initializer that it can now perform pre-user-setup tasks.
281 sendBroadcast(result);
282 } else {
283 ProvisionLogger.logi("Initializer component doesn't have a receiver for "
284 + "android.app.action.READY_FOR_USER_INITIALIZATION. Skipping broadcast "
285 + "and finishing user initialization.");
286 provisionDevice();
287 }
288 } else {
289 // No initializer, set the device provisioned ourselves.
290 provisionDevice();
291 }
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100292 // Note: the DeviceOwnerProvisioningService will stop itself.
Sander Alewijnse1c8d9312014-08-18 20:00:49 +0100293 setResult(Activity.RESULT_OK);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100294 finish();
295 }
296
Julia Reynolds3f4eb372015-02-11 13:46:42 -0500297 private void provisionDevice() {
298 // The Setup wizards listens to this flag and finishes itself when it is set.
299 // It then fires a home intent, which we catch in the HomeReceiverActivity before
300 // sending the intent to notify the mdm that provisioning is complete.
301 Global.putInt(getContentResolver(), Global.DEVICE_PROVISIONED, 1);
302 Secure.putInt(getContentResolver(), Secure.USER_SETUP_COMPLETE, 1);
303 }
304
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100305 private void requestEncryption(MessageParser messageParser, ProvisioningParams params) {
Sander Alewijnse56f71572014-06-23 16:21:33 +0100306 Intent encryptIntent = new Intent(DeviceOwnerProvisioningActivity.this,
307 EncryptDeviceActivity.class);
308
309 Bundle resumeExtras = new Bundle();
310 resumeExtras.putString(EncryptDeviceActivity.EXTRA_RESUME_TARGET,
311 EncryptDeviceActivity.TARGET_DEVICE_OWNER);
Sander Alewijnsef88f7092014-08-20 16:26:09 +0100312 messageParser.addProvisioningParamsToBundle(resumeExtras, params);
Sander Alewijnse56f71572014-06-23 16:21:33 +0100313
314 encryptIntent.putExtra(EncryptDeviceActivity.EXTRA_RESUME, resumeExtras);
315
316 startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE);
317 }
318
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100319 private void requestWifiPick() {
320 startActivityForResult(AddWifiNetworkTask.getWifiPickIntent(), WIFI_REQUEST_CODE);
321 }
322
Sander Alewijnse63254f42014-03-21 15:31:12 +0000323 @Override
324 public void onBackPressed() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000325 if (mCancelDialogShown) {
326 return;
327 }
328
329 mCancelDialogShown = true;
Sander Alewijnsed7043852014-06-17 15:50:48 +0100330 showCancelResetDialog();
331 }
332
333 private void showCancelResetDialog() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000334 new AlertDialog.Builder(DeviceOwnerProvisioningActivity.this)
335 .setCancelable(false)
336 .setTitle(R.string.device_owner_cancel_title)
337 .setMessage(R.string.device_owner_cancel_message)
338 .setNegativeButton(R.string.device_owner_cancel_cancel,
339 new DialogInterface.OnClickListener() {
340 @Override
341 public void onClick(DialogInterface dialog,int id) {
342 dialog.dismiss();
343 handlePendingIntents();
344 mCancelDialogShown = false;
345 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100346 })
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000347 .setPositiveButton(R.string.device_owner_error_reset,
348 new DialogInterface.OnClickListener() {
349 @Override
350 public void onClick(DialogInterface dialog,int id) {
351 dialog.dismiss();
Sander Alewijnsed7043852014-06-17 15:50:48 +0100352
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000353 // Factory reset the device.
354 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
355 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
356 intent.putExtra(Intent.EXTRA_REASON,
357 "DeviceOwnerProvisioningActivity.showCancelResetDialog()");
358 sendBroadcast(intent);
359 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
360 DeviceOwnerProvisioningService.class));
361 finish();
362 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000363 })
364 .show()
365 .getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000366 }
367
368 private void handlePendingIntents() {
369 for (Intent intent : mPendingProvisioningIntents) {
370 if (DEBUG) ProvisionLogger.logd("Handling pending intent " + intent.getAction());
371 handleProvisioningIntent(intent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100372 }
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000373 mPendingProvisioningIntents.clear();
Sander Alewijnse63254f42014-03-21 15:31:12 +0000374 }
375
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100376 private void progressUpdate(int progressMessage) {
377 mProgressTextView.setText(progressMessage);
378 }
379
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100380 @Override
381 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
382 if (requestCode == ENCRYPT_DEVICE_REQUEST_CODE) {
383 if (resultCode == RESULT_CANCELED) {
384 ProvisionLogger.loge("User canceled device encryption.");
385 finish();
386 }
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100387 } else if (requestCode == WIFI_REQUEST_CODE) {
388 if (resultCode == RESULT_CANCELED) {
389 ProvisionLogger.loge("User canceled wifi picking.");
390 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
391 DeviceOwnerProvisioningService.class));
392 finish();
393 } else if (resultCode == RESULT_OK) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000394 if (DEBUG) ProvisionLogger.logd("Wifi request result is OK");
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100395 if (AddWifiNetworkTask.isConnectedToWifi(this)) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000396 showInterstitialAndProvision(mParams);
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100397 } else {
398 requestWifiPick();
399 }
400 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100401 }
402 }
403
Sander Alewijnsed7043852014-06-17 15:50:48 +0100404 private void error(int dialogMessage, boolean resetRequired) {
405 AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
Jessica Hummelc3fc45e2014-09-01 19:01:12 +0100406 .setTitle(R.string.provisioning_error_title)
407 .setMessage(dialogMessage)
408 .setCancelable(false);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100409 if (resetRequired) {
410 alertBuilder.setPositiveButton(R.string.device_owner_error_reset,
411 new DialogInterface.OnClickListener() {
412 @Override
413 public void onClick(DialogInterface dialog,int id) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000414 dialog.dismiss();
415
Sander Alewijnsed7043852014-06-17 15:50:48 +0100416 // Factory reset the device.
Jeff Sharkey56ebde02014-09-24 13:58:43 -0700417 Intent intent = new Intent(Intent.ACTION_MASTER_CLEAR);
418 intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
419 intent.putExtra(Intent.EXTRA_REASON,
420 "DeviceOwnerProvisioningActivity.error()");
421 sendBroadcast(intent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100422 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
423 DeviceOwnerProvisioningService.class));
424 finish();
425 }
426 });
427 } else {
428 alertBuilder.setPositiveButton(R.string.device_owner_error_ok,
429 new DialogInterface.OnClickListener() {
430 @Override
431 public void onClick(DialogInterface dialog,int id) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000432 dialog.dismiss();
433
Sander Alewijnsed7043852014-06-17 15:50:48 +0100434 // Close activity.
435 stopService(new Intent(DeviceOwnerProvisioningActivity.this,
436 DeviceOwnerProvisioningService.class));
437 finish();
438 }
439 });
440 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000441 alertBuilder.show().getWindow().getDecorView().setSystemUiVisibility(IMMERSIVE_FLAGS);
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000442 }
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100443
Jessica Hummel14eeef92014-06-16 11:06:20 +0100444 @Override
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100445 protected void onSaveInstanceState(Bundle outState) {
446 outState.putBoolean(KEY_USER_CONSENTED, mUserConsented);
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000447 outState.putBoolean(KEY_CANCEL_DIALOG_SHOWN, mCancelDialogShown);
448 outState.putParcelableArrayList(KEY_PENDING_INTENTS, mPendingProvisioningIntents);
Sander Alewijnse8f9bd132014-09-08 12:21:26 +0100449 }
450
451 @Override
Sander Alewijnse56f71572014-06-23 16:21:33 +0100452 public void onDestroy() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000453 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONDESTROY");
Sander Alewijnse56f71572014-06-23 16:21:33 +0100454 if (mServiceMessageReceiver != null) {
455 LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver);
456 mServiceMessageReceiver = null;
457 }
Sander Alewijnse56f71572014-06-23 16:21:33 +0100458 super.onDestroy();
459 }
460
461 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100462 protected void onRestart() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000463 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESTART");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100464 super.onRestart();
465 }
466
Jessica Hummel14eeef92014-06-16 11:06:20 +0100467 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100468 protected void onResume() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000469 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONRESUME");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100470 super.onResume();
471 }
472
Jessica Hummel14eeef92014-06-16 11:06:20 +0100473 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100474 protected void onPause() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000475 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONPAUSE");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100476 super.onPause();
477 }
478
Jessica Hummel14eeef92014-06-16 11:06:20 +0100479 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100480 protected void onStop() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000481 if (DEBUG) ProvisionLogger.logd("Device owner provisioning activity ONSTOP");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100482 super.onStop();
483 }
Rubin Xu44cdbdf2014-11-28 15:19:14 +0000484
485 @Override
486 public void onNavigationBarCreated(SetupWizardNavBar bar) {
487 bar.getNextButton().setVisibility(View.INVISIBLE);
488 }
489
490 @Override
491 public void onNavigateBack() {
492 onBackPressed();
493 }
494
495 @Override
496 public void onNavigateNext() {
497 }
Sander Alewijnseed0883b2014-03-18 15:01:13 +0000498}
499