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