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

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

Enable screen off brightness sensor based on policy

Tapping the fingerprint sensor changes the display state to ON. It's not necessary to unsubscribe from the sensor at that point (and re-subscribe if the fingerprint wasn't recognized) because the doze brightness is still being used.

Additionaly:
- add missing tests
- rename isEnabled to isDisplayEnabled - isEnabled sounded like the sensor or the strategy is enabled/disabled

Bug: 341827308
Test: atest DisplayPowerControllerTest, DisplayBrightnessControllerTest, AutoBrightnessFallbackStrategyTest
Flag: EXEMPT bugfix
Change-Id: Ifd80667704cd91a29c317333f4e2a976127b67f5
parent ab4779e9
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
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_DEFAULT;
import static com.android.server.display.AutomaticBrightnessController.AUTO_BRIGHTNESS_MODE_DOZE;
@@ -1393,8 +1394,8 @@ final class DisplayPowerController implements AutomaticBrightnessController.Call
            if (mScreenOffBrightnessSensorController != null) {
                mScreenOffBrightnessSensorController
                        .setLightSensorEnabled(displayBrightnessState.getShouldUseAutoBrightness()
                        && mIsEnabled && (state == Display.STATE_OFF
                        || (state == Display.STATE_DOZE && !allowAutoBrightnessWhileDozing))
                        && mIsEnabled && (mPowerRequest.policy == POLICY_OFF
                        || (mPowerRequest.policy == POLICY_DOZE && !allowAutoBrightnessWhileDozing))
                        && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
            }
        }
+4 −4
Original line number Diff line number Diff line
@@ -359,11 +359,11 @@ public final class DisplayBrightnessController {
    public void setUpAutoBrightness(AutomaticBrightnessController automaticBrightnessController,
            SensorManager sensorManager,
            DisplayDeviceConfig displayDeviceConfig, Handler handler,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isDisplayEnabled,
            int leadDisplayId) {
        setAutomaticBrightnessController(automaticBrightnessController);
        setUpAutoBrightnessFallbackStrategy(sensorManager, displayDeviceConfig, handler,
                brightnessMappingStrategy, isEnabled, leadDisplayId);
                brightnessMappingStrategy, isDisplayEnabled, leadDisplayId);
    }

    /**
@@ -534,14 +534,14 @@ public final class DisplayBrightnessController {

    private void setUpAutoBrightnessFallbackStrategy(SensorManager sensorManager,
            DisplayDeviceConfig displayDeviceConfig, Handler handler,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isDisplayEnabled,
            int leadDisplayId) {
        AutoBrightnessFallbackStrategy autoBrightnessFallbackStrategy =
                getAutoBrightnessFallbackStrategy();
        if (autoBrightnessFallbackStrategy != null) {
            autoBrightnessFallbackStrategy.setupAutoBrightnessFallbackSensor(
                    sensorManager, displayDeviceConfig, handler, brightnessMappingStrategy,
                    isEnabled, leadDisplayId);
                    isDisplayEnabled, leadDisplayId);
        }
    }

+10 −9
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

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;
import android.hardware.Sensor;
@@ -23,7 +26,6 @@ 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;
@@ -53,7 +55,7 @@ public final class AutoBrightnessFallbackStrategy implements DisplayBrightnessSt
    Sensor mScreenOffBrightnessSensor;

    // Indicates if the associated LogicalDisplay is enabled or not.
    private boolean mIsEnabled;
    private boolean mIsDisplayEnabled;

    // Represents if the associated display is a lead display or not. If not, the variable
    // represents the lead display ID
@@ -97,7 +99,7 @@ public final class AutoBrightnessFallbackStrategy implements DisplayBrightnessSt
    public void dump(PrintWriter writer) {
        writer.println("AutoBrightnessFallbackStrategy:");
        writer.println("  mLeadDisplayId=" + mLeadDisplayId);
        writer.println("  mIsEnabled=" + mIsEnabled);
        writer.println("  mIsDisplayEnabled=" + mIsDisplayEnabled);
        if (mScreenOffBrightnessSensorController != null) {
            IndentingPrintWriter ipw = new IndentingPrintWriter(writer, " ");
            mScreenOffBrightnessSensorController.dump(ipw);
@@ -108,11 +110,10 @@ public final class AutoBrightnessFallbackStrategy implements DisplayBrightnessSt
    public void strategySelectionPostProcessor(
            StrategySelectionNotifyRequest strategySelectionNotifyRequest) {
        if (mScreenOffBrightnessSensorController != null) {
            int targetDisplayState = strategySelectionNotifyRequest.getTargetDisplayState();
            int policy = strategySelectionNotifyRequest.getDisplayPowerRequest().policy;
            mScreenOffBrightnessSensorController.setLightSensorEnabled(
                    strategySelectionNotifyRequest.isAutoBrightnessEnabled() && mIsEnabled
                            && (targetDisplayState == Display.STATE_OFF
                            || (targetDisplayState == Display.STATE_DOZE
                    strategySelectionNotifyRequest.isAutoBrightnessEnabled() && mIsDisplayEnabled
                            && (policy == POLICY_OFF || (policy == POLICY_DOZE
                            && !strategySelectionNotifyRequest
                            .isAllowAutoBrightnessWhileDozingConfig()))
                            && mLeadDisplayId == Layout.NO_LEAD_DISPLAY);
@@ -132,9 +133,9 @@ public final class AutoBrightnessFallbackStrategy implements DisplayBrightnessSt
     */
    public void setupAutoBrightnessFallbackSensor(SensorManager sensorManager,
            DisplayDeviceConfig displayDeviceConfig, Handler handler,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isEnabled,
            BrightnessMappingStrategy brightnessMappingStrategy, boolean isDisplayEnabled,
            int leadDisplayId) {
        mIsEnabled = isEnabled;
        mIsDisplayEnabled = isDisplayEnabled;
        mLeadDisplayId = leadDisplayId;
        if (mScreenOffBrightnessSensorController != null) {
            mScreenOffBrightnessSensorController.stop();
+24 −0
Original line number Diff line number Diff line
@@ -936,6 +936,9 @@ public final class DisplayPowerControllerTest {

    @Test
    public void testSetScreenOffBrightnessSensorDisabled_DisplayIsOn() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        DisplayPowerRequest dpr = new DisplayPowerRequest();
        dpr.policy = DisplayPowerRequest.POLICY_BRIGHT;

@@ -948,6 +951,9 @@ public final class DisplayPowerControllerTest {

    @Test
    public void testSetScreenOffBrightnessSensorDisabled_DisplayIsAFollower() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        DisplayPowerRequest dpr = new DisplayPowerRequest();
        dpr.policy = DisplayPowerRequest.POLICY_OFF;

@@ -959,6 +965,24 @@ public final class DisplayPowerControllerTest {
                .setLightSensorEnabled(false);
    }

    @Test
    public void testSetScreenOffBrightnessSensorDisabled_AutoBrightnessInDoze() {
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        mContext.getOrCreateTestableResources().addOverride(
                com.android.internal.R.bool.config_allowAutoBrightnessWhileDozing, true);
        mHolder = createDisplayPowerController(DISPLAY_ID, UNIQUE_ID);
        DisplayPowerRequest dpr = new DisplayPowerRequest();
        dpr.policy = DisplayPowerRequest.POLICY_DOZE;

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

        verify(mHolder.screenOffBrightnessSensorController, atLeastOnce())
                .setLightSensorEnabled(false);
    }

    @Test
    public void testStopScreenOffBrightnessSensorControllerWhenDisplayDeviceChanges() {
        // New display device
+5 −4
Original line number Diff line number Diff line
@@ -545,17 +545,18 @@ public final class DisplayBrightnessControllerTest {
        DisplayDeviceConfig displayDeviceConfig = mock(DisplayDeviceConfig.class);
        Handler handler = mock(Handler.class);
        BrightnessMappingStrategy brightnessMappingStrategy = mock(BrightnessMappingStrategy.class);
        boolean isEnabled = true;
        boolean isDisplayEnabled = true;
        int leadDisplayId = 2;

        mDisplayBrightnessController.setUpAutoBrightness(automaticBrightnessController,
                sensorManager, displayDeviceConfig, handler, brightnessMappingStrategy, isEnabled,
                leadDisplayId);
                sensorManager, displayDeviceConfig, handler, brightnessMappingStrategy,
                isDisplayEnabled, leadDisplayId);
        assertEquals(automaticBrightnessController,
                mDisplayBrightnessController.mAutomaticBrightnessController);
        verify(automaticBrightnessStrategy).setAutomaticBrightnessController(
                automaticBrightnessController);
        verify(autoBrightnessFallbackStrategy).setupAutoBrightnessFallbackSensor(sensorManager,
                displayDeviceConfig, handler, brightnessMappingStrategy, isEnabled, leadDisplayId);
                displayDeviceConfig, handler, brightnessMappingStrategy, isDisplayEnabled,
                leadDisplayId);
    }
}
Loading