A better auto brightness

Bug: 14927472
Change-Id: I62d0695b7da16ffa7a27c465c3b6bc4b1515f9c0
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index f8d7c3e..5b2c8db 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -366,15 +366,6 @@
     }
 
     /**
-     * Returns true if the screen auto-brightness adjustment setting should
-     * be available in the UI.  This setting is experimental and disabled by default.
-     * @hide
-     */
-    public static boolean useScreenAutoBrightnessAdjustmentFeature() {
-        return SystemProperties.getBoolean("persist.power.useautobrightadj", false);
-    }
-
-    /**
      * Returns true if the twilight service should be used to adjust screen brightness
      * policy.  This setting is experimental and disabled by default.
      * @hide
diff --git a/packages/SystemUI/res/layout/status_bar_toggle_slider.xml b/packages/SystemUI/res/layout/status_bar_toggle_slider.xml
index 0e84762..7671c354a 100644
--- a/packages/SystemUI/res/layout/status_bar_toggle_slider.xml
+++ b/packages/SystemUI/res/layout/status_bar_toggle_slider.xml
@@ -29,6 +29,7 @@
         android:layout_alignParentBottom="true"
         android:button="@null"
         android:background="@*android:drawable/switch_track_quantum"
+        android:visibility="gone"
         />
     <com.android.systemui.settings.ToggleSeekBar
         android:id="@+id/slider"
@@ -36,6 +37,7 @@
         android:layout_height="wrap_content"
         android:layout_toEndOf="@id/toggle"
         android:layout_centerVertical="true"
+        android:layout_alignParentStart="true"
         android:layout_alignParentEnd="true"
         android:paddingStart="20dp"
         android:paddingEnd="20dp"
@@ -51,5 +53,6 @@
         android:paddingTop="26dp"
         android:textColor="#666666"
         android:textSize="12sp"
+        android:visibility="gone"
         />
 </merge>
diff --git a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
index 1747e6e..c0a07e9 100644
--- a/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
+++ b/packages/SystemUI/src/com/android/systemui/settings/BrightnessController.java
@@ -28,7 +28,6 @@
 import android.os.ServiceManager;
 import android.os.UserHandle;
 import android.provider.Settings;
-import android.provider.Settings.SettingNotFoundException;
 import android.widget.ImageView;
 
 import java.util.ArrayList;
@@ -36,6 +35,12 @@
 public class BrightnessController implements ToggleSlider.Listener {
     private static final String TAG = "StatusBar.BrightnessController";
 
+    /**
+     * {@link android.provider.Settings.System#SCREEN_AUTO_BRIGHTNESS_ADJ} uses the range [-1, 1].
+     * Using this factor, it is converted to [0, BRIGHTNESS_ADJ_RESOLUTION] for the SeekBar.
+     */
+    private static final float BRIGHTNESS_ADJ_RESOLUTION = 100;
+
     private final int mMinimumBacklight;
     private final int mMaximumBacklight;
 
@@ -51,6 +56,8 @@
     private ArrayList<BrightnessStateChangeCallback> mChangeCallbacks =
             new ArrayList<BrightnessStateChangeCallback>();
 
+    private boolean mAutomatic;
+
     public interface BrightnessStateChangeCallback {
         public void onBrightnessLevelChanged();
     }
@@ -62,6 +69,8 @@
                 Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);
         private final Uri BRIGHTNESS_URI =
                 Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
+        private final Uri BRIGHTNESS_ADJ_URI =
+                Settings.System.getUriFor(Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ);
 
         public BrightnessObserver(Handler handler) {
             super(handler);
@@ -77,7 +86,10 @@
             if (selfChange) return;
             if (BRIGHTNESS_MODE_URI.equals(uri)) {
                 updateMode();
-            } else if (BRIGHTNESS_URI.equals(uri)) {
+                updateSlider();
+            } else if (BRIGHTNESS_URI.equals(uri) && !mAutomatic) {
+                updateSlider();
+            } else if (BRIGHTNESS_ADJ_URI.equals(uri) && mAutomatic) {
                 updateSlider();
             } else {
                 updateMode();
@@ -97,6 +109,9 @@
             cr.registerContentObserver(
                     BRIGHTNESS_URI,
                     false, this, UserHandle.USER_ALL);
+            cr.registerContentObserver(
+                    BRIGHTNESS_ADJ_URI,
+                    false, this, UserHandle.USER_ALL);
         }
 
         public void stopObserving() {
@@ -163,10 +178,8 @@
     }
 
     public void onChanged(ToggleSlider view, boolean tracking, boolean automatic, int value) {
-        setMode(automatic ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
-                : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
-        updateIcon(automatic);
-        if (!automatic) {
+        updateIcon(mAutomatic);
+        if (!mAutomatic) {
             final int val = value + mMinimumBacklight;
             setBrightness(val);
             if (!tracking) {
@@ -178,6 +191,18 @@
                         }
                     });
             }
+        } else {
+            final float adj = value / (BRIGHTNESS_ADJ_RESOLUTION / 2f) - 1;
+            setBrignessAdj(adj);
+            if (!tracking) {
+                AsyncTask.execute(new Runnable() {
+                    public void run() {
+                        Settings.System.putFloatForUser(mContext.getContentResolver(),
+                                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, adj,
+                                UserHandle.USER_CURRENT);
+                    }
+                });
+            }
         }
 
         for (BrightnessStateChangeCallback cb : mChangeCallbacks) {
@@ -198,6 +223,13 @@
         }
     }
 
+    private void setBrignessAdj(float adj) {
+        try {
+            mPower.setTemporaryScreenAutoBrightnessAdjustmentSettingOverride(adj);
+        } catch (RemoteException ex) {
+        }
+    }
+
     private void updateIcon(boolean automatic) {
         if (mIcon != null) {
             mIcon.setImageResource(automatic ?
@@ -210,15 +242,12 @@
     private void updateMode() {
         if (mAutomaticAvailable) {
             int automatic;
-            try {
-                automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
-                        Settings.System.SCREEN_BRIGHTNESS_MODE,
-                        UserHandle.USER_CURRENT);
-            } catch (SettingNotFoundException snfe) {
-                automatic = 0;
-            }
-            mControl.setChecked(automatic != 0);
-            updateIcon(automatic != 0);
+            automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
+                    Settings.System.SCREEN_BRIGHTNESS_MODE,
+                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL,
+                    UserHandle.USER_CURRENT);
+            mAutomatic = automatic != 0;
+            updateIcon(mAutomatic);
         } else {
             mControl.setChecked(false);
             updateIcon(false /*automatic*/);
@@ -227,16 +256,20 @@
 
     /** Fetch the brightness from the system settings and update the slider */
     private void updateSlider() {
-        int value;
-        try {
-            value = Settings.System.getIntForUser(mContext.getContentResolver(),
-                    Settings.System.SCREEN_BRIGHTNESS,
+        if (mAutomatic) {
+            float value = Settings.System.getFloatForUser(mContext.getContentResolver(),
+                    Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0,
                     UserHandle.USER_CURRENT);
-        } catch (SettingNotFoundException ex) {
-            value = mMaximumBacklight;
+            mControl.setMax((int) BRIGHTNESS_ADJ_RESOLUTION);
+            mControl.setValue((int) ((value + 1) * BRIGHTNESS_ADJ_RESOLUTION / 2f));
+        } else {
+            int value;
+            value = Settings.System.getIntForUser(mContext.getContentResolver(),
+                    Settings.System.SCREEN_BRIGHTNESS, mMaximumBacklight,
+                    UserHandle.USER_CURRENT);
+            mControl.setMax(mMaximumBacklight - mMinimumBacklight);
+            mControl.setValue(value - mMinimumBacklight);
         }
-        mControl.setMax(mMaximumBacklight - mMinimumBacklight);
-        mControl.setValue(value - mMinimumBacklight);
     }
 
 }
diff --git a/services/core/java/com/android/server/display/AutomaticBrightnessController.java b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
index 5fa1584..af0c56b 100644
--- a/services/core/java/com/android/server/display/AutomaticBrightnessController.java
+++ b/services/core/java/com/android/server/display/AutomaticBrightnessController.java
@@ -48,8 +48,7 @@
     private static final boolean DEBUG_PRETEND_LIGHT_SENSOR_ABSENT = false;
 
     // If true, enables the use of the screen auto-brightness adjustment setting.
-    private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT =
-            PowerManager.useScreenAutoBrightnessAdjustmentFeature();
+    private static final boolean USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT = true;
 
     // The maximum range of gamma adjustment possible using the screen
     // auto-brightness adjustment setting.