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

Commit 9bcb8485 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Set brightness percentage" into main

parents 7857d5ab 3dcfaac7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -21052,8 +21052,10 @@ package android.hardware.display {
    method public void registerDisplayListener(android.hardware.display.DisplayManager.DisplayListener, android.os.Handler);
    method @FlaggedApi("com.android.server.display.feature.flags.display_listener_performance_improvements") public void registerDisplayListener(@NonNull java.util.concurrent.Executor, long, @NonNull android.hardware.display.DisplayManager.DisplayListener);
    method @FlaggedApi("com.android.server.display.feature.flags.display_topology_api") @RequiresPermission("android.permission.MANAGE_DISPLAYS") public void registerTopologyListener(@NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.hardware.display.DisplayTopology>);
    method @FlaggedApi("com.android.server.display.feature.flags.set_brightness_by_unit") @RequiresPermission(android.Manifest.permission.WRITE_SETTINGS) public void setBrightness(int, float, int);
    method public void unregisterDisplayListener(android.hardware.display.DisplayManager.DisplayListener);
    method @FlaggedApi("com.android.server.display.feature.flags.display_topology_api") @RequiresPermission("android.permission.MANAGE_DISPLAYS") public void unregisterTopologyListener(@NonNull java.util.function.Consumer<android.hardware.display.DisplayTopology>);
    field @FlaggedApi("com.android.server.display.feature.flags.set_brightness_by_unit") public static final int BRIGHTNESS_UNIT_PERCENTAGE = 0; // 0x0
    field @FlaggedApi("com.android.server.display.feature.flags.display_category_built_in") public static final String DISPLAY_CATEGORY_BUILT_IN_DISPLAYS = "android.hardware.display.category.BUILT_IN_DISPLAYS";
    field public static final String DISPLAY_CATEGORY_PRESENTATION = "android.hardware.display.category.PRESENTATION";
    field @FlaggedApi("com.android.server.display.feature.flags.display_listener_performance_improvements") public static final long EVENT_TYPE_DISPLAY_ADDED = 1L; // 0x1L
+53 −0
Original line number Diff line number Diff line
@@ -714,6 +714,36 @@ public final class DisplayManager {
     */
    public static final long PRIVATE_EVENT_TYPE_DISPLAY_COMMITTED_STATE_CHANGED = 1L << 3;

    /**
     * Brightness value type where the value is in the range [0, 100] and when set, the brightness
     * UI slider will show the same value. It gets converted from the user-perception scale to
     * the power scale and mapped to the current brightness range. The current brightness range may
     * change over time depending on the device state (such as the brightness of the ambient
     * environment). When this type of used, 0 and 100 map to the current brightness minimum and
     * maximum respectively.
     */
    @FlaggedApi(Flags.FLAG_SET_BRIGHTNESS_BY_UNIT)
    public static final int BRIGHTNESS_UNIT_PERCENTAGE = 0;

    /**
     * @hide
     */
    @IntDef(prefix = { "BRIGHTNESS_UNIT_" }, value = {
            BRIGHTNESS_UNIT_PERCENTAGE
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BrightnessUnit {}

    /**
     * @hide
     */
    public static String brightnessUnitToString(@BrightnessUnit int unit) {
        if (Flags.setBrightnessByUnit() && unit == BRIGHTNESS_UNIT_PERCENTAGE) {
            return "percentage";
        } else {
            throw new IllegalStateException("Unexpected value: " + unit);
        }
    }

    /** @hide */
    public DisplayManager(Context context) {
@@ -1549,6 +1579,18 @@ public final class DisplayManager {
        mGlobal.setBrightness(displayId, brightness);
    }

    /**
     * Sets the brightness of the specified display. Accepts different brightness units.
     * @param displayId The logical display ID
     * @param value The brightness value to set
     * @param unit The unit of the brightness value
     */
    @FlaggedApi(Flags.FLAG_SET_BRIGHTNESS_BY_UNIT)
    @RequiresPermission(Manifest.permission.WRITE_SETTINGS)
    public void setBrightness(int displayId, float value, @BrightnessUnit int unit) {
        mGlobal.setBrightness(displayId, value, unit);
    }


    /**
     * Gets the brightness of the specified display.
@@ -1567,6 +1609,17 @@ public final class DisplayManager {
        return mGlobal.getBrightness(displayId);
    }

    /**
     * Gets the brightness of the specified display in the specified brightness unit.
     * @param displayId The display of which brightness value to get from.
     * @param unit The unit of the brightness value
     *
     * @hide
     */
    public float getBrightness(int displayId, @BrightnessUnit int unit) {
        return mGlobal.getBrightness(displayId, unit);
    }


    /**
     * Temporarily sets the auto brightness adjustment factor.
+24 −0
Original line number Diff line number Diff line
@@ -1136,6 +1136,19 @@ public final class DisplayManagerGlobal {
        }
    }

    /**
     * @see DisplayManager#setBrightness(int, float, int)
     */
    @RequiresPermission(Manifest.permission.WRITE_SETTINGS)
    public void setBrightness(int displayId, float value,
            @DisplayManager.BrightnessUnit int unit) {
        try {
            mDm.setBrightnessByUnit(displayId, value, unit);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Report whether/how the display supports DISPLAY_DECORATION.
     *
@@ -1166,6 +1179,17 @@ public final class DisplayManagerGlobal {
        }
    }

    /**
     * @see DisplayManager#getBrightness(int, int)
     */
    public float getBrightness(int displayId, @DisplayManager.BrightnessUnit int unit) {
        try {
            return mDm.getBrightnessByUnit(displayId, unit);
        } catch (RemoteException ex) {
            throw ex.rethrowFromSystemServer();
        }
    }

    /**
     * Temporarily sets the auto brightness adjustment factor.
     * <p>
+7 −0
Original line number Diff line number Diff line
@@ -171,10 +171,17 @@ interface IDisplayManager {
    @EnforcePermission("CONTROL_DISPLAY_BRIGHTNESS")
    void setBrightness(int displayId, float brightness);

    // Set the display brightness. Accepts different brightness units.
    @EnforcePermission("WRITE_SETTINGS")
    void setBrightnessByUnit(int displayId, float value, int unit);

    // Retrieves the display brightness.
    @EnforcePermission("CONTROL_DISPLAY_BRIGHTNESS")
    float getBrightness(int displayId);

    // Retrieves the display brightness in the specified brightness unit.
    float getBrightnessByUnit(int displayId, int unit);

    // Temporarily sets the auto brightness adjustment factor.
    @EnforcePermission("CONTROL_DISPLAY_BRIGHTNESS")
    void setTemporaryAutoBrightnessAdjustment(float adjustment);
+107 −33
Original line number Diff line number Diff line
@@ -27,9 +27,11 @@ import static android.Manifest.permission.INTERNAL_SYSTEM_WINDOW;
import static android.Manifest.permission.MANAGE_DISPLAYS;
import static android.Manifest.permission.MODIFY_HDR_CONVERSION_MODE;
import static android.Manifest.permission.RESTRICT_DISPLAY_MODES;
import static android.Manifest.permission.WRITE_SETTINGS;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE;
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE;
import static android.hardware.display.DisplayManager.BRIGHTNESS_UNIT_PERCENTAGE;
import static android.hardware.display.DisplayManagerGlobal.InternalEventFlag;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_ALWAYS_UNLOCKED;
import static android.hardware.display.DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR;
@@ -172,6 +174,7 @@ import android.window.ScreenCapture;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.display.BrightnessSynchronizer;
import com.android.internal.display.BrightnessUtils;
import com.android.internal.foldables.FoldLockSettingAvailabilityProvider;
import com.android.internal.os.BackgroundThread;
import com.android.internal.util.ArrayUtils;
@@ -4199,6 +4202,47 @@ public final class DisplayManagerService extends SystemService {
        return config;
    }

    private BrightnessInfo getBrightnessInfoInternal(int displayId) {
        synchronized (mSyncRoot) {
            LogicalDisplay display = mLogicalDisplayMapper.getDisplayLocked(
                    displayId, /* includeDisabled= */ false);
            if (display == null || !display.isEnabledLocked()) {
                return null;
            }
            DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
            if (dpc != null) {
                return dpc.getBrightnessInfo();
            }
        }
        return null;
    }

    private float getBrightnessInternal(int displayId) {
        synchronized (mSyncRoot) {
            DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
            if (dpc != null) {
                return dpc.getScreenBrightnessSetting();
            } else {
                return PowerManager.BRIGHTNESS_INVALID_FLOAT;
            }
        }
    }

    private void setBrightnessInternal(int displayId, float brightness) {
        if (Float.isNaN(brightness)) {
            Slog.w(TAG, "Attempted to set invalid brightness: " + brightness);
            return;
        }
        MathUtils.constrain(brightness, PowerManager.BRIGHTNESS_MIN, PowerManager.BRIGHTNESS_MAX);
        synchronized (mSyncRoot) {
            DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
            if (dpc != null) {
                dpc.setBrightness(brightness);
            }
            mPersistentDataStore.saveIfNeeded();
        }
    }

    private final class DisplayManagerHandler extends Handler {
        public DisplayManagerHandler(Looper looper) {
            super(looper, null, true /*async*/);
@@ -5327,21 +5371,10 @@ public final class DisplayManagerService extends SystemService {
            getBrightnessInfo_enforcePermission();
            final long token = Binder.clearCallingIdentity();
            try {
                synchronized (mSyncRoot) {
                    LogicalDisplay display = mLogicalDisplayMapper.getDisplayLocked(
                            displayId, /* includeDisabled= */ false);
                    if (display == null || !display.isEnabledLocked()) {
                        return null;
                    }
                    DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc != null) {
                        return dpc.getBrightnessInfo();
                    }
                }
                return getBrightnessInfoInternal(displayId);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
            return null;
        }

        @Override // Binder call
@@ -5371,23 +5404,45 @@ public final class DisplayManagerService extends SystemService {
        @Override // Binder call
        public void setBrightness(int displayId, float brightness) {
            setBrightness_enforcePermission();
            if (Float.isNaN(brightness)) {
                Slog.w(TAG, "Attempted to set invalid brightness: " + brightness);
                return;
            }
            MathUtils.constrain(brightness, PowerManager.BRIGHTNESS_MIN,
                    PowerManager.BRIGHTNESS_MAX);
            final long token = Binder.clearCallingIdentity();
            try {
                setBrightnessInternal(displayId, brightness);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @EnforcePermission(WRITE_SETTINGS)
        @Override // Binder call
        public void setBrightnessByUnit(int displayId, float value,
                @DisplayManager.BrightnessUnit int unit) {
            setBrightnessByUnit_enforcePermission();
            synchronized (mSyncRoot) {
                    DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc != null) {
                        dpc.setBrightness(brightness);
                float brightnessFloat;
                if (unit == BRIGHTNESS_UNIT_PERCENTAGE) {
                    if (value < 0 || value > 100) {
                        throw new IllegalArgumentException(
                                "Attempted to set invalid brightness percentage: " + value + "%");
                    }
                    mPersistentDataStore.saveIfNeeded();

                    BrightnessInfo info = getBrightnessInfoInternal(displayId);
                    if (info == null) {
                        Slog.w(TAG,
                                "setBrightnessByUnit: no BrightnessInfo for display "
                                        + displayId);
                        return;
                    }
            } finally {
                Binder.restoreCallingIdentity(token);

                    // Convert from user-perception to power-linear scale
                    float linearBrightness = BrightnessUtils.convertGammaToLinear(value / 100);

                    // Interpolate to the range [currentlyAllowedMin, currentlyAllowedMax]
                    brightnessFloat = MathUtils.lerp(info.brightnessMinimum, info.brightnessMaximum,
                            linearBrightness);
                } else {
                    throw new IllegalArgumentException("Invalid brightness unit: " + unit);
                }
                setBrightnessInternal(displayId, brightnessFloat);
            }
        }

@@ -5395,19 +5450,38 @@ public final class DisplayManagerService extends SystemService {
        @Override // Binder call
        public float getBrightness(int displayId) {
            getBrightness_enforcePermission();
            float brightness = PowerManager.BRIGHTNESS_INVALID_FLOAT;
            final long token = Binder.clearCallingIdentity();
            try {
                return getBrightnessInternal(displayId);
            } finally {
                Binder.restoreCallingIdentity(token);
            }
        }

        @Override // Binder call
        public float getBrightnessByUnit(int displayId, @DisplayManager.BrightnessUnit int unit) {
            synchronized (mSyncRoot) {
                    DisplayPowerController dpc = mDisplayPowerControllers.get(displayId);
                    if (dpc != null) {
                        brightness = dpc.getScreenBrightnessSetting();
                if (unit == BRIGHTNESS_UNIT_PERCENTAGE) {
                    BrightnessInfo info = getBrightnessInfoInternal(displayId);
                    if (info == null) {
                        Slog.w(TAG,
                                "getBrightnessByUnit: no BrightnessInfo for display "
                                        + displayId);
                        return PowerManager.BRIGHTNESS_INVALID;
                    }
                    float brightnessFloat = getBrightnessInternal(displayId);
                    float normalizedBrightness = MathUtils.norm(info.brightnessMinimum,
                            info.brightnessMaximum, brightnessFloat);

                    // Convert from power-linear scale to user-perception
                    float gammaBrightness = BrightnessUtils.convertLinearToGamma(
                            normalizedBrightness);

                    return gammaBrightness * 100;
                } else {
                    throw new IllegalArgumentException("Invalid brightness unit: " + unit);
                }
            } finally {
                Binder.restoreCallingIdentity(token);
            }
            return brightness;
        }

        @EnforcePermission(CONTROL_DISPLAY_BRIGHTNESS)
Loading