PowerManagerService: Avoid paralysis when light events are reported frequently.
Avoid resetting the debounce timer for automatic brightness if a new light event
is received that agrees with the direction of change of the previous event(s).
Change-Id: Id4d71f6db46ded46b24eb44cb8de9b2cfedb3f06
Signed-off-by: Mike Lockwood <lockwood@google.com>
diff --git a/services/java/com/android/server/PowerManagerService.java b/services/java/com/android/server/PowerManagerService.java
index 30e90a8..5cf013f 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -219,6 +219,8 @@
private float mLightSensorValue = -1;
private boolean mProxIgnoredBecauseScreenTurnedOff = false;
private int mHighestLightSensorValue = -1;
+ private boolean mLightSensorPendingDecrease = false;
+ private boolean mLightSensorPendingIncrease = false;
private float mLightSensorPendingValue = -1;
private int mLightSensorScreenBrightness = -1;
private int mLightSensorButtonBrightness = -1;
@@ -1141,6 +1143,8 @@
pw.println(" mLightSensorEnabled=" + mLightSensorEnabled);
pw.println(" mLightSensorValue=" + mLightSensorValue
+ " mLightSensorPendingValue=" + mLightSensorPendingValue);
+ pw.println(" mLightSensorPendingDecrease=" + mLightSensorPendingDecrease
+ + " mLightSensorPendingIncrease=" + mLightSensorPendingIncrease);
pw.println(" mLightSensorScreenBrightness=" + mLightSensorScreenBrightness
+ " mLightSensorButtonBrightness=" + mLightSensorButtonBrightness
+ " mLightSensorKeyboardBrightness=" + mLightSensorKeyboardBrightness);
@@ -1742,6 +1746,8 @@
} else {
// cancel light sensor task
mHandler.removeCallbacks(mAutoBrightnessTask);
+ mLightSensorPendingDecrease = false;
+ mLightSensorPendingIncrease = false;
mScreenOffTime = SystemClock.elapsedRealtime();
long identity = Binder.clearCallingIdentity();
try {
@@ -2325,9 +2331,10 @@
private Runnable mAutoBrightnessTask = new Runnable() {
public void run() {
synchronized (mLocks) {
- int value = (int)mLightSensorPendingValue;
- if (value >= 0) {
- mLightSensorPendingValue = -1;
+ if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
+ int value = (int)mLightSensorPendingValue;
+ mLightSensorPendingDecrease = false;
+ mLightSensorPendingIncrease = false;
lightSensorChangedLocked(value);
}
}
@@ -2981,19 +2988,29 @@
if (mDebugLightSensor) {
Slog.d(TAG, "onSensorChanged: light value: " + value);
}
- mHandler.removeCallbacks(mAutoBrightnessTask);
- if (mLightSensorValue != value) {
- 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
- mLightSensorPendingValue = value;
- mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
- }
+ if (mLightSensorValue == -1 ||
+ milliseconds < mLastScreenOnTime + mLightSensorWarmupTime) {
+ // process the value immediately if screen has just turned on
+ mHandler.removeCallbacks(mAutoBrightnessTask);
+ mLightSensorPendingDecrease = false;
+ mLightSensorPendingIncrease = false;
+ lightSensorChangedLocked(value);
} else {
- mLightSensorPendingValue = -1;
+ if ((value > mLightSensorValue && mLightSensorPendingDecrease) ||
+ (value < mLightSensorValue && mLightSensorPendingIncrease) ||
+ (value == mLightSensorValue) ||
+ (!mLightSensorPendingDecrease && !mLightSensorPendingIncrease)) {
+ // delay processing to debounce the sensor
+ mHandler.removeCallbacks(mAutoBrightnessTask);
+ mLightSensorPendingDecrease = (value < mLightSensorValue);
+ mLightSensorPendingIncrease = (value > mLightSensorValue);
+ if (mLightSensorPendingDecrease || mLightSensorPendingIncrease) {
+ mLightSensorPendingValue = value;
+ mHandler.postDelayed(mAutoBrightnessTask, LIGHT_SENSOR_DELAY);
+ }
+ } else {
+ mLightSensorPendingValue = value;
+ }
}
}
}