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

Commit f336f64a authored by Fiona Campbell's avatar Fiona Campbell
Browse files

Ensure invalid brightness values aren't used

Previously, we were adding invalid brightness values to the short term
model; specifically, -1.0 was being added, causing the short term model
to be very dark. This change ensures that we validate the values before
adding them to the curve.

Bug: 283055494
Test: atest com.android.server.display
Test: atest AutomaticBrightnessControllerTest
Change-Id: I4a53f5ea396c353c69b006dfda76e2f7acce7651
parent 4b4b8d7b
Loading
Loading
Loading
Loading
+12 −11
Original line number Diff line number Diff line
@@ -333,12 +333,9 @@ public class AutomaticBrightnessController {
        // Initialize to active (normal) screen brightness mode
        switchToInteractiveScreenBrightnessMode();

        if (userLux != BrightnessMappingStrategy.NO_USER_LUX
                && userBrightness != BrightnessMappingStrategy.NO_USER_BRIGHTNESS) {
        // Use the given short-term model
        setScreenBrightnessByUser(userLux, userBrightness);
    }
    }

    /**
     * Enable/disable logging.
@@ -520,6 +517,10 @@ public class AutomaticBrightnessController {
    }

    private boolean setScreenBrightnessByUser(float lux, float brightness) {
        if (lux == BrightnessMappingStrategy.NO_USER_LUX
                || brightness == BrightnessMappingStrategy.NO_USER_BRIGHTNESS) {
            return false;
        }
        mCurrentBrightnessMapper.addUserDataPoint(lux, brightness);
        mShortTermModel.setUserBrightness(lux, brightness);
        return true;
@@ -1234,14 +1235,14 @@ public class AutomaticBrightnessController {
        // light.
        // The anchor determines what were the light levels when the user has set their preference,
        // and we use a relative threshold to determine when to revert to the OEM curve.
        private float mAnchor = -1f;
        private float mBrightness;
        private boolean mIsValid = true;
        private float mAnchor = BrightnessMappingStrategy.NO_USER_LUX;
        private float mBrightness = BrightnessMappingStrategy.NO_USER_BRIGHTNESS;
        private boolean mIsValid = false;

        private void reset() {
            mAnchor = -1f;
            mBrightness = -1f;
            mIsValid = true;
            mAnchor = BrightnessMappingStrategy.NO_USER_LUX;
            mBrightness = BrightnessMappingStrategy.NO_USER_BRIGHTNESS;
            mIsValid = false;
        }

        private void invalidate() {
+36 −0
Original line number Diff line number Diff line
@@ -472,6 +472,42 @@ public class AutomaticBrightnessControllerTest {
                .addUserDataPoint(anyFloat(), anyFloat());
    }

    @Test
    public void testSwitchBetweenModesNoUserInteractions() throws Exception {
        ArgumentCaptor<SensorEventListener> listenerCaptor =
                ArgumentCaptor.forClass(SensorEventListener.class);
        verify(mSensorManager).registerListener(listenerCaptor.capture(), eq(mLightSensor),
                eq(INITIAL_LIGHT_SENSOR_RATE * 1000), any(Handler.class));
        SensorEventListener listener = listenerCaptor.getValue();

        // Sensor reads 123 lux,
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 123));
        when(mBrightnessMappingStrategy.getShortTermModelTimeout()).thenReturn(2000L);
        when(mBrightnessMappingStrategy.getUserBrightness()).thenReturn(-1.0f);
        when(mBrightnessMappingStrategy.getUserLux()).thenReturn(-1.0f);

        // No user brightness interaction.

        mController.switchToIdleMode();
        when(mIdleBrightnessMappingStrategy.isForIdleMode()).thenReturn(true);
        when(mIdleBrightnessMappingStrategy.getUserBrightness()).thenReturn(-1.0f);
        when(mIdleBrightnessMappingStrategy.getUserLux()).thenReturn(-1.0f);

        // Sensor reads 1000 lux,
        listener.onSensorChanged(TestUtils.createSensorEvent(mLightSensor, 1000));
        // Do not fast-forward time.
        mTestLooper.dispatchAll();

        mController.switchToInteractiveScreenBrightnessMode();
        // Do not fast-forward time
        mTestLooper.dispatchAll();

        // Ensure that there are no data points added, since the user has never adjusted the
        // brightness
        verify(mBrightnessMappingStrategy, times(0))
                .addUserDataPoint(anyFloat(), anyFloat());
    }

    @Test
    public void testSwitchToIdleMappingStrategy() throws Exception {
        ArgumentCaptor<SensorEventListener> listenerCaptor =