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 24 import com.android.settings.core.PreferenceControllerMixin; 25 import com.android.settingslib.RestrictedLockUtils; 26 import com.android.settingslib.RestrictedLockUtilsInternal; 27 import com.android.settingslib.RestrictedSwitchPreference; 28 import com.android.settingslib.core.AbstractPreferenceController; 29 30 public class AutoTimePreferenceController extends AbstractPreferenceController 31 implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 32 33 private static final String KEY_AUTO_TIME = "auto_time"; 34 private final UpdateTimeAndDateCallback mCallback; 35 AutoTimePreferenceController(Context context, UpdateTimeAndDateCallback callback)36 public AutoTimePreferenceController(Context context, UpdateTimeAndDateCallback callback) { 37 super(context); 38 mCallback = callback; 39 } 40 41 @Override isAvailable()42 public boolean isAvailable() { 43 return true; 44 } 45 46 @Override updateState(Preference preference)47 public void updateState(Preference preference) { 48 if (!(preference instanceof RestrictedSwitchPreference)) { 49 return; 50 } 51 if (!((RestrictedSwitchPreference) preference).isDisabledByAdmin()) { 52 ((RestrictedSwitchPreference) preference).setDisabledByAdmin( 53 getEnforcedAdminProperty()); 54 } 55 ((RestrictedSwitchPreference) preference).setChecked(isEnabled()); 56 } 57 58 @Override getPreferenceKey()59 public String getPreferenceKey() { 60 return KEY_AUTO_TIME; 61 } 62 63 @Override onPreferenceChange(Preference preference, Object newValue)64 public boolean onPreferenceChange(Preference preference, Object newValue) { 65 boolean autoEnabled = (Boolean) newValue; 66 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AUTO_TIME, 67 autoEnabled ? 1 : 0); 68 mCallback.updateTimeAndDateDisplay(mContext); 69 return true; 70 } 71 isEnabled()72 public boolean isEnabled() { 73 return Settings.Global.getInt(mContext.getContentResolver(), 74 Settings.Global.AUTO_TIME, 0) > 0; 75 } 76 getEnforcedAdminProperty()77 private RestrictedLockUtils.EnforcedAdmin getEnforcedAdminProperty() { 78 return RestrictedLockUtilsInternal.checkIfAutoTimeRequired(mContext); 79 } 80 } 81