3333import android .os .Message ;
3434import android .os .SystemClock ;
3535import android .util .Slog ;
36+ import android .util .Spline ;
3637import android .util .TimeUtils ;
3738
3839import java .io .PrintWriter ;
3940import java .io .StringWriter ;
40- import java .util .Arrays ;
4141import java .util .concurrent .CountDownLatch ;
4242import java .util .concurrent .Executor ;
4343
@@ -98,9 +98,9 @@ final class DisplayPowerController {
9898 // average of light samples. Different constants are used
9999 // to calculate the average light level when adapting to brighter or
100100 // dimmer environments.
101- // This parameter only controls the averaging of light samples.
102- private static final long BRIGHTENING_LIGHT_TIME_CONSTANT = 1500 ;
103- private static final long DIMMING_LIGHT_TIME_CONSTANT = 3000 ;
101+ // This parameter only controls the filtering of light samples.
102+ private static final long BRIGHTENING_LIGHT_TIME_CONSTANT = 500 ;
103+ private static final long DIMMING_LIGHT_TIME_CONSTANT = 2000 ;
104104
105105 // Stability requirements in milliseconds for accepting a new brightness
106106 // level. This is used for debouncing the light sensor. Different constants
@@ -144,8 +144,7 @@ final class DisplayPowerController {
144144
145145 // Auto-brightness.
146146 private boolean mUseSoftwareAutoBrightnessConfig ;
147- private int [] mAutoBrightnessLevelsConfig ;
148- private int [] mAutoBrightnessLcdBacklightValuesConfig ;
147+ private Spline mScreenAutoBrightnessSpline ;
149148
150149 // Amount of time to delay auto-brightness after screen on while waiting for
151150 // the light sensor to warm-up in milliseconds.
@@ -289,17 +288,18 @@ public DisplayPowerController(Looper looper, Context context, Notifier notifier,
289288 mUseSoftwareAutoBrightnessConfig = resources .getBoolean (
290289 com .android .internal .R .bool .config_automatic_brightness_available );
291290 if (mUseSoftwareAutoBrightnessConfig ) {
292- mAutoBrightnessLevelsConfig = resources .getIntArray (
291+ int [] lux = resources .getIntArray (
293292 com .android .internal .R .array .config_autoBrightnessLevels );
294- mAutoBrightnessLcdBacklightValuesConfig = resources .getIntArray (
293+ int [] screenBrightness = resources .getIntArray (
295294 com .android .internal .R .array .config_autoBrightnessLcdBacklightValues );
296- if (mAutoBrightnessLcdBacklightValuesConfig .length
297- != mAutoBrightnessLevelsConfig .length + 1 ) {
295+
296+ mScreenAutoBrightnessSpline = createAutoBrightnessSpline (lux , screenBrightness );
297+ if (mScreenAutoBrightnessSpline == null ) {
298298 Slog .e (TAG , "Error in config.xml. config_autoBrightnessLcdBacklightValues "
299- + "(size " + mAutoBrightnessLcdBacklightValuesConfig .length + ") "
300- + "should have exactly one more entry than "
301- + "config_autoBrightnessLevels (size "
302- + mAutoBrightnessLevelsConfig . length + ") . "
299+ + "(size " + screenBrightness .length + ") "
300+ + "must be monotic and have exactly one more entry than "
301+ + "config_autoBrightnessLevels (size " + lux . length + ") "
302+ + "which must be strictly increasing . "
303303 + "Auto-brightness will be disabled." );
304304 mUseSoftwareAutoBrightnessConfig = false ;
305305 }
@@ -322,6 +322,31 @@ public DisplayPowerController(Looper looper, Context context, Notifier notifier,
322322 }
323323 }
324324
325+ private static Spline createAutoBrightnessSpline (int [] lux , int [] brightness ) {
326+ try {
327+ final int n = brightness .length ;
328+ float [] x = new float [n ];
329+ float [] y = new float [n ];
330+ y [0 ] = brightness [0 ];
331+ for (int i = 1 ; i < n ; i ++) {
332+ x [i ] = lux [i - 1 ];
333+ y [i ] = brightness [i ];
334+ }
335+
336+ Spline spline = Spline .createMonotoneCubicSpline (x , y );
337+ if (false ) {
338+ Slog .d (TAG , "Auto-brightness spline: " + spline );
339+ for (float v = 1f ; v < lux [lux .length - 1 ] * 1.25f ; v *= 1.25f ) {
340+ Slog .d (TAG , String .format (" %7.1f: %7.1f" , v , spline .interpolate (v )));
341+ }
342+ }
343+ return spline ;
344+ } catch (IllegalArgumentException ex ) {
345+ Slog .e (TAG , "Could not create auto-brightness spline." , ex );
346+ return null ;
347+ }
348+ }
349+
325350 /**
326351 * Returns true if the proximity sensor screen-off function is available.
327352 */
@@ -768,13 +793,13 @@ private void updateAutoBrightness(boolean sendUpdate) {
768793 return ;
769794 }
770795
771- final int newScreenAutoBrightness = mapLuxToBrightness (mLightMeasurement ,
772- mAutoBrightnessLevelsConfig ,
773- mAutoBrightnessLcdBacklightValuesConfig );
796+ final int newScreenAutoBrightness = interpolateBrightness (
797+ mScreenAutoBrightnessSpline , mLightMeasurement );
774798 if (mScreenAutoBrightness != newScreenAutoBrightness ) {
775799 if (DEBUG ) {
776800 Slog .d (TAG , "updateAutoBrightness: mScreenAutoBrightness="
777- + mScreenAutoBrightness );
801+ + mScreenAutoBrightness + "newScreenAutoBrightness="
802+ + newScreenAutoBrightness );
778803 }
779804
780805 mScreenAutoBrightness = newScreenAutoBrightness ;
@@ -784,20 +809,8 @@ private void updateAutoBrightness(boolean sendUpdate) {
784809 }
785810 }
786811
787- /**
788- * Maps a light sensor measurement in lux to a brightness value given
789- * a table of lux breakpoint values and a table of brightnesses that
790- * is one element larger.
791- */
792- private static int mapLuxToBrightness (float lux ,
793- int [] fromLux , int [] toBrightness ) {
794- // TODO implement interpolation and possibly range expansion
795- int level = 0 ;
796- final int count = fromLux .length ;
797- while (level < count && lux >= fromLux [level ]) {
798- level += 1 ;
799- }
800- return toBrightness [level ];
812+ private static int interpolateBrightness (Spline spline , float lux ) {
813+ return Math .min (255 , Math .max (0 , (int )Math .round (spline .interpolate (lux ))));
801814 }
802815
803816 private void sendOnStateChanged () {
@@ -839,10 +852,7 @@ public void dump(PrintWriter pw) {
839852 pw .println (" mScreenBrightnessDimConfig=" + mScreenBrightnessDimConfig );
840853 pw .println (" mUseSoftwareAutoBrightnessConfig="
841854 + mUseSoftwareAutoBrightnessConfig );
842- pw .println (" mAutoBrightnessLevelsConfig="
843- + Arrays .toString (mAutoBrightnessLevelsConfig ));
844- pw .println (" mAutoBrightnessLcdBacklightValuesConfig="
845- + Arrays .toString (mAutoBrightnessLcdBacklightValuesConfig ));
855+ pw .println (" mScreenAutoBrightnessSpline=" + mScreenAutoBrightnessSpline );
846856 pw .println (" mLightSensorWarmUpTimeConfig=" + mLightSensorWarmUpTimeConfig );
847857
848858 if (Looper .myLooper () == mHandler .getLooper ()) {
0 commit comments