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 */ 14package com.android.settings.display; 15 16import android.content.Context; 17import android.content.Intent; 18import android.provider.Settings; 19import android.support.v14.preference.SwitchPreference; 20import android.support.v7.preference.Preference; 21 22import com.android.settings.DisplaySettings; 23import com.android.settings.core.PreferenceControllerMixin; 24import com.android.settings.search.DatabaseIndexingUtils; 25import com.android.settings.search.InlineSwitchPayload; 26import com.android.settings.search.ResultPayload; 27import com.android.settings.R; 28import com.android.settingslib.core.AbstractPreferenceController; 29 30import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 31import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC; 32import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL; 33 34 35public class AutoBrightnessPreferenceController extends AbstractPreferenceController implements 36 PreferenceControllerMixin, Preference.OnPreferenceChangeListener { 37 38 private final String mAutoBrightnessKey; 39 40 private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE; 41 private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL; 42 43 public AutoBrightnessPreferenceController(Context context, String key) { 44 super(context); 45 mAutoBrightnessKey = key; 46 } 47 48 @Override 49 public boolean isAvailable() { 50 return mContext.getResources().getBoolean( 51 com.android.internal.R.bool.config_automatic_brightness_available); 52 } 53 54 @Override 55 public String getPreferenceKey() { 56 return mAutoBrightnessKey; 57 } 58 59 @Override 60 public void updateState(Preference preference) { 61 int brightnessMode = Settings.System.getInt(mContext.getContentResolver(), 62 SYSTEM_KEY, DEFAULT_VALUE); 63 ((SwitchPreference) preference).setChecked(brightnessMode != DEFAULT_VALUE); 64 } 65 66 @Override 67 public boolean onPreferenceChange(Preference preference, Object newValue) { 68 boolean auto = (Boolean) newValue; 69 Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY, 70 auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE); 71 return true; 72 } 73 74 @Override 75 public ResultPayload getResultPayload() { 76 final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext, 77 DisplaySettings.class.getName(), mAutoBrightnessKey, 78 mContext.getString(R.string.display_settings)); 79 80 return new InlineSwitchPayload(SYSTEM_KEY, 81 ResultPayload.SettingsSource.SYSTEM, SCREEN_BRIGHTNESS_MODE_AUTOMATIC, intent, 82 isAvailable(), DEFAULT_VALUE); 83 } 84}