Do not apply light sensor debounce logic while the light sensor is still warming up.

This allows us to adjust immediately when we receive a correct light sensor reading
after the screen turns on.

Change-Id: I627047365640761727348e577c9164a6d6be2772
Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/core/res/res/values/config.xml b/core/res/res/values/config.xml
index 9058221..aaa1d8b 100644
--- a/core/res/res/values/config.xml
+++ b/core/res/res/values/config.xml
@@ -200,6 +200,11 @@
     <integer-array name="config_autoBrightnessKeyboardBacklightValues">
     </integer-array>
 
+    <!-- Amount of time it takes for the light sensor to warm up in milliseconds.
+         For this time after the screen turns on, the Power Manager
+         will not debounce light sensor readings -->
+    <integer name="config_lightSensorWarmupTime">0</integer>
+
     <!-- Enables swipe versus poly-finger touch disambiguation in the KeyboardView -->
     <bool name="config_swipeDisambiguation">true</bool>
 </resources>
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 2e1cf56..af93d36 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -212,8 +212,12 @@
     private volatile boolean mPokeAwakeOnSet = false;
     private volatile boolean mInitComplete = false;
     private HashMap<IBinder,PokeLock> mPokeLocks = new HashMap<IBinder,PokeLock>();
+    // mScreenOnTime and mScreenOnStartTime are used for computing total time screen
+    // has been on since boot
     private long mScreenOnTime;
     private long mScreenOnStartTime;
+    // mLastScreenOnTime is the time the screen was last turned on
+    private long mLastScreenOnTime;
     private boolean mPreventScreenOn;
     private int mScreenBrightnessOverride = -1;
     private boolean mUseSoftwareAutoBrightness;
@@ -222,6 +226,7 @@
     private int[] mLcdBacklightValues;
     private int[] mButtonBacklightValues;
     private int[] mKeyboardBacklightValues;
+    private int mLightSensorWarmupTime;
 
     // Used when logging number and duration of touch-down cycles
     private long mTotalTouchDownTime;
@@ -456,6 +461,8 @@
                     com.android.internal.R.array.config_autoBrightnessButtonBacklightValues);
             mKeyboardBacklightValues = resources.getIntArray(
                     com.android.internal.R.array.config_autoBrightnessKeyboardBacklightValues);
+            mLightSensorWarmupTime = resources.getInteger(
+                    com.android.internal.R.integer.config_lightSensorWarmupTime);
         }
 
        ContentResolver resolver = mContext.getContentResolver();
@@ -886,6 +893,7 @@
         pw.println("  mPreventScreenOn=" + mPreventScreenOn
                 + "  mScreenBrightnessOverride=" + mScreenBrightnessOverride);
         pw.println("  mTotalDelaySetting=" + mTotalDelaySetting);
+        pw.println("  mLastScreenOnTime=" + mLastScreenOnTime);
         pw.println("  mBroadcastWakeLock=" + mBroadcastWakeLock);
         pw.println("  mStayOnWhilePluggedInScreenDimLock=" + mStayOnWhilePluggedInScreenDimLock);
         pw.println("  mStayOnWhilePluggedInPartialLock=" + mStayOnWhilePluggedInPartialLock);
@@ -1299,15 +1307,18 @@
 
     private int setScreenStateLocked(boolean on) {
         int err = Power.setScreenState(on);
-        if (err == 0 && mUseSoftwareAutoBrightness) {
-            enableLightSensor(on);
-            if (!on) {
-                // make sure button and key backlights are off too
-                mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BUTTONS, 0);
-                mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_KEYBOARD, 0);
-                // clear current value so we will update based on the new conditions
-                // when the sensor is reenabled.
-                mLightSensorValue = -1;
+        if (err == 0) {
+            mLastScreenOnTime = (on ? SystemClock.elapsedRealtime() : 0);
+            if (mUseSoftwareAutoBrightness) {
+                enableLightSensor(on);
+                if (!on) {
+                    // make sure button and key backlights are off too
+                    mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_BUTTONS, 0);
+                    mHardware.setLightBrightness_UNCHECKED(HardwareService.LIGHT_ID_KEYBOARD, 0);
+                    // clear current value so we will update based on the new conditions
+                    // when the sensor is reenabled.
+                    mLightSensorValue = -1;
+                }
             }
         }
         return err;
@@ -2431,13 +2442,15 @@
         public void onSensorChanged(SensorEvent event) {
             synchronized (mLocks) {
                 int value = (int)event.values[0];
+                long milliseconds = event.timestamp / 1000000;
                 if (mDebugLightSensor) {
                     Log.d(TAG, "onSensorChanged: light value: " + value);
                 }
                 mHandler.removeCallbacks(mAutoBrightnessTask);
                 if (mLightSensorValue != value) {
-                    if (mLightSensorValue == -1) {
-                        // process the value immediately
+                    if (mLightSensorValue == -1 ||
+                            milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
+                        // process the value immediately if screen has just turned on
                         lightSensorChangedLocked(value);
                     } else {
                         // delay processing to debounce the sensor