@@ -89,6 +89,9 @@ final class DisplayPowerController {
8989 // auto-brightness adjustment setting.
9090 private static final float SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA = 3.0f ;
9191
92+ // The minimum reduction in brightness when dimmed.
93+ private static final int SCREEN_DIM_MINIMUM_REDUCTION = 10 ;
94+
9295 // If true, enables the use of the current time as an auto-brightness adjustment.
9396 // The basic idea here is to expand the dynamic range of auto-brightness
9497 // when it is especially dark outside. The light sensor tends to perform
@@ -185,6 +188,12 @@ final class DisplayPowerController {
185188 // The dim screen brightness.
186189 private final int mScreenBrightnessDimConfig ;
187190
191+ // The minimum allowed brightness.
192+ private final int mScreenBrightnessRangeMinimum ;
193+
194+ // The maximum allowed brightness.
195+ private final int mScreenBrightnessRangeMaximum ;
196+
188197 // True if auto-brightness should be used.
189198 private boolean mUseSoftwareAutoBrightnessConfig ;
190199
@@ -343,8 +352,14 @@ public DisplayPowerController(Looper looper, Context context, Notifier notifier,
343352 mDisplayManager = (DisplayManager )context .getSystemService (Context .DISPLAY_SERVICE );
344353
345354 final Resources resources = context .getResources ();
346- mScreenBrightnessDimConfig = resources .getInteger (
347- com .android .internal .R .integer .config_screenBrightnessDim );
355+
356+ mScreenBrightnessDimConfig = clampAbsoluteBrightness (resources .getInteger (
357+ com .android .internal .R .integer .config_screenBrightnessDim ));
358+
359+ int screenBrightnessMinimum = Math .min (resources .getInteger (
360+ com .android .internal .R .integer .config_screenBrightnessSettingMinimum ),
361+ mScreenBrightnessDimConfig );
362+
348363 mUseSoftwareAutoBrightnessConfig = resources .getBoolean (
349364 com .android .internal .R .bool .config_automatic_brightness_available );
350365 if (mUseSoftwareAutoBrightnessConfig ) {
@@ -362,12 +377,19 @@ public DisplayPowerController(Looper looper, Context context, Notifier notifier,
362377 + "which must be strictly increasing. "
363378 + "Auto-brightness will be disabled." );
364379 mUseSoftwareAutoBrightnessConfig = false ;
380+ } else {
381+ if (screenBrightness [0 ] < screenBrightnessMinimum ) {
382+ screenBrightnessMinimum = screenBrightness [0 ];
383+ }
365384 }
366385
367386 mLightSensorWarmUpTimeConfig = resources .getInteger (
368387 com .android .internal .R .integer .config_lightSensorWarmupTime );
369388 }
370389
390+ mScreenBrightnessRangeMinimum = clampAbsoluteBrightness (screenBrightnessMinimum );
391+ mScreenBrightnessRangeMaximum = PowerManager .BRIGHTNESS_ON ;
392+
371393 mElectronBeamAnimatesBacklightConfig = resources .getBoolean (
372394 com .android .internal .R .bool .config_animateScreenLights );
373395
@@ -394,14 +416,14 @@ private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
394416 final int n = brightness .length ;
395417 float [] x = new float [n ];
396418 float [] y = new float [n ];
397- y [0 ] = ( float ) brightness [0 ] / PowerManager . BRIGHTNESS_ON ;
419+ y [0 ] = normalizeAbsoluteBrightness ( brightness [0 ]) ;
398420 for (int i = 1 ; i < n ; i ++) {
399421 x [i ] = lux [i - 1 ];
400- y [i ] = ( float ) brightness [i ] / PowerManager . BRIGHTNESS_ON ;
422+ y [i ] = normalizeAbsoluteBrightness ( brightness [i ]) ;
401423 }
402424
403425 Spline spline = Spline .createMonotoneCubicSpline (x , y );
404- if (false ) {
426+ if (DEBUG ) {
405427 Slog .d (TAG , "Auto-brightness spline: " + spline );
406428 for (float v = 1f ; v < lux [lux .length - 1 ] * 1.25f ; v *= 1.25f ) {
407429 Slog .d (TAG , String .format (" %7.1f: %7.1f" , v , spline .interpolate (v )));
@@ -602,30 +624,31 @@ private void updatePowerState() {
602624 }
603625
604626 // Set the screen brightness.
605- if (mPowerRequest .screenState == DisplayPowerRequest .SCREEN_STATE_DIM ) {
606- // Screen is dimmed. Overrides everything else.
607- animateScreenBrightness (
608- clampScreenBrightness (mScreenBrightnessDimConfig ),
609- BRIGHTNESS_RAMP_RATE_FAST );
610- mUsingScreenAutoBrightness = false ;
611- } else if (mPowerRequest .screenState == DisplayPowerRequest .SCREEN_STATE_BRIGHT ) {
627+ if (wantScreenOn (mPowerRequest .screenState )) {
628+ int target ;
629+ boolean slow ;
612630 if (mScreenAutoBrightness >= 0 && mLightSensorEnabled ) {
613631 // Use current auto-brightness value.
614- animateScreenBrightness (
615- clampScreenBrightness (mScreenAutoBrightness ),
616- mUsingScreenAutoBrightness ? BRIGHTNESS_RAMP_RATE_SLOW :
617- BRIGHTNESS_RAMP_RATE_FAST );
632+ target = mScreenAutoBrightness ;
633+ slow = mUsingScreenAutoBrightness ;
618634 mUsingScreenAutoBrightness = true ;
619635 } else {
620636 // Light sensor is disabled or not ready yet.
621637 // Use the current brightness setting from the request, which is expected
622638 // provide a nominal default value for the case where auto-brightness
623639 // is not ready yet.
624- animateScreenBrightness (
625- clampScreenBrightness (mPowerRequest .screenBrightness ),
626- BRIGHTNESS_RAMP_RATE_FAST );
640+ target = mPowerRequest .screenBrightness ;
641+ slow = false ;
627642 mUsingScreenAutoBrightness = false ;
628643 }
644+ if (mPowerRequest .screenState == DisplayPowerRequest .SCREEN_STATE_DIM ) {
645+ // Screen is dimmed. Sets an upper bound on everything else.
646+ target = Math .min (target - SCREEN_DIM_MINIMUM_REDUCTION ,
647+ mScreenBrightnessDimConfig );
648+ slow = false ;
649+ }
650+ animateScreenBrightness (clampScreenBrightness (target ),
651+ slow ? BRIGHTNESS_RAMP_RATE_SLOW : BRIGHTNESS_RAMP_RATE_FAST );
629652 } else {
630653 // Screen is off. Don't bother changing the brightness.
631654 mUsingScreenAutoBrightness = false ;
@@ -729,7 +752,25 @@ private void setScreenOn(boolean on) {
729752 }
730753
731754 private int clampScreenBrightness (int value ) {
732- return Math .min (Math .max (Math .max (value , mScreenBrightnessDimConfig ), 0 ), 255 );
755+ return clamp (value , mScreenBrightnessRangeMinimum , mScreenBrightnessRangeMaximum );
756+ }
757+
758+ private static int clampAbsoluteBrightness (int value ) {
759+ return clamp (value , PowerManager .BRIGHTNESS_OFF , PowerManager .BRIGHTNESS_ON );
760+ }
761+
762+ private static int clamp (int value , int min , int max ) {
763+ if (value <= min ) {
764+ return min ;
765+ }
766+ if (value >= max ) {
767+ return max ;
768+ }
769+ return value ;
770+ }
771+
772+ private static float normalizeAbsoluteBrightness (int value ) {
773+ return (float )clampAbsoluteBrightness (value ) / PowerManager .BRIGHTNESS_ON ;
733774 }
734775
735776 private void animateScreenBrightness (int target , int rate ) {
@@ -1055,6 +1096,8 @@ public void dump(final PrintWriter pw) {
10551096 pw .println ();
10561097 pw .println ("Display Controller Configuration:" );
10571098 pw .println (" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig );
1099+ pw .println (" mScreenBrightnessRangeMinimum=" + mScreenBrightnessRangeMinimum );
1100+ pw .println (" mScreenBrightnessRangeMaximum=" + mScreenBrightnessRangeMaximum );
10581101 pw .println (" mUseSoftwareAutoBrightnessConfig="
10591102 + mUseSoftwareAutoBrightnessConfig );
10601103 pw .println (" mScreenAutoBrightnessSpline=" + mScreenAutoBrightnessSpline );
0 commit comments