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

Commit 8e83eb43 authored by Tyler Freeman's avatar Tyler Freeman Committed by Android (Google) Code Review
Browse files

Revert "feat(force invert): force force-dark if force invert is ..."

Revert submission 24432530

Reason for revert: https://buganizer.corp.google.com/issues/297916356

Reverted changes: /q/submissionid:24432530

Change-Id: If5f5d2c523b4139f49d2e8610ecc87c424ea3826
parent 1a286c06
Loading
Loading
Loading
Loading
+3 −21
Original line number Original line Diff line number Diff line
@@ -152,7 +152,6 @@ import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.SystemProperties;
import android.os.Trace;
import android.os.Trace;
import android.os.UserHandle;
import android.os.UserHandle;
import android.provider.Settings;
import android.sysprop.DisplayProperties;
import android.sysprop.DisplayProperties;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
import android.util.AndroidRuntimeException;
@@ -1727,21 +1726,8 @@ public final class ViewRootImpl implements ViewParent,
        return getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
        return getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    }
    }


    /** Returns true if force dark should be enabled according to various settings */
    private void updateForceDarkMode() {
    @VisibleForTesting
        if (mAttachInfo.mThreadedRenderer == null) return;
    public boolean isForceDarkEnabled() {
        boolean isForceInvertEnabled = Settings.Secure.getIntForUser(
                mContext.getContentResolver(),
                Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED,
                /* def= */ 0,
                UserHandle.myUserId()) == 1;
        // 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) {
            return true;
        }


        boolean useAutoDark = getNightMode() == Configuration.UI_MODE_NIGHT_YES;
        boolean useAutoDark = getNightMode() == Configuration.UI_MODE_NIGHT_YES;


@@ -1753,12 +1739,8 @@ public final class ViewRootImpl implements ViewParent,
                    && a.getBoolean(R.styleable.Theme_forceDarkAllowed, forceDarkAllowedDefault);
                    && a.getBoolean(R.styleable.Theme_forceDarkAllowed, forceDarkAllowedDefault);
            a.recycle();
            a.recycle();
        }
        }
        return useAutoDark;
    }


    private void updateForceDarkMode() {
        if (mAttachInfo.mThreadedRenderer.setForceDark(useAutoDark)) {
        if (mAttachInfo.mThreadedRenderer == null) return;
        if (mAttachInfo.mThreadedRenderer.setForceDark(isForceDarkEnabled())) {
            // TODO: Don't require regenerating all display lists to apply this setting
            // TODO: Don't require regenerating all display lists to apply this setting
            invalidateWorld(mView);
            invalidateWorld(mView);
        }
        }
+0 −111
Original line number Original line Diff line number Diff line
@@ -38,17 +38,12 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.fail;
import static org.junit.Assume.assumeTrue;


import android.app.Instrumentation;
import android.app.Instrumentation;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Context;
import android.hardware.display.DisplayManagerGlobal;
import android.hardware.display.DisplayManagerGlobal;
import android.os.Binder;
import android.os.Binder;
import android.os.SystemProperties;
import android.platform.test.annotations.Presubmit;
import android.platform.test.annotations.Presubmit;
import android.provider.Settings;
import android.util.Log;
import android.view.WindowInsets.Side;
import android.view.WindowInsets.Side;
import android.view.WindowInsets.Type;
import android.view.WindowInsets.Type;


@@ -57,9 +52,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.platform.app.InstrumentationRegistry;


import com.android.compatibility.common.util.ShellIdentityUtils;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.BeforeClass;
@@ -79,7 +71,6 @@ import java.util.concurrent.TimeUnit;
@SmallTest
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
public class ViewRootImplTest {
public class ViewRootImplTest {
    private static final String TAG = "ViewRootImplTest";


    private ViewRootImpl mViewRootImpl;
    private ViewRootImpl mViewRootImpl;
    private volatile boolean mKeyReceived = false;
    private volatile boolean mKeyReceived = false;
@@ -110,18 +101,6 @@ public class ViewRootImplTest {
                mViewRootImpl = new ViewRootImpl(sContext, sContext.getDisplayNoVerify()));
                mViewRootImpl = new ViewRootImpl(sContext, sContext.getDisplayNoVerify()));
    }
    }


    @After
    public void teardown() {
        ShellIdentityUtils.invokeWithShellPermissions(() -> {
            Settings.Secure.resetToDefaults(sContext.getContentResolver(), TAG);

            var uiModeManager = sContext.getSystemService(UiModeManager.class);
            uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_NO);

            setForceDarkSysProp(false);
        });
    }

    @Test
    @Test
    public void adjustLayoutParamsForCompatibility_layoutFullscreen() {
    public void adjustLayoutParamsForCompatibility_layoutFullscreen() {
        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
        final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(TYPE_APPLICATION);
@@ -421,96 +400,6 @@ public class ViewRootImplTest {
        assertThat(result).isFalse();
        assertThat(result).isFalse();
    }
    }


    @Test
    public void forceInvertOffDarkThemeOff_forceDarkModeDisabled() {
        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.isForceDarkEnabled()).isFalse();
    }

    @Test
    public void forceInvertOnDarkThemeOff_forceDarkModeEnabled() {
        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.isForceDarkEnabled()).isTrue();
    }

    @Test
    public void forceInvertOffForceDarkOff_forceDarkModeDisabled() {
        ShellIdentityUtils.invokeWithShellPermissions(() -> {
            Settings.Secure.putInt(
                    sContext.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED,
                    /* value= */ 0
            );

            // TODO(b/297556388): figure out how to set this without getting blocked by SELinux
            assumeTrue(setForceDarkSysProp(true));
        });

        sInstrumentation.runOnMainSync(() ->
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isFalse();
    }

    @Test
    public void forceInvertOffForceDarkOn_forceDarkModeEnabled() {
        ShellIdentityUtils.invokeWithShellPermissions(() -> {
            Settings.Secure.putInt(
                    sContext.getContentResolver(),
                    Settings.Secure.ACCESSIBILITY_FORCE_INVERT_COLOR_ENABLED,
                    /* value= */ 0
            );

            assumeTrue(setForceDarkSysProp(true));
        });

        sInstrumentation.runOnMainSync(() ->
                mViewRootImpl.updateConfiguration(sContext.getDisplayNoVerify().getDisplayId())
        );

        assertThat(mViewRootImpl.isForceDarkEnabled()).isTrue();
    }

    private boolean setForceDarkSysProp(boolean isForceDarkEnabled) {
        try {
            SystemProperties.set(
                    ThreadedRenderer.DEBUG_FORCE_DARK,
                    Boolean.toString(isForceDarkEnabled)
            );
            return true;
        } catch (Exception e) {
            Log.e(TAG, "Failed to set force_dark sysprop", e);
            return false;
        }
    }

    class KeyView extends View {
    class KeyView extends View {
        KeyView(Context context) {
        KeyView(Context context) {
            super(context);
            super(context);