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

Commit 09ed5693 authored by Matías Hernández's avatar Matías Hernández
Browse files

Remove leftover wallpaper dim on reboot

Unlike other device effects (AOD supression, grayscale, etc) dimWallpaper survives reboots. Because DefaultDeviceEffectsApplier only applies _changed_ effects, this means that dimWallpaper would be stuck if it was active before the reboot, but inactive afterwards.

Although this condition (mode stopping on reboot) is rather rare, it can happen, for example, if DND was set with a timeout and the timeout expired between power-off and power-on.

Fixes: 419466188
Test: atest DefaultDeviceEffectsApplierTest
Flag: EXEMPT Bugfix
Change-Id: Iaeb4b09b4378310142ec1fefe6f9a7409e48411c
parent 66e62af0
Loading
Loading
Loading
Loading
+22 −15
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
                () -> {
                    maybeSuppressAmbientDisplay(effects.shouldSuppressAmbientDisplay());
                    maybeDisplayGrayscale(effects.shouldDisplayGrayscale());
                    maybeDimWallpaper(effects.shouldDimWallpaper());
                    maybeDimWallpaper(effects.shouldDimWallpaper(), origin);
                    maybeUseNightMode(effects.shouldUseNightMode(), origin);
                });

@@ -120,28 +120,35 @@ public class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
        }
    }

    protected void maybeDimWallpaper(boolean shouldDimWallpaper) {
        if (mLastAppliedEffects.shouldDimWallpaper() != shouldDimWallpaper) {
            if (mWallpaperManager != null) {
    protected void maybeDimWallpaper(boolean shouldDimWallpaper, @ConfigOrigin int origin) {
        if (mWallpaperManager == null) {
            return;
        }
        try {
            // Unlike the other effects, dimWallpaper is serialized (and thus preserved on reboot).
            if (mLastAppliedEffects.shouldDimWallpaper() != shouldDimWallpaper) {
                traceApplyDeviceEffect("dimWallpaper", shouldDimWallpaper);
                mWallpaperManager.setWallpaperDimAmount(
                        shouldDimWallpaper
                                ? WALLPAPER_DIM_AMOUNT_DIMMED
                                : WALLPAPER_DIM_AMOUNT_NORMAL);
            } else if (origin == ZenModeConfig.ORIGIN_INIT
                    && !shouldDimWallpaper
                    && mWallpaperManager.getWallpaperDimAmount() == WALLPAPER_DIM_AMOUNT_DIMMED) {
                traceApplyDeviceEffect("dimWallpaper (reset on boot)", false);
                mWallpaperManager.setWallpaperDimAmount(WALLPAPER_DIM_AMOUNT_NORMAL);
            }
        } catch (Exception e) {
            Slog.e(TAG, "Could not change wallpaper override", e);
        }
    }
        }
    }

    protected void maybeUseNightMode(boolean shouldUseNightMode, @ConfigOrigin int origin) {
        if (mLastAppliedEffects.shouldUseNightMode() != shouldUseNightMode) {
            try {
                updateOrScheduleNightMode(shouldUseNightMode, origin);
            } catch (Exception e) {
                Slog.e(TAG, "Could not change dark theme override", e);
                Slog.e(TAG, "Could not change night mode override", e);
            }
        }
    }
@@ -184,7 +191,7 @@ public class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {
                        useNightMode ? MODE_ATTENTION_THEME_OVERLAY_NIGHT
                                : MODE_ATTENTION_THEME_OVERLAY_OFF);
            } catch (Exception e) {
                Slog.e(TAG, "Could not change wallpaper override", e);
                Slog.e(TAG, "Could not change night mode override", e);
            }
        });
    }
+27 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.notification;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_NIGHT;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_OFF;
import static android.service.notification.ZenModeConfig.ORIGIN_APP;
import static android.service.notification.ZenModeConfig.ORIGIN_INIT;
import static android.service.notification.ZenModeConfig.ORIGIN_USER_IN_SYSTEMUI;

import static com.google.common.truth.Truth.assertThat;
@@ -81,6 +82,8 @@ public class DefaultDeviceEffectsApplierTest {
    @Mock UiModeManager mUiModeManager;
    @Mock WallpaperManager mWallpaperManager;

    private static final ZenDeviceEffects NO_EFFECTS = new ZenDeviceEffects.Builder().build();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
@@ -361,4 +364,28 @@ public class DefaultDeviceEffectsApplierTest {

        // No crashes
    }

    @Test
    public void apply_onBootWithLeftoverDim_forceRemovesDimWallpaper() {
        when(mWallpaperManager.getWallpaperDimAmount()).thenReturn(0.6f);
        mApplier.apply(NO_EFFECTS, ORIGIN_INIT);
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0f));

        String zenLog = getZenLog();
        assertThat(zenLog).contains("apply_device_effect: dimWallpaper (reset on boot) -> false");
    }

    @Test
    public void apply_onBootWithUnrelatedDim_doesNotForceRemoveDimWallpaper() {
        when(mWallpaperManager.getWallpaperDimAmount()).thenReturn(0.42f);
        mApplier.apply(NO_EFFECTS, ORIGIN_INIT);
        verify(mWallpaperManager, never()).setWallpaperDimAmount(anyFloat());
    }

    @Test
    public void apply_notOnBoot_doesNotForceRemoveDimWallpaper() {
        when(mWallpaperManager.getWallpaperDimAmount()).thenReturn(0.6f);
        mApplier.apply(NO_EFFECTS, ORIGIN_APP);
        verify(mWallpaperManager, never()).setWallpaperDimAmount(anyFloat());
    }
}