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
diff --git a/services/core/java/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategy.java b/services/core/java/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategy.java
index f809a49..37b6931 100644
--- a/services/core/java/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategy.java
+++ b/services/core/java/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategy.java
@@ -165,9 +165,8 @@
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 @@
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 @@
* @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;
- adjustAutomaticBrightnessStateIfValid(brightness);
+ if (!isAutomaticBrightnessAdjusted) {
+ adjustAutomaticBrightnessStateIfValid(brightness);
+ }
return brightness;
}
diff --git a/services/tests/displayservicetests/src/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategyTest.java b/services/tests/displayservicetests/src/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategyTest.java
index 19bff56..d19f479 100644
--- a/services/tests/displayservicetests/src/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategyTest.java
+++ b/services/tests/displayservicetests/src/com/android/server/display/brightness/strategy/AutomaticBrightnessStrategyTest.java
@@ -395,7 +395,7 @@
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 @@
}
@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 @@
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 @@
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 @@
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)