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

Commit fce5e5cd authored by Vladimir Komsiyski's avatar Vladimir Komsiyski
Browse files

Support custom virtual display dim brightness

This is twofold:

 -  In PowerManagerService, use the relevant DisplayInfo to get the
    brightness constraints. This is a no-op because both PMS and
    DisplayDeviceConfig use the same resources to get the min/max
    and default brightness. As a bonus, PhoneWindowManager can get
    these constraints for the specific display.

 -  Add a dim brightness API to VirtualDisplayConfig and populate a
    new DisplayInfo field with it, similar to how the default
    brightness is handled. And simply add the relevant displayId to
    DisplayDimModifier.

Fix: 377443330
Test: presubmit
Flag: android.companion.virtualdevice.flags.device_aware_display_power
Change-Id: Iecd62ca0f4bb395bda2221e0b51e40b7e21558c9
parent a217bb28
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -20771,6 +20771,7 @@ package android.hardware.display {
    method public int describeContents();
    method @FlaggedApi("android.companion.virtualdevice.flags.device_aware_display_power") @FloatRange(from=0.0f, to=1.0f) public float getDefaultBrightness();
    method public int getDensityDpi();
    method @FlaggedApi("android.companion.virtualdevice.flags.device_aware_display_power") @FloatRange(from=0.0f, to=1.0f) public float getDimBrightness();
    method @NonNull public java.util.Set<java.lang.String> getDisplayCategories();
    method public int getFlags();
    method public int getHeight();
@@ -20792,6 +20793,7 @@ package android.hardware.display {
    method @NonNull public android.hardware.display.VirtualDisplayConfig build();
    method @FlaggedApi("android.companion.virtualdevice.flags.device_aware_display_power") @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setBrightnessListener(@NonNull java.util.concurrent.Executor, @NonNull android.hardware.display.VirtualDisplayConfig.BrightnessListener);
    method @FlaggedApi("android.companion.virtualdevice.flags.device_aware_display_power") @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setDefaultBrightness(@FloatRange(from=0.0f, to=1.0f) float);
    method @FlaggedApi("android.companion.virtualdevice.flags.device_aware_display_power") @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setDimBrightness(@FloatRange(from=0.0f, to=1.0f) float);
    method @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setDisplayCategories(@NonNull java.util.Set<java.lang.String>);
    method @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setFlags(int);
    method @NonNull public android.hardware.display.VirtualDisplayConfig.Builder setRequestedRefreshRate(@FloatRange(from=0.0f) float);
+65 −5
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ public final class VirtualDisplayConfig implements Parcelable {
    private final DisplayCutout mDisplayCutout;
    private final boolean mIgnoreActivitySizeRestrictions;
    private final float mDefaultBrightness;
    private final float mDimBrightness;
    private final IBrightnessListener mBrightnessListener;

    private VirtualDisplayConfig(
@@ -84,6 +85,7 @@ public final class VirtualDisplayConfig implements Parcelable {
            @Nullable DisplayCutout displayCutout,
            boolean ignoreActivitySizeRestrictions,
            @FloatRange(from = 0.0f, to = 1.0f) float defaultBrightness,
            @FloatRange(from = 0.0f, to = 1.0f) float dimBrightness,
            IBrightnessListener brightnessListener) {
        mName = name;
        mWidth = width;
@@ -100,6 +102,7 @@ public final class VirtualDisplayConfig implements Parcelable {
        mDisplayCutout = displayCutout;
        mIgnoreActivitySizeRestrictions = ignoreActivitySizeRestrictions;
        mDefaultBrightness = defaultBrightness;
        mDimBrightness = dimBrightness;
        mBrightnessListener = brightnessListener;
    }

@@ -179,6 +182,19 @@ public final class VirtualDisplayConfig implements Parcelable {
        return mDefaultBrightness;
    }

    /**
     * Returns the dim brightness of the display.
     *
     * <p>Value of {@code 0.0} indicates the minimum supported brightness and value of {@code 1.0}
     * indicates the maximum supported brightness.</p>
     *
     * @see Builder#setDimBrightness(float)
     */
    @FlaggedApi(android.companion.virtualdevice.flags.Flags.FLAG_DEVICE_AWARE_DISPLAY_POWER)
    public @FloatRange(from = 0.0f, to = 1.0f) float getDimBrightness() {
        return mDimBrightness;
    }

    /**
     * Returns the listener to get notified about changes in the display brightness.
     * @hide
@@ -278,6 +294,7 @@ public final class VirtualDisplayConfig implements Parcelable {
        DisplayCutout.ParcelableWrapper.writeCutoutToParcel(mDisplayCutout, dest, flags);
        dest.writeBoolean(mIgnoreActivitySizeRestrictions);
        dest.writeFloat(mDefaultBrightness);
        dest.writeFloat(mDimBrightness);
        dest.writeStrongBinder(mBrightnessListener != null ? mBrightnessListener.asBinder() : null);
    }

@@ -308,8 +325,8 @@ public final class VirtualDisplayConfig implements Parcelable {
                && mIgnoreActivitySizeRestrictions == that.mIgnoreActivitySizeRestrictions
                && Objects.equals(mDisplayCutout, that.mDisplayCutout)
                && mDefaultBrightness == that.mDefaultBrightness
                && mDimBrightness == that.mDimBrightness
                && Objects.equals(mBrightnessListener, that.mBrightnessListener);

    }

    @Override
@@ -318,7 +335,8 @@ public final class VirtualDisplayConfig implements Parcelable {
                mName, mWidth, mHeight, mDensityDpi, mFlags, mSurface, mUniqueId,
                mDisplayIdToMirror, mWindowManagerMirroringEnabled, mDisplayCategories,
                mRequestedRefreshRate, mIsHomeSupported, mDisplayCutout,
                mIgnoreActivitySizeRestrictions, mDefaultBrightness, mBrightnessListener);
                mIgnoreActivitySizeRestrictions, mDefaultBrightness, mDimBrightness,
                mBrightnessListener);
        return hashCode;
    }

@@ -341,6 +359,7 @@ public final class VirtualDisplayConfig implements Parcelable {
                + " mDisplayCutout=" + mDisplayCutout
                + " mIgnoreActivitySizeRestrictions=" + mIgnoreActivitySizeRestrictions
                + " mDefaultBrightness=" + mDefaultBrightness
                + " mDimBrightness=" + mDimBrightness
                + ")";
    }

@@ -360,8 +379,8 @@ public final class VirtualDisplayConfig implements Parcelable {
        mDisplayCutout = DisplayCutout.ParcelableWrapper.readCutoutFromParcel(in);
        mIgnoreActivitySizeRestrictions = in.readBoolean();
        mDefaultBrightness = in.readFloat();
        mDimBrightness = in.readFloat();
        mBrightnessListener = IBrightnessListener.Stub.asInterface(in.readStrongBinder());

    }

    /**
@@ -432,6 +451,7 @@ public final class VirtualDisplayConfig implements Parcelable {
        private DisplayCutout mDisplayCutout = null;
        private boolean mIgnoreActivitySizeRestrictions = false;
        private float mDefaultBrightness = 0.0f;
        private float mDimBrightness = PowerManager.BRIGHTNESS_INVALID;
        private IBrightnessListener mBrightnessListener = null;

        /**
@@ -635,14 +655,14 @@ public final class VirtualDisplayConfig implements Parcelable {
         *
         * <p>If unset, defaults to {@code 0.0}</p>
         *
         * @throws IllegalArgumentException if the brightness is outside the valid range [0.0, 1.0]
         * @see android.view.View#setKeepScreenOn(boolean)
         * @see #setBrightnessListener(Executor, BrightnessListener)
         */
        @FlaggedApi(android.companion.virtualdevice.flags.Flags.FLAG_DEVICE_AWARE_DISPLAY_POWER)
        @NonNull
        public Builder setDefaultBrightness(@FloatRange(from = 0.0f, to = 1.0f) float brightness) {
            if (brightness < PowerManager.BRIGHTNESS_MIN
                    || brightness > PowerManager.BRIGHTNESS_MAX) {
            if (!isValidBrightness(brightness)) {
                throw new IllegalArgumentException(
                        "Virtual display default brightness must be in range [0.0, 1.0]");
            }
@@ -650,6 +670,33 @@ public final class VirtualDisplayConfig implements Parcelable {
            return this;
        }

        /**
         * Sets the dim brightness of the display.
         *
         * <p>The system will use this brightness value whenever the display should be dim, i.e.
         * it is powered on and dimmed due to user activity or app activity.</p>
         *
         * <p>Value of {@code 0.0} indicates the minimum supported brightness and value of
         * {@code 1.0} indicates the maximum supported brightness.</p>
         *
         * <p>If set, the default brightness must also be set to a value greater or equal to the
         * dim brightness. If unset, defaults to the system default.</p>
         *
         * @throws IllegalArgumentException if the brightness is outside the valid range [0.0, 1.0]
         * @see Builder#setDefaultBrightness(float)
         * @see #setBrightnessListener(Executor, BrightnessListener)
         */
        @FlaggedApi(android.companion.virtualdevice.flags.Flags.FLAG_DEVICE_AWARE_DISPLAY_POWER)
        @NonNull
        public Builder setDimBrightness(@FloatRange(from = 0.0f, to = 1.0f) float brightness) {
            if (!isValidBrightness(brightness)) {
                throw new IllegalArgumentException(
                        "Virtual display dim brightness must be in range [0.0, 1.0]");
            }
            mDimBrightness = brightness;
            return this;
        }

        /**
         * Sets the listener to get notified about changes in the display brightness.
         *
@@ -666,11 +713,23 @@ public final class VirtualDisplayConfig implements Parcelable {
            return this;
        }

        private boolean isValidBrightness(float brightness) {
            return !Float.isNaN(brightness) && PowerManager.BRIGHTNESS_MIN <= brightness
                    && brightness <= PowerManager.BRIGHTNESS_MAX;
        }

        /**
         * Builds the {@link VirtualDisplayConfig} instance.
         *
         * @throws IllegalArgumentException if the dim brightness is set to a value greater than
         *   the default brightness.
         */
        @NonNull
        public VirtualDisplayConfig build() {
            if (isValidBrightness(mDimBrightness) && mDimBrightness > mDefaultBrightness) {
                throw new IllegalArgumentException(
                        "The dim brightness must not be greater than the default brightness");
            }
            return new VirtualDisplayConfig(
                    mName,
                    mWidth,
@@ -687,6 +746,7 @@ public final class VirtualDisplayConfig implements Parcelable {
                    mDisplayCutout,
                    mIgnoreActivitySizeRestrictions,
                    mDefaultBrightness,
                    mDimBrightness,
                    mBrightnessListener);
        }
    }
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ interface IPowerManager
    void goToSleepWithDisplayId(int displayId, long time, int reason, int flags);
    @UnsupportedAppUsage(maxTargetSdk = 28)
    void nap(long time);
    float getBrightnessConstraint(int constraint);
    float getBrightnessConstraint(int displayId, int constraint);
    @UnsupportedAppUsage
    boolean isInteractive();
    boolean isDisplayInteractive(int displayId);
+10 −2
Original line number Diff line number Diff line
@@ -1266,9 +1266,17 @@ public final class PowerManager {
     * @hide
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
    public float getBrightnessConstraint(int constraint) {
    public float getBrightnessConstraint(@BrightnessConstraint int constraint) {
        return getBrightnessConstraint(Display.DEFAULT_DISPLAY, constraint);
    }

    /**
     * Gets a float screen brightness setting for a specific display.
     * @hide
     */
    public float getBrightnessConstraint(int displayId, @BrightnessConstraint int constraint) {
        try {
            return mService.getBrightnessConstraint(constraint);
            return mService.getBrightnessConstraint(displayId, constraint);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
+12 −0
Original line number Diff line number Diff line
@@ -360,6 +360,12 @@ public final class DisplayInfo implements Parcelable {
     */
    public float brightnessDefault;

    /**
     * The current dim brightness of the display. Value between 0.0 and 1.0,
     * derived from the configuration of the display device of this logical display.
     */
    public float brightnessDim;

    /**
     * The {@link RoundedCorners} if present, otherwise {@code null}.
     */
@@ -479,6 +485,7 @@ public final class DisplayInfo implements Parcelable {
                && brightnessMinimum == other.brightnessMinimum
                && brightnessMaximum == other.brightnessMaximum
                && brightnessDefault == other.brightnessDefault
                && brightnessDim == other.brightnessDim
                && Objects.equals(roundedCorners, other.roundedCorners)
                && installOrientation == other.installOrientation
                && Objects.equals(displayShape, other.displayShape)
@@ -546,6 +553,7 @@ public final class DisplayInfo implements Parcelable {
        brightnessMinimum = other.brightnessMinimum;
        brightnessMaximum = other.brightnessMaximum;
        brightnessDefault = other.brightnessDefault;
        brightnessDim = other.brightnessDim;
        roundedCorners = other.roundedCorners;
        installOrientation = other.installOrientation;
        displayShape = other.displayShape;
@@ -620,6 +628,7 @@ public final class DisplayInfo implements Parcelable {
        brightnessMinimum = source.readFloat();
        brightnessMaximum = source.readFloat();
        brightnessDefault = source.readFloat();
        brightnessDim = source.readFloat();
        roundedCorners = source.readTypedObject(RoundedCorners.CREATOR);
        int numUserDisabledFormats = source.readInt();
        userDisabledHdrTypes = new int[numUserDisabledFormats];
@@ -696,6 +705,7 @@ public final class DisplayInfo implements Parcelable {
        dest.writeFloat(brightnessMinimum);
        dest.writeFloat(brightnessMaximum);
        dest.writeFloat(brightnessDefault);
        dest.writeFloat(brightnessDim);
        dest.writeTypedObject(roundedCorners, flags);
        dest.writeInt(userDisabledHdrTypes.length);
        for (int i = 0; i < userDisabledHdrTypes.length; i++) {
@@ -994,6 +1004,8 @@ public final class DisplayInfo implements Parcelable {
        sb.append(brightnessMaximum);
        sb.append(", brightnessDefault ");
        sb.append(brightnessDefault);
        sb.append(", brightnessDim ");
        sb.append(brightnessDim);
        sb.append(", installOrientation ");
        sb.append(Surface.rotationToString(installOrientation));
        sb.append(", layoutLimitedRefreshRate ");
Loading