Loading core/java/android/view/ViewRootImpl.java +33 −32 Original line number Diff line number Diff line Loading @@ -153,6 +153,7 @@ import android.annotation.UiContext; import android.app.ActivityManager; import android.app.ActivityThread; import android.app.ResourcesManager; import android.app.UiModeManager; import android.app.WindowConfiguration; import android.app.compat.CompatChanges; import android.app.servertransaction.WindowStateTransactionItem; Loading Loading @@ -196,6 +197,7 @@ import android.hardware.display.DisplayManagerGlobal; import android.hardware.input.InputManagerGlobal; import android.hardware.input.InputSettings; import android.media.AudioManager; import android.net.Uri; import android.os.Binder; import android.os.Build; import android.os.Bundle; Loading Loading @@ -472,8 +474,6 @@ public final class ViewRootImpl implements ViewParent, @Nullable private ContentObserver mForceInvertObserver; private static final int INVALID_VALUE = Integer.MIN_VALUE; private int mForceInvertEnabled = INVALID_VALUE; /** * Callback for notifying about global configuration changes. */ Loading Loading @@ -555,6 +555,8 @@ public final class ViewRootImpl implements ViewParent, @UiContext public final Context mContext; private UiModeManager mUiModeManager; @UnsupportedAppUsage final IWindowSession mWindowSession; @NonNull Display mDisplay; Loading Loading @@ -1804,23 +1806,6 @@ public final class ViewRootImpl implements ViewParent, } } private boolean isForceInvertEnabled() { if (mForceInvertEnabled == INVALID_VALUE) { reloadForceInvertEnabled(); } return mForceInvertEnabled == 1; } private void reloadForceInvertEnabled() { if (forceInvertColor()) { mForceInvertEnabled = Settings.Secure.getIntForUser( mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* def= */ 0, UserHandle.myUserId()); } } /** * Register any kind of listeners if setView was success. */ Loading Loading @@ -1856,20 +1841,25 @@ public final class ViewRootImpl implements ViewParent, mForceInvertObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange) { reloadForceInvertEnabled(); updateForceDarkMode(); } }; mContext.getContentResolver().registerContentObserver( final Uri[] urisToObserve = { Settings.Secure.getUriFor( Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED ), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED), Settings.Secure.getUriFor(Settings.Secure.UI_NIGHT_MODE) }; for (Uri uri : urisToObserve) { mContext.getContentResolver().registerContentObserver( uri, false, mForceInvertObserver, UserHandle.myUserId()); } } } } /** * Unregister all listeners while detachedFromWindow. Loading Loading @@ -2073,21 +2063,25 @@ public final class ViewRootImpl implements ViewParent, return getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; } /** Returns true if force dark should be enabled according to various settings */ /** * Determines the type of force dark to apply, considering force inversion, system night mode, * and app-specific settings (including developer opt-outs). * * @return A {@link ForceDarkType.ForceDarkTypeDef} constant indicating the force dark type. */ @VisibleForTesting public @ForceDarkType.ForceDarkTypeDef int determineForceDarkType() { if (forceInvertColor()) { // Force invert ignores all developer opt-outs. // We also ignore dark theme, since the app developer can override the user's preference // for dark mode in configuration.uiMode. Instead, we assume that the force invert // setting will be enabled at the same time dark theme is in the Settings app. if (isForceInvertEnabled()) { // for dark mode in configuration.uiMode. Instead, we assume that both force invert and // the system's dark theme are enabled. if (getUiModeManager().getForceInvertState() == UiModeManager.FORCE_INVERT_TYPE_DARK) { return ForceDarkType.FORCE_INVERT_COLOR_DARK; } } boolean useAutoDark = getNightMode() == Configuration.UI_MODE_NIGHT_YES; if (useAutoDark) { boolean forceDarkAllowedDefault = SystemProperties.getBoolean(ThreadedRenderer.DEBUG_FORCE_DARK, false); Loading Loading @@ -9401,6 +9395,13 @@ public final class ViewRootImpl implements ViewParent, return mAudioManager; } private UiModeManager getUiModeManager() { if (mUiModeManager == null) { mUiModeManager = mContext.getSystemService(UiModeManager.class); } return mUiModeManager; } private Vibrator getSystemVibrator() { if (mVibrator == null) { mVibrator = mContext.getSystemService(Vibrator.class); Loading core/tests/coretests/src/android/view/ViewRootImplTest.java +71 −44 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.view; import static android.app.UiModeManager.MODE_NIGHT_NO; import static android.app.UiModeManager.MODE_NIGHT_YES; import static android.util.SequenceUtils.getInitSeq; import static android.view.HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING; import static android.view.InputDevice.SOURCE_ROTARY_ENCODER; Loading Loading @@ -67,8 +69,10 @@ import static org.junit.Assume.assumeTrue; import android.annotation.NonNull; import android.app.Instrumentation; import android.app.UiModeManager; import android.app.UiModeManager.ForceInvertType; import android.content.Context; import android.graphics.ForceDarkType; import android.graphics.ForceDarkType.ForceDarkTypeDef; import android.graphics.Rect; import android.hardware.display.DisplayManagerGlobal; import android.os.Binder; Loading @@ -93,9 +97,12 @@ import androidx.test.filters.SmallTest; import androidx.test.platform.app.InstrumentationRegistry; import com.android.compatibility.common.util.ShellIdentityUtils; import com.android.compatibility.common.util.TestUtils; import com.android.cts.input.BlockingQueueEventVerifier; import com.android.window.flags.Flags; import com.google.common.truth.Expect; import org.hamcrest.Matcher; import org.junit.After; import org.junit.AfterClass; Loading Loading @@ -124,6 +131,8 @@ public class ViewRootImplTest { @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Rule public final Expect mExpect = Expect.create(); private ViewRootImpl mViewRootImpl; private View mView; Loading Loading @@ -1507,49 +1516,34 @@ public class ViewRootImplTest { } @Test public void forceInvertOffDarkThemeOff_forceDarkModeDisabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* value= */ 0 ); var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE); } @Test public void forceInvertOnDarkThemeOff_forceDarkModeEnabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* value= */ 1 ); var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); assertThat(mViewRootImpl.determineForceDarkType()) .isEqualTo(ForceDarkType.FORCE_INVERT_COLOR_DARK); @RequiresFlagsEnabled(FLAG_FORCE_INVERT_COLOR) public void updateConfiguration_returnsExpectedForceDarkMode() { waitForSystemNightModeActivated(true); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_DARK, ForceDarkType.FORCE_INVERT_COLOR_DARK); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_DARK, ForceDarkType.FORCE_INVERT_COLOR_DARK); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); waitForSystemNightModeActivated(false); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); } @Test @EnableFlags(FLAG_FORCE_INVERT_COLOR) public void forceInvertOffForceDarkOff_forceDarkModeDisabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Loading @@ -1562,15 +1556,14 @@ public class ViewRootImplTest { }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE); } @Test @EnableFlags(FLAG_FORCE_INVERT_COLOR) public void forceInvertOffForceDarkOn_forceDarkModeEnabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Loading @@ -1582,8 +1575,7 @@ public class ViewRootImplTest { }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.FORCE_DARK); } Loading Loading @@ -1790,4 +1782,39 @@ public class ViewRootImplTest { () -> view.getViewTreeObserver().removeOnDrawListener(listener)); } } private void waitForSystemNightModeActivated(boolean active) { ShellIdentityUtils.invokeWithShellPermissions(() -> sInstrumentation.runOnMainSync(() -> { var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightModeActivated(active); })); sInstrumentation.waitForIdleSync(); } private void verifyForceDarkType(boolean isAppInNightMode, boolean isForceInvertEnabled, @ForceInvertType int expectedForceInvertType, @ForceDarkTypeDef int expectedForceDarkType) { var uiModeManager = sContext.getSystemService(UiModeManager.class); ShellIdentityUtils.invokeWithShellPermissions(() -> { uiModeManager.setApplicationNightMode( isAppInNightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO); Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, isForceInvertEnabled ? 1 : 0); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); try { TestUtils.waitUntil("Waiting for force invert state changed", () -> (uiModeManager.getForceInvertState() == expectedForceInvertType)); } catch (Exception e) { Log.e(TAG, "Unexpected error trying to apply force invert state. " + e); e.printStackTrace(); } mExpect.that(mViewRootImpl.determineForceDarkType()).isEqualTo(expectedForceDarkType); } } Loading
core/java/android/view/ViewRootImpl.java +33 −32 Original line number Diff line number Diff line Loading @@ -153,6 +153,7 @@ import android.annotation.UiContext; import android.app.ActivityManager; import android.app.ActivityThread; import android.app.ResourcesManager; import android.app.UiModeManager; import android.app.WindowConfiguration; import android.app.compat.CompatChanges; import android.app.servertransaction.WindowStateTransactionItem; Loading Loading @@ -196,6 +197,7 @@ import android.hardware.display.DisplayManagerGlobal; import android.hardware.input.InputManagerGlobal; import android.hardware.input.InputSettings; import android.media.AudioManager; import android.net.Uri; import android.os.Binder; import android.os.Build; import android.os.Bundle; Loading Loading @@ -472,8 +474,6 @@ public final class ViewRootImpl implements ViewParent, @Nullable private ContentObserver mForceInvertObserver; private static final int INVALID_VALUE = Integer.MIN_VALUE; private int mForceInvertEnabled = INVALID_VALUE; /** * Callback for notifying about global configuration changes. */ Loading Loading @@ -555,6 +555,8 @@ public final class ViewRootImpl implements ViewParent, @UiContext public final Context mContext; private UiModeManager mUiModeManager; @UnsupportedAppUsage final IWindowSession mWindowSession; @NonNull Display mDisplay; Loading Loading @@ -1804,23 +1806,6 @@ public final class ViewRootImpl implements ViewParent, } } private boolean isForceInvertEnabled() { if (mForceInvertEnabled == INVALID_VALUE) { reloadForceInvertEnabled(); } return mForceInvertEnabled == 1; } private void reloadForceInvertEnabled() { if (forceInvertColor()) { mForceInvertEnabled = Settings.Secure.getIntForUser( mContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* def= */ 0, UserHandle.myUserId()); } } /** * Register any kind of listeners if setView was success. */ Loading Loading @@ -1856,20 +1841,25 @@ public final class ViewRootImpl implements ViewParent, mForceInvertObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange) { reloadForceInvertEnabled(); updateForceDarkMode(); } }; mContext.getContentResolver().registerContentObserver( final Uri[] urisToObserve = { Settings.Secure.getUriFor( Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED ), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED), Settings.Secure.getUriFor(Settings.Secure.UI_NIGHT_MODE) }; for (Uri uri : urisToObserve) { mContext.getContentResolver().registerContentObserver( uri, false, mForceInvertObserver, UserHandle.myUserId()); } } } } /** * Unregister all listeners while detachedFromWindow. Loading Loading @@ -2073,21 +2063,25 @@ public final class ViewRootImpl implements ViewParent, return getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; } /** Returns true if force dark should be enabled according to various settings */ /** * Determines the type of force dark to apply, considering force inversion, system night mode, * and app-specific settings (including developer opt-outs). * * @return A {@link ForceDarkType.ForceDarkTypeDef} constant indicating the force dark type. */ @VisibleForTesting public @ForceDarkType.ForceDarkTypeDef int determineForceDarkType() { if (forceInvertColor()) { // Force invert ignores all developer opt-outs. // We also ignore dark theme, since the app developer can override the user's preference // for dark mode in configuration.uiMode. Instead, we assume that the force invert // setting will be enabled at the same time dark theme is in the Settings app. if (isForceInvertEnabled()) { // for dark mode in configuration.uiMode. Instead, we assume that both force invert and // the system's dark theme are enabled. if (getUiModeManager().getForceInvertState() == UiModeManager.FORCE_INVERT_TYPE_DARK) { return ForceDarkType.FORCE_INVERT_COLOR_DARK; } } boolean useAutoDark = getNightMode() == Configuration.UI_MODE_NIGHT_YES; if (useAutoDark) { boolean forceDarkAllowedDefault = SystemProperties.getBoolean(ThreadedRenderer.DEBUG_FORCE_DARK, false); Loading Loading @@ -9401,6 +9395,13 @@ public final class ViewRootImpl implements ViewParent, return mAudioManager; } private UiModeManager getUiModeManager() { if (mUiModeManager == null) { mUiModeManager = mContext.getSystemService(UiModeManager.class); } return mUiModeManager; } private Vibrator getSystemVibrator() { if (mVibrator == null) { mVibrator = mContext.getSystemService(Vibrator.class); Loading
core/tests/coretests/src/android/view/ViewRootImplTest.java +71 −44 Original line number Diff line number Diff line Loading @@ -16,6 +16,8 @@ package android.view; import static android.app.UiModeManager.MODE_NIGHT_NO; import static android.app.UiModeManager.MODE_NIGHT_YES; import static android.util.SequenceUtils.getInitSeq; import static android.view.HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING; import static android.view.InputDevice.SOURCE_ROTARY_ENCODER; Loading Loading @@ -67,8 +69,10 @@ import static org.junit.Assume.assumeTrue; import android.annotation.NonNull; import android.app.Instrumentation; import android.app.UiModeManager; import android.app.UiModeManager.ForceInvertType; import android.content.Context; import android.graphics.ForceDarkType; import android.graphics.ForceDarkType.ForceDarkTypeDef; import android.graphics.Rect; import android.hardware.display.DisplayManagerGlobal; import android.os.Binder; Loading @@ -93,9 +97,12 @@ import androidx.test.filters.SmallTest; import androidx.test.platform.app.InstrumentationRegistry; import com.android.compatibility.common.util.ShellIdentityUtils; import com.android.compatibility.common.util.TestUtils; import com.android.cts.input.BlockingQueueEventVerifier; import com.android.window.flags.Flags; import com.google.common.truth.Expect; import org.hamcrest.Matcher; import org.junit.After; import org.junit.AfterClass; Loading Loading @@ -124,6 +131,8 @@ public class ViewRootImplTest { @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule(); @Rule public final Expect mExpect = Expect.create(); private ViewRootImpl mViewRootImpl; private View mView; Loading Loading @@ -1507,49 +1516,34 @@ public class ViewRootImplTest { } @Test public void forceInvertOffDarkThemeOff_forceDarkModeDisabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* value= */ 0 ); var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE); } @Test public void forceInvertOnDarkThemeOff_forceDarkModeEnabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, /* value= */ 1 ); var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); assertThat(mViewRootImpl.determineForceDarkType()) .isEqualTo(ForceDarkType.FORCE_INVERT_COLOR_DARK); @RequiresFlagsEnabled(FLAG_FORCE_INVERT_COLOR) public void updateConfiguration_returnsExpectedForceDarkMode() { waitForSystemNightModeActivated(true); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_DARK, ForceDarkType.FORCE_INVERT_COLOR_DARK); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_DARK, ForceDarkType.FORCE_INVERT_COLOR_DARK); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); waitForSystemNightModeActivated(false); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ true, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ true, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); verifyForceDarkType(/* isAppInNightMode= */ false, /* isForceInvertEnabled= */ false, UiModeManager.FORCE_INVERT_TYPE_OFF, ForceDarkType.NONE); } @Test @EnableFlags(FLAG_FORCE_INVERT_COLOR) public void forceInvertOffForceDarkOff_forceDarkModeDisabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Loading @@ -1562,15 +1556,14 @@ public class ViewRootImplTest { }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.NONE); } @Test @EnableFlags(FLAG_FORCE_INVERT_COLOR) public void forceInvertOffForceDarkOn_forceDarkModeEnabled() { mSetFlagsRule.enableFlags(FLAG_FORCE_INVERT_COLOR); ShellIdentityUtils.invokeWithShellPermissions(() -> { Settings.Secure.putInt( sContext.getContentResolver(), Loading @@ -1582,8 +1575,7 @@ public class ViewRootImplTest { }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId()) ); mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); assertThat(mViewRootImpl.determineForceDarkType()).isEqualTo(ForceDarkType.FORCE_DARK); } Loading Loading @@ -1790,4 +1782,39 @@ public class ViewRootImplTest { () -> view.getViewTreeObserver().removeOnDrawListener(listener)); } } private void waitForSystemNightModeActivated(boolean active) { ShellIdentityUtils.invokeWithShellPermissions(() -> sInstrumentation.runOnMainSync(() -> { var uiModeManager = sContext.getSystemService(UiModeManager.class); uiModeManager.setNightModeActivated(active); })); sInstrumentation.waitForIdleSync(); } private void verifyForceDarkType(boolean isAppInNightMode, boolean isForceInvertEnabled, @ForceInvertType int expectedForceInvertType, @ForceDarkTypeDef int expectedForceDarkType) { var uiModeManager = sContext.getSystemService(UiModeManager.class); ShellIdentityUtils.invokeWithShellPermissions(() -> { uiModeManager.setApplicationNightMode( isAppInNightMode ? MODE_NIGHT_YES : MODE_NIGHT_NO); Settings.Secure.putInt( sContext.getContentResolver(), Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED, isForceInvertEnabled ? 1 : 0); }); sInstrumentation.runOnMainSync(() -> mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())); try { TestUtils.waitUntil("Waiting for force invert state changed", () -> (uiModeManager.getForceInvertState() == expectedForceInvertType)); } catch (Exception e) { Log.e(TAG, "Unexpected error trying to apply force invert state. " + e); e.printStackTrace(); } mExpect.that(mViewRootImpl.determineForceDarkType()).isEqualTo(expectedForceDarkType); } }