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

Commit c777ddc5 authored by Piotr Wilczyński's avatar Piotr Wilczyński
Browse files

Fix auto-brightness when display on and policy doze

This is restoring the original behaviour from before ag/26149495 was merged - don't run auto-brightness if we have POLICY_DOZE and display STATE_ON.

Also, if config allows auto-brightness in doze, enable it if policy is POLICY_DOZE so that we can keep running auto-brightness after a tilt-to-wake gesture (after which we have POLICY_DOZE and display STATE_ON), but not if the screen is off.

Bug: 341827308
Test: AutomaticBrightnessStrategyTest, AutomaticBrightnessStrategy2Test
Flag: not needed, this is a regression fix
Change-Id: Idb92abfa5ec4823125d1b9076941df0e724f030e
parent 73e875a6
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.server.display.brightness.strategy;

import static android.hardware.display.DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;

import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DOZE;

@@ -133,14 +135,20 @@ public class AutomaticBrightnessStrategy extends AutomaticBrightnessStrategy2
        // We are still in the process of updating the power state, so there's no need to trigger
        // an update again
        switchMode(targetDisplayState, /* sendUpdate= */ false);
        final boolean autoBrightnessEnabledInDoze =
                allowAutoBrightnessWhileDozingConfig && Display.isDozeState(targetDisplayState);

        // If the policy is POLICY_DOZE and the display state is STATE_ON, auto-brightness should
        // only be enabled if the config allows it
        final boolean autoBrightnessEnabledInDoze = allowAutoBrightnessWhileDozingConfig
                && policy == POLICY_DOZE && targetDisplayState != Display.STATE_OFF;

        mIsAutoBrightnessEnabled = shouldUseAutoBrightness()
                && (targetDisplayState == Display.STATE_ON || autoBrightnessEnabledInDoze)
                && ((targetDisplayState == Display.STATE_ON && policy != POLICY_DOZE)
                || autoBrightnessEnabledInDoze)
                && brightnessReason != BrightnessReason.REASON_OVERRIDE
                && mAutomaticBrightnessController != null;
        mAutoBrightnessDisabledDueToDisplayOff = shouldUseAutoBrightness()
                && !(targetDisplayState == Display.STATE_ON || autoBrightnessEnabledInDoze);
                && !((targetDisplayState == Display.STATE_ON && policy != POLICY_DOZE)
                || autoBrightnessEnabledInDoze);
        final int autoBrightnessState = mIsAutoBrightnessEnabled
                && brightnessReason != BrightnessReason.REASON_FOLLOWER
                ? AutomaticBrightnessController.AUTO_BRIGHTNESS_ENABLED
+11 −4
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.server.display.brightness.strategy;

import static android.hardware.display.DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;

import android.annotation.Nullable;
import android.content.Context;
import android.hardware.display.BrightnessConfiguration;
@@ -107,14 +109,19 @@ public class AutomaticBrightnessStrategy2 {
    public void setAutoBrightnessState(int targetDisplayState,
            boolean allowAutoBrightnessWhileDozingConfig, int brightnessReason, int policy,
            float lastUserSetScreenBrightness, boolean userSetBrightnessChanged) {
        final boolean autoBrightnessEnabledInDoze =
                allowAutoBrightnessWhileDozingConfig && Display.isDozeState(targetDisplayState);
        // If the policy is POLICY_DOZE and the display state is STATE_ON, auto-brightness should
        // only be enabled if the config allows it
        final boolean autoBrightnessEnabledInDoze = allowAutoBrightnessWhileDozingConfig
                && policy == POLICY_DOZE && targetDisplayState != Display.STATE_OFF;

        mIsAutoBrightnessEnabled = shouldUseAutoBrightness()
                && (targetDisplayState == Display.STATE_ON || autoBrightnessEnabledInDoze)
                && ((targetDisplayState == Display.STATE_ON && policy != POLICY_DOZE)
                || autoBrightnessEnabledInDoze)
                && brightnessReason != BrightnessReason.REASON_OVERRIDE
                && mAutomaticBrightnessController != null;
        mAutoBrightnessDisabledDueToDisplayOff = shouldUseAutoBrightness()
                && !(targetDisplayState == Display.STATE_ON || autoBrightnessEnabledInDoze);
                && !((targetDisplayState == Display.STATE_ON  && policy != POLICY_DOZE)
                || autoBrightnessEnabledInDoze);
        final int autoBrightnessState = mIsAutoBrightnessEnabled
                && brightnessReason != BrightnessReason.REASON_FOLLOWER
                ? AutomaticBrightnessController.AUTO_BRIGHTNESS_ENABLED
+54 −1
Original line number Diff line number Diff line
@@ -192,7 +192,7 @@ public class AutomaticBrightnessStrategy2Test {
    }

    @Test
    public void testAutoBrightnessState_DisplayIsInDoze_ConfigDoesAllow() {
    public void testAutoBrightnessState_DeviceIsInDoze_ConfigDoesAllow() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_DOZE;
        boolean allowAutoBrightnessWhileDozing = true;
@@ -217,6 +217,32 @@ public class AutomaticBrightnessStrategy2Test {
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DeviceIsInDoze_ConfigDoesAllow_ScreenOff() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_OFF;
        boolean allowAutoBrightnessWhileDozing = true;
        int brightnessReason = BrightnessReason.REASON_UNKNOWN;
        int policy = DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;
        float lastUserSetBrightness = 0.2f;
        boolean userSetBrightnessChanged = true;
        Settings.System.putFloat(mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.4f);
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments();
        mAutomaticBrightnessStrategy.setAutoBrightnessState(targetDisplayState,
                allowAutoBrightnessWhileDozing, brightnessReason, policy, lastUserSetBrightness,
                userSetBrightnessChanged);
        verify(mAutomaticBrightnessController)
                .configure(AutomaticBrightnessController.AUTO_BRIGHTNESS_OFF_DUE_TO_DISPLAY_STATE,
                        mBrightnessConfiguration,
                        lastUserSetBrightness,
                        userSetBrightnessChanged, /* adjustment */ 0.4f,
                        /* userChangedAutoBrightnessAdjustment= */ true, policy,
                        targetDisplayState, /* shouldResetShortTermModel */ true);
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessEnabled());
        assertTrue(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DisplayIsOn() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
@@ -244,6 +270,33 @@ public class AutomaticBrightnessStrategy2Test {
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DisplayIsOn_PolicyIsDoze() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_ON;
        boolean allowAutoBrightnessWhileDozing = false;
        int brightnessReason = BrightnessReason.REASON_UNKNOWN;
        float lastUserSetBrightness = 0.2f;
        boolean userSetBrightnessChanged = true;
        int policy = DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;
        float pendingBrightnessAdjustment = 0.1f;
        Settings.System.putFloat(mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, pendingBrightnessAdjustment);
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments();
        mAutomaticBrightnessStrategy.setAutoBrightnessState(targetDisplayState,
                allowAutoBrightnessWhileDozing, brightnessReason, policy, lastUserSetBrightness,
                userSetBrightnessChanged);
        verify(mAutomaticBrightnessController)
                .configure(AutomaticBrightnessController.AUTO_BRIGHTNESS_OFF_DUE_TO_DISPLAY_STATE,
                        mBrightnessConfiguration,
                        lastUserSetBrightness,
                        userSetBrightnessChanged, pendingBrightnessAdjustment,
                        /* userChangedAutoBrightnessAdjustment= */ true, policy, targetDisplayState,
                        /* shouldResetShortTermModel */ true);
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessEnabled());
        assertTrue(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void accommodateUserBrightnessChangesWorksAsExpected() {
        // Verify the state if automaticBrightnessController is configured.
+54 −1
Original line number Diff line number Diff line
@@ -202,7 +202,7 @@ public class AutomaticBrightnessStrategyTest {
    }

    @Test
    public void testAutoBrightnessState_DisplayIsInDoze_ConfigDoesAllow() {
    public void testAutoBrightnessState_DeviceIsInDoze_ConfigDoesAllow() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_DOZE;
        boolean allowAutoBrightnessWhileDozing = true;
@@ -227,6 +227,32 @@ public class AutomaticBrightnessStrategyTest {
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DeviceIsInDoze_ConfigDoesAllow_ScreenOff() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_OFF;
        boolean allowAutoBrightnessWhileDozing = true;
        int brightnessReason = BrightnessReason.REASON_UNKNOWN;
        int policy = DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;
        float lastUserSetBrightness = 0.2f;
        boolean userSetBrightnessChanged = true;
        Settings.System.putFloat(mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, 0.4f);
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments();
        mAutomaticBrightnessStrategy.setAutoBrightnessState(targetDisplayState,
                allowAutoBrightnessWhileDozing, brightnessReason, policy, lastUserSetBrightness,
                userSetBrightnessChanged);
        verify(mAutomaticBrightnessController)
                .configure(AutomaticBrightnessController.AUTO_BRIGHTNESS_OFF_DUE_TO_DISPLAY_STATE,
                        mBrightnessConfiguration,
                        lastUserSetBrightness,
                        userSetBrightnessChanged, /* adjustment */ 0.4f,
                        /* userChangedAutoBrightnessAdjustment= */ true, policy,
                        targetDisplayState, /* shouldResetShortTermModel */ true);
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessEnabled());
        assertTrue(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DisplayIsOn() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
@@ -254,6 +280,33 @@ public class AutomaticBrightnessStrategyTest {
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_DisplayIsOn_PolicyIsDoze() {
        mAutomaticBrightnessStrategy.setUseAutoBrightness(true);
        int targetDisplayState = Display.STATE_ON;
        boolean allowAutoBrightnessWhileDozing = false;
        int brightnessReason = BrightnessReason.REASON_UNKNOWN;
        float lastUserSetBrightness = 0.2f;
        boolean userSetBrightnessChanged = true;
        int policy = DisplayManagerInternal.DisplayPowerRequest.POLICY_DOZE;
        float pendingBrightnessAdjustment = 0.1f;
        Settings.System.putFloat(mContext.getContentResolver(),
                Settings.System.SCREEN_AUTO_BRIGHTNESS_ADJ, pendingBrightnessAdjustment);
        mAutomaticBrightnessStrategy.updatePendingAutoBrightnessAdjustments();
        mAutomaticBrightnessStrategy.setAutoBrightnessState(targetDisplayState,
                allowAutoBrightnessWhileDozing, brightnessReason, policy, lastUserSetBrightness,
                userSetBrightnessChanged);
        verify(mAutomaticBrightnessController)
                .configure(AutomaticBrightnessController.AUTO_BRIGHTNESS_OFF_DUE_TO_DISPLAY_STATE,
                        mBrightnessConfiguration,
                        lastUserSetBrightness,
                        userSetBrightnessChanged, pendingBrightnessAdjustment,
                        /* userChangedAutoBrightnessAdjustment= */ true, policy, targetDisplayState,
                        /* shouldResetShortTermModel */ true);
        assertFalse(mAutomaticBrightnessStrategy.isAutoBrightnessEnabled());
        assertTrue(mAutomaticBrightnessStrategy.isAutoBrightnessDisabledDueToDisplayOff());
    }

    @Test
    public void testAutoBrightnessState_modeSwitch() {
        // Setup the test