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

Commit 8bd951c7 authored by Pranav Vashi's avatar Pranav Vashi Committed by Bruno Martins
Browse files

ThemeOverlayController: Ensure we always disable the neutral overlay

* Skipping neutral overlays is not enough. We need to explicitly disable neutral overlays
  and enable black theme.
* Code ref for getOverlayID: https://github.com/yaap/packages_apps_YASP/commit/fda23fa5e9ac4e44f9ac2786afbf531a307be4a3



Change-Id: Ie4310d65dadb71d7151b2e1a87eea92dec5f0d55
Signed-off-by: default avatarPranav Vashi <neobuddy89@gmail.com>
parent 24d0a54d
Loading
Loading
Loading
Loading
+42 −0
Original line number Diff line number Diff line
@@ -67,6 +67,9 @@ public class ThemeOverlayApplier implements Dumpable {
    @VisibleForTesting
    static final String SYSUI_PACKAGE = "com.android.systemui";

    static final String OVERLAY_BLACK_THEME =
            "org.lineageos.overlay.customization.blacktheme";

    static final String OVERLAY_CATEGORY_DYNAMIC_COLOR =
            "android.theme.customization.dynamic_color";
    static final String OVERLAY_CATEGORY_ACCENT_COLOR =
@@ -205,6 +208,7 @@ public class ThemeOverlayApplier implements Dumpable {
    ) {

        mBgExecutor.execute(() -> {
            boolean isBlackMode = false;

            // Disable all overlays that have not been specified in the user setting.
            final Set<String> overlayCategoriesToDisable = new HashSet<>(THEME_CATEGORIES);
@@ -228,6 +232,7 @@ public class ThemeOverlayApplier implements Dumpable {
            OverlayManagerTransaction.Builder transaction = getTransactionBuilder();
            HashSet<OverlayIdentifier> identifiersPending = new HashSet<>();
            if (pendingCreation != null) {
                isBlackMode = pendingCreation.length == 2;
                for (FabricatedOverlay overlay : pendingCreation) {
                    identifiersPending.add(overlay.getIdentifier());
                    transaction.registerFabricatedOverlay(overlay);
@@ -257,9 +262,46 @@ public class ThemeOverlayApplier implements Dumpable {
            } catch (SecurityException | IllegalStateException e) {
                Log.e(TAG, "setEnabled failed", e);
            }

            checkDarkUserOverlays(currentUser, onComplete, isBlackMode);
        });
    }

    private void checkDarkUserOverlays(
            int currentUser,
            Runnable onComplete,
            boolean isBlackMode
    ) {
        OverlayManagerTransaction.Builder transaction = getTransactionBuilder();
        try {
            transaction.setEnabled(getOverlayID(OVERLAY_BLACK_THEME), isBlackMode, currentUser);
            transaction.setEnabled(getOverlayID("android:neutral"), !isBlackMode, currentUser);
            mOverlayManager.commit(transaction.build());
            if (onComplete != null) {
                Log.d(TAG, "Executing onComplete runnable");
                mMainExecutor.execute(onComplete);
            }
        } catch (SecurityException | IllegalStateException e) {
            Log.e(TAG, "setEnabled failed", e);
        }
    }

    private OverlayIdentifier getOverlayID(String name) throws IllegalStateException {
        if (name.contains(":")) {
            final String[] value = name.split(":");
            final String pkgName = value[0];
            final String overlayName = value[1];
            final List<OverlayInfo> infos =
                    mOverlayManager.getOverlayInfosForTarget(pkgName, UserHandle.CURRENT);
            for (OverlayInfo info : infos) {
                if (overlayName.equals(info.getOverlayName()))
                    return info.getOverlayIdentifier();
            }
            throw new IllegalStateException("No overlay found for " + name);
        }
        return mOverlayManager.getOverlayInfo(name, UserHandle.CURRENT).getOverlayIdentifier();
    }

    @VisibleForTesting
    protected OverlayManagerTransaction.Builder getTransactionBuilder() {
        return new OverlayManagerTransaction.Builder();
+6 −37
Original line number Diff line number Diff line
@@ -41,9 +41,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.om.FabricatedOverlay;
import android.content.om.IOverlayManager;
import android.content.om.OverlayIdentifier;
import android.content.om.OverlayInfo;
import android.content.pm.UserInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
@@ -51,8 +49,6 @@ import android.database.ContentObserver;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
@@ -124,8 +120,6 @@ import javax.inject.Inject;
@SysUISingleton
public class ThemeOverlayController implements CoreStartable, Dumpable {
    protected static final String TAG = "ThemeOverlayController";
    protected static final String OVERLAY_BERRY_BLACK_THEME =
            "org.lineageos.overlay.customization.blacktheme";
    private static final boolean DEBUG = true;

    private final ThemeOverlayApplier mThemeManager;
@@ -176,8 +170,6 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
    private ColorScheme mDarkColorScheme;
    private ColorScheme mLightColorScheme;

    private IOverlayManager mOverlayManager;

    // Defers changing themes until Setup Wizard is done.
    private boolean mDeferredThemeEvaluation;
    // Determines if we should ignore THEME_CUSTOMIZATION_OVERLAY_PACKAGES setting changes.
@@ -448,9 +440,6 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
        mUiModeManager = uiModeManager;
        mActivityManager = activityManager;
        dumpManager.registerDumpable(TAG, this);

        mOverlayManager = IOverlayManager.Stub.asInterface(
                ServiceManager.getService(Context.OVERLAY_SERVICE));
    }

    @Override
@@ -772,9 +761,6 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
    }

    private void updateThemeOverlays() {
        boolean skipNeutral;
        boolean enableNeutral;

        final int currentUser = mUserTracker.getUserId();
        final String overlayPackageJson = mSecureSettings.getStringForUser(
                Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES,
@@ -826,23 +812,14 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
            }
        }

        if (mOverlayManager != null) {
            OverlayInfo info = null;
            try {
                info = mOverlayManager.getOverlayInfo(OVERLAY_BERRY_BLACK_THEME,
                        mUserTracker.getUserId());
            } catch (RemoteException e) {
                Log.e(TAG, "Failed getting overlay " + OVERLAY_BERRY_BLACK_THEME + " info");
                e.printStackTrace();
            }
            skipNeutral = isNightMode() && info != null && info.isEnabled();
            enableNeutral = !isNightMode() && info != null && info.isEnabled();
        }
        boolean isBlackMode = (LineageSettings.Secure.getIntForUser(
                mContext.getContentResolver(), LineageSettings.Secure.BERRY_BLACK_THEME,
                0, currentUser) == 1) && isNightMode();

        // Compatibility with legacy themes, where full packages were defined, instead of just
        // colors.
        if (!categoryToPackage.containsKey(OVERLAY_CATEGORY_SYSTEM_PALETTE)
                && mNeutralOverlay != null && (!skipNeutral || enableNeutral)) {
                && mNeutralOverlay != null && !isBlackMode) {
            categoryToPackage.put(OVERLAY_CATEGORY_SYSTEM_PALETTE,
                    mNeutralOverlay.getIdentifier());
        }
@@ -855,14 +832,6 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
            categoryToPackage.put(OVERLAY_CATEGORY_DYNAMIC_COLOR, mDynamicOverlay.getIdentifier());
        }

        boolean isBlackMode = (LineageSettings.Secure.getIntForUser(
                mContext.getContentResolver(), LineageSettings.Secure.BERRY_BLACK_THEME,
                0, currentUser) == 1) && isNightMode();
        if (categoryToPackage.containsKey(OVERLAY_CATEGORY_SYSTEM_PALETTE) && isBlackMode) {
            OverlayIdentifier blackTheme = new OverlayIdentifier(OVERLAY_BERRY_BLACK_THEME);
            categoryToPackage.put(OVERLAY_CATEGORY_SYSTEM_PALETTE, blackTheme);
        }

        Set<UserHandle> managedProfiles = new HashSet<>();
        for (UserInfo userInfo : mUserManager.getEnabledProfiles(currentUser)) {
            if (userInfo.isProfile()) {
@@ -886,10 +855,10 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {

        if (mNeedsOverlayCreation) {
            mNeedsOverlayCreation = false;
            fOverlays = new FabricatedOverlay[skipNeutral ? 2 : 3];
            fOverlays = new FabricatedOverlay[isBlackMode ? 2 : 3];
            int c = 0;
            fOverlays[c++] = mSecondaryOverlay;
            if (!skipNeutral || enableNeutral) {
            if (!isBlackMode) {
                fOverlays[c++] = mNeutralOverlay;
            }
            fOverlays[c++] = mDynamicOverlay;