blob: 0f59f7167cb9ad090c978e5ed533c807842fe68a [file] [log] [blame]
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +01001/*
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
Sander Alewijnseab18ea72014-09-11 15:15:23 +010019import static android.app.admin.DeviceAdminReceiver.ACTION_PROFILE_PROVISIONING_COMPLETE;
20import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010022import android.app.AlarmManager;
23import android.app.Service;
Sander Alewijnseab18ea72014-09-11 15:15:23 +010024import android.content.BroadcastReceiver;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010025import android.content.ComponentName;
26import android.content.Context;
27import android.content.Intent;
Sander Alewijnseab18ea72014-09-11 15:15:23 +010028import android.content.IntentFilter;
29import android.content.pm.PackageManager;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010030import android.os.IBinder;
Nicolas Prevot3b76f0d2014-09-03 15:33:42 +010031import android.os.UserHandle;
Jessica Hummel81fe1042014-06-23 17:10:38 +010032import android.support.v4.content.LocalBroadcastManager;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010033import android.text.TextUtils;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010034
35import com.android.internal.app.LocalePicker;
36import com.android.managedprovisioning.task.AddWifiNetworkTask;
Sander Alewijnse2818d322014-05-20 14:54:13 +010037import com.android.managedprovisioning.task.DeleteNonRequiredAppsTask;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010038import com.android.managedprovisioning.task.DownloadPackageTask;
39import com.android.managedprovisioning.task.InstallPackageTask;
40import com.android.managedprovisioning.task.SetDevicePolicyTask;
41
42import java.lang.Runnable;
43import java.util.Locale;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010044
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 */
55public class DeviceOwnerProvisioningService extends Service {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +000056 private static final boolean DEBUG = false; // To control logging.
57
Sander Alewijnse639e94c2014-05-01 16:07:51 +010058 /**
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 Alewijnse326bcfd2014-06-25 15:24:03 +010066 // 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 Alewijnsed7043852014-06-17 15:50:48 +010071 protected static final String ACTION_PROVISIONING_SUCCESS =
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010072 "com.android.managedprovisioning.provisioning_success";
Sander Alewijnsed7043852014-06-17 15:50:48 +010073 protected static final String ACTION_PROVISIONING_ERROR =
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010074 "com.android.managedprovisioning.error";
Sander Alewijnsed7043852014-06-17 15:50:48 +010075 protected static final String EXTRA_USER_VISIBLE_ERROR_ID_KEY =
76 "UserVisibleErrorMessage-Id";
77 protected static final String ACTION_PROGRESS_UPDATE =
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010078 "com.android.managedprovisioning.progress_update";
Sander Alewijnsed7043852014-06-17 15:50:48 +010079 protected static final String EXTRA_PROGRESS_MESSAGE_ID_KEY =
80 "ProgressMessageId";
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010081 protected static final String ACTION_REQUEST_WIFI_PICK =
82 "com.android.managedprovisioning.request_wifi_pick";
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +010083
Sander Alewijnseab18ea72014-09-11 15:15:23 +010084 // 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 Alewijnse326bcfd2014-06-25 15:24:03 +010089 // Indicates whether provisioning has started.
Sander Alewijnsed7043852014-06-17 15:50:48 +010090 private boolean mProvisioningInFlight = false;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010091
92 // MessageId of the last progress message.
Sander Alewijnsed7043852014-06-17 15:50:48 +010093 private int mLastProgressMessage = -1;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010094
95 // MessageId of the last error message.
Sander Alewijnsed7043852014-06-17 15:50:48 +010096 private int mLastErrorMessage = -1;
Sander Alewijnse326bcfd2014-06-25 15:24:03 +010097
98 // Indicates whether provisioning has finished succesfully (service waiting to stop).
Sander Alewijnsed7043852014-06-17 15:50:48 +010099 private boolean mDone = false;
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100100
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100101 // Provisioning tasks.
102 private AddWifiNetworkTask mAddWifiNetworkTask;
103 private DownloadPackageTask mDownloadPackageTask;
104 private InstallPackageTask mInstallPackageTask;
105 private SetDevicePolicyTask mSetDevicePolicyTask;
106 private DeleteNonRequiredAppsTask mDeleteNonRequiredAppsTask;
107
Sander Alewijnsed7043852014-06-17 15:50:48 +0100108 private ProvisioningParams mParams;
109
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100110 private BroadcastReceiver mIndirectHomeReceiver;
111
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100112 @Override
113 public int onStartCommand(final Intent intent, int flags, int startId) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000114 if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONSTARTCOMMAND.");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100115
Sander Alewijnsed7043852014-06-17 15:50:48 +0100116 synchronized (this) { // Make operations on mProvisioningInFlight atomic.
117 if (mProvisioningInFlight) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000118 if (DEBUG) ProvisionLogger.logd("Provisioning already in flight.");
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100119
Sander Alewijnsed7043852014-06-17 15:50:48 +0100120 sendProgressUpdateToActivity();
121
122 // Send error message if currently in error state.
123 if (mLastErrorMessage >= 0) {
124 sendError();
125 }
126
Sander Alewijnse326bcfd2014-06-25 15:24:03 +0100127 // Send success if provisioning was succesful.
Sander Alewijnsed7043852014-06-17 15:50:48 +0100128 if (mDone) {
129 onProvisioningSuccess(mParams.mDeviceAdminPackageName);
130 }
131 } else {
132 mProvisioningInFlight = true;
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000133 if (DEBUG) ProvisionLogger.logd("First start of the service.");
Sander Alewijnsed7043852014-06-17 15:50:48 +0100134 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 Alewijnseab18ea72014-09-11 15:15:23 +0100139 registerHomeIntentReceiver();
140
Sander Alewijnsed7043852014-06-17 15:50:48 +0100141 // 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 Alewijnse28bffd62014-06-05 10:54:26 +0100148 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100149 }
150 return START_NOT_STICKY;
151 }
152
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100153 // 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 Alewijnse04ab6fe2014-04-29 10:53:54 +0100192 /**
193 * This is the core method of this class. It goes through every provisioning step.
Julia Reynoldsccd60162015-02-17 11:53:48 -0500194 * Each task checks if it is required and executes if it is.
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100195 */
Sander Alewijnsee96a8202014-05-19 16:55:39 +0100196 private void startDeviceOwnerProvisioning(final ProvisioningParams params) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000197 if (DEBUG) ProvisionLogger.logd("Starting device owner provisioning");
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100198
199 // Construct Tasks. Do not start them yet.
Julia Reynoldsccd60162015-02-17 11:53:48 -0500200 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 Alewijnse04ab6fe2014-04-29 10:53:54 +0100208
Julia Reynoldsccd60162015-02-17 11:53:48 -0500209 @Override
210 public void onError(){
211 error(R.string.device_owner_error_wifi);
212 }
213 });
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100214
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100215 mDownloadPackageTask = new DownloadPackageTask(this,
Sander Alewijnse56f71572014-06-23 16:21:33 +0100216 params.mDeviceAdminPackageDownloadLocation, params.mDeviceAdminPackageChecksum,
Sander Alewijnsed8dcb1f2014-07-24 15:24:50 +0100217 params.mDeviceAdminPackageDownloadCookieHeader, new DownloadPackageTask.Callback() {
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100218 @Override
219 public void onSuccess() {
220 String downloadLocation =
221 mDownloadPackageTask.getDownloadedPackageLocation();
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100222 progressUpdate(R.string.progress_install);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100223 mInstallPackageTask.run(downloadLocation);
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100224 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100225
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100226 @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 Alewijnse04ab6fe2014-04-29 10:53:54 +0100241
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100242 mInstallPackageTask = new InstallPackageTask(this,
Julia Reynoldsccd60162015-02-17 11:53:48 -0500243 params.mDeviceAdminPackageName, params.mDeviceAdminPackageDownloadLocation,
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100244 new InstallPackageTask.Callback() {
245 @Override
246 public void onSuccess() {
247 progressUpdate(R.string.progress_set_owner);
248 mSetDevicePolicyTask.run();
249 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100250
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100251 @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 Alewijnse04ab6fe2014-04-29 10:53:54 +0100266
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100267 mSetDevicePolicyTask = new SetDevicePolicyTask(this,
Sander Alewijnse56f71572014-06-23 16:21:33 +0100268 params.mDeviceAdminPackageName,
269 getResources().getString(R.string.default_owned_device_username),
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100270 new SetDevicePolicyTask.Callback() {
271 @Override
272 public void onSuccess() {
Julia Reynoldsccd60162015-02-17 11:53:48 -0500273 mDeleteNonRequiredAppsTask.run();
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100274 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100275
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100276 @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 Alewijnsecdf13b12014-06-19 15:58:15 +0100282 case SetDevicePolicyTask.ERROR_NO_RECEIVER:
Sander Alewijnseb6578e72014-07-25 17:12:05 +0100283 error(R.string.device_owner_error_package_invalid);
Sander Alewijnsecdf13b12014-06-19 15:58:15 +0100284 break;
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100285 default:
286 error(R.string.device_owner_error_general);
287 break;
288 }
289 }
290 });
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100291
Nicolas Prevot3ebb7b02014-09-08 15:42:15 +0100292 mDeleteNonRequiredAppsTask = new DeleteNonRequiredAppsTask(
Sander Alewijnsed5e4c422014-11-25 17:56:16 +0000293 this, params.mDeviceAdminPackageName, R.array.required_apps_managed_device,
294 R.array.vendor_required_apps_managed_device, true /* creating new profile */,
Julia Reynoldsccd60162015-02-17 11:53:48 -0500295 UserHandle.USER_OWNER, params.mLeaveAllSystemAppsEnabled,
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100296 new DeleteNonRequiredAppsTask.Callback() {
Sander Alewijnse2818d322014-05-20 14:54:13 +0100297 public void onSuccess() {
298 // Done with provisioning. Success.
Sander Alewijnsecdf13b12014-06-19 15:58:15 +0100299 onProvisioningSuccess(params.mDeviceAdminPackageName);
Sander Alewijnse2818d322014-05-20 14:54:13 +0100300 }
301
Jessica Hummel14eeef92014-06-16 11:06:20 +0100302 @Override
Sander Alewijnse2818d322014-05-20 14:54:13 +0100303 public void onError() {
304 error(R.string.device_owner_error_general);
305 };
Nicolas Prevot3ebb7b02014-09-08 15:42:15 +0100306 });
Sander Alewijnse2818d322014-05-20 14:54:13 +0100307
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100308 // Start first task, which starts next task in its callback, etc.
Julia Reynoldsccd60162015-02-17 11:53:48 -0500309 progressUpdate(R.string.progress_connect_to_wifi);
310 mAddWifiNetworkTask.run();
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100311 }
312
313 private void error(int dialogMessage) {
Sander Alewijnsed7043852014-06-17 15:50:48 +0100314 mLastErrorMessage = dialogMessage;
315 sendError();
316 // Wait for stopService() call from the activity.
317 }
Jessica Hummel14eeef92014-06-16 11:06:20 +0100318
Sander Alewijnsed7043852014-06-17 15:50:48 +0100319 private void sendError() {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000320 if (DEBUG) {
321 ProvisionLogger.logd("Reporting Error: " + getResources()
322 .getString(mLastErrorMessage));
323 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100324 Intent intent = new Intent(ACTION_PROVISIONING_ERROR);
Jessica Hummel14eeef92014-06-16 11:06:20 +0100325 intent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100326 intent.putExtra(EXTRA_USER_VISIBLE_ERROR_ID_KEY, mLastErrorMessage);
Jessica Hummel81fe1042014-06-23 17:10:38 +0100327 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100328 }
329
330 private void progressUpdate(int progressMessage) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000331 if (DEBUG) {
332 ProvisionLogger.logd("Reporting progress update: " + getResources()
333 .getString(progressMessage));
334 }
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100335 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 Hummel14eeef92014-06-16 11:06:20 +0100342 intent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class);
Jessica Hummel81fe1042014-06-23 17:10:38 +0100343 LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100344 }
345
Sander Alewijnsecdf13b12014-06-19 15:58:15 +0100346 private void onProvisioningSuccess(String deviceAdminPackage) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000347 if (DEBUG) ProvisionLogger.logd("Reporting success.");
Sander Alewijnsed7043852014-06-17 15:50:48 +0100348 mDone = true;
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100349
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 Hummel14eeef92014-06-16 11:06:20 +0100358 Intent successIntent = new Intent(ACTION_PROVISIONING_SUCCESS);
359 successIntent.setClass(this, DeviceOwnerProvisioningActivity.ServiceMessageReceiver.class);
Jessica Hummel81fe1042014-06-23 17:10:38 +0100360 LocalBroadcastManager.getInstance(this).sendBroadcast(successIntent);
Sander Alewijnsed7043852014-06-17 15:50:48 +0100361 // Wait for stopService() call from the activity.
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100362 }
363
364 private void initializeProvisioningEnvironment(ProvisioningParams params) {
365 setTimeAndTimezone(params.mTimeZone, params.mLocalTime);
366 setLocale(params.mLocale);
Sander Alewijnse639e94c2014-05-01 16:07:51 +0100367
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 Alewijnse9a40ab02014-10-27 18:36:56 +0000371 if (DEBUG) ProvisionLogger.logd("Starting cdma activation activity");
Sander Alewijnse639e94c2014-05-01 16:07:51 +0100372 startActivity(intent); // Activity will be a Nop if not a CDMA device.
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100373 }
374
Sander Alewijnsed7043852014-06-17 15:50:48 +0100375 private void setTimeAndTimezone(String timeZone, long localTime) {
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100376 try {
377 final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
378 if (timeZone != null) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000379 if (DEBUG) ProvisionLogger.logd("Setting time zone to " + timeZone);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100380 am.setTimeZone(timeZone);
381 }
Sander Alewijnsed7043852014-06-17 15:50:48 +0100382 if (localTime > 0) {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000383 if (DEBUG) ProvisionLogger.logd("Setting time to " + localTime);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100384 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 Alewijnse9a40ab02014-10-27 18:36:56 +0000397 if (DEBUG) ProvisionLogger.logd("Setting locale to " + locale);
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100398 // 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 Alewijnsed7043852014-06-17 15:50:48 +0100408 public void onCreate () {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000409 if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONCREATE.");
Sander Alewijnsed7043852014-06-17 15:50:48 +0100410 }
411
412 @Override
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100413 public void onDestroy () {
Sander Alewijnse9a40ab02014-10-27 18:36:56 +0000414 if (DEBUG) ProvisionLogger.logd("Device owner provisioning service ONDESTROY");
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100415 if (mAddWifiNetworkTask != null) {
Sander Alewijnsed7043852014-06-17 15:50:48 +0100416 mAddWifiNetworkTask.cleanUp();
417 }
418 if (mDownloadPackageTask != null) {
419 mDownloadPackageTask.cleanUp();
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100420 }
Sander Alewijnseab18ea72014-09-11 15:15:23 +0100421 if (mIndirectHomeReceiver != null) {
422 LocalBroadcastManager.getInstance(this).unregisterReceiver(mIndirectHomeReceiver);
423 mIndirectHomeReceiver = null;
424 }
425
Sander Alewijnse28bffd62014-06-05 10:54:26 +0100426 }
427
428 @Override
Sander Alewijnse04ab6fe2014-04-29 10:53:54 +0100429 public IBinder onBind(Intent intent) {
430 return null;
431 }
432}
433