Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [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.AlarmManager; |
| 20 | import android.app.Service; |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 21 | import android.app.admin.DeviceInitializerStatus; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 27 | import android.os.Bundle; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 28 | import android.os.IBinder; |
Nicolas Prevot | 3b76f0d | 2014-09-03 15:33:42 +0100 | [diff] [blame] | 29 | import android.os.UserHandle; |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 30 | import android.support.v4.content.LocalBroadcastManager; |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 31 | import android.text.TextUtils; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 32 | |
| 33 | import com.android.internal.app.LocalePicker; |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 34 | import com.android.managedprovisioning.ProvisioningParams.PackageDownloadInfo; |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 35 | import com.android.managedprovisioning.proxy.BluetoothConnectionService; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 36 | import com.android.managedprovisioning.task.AddWifiNetworkTask; |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 37 | import com.android.managedprovisioning.task.BluetoothConnectTask; |
Sander Alewijnse | 2818d32 | 2014-05-20 14:54:13 +0100 | [diff] [blame] | 38 | import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 39 | import com.android.managedprovisioning.task.DownloadPackageTask; |
| 40 | import com.android.managedprovisioning.task.InstallPackageTask; |
| 41 | import com.android.managedprovisioning.task.SetDevicePolicyTask; |
Craig Lafayette | 0992876 | 2015-03-29 20:48:07 -0400 | [diff] [blame] | 42 | import com.android.managedprovisioning.task.WipeResetProtectionTask; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 43 | |
| 44 | import java.lang.Runnable; |
| 45 | import java.util.Locale; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * This service does the work for the DeviceOwnerProvisioningActivity. |
| 49 | * Feedback is sent back to the activity via intents. |
| 50 | * |
| 51 | * <p> |
| 52 | * If the corresponding activity is killed and restarted, the service is |
| 53 | * called twice. The service will not start the provisioning flow a second time, but instead |
| 54 | * send a status update to the activity. |
| 55 | * </p> |
| 56 | */ |
| 57 | public class DeviceOwnerProvisioningService extends Service { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 58 | private static final boolean DEBUG = false; // To control logging. |
| 59 | |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 60 | private static final String DEVICE_OWNER = "deviceOwner"; |
| 61 | private static final String DEVICE_INITIALIZER = "deviceInitializer"; |
| 62 | |
Sander Alewijnse | 639e94c | 2014-05-01 16:07:51 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Intent action to activate the CDMA phone connection by OTASP. |
| 65 | * This is not necessary for a GSM phone connection, which is activated automatically. |
| 66 | * String must agree with the constants in com.android.phone.InCallScreenShowActivation. |
| 67 | */ |
| 68 | private static final String ACTION_PERFORM_CDMA_PROVISIONING = |
| 69 | "com.android.phone.PERFORM_CDMA_PROVISIONING"; |
| 70 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 71 | // Intent actions and extras for communication from DeviceOwnerProvisioningService to Activity. |
| 72 | protected static final String EXTRA_PROVISIONING_PARAMS = |
| 73 | "ProvisioningParams"; |
| 74 | |
| 75 | // Intent actions and extras for communication from DeviceOwnerProvisioningActivity to Service. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 76 | protected static final String ACTION_PROVISIONING_SUCCESS = |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 77 | "com.android.managedprovisioning.provisioning_success"; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 78 | protected static final String ACTION_PROVISIONING_ERROR = |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 79 | "com.android.managedprovisioning.error"; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 80 | protected static final String EXTRA_USER_VISIBLE_ERROR_ID_KEY = |
| 81 | "UserVisibleErrorMessage-Id"; |
| 82 | protected static final String ACTION_PROGRESS_UPDATE = |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 83 | "com.android.managedprovisioning.progress_update"; |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 84 | protected static final String EXTRA_PROGRESS_MESSAGE_ID_KEY = |
| 85 | "ProgressMessageId"; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 86 | protected static final String ACTION_REQUEST_WIFI_PICK = |
| 87 | "com.android.managedprovisioning.request_wifi_pick"; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 88 | |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 89 | // Intent action used by the HomeReceiverActivity to notify this Service that a HOME intent was |
| 90 | // received, which indicates that the Setup wizard has closed after provisioning completed. |
| 91 | protected static final String ACTION_HOME_INDIRECT = |
| 92 | "com.android.managedprovisioning.home_indirect"; |
| 93 | |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 94 | // Indicates whether provisioning has started. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 95 | private boolean mProvisioningInFlight = false; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 96 | |
| 97 | // MessageId of the last progress message. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 98 | private int mLastProgressMessage = -1; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 99 | |
| 100 | // MessageId of the last error message. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 101 | private int mLastErrorMessage = -1; |
Sander Alewijnse | 326bcfd | 2014-06-25 15:24:03 +0100 | [diff] [blame] | 102 | |
Rubin Xu | 1662fa3 | 2015-03-18 12:44:28 +0000 | [diff] [blame] | 103 | // Indicates whether provisioning has finished successfully (service waiting to stop). |
| 104 | private volatile boolean mDone = false; |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 105 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 106 | // Provisioning tasks. |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 107 | private BluetoothConnectTask mBluetoothConnectTask; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 108 | private AddWifiNetworkTask mAddWifiNetworkTask; |
Craig Lafayette | 0992876 | 2015-03-29 20:48:07 -0400 | [diff] [blame] | 109 | private WipeResetProtectionTask mWipeResetProtectionTask; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 110 | private DownloadPackageTask mDownloadPackageTask; |
| 111 | private InstallPackageTask mInstallPackageTask; |
| 112 | private SetDevicePolicyTask mSetDevicePolicyTask; |
| 113 | private DeleteNonRequiredAppsTask mDeleteNonRequiredAppsTask; |
| 114 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 115 | private ProvisioningParams mParams; |
| 116 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 117 | @Override |
| 118 | public int onStartCommand(final Intent intent, int flags, int startId) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 119 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONSTARTCOMMAND."); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 120 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 121 | synchronized (this) { // Make operations on mProvisioningInFlight atomic. |
| 122 | if (mProvisioningInFlight) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 123 | if (DEBUG) ProvisionLogger.logd("Provisioning already in flight."); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 124 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 125 | sendProgressUpdateToActivity(); |
| 126 | |
| 127 | // Send error message if currently in error state. |
| 128 | if (mLastErrorMessage >= 0) { |
| 129 | sendError(); |
| 130 | } |
| 131 | |
Rubin Xu | 1662fa3 | 2015-03-18 12:44:28 +0000 | [diff] [blame] | 132 | // Send success if provisioning was successful. |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 133 | if (mDone) { |
Nicolas Prevot | 0b44725 | 2015-03-09 14:59:02 +0000 | [diff] [blame] | 134 | onProvisioningSuccess(); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 135 | } |
| 136 | } else { |
| 137 | mProvisioningInFlight = true; |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 138 | if (DEBUG) ProvisionLogger.logd("First start of the service."); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 139 | progressUpdate(R.string.progress_data_process); |
| 140 | |
| 141 | // Load the ProvisioningParams (from message in Intent). |
| 142 | mParams = (ProvisioningParams) intent.getParcelableExtra(EXTRA_PROVISIONING_PARAMS); |
| 143 | |
| 144 | // Do the work on a separate thread. |
| 145 | new Thread(new Runnable() { |
| 146 | public void run() { |
| 147 | initializeProvisioningEnvironment(mParams); |
| 148 | startDeviceOwnerProvisioning(mParams); |
| 149 | } |
| 150 | }).start(); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 151 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 152 | } |
| 153 | return START_NOT_STICKY; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * This is the core method of this class. It goes through every provisioning step. |
Julia Reynolds | ccd6016 | 2015-02-17 11:53:48 -0500 | [diff] [blame] | 158 | * Each task checks if it is required and executes if it is. |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 159 | */ |
Sander Alewijnse | e96a820 | 2014-05-19 16:55:39 +0100 | [diff] [blame] | 160 | private void startDeviceOwnerProvisioning(final ProvisioningParams params) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 161 | if (DEBUG) ProvisionLogger.logd("Starting device owner provisioning"); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 162 | |
| 163 | // Construct Tasks. Do not start them yet. |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 164 | mBluetoothConnectTask = new BluetoothConnectTask(this, params.bluetoothInfo, |
| 165 | !TextUtils.isEmpty(params.wifiInfo.ssid), |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 166 | new BluetoothConnectTask.Callback() { |
| 167 | @Override |
| 168 | public void onSuccess() { |
| 169 | progressUpdate(R.string.progress_connect_to_wifi); |
| 170 | mAddWifiNetworkTask.run(); |
| 171 | } |
| 172 | @Override |
| 173 | public void onError() { |
| 174 | error(R.string.device_owner_error_bluetooth); |
| 175 | } |
| 176 | }); |
| 177 | |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 178 | mAddWifiNetworkTask = new AddWifiNetworkTask(this, params.wifiInfo, |
| 179 | new AddWifiNetworkTask.Callback() { |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 180 | @Override |
| 181 | public void onSuccess() { |
Craig Lafayette | 0992876 | 2015-03-29 20:48:07 -0400 | [diff] [blame] | 182 | progressUpdate(R.string.progress_wipe_frp); |
| 183 | mWipeResetProtectionTask.run(); |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void onError(){ |
| 188 | error(R.string.device_owner_error_wifi); |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 189 | statusUpdate(DeviceInitializerStatus.STATUS_ERROR_CONNECT_WIFI); |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 190 | } |
| 191 | }); |
| 192 | |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 193 | mWipeResetProtectionTask = new WipeResetProtectionTask(this, params.frpChallengeBundle, |
Craig Lafayette | 0992876 | 2015-03-29 20:48:07 -0400 | [diff] [blame] | 194 | new WipeResetProtectionTask.Callback() { |
| 195 | @Override |
| 196 | public void onSuccess() { |
| 197 | progressUpdate(R.string.progress_download); |
| 198 | mDownloadPackageTask.run(); |
| 199 | } |
| 200 | @Override |
| 201 | public void onError() { |
| 202 | error(R.string.device_owner_error_frp); |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 203 | statusUpdate(DeviceInitializerStatus. |
| 204 | STATUS_ERROR_RESET_PROTECTION_BLOCKING_PROVISIONING); |
Craig Lafayette | 0992876 | 2015-03-29 20:48:07 -0400 | [diff] [blame] | 205 | } |
| 206 | }); |
| 207 | |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 208 | mDownloadPackageTask = new DownloadPackageTask(this, |
| 209 | new DownloadPackageTask.Callback() { |
Julia Reynolds | ccd6016 | 2015-02-17 11:53:48 -0500 | [diff] [blame] | 210 | @Override |
| 211 | public void onSuccess() { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 212 | progressUpdate(R.string.progress_install); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 213 | mInstallPackageTask.addInstallIfNecessary( |
| 214 | params.inferDeviceAdminPackageName(), |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 215 | mDownloadPackageTask.getDownloadedPackageLocation( |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 216 | DEVICE_OWNER)); |
| 217 | mInstallPackageTask.addInstallIfNecessary( |
| 218 | params.getDeviceInitializerPackageName(), |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 219 | mDownloadPackageTask.getDownloadedPackageLocation( |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 220 | DEVICE_INITIALIZER)); |
| 221 | mInstallPackageTask.run(); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 222 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 223 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 224 | @Override |
| 225 | public void onError(int errorCode) { |
| 226 | switch(errorCode) { |
| 227 | case DownloadPackageTask.ERROR_HASH_MISMATCH: |
| 228 | error(R.string.device_owner_error_hash_mismatch); |
| 229 | break; |
| 230 | case DownloadPackageTask.ERROR_DOWNLOAD_FAILED: |
| 231 | error(R.string.device_owner_error_download_failed); |
| 232 | break; |
| 233 | default: |
| 234 | error(R.string.device_owner_error_general); |
| 235 | break; |
| 236 | } |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 237 | statusUpdate(DeviceInitializerStatus.STATUS_ERROR_DOWNLOAD_PACKAGE); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 238 | } |
| 239 | }); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 240 | |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 241 | // Add packages to download to the DownloadPackageTask. |
| 242 | mDownloadPackageTask.addDownloadIfNecessary(params.inferDeviceAdminPackageName(), |
| 243 | params.deviceAdminDownloadInfo, DEVICE_OWNER); |
| 244 | mDownloadPackageTask.addDownloadIfNecessary(params.getDeviceInitializerPackageName(), |
| 245 | params.deviceInitializerDownloadInfo, DEVICE_INITIALIZER); |
| 246 | |
| 247 | mInstallPackageTask = new InstallPackageTask(this, |
| 248 | new InstallPackageTask.Callback() { |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 249 | @Override |
| 250 | public void onSuccess() { |
| 251 | progressUpdate(R.string.progress_set_owner); |
Nicolas Prevot | 668d65f | 2015-03-10 17:58:15 +0000 | [diff] [blame] | 252 | try { |
| 253 | // Now that the app has been installed, we can look for the device admin |
| 254 | // component in it. |
| 255 | mSetDevicePolicyTask.run(mParams.inferDeviceAdminComponentName( |
| 256 | DeviceOwnerProvisioningService.this)); |
| 257 | } catch (Utils.IllegalProvisioningArgumentException e) { |
| 258 | error(R.string.device_owner_error_general); |
| 259 | ProvisionLogger.loge("Failed to infer the device admin component name", |
| 260 | e); |
| 261 | return; |
| 262 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 263 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 264 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 265 | @Override |
| 266 | public void onError(int errorCode) { |
| 267 | switch(errorCode) { |
| 268 | case InstallPackageTask.ERROR_PACKAGE_INVALID: |
| 269 | error(R.string.device_owner_error_package_invalid); |
| 270 | break; |
| 271 | case InstallPackageTask.ERROR_INSTALLATION_FAILED: |
| 272 | error(R.string.device_owner_error_installation_failed); |
| 273 | break; |
Joe Delfino | 84e56f5 | 2015-03-27 09:56:18 -0400 | [diff] [blame] | 274 | case InstallPackageTask.ERROR_PACKAGE_NAME_INVALID: |
| 275 | error(R.string.device_owner_error_package_name_invalid); |
| 276 | break; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 277 | default: |
| 278 | error(R.string.device_owner_error_general); |
| 279 | break; |
| 280 | } |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 281 | statusUpdate(DeviceInitializerStatus.STATUS_ERROR_INSTALL_PACKAGE); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 282 | } |
| 283 | }); |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 284 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 285 | mSetDevicePolicyTask = new SetDevicePolicyTask(this, |
Sander Alewijnse | 56f7157 | 2014-06-23 16:21:33 +0100 | [diff] [blame] | 286 | getResources().getString(R.string.default_owned_device_username), |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 287 | params.deviceInitializerComponentName, |
Julia Reynolds | 3f4eb37 | 2015-02-11 13:46:42 -0500 | [diff] [blame] | 288 | getResources().getString(R.string.default_owned_device_username), |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 289 | new SetDevicePolicyTask.Callback() { |
| 290 | @Override |
| 291 | public void onSuccess() { |
Julia Reynolds | ccd6016 | 2015-02-17 11:53:48 -0500 | [diff] [blame] | 292 | mDeleteNonRequiredAppsTask.run(); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 293 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 294 | |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 295 | @Override |
| 296 | public void onError(int errorCode) { |
| 297 | switch(errorCode) { |
| 298 | case SetDevicePolicyTask.ERROR_PACKAGE_NOT_INSTALLED: |
| 299 | error(R.string.device_owner_error_package_not_installed); |
| 300 | break; |
Sander Alewijnse | cdf13b1 | 2014-06-19 15:58:15 +0100 | [diff] [blame] | 301 | case SetDevicePolicyTask.ERROR_NO_RECEIVER: |
Sander Alewijnse | b6578e7 | 2014-07-25 17:12:05 +0100 | [diff] [blame] | 302 | error(R.string.device_owner_error_package_invalid); |
Sander Alewijnse | cdf13b1 | 2014-06-19 15:58:15 +0100 | [diff] [blame] | 303 | break; |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 304 | default: |
| 305 | error(R.string.device_owner_error_general); |
| 306 | break; |
| 307 | } |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 308 | statusUpdate(DeviceInitializerStatus.STATUS_ERROR_SET_DEVICE_POLICY); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 309 | } |
| 310 | }); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 311 | |
Nicolas Prevot | 3ebb7b0 | 2014-09-08 15:42:15 +0100 | [diff] [blame] | 312 | mDeleteNonRequiredAppsTask = new DeleteNonRequiredAppsTask( |
Nicolas Prevot | 668d65f | 2015-03-10 17:58:15 +0000 | [diff] [blame] | 313 | this, params.inferDeviceAdminPackageName(), R.array.required_apps_managed_device, |
Sander Alewijnse | d5e4c42 | 2014-11-25 17:56:16 +0000 | [diff] [blame] | 314 | R.array.vendor_required_apps_managed_device, true /* creating new profile */, |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 315 | UserHandle.USER_OWNER, params.leaveAllSystemAppsEnabled, |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 316 | new DeleteNonRequiredAppsTask.Callback() { |
Rubin Xu | 1662fa3 | 2015-03-18 12:44:28 +0000 | [diff] [blame] | 317 | @Override |
Sander Alewijnse | 2818d32 | 2014-05-20 14:54:13 +0100 | [diff] [blame] | 318 | public void onSuccess() { |
| 319 | // Done with provisioning. Success. |
Nicolas Prevot | 0b44725 | 2015-03-09 14:59:02 +0000 | [diff] [blame] | 320 | onProvisioningSuccess(); |
Sander Alewijnse | 2818d32 | 2014-05-20 14:54:13 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 323 | @Override |
Sander Alewijnse | 2818d32 | 2014-05-20 14:54:13 +0100 | [diff] [blame] | 324 | public void onError() { |
| 325 | error(R.string.device_owner_error_general); |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 326 | statusUpdate(DeviceInitializerStatus.STATUS_ERROR_DELETE_APPS); |
Rubin Xu | 1662fa3 | 2015-03-18 12:44:28 +0000 | [diff] [blame] | 327 | } |
Nicolas Prevot | 3ebb7b0 | 2014-09-08 15:42:15 +0100 | [diff] [blame] | 328 | }); |
Sander Alewijnse | 2818d32 | 2014-05-20 14:54:13 +0100 | [diff] [blame] | 329 | |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 330 | // Start first task, which starts next task in its callback, etc. |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 331 | progressUpdate(R.string.progress_start_bluetooth); |
| 332 | mBluetoothConnectTask.run(); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | private void error(int dialogMessage) { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 336 | mLastErrorMessage = dialogMessage; |
| 337 | sendError(); |
| 338 | // Wait for stopService() call from the activity. |
| 339 | } |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 340 | |
Craig Lafayette | c25553f | 2015-03-28 17:00:52 -0400 | [diff] [blame] | 341 | private void statusUpdate(int statusCode) { |
| 342 | BluetoothConnectionService.sendStatusUpdate(this, statusCode); |
| 343 | } |
| 344 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 345 | private void sendError() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 346 | if (DEBUG) { |
| 347 | ProvisionLogger.logd("Reporting Error: " + getResources() |
| 348 | .getString(mLastErrorMessage)); |
| 349 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 350 | Intent intent = new Intent(ACTION_PROVISIONING_ERROR); |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 351 | intent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 352 | intent.putExtra(EXTRA_USER_VISIBLE_ERROR_ID_KEY, mLastErrorMessage); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 353 | LocalBroadcastManager.getInstance(this).sendBroadcast(intent); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | private void progressUpdate(int progressMessage) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 357 | if (DEBUG) { |
| 358 | ProvisionLogger.logd("Reporting progress update: " + getResources() |
| 359 | .getString(progressMessage)); |
| 360 | } |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 361 | mLastProgressMessage = progressMessage; |
| 362 | sendProgressUpdateToActivity(); |
| 363 | } |
| 364 | |
| 365 | private void sendProgressUpdateToActivity() { |
| 366 | Intent intent = new Intent(ACTION_PROGRESS_UPDATE); |
| 367 | intent.putExtra(EXTRA_PROGRESS_MESSAGE_ID_KEY, mLastProgressMessage); |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 368 | intent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 369 | LocalBroadcastManager.getInstance(this).sendBroadcast(intent); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 370 | } |
| 371 | |
Nicolas Prevot | 0b44725 | 2015-03-09 14:59:02 +0000 | [diff] [blame] | 372 | private void onProvisioningSuccess() { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 373 | if (DEBUG) ProvisionLogger.logd("Reporting success."); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 374 | mDone = true; |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 375 | |
Rubin Xu | 292e9a3 | 2015-03-20 17:02:38 +0000 | [diff] [blame] | 376 | // Persist mParams so HomeReceiverActivity can later retrieve them to finalize provisioning. |
| 377 | // This is necessary to deal with accidental reboots during DIA setup, which happens between |
| 378 | // the end of this method and HomeReceiverActivity captures the home intent. |
| 379 | IntentStore store = BootReminder.getDeviceOwnerFinalizingIntentStore(this); |
| 380 | Bundle resumeBundle = new Bundle(); |
| 381 | (new MessageParser()).addProvisioningParamsToBundle(resumeBundle, mParams); |
| 382 | store.save(resumeBundle); |
| 383 | |
Sander Alewijnse | ab18ea7 | 2014-09-11 15:15:23 +0100 | [diff] [blame] | 384 | // Enable the HomeReceiverActivity, since the DeviceOwnerProvisioningActivity will shutdown |
| 385 | // the Setup wizard soon, which will result in a home intent that should be caught by the |
| 386 | // HomeReceiverActivity. |
| 387 | PackageManager pm = getPackageManager(); |
| 388 | pm.setComponentEnabledSetting(new ComponentName(DeviceOwnerProvisioningService.this, |
| 389 | HomeReceiverActivity.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, |
| 390 | PackageManager.DONT_KILL_APP); |
| 391 | |
Jessica Hummel | 14eeef9 | 2014-06-16 11:06:20 +0100 | [diff] [blame] | 392 | Intent successIntent = new Intent(ACTION_PROVISIONING_SUCCESS); |
| 393 | successIntent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class); |
Jessica Hummel | 81fe104 | 2014-06-23 17:10:38 +0100 | [diff] [blame] | 394 | LocalBroadcastManager.getInstance(this).sendBroadcast(successIntent); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 395 | // Wait for stopService() call from the activity. |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | private void initializeProvisioningEnvironment(ProvisioningParams params) { |
Sander Alewijnse | 74d6c14 | 2015-04-13 11:51:19 +0100 | [diff] [blame] | 399 | setTimeAndTimezone(params.timeZone, params.localTime); |
| 400 | setLocale(params.locale); |
Sander Alewijnse | 639e94c | 2014-05-01 16:07:51 +0100 | [diff] [blame] | 401 | |
| 402 | // Start CDMA activation to enable phone calls. |
| 403 | final Intent intent = new Intent(ACTION_PERFORM_CDMA_PROVISIONING); |
| 404 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 405 | if (DEBUG) ProvisionLogger.logd("Starting cdma activation activity"); |
Sander Alewijnse | 639e94c | 2014-05-01 16:07:51 +0100 | [diff] [blame] | 406 | startActivity(intent); // Activity will be a Nop if not a CDMA device. |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 409 | private void setTimeAndTimezone(String timeZone, long localTime) { |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 410 | try { |
| 411 | final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); |
| 412 | if (timeZone != null) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 413 | if (DEBUG) ProvisionLogger.logd("Setting time zone to " + timeZone); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 414 | am.setTimeZone(timeZone); |
| 415 | } |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 416 | if (localTime > 0) { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 417 | if (DEBUG) ProvisionLogger.logd("Setting time to " + localTime); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 418 | am.setTime(localTime); |
| 419 | } |
| 420 | } catch (Exception e) { |
| 421 | ProvisionLogger.loge("Alarm manager failed to set the system time/timezone."); |
| 422 | // Do not stop provisioning process, but ignore this error. |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private void setLocale(Locale locale) { |
| 427 | if (locale == null || locale.equals(Locale.getDefault())) { |
| 428 | return; |
| 429 | } |
| 430 | try { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 431 | if (DEBUG) ProvisionLogger.logd("Setting locale to " + locale); |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 432 | // If locale is different from current locale this results in a configuration change, |
| 433 | // which will trigger the restarting of the activity. |
| 434 | LocalePicker.updateLocale(locale); |
| 435 | } catch (Exception e) { |
| 436 | ProvisionLogger.loge("Failed to set the system locale."); |
| 437 | // Do not stop provisioning process, but ignore this error. |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | @Override |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 442 | public void onCreate () { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 443 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONCREATE."); |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 447 | public void onDestroy () { |
Sander Alewijnse | 9a40ab0 | 2014-10-27 18:36:56 +0000 | [diff] [blame] | 448 | if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONDESTROY"); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 449 | if (mAddWifiNetworkTask != null) { |
Sander Alewijnse | d704385 | 2014-06-17 15:50:48 +0100 | [diff] [blame] | 450 | mAddWifiNetworkTask.cleanUp(); |
| 451 | } |
| 452 | if (mDownloadPackageTask != null) { |
| 453 | mDownloadPackageTask.cleanUp(); |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 454 | } |
Craig Lafayette | 85582cf | 2015-04-14 22:57:33 -0400 | [diff] [blame] | 455 | if (mBluetoothConnectTask != null) { |
| 456 | mBluetoothConnectTask.cleanUp(); |
| 457 | } |
Sander Alewijnse | 28bffd6 | 2014-06-05 10:54:26 +0100 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Override |
Sander Alewijnse | 04ab6fe | 2014-04-29 10:53:54 +0100 | [diff] [blame] | 461 | public IBinder onBind(Intent intent) { |
| 462 | return null; |
| 463 | } |
| 464 | } |
| 465 | |