关键代码在frameworks/base/services/core/java/com/android/server/display
BrightnessMappingStrategy.java
AutomaticBrightnessController.java
DisplayPowerController.java
这三个
AutomaticBrightnessController.java
private void updateAutoBrightness(boolean sendUpdate, boolean isManuallySet) {if (!mAmbientLuxValid) {return;}float value = mBrightnessMapper.getBrightness(mAmbientLux, mForegroundAppPackageName,mForegroundAppCategory);float newScreenAutoBrightness = clampScreenBrightness(value);// If screenAutoBrightness is set, we should have screen{Brightening,Darkening}Threshold,// in which case we ignore the new screen brightness if it doesn't differ enough from the// previous one.if (!Float.isNaN(mScreenAutoBrightness)&& !isManuallySet&& newScreenAutoBrightness > mScreenDarkeningThreshold&& newScreenAutoBrightness < mScreenBrighteningThreshold) {if (mLoggingEnabled) {Slog.d(TAG, "ignoring newScreenAutoBrightness: "+ mScreenDarkeningThreshold + " < " + newScreenAutoBrightness+ " < " + mScreenBrighteningThreshold);}Slog.d("aaron", "ignoring newScreenAutoBrightness: "+ mScreenDarkeningThreshold + " < " + newScreenAutoBrightness+ " < " + mScreenBrighteningThreshold);return;}if (!BrightnessSynchronizer.floatEquals(mScreenAutoBrightness,newScreenAutoBrightness)) {if (mLoggingEnabled) {Slog.d(TAG, "updateAutoBrightness: "+ "mScreenAutoBrightness=" + mScreenAutoBrightness + ", "+ "newScreenAutoBrightness=" + newScreenAutoBrightness);}mScreenAutoBrightness = newScreenAutoBrightness;mScreenBrighteningThreshold = clampScreenBrightness(mScreenBrightnessThresholds.getBrighteningThreshold(newScreenAutoBrightness));mScreenDarkeningThreshold = clampScreenBrightness(mScreenBrightnessThresholds.getDarkeningThreshold(newScreenAutoBrightness));if (sendUpdate) {Log.i("aaron","AutomaticBrightnessController --- updateAutoBrightness() 772 : updateBrightness()");mCallbacks.updateBrightness();}}}
一般走这。然后反过来看哪里调用它。
private final SensorEventListener mLightSensorListener = new SensorEventListener() {@Overridepublic void onSensorChanged(SensorEvent event) {if (mLightSensorEnabled) {final long time = SystemClock.uptimeMillis();final float lux = event.values[0];handleLightSensorEvent(time, lux);}}@Overridepublic void onAccuracyChanged(Sensor sensor, int accuracy) {// Not used.}};
这边还会监听light sensor,最后也会调用
updateAutoBrightness
mCallbacks.updateBrightness();
这个回调会调用到
DisplayPowerController的
@Overridepublic void updateBrightness() {sendUpdatePowerState();}
最后走到
private void updatePowerState() {//省略代码}
而 BrightnessMappingStrategy是会用到一些配置 环境亮度和 屏幕亮度的 对照表类似。
@Nullablepublic static BrightnessMappingStrategy create(Resources resources) {float[] luxLevels = getLuxLevels(resources.getIntArray(com.android.internal.R.array.config_autoBrightnessLevels));int[] brightnessLevelsBacklight = resources.getIntArray(com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);float[] brightnessLevelsNits = getFloatArray(resources.obtainTypedArray(com.android.internal.R.array.config_autoBrightnessDisplayValuesNits));float autoBrightnessAdjustmentMaxGamma = resources.getFraction(com.android.internal.R.fraction.config_autoBrightnessAdjustmentMaxGamma,1, 1);float[] nitsRange = getFloatArray(resources.obtainTypedArray(com.android.internal.R.array.config_screenBrightnessNits));int[] backlightRange = resources.getIntArray(com.android.internal.R.array.config_screenBrightnessBacklight);long shortTermModelTimeout = resources.getInteger(com.android.internal.R.integer.config_autoBrightnessShortTermModelTimeout);if (isValidMapping(nitsRange, backlightRange)&& isValidMapping(luxLevels, brightnessLevelsNits)) {int minimumBacklight = resources.getInteger(com.android.internal.R.integer.config_screenBrightnessSettingMinimum);int maximumBacklight = resources.getInteger(com.android.internal.R.integer.config_screenBrightnessSettingMaximum);if (backlightRange[0] > minimumBacklight|| backlightRange[backlightRange.length - 1] < maximumBacklight) {Slog.w(TAG, "Screen brightness mapping does not cover whole range of available " +"backlight values, autobrightness functionality may be impaired.");}BrightnessConfiguration.Builder builder = new BrightnessConfiguration.Builder(luxLevels, brightnessLevelsNits);builder.setShortTermModelTimeoutMillis(shortTermModelTimeout);builder.setShortTermModelLowerLuxMultiplier(SHORT_TERM_MODEL_THRESHOLD_RATIO);builder.setShortTermModelUpperLuxMultiplier(SHORT_TERM_MODEL_THRESHOLD_RATIO);return new PhysicalMappingStrategy(builder.build(), nitsRange, backlightRange,autoBrightnessAdjustmentMaxGamma);} else if (isValidMapping(luxLevels, brightnessLevelsBacklight)) {return new SimpleMappingStrategy(luxLevels, brightnessLevelsBacklight,autoBrightnessAdjustmentMaxGamma, shortTermModelTimeout);} else {return null;}}
这边会根据配置信息 new不同的对象。配置信息在 frameworks/base/res/values/config.xml中。