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

Commit 5af7d3b2 authored by Candice's avatar Candice Committed by Candice Lo
Browse files

Fix the timing of updating ForceInvert state and add corresponding tests

1. Use the mComputedNightMode kept in the UiModeManagerService to
   identify the system mode to avoid extra access to the SystemUi
   context and reflect the actual system theme
2. Update ForceInvert state when the mComputedNightMode is
   updated(content observer to the settings key UI_NIGHT_MODE is not
   sufficient since it will stay in MODE_NIGHT_CUSTOM if user toggle
   DarkTheme when they have a schedule)
3. Add tests to cover the logic for getForceInvertState

Bug: 404722593
Bug: 405366199
Test: atest UiModeManagerServiceTest
Flag: android.view.accessibility.force_invert_color
Change-Id: I6b8048602d26d22adeb42e54bf502fa2ee5f46fa
parent d9a023d1
Loading
Loading
Loading
Loading
+4 −10
Original line number Diff line number Diff line
@@ -47,7 +47,6 @@ import android.annotation.Nullable;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityTaskManager;
import android.app.ActivityThread;
import android.app.AlarmManager;
import android.app.IOnProjectionStateChangedListener;
import android.app.IUiModeManager;
@@ -1445,12 +1444,13 @@ final class UiModeManagerService extends SystemService {
     *
     * @hide
     */
    private int getForceInvertStateInternal() {
    @VisibleForTesting
    int getForceInvertStateInternal() {
        if (!android.view.accessibility.Flags.forceInvertColor()) {
            return FORCE_INVERT_TYPE_OFF;
        }

        if (!isSystemInDarkTheme()) {
        if (!mComputedNightMode) {
            return FORCE_INVERT_TYPE_OFF;
        }

@@ -1461,13 +1461,6 @@ final class UiModeManagerService extends SystemService {
        return FORCE_INVERT_TYPE_DARK;
    }

    private boolean isSystemInDarkTheme() {
        Context sysUiContext = ActivityThread.currentActivityThread().getSystemUiContext();
        int sysUiNightMode = sysUiContext.getResources().getConfiguration().uiMode
                & Configuration.UI_MODE_NIGHT_MASK;
        return sysUiNightMode == Configuration.UI_MODE_NIGHT_YES;
    }

    private boolean isForceInvert() {
        return Settings.Secure.getIntForUser(
                getContext().getContentResolver(),
@@ -2215,6 +2208,7 @@ final class UiModeManagerService extends SystemService {
            case (UiModeManager.MODE_ATTENTION_THEME_OVERLAY_DAY) -> false;
            default -> newComputedValue; // case OFF
        };
        updateForceInvertStates();

        if (appliedOverrides) {
            return;
+41 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
package com.android.server;

import static android.Manifest.permission.MODIFY_DAY_NIGHT_MODE;
import static android.app.UiModeManager.FORCE_INVERT_TYPE_DARK;
import static android.app.UiModeManager.FORCE_INVERT_TYPE_OFF;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_DAY;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_NIGHT;
import static android.app.UiModeManager.MODE_ATTENTION_THEME_OVERLAY_OFF;
@@ -89,6 +91,7 @@ import android.os.Process;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.test.FakePermissionEnforcer;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.service.dreams.DreamManagerInternal;
@@ -1514,6 +1517,44 @@ public class UiModeManagerServiceTest extends UiServiceTestCase {
        testAttentionModeThemeOverlay(true);
    }

    @Test
    @EnableFlags(android.view.accessibility.Flags.FLAG_FORCE_INVERT_COLOR)
    public void getForceInvertState_nightModeFalse_returnsOff() throws RemoteException {
        mService.setNightModeActivated(false);

        assertThat(mUiManagerService.getForceInvertStateInternal())
                .isEqualTo(FORCE_INVERT_TYPE_OFF);
    }

    @Test
    @EnableFlags(android.view.accessibility.Flags.FLAG_FORCE_INVERT_COLOR)
    public void getForceInvertState_nightModeTrueAndForceInvertOff_returnsOff()
            throws RemoteException {
        mService.setNightModeActivated(true);

        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED,
                /* value= */ 0);

        assertThat(mUiManagerService.getForceInvertStateInternal())
                .isEqualTo(FORCE_INVERT_TYPE_OFF);
    }

    @Test
    @EnableFlags(android.view.accessibility.Flags.FLAG_FORCE_INVERT_COLOR)
    public void getForceInvertState_nightModeTrueAndForceInvertOn_returnsDark() throws Exception {
        mService.setNightModeActivated(true);

        Settings.Secure.putInt(
                mContentResolver,
                Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED,
                /* value= */ 1);

        assertThat(mUiManagerService.getForceInvertStateInternal())
                .isEqualTo(FORCE_INVERT_TYPE_DARK);
    }

    private void triggerDockIntent() {
        final Intent dockedIntent =
                new Intent(Intent.ACTION_DOCK_EVENT)