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

Commit eb64e170 authored by Marcelo Arteiro's avatar Marcelo Arteiro
Browse files

Fixes inability to toggle dark mode when in a mode.

Bug: 374703607
Change-Id: Ifb2a021bdcd04be09556bb69c0e9e292aaf720e0
Test: Manual
Flag: EXEMPT bugfix
parent 34117946
Loading
Loading
Loading
Loading
+11 −9
Original line number Original line Diff line number Diff line
@@ -885,8 +885,6 @@ final class UiModeManagerService extends SystemService {
                                ? customModeType
                                ? customModeType
                                : MODE_NIGHT_CUSTOM_TYPE_UNKNOWN;
                                : MODE_NIGHT_CUSTOM_TYPE_UNKNOWN;
                        mNightMode.set(mode);
                        mNightMode.set(mode);
                        //deactivates AttentionMode if user toggles DarkTheme
                        mAttentionModeThemeOverlay = MODE_ATTENTION_THEME_OVERLAY_OFF;
                        resetNightModeOverrideLocked();
                        resetNightModeOverrideLocked();
                        persistNightMode(user);
                        persistNightMode(user);
                        // on screen off will update configuration instead
                        // on screen off will update configuration instead
@@ -1009,15 +1007,16 @@ final class UiModeManagerService extends SystemService {


        @Override
        @Override
        public boolean setNightModeActivatedForCustomMode(int modeNightCustomType, boolean active) {
        public boolean setNightModeActivatedForCustomMode(int modeNightCustomType, boolean active) {
            return setNightModeActivatedForModeInternal(modeNightCustomType, active);
            return setNightModeActivatedForModeInternal(modeNightCustomType, active, false);
        }
        }


        @Override
        @Override
        public boolean setNightModeActivated(boolean active) {
        public boolean setNightModeActivated(boolean active) {
            return setNightModeActivatedForModeInternal(mNightModeCustomType, active);
            return setNightModeActivatedForModeInternal(mNightModeCustomType, active, true);
        }
        }


        private boolean setNightModeActivatedForModeInternal(int modeCustomType, boolean active) {
        private boolean setNightModeActivatedForModeInternal(int modeCustomType,
            boolean active, boolean isUserInteraction) {
            if (getContext().checkCallingOrSelfPermission(
            if (getContext().checkCallingOrSelfPermission(
                    android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
                    android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
                    != PackageManager.PERMISSION_GRANTED) {
                    != PackageManager.PERMISSION_GRANTED) {
@@ -1053,13 +1052,16 @@ final class UiModeManagerService extends SystemService {
                        mOverrideNightModeOn = active;
                        mOverrideNightModeOn = active;
                        mOverrideNightModeUser = mCurrentUser;
                        mOverrideNightModeUser = mCurrentUser;
                        persistNightModeOverrides(mCurrentUser);
                        persistNightModeOverrides(mCurrentUser);
                    } else if (mNightMode.get() == UiModeManager.MODE_NIGHT_NO
                    } else if (mNightMode.get() == UiModeManager.MODE_NIGHT_NO && active) {
                            && active) {
                        mNightMode.set(UiModeManager.MODE_NIGHT_YES);
                        mNightMode.set(UiModeManager.MODE_NIGHT_YES);
                    } else if (mNightMode.get() == UiModeManager.MODE_NIGHT_YES
                    } else if (mNightMode.get() == UiModeManager.MODE_NIGHT_YES && !active) {
                            && !active) {
                        mNightMode.set(UiModeManager.MODE_NIGHT_NO);
                        mNightMode.set(UiModeManager.MODE_NIGHT_NO);
                    }
                    }

                    if (isUserInteraction) {
                        // deactivates AttentionMode if user toggles DarkTheme
                        mAttentionModeThemeOverlay = MODE_ATTENTION_THEME_OVERLAY_OFF;
                    }
                    updateConfigurationLocked();
                    updateConfigurationLocked();
                    applyConfigurationExternallyLocked();
                    applyConfigurationExternallyLocked();
                    persistNightMode(mCurrentUser);
                    persistNightMode(mCurrentUser);
+20 −9
Original line number Original line Diff line number Diff line
@@ -1460,9 +1460,12 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
        verify(mInjector).startDreamWhenDockedIfAppropriate(mContext);
        verify(mInjector).startDreamWhenDockedIfAppropriate(mContext);
    }
    }


    private void testAttentionModeThemeOverlay(boolean modeNight) throws RemoteException {
    // Test the attention mode overlay with all the possible attention modes and the initial night
    // mode state. Also tests if the attention mode is turned off when the night mode is toggled by
    // the user.
    private void testAttentionModeThemeOverlay(boolean initialNightMode) throws RemoteException {
        //setup
        //setup
        if (modeNight) {
        if (initialNightMode) {
            mService.setNightMode(MODE_NIGHT_YES);
            mService.setNightMode(MODE_NIGHT_YES);
            assertTrue(mUiManagerService.getConfiguration().isNightModeActive());
            assertTrue(mUiManagerService.getConfiguration().isNightModeActive());
        } else {
        } else {
@@ -1472,21 +1475,29 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {


        // attention modes with expected night modes
        // attention modes with expected night modes
        Map<Integer, Boolean> modes = Map.of(
        Map<Integer, Boolean> modes = Map.of(
                MODE_ATTENTION_THEME_OVERLAY_OFF, modeNight,
                MODE_ATTENTION_THEME_OVERLAY_OFF, initialNightMode,
                MODE_ATTENTION_THEME_OVERLAY_DAY, false,
                MODE_ATTENTION_THEME_OVERLAY_DAY, false,
                MODE_ATTENTION_THEME_OVERLAY_NIGHT, true
                MODE_ATTENTION_THEME_OVERLAY_NIGHT, true
        );
        );


        // test
        // test
        for (int aMode : modes.keySet()) {
        for (int attentionMode : modes.keySet()) {
            try {
            try {
                mService.setAttentionModeThemeOverlay(aMode);
                mService.setAttentionModeThemeOverlay(attentionMode);


                int appliedAMode = mService.getAttentionModeThemeOverlay();
                int appliedAMode = mService.getAttentionModeThemeOverlay();
                boolean nMode = modes.get(aMode);
                boolean expectedNightMode = modes.get(attentionMode);


                assertEquals(aMode, appliedAMode);
                assertEquals(attentionMode, appliedAMode);
                assertEquals(isNightModeActivated(), nMode);
                assertEquals(expectedNightMode, isNightModeActivated());

                // If attentionMode is active, flip the night mode and assets
                // the attention mode is disabled
                if (attentionMode != MODE_ATTENTION_THEME_OVERLAY_OFF) {
                    mService.setNightModeActivated(!expectedNightMode);
                    assertEquals(MODE_ATTENTION_THEME_OVERLAY_OFF,
                            mService.getAttentionModeThemeOverlay());
                }
            } catch (RemoteException e) {
            } catch (RemoteException e) {
                fail("Error communicating with server: " + e.getMessage());
                fail("Error communicating with server: " + e.getMessage());
            }
            }