1 /* 2 * Copyright (C) 2018 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.car.settings.display; 18 19 import static com.android.settingslib.display.BrightnessUtils.GAMMA_SPACE_MAX; 20 import static com.android.settingslib.display.BrightnessUtils.convertGammaToLinear; 21 import static com.android.settingslib.display.BrightnessUtils.convertLinearToGamma; 22 23 import android.car.drivingstate.CarUxRestrictions; 24 import android.content.Context; 25 import android.os.PowerManager; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.Logger; 31 import com.android.car.settings.common.PreferenceController; 32 import com.android.car.settings.common.SeekBarPreference; 33 34 /** Business logic for changing the brightness of the display. */ 35 public class BrightnessLevelPreferenceController extends PreferenceController<SeekBarPreference> { 36 37 private static final Logger LOG = new Logger(BrightnessLevelPreferenceController.class); 38 private final int mMaximumBacklight; 39 private final int mMinimumBacklight; 40 BrightnessLevelPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41 public BrightnessLevelPreferenceController(Context context, String preferenceKey, 42 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 43 super(context, preferenceKey, fragmentController, uxRestrictions); 44 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 45 mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting(); 46 mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting(); 47 } 48 49 @Override getPreferenceType()50 protected Class<SeekBarPreference> getPreferenceType() { 51 return SeekBarPreference.class; 52 } 53 54 @Override updateState(SeekBarPreference preference)55 protected void updateState(SeekBarPreference preference) { 56 preference.setMax(GAMMA_SPACE_MAX); 57 preference.setValue(getSeekbarValue()); 58 preference.setContinuousUpdate(true); 59 } 60 61 @Override handlePreferenceChanged(SeekBarPreference preference, Object newValue)62 protected boolean handlePreferenceChanged(SeekBarPreference preference, Object newValue) { 63 int gamma = (Integer) newValue; 64 int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight); 65 Settings.System.putIntForUser(getContext().getContentResolver(), 66 Settings.System.SCREEN_BRIGHTNESS, linear, UserHandle.myUserId()); 67 return true; 68 } 69 getSeekbarValue()70 private int getSeekbarValue() { 71 int gamma = GAMMA_SPACE_MAX; 72 try { 73 int linear = Settings.System.getIntForUser(getContext().getContentResolver(), 74 Settings.System.SCREEN_BRIGHTNESS, UserHandle.myUserId()); 75 gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight); 76 } catch (Settings.SettingNotFoundException e) { 77 LOG.w("Can't find setting for SCREEN_BRIGHTNESS."); 78 } 79 return gamma; 80 } 81 } 82