Home | History | Annotate | Download | only in display
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
      5  * except in compliance with the License. You may obtain a copy of the License at
      6  *
      7  *      http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the
     10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     11  * KIND, either express or implied. See the License for the specific language governing
     12  * permissions and limitations under the License.
     13  */
     14 package com.android.settings.display;
     15 
     16 import android.content.Context;
     17 import android.provider.Settings;
     18 import android.support.v14.preference.SwitchPreference;
     19 import android.support.v7.preference.Preference;
     20 
     21 import android.util.ArrayMap;
     22 import com.android.settings.core.PreferenceController;
     23 import com.android.settings.search2.InlineSwitchPayload;
     24 import com.android.settings.search2.ResultPayload;
     25 
     26 import java.util.Map;
     27 
     28 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
     29 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
     30 import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
     31 
     32 
     33 public class AutoBrightnessPreferenceController extends PreferenceController implements
     34         Preference.OnPreferenceChangeListener {
     35 
     36     private final String mAutoBrightnessKey;
     37 
     38     public AutoBrightnessPreferenceController(Context context, String key) {
     39         super(context);
     40         mAutoBrightnessKey = key;
     41     }
     42 
     43     @Override
     44     public boolean isAvailable() {
     45         return mContext.getResources().getBoolean(
     46                 com.android.internal.R.bool.config_automatic_brightness_available);
     47     }
     48 
     49     @Override
     50     public String getPreferenceKey() {
     51         return mAutoBrightnessKey;
     52     }
     53 
     54     @Override
     55     public void updateState(Preference preference) {
     56         int brightnessMode = Settings.System.getInt(mContext.getContentResolver(),
     57                 SCREEN_BRIGHTNESS_MODE, SCREEN_BRIGHTNESS_MODE_MANUAL);
     58         ((SwitchPreference) preference).setChecked(brightnessMode != SCREEN_BRIGHTNESS_MODE_MANUAL);
     59     }
     60 
     61     @Override
     62     public boolean onPreferenceChange(Preference preference, Object newValue) {
     63         boolean auto = (Boolean) newValue;
     64         Settings.System.putInt(mContext.getContentResolver(), SCREEN_BRIGHTNESS_MODE,
     65                 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : SCREEN_BRIGHTNESS_MODE_MANUAL);
     66         return true;
     67     }
     68 
     69     @Override
     70     public ResultPayload getResultPayload() {
     71         final Map<Integer, Boolean> valueMap = new ArrayMap<>();
     72         valueMap.put(SCREEN_BRIGHTNESS_MODE_AUTOMATIC, true);
     73         valueMap.put(SCREEN_BRIGHTNESS_MODE_MANUAL, false);
     74 
     75         return new InlineSwitchPayload(SCREEN_BRIGHTNESS_MODE,
     76                 ResultPayload.SettingsSource.SYSTEM, valueMap);
     77     }
     78 }
     79