1 /*
2  * Copyright (C) 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.settings.datetime;
18 
19 import android.content.Context;
20 
21 import androidx.annotation.VisibleForTesting;
22 import androidx.preference.Preference;
23 
24 import com.android.settings.core.PreferenceControllerMixin;
25 import com.android.settingslib.RestrictedPreference;
26 import com.android.settingslib.core.AbstractPreferenceController;
27 import com.android.settingslib.datetime.ZoneGetter;
28 
29 import java.util.Calendar;
30 
31 public class TimeZonePreferenceController extends AbstractPreferenceController
32         implements PreferenceControllerMixin {
33 
34     private static final String KEY_TIMEZONE = "timezone";
35 
36     private final AutoTimeZonePreferenceController mAutoTimeZonePreferenceController;
37 
TimeZonePreferenceController(Context context, AutoTimeZonePreferenceController autoTimeZonePreferenceController)38     public TimeZonePreferenceController(Context context,
39             AutoTimeZonePreferenceController autoTimeZonePreferenceController) {
40         super(context);
41         mAutoTimeZonePreferenceController = autoTimeZonePreferenceController;
42     }
43 
44     @Override
updateState(Preference preference)45     public void updateState(Preference preference) {
46         if (!(preference instanceof RestrictedPreference)) {
47             return;
48         }
49         preference.setSummary(getTimeZoneOffsetAndName());
50         if( !((RestrictedPreference) preference).isDisabledByAdmin()) {
51             preference.setEnabled(!mAutoTimeZonePreferenceController.isEnabled());
52         }
53     }
54 
55     @Override
isAvailable()56     public boolean isAvailable() {
57         return true;
58     }
59 
60     @Override
getPreferenceKey()61     public String getPreferenceKey() {
62         return KEY_TIMEZONE;
63     }
64 
65     @VisibleForTesting
getTimeZoneOffsetAndName()66     CharSequence getTimeZoneOffsetAndName() {
67         final Calendar now = Calendar.getInstance();
68         return ZoneGetter.getTimeZoneOffsetAndName(mContext,
69                 now.getTimeZone(), now.getTime());
70     }
71 }
72