2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.datetime;
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.support.v14.preference.SwitchPreference;
22 import android.support.v7.preference.Preference;
24 import com.android.settings.Utils;
25 import com.android.settings.core.PreferenceControllerMixin;
26 import com.android.settingslib.core.AbstractPreferenceController;
28 public class AutoTimeZonePreferenceController extends AbstractPreferenceController
29 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
31 private static final String KEY_AUTO_TIME_ZONE = "auto_zone";
33 private final boolean mIsFromSUW;
34 private final UpdateTimeAndDateCallback mCallback;
36 public AutoTimeZonePreferenceController(Context context, UpdateTimeAndDateCallback callback,
40 mIsFromSUW = isFromSUW;
44 public boolean isAvailable() {
45 return !(Utils.isWifiOnly(mContext) || mIsFromSUW);
49 public String getPreferenceKey() {
50 return KEY_AUTO_TIME_ZONE;
54 public void updateState(Preference preference) {
55 if (!(preference instanceof SwitchPreference)) {
58 ((SwitchPreference) preference).setChecked(isEnabled());
62 public boolean onPreferenceChange(Preference preference, Object newValue) {
63 boolean autoZoneEnabled = (Boolean) newValue;
64 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME_ZONE,
65 autoZoneEnabled ? 1 : 0);
66 mCallback.updateTimeAndDateDisplay(mContext);
70 public boolean isEnabled() {
71 return isAvailable() && Settings.Global.getInt(mContext.getContentResolver(),
72 Settings.Global.AUTO_TIME_ZONE, 0) > 0;