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

Commit f48aabce authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Apply "night mode" device effect immediately if the keyguard is showing" into main

parents e39b6ac8 14d9edbd
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_OFF;
import static com.android.server.notification.ZenLog.traceApplyDeviceEffect;
import static com.android.server.notification.ZenLog.traceScheduleApplyDeviceEffect;

import android.app.KeyguardManager;
import android.app.UiModeManager;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
@@ -53,6 +54,7 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {

    private final Context mContext;
    private final ColorDisplayManager mColorDisplayManager;
    private final KeyguardManager mKeyguardManager;
    private final PowerManager mPowerManager;
    private final UiModeManager mUiModeManager;
    private final WallpaperManager mWallpaperManager;
@@ -67,6 +69,7 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
    DefaultDeviceEffectsApplier(Context context) {
        mContext = context;
        mColorDisplayManager = context.getSystemService(ColorDisplayManager.class);
        mKeyguardManager = context.getSystemService(KeyguardManager.class);
        mPowerManager = context.getSystemService(PowerManager.class);
        mUiModeManager = context.getSystemService(UiModeManager.class);
        WallpaperManager wallpaperManager = context.getSystemService(WallpaperManager.class);
@@ -133,12 +136,14 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {

        // Changing the theme can be disruptive for the user (Activities are likely recreated, may
        // lose some state). Therefore we only apply the change immediately if the rule was
        // activated manually, or we are initializing, or the screen is currently off/dreaming.
        // activated manually, or we are initializing, or the screen is currently off/dreaming,
        // or if the device is locked.
        if (origin == ZenModeConfig.ORIGIN_INIT
                || origin == ZenModeConfig.ORIGIN_INIT_USER
                || origin == ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI
                || origin == ZenModeConfig.ORIGIN_USER_IN_APP
                || !mPowerManager.isInteractive()) {
                || !mPowerManager.isInteractive()
                || (android.app.Flags.modesUi() && mKeyguardManager.isKeyguardLocked())) {
            unregisterScreenOffReceiver();
            updateNightModeImmediately(useNightMode);
        } else {
+19 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import android.app.KeyguardManager;
import android.app.UiModeManager;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
@@ -78,6 +79,7 @@ public class DefaultDeviceEffectsApplierTest {
    private DefaultDeviceEffectsApplier mApplier;
    @Mock PowerManager mPowerManager;
    @Mock ColorDisplayManager mColorDisplayManager;
    @Mock KeyguardManager mKeyguardManager;
    @Mock UiModeManager mUiModeManager;
    @Mock WallpaperManager mWallpaperManager;

@@ -87,6 +89,7 @@ public class DefaultDeviceEffectsApplierTest {
        mContext = spy(new TestableContext(InstrumentationRegistry.getContext(), null));
        mContext.addMockSystemService(PowerManager.class, mPowerManager);
        mContext.addMockSystemService(ColorDisplayManager.class, mColorDisplayManager);
        mContext.addMockSystemService(KeyguardManager.class, mKeyguardManager);
        mContext.addMockSystemService(UiModeManager.class, mUiModeManager);
        mContext.addMockSystemService(WallpaperManager.class, mWallpaperManager);
        when(mWallpaperManager.isWallpaperSupported()).thenReturn(true);
@@ -310,6 +313,22 @@ public class DefaultDeviceEffectsApplierTest {
        verify(mContext, never()).registerReceiver(any(), any(), anyInt());
    }

    @Test
    @EnableFlags({android.app.Flags.FLAG_MODES_API, android.app.Flags.FLAG_MODES_UI})
    public void apply_nightModeWithScreenOnAndKeyguardShowing_appliedImmediately(
            @TestParameter ZenChangeOrigin origin) {

        when(mPowerManager.isInteractive()).thenReturn(true);
        when(mKeyguardManager.isKeyguardLocked()).thenReturn(true);

        mApplier.apply(new ZenDeviceEffects.Builder().setShouldUseNightMode(true).build(),
                origin.value());

        // Effect was applied, and no broadcast receiver was registered.
        verify(mUiModeManager).setAttentionModeThemeOverlay(eq(MODE_ATTENTION_THEME_OVERLAY_NIGHT));
        verify(mContext, never()).registerReceiver(any(), any(), anyInt());
    }

    @Test
    @TestParameters({"{origin: ORIGIN_USER_IN_SYSTEMUI}", "{origin: ORIGIN_USER_IN_APP}",
            "{origin: ORIGIN_INIT}", "{origin: ORIGIN_INIT_USER}"})