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

Commit 786c3514 authored by Rupesh Bansal's avatar Rupesh Bansal
Browse files

Apply autobrightness adjustments while checking the strategy validity

For checking the validity of autobrightness, we first check check if
the value in autobrightness controller is a valid one, and qualify the
strategy accordingly. However, the adjustments are not applied when this
validation is done, but instead applied when the actual brightness is
loaded while executing the strategy. While this is fine for most cases,
for some corner cases(like brightness being invalid because of invalid
lux), auto brightness adjustments are not applied.

Bug: 341643213
Test: atest AutomaticBrightnessStrategyTest
Change-Id: I4ebce5905b9237af8bb747158a3a9fcc565617f4
parent 538cb754
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -165,9 +165,8 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
    public boolean isAutoBrightnessValid() {
        boolean isValid = false;
        if (isAutoBrightnessEnabled()) {
            float brightness = (mAutomaticBrightnessController != null)
                    ? mAutomaticBrightnessController.getAutomaticScreenBrightness(null)
                    : PowerManager.BRIGHTNESS_INVALID_FLOAT;
            float brightness = getAutomaticScreenBrightness(null,
                    /* isAutomaticBrightnessAdjusted = */ false);
            if (BrightnessUtils.isValidBrightnessValue(brightness)
                    || brightness == PowerManager.BRIGHTNESS_OFF_FLOAT) {
                isValid = true;
@@ -274,7 +273,12 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
        BrightnessReason brightnessReason = new BrightnessReason();
        brightnessReason.setReason(BrightnessReason.REASON_AUTOMATIC);
        BrightnessEvent brightnessEvent = mInjector.getBrightnessEvent(mDisplayId);
        float brightness = getAutomaticScreenBrightness(brightnessEvent);

        // AutoBrightness adjustments were already applied while checking the validity of this
        // strategy. Reapplying them again will result in incorrect adjustment reason flags as we
        // might end up assuming no adjustments are applied
        float brightness = getAutomaticScreenBrightness(brightnessEvent,
                /* isAutomaticBrightnessAdjusted = */ true);
        return new DisplayBrightnessState.Builder()
                .setBrightness(brightness)
                .setSdrBrightness(brightness)
@@ -355,11 +359,14 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
     * @param brightnessEvent Event object to populate with details about why the specific
     *                        brightness was chosen.
     */
    public float getAutomaticScreenBrightness(BrightnessEvent brightnessEvent) {
    public float getAutomaticScreenBrightness(BrightnessEvent brightnessEvent,
            boolean isAutomaticBrightnessAdjusted) {
        float brightness = (mAutomaticBrightnessController != null)
                ? mAutomaticBrightnessController.getAutomaticScreenBrightness(brightnessEvent)
                : PowerManager.BRIGHTNESS_INVALID_FLOAT;
        if (!isAutomaticBrightnessAdjusted) {
            adjustAutomaticBrightnessStateIfValid(brightness);
        }
        return brightness;
    }

+26 −2
Original line number Diff line number Diff line
@@ -395,7 +395,7 @@ public class AutomaticBrightnessStrategyTest {
                automaticBrightnessController);
        assertEquals(automaticScreenBrightness,
                mAutomaticBrightnessStrategy.getAutomaticScreenBrightness(
                        new BrightnessEvent(DISPLAY_ID)), 0.0f);
                        new BrightnessEvent(DISPLAY_ID), false), 0.0f);
        assertEquals(automaticScreenBrightness,
                mAutomaticBrightnessStrategy.getAutomaticScreenBrightnessBasedOnLastUsedLux(
                        new BrightnessEvent(DISPLAY_ID)), 0.0f);
@@ -461,8 +461,12 @@ public class AutomaticBrightnessStrategyTest {
    }

    @Test
    public void isAutoBrightnessValid_returnsTrueWhenBrightnessIsValid() {
    public void isAutoBrightnessValid_returnsTrueWhenBrightnessIsValid_adjustsAutoBrightness()
            throws Settings.SettingNotFoundException {
        float adjustment = 0.1f;
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        when(mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment())
                .thenReturn(0.1f);
        mAutomaticBrightnessStrategy.setAutoBrightnessState(Display.STATE_ON, true,
                BrightnessReason.REASON_UNKNOWN,
                DisplayManagerInternal.DisplayPowerRequest.POLICY_BRIGHT, 0.1f,
@@ -470,6 +474,11 @@ public class AutomaticBrightnessStrategyTest {
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(null))
                .thenReturn(0.2f);
        assertTrue(mAutomaticBrightnessStrategy.isAutoBrightnessValid());
        assertEquals(adjustment, mAutomaticBrightnessStrategy.getAutoBrightnessAdjustment(), 0.0f);
        assertEquals(adjustment, Settings.System.getFloatForUser(
                mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ,
                UserHandle.USER_CURRENT), 0.0f);
    }

    @Test
@@ -486,6 +495,15 @@ public class AutomaticBrightnessStrategyTest {
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(brightnessEvent))
                .thenReturn(brightness);


        // We do this to apply the automatic brightness adjustments
        when(mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment()).thenReturn(
                0.25f);
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(null))
                .thenReturn(brightness);
        assertEquals(brightness, mAutomaticBrightnessStrategy
                .getAutomaticScreenBrightness(null, false), 0.0f);

        DisplayManagerInternal.DisplayPowerRequest displayPowerRequest =
                mock(DisplayManagerInternal.DisplayPowerRequest.class);
        DisplayBrightnessState expectedDisplayBrightnessState = new DisplayBrightnessState.Builder()
@@ -529,6 +547,12 @@ public class AutomaticBrightnessStrategyTest {
        when(mAutomaticBrightnessController.getAutomaticScreenBrightnessAdjustment()).thenReturn(
                autoBrightnessAdjustment);

        // We do this to apply the automatic brightness adjustments
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(null))
                .thenReturn(brightness);
        assertEquals(brightness, mAutomaticBrightnessStrategy
                .getAutomaticScreenBrightness(null, false), 0.0f);

        DisplayBrightnessState expectedDisplayBrightnessState = new DisplayBrightnessState.Builder()
                .setBrightness(brightness)
                .setSdrBrightness(brightness)