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