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

Commit 93201918 authored by Fiona Campbell's avatar Fiona Campbell
Browse files

Store Brightness Per Display

This changes stores a brightness value per display.

Add brightness value item to persistent data store.
Add BrightnessSettingController to save and retrieve brightnesses.
It is indexed by primary display device of the concerned logical display.
Add method in DisplayManager to expose this.

Bug: 176985835
Bug: 147415200
Bug: 175286226

Test: manual, check "adb shell dumpsys display | grep -A10 Persistent" shows brightness values for each display attached.
Change-Id: Ib8557f8a0f45a5bfb0810967f678015d3f121bb9
parent 80e87134
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package android.hardware.display;
import static android.view.Display.DEFAULT_DISPLAY;

import android.Manifest;
import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.LongDef;
import android.annotation.NonNull;
@@ -921,6 +922,43 @@ public final class DisplayManager {
        mGlobal.setTemporaryBrightness(displayId, brightness);
    }


    /**
     * Sets the brightness of the specified display.
     * <p>
     * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS}
     * permission.
     * </p>
     *
     * @param displayId the logical display id
     * @param brightness The brightness value from 0.0f to 1.0f.
     *
     * @hide
     */
    @RequiresPermission(Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
    public void setBrightness(int displayId, @FloatRange(from = 0f, to = 1f) float brightness) {
        mGlobal.setBrightness(displayId, brightness);
    }


    /**
     * Gets the brightness of the specified display.
     * <p>
     * Requires the {@link android.Manifest.permission#CONTROL_DISPLAY_BRIGHTNESS}
     * permission.
     * </p>
     *
     * @param displayId The display of which brightness value to get from.
     *
     * @hide
     */
    @RequiresPermission(Manifest.permission.CONTROL_DISPLAY_BRIGHTNESS)
    @FloatRange(from = 0f, to = 1f)
    public float getBrightness(int displayId) {
        return mGlobal.getBrightness(displayId);
    }


    /**
     * Temporarily sets the auto brightness adjustment factor.
     * <p>
+31 −0
Original line number Diff line number Diff line
@@ -690,6 +690,37 @@ public final class DisplayManagerGlobal {
        }
    }


    /**
     * Sets the brightness of the display.
     *
     * @param brightness The brightness value from 0.0f to 1.0f.
     *
     * @hide
     */
    public void setBrightness(int displayId, float brightness) {
        try {
            mDm.setBrightness(displayId, brightness);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Gets the brightness of the display.
     *
     * @param displayId The display from which to get the brightness
     *
     * @hide
     */
    public float getBrightness(int displayId) {
        try {
            return mDm.getBrightness(displayId);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Temporarily sets the auto brightness adjustment factor.
     * <p>
+6 −0
Original line number Diff line number Diff line
@@ -119,6 +119,12 @@ interface IDisplayManager {
    // Temporarily sets the display brightness.
    void setTemporaryBrightness(int displayId, float brightness);

    // Saves the display brightness.
    void setBrightness(int displayId, float brightness);

    // Retrieves the display brightness.
    float getBrightness(int displayId);

    // Temporarily sets the auto brightness adjustment factor.
    void setTemporaryAutoBrightnessAdjustment(float adjustment);

+14 −13
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ package com.android.internal.display;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
@@ -28,6 +29,7 @@ import android.os.PowerManager;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.MathUtils;
import android.view.Display;

import java.util.LinkedList;
import java.util.Queue;
@@ -52,6 +54,7 @@ public class BrightnessSynchronizer {
    // This value is approximately 1/3 of the smallest possible brightness value.
    public static final float EPSILON = 0.001f;

    private DisplayManager mDisplayManager;
    private final Context mContext;

    private final Queue<Object> mWriteHistory = new LinkedList<>();
@@ -87,11 +90,15 @@ public class BrightnessSynchronizer {
     * value, if float is invalid. If both are invalid, use default float value from config.
     */
    public void startSynchronizing() {
        if (mDisplayManager == null) {
            mDisplayManager = mContext.getSystemService(DisplayManager.class);
        }

        final BrightnessSyncObserver brightnessSyncObserver;
        brightnessSyncObserver = new BrightnessSyncObserver(mHandler);
        brightnessSyncObserver.startObserving();

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

        if (!Float.isNaN(currentFloatBrightness)) {
@@ -101,9 +108,7 @@ public class BrightnessSynchronizer {
        } else {
            final float defaultBrightness = mContext.getResources().getFloat(
                    com.android.internal.R.dimen.config_screenBrightnessSettingDefaultFloat);
            Settings.System.putFloatForUser(mContext.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_FLOAT, defaultBrightness,
                    UserHandle.USER_CURRENT);
            mDisplayManager.setBrightness(Display.DEFAULT_DISPLAY, defaultBrightness);

        }
    }
@@ -135,7 +140,7 @@ public class BrightnessSynchronizer {
    /**
     * 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 privimite (to preserve precision), but is a value within the
     * Value returned as a float primitive (to preserve precision), but is a value within the
     * int-system range.
     */
    public static float brightnessFloatToIntRange(float brightnessFloat) {
@@ -152,10 +157,8 @@ public class BrightnessSynchronizer {
        }
    }

    private static float getScreenBrightnessFloat(Context context) {
        return Settings.System.getFloatForUser(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_FLOAT, PowerManager.BRIGHTNESS_INVALID_FLOAT,
                UserHandle.USER_CURRENT);
    private float getScreenBrightnessFloat() {
        return mDisplayManager.getBrightness(Display.DEFAULT_DISPLAY);
    }

    private static int getScreenBrightnessInt(Context context) {
@@ -184,9 +187,7 @@ public class BrightnessSynchronizer {
            float newBrightnessFloat = brightnessIntToFloat(value);
            mWriteHistory.offer(newBrightnessFloat);
            mPreferredSettingValue = newBrightnessFloat;
            Settings.System.putFloatForUser(mContext.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_FLOAT, newBrightnessFloat,
                    UserHandle.USER_CURRENT);
            mDisplayManager.setBrightness(Display.DEFAULT_DISPLAY, newBrightnessFloat);
        }
    }

@@ -255,7 +256,7 @@ public class BrightnessSynchronizer {
                mHandler.removeMessages(MSG_UPDATE_FLOAT);
                mHandler.obtainMessage(MSG_UPDATE_FLOAT, currentBrightness, 0).sendToTarget();
            } else if (BRIGHTNESS_FLOAT_URI.equals(uri)) {
                float currentFloat = getScreenBrightnessFloat(mContext);
                float currentFloat = getScreenBrightnessFloat();
                int toSend = Float.floatToIntBits(currentFloat);
                mHandler.removeMessages(MSG_UPDATE_INT);
                mHandler.obtainMessage(MSG_UPDATE_INT, toSend, 0).sendToTarget();
+2 −13
Original line number Diff line number Diff line
@@ -241,15 +241,7 @@ public class BrightnessController implements ToggleSlider.Listener {
        public void run() {
            final float valFloat;
            final boolean inVrMode = mIsVrModeEnabled;
            if (inVrMode) {
                valFloat = Settings.System.getFloatForUser(mContext.getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT, mDefaultBacklightForVr,
                        UserHandle.USER_CURRENT);
            } else {
                valFloat = Settings.System.getFloatForUser(mContext.getContentResolver(),
                        Settings.System.SCREEN_BRIGHTNESS_FLOAT, mDefaultBacklight,
                        UserHandle.USER_CURRENT);
            }
            valFloat = mDisplayManager.getBrightness(mDisplayId);
            // Value is passed as intbits, since this is what the message takes.
            final int valueAsIntBits = Float.floatToIntBits(valFloat);
            mHandler.obtainMessage(MSG_UPDATE_SLIDER, valueAsIntBits,
@@ -364,14 +356,12 @@ public class BrightnessController implements ToggleSlider.Listener {
            metric = MetricsEvent.ACTION_BRIGHTNESS_FOR_VR;
            minBacklight = mMinimumBacklightForVr;
            maxBacklight = mMaximumBacklightForVr;
            settingToChange = Settings.System.SCREEN_BRIGHTNESS_FOR_VR_FLOAT;
        } else {
            metric = mAutomatic
                    ? MetricsEvent.ACTION_BRIGHTNESS_AUTO
                    : MetricsEvent.ACTION_BRIGHTNESS;
            minBacklight = PowerManager.BRIGHTNESS_MIN;
            maxBacklight = PowerManager.BRIGHTNESS_MAX;
            settingToChange = Settings.System.SCREEN_BRIGHTNESS_FLOAT;
        }
        final float valFloat = MathUtils.min(convertGammaToLinearFloat(value,
                minBacklight, maxBacklight),
@@ -386,8 +376,7 @@ public class BrightnessController implements ToggleSlider.Listener {
        if (!tracking) {
            AsyncTask.execute(new Runnable() {
                    public void run() {
                        Settings.System.putFloatForUser(mContext.getContentResolver(),
                                settingToChange, valFloat, UserHandle.USER_CURRENT);
                        mDisplayManager.setBrightness(mDisplayId, valFloat);
                    }
                });
        }
Loading