下拉状态栏有个亮度的进度条,如果开启了亮度自动调节开关,会随着周围光线变化,这个进度条也会随着变化,接下来就是看看这个功能是如何实现的。
源码版本
基于 Android 9.0 分析。
BrightnessDialog,位于:frameworks/base/packages/SystemUI/src/com/android/systemui/settings/BrightnessDialog.java
ToggleSliderView,位于:frameworks/base/packages/SystemUI/src/com/android/systemui/settings/ToggleSliderView.java
DisplayPowerController,位于:frameworks/base/services/core/java/com/android/server/display/DisplayPowerController.java
AutomaticBrightnessController,位于:frameworks/base/services/core/java/com/android/server/display/AutomaticBrightnessController.java
BrightnessMappingStrategy,
概述
状态栏里亮度页面是 BrightnessDialog,其中进度条设置是 ToggleSliderView,亮度自动调节主要是 DisplayPowerController 和 AutomaticBrightnessController 两个类,当亮度发生变化时,如果关联到 ToggleSliderView,用的是 ContentObserver,Uri 为 Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ。
源码梳理
1、BrightnessDialog#onCreate:
@Override
2、这里进行了 BrightnessController 初始化,来看下:
publicBrightnessController(Contextcontext,ImageViewicon,ToggleSlidercontrol){
又进行了 BrightnessObserver 初始化:
/**ContentObservertowatchbrightness**/
其实我目前下载的源码,这块功能是不全的,我已经加上了,哪里进行 BrightnessObserver 的 ContentObserver 注册呢?
3、回到 BrightnessDialog#onStart:
@Override
4、调用mBrightnessController.registerCallbacks();
最终走到 mStartListeningRunnable:
private
当亮度有变化时,会走 BrightnessObserver#onChange,最终走到:
private
走 updateSlider方法,到 :
privatevoidanimateSliderTo(inttarget){
5、跳到 ToggleSliderView#setValue:
@Override
接下来就是看看亮度自动调节主要的两个类 DisplayPowerController 和 AutomaticBrightnessController。DisplayPowerController 属于 Display 模块,其控制设备屏幕亮灭、背光、与Power关系密切,这里主要看下屏幕亮度的控制这方面的逻辑。
6、首先,在 DisplayManagerService 中初始化 DisplayPowerController,如下:
private
7、接着看下 DisplayPowerController 构造方法,如下:
DisplayPowerCallbackscallbacks,Handlerhandler, SensorManagersensorManager,DisplayBlankerblanker){publicDisplayPowerController(Contextcontext,
由于亮屏之后屏幕自动亮度才会生效,所以在亮屏的时候,流程会走到 DisplayPowerController 中的核心函数 updatePowerState():
privatevoidupdatePowerState(){
接下来分别看看 autoBrightnessAdjustment 和 newAutoBrightnessAdjustment 怎么来的?
autoBrightnessAdjustment 是来自 mTemporaryAutoBrightnessAdjustment 或 mAutoBrightnessAdjustment 赋值,mAutoBrightnessAdjustment 在第 7 步mAutoBrightnessAdjustment = getAutoBrightnessAdjustmentSetting();
有初始化,看下 getAutoBrightnessAdjustmentSetting():
privatefloatgetAutoBrightnessAdjustmentSetting(){
继续看下 clampAutoBrightnessAdjustment:
privatestaticfloatclampAutoBrightnessAdjustment(floatvalue){
这里注意下 MathUtils.constrain() 表示百分比缩放函数,比如 MathUtils.constrain(0.5, 0, 255) 表示 (255-0)*0.5。
这样了解了 autoBrightnessAdjustment,接下来看 newAutoBrightnessAdjustment。
8、回到 DisplayPowerController#updatePowerState(),看到 newAutoBrightnessAdjustment 调用了AutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment()
,最终是到了BrightnessMapper#getAutoBrightnessAdjustment()
其中 mAutoBrightnessAdjustment 变量,赋值是在BrightnessMapper#setAutoBrightnessAdjustment
:
@Override
9、BrightnessMapper#setAutoBrightnessAdjustment 这个方法调用又回到了 AutomaticBrightnessController#setAutoBrightnessAdjustment:
privatebooleansetAutoBrightnessAdjustment(floatadjustment){
AutomaticBrightnessController#setAutoBrightnessAdjustment
调用是来到AutomaticBrightnessController#configure()
方法:
publicvoidconfigure(booleanenable,@NullableBrightnessConfigurationconfiguration,floatbrightness,booleanuserChangedBrightness,floatadjustment,booleanuserChangedAutoBrightnessAdjustment,intdisplayPolicy){
AutomaticBrightnessController#configure()
调用来到了DisplayPowerController #updatePowerState()
。
这样也知道了 newAutoBrightnessAdjustment,继续 putAutoBrightnessAdjustmentSetting:
privatevoidputAutoBrightnessAdjustmentSetting(floatadjustment){
就调到第 4 步 BrightnessObserver#onChange,进度条随之变化,Over!
拓展阅读
AOSP 源码下载
AOSP 源码整编单编
Android Studio 导入 AOSP 源码