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; |
| 21 | import android.app.admin.DevicePolicyManager; |
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; |
| 28 | import android.provider.Settings.Global; |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame^] | 29 | import android.support.v4.content.LocalBroadcastManager; |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 32 | import android.widget.TextView; |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 33 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 34 | /** |
| 35 | * This activity starts device owner provisioning: |
| 36 | * It downloads a mobile device management application(mdm) from a given url and installs it, |
| 37 | * or a given mdm is already present on the device. The mdm is set as the owner of the device so |
| 38 | * that it has full control over the device: |
| 39 | * TODO: put link here with documentation on how a device owner has control over the device |
| 40 | * The mdm can then execute further setup steps. |
| 41 | * |
| 42 | * <p> |
| 43 | * An example use case might be when a company wants to set up a device for a single use case |
| 44 | * (such as giving instructions). |
| 45 | * </p> |
| 46 | * |
| 47 | * <p> |
| 48 | * Provisioning is triggered by a programmer device that sends required provisioning parameters via |
| 49 | * nfc. For an example of a programmer app see: |
| 50 | * com.example.android.apis.app.DeviceProvisioningProgrammerSample. |
| 51 | * </p> |
| 52 | * |
Sander Alewijnse | 4c4badf | 2014-03-20 14:12:49 +0000 | [diff] [blame] | 53 | * <p> |
| 54 | * In the unlikely case that this activity is killed the whole provisioning process so far is |
| 55 | * repeated. We made sure that all tasks can be done twice without causing any problems. |
| 56 | * </p> |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 57 | */ |
| 58 | public class DeviceOwnerProvisioningActivity extends Activity { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 59 | private static final int ENCRYPT_DEVICE_REQUEST_CODE = 1; |
| 60 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 61 | private BroadcastReceiver mServiceMessageReceiver; |
| 62 | private TextView mProgressTextView; |
| 63 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 64 | @Override |
| 65 | public void onCreate(Bundle savedInstanceState) { |
| 66 | super.onCreate(savedInstanceState); |
| 67 | |
| 68 | ProvisionLogger.logd("Device owner provisioning activity ONCREATE"); |
| 69 | |
| 70 | // Check whether we can provision. |
| 71 | if (Global.getInt(getContentResolver(), Global.DEVICE_PROVISIONED, /* default */ 0) != 0) { |
| 72 | ProvisionLogger.loge("Device already provisioned."); |
| 73 | error(R.string.device_owner_error_already_provisioned); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | DevicePolicyManager dpm = (DevicePolicyManager) |
| 78 | getSystemService(Context.DEVICE_POLICY_SERVICE); |
| 79 | if (dpm.getDeviceOwner() != null) { |
| 80 | ProvisionLogger.loge("Device owner already present."); |
| 81 | error(R.string.device_owner_error_already_owned); |
| 82 | return; |
| 83 | } |
| 84 | |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 85 | final LayoutInflater inflater = getLayoutInflater(); |
Sander Alewijnse | 8ce0c51 | 2014-06-03 17:49:02 +0100 | [diff] [blame] | 86 | final View contentView = inflater.inflate(R.layout.progress, null); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 87 | setContentView(contentView); |
Sander Alewijnse | 8ce0c51 | 2014-06-03 17:49:02 +0100 | [diff] [blame] | 88 | mProgressTextView = (TextView) findViewById(R.id.prog_text); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 89 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 90 | // Setup broadcast receiver for feedback from service. |
| 91 | mServiceMessageReceiver = new ServiceMessageReceiver(); |
| 92 | IntentFilter filter = new IntentFilter(); |
| 93 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS); |
| 94 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR); |
| 95 | filter.addAction(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 96 | filter.addAction(DeviceOwnerProvisioningService.ACTION_REQUEST_ENCRYPTION); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame^] | 97 | LocalBroadcastManager.getInstance(this).registerReceiver(mServiceMessageReceiver, filter); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 98 | |
| 99 | // Start service. |
| 100 | Intent intent = new Intent(this, DeviceOwnerProvisioningService.class); |
| 101 | intent.putExtras(getIntent()); |
| 102 | startService(intent); |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 105 | class ServiceMessageReceiver extends BroadcastReceiver |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 106 | { |
| 107 | @Override |
| 108 | public void onReceive(Context context, Intent intent) |
| 109 | { |
| 110 | String action = intent.getAction(); |
| 111 | if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_SUCCESS)) { |
| 112 | ProvisionLogger.logd("Successfully provisioned"); |
| 113 | finish(); |
| 114 | return; |
| 115 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROVISIONING_ERROR)) { |
| 116 | int errorCode = intent.getIntExtra( |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 117 | DeviceOwnerProvisioningService.EXTRA_USER_VISIBLE_ERROR_ID_KEY, -1); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 118 | ProvisionLogger.logd("Error reported with code " |
| 119 | + getResources().getString(errorCode)); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 120 | if (errorCode < 0) { |
| 121 | error(R.string.device_owner_error_general); |
| 122 | return; |
Sander Alewijnse | af8413e | 2014-03-19 11:37:44 +0000 | [diff] [blame] | 123 | } else { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 124 | error(errorCode); |
| 125 | } |
| 126 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_PROGRESS_UPDATE)) { |
| 127 | int progressMessage = intent.getIntExtra( |
| 128 | DeviceOwnerProvisioningService.EXTRA_PROGRESS_MESSAGE_ID_KEY, -1); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 129 | ProvisionLogger.logd("Progress update reported with code " |
| 130 | + getResources().getString(progressMessage)); |
| 131 | if (progressMessage >= 0) { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 132 | progressUpdate(progressMessage); |
Sander Alewijnse | af8413e | 2014-03-19 11:37:44 +0000 | [diff] [blame] | 133 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 134 | } else if (action.equals(DeviceOwnerProvisioningService.ACTION_REQUEST_ENCRYPTION)) { |
| 135 | ProvisionLogger.logd("Received request to encrypt device."); |
| 136 | Intent encryptIntent = new Intent(DeviceOwnerProvisioningActivity.this, |
| 137 | EncryptDeviceActivity.class); |
| 138 | encryptIntent.putExtras(intent); |
| 139 | startActivityForResult(encryptIntent, ENCRYPT_DEVICE_REQUEST_CODE); |
Sander Alewijnse | c775738 | 2014-03-18 17:09:45 +0000 | [diff] [blame] | 140 | } |
Sander Alewijnse | 63254f4 | 2014-03-21 15:31:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void onBackPressed() { |
| 146 | // TODO: Handle this graciously by stopping the provisioning flow and cleaning up. |
| 147 | } |
| 148 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 149 | @Override |
| 150 | public void onDestroy() { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 151 | ProvisionLogger.logd("Device owner provisioning activity ONDESTROY"); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame^] | 152 | LocalBroadcastManager.getInstance(this).unregisterReceiver(mServiceMessageReceiver); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 153 | super.onDestroy(); |
| 154 | } |
| 155 | |
| 156 | private void progressUpdate(int progressMessage) { |
| 157 | mProgressTextView.setText(progressMessage); |
| 158 | } |
| 159 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 160 | @Override |
| 161 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 162 | if (requestCode == ENCRYPT_DEVICE_REQUEST_CODE) { |
| 163 | if (resultCode == RESULT_CANCELED) { |
| 164 | ProvisionLogger.loge("User canceled device encryption."); |
| 165 | finish(); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 170 | private void error(int dialogMessage) { |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 171 | AlertDialog dlg = new AlertDialog.Builder(this) |
| 172 | .setTitle(R.string.device_owner_error_title) |
| 173 | .setMessage(dialogMessage) |
| 174 | .setCancelable(false) |
| 175 | .setPositiveButton("OK", new DialogInterface.OnClickListener() { |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 176 | @Override |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 177 | public void onClick(DialogInterface dialog,int id) { |
| 178 | // Close activity |
| 179 | finish(); |
| 180 | } |
| 181 | }) |
| 182 | .create(); |
| 183 | dlg.show(); |
| 184 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 185 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 186 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 187 | protected void onRestart() { |
| 188 | ProvisionLogger.logd("Device owner provisioning activity ONRESTART"); |
| 189 | super.onRestart(); |
| 190 | } |
| 191 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 192 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 193 | protected void onResume() { |
| 194 | ProvisionLogger.logd("Device owner provisioning activity ONRESUME"); |
| 195 | super.onResume(); |
| 196 | } |
| 197 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 198 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 199 | protected void onPause() { |
| 200 | ProvisionLogger.logd("Device owner provisioning activity ONPAUSE"); |
| 201 | super.onPause(); |
| 202 | } |
| 203 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 204 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 205 | protected void onStop() { |
| 206 | ProvisionLogger.logd("Device owner provisioning activity ONSTOP"); |
| 207 | super.onStop(); |
| 208 | } |
Sander Alewijnse | ed0883b | 2014-03-18 15:01:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |