1 /*
2  * Copyright 2016, 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.task;
18 
19 import android.app.AlarmManager;
20 import android.content.Context;
21 
22 import com.android.internal.app.LocalePicker;
23 import com.android.managedprovisioning.common.ProvisionLogger;
24 import com.android.managedprovisioning.model.ProvisioningParams;
25 
26 import java.util.Locale;
27 
28 /**
29  * Initialization of locale and timezone.
30  */
31 public class DeviceOwnerInitializeProvisioningTask extends AbstractProvisioningTask {
32 
DeviceOwnerInitializeProvisioningTask(Context context, ProvisioningParams params, Callback callback)33     public DeviceOwnerInitializeProvisioningTask(Context context, ProvisioningParams params,
34             Callback callback) {
35         super(context, params, callback);
36     }
37 
38     @Override
run(int userId)39     public void run(int userId) {
40         setTimeAndTimezone(mProvisioningParams.timeZone, mProvisioningParams.localTime);
41         setLocale(mProvisioningParams.locale);
42 
43         success();
44     }
45 
setTimeAndTimezone(String timeZone, long localTime)46     private void setTimeAndTimezone(String timeZone, long localTime) {
47         try {
48             final AlarmManager alarmManager = mContext.getSystemService(AlarmManager.class);
49             if (timeZone != null) {
50                 alarmManager.setTimeZone(timeZone);
51             }
52             if (localTime > 0) {
53                 alarmManager.setTime(localTime);
54             }
55         } catch (Exception e) {
56             ProvisionLogger.loge("Alarm manager failed to set the system time/timezone.", e);
57             // Do not stop provisioning process, but ignore this error.
58         }
59     }
60 
setLocale(Locale locale)61     private void setLocale(Locale locale) {
62         if (locale == null || locale.equals(Locale.getDefault())) {
63             return;
64         }
65         try {
66             // If locale is different from current locale this results in a configuration change,
67             // which will trigger the restarting of the activity.
68             LocalePicker.updateLocale(locale);
69         } catch (Exception e) {
70             ProvisionLogger.loge("Failed to set the system locale.", e);
71             // Do not stop provisioning process, but ignore this error.
72         }
73     }
74 }
75