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

Commit 079c69e4 authored by Josh Tsuji's avatar Josh Tsuji Committed by Android (Google) Code Review
Browse files

Merge changes I93374f42,I0a1ae320 into sc-v2-dev

* changes:
  Apply screen off brightness clamp when going to sleep.
  Improve brightness handling during screen off timeout.
parents 442ba547 05ad234c
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -1388,6 +1388,12 @@
    <integer name="config_screenBrightnessDim">10</integer>
    <item name="config_screenBrightnessDimFloat" format="float" type="dimen">0.05</item>

    <!-- If the screen brightness is already set at or below config_screenBrightnessDim, and the
         user activity timeout expires, we still want to dim the screen slightly to indicate that
         the device is about to go to sleep. The screen will dim by this amount in that case.
         -->
    <item name="config_screenBrightnessMinimumDimAmountFloat" format="float" type="dimen">0.04</item>

    <!-- Minimum allowable screen brightness to use in a very dark room.
         This value sets the floor for the darkest possible auto-brightness
         adjustment.  It is expected to be somewhat less than the first entry in
+1 −0
Original line number Diff line number Diff line
@@ -2040,6 +2040,7 @@
  <java-symbol type="dimen" name="config_screenBrightnessSettingDefaultFloat" />
  <java-symbol type="dimen" name="config_screenBrightnessDozeFloat" />
  <java-symbol type="dimen" name="config_screenBrightnessDimFloat" />
  <java-symbol type="dimen" name="config_screenBrightnessMinimumDimAmountFloat" />
  <java-symbol type="integer" name="config_screenBrightnessDark" />
  <java-symbol type="integer" name="config_screenBrightnessDim" />
  <java-symbol type="integer" name="config_screenBrightnessDoze" />
+39 −9
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

package com.android.systemui.doze;

import static android.os.PowerManager.GO_TO_SLEEP_REASON_TIMEOUT;

import static com.android.systemui.keyguard.WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -31,11 +35,13 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.util.IndentingPrintWriter;

import com.android.internal.R;
import com.android.systemui.doze.dagger.BrightnessSensor;
import com.android.systemui.doze.dagger.DozeScope;
import com.android.systemui.doze.dagger.WrappedService;
import com.android.systemui.keyguard.WakefulnessLifecycle;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.phone.UnlockedScreenOffAnimationController;
import com.android.systemui.statusbar.policy.DevicePostureController;
import com.android.systemui.util.sensors.AsyncSensorManager;

@@ -57,6 +63,12 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
            "com.android.systemui.doze.AOD_BRIGHTNESS";
    protected static final String BRIGHTNESS_BUCKET = "brightness_bucket";

    /**
     * Just before the screen times out from user inactivity, DisplayPowerController dims the screen
     * brightness to the lower of {@link #mScreenBrightnessDim}, or the current brightness minus
     * this amount.
     */
    private final float mScreenBrightnessMinimumDimAmountFloat;
    private final Context mContext;
    private final DozeMachine.Service mDozeService;
    private final DozeHost mDozeHost;
@@ -87,6 +99,8 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
     */
    private int mDebugBrightnessBucket = -1;

    private UnlockedScreenOffAnimationController mUnlockedScreenOffAnimationController;

    @Inject
    public DozeScreenBrightness(
            Context context,
@@ -98,8 +112,8 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
            WakefulnessLifecycle wakefulnessLifecycle,
            DozeParameters dozeParameters,
            DevicePostureController devicePostureController,
            DozeLog dozeLog
    ) {
            DozeLog dozeLog,
            UnlockedScreenOffAnimationController unlockedScreenOffAnimationController) {
        mContext = context;
        mDozeService = service;
        mSensorManager = sensorManager;
@@ -111,6 +125,10 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
        mDozeHost = host;
        mHandler = handler;
        mDozeLog = dozeLog;
        mUnlockedScreenOffAnimationController = unlockedScreenOffAnimationController;

        mScreenBrightnessMinimumDimAmountFloat = context.getResources().getFloat(
                R.dimen.config_screenBrightnessMinimumDimAmountFloat);

        mDefaultDozeBrightness = alwaysOnDisplayPolicy.defaultDozeBrightness;
        mScreenBrightnessDim = alwaysOnDisplayPolicy.dimBrightness;
@@ -163,14 +181,15 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
        }
    }

    private void updateBrightnessAndReady(boolean force) {
    public void updateBrightnessAndReady(boolean force) {
        if (force || mRegistered || mDebugBrightnessBucket != -1) {
            int sensorValue = mDebugBrightnessBucket == -1
                    ? mLastSensorValue : mDebugBrightnessBucket;
            int brightness = computeBrightness(sensorValue);
            boolean brightnessReady = brightness > 0;
            if (brightnessReady) {
                mDozeService.setDozeScreenBrightness(clampToUserSetting(brightness));
                mDozeService.setDozeScreenBrightness(
                        clampToDimBrightnessForScreenOff(clampToUserSetting(brightness)));
            }

            int scrimOpacity = -1;
@@ -243,13 +262,24 @@ public class DozeScreenBrightness extends BroadcastReceiver implements DozeMachi
    /**
     * Clamp the brightness to the dim brightness value used by PowerManagerService just before the
     * device times out and goes to sleep, if we are sleeping from a timeout. This ensures that we
     * don't raise the brightness back to the user setting before playing the screen off animation.
     * don't raise the brightness back to the user setting before or during the screen off
     * animation.
     */
    private int clampToDimBrightnessForScreenOff(int brightness) {
        if (mDozeParameters.shouldControlUnlockedScreenOff()
                && mWakefulnessLifecycle.getLastSleepReason()
                == PowerManager.GO_TO_SLEEP_REASON_TIMEOUT) {
            return Math.min(mScreenBrightnessDim, brightness);
        final boolean screenTurningOff =
                mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()
                        || mWakefulnessLifecycle.getWakefulness() == WAKEFULNESS_GOING_TO_SLEEP;
        if (screenTurningOff
                && mWakefulnessLifecycle.getLastSleepReason() == GO_TO_SLEEP_REASON_TIMEOUT) {
            return Math.max(
                    PowerManager.BRIGHTNESS_OFF,
                    // Use the lower of either the dim brightness, or the current brightness reduced
                    // by the minimum dim amount. This is the same logic used in
                    // DisplayPowerController#updatePowerState to apply a minimum dim amount.
                    Math.min(
                            brightness - (int) Math.floor(
                                    mScreenBrightnessMinimumDimAmountFloat * 255),
                            mScreenBrightnessDim));
        } else {
            return brightness;
        }
+10 −1
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ public class DozeScreenState implements DozeMachine.Part {
    private final Provider<UdfpsController> mUdfpsControllerProvider;
    @Nullable private UdfpsController mUdfpsController;
    private final DozeLog mDozeLog;
    private final DozeScreenBrightness mDozeScreenBrightness;

    private int mPendingScreenState = Display.STATE_UNKNOWN;
    private SettableWakeLock mWakeLock;
@@ -90,7 +91,8 @@ public class DozeScreenState implements DozeMachine.Part {
            WakeLock wakeLock,
            AuthController authController,
            Provider<UdfpsController> udfpsControllerProvider,
            DozeLog dozeLog) {
            DozeLog dozeLog,
            DozeScreenBrightness dozeScreenBrightness) {
        mDozeService = service;
        mHandler = handler;
        mParameters = parameters;
@@ -99,6 +101,7 @@ public class DozeScreenState implements DozeMachine.Part {
        mAuthController = authController;
        mUdfpsControllerProvider = udfpsControllerProvider;
        mDozeLog = dozeLog;
        mDozeScreenBrightness = dozeScreenBrightness;

        updateUdfpsController();
        if (mUdfpsController == null) {
@@ -209,6 +212,12 @@ public class DozeScreenState implements DozeMachine.Part {
        if (screenState != Display.STATE_UNKNOWN) {
            if (DEBUG) Log.d(TAG, "setDozeScreenState(" + screenState + ")");
            mDozeService.setDozeScreenState(screenState);
            if (screenState == Display.STATE_DOZE) {
                // If we're entering doze, update the doze screen brightness. We might have been
                // clamping it to the dim brightness during the screen off animation, and we should
                // now change it to the brightness we actually want according to the sensor.
                mDozeScreenBrightness.updateBrightnessAndReady(false /* force */);
            }
            mPendingScreenState = Display.STATE_UNKNOWN;
            mWakeLock.setAcquired(false);
        }
+53 −11
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import com.android.systemui.SysuiTestCase;
import com.android.systemui.dock.DockManager;
import com.android.systemui.keyguard.WakefulnessLifecycle;
import com.android.systemui.statusbar.phone.DozeParameters;
import com.android.systemui.statusbar.phone.UnlockedScreenOffAnimationController;
import com.android.systemui.statusbar.policy.DevicePostureController;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.concurrency.FakeThreadFactory;
@@ -93,6 +94,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
    DevicePostureController mDevicePostureController;
    @Mock
    DozeLog mDozeLog;
    @Mock
    private UnlockedScreenOffAnimationController mUnlockedScreenOffAnimationController;
    private FakeExecutor mFakeExecutor = new FakeExecutor(new FakeSystemClock());
    private FakeThreadFactory mFakeThreadFactory = new FakeThreadFactory(mFakeExecutor);

@@ -130,7 +133,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);
    }

    @Test
@@ -236,7 +240,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);
        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);
        reset(mDozeHost);
@@ -273,7 +278,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE_AOD);
@@ -304,7 +310,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);

        // GIVEN the device is in AOD
        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
@@ -342,7 +349,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);

        // GIVEN device is in AOD
        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
@@ -384,7 +392,8 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
                mWakefulnessLifecycle,
                mDozeParameters,
                mDevicePostureController,
                mDozeLog);
                mDozeLog,
                mUnlockedScreenOffAnimationController);
        verify(mDevicePostureController).addCallback(postureCallbackCaptor.capture());

        // GIVEN device is in AOD
@@ -466,24 +475,26 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
    }

    @Test
    public void transitionToDoze_duringScreenOff_afterTimeout_clampsToDim() {
    public void transitionToDoze_duringUnlockedScreenOff_afterTimeout_clampsToDim() {
        when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(true);
        when(mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()).thenReturn(true);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);

        // If we're dozing after a timeout, and playing the unlocked screen animation, we should
        // stay at dim brightness, because the screen dims just before timeout.
        assertEquals(mServiceFake.screenBrightness, DIM_BRIGHTNESS);
        // stay at or below dim brightness, because the screen dims just before timeout.
        assertTrue(mServiceFake.screenBrightness <= DIM_BRIGHTNESS);
    }

    @Test
    public void transitionToDoze_duringScreenOff_notAfterTimeout_doesNotClampToDim() {
    public void transitionToDoze_duringUnlockedScreenOff_notAfterTimeout_doesNotClampToDim() {
        when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON);
        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(true);
        when(mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()).thenReturn(true);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);
@@ -494,10 +505,11 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
    }

    @Test
    public void transitionToDoze_duringScreenOff_afterTimeout_noScreenOff_doesNotClampToDim() {
    public void transitionToDoze_duringUnlockedScreenOff_afterTimeout_noScreenOff_doesNotClampToDim() {
        when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
        when(mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()).thenReturn(false);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);
@@ -506,6 +518,36 @@ public class DozeScreenBrightnessTest extends SysuiTestCase {
        assertEquals(mServiceFake.screenBrightness, DEFAULT_BRIGHTNESS);
    }

    @Test
    public void transitionToDoze_duringLockedScreenOff_afterTimeout_clampsToDim() {
        when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);
        when(mWakefulnessLifecycle.getWakefulness()).thenReturn(
                WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP);
        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
        when(mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()).thenReturn(false);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);

        assertTrue(mServiceFake.screenBrightness <= DIM_BRIGHTNESS);
    }

    @Test
    public void transitionToDoze_duringLockedScreenOff_notAfterTimeout_doesNotClampToDim() {
        when(mWakefulnessLifecycle.getLastSleepReason()).thenReturn(
                PowerManager.GO_TO_SLEEP_REASON_POWER_BUTTON);
        when(mWakefulnessLifecycle.getWakefulness()).thenReturn(
                WakefulnessLifecycle.WAKEFULNESS_GOING_TO_SLEEP);
        when(mDozeParameters.shouldControlUnlockedScreenOff()).thenReturn(false);
        when(mUnlockedScreenOffAnimationController.isScreenOffAnimationPlaying()).thenReturn(false);

        mScreen.transitionTo(UNINITIALIZED, INITIALIZED);
        mScreen.transitionTo(INITIALIZED, DOZE);

        assertEquals(mServiceFake.screenBrightness, DEFAULT_BRIGHTNESS);
    }

    private void waitForSensorManager() {
        mFakeExecutor.runAllReady();
    }
Loading