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

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

Use new UiModeManager APIs for toggling night mode

Fixes: 314285749
Test: atest DefaultDeviceEffectsApplierTest

Change-Id: I4ea80afa4d186a4d8e3b3f9b1d493a47d3df4d4c
parent 3df98340
Loading
Loading
Loading
Loading
+5 −5
Original line number Original line Diff line number Diff line
@@ -16,7 +16,8 @@


package com.android.server.notification;
package com.android.server.notification;


import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_NIGHT;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_OFF;


import android.app.UiModeManager;
import android.app.UiModeManager;
import android.app.WallpaperManager;
import android.app.WallpaperManager;
@@ -128,10 +129,9 @@ class DefaultDeviceEffectsApplier implements DeviceEffectsApplier {


    private void updateNightModeImmediately(boolean useNightMode) {
    private void updateNightModeImmediately(boolean useNightMode) {
        Binder.withCleanCallingIdentity(() -> {
        Binder.withCleanCallingIdentity(() -> {
            // TODO: b/314285749 - Placeholder; use real APIs when available.
            mUiModeManager.setAttentionModeThemeOverlay(
            mUiModeManager.setNightModeCustomType(MODE_NIGHT_CUSTOM_TYPE_BEDTIME);
                    useNightMode ? MODE_ATTENTION_THEME_OVERLAY_NIGHT
            mUiModeManager.setNightModeActivatedForCustomMode(MODE_NIGHT_CUSTOM_TYPE_BEDTIME,
                            : MODE_ATTENTION_THEME_OVERLAY_OFF);
                    useNightMode);
        });
        });
    }
    }


+32 −12
Original line number Original line Diff line number Diff line
@@ -16,7 +16,8 @@


package com.android.server.notification;
package com.android.server.notification;


import static android.app.UiModeManager.MODE_NIGHT_CUSTOM_TYPE_BEDTIME;
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.UPDATE_ORIGIN_APP;
import static android.service.notification.ZenModeConfig.UPDATE_ORIGIN_APP;
import static android.service.notification.ZenModeConfig.UPDATE_ORIGIN_USER;
import static android.service.notification.ZenModeConfig.UPDATE_ORIGIN_USER;


@@ -121,28 +122,49 @@ public class DefaultDeviceEffectsApplierTest {
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
        verify(mColorDisplayManager).setSaturationLevel(eq(0));
        verify(mColorDisplayManager).setSaturationLevel(eq(0));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
        verify(mUiModeManager).setNightModeActivatedForCustomMode(
        verify(mUiModeManager).setAttentionModeThemeOverlay(eq(MODE_ATTENTION_THEME_OVERLAY_NIGHT));
                eq(MODE_NIGHT_CUSTOM_TYPE_BEDTIME), eq(true));
    }
    }


    @Test
    @Test
    public void apply_removesPreviouslyAppliedEffects() {
    public void apply_removesEffects() {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);


        ZenDeviceEffects previousEffects = new ZenDeviceEffects.Builder()
        ZenDeviceEffects previousEffects = new ZenDeviceEffects.Builder()
                .setShouldSuppressAmbientDisplay(true)
                .setShouldSuppressAmbientDisplay(true)
                .setShouldDimWallpaper(true)
                .setShouldDimWallpaper(true)
                .setShouldDisplayGrayscale(true)
                .setShouldUseNightMode(true)
                .build();
                .build();
        mApplier.apply(previousEffects, UPDATE_ORIGIN_USER);
        mApplier.apply(previousEffects, UPDATE_ORIGIN_USER);
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));
        verify(mColorDisplayManager).setSaturationLevel(eq(0));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
        verify(mUiModeManager).setAttentionModeThemeOverlay(eq(MODE_ATTENTION_THEME_OVERLAY_NIGHT));


        ZenDeviceEffects noEffects = new ZenDeviceEffects.Builder().build();
        ZenDeviceEffects noEffects = new ZenDeviceEffects.Builder().build();
        mApplier.apply(noEffects, UPDATE_ORIGIN_USER);
        mApplier.apply(noEffects, UPDATE_ORIGIN_USER);


        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(false));
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(false));
        verify(mColorDisplayManager).setSaturationLevel(eq(100));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.0f));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.0f));
        verifyZeroInteractions(mColorDisplayManager, mUiModeManager);
        verify(mUiModeManager).setAttentionModeThemeOverlay(eq(MODE_ATTENTION_THEME_OVERLAY_OFF));
    }

    @Test
    public void apply_removesOnlyPreviouslyAppliedEffects() {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);

        ZenDeviceEffects previousEffects = new ZenDeviceEffects.Builder()
                .setShouldSuppressAmbientDisplay(true)
                .build();
        mApplier.apply(previousEffects, UPDATE_ORIGIN_USER);
        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(true));

        ZenDeviceEffects noEffects = new ZenDeviceEffects.Builder().build();
        mApplier.apply(noEffects, UPDATE_ORIGIN_USER);

        verify(mPowerManager).suppressAmbientDisplay(anyString(), eq(false));
        verifyZeroInteractions(mColorDisplayManager, mWallpaperManager, mUiModeManager);
    }
    }


    @Test
    @Test
@@ -150,6 +172,7 @@ public class DefaultDeviceEffectsApplierTest {
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
        mSetFlagsRule.enableFlags(android.app.Flags.FLAG_MODES_API);
        mContext.addMockSystemService(ColorDisplayManager.class, null);
        mContext.addMockSystemService(ColorDisplayManager.class, null);
        mContext.addMockSystemService(WallpaperManager.class, null);
        mContext.addMockSystemService(WallpaperManager.class, null);
        mApplier = new DefaultDeviceEffectsApplier(mContext);


        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
        ZenDeviceEffects effects = new ZenDeviceEffects.Builder()
                .setShouldSuppressAmbientDisplay(true)
                .setShouldSuppressAmbientDisplay(true)
@@ -177,7 +200,7 @@ public class DefaultDeviceEffectsApplierTest {
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));
        verify(mWallpaperManager).setWallpaperDimAmount(eq(0.6f));


        verify(mPowerManager, never()).suppressAmbientDisplay(anyString(), anyBoolean());
        verify(mPowerManager, never()).suppressAmbientDisplay(anyString(), anyBoolean());
        verify(mUiModeManager, never()).setNightModeActivatedForCustomMode(anyInt(), anyBoolean());
        verify(mUiModeManager, never()).setAttentionModeThemeOverlay(anyInt());
    }
    }


    @Test
    @Test
@@ -223,8 +246,7 @@ public class DefaultDeviceEffectsApplierTest {
        screenOffReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));
        screenOffReceiver.onReceive(mContext, new Intent(Intent.ACTION_SCREEN_OFF));


        // So the effect is applied, and we stopped listening for this event.
        // So the effect is applied, and we stopped listening for this event.
        verify(mUiModeManager).setNightModeActivatedForCustomMode(
        verify(mUiModeManager).setAttentionModeThemeOverlay(eq(MODE_ATTENTION_THEME_OVERLAY_NIGHT));
                eq(MODE_NIGHT_CUSTOM_TYPE_BEDTIME), eq(true));
        verify(mContext).unregisterReceiver(eq(screenOffReceiver));
        verify(mContext).unregisterReceiver(eq(screenOffReceiver));
    }
    }


@@ -239,8 +261,7 @@ public class DefaultDeviceEffectsApplierTest {
                origin.value());
                origin.value());


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


@@ -256,8 +277,7 @@ public class DefaultDeviceEffectsApplierTest {
                origin.value());
                origin.value());


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