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