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

Commit 96cc279c authored by Rupesh Bansal's avatar Rupesh Bansal
Browse files

change autobrightness slowly when valid

We recently surfaced an issue where the brightness rampup/rampdown will
happen fast even if the brightness was already applied with no new
adjustments

Test: atest AutomaticBrightnessStrategyTest
Bug: 304313764
Change-Id: I58040b3b4887fdbd0cc23cf6af31c2ce400d297c
parent b6778b2e
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -102,6 +102,9 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2

    private DisplayManagerFlags mDisplayManagerFlags;

    // Indicates if the current auto-brightness should be ramped up or down slowly.
    private boolean mIsSlowChange;

    @VisibleForTesting
    AutomaticBrightnessStrategy(Context context, int displayId, Injector injector,
            DisplayManagerFlags displayManagerFlags) {
@@ -172,6 +175,11 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
                isValid = true;
            }
        }

        // A change is slow when the auto-brightness was already applied, and there are no new
        // auto-brightness adjustments from an external client(e.g. Moving the slider). As such,
        // it is important to record this value before applying the current auto-brightness.
        mIsSlowChange = hasAppliedAutoBrightness() && !getAutoBrightnessAdjustmentChanged();
        setAutoBrightnessApplied(isValid);
        return isValid;
    }
@@ -284,8 +292,7 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
                .setSdrBrightness(brightness)
                .setBrightnessReason(brightnessReason)
                .setDisplayBrightnessStrategyName(getName())
                .setIsSlowChange(hasAppliedAutoBrightness()
                        && !getAutoBrightnessAdjustmentChanged())
                .setIsSlowChange(mIsSlowChange)
                .setBrightnessEvent(brightnessEvent)
                .setBrightnessAdjustmentFlag(mAutoBrightnessAdjustmentReasonsFlags)
                .setShouldUpdateScreenBrightnessSetting(
+80 −0
Original line number Diff line number Diff line
@@ -570,6 +570,86 @@ public class AutomaticBrightnessStrategyTest {
        assertEquals(expectedDisplayBrightnessState, actualDisplayBrightnessState);
    }

    @Test
    public void
            updateBrightness_constructsDisplayBrightnessState_withNoAdjustmentFlag_isSlowChange() {
        BrightnessEvent brightnessEvent = new BrightnessEvent(DISPLAY_ID);
        mAutomaticBrightnessStrategy = new AutomaticBrightnessStrategy(
                mContext, DISPLAY_ID, displayId -> brightnessEvent, mDisplayManagerFlags);
        mAutomaticBrightnessStrategy.setAutomaticBrightnessController(
                mAutomaticBrightnessController);
        float brightness = 0.4f;
        BrightnessReason brightnessReason = new BrightnessReason();
        brightnessReason.setReason(BrightnessReason.REASON_AUTOMATIC);
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(brightnessEvent))
                .thenReturn(brightness);

        // Set the state such that auto-brightness was already applied
        mAutomaticBrightnessStrategy.setAutoBrightnessApplied(true);

        // Update the auto-brightess validity state to change the isSlowChange flag
        mAutomaticBrightnessStrategy.isAutoBrightnessValid();

        DisplayManagerInternal.DisplayPowerRequest displayPowerRequest =
                mock(DisplayManagerInternal.DisplayPowerRequest.class);

        DisplayBrightnessState expectedDisplayBrightnessState = new DisplayBrightnessState.Builder()
                .setBrightness(brightness)
                .setSdrBrightness(brightness)
                .setBrightnessReason(brightnessReason)
                .setDisplayBrightnessStrategyName(mAutomaticBrightnessStrategy.getName())
                .setIsSlowChange(true)
                .setBrightnessEvent(brightnessEvent)
                .setBrightnessAdjustmentFlag(0)
                .setShouldUpdateScreenBrightnessSetting(true)
                .setIsUserInitiatedChange(true)
                .build();
        DisplayBrightnessState actualDisplayBrightnessState = mAutomaticBrightnessStrategy
                .updateBrightness(new StrategyExecutionRequest(displayPowerRequest, 0.6f,
                        /* userSetBrightnessChanged= */ true));
        assertEquals(expectedDisplayBrightnessState, actualDisplayBrightnessState);
    }


    @Test
    public void updateBrightness_autoBrightnessNotApplied_noAdjustments_isNotSlowChange() {
        BrightnessEvent brightnessEvent = new BrightnessEvent(DISPLAY_ID);
        mAutomaticBrightnessStrategy = new AutomaticBrightnessStrategy(
                mContext, DISPLAY_ID, displayId -> brightnessEvent, mDisplayManagerFlags);
        mAutomaticBrightnessStrategy.setAutomaticBrightnessController(
                mAutomaticBrightnessController);
        float brightness = 0.4f;
        BrightnessReason brightnessReason = new BrightnessReason();
        brightnessReason.setReason(BrightnessReason.REASON_AUTOMATIC);
        when(mAutomaticBrightnessController.getAutomaticScreenBrightness(brightnessEvent))
                .thenReturn(brightness);

        // Set the state such that auto-brightness was not already applied
        mAutomaticBrightnessStrategy.setAutoBrightnessApplied(false);

        // Update the auto-brightess validity state to change the isSlowChange flag
        mAutomaticBrightnessStrategy.isAutoBrightnessValid();

        DisplayManagerInternal.DisplayPowerRequest displayPowerRequest =
                mock(DisplayManagerInternal.DisplayPowerRequest.class);

        DisplayBrightnessState expectedDisplayBrightnessState = new DisplayBrightnessState.Builder()
                .setBrightness(brightness)
                .setSdrBrightness(brightness)
                .setBrightnessReason(brightnessReason)
                .setDisplayBrightnessStrategyName(mAutomaticBrightnessStrategy.getName())
                .setIsSlowChange(false)
                .setBrightnessEvent(brightnessEvent)
                .setBrightnessAdjustmentFlag(0)
                .setShouldUpdateScreenBrightnessSetting(true)
                .setIsUserInitiatedChange(true)
                .build();
        DisplayBrightnessState actualDisplayBrightnessState = mAutomaticBrightnessStrategy
                .updateBrightness(new StrategyExecutionRequest(displayPowerRequest, 0.6f,
                        /* userSetBrightnessChanged= */ true));
        assertEquals(expectedDisplayBrightnessState, actualDisplayBrightnessState);
    }

    private void setPendingAutoBrightnessAdjustment(float pendingAutoBrightnessAdjustment) {
        Settings.System.putFloat(mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, pendingAutoBrightnessAdjustment);