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.car.userlib.CarUserManagerHelper; 25 import android.content.Context; 26 import android.os.PowerManager; 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 CarUserManagerHelper mCarUserManagerHelper; 39 private final int mMaximumBacklight; 40 private final int mMinimumBacklight; 41 BrightnessLevelPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)42 public BrightnessLevelPreferenceController(Context context, String preferenceKey, 43 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 44 super(context, preferenceKey, fragmentController, uxRestrictions); 45 mCarUserManagerHelper = new CarUserManagerHelper(context); 46 PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 47 mMaximumBacklight = powerManager.getMaximumScreenBrightnessSetting(); 48 mMinimumBacklight = powerManager.getMinimumScreenBrightnessSetting(); 49 } 50 51 @Override getPreferenceType()52 protected Class<SeekBarPreference> getPreferenceType() { 53 return SeekBarPreference.class; 54 } 55 56 @Override updateState(SeekBarPreference preference)57 protected void updateState(SeekBarPreference preference) { 58 preference.setMax(GAMMA_SPACE_MAX); 59 preference.setValue(getSeekbarValue()); 60 preference.setContinuousUpdate(true); 61 } 62 63 @Override handlePreferenceChanged(SeekBarPreference preference, Object newValue)64 protected boolean handlePreferenceChanged(SeekBarPreference preference, Object newValue) { 65 int gamma = (Integer) newValue; 66 int linear = convertGammaToLinear(gamma, mMinimumBacklight, mMaximumBacklight); 67 Settings.System.putIntForUser(getContext().getContentResolver(), 68 Settings.System.SCREEN_BRIGHTNESS, linear, 69 mCarUserManagerHelper.getCurrentProcessUserId()); 70 return true; 71 } 72 getSeekbarValue()73 private int getSeekbarValue() { 74 int gamma = GAMMA_SPACE_MAX; 75 try { 76 int linear = Settings.System.getIntForUser(getContext().getContentResolver(), 77 Settings.System.SCREEN_BRIGHTNESS, 78 mCarUserManagerHelper.getCurrentProcessUserId()); 79 gamma = convertLinearToGamma(linear, mMinimumBacklight, mMaximumBacklight); 80 } catch (Settings.SettingNotFoundException e) { 81 LOG.w("Can't find setting for SCREEN_BRIGHTNESS."); 82 } 83 return gamma; 84 } 85 } 86