Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad49bcac authored by Danny Baumann's avatar Danny Baumann Committed by Gerrit Code Review
Browse files

Add back backlight brightness curve adjustment.

Change-Id: I58408111d6366a281303afb469d78588ae88d275
parent 8e88ebea
Loading
Loading
Loading
Loading
+6 −118
Original line number Diff line number Diff line
@@ -1705,134 +1705,22 @@ public final class Settings {
        public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;

        /**
         * Indicates that custom light sensor settings has changed.
         * The value is random and changes reloads light settings.
         *
         * @hide
         */
        public static final String LIGHTS_CHANGED = "lights_changed";

        /**
         * Whether custom light sensor levels & values are enabled. The value is
         * boolean (1 or 0).
         *
         * @hide
         */
        public static final String LIGHT_SENSOR_CUSTOM = "light_sensor_custom";

        /**
         * Screen dim value to use if LIGHT_SENSOR_CUSTOM is set. The value is int.
         * Default is android.os.BRIGHTNESS_DIM.
         *
         * @hide
         */
        public static final String LIGHT_SCREEN_DIM = "light_screen_dim";

        /**
         * Custom light sensor levels. The value is a comma separated int array
         * with length N.
         * Custom automatic brightness light sensor levels.
         * The value is a comma separated int array with length N.
         * Example: "100,300,3000".
         *
         * @hide
         */
        public static final String LIGHT_SENSOR_LEVELS = "light_sensor_levels";
        public static final String AUTO_BRIGHTNESS_LUX = "auto_brightness_lux";

        /**
         * Custom light sensor lcd values. The value is a comma separated int array
         * with length N+1.
         * Custom automatic brightness display backlight brightness values.
         * The value is a comma separated int array with length N+1.
         * Example: "10,50,100,255".
         *
         * @hide
         */
        public static final String LIGHT_SENSOR_LCD_VALUES = "light_sensor_lcd_values";

        /**
         * Custom light sensor lcd values. The value is a comma separated int array
         * with length N+1.
         * Example: "10,50,100,255".
         *
         * @hide
         */
        public static final String LIGHT_SENSOR_BUTTON_VALUES = "light_sensor_button_values";

        /**
         * Custom light sensor lcd values. The value is a comma separated int array
         * with length N+1.
         * Example: "10,50,100,255".
         *
         * @hide
         */
        public static final String LIGHT_SENSOR_KEYBOARD_VALUES = "light_sensor_keyboard_values";

        /**
         * Whether light sensor is allowed to decrease when calculating automatic
         * backlight. The value is boolean (1 or 0).
         *
         * @hide
         */
        public static final String LIGHT_DECREASE = "light_decrease";

        /**
         * Light sensor hysteresis for decreasing backlight. The value is
         * int (0-99) representing % (0-0.99 as float). Example:
         *
         * Levels     Output
         * 0 - 100    50
         * 100 - 200  100
         * 200 - Inf  255
         *
         * Current sensor value is 150 which gives light value 100. Hysteresis is 50.
         * Current level lower bound is 100 and previous lower bound is 0.
         * Sensor value must drop below 100-(100-0)*(50/100)=50 for output to become 50
         * (corresponding to the 0 - 100 level).
         * @hide
         */
        public static final String LIGHT_HYSTERESIS = "light_hysteresis";

        /**
         * Whether light sensor used when calculating automatic backlight should
         * be filtered through an moving average filter.
         * The value is boolean (1 or 0).
         *
         * @hide
         */
        public static final String LIGHT_FILTER = "light_filter";

        /**
         * Window length of filter used when calculating automatic backlight.
         * One minute means that the average sensor value last minute is used.
         * The value is integer (milliseconds)
         *
         * @hide
         */
        public static final String LIGHT_FILTER_WINDOW = "light_filter_window";

        /**
         * Reset threshold of filter used when calculating automatic backlight.
         * Sudden large jumps in sensor value resets the filter. This is used
         * to make the filter respond quickly to large enough changes in input
         * while still filtering small changes. Example:
         *
         * Current filter value (average) is 100 and sensor value is changing to
         * 10, 150, 100, 30, 50. The filter is continously taking the average of
         * the samples. Now the user goes outside and the value jumps over 1000.
         * The difference between current average and new sample is larger than
         * the reset threshold and filter is reset. It begins calculating a new
         * average on samples around 1000 (say, 800, 1200, 1000, 1100 etc.)
         *
         * The value is integer (lux)
         *
         * @hide
         */
        public static final String LIGHT_FILTER_RESET = "light_filter_reset";

        /**
         * Sample interval of filter used when calculating automatic backlight.
         * The value is integer (milliseconds)
         *
         * @hide
         */
        public static final String LIGHT_FILTER_INTERVAL = "light_filter_interval";
        public static final String AUTO_BRIGHTNESS_BACKLIGHT = "auto_brightness_backlight";

        /**
         * Whether to enable the electron beam animation when turning screen on
+0 −11
Original line number Diff line number Diff line
@@ -51,8 +51,6 @@ public class BrightnessButton extends PowerButton {
    static {
        OBSERVED_URIS.add(BRIGHTNESS_URI);
        OBSERVED_URIS.add(BRIGHTNESS_MODE_URI);
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.LIGHT_SENSOR_CUSTOM));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.LIGHT_SCREEN_DIM));
        OBSERVED_URIS.add(Settings.System.getUriFor(Settings.System.EXPANDED_BRIGHTNESS_MODE));
    }

@@ -156,15 +154,6 @@ public class BrightnessButton extends PowerButton {
    }

    private void updateSettings(ContentResolver resolver) {
        boolean lightSensorCustom = (Settings.System.getInt(resolver,
                Settings.System.LIGHT_SENSOR_CUSTOM, 0) != 0);
        if (lightSensorCustom) {
            BACKLIGHTS[1] = Settings.System.getInt(resolver, Settings.System.LIGHT_SCREEN_DIM,
                    MIN_BACKLIGHT);
        } else {
            BACKLIGHTS[1] = MIN_BACKLIGHT;
        }

        String[] modes = parseStoredValue(Settings.System.getString(
                resolver, Settings.System.EXPANDED_BRIGHTNESS_MODE));
        if (modes == null || modes.length == 0) {
+100 −23
Original line number Diff line number Diff line
@@ -22,7 +22,9 @@ import com.android.server.TwilightService.TwilightState;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
@@ -30,11 +32,14 @@ import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.SystemSensorManager;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.format.DateUtils;
import android.util.FloatMath;
import android.util.Slog;
@@ -168,6 +173,9 @@ final class DisplayPowerController {
    // The display blanker.
    private final DisplayBlanker mDisplayBlanker;

    // Our context
    private final Context mContext;

    // Our handler.
    private final DisplayControllerHandler mHandler;

@@ -340,6 +348,7 @@ final class DisplayPowerController {

    // Twilight changed.  We might recalculate auto-brightness values.
    private boolean mTwilightChanged;
    private boolean mAutoBrightnessSettingsChanged;

    /**
     * Creates the display power controller.
@@ -348,6 +357,7 @@ final class DisplayPowerController {
            LightsService lights, TwilightService twilight,
            DisplayBlanker displayBlanker,
            Callbacks callbacks, Handler callbackHandler) {
        mContext = context;
        mHandler = new DisplayControllerHandler(looper);
        mNotifier = notifier;
        mDisplayBlanker = displayBlanker;
@@ -368,36 +378,35 @@ final class DisplayPowerController {
                com.android.internal.R.integer.config_screenBrightnessSettingMinimum),
                mScreenBrightnessDimConfig);

        mScreenBrightnessRangeMinimum = clampAbsoluteBrightness(screenBrightnessMinimum);
        mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON;

        mUseSoftwareAutoBrightnessConfig = resources.getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available);
        if (mUseSoftwareAutoBrightnessConfig) {
            int[] lux = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLevels);
            int[] screenBrightness = resources.getIntArray(
                    com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);

            mScreenAutoBrightnessSpline = createAutoBrightnessSpline(lux, screenBrightness);
            if (mScreenAutoBrightnessSpline == null) {
                Slog.e(TAG, "Error in config.xml.  config_autoBrightnessLcdBacklightValues "
                        + "(size " + screenBrightness.length + ") "
                        + "must be monotic and have exactly one more entry than "
                        + "config_autoBrightnessLevels (size " + lux.length + ") "
                        + "which must be strictly increasing.  "
                        + "Auto-brightness will be disabled.");
                mUseSoftwareAutoBrightnessConfig = false;
            } else {
                if (screenBrightness[0] < screenBrightnessMinimum) {
                    screenBrightnessMinimum = screenBrightness[0];
                }
        if (mUseSoftwareAutoBrightnessConfig) {
            final ContentResolver cr = mContext.getContentResolver();
            final ContentObserver observer = new ContentObserver(mHandler) {
                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    mAutoBrightnessSettingsChanged = true;
                    updateAutomaticBrightnessSettings();
                    updatePowerState();
                }
            };

            cr.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.AUTO_BRIGHTNESS_LUX),
                    false, observer, UserHandle.USER_ALL);
            cr.registerContentObserver(
                    Settings.System.getUriFor(Settings.System.AUTO_BRIGHTNESS_BACKLIGHT),
                    false, observer, UserHandle.USER_ALL);

            mLightSensorWarmUpTimeConfig = resources.getInteger(
                    com.android.internal.R.integer.config_lightSensorWarmupTime);
            updateAutomaticBrightnessSettings();
        }

        mScreenBrightnessRangeMinimum = clampAbsoluteBrightness(screenBrightnessMinimum);
        mScreenBrightnessRangeMaximum = PowerManager.BRIGHTNESS_ON;

        mElectronBeamFadesConfig = resources.getBoolean(
                com.android.internal.R.bool.config_animateScreenLights);

@@ -419,6 +428,62 @@ final class DisplayPowerController {
        }
    }

    private void updateAutomaticBrightnessSettings() {
        int[] lux = getIntArrayForSetting(Settings.System.AUTO_BRIGHTNESS_LUX);
        int[] values = getIntArrayForSetting(Settings.System.AUTO_BRIGHTNESS_BACKLIGHT);
        Resources res = mContext.getResources();

        mScreenAutoBrightnessSpline = null;
        mUseSoftwareAutoBrightnessConfig = true;

        if (lux != null && values != null) {
            mScreenAutoBrightnessSpline = createAutoBrightnessSpline(lux, values);
            if (mScreenAutoBrightnessSpline == null) {
                Slog.w(TAG, "Found invalid auto-brightness configuration, falling back to default");
            }
        }

        if (mScreenAutoBrightnessSpline == null) {
            lux = res.getIntArray(com.android.internal.R.array.config_autoBrightnessLevels);
            values = res.getIntArray(com.android.internal.R.array.config_autoBrightnessLcdBacklightValues);
            mScreenAutoBrightnessSpline = createAutoBrightnessSpline(lux, values);
        }

        if (mScreenAutoBrightnessSpline == null) {
            Slog.e(TAG, "Error in config.xml.  config_autoBrightnessLcdBacklightValues "
                    + "(size " + values.length + ") "
                    + "must be monotic and have exactly one more entry than "
                    + "config_autoBrightnessLevels (size " + lux.length + ") "
                    + "which must be strictly increasing.  "
                    + "Auto-brightness will be disabled.");
            mUseSoftwareAutoBrightnessConfig = false;
            return;
        }
    }

    private int[] getIntArrayForSetting(String setting) {
        final String value = Settings.System.getStringForUser(
                mContext.getContentResolver(), setting, UserHandle.USER_CURRENT);
        if (value == null) {
            return null;
        }
        String[] items = value.split(",");
        if (items == null || items.length == 0) {
            return null;
        }

        int[] values = new int[items.length];
        for (int i = 0; i < items.length; i++) {
            try {
                values[i] = Integer.valueOf(items[i]);
            } catch (NumberFormatException e) {
                return null;
            }
        }

        return values;
    }

    private static Spline createAutoBrightnessSpline(int[] lux, int[] brightness) {
        try {
            final int n = brightness.length;
@@ -430,6 +495,12 @@ final class DisplayPowerController {
                y[i] = normalizeAbsoluteBrightness(brightness[i]);
            }

            if (DEBUG) {
                for (int i = 0; i < n; i++) {
                    Slog.d(TAG, "Spline data[" + i + "]: x = " + x[i] + " y = " + y[i]);
                }
            }

            Spline spline = Spline.createMonotoneCubicSpline(x, y);
            if (DEBUG) {
                Slog.d(TAG, "Auto-brightness spline: " + spline);
@@ -557,9 +628,11 @@ final class DisplayPowerController {
        // Update the power state request.
        final boolean mustNotify;
        boolean mustInitialize = false;
        boolean updateAutoBrightness = mTwilightChanged;
        boolean updateAutoBrightness = mTwilightChanged || mAutoBrightnessSettingsChanged;
        boolean wasDim = false;

        mTwilightChanged = false;
        mAutoBrightnessSettingsChanged = false;

        synchronized (mLock) {
            mPendingUpdatePowerStateLocked = false;
@@ -923,7 +996,7 @@ final class DisplayPowerController {
            mDebounceLuxTime = time;
            if (DEBUG) {
                Slog.d(TAG, "updateAmbientLux: Initializing: "
                        + ", mRecentShortTermAverageLux=" + mRecentShortTermAverageLux
                        + "mRecentShortTermAverageLux=" + mRecentShortTermAverageLux
                        + ", mRecentLongTermAverageLux=" + mRecentLongTermAverageLux
                        + ", mAmbientLux=" + mAmbientLux);
            }
@@ -1047,6 +1120,10 @@ final class DisplayPowerController {
        float value = mScreenAutoBrightnessSpline.interpolate(mAmbientLux);
        float gamma = 1.0f;

        if (DEBUG) {
            Slog.d(TAG, "updateAutoBrightness: mAmbientLux=" + mAmbientLux + " -> value=" + value);
        }

        if (USE_SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT
                && mPowerRequest.screenAutoBrightnessAdjustment != 0.0f) {
            final float adjGamma = FloatMath.pow(SCREEN_AUTO_BRIGHTNESS_ADJUSTMENT_MAX_GAMMA,