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

Commit a4340559 authored by Tyler Freeman's avatar Tyler Freeman
Browse files

fix(force invert): cache force dark setting

This optimizes to only reload the setting if it has actually changed.

Bug: 282821643
Test: atest ForceInvertScreenshotTest
Flag: ACONFIG android.view.accessibility.Flags.forceInvertColor DEVELOPMENT

Change-Id: I67029f5835ef2e6ce964eac684651a54dbde2bdf
parent 2e11589a
Loading
Loading
Loading
Loading
+21 −6
Original line number Diff line number Diff line
@@ -396,6 +396,8 @@ 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.
     */
@@ -1601,6 +1603,23 @@ 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.
     */
@@ -1627,6 +1646,7 @@ public final class ViewRootImpl implements ViewParent,
                mForceInvertObserver = new ContentObserver(mHandler) {
                    @Override
                    public void onChange(boolean selfChange) {
                        reloadForceInvertEnabled();
                        updateForceDarkMode();
                    }
                };
@@ -1847,16 +1867,11 @@ public final class ViewRootImpl implements ViewParent,
    @VisibleForTesting
    public @ForceDarkType.ForceDarkTypeDef int determineForceDarkType() {
        if (forceInvertColor()) {
            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) {
            if (isForceInvertEnabled()) {
                return ForceDarkType.FORCE_INVERT_COLOR_DARK;
            }
        }