屏幕亮度Brightness
源码部分
设置-显示-亮度百分比
packages\apps\Settings\src\com\android\settings\display\BrightnessLevelPreferenceController.java
private void updatedSummary(Preference preference) {
if (preference != null) {
preference.setSummary(NumberFormat.getPercentInstance().format(getCurrentBrightness()));
}
}
private double getCurrentBrightness() {
final int value;
if (isInVrMode()) {
value = convertLinearToGamma(System.getInt(mContentResolver,
System.SCREEN_BRIGHTNESS_FOR_VR, mMaxBrightness),
mMinVrBrightness, mMaxVrBrightness);
} else {
value = convertLinearToGamma(Settings.System.getInt(mContentResolver,
System.SCREEN_BRIGHTNESS, mMinBrightness),
mMinBrightness, mMaxBrightness);
}
return getPercentage(value, 0, GAMMA_SPACE_MAX);
}
private double getPercentage(double value, int min, int max) {
if (value > max) {
return 1.0;
}
if (value < min) {
return 0.0;
}
return (value - min) / (max - min);
}
convertLinearToGamma()非线性计算转化
frameworks\base\packages\SettingsLib\src\com\android\settingslib\display\BrightnessUtils.java
public static final int convertLinearToGamma(int val, int min, int max) {
// For some reason, HLG normalizes to the range [0, 12] rather than [0, 1]
final float normalizedVal = MathUtils.norm(min, max, val) * 12;
final float ret;
if (normalizedVal <= 1f) {
ret = MathUtils.sqrt(normalizedVal) * R;
} else {
ret = A * MathUtils.log(normalizedVal - B) + C;
}
return Math.round(MathUtils.lerp(0, GAMMA_SPACE_MAX, ret));
}
BrightnessDialog及其控制
# BrightnessDialog
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessDialog.java
final ImageView icon = findViewById(R.id.brightness_icon);
final ToggleSliderView slider = findViewById(R.id.brightness_slider);
mBrightnessController = new BrightnessController(this, icon, slider);
# BrightnessDialog控制
frameworks\base\packages\SystemUI\src\com\android\systemui\settings\BrightnessController.java
@Override
public void onChanged(ToggleSlider toggleSlider, boolean tracking, boolean automatic,
int value, boolean stopTracking) {
updateIcon(mAutomatic);
if (mExternalChange) return;
if (mSliderAnimator != null) {
mSliderAnimator.cancel();
}
final int min;
final int max;
final int metric;
final String setting;
if (mIsVrModeEnabled) {
metric = MetricsEvent.ACTION_BRIGHTNESS_FOR_VR;
min = mMinimumBacklightForVr;
max = mMaximumBacklightForVr;
setting = Settings.System.SCREEN_BRIGHTNESS_FOR_VR;
} else {
metric = mAutomatic
? MetricsEvent.ACTION_BRIGHTNESS_AUTO
: MetricsEvent.ACTION_BRIGHTNESS;
min = mMinimumBacklight;
max = mMaximumBacklight;
setting = Settings.System.SCREEN_BRIGHTNESS;
}
final int val = convertGammaToLinear(value, min, max);
if (stopTracking) {
MetricsLogger.action(mContext, metric, val);
}
setBrightness(val);
if (!tracking) {
AsyncTask.execute(new Runnable() {
public void run() {
Settings.System.putIntForUser(mContext.getContentResolver(),
setting, val, UserHandle.USER_CURRENT);
}
});
}
for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
cb.onBrightnessLevelChanged();
}
}
private void updateSlider(int val, boolean inVrMode) {
final int min;
final int max;
if (inVrMode) {
min = mMinimumBacklightForVr;
max = mMaximumBacklightForVr;
} else {
min = mMinimumBacklight;
max = mMaximumBacklight;
}
if (val == convertGammaToLinear(mControl.getValue(), min, max)) {
// If we have more resolution on the slider than we do in the actual setting, then
// multiple slider positions will map to the same setting value. Thus, if we see a
// setting value here that maps to the current slider position, we don't bother to
// calculate the new slider position since it may differ and look like a brightness
// change to the user even though it isn't one.
return;
}
final int sliderVal = convertLinearToGamma(val, min, max);
animateSliderTo(sliderVal);
}
亮度参数配置
默认亮度、自动亮度开关
frameworks\base\packages\SettingsProvider\res\values\defaults.xml
<!-- Default screen brightness, from 0 to 255. 102 is 40%. -->
<integer name="def_screen_brightness">102</integer>
<bool name="def_screen_brightness_automatic_mode">false</bool>
最大亮度、最小亮度
frameworks\base\core\res\res\values\config.xml
<!-- Minimum screen brightness setting allowed by the power manager.
The user is forbidden from setting the brightness below this level. -->
<integer name="config_screenBrightnessSettingMinimum">10</integer>
<!-- Maximum screen brightness allowed by the power manager.
The user is forbidden from setting the brightness above this level. -->
<integer name="config_screenBrightnessSettingMaximum">255</integer>
<!-- Default screen brightness setting.
Must be in the range specified by minimum and maximum. -->
<integer name="config_screenBrightnessSettingDefault">102</integer>
adb命令控制亮度
查看亮度值
adb shell
$ cd /sys/class/leds/lcd-backlight
$ ls
$ brightness device max_brightness power subsystem trigger uevent
$ cat brightness
# 获取亮度是否为自动获取
C:\Users\Administrator>adb shell settings get system screen_brightness_mode
1
# 获取当前亮度值
C:\Users\Administrator>adb shell settings get system screen_brightness
30
# 更改亮度值(亮度值在0—255之间整数)
C:\Users\Administrator>adb shell settings put system screen_brightness 150