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

Commit 28c5f05a authored by Santos Cordon's avatar Santos Cordon Committed by Automerger Merge Worker
Browse files

Merge "SCREEN_BRIGHTNESS - map 255 to current max" into sc-dev am: e4e836a1

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15359296

Change-Id: I0e027bc0ecbdea29c8b86c1785dc514e69efdff8
parents 0485ac6b e4e836a1
Loading
Loading
Loading
Loading
+57 −33
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package com.android.internal.display;

import android.annotation.NonNull;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.hardware.display.BrightnessInfo;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.net.Uri;
@@ -61,10 +63,10 @@ public class BrightnessSynchronizer {
                    updateBrightnessFloatFromInt(msg.arg1);
                    break;
                case MSG_UPDATE_INT:
                    updateBrightnessIntFromFloat(Float.intBitsToFloat(msg.arg1));
                    updateBrightnessIntFromFloat((BrightnessInfo) msg.obj);
                    break;
                case MSG_UPDATE_BOTH:
                    updateBoth(Float.intBitsToFloat(msg.arg1));
                    updateBoth((BrightnessInfo) msg.obj, Float.intBitsToFloat(msg.arg1));
                    break;
                default:
                    super.handleMessage(msg);
@@ -95,11 +97,11 @@ public class BrightnessSynchronizer {
        brightnessSyncObserver = new BrightnessSyncObserver();
        brightnessSyncObserver.startObserving();

        final float currentFloatBrightness = getScreenBrightnessFloat();
        final BrightnessInfo brightnessInfo = getBrightnessInfo();
        final int currentIntBrightness = getScreenBrightnessInt(mContext);

        if (!Float.isNaN(currentFloatBrightness)) {
            updateBrightnessIntFromFloat(currentFloatBrightness);
        if (brightnessInfo != null && !Float.isNaN(brightnessInfo.brightness)) {
            updateBrightnessIntFromFloat(brightnessInfo);
        } else if (currentIntBrightness != -1) {
            updateBrightnessFloatFromInt(currentIntBrightness);
        } else {
@@ -112,45 +114,52 @@ public class BrightnessSynchronizer {

    /**
     * Converts between the int brightness system and the float brightness system.
     *
     * @param brightnessInt The int brightness value to convert.
     */
    public static float brightnessIntToFloat(int brightnessInt) {
        return brightnessIntToFloat(brightnessInt, null);
    }

    private static float brightnessIntToFloat(int brightnessInt, BrightnessInfo info) {
        if (brightnessInt == PowerManager.BRIGHTNESS_OFF) {
            return PowerManager.BRIGHTNESS_OFF_FLOAT;
        } else if (brightnessInt == PowerManager.BRIGHTNESS_INVALID) {
            return PowerManager.BRIGHTNESS_INVALID_FLOAT;
        } else {
            final float minFloat = PowerManager.BRIGHTNESS_MIN;
            final float maxFloat = PowerManager.BRIGHTNESS_MAX;
            final float minFloat = info != null
                    ? info.brightnessMinimum : PowerManager.BRIGHTNESS_MIN;
            final float maxFloat = info != null
                    ? info.brightnessMaximum : PowerManager.BRIGHTNESS_MAX;
            final float minInt = PowerManager.BRIGHTNESS_OFF + 1;
            final float maxInt = PowerManager.BRIGHTNESS_ON;
            return MathUtils.constrainedMap(minFloat, maxFloat, minInt, maxInt, brightnessInt);
        }
    }

    /**
     * Converts between the float brightness system and the int brightness system.
     */
    public static int brightnessFloatToInt(float brightnessFloat) {
        return Math.round(brightnessFloatToIntRange(brightnessFloat));
    }

    /**
     * Translates specified value from the float brightness system to the int brightness system,
     * given the min/max of each range. Accounts for special values such as OFF and invalid values.
     * Value returned as a float primitive (to preserve precision), but is a value within the
     * int-system range.
     *
     * @param brightnessFloat The float brightness value to convert.
     * @param info Brightness information to use in the conversion.
     */
    public static float brightnessFloatToIntRange(float brightnessFloat) {
    public static int brightnessFloatToInt(float brightnessFloat, BrightnessInfo info) {
        if (floatEquals(brightnessFloat, PowerManager.BRIGHTNESS_OFF_FLOAT)) {
            return PowerManager.BRIGHTNESS_OFF;
        } else if (Float.isNaN(brightnessFloat)) {
            return PowerManager.BRIGHTNESS_INVALID;
        } else {
            final float minFloat = PowerManager.BRIGHTNESS_MIN;
            final float maxFloat = PowerManager.BRIGHTNESS_MAX;
            final float minFloat = info != null
                    ? info.brightnessMinimum : PowerManager.BRIGHTNESS_MIN;
            final float maxFloat = info != null
                    ? info.brightnessMaximum : PowerManager.BRIGHTNESS_MAX;
            final float minInt = PowerManager.BRIGHTNESS_OFF + 1;
            final float maxInt = PowerManager.BRIGHTNESS_ON;
            return MathUtils.constrainedMap(minInt, maxInt, minFloat, maxFloat, brightnessFloat);
            return Math.round(MathUtils.constrainedMap(minInt, maxInt, minFloat, maxFloat,
                    brightnessFloat));
        }
    }

@@ -185,35 +194,37 @@ public class BrightnessSynchronizer {
     * @param value Brightness value as int to store in the float setting.
     */
    private void updateBrightnessFloatFromInt(int value) {
        if (brightnessFloatToInt(mPreferredSettingValue) == value) {
        final BrightnessInfo info = getBrightnessInfo();
        if (brightnessFloatToInt(mPreferredSettingValue, info) == value) {
            return;
        }

        mPreferredSettingValue = brightnessIntToFloat(value);
        mPreferredSettingValue = brightnessIntToFloat(value, info);
        final int newBrightnessAsIntBits = Float.floatToIntBits(mPreferredSettingValue);
        mHandler.removeMessages(MSG_UPDATE_BOTH);
        mHandler.obtainMessage(MSG_UPDATE_BOTH, newBrightnessAsIntBits, 0).sendToTarget();
    }

    /**
     * Updates the settings based on a passed in float value. This is called whenever the float
     * setting changes. mPreferredSettingValue holds the most recently updated brightness value
     * as a float that we would like the display to be set to.
     * Updates the settings from the specified {@link BrightnessInfo}. This is called whenever the
     * float brightness changed from DisplayManager. mPreferredSettingValue holds the most recently
     * updated brightness value as a float that we would like the display to be set to.
     *
     * We then schedule an update to both the int and float settings, but, remove all the other
     * messages to update all, to prevent us getting stuck in a loop.
     *
     * @param value Brightness setting as float to store in int setting.
     * @param brightnessInfo Current brightness information
     */
    private void updateBrightnessIntFromFloat(float value) {
    private void updateBrightnessIntFromFloat(@NonNull BrightnessInfo brightnessInfo) {
        final float value = brightnessInfo.brightness;
        if (floatEquals(mPreferredSettingValue, value)) {
            return;
        }

        mPreferredSettingValue = value;
        final int newBrightnessAsIntBits = Float.floatToIntBits(mPreferredSettingValue);
        mHandler.removeMessages(MSG_UPDATE_BOTH);
        mHandler.obtainMessage(MSG_UPDATE_BOTH, newBrightnessAsIntBits, 0).sendToTarget();
        mHandler.obtainMessage(MSG_UPDATE_BOTH, Float.floatToIntBits(value), 0, brightnessInfo)
                .sendToTarget();
    }


@@ -222,16 +233,24 @@ public class BrightnessSynchronizer {
     * mDisplayManager.setBrightness automatically checks for changes
     * Settings.System.putIntForUser needs to be checked, to prevent an extra callback to this class
     *
     * @param brightnessInfo Brightness information, takes precedent over newBrightnessFloat
     * @param newBrightnessFloat Brightness setting as float to store in both settings
     */
    private void updateBoth(float newBrightnessFloat) {
        int newBrightnessInt = brightnessFloatToInt(newBrightnessFloat);
    private void updateBoth(BrightnessInfo brightnessInfo, float newBrightnessFloat) {
        int newBrightnessInt = brightnessFloatToInt(newBrightnessFloat, brightnessInfo);
        mDisplayManager.setBrightness(Display.DEFAULT_DISPLAY, newBrightnessFloat);
        if (getScreenBrightnessInt(mContext) != newBrightnessInt) {
            Settings.System.putIntForUser(mContext.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS, newBrightnessInt, UserHandle.USER_CURRENT);
        }
    }

    private BrightnessInfo getBrightnessInfo() {
        final Display display = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY);
        if (display != null) {
            return display.getBrightnessInfo();
        }
        return null;
    }

    /**
@@ -263,10 +282,15 @@ public class BrightnessSynchronizer {

            @Override
            public void onDisplayChanged(int displayId) {
                float currentFloat = getScreenBrightnessFloat();
                int toSend = Float.floatToIntBits(currentFloat);
                if (displayId != Display.DEFAULT_DISPLAY) {
                    return;
                }

                final BrightnessInfo info = getBrightnessInfo();
                if (info != null) {
                    mHandler.removeMessages(MSG_UPDATE_INT);
                mHandler.obtainMessage(MSG_UPDATE_INT, toSend, 0).sendToTarget();
                    mHandler.obtainMessage(MSG_UPDATE_INT, info).sendToTarget();
                }
            }
        };

+3 −2
Original line number Diff line number Diff line
@@ -354,9 +354,10 @@ public class BrightnessController implements ToggleSlider.Listener {
                convertGammaToLinearFloat(value, minBacklight, maxBacklight),
                maxBacklight);
        if (stopTracking) {
            // TODO(brightnessfloat): change to use float value instead.
            // Log brightness as a value between 0-1000 directly correlated to brightnesses 0-1.0
            MetricsLogger.action(mContext, metric,
                    BrightnessSynchronizer.brightnessFloatToInt(valFloat));
                    Math.round(MathUtils.constrainedMap(0, 1000, PowerManager.BRIGHTNESS_MIN,
                        PowerManager.BRIGHTNESS_MAX, valFloat)));

        }
        setBrightness(valFloat);
+1 −1
Original line number Diff line number Diff line
@@ -2342,7 +2342,7 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            try {
                // TODO(brightnessfloat): change BatteryStats to use float
                mBatteryStats.noteScreenBrightness(BrightnessSynchronizer.brightnessFloatToInt(
                        brightness));
                        brightness, null));
            } catch (RemoteException e) {
                // same process
            }
+3 −2
Original line number Diff line number Diff line
@@ -795,11 +795,12 @@ final class LocalDisplayAdapter extends DisplayAdapter {
                            mBacklightAdapter.setBacklight(sdrBacklight, sdrNits, backlight, nits);
                            Trace.traceCounter(Trace.TRACE_TAG_POWER,
                                    "ScreenBrightness",
                                    BrightnessSynchronizer.brightnessFloatToInt(brightnessState));
                                    BrightnessSynchronizer.brightnessFloatToInt(
                                            brightnessState, null));
                            Trace.traceCounter(Trace.TRACE_TAG_POWER,
                                    "SdrScreenBrightness",
                                    BrightnessSynchronizer.brightnessFloatToInt(
                                            sdrBrightnessState));
                                            sdrBrightnessState, null));
                        } finally {
                            Trace.traceEnd(Trace.TRACE_TAG_POWER);
                        }
+1 −1
Original line number Diff line number Diff line
@@ -293,7 +293,7 @@ public class LightsService extends SystemService {
                            + ": brightness=" + brightness);
                    return;
                }
                int brightnessInt = BrightnessSynchronizer.brightnessFloatToInt(brightness);
                int brightnessInt = BrightnessSynchronizer.brightnessFloatToInt(brightness, null);
                int color = brightnessInt & 0x000000ff;
                color = 0xff000000 | (color << 16) | (color << 8) | color;
                setLightLocked(color, LIGHT_FLASH_NONE, 0, 0, brightnessMode);