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