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