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