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

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

Use screen off brightness sensor when screen is off due to proximity

When the screen is off due to proximity, the policy isn't POLICY_OFF. By looking at the display state instead we can keep the sensor on.

Bug: 362940418
Flag: EXEMPT bugfix
Test: AutoBrightnessFallbackStrategyTest, DisplayPowerControllerTest
Change-Id: I78da47341e1727b976f57f46b10cf7e74ec0e56e
parent 34f9f07e
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.display;

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

import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_BEDTIME_WEAR;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DEFAULT;
@@ -1411,8 +1410,9 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            if (mScreenOffBrightnessSensorController != null) {
                mScreenOffBrightnessSensorController
                        .setLightSensorEnabled(displayBrightnessState.getShouldUseAutoBrightness()
                        && mIsEnabled && (mPowerRequest.policy == POLICY_OFF
                        || (mPowerRequest.policy == POLICY_DOZE && !allowAutoBrightnessWhileDozing))
                        && mIsEnabled && (state == Display.STATE_OFF
                                || (mPowerRequest.policy == POLICY_DOZE
                                && !allowAutoBrightnessWhileDozing))
                        && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
            }
        }
+3 −2
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.display.brightness.strategy;

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

import android.annotation.NonNull;
import android.annotation.Nullable;
@@ -26,6 +25,7 @@ import android.hardware.SensorManager;
import android.os.Handler;
import android.os.SystemClock;
import android.util.IndentingPrintWriter;
import android.view.Display;

import com.android.internal.annotations.VisibleForTesting;
import com.android.server.display.BrightnessMappingStrategy;
@@ -112,9 +112,10 @@ public final class AutoBrightnessFallbackStrategy implements DisplayBrightnessSt
            StrategySelectionNotifyRequest strategySelectionNotifyRequest) {
        if (mScreenOffBrightnessSensorController != null) {
            int policy = strategySelectionNotifyRequest.getDisplayPowerRequest().policy;
            int state = strategySelectionNotifyRequest.getTargetDisplayState();
            mScreenOffBrightnessSensorController.setLightSensorEnabled(
                    strategySelectionNotifyRequest.isAutoBrightnessEnabled() && mIsDisplayEnabled
                            && (policy == POLICY_OFF || (policy == POLICY_DOZE
                            && (state == Display.STATE_OFF || (policy == POLICY_DOZE
                            && !strategySelectionNotifyRequest
                            .isAllowAutoBrightnessWhileDozingConfig()))
                            && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
+35 −1
Original line number Diff line number Diff line
@@ -844,7 +844,7 @@ public final class DisplayPowerControllerTest {
    }

    @Test
    public void testSetScreenOffBrightnessSensorEnabled_DisplayIsOff() {
    public void testSetScreenOffBrightnessSensorEnabled_PolicyIsOff() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
@@ -878,6 +878,40 @@ public final class DisplayPowerControllerTest {
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat(), eq(false));
    }

    @Test
    public void testSetScreenOffBrightnessSensorEnabled_PolicyBright_DisplayIsOff() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);

        when(mHolder.displayPowerState.getScreenState()).thenReturn(Display.STATE_OFF);

        DisplayPowerRequest dpr = new DisplayPowerRequest();
        dpr.policy = DisplayPowerRequest.POLICY_BRIGHT;
        mHolder.dpc.requestPowerState(dpr, /* waitForNegativeProximity= */ false);
        advanceTime(1); // Run updatePowerState

        verify(mHolder.screenOffBrightnessSensorController, atLeastOnce())
                .setLightSensorEnabled(true);

        // The display turns on and we use the brightness value recommended by
        // ScreenOffBrightnessSensorController
        clearInvocations(mHolder.screenOffBrightnessSensorController);
        float brightness = 0.14f;
        when(mHolder.screenOffBrightnessSensorController.getAutomaticScreenBrightness())
                .thenReturn(brightness);
        when(mHolder.displayPowerState.getScreenState()).thenReturn(Display.STATE_ON);
        when(mHolder.automaticBrightnessController.getAutomaticScreenBrightness(
                any(BrightnessEvent.class))).thenReturn(PowerManager.BRIGHTNESS_INVALID_FLOAT);

        mHolder.dpc.updateBrightness();
        advanceTime(1); // Run updatePowerState

        verify(mHolder.screenOffBrightnessSensorController, atLeastOnce())
                .getAutomaticScreenBrightness();
        verify(mHolder.animator).animateTo(eq(brightness), anyFloat(), anyFloat(), eq(false));
    }

    @Test
    public void testSetScreenOffBrightnessSensorEnabled_DisplayIsInDoze() {
        mContext.getOrCreateTestableResources().addOverride(
+23 −0
Original line number Diff line number Diff line
@@ -157,6 +157,29 @@ public class AutoBrightnessFallbackStrategyTest {
        verify(mScreenOffBrightnessSensorController).setLightSensorEnabled(true);
    }

    @Test
    public void testPostProcess_EnableSensor_PolicyBright_ScreenOff() {
        boolean isDisplayEnabled = true;
        int leadDisplayId = NO_LEAD_DISPLAY;
        mAutoBrightnessFallbackStrategy.setupAutoBrightnessFallbackSensor(mSensorManager,
                mDisplayDeviceConfig,
                mHandler, mBrightnessMappingStrategy, isDisplayEnabled, leadDisplayId);

        DisplayManagerInternal.DisplayPowerRequest dpr =
                new DisplayManagerInternal.DisplayPowerRequest();
        dpr.policy = POLICY_BRIGHT;
        StrategySelectionNotifyRequest ssnr = new StrategySelectionNotifyRequest(dpr,
                Display.STATE_OFF, mAutoBrightnessFallbackStrategy,
                /* lastUserSetScreenBrightness= */ PowerManager.BRIGHTNESS_INVALID_FLOAT,
                /* userSetBrightnessChanged= */ false,
                /* allowAutoBrightnessWhileDozingConfig= */ false,
                /* isAutoBrightnessEnabled= */ true,
                /* isBedtimeModeWearEnabled= */ false);
        mAutoBrightnessFallbackStrategy.strategySelectionPostProcessor(ssnr);

        verify(mScreenOffBrightnessSensorController).setLightSensorEnabled(true);
    }

    @Test
    public void testPostProcess_EnableSensor_PolicyDoze() {
        boolean isDisplayEnabled = true;