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
This commit is contained in:
Fiona Campbell
2023-05-26 14:03:09 +00:00
parent 4b4b8d7b08
commit f336f64a11
2 changed files with 48 additions and 11 deletions

View File

@@ -333,11 +333,8 @@ 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);
}
// Use the given short-term model
setScreenBrightnessByUser(userLux, userBrightness);
}
/**
@@ -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() {

View File

@@ -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 =