Fix inconsistency in how we handle light sensor when battery is low.

In lightSensorChangedLocked we ignore the light sensor if the battery is low.
But in applyButtonState() and applyKeyboardState() we were still using the previous
mLightSensorScreenBrightness value, which resulted in a race condition that could
leave the button lights on after the screen turns off.
Now we ignore the light sensor value and button brightness override if the battery
is low so the low battery behavior is consistent.

Change-Id: I4943f8904299883211a95596ee207df69d1eaea2
BUG: 2570962

Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index c9dd553..b9021b0 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -1730,7 +1730,7 @@
         }
 
         if (offMask != 0) {
-            //Slog.i(TAG, "Setting brightess off: " + offMask);
+            if (mSpew) Slog.i(TAG, "Setting brightess off: " + offMask);
             setLightBrightness(offMask, Power.BRIGHTNESS_OFF);
         }
         if (dimMask != 0) {
@@ -1739,7 +1739,7 @@
                     brightness > Power.BRIGHTNESS_LOW_BATTERY) {
                 brightness = Power.BRIGHTNESS_LOW_BATTERY;
             }
-            //Slog.i(TAG, "Setting brightess dim " + brightness + ": " + offMask);
+            if (mSpew) Slog.i(TAG, "Setting brightess dim " + brightness + ": " + dimMask);
             setLightBrightness(dimMask, brightness);
         }
         if (onMask != 0) {
@@ -1748,7 +1748,7 @@
                     brightness > Power.BRIGHTNESS_LOW_BATTERY) {
                 brightness = Power.BRIGHTNESS_LOW_BATTERY;
             }
-            //Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
+            if (mSpew) Slog.i(TAG, "Setting brightess on " + brightness + ": " + onMask);
             setLightBrightness(onMask, brightness);
         }
     }
@@ -1883,6 +1883,10 @@
 
     private int applyButtonState(int state) {
         int brightness = -1;
+        if ((state & BATTERY_LOW_BIT) != 0) {
+            // do not override brightness if the battery is low
+            return state;
+        }
         if (mButtonBrightnessOverride >= 0) {
             brightness = mButtonBrightnessOverride;
         } else if (mLightSensorButtonBrightness >= 0 && mUseSoftwareAutoBrightness) {
@@ -1899,6 +1903,10 @@
 
     private int applyKeyboardState(int state) {
         int brightness = -1;
+        if ((state & BATTERY_LOW_BIT) != 0) {
+            // do not override brightness if the battery is low
+            return state;
+        }
         if (!mKeyboardVisible) {
             brightness = 0;
         } else if (mButtonBrightnessOverride >= 0) {