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