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 import android.provider.Settings;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.SwitchPreference;
24 
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.Utils;
27 import com.android.settingslib.core.AbstractPreferenceController;
28 
29 public class AutoTimeZonePreferenceController extends AbstractPreferenceController
30         implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31 
32     private static final String KEY_AUTO_TIME_ZONE = "auto_zone";
33 
34     private final boolean mIsFromSUW;
35     private final UpdateTimeAndDateCallback mCallback;
36 
AutoTimeZonePreferenceController(Context context, UpdateTimeAndDateCallback callback, boolean isFromSUW)37     public AutoTimeZonePreferenceController(Context context, UpdateTimeAndDateCallback callback,
38             boolean isFromSUW) {
39         super(context);
40         mCallback = callback;
41         mIsFromSUW = isFromSUW;
42     }
43 
44     @Override
isAvailable()45     public boolean isAvailable() {
46         return !(Utils.isWifiOnly(mContext) || mIsFromSUW);
47     }
48 
49     @Override
getPreferenceKey()50     public String getPreferenceKey() {
51         return KEY_AUTO_TIME_ZONE;
52     }
53 
54     @Override
updateState(Preference preference)55     public void updateState(Preference preference) {
56         if (!(preference instanceof SwitchPreference)) {
57             return;
58         }
59         ((SwitchPreference) preference).setChecked(isEnabled());
60     }
61 
62     @Override
onPreferenceChange(Preference preference, Object newValue)63     public boolean onPreferenceChange(Preference preference, Object newValue) {
64         boolean autoZoneEnabled = (Boolean) newValue;
65         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME_ZONE,
66                 autoZoneEnabled ? 1 : 0);
67         mCallback.updateTimeAndDateDisplay(mContext);
68         return true;
69     }
70 
isEnabled()71     public boolean isEnabled() {
72         return isAvailable() && Settings.Global.getInt(mContext.getContentResolver(),
73                 Settings.Global.AUTO_TIME_ZONE, 0) > 0;
74     }
75 }
76