Keep automatic screen brightness monotonically increasing until screen is turned off.

This is an experimental change to avoid the light sensor screen fluctuation problem.
We only do this when undocked to since the lighting should be stable in the docked case
and since the dock keeps the screen on we need to be able to adjust the lighting.

Change-Id: I70afcc393f51f2679be8228d6cb993ddc07e5986
BUG: 2387223
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 e14a973..1e7dd99 100644
--- a/services/java/com/android/server/PowerManagerService.java
+++ b/services/java/com/android/server/PowerManagerService.java
@@ -212,11 +212,13 @@
     private Sensor mLightSensor;
     private boolean mLightSensorEnabled;
     private float mLightSensorValue = -1;
+    private int mHighestLightSensorValue = -1;
     private float mLightSensorPendingValue = -1;
     private int mLightSensorScreenBrightness = -1;
     private int mLightSensorButtonBrightness = -1;
     private int mLightSensorKeyboardBrightness = -1;
     private boolean mDimScreen = true;
+    private boolean mIsDocked = false;
     private long mNextTimeout;
     private volatile int mPokey = 0;
     private volatile boolean mPokeAwakeOnSet = false;
@@ -363,6 +365,15 @@
         }
     }
 
+    private final class DockReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
+                    Intent.EXTRA_DOCK_STATE_UNDOCKED);
+            dockStateChanged(state);
+        }
+    }
+
     /**
      * Set the setting that determines whether the device stays on when plugged in.
      * The argument is a bit string, with each bit specifying a power source that,
@@ -527,6 +538,9 @@
         filter = new IntentFilter();
         filter.addAction(Intent.ACTION_BOOT_COMPLETED);
         mContext.registerReceiver(new BootCompletedReceiver(), filter);
+        filter = new IntentFilter();
+        filter.addAction(Intent.ACTION_DOCK_EVENT);
+        mContext.registerReceiver(new DockReceiver(), filter);
 
         // Listen for secure settings changes
         mContext.getContentResolver().registerContentObserver(
@@ -1389,6 +1403,8 @@
                     // clear current value so we will update based on the new conditions
                     // when the sensor is reenabled.
                     mLightSensorValue = -1;
+                    // reset our highest light sensor value when the screen turns off
+                    mHighestLightSensorValue = -1;
                 }
             }
         }
@@ -2059,15 +2075,40 @@
         }
     };
 
+    private void dockStateChanged(int state) {
+        synchronized (mLocks) {
+            mIsDocked = (state != Intent.EXTRA_DOCK_STATE_UNDOCKED);
+            if (mIsDocked) {
+                mHighestLightSensorValue = -1;
+            }
+            if ((mPowerState & SCREEN_ON_BIT) != 0) {
+                // force lights recalculation
+                int value = (int)mLightSensorValue;
+                mLightSensorValue = -1;
+                lightSensorChangedLocked(value);
+            }
+        }
+    }
+
     private void lightSensorChangedLocked(int value) {
         if (mDebugLightSensor) {
             Log.d(TAG, "lightSensorChangedLocked " + value);
         }
 
+        // do not allow light sensor value to decrease
+        if (mHighestLightSensorValue < value) {
+            mHighestLightSensorValue = value;
+        }
+
         if (mLightSensorValue != value) {
             mLightSensorValue = value;
             if ((mPowerState & BATTERY_LOW_BIT) == 0) {
-                int lcdValue = getAutoBrightnessValue(value, mLcdBacklightValues);
+                // use maximum light sensor value seen since screen went on for LCD to avoid flicker
+                // we only do this if we are undocked, since lighting should be stable when
+                // stationary in a dock.
+                int lcdValue = getAutoBrightnessValue(
+                        (mIsDocked ? value : mHighestLightSensorValue),
+                        mLcdBacklightValues);
                 int buttonValue = getAutoBrightnessValue(value, mButtonBacklightValues);
                 int keyboardValue;
                 if (mKeyboardVisible) {