当前位置:   article > 正文

android自动亮度流程,Android 如何实现亮度自动调节

android studio如何自动调节亮度

下拉状态栏有个亮度的进度条,如果开启了亮度自动调节开关,会随着周围光线变化,这个进度条也会随着变化,接下来就是看看这个功能是如何实现的。

源码版本

基于 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

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//省略部分代码

mBrightnessController = new BrightnessController(this, icon, slider);

}

2、这里进行了 BrightnessController 初始化,来看下:

public BrightnessController(Context context, ImageView icon, ToggleSlider control) {

//省略部分代码

mBrightnessObserver = new BrightnessObserver(mHandler);

//省略部分代码

}

又进行了 BrightnessObserver 初始化:

/** ContentObserver to watch brightness **/

private class BrightnessObserver extends ContentObserver {

//省略部分代码

private final Uri BRIGHTNESS_FOR_VR_URI =

Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_FOR_VR);

//Add By WuXiaolong for AutomaticBrightness

private final Uri BRIGHTNESS_ADJ_URI =

Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ);

public BrightnessObserver(Handler handler) {

super(handler);

}

@Override

public void onChange(boolean selfChange) {

onChange(selfChange, null);

}

@Override

public void onChange(boolean selfChange, Uri uri) {

if (selfChange) return;

if (BRIGHTNESS_MODE_URI.equals(uri)) {

mBackgroundHandler.post(mUpdateModeRunnable);

mBackgroundHandler.post(mUpdateSliderRunnable);

}

//省略部分代码

//Add By WuXiaolong for AutomaticBrightness

else if (BRIGHTNESS_ADJ_URI.equals(uri) && mAutomatic) {

mBackgroundHandler.post(mUpdateSliderRunnable);

} else {

mBackgroundHandler.post(mUpdateModeRunnable);

mBackgroundHandler.post(mUpdateSliderRunnable);

}

for (BrightnessStateChangeCallback cb : mChangeCallbacks) {

cb.onBrightnessLevelChanged();

}

}

public void startObserving() {

final ContentResolver cr =

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/642979
推荐阅读
相关标签
  

闽ICP备14008679号