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

Commit 4e09a180 authored by Fiona Campbell's avatar Fiona Campbell Committed by Automerger Merge Worker
Browse files

Merge "Store Brightness Per Display" into sc-dev am: acc5fbcb

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

Change-Id: I3ad4774db8c45229944aacb982a78c82fea58cee
parents be7e6788 acc5fbcb
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.Display.HdrCapabilities.HdrType;

import android.Manifest;
import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.LongDef;
import android.annotation.NonNull;
@@ -960,6 +961,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
@@ -733,6 +733,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
@@ -128,6 +128,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