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

Unverified Commit 002a97c9 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊 Committed by Michael Bestas
Browse files

SystemUI: Support black theme for dark mode



Co-authored-by: default avatarJesse Chan <jc@lineageos.org>
Co-authored-by: default avatarMichael Bestas <mkbestas@gmail.com>
Co-authored-by: default avatarIdo Ben-Hur <idoybh2@gmail.com>
Co-authored-by: default avatarPranav Vashi <neobuddy89@gmail.com>
Change-Id: I57cd53de8f2c1e4d445441b514875b6af915b858
parent 44903d4d
Loading
Loading
Loading
Loading
+41 −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,45 @@ 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);
        }
        OverlayInfo overlayInfo = mOverlayManager.getOverlayInfo(name, UserHandle.CURRENT);
        if (overlayInfo != null)
            return overlayInfo.getOverlayIdentifier();
        throw new IllegalStateException("No overlay found for " + name);
    }

    @VisibleForTesting
    protected OverlayManagerTransaction.Builder getTransactionBuilder() {
        return new OverlayManagerTransaction.Builder();
+35 −4
Original line number Diff line number Diff line
@@ -96,6 +96,8 @@ import com.google.ux.material.libmonet.dynamiccolor.MaterialDynamicColors;
import kotlinx.coroutines.flow.Flow;
import kotlinx.coroutines.flow.StateFlow;

import lineageos.providers.LineageSettings;

import org.json.JSONException;
import org.json.JSONObject;

@@ -524,6 +526,27 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
            });
        }

        mSecureSettings.registerContentObserverForUserSync(
                LineageSettings.Secure.getUriFor(LineageSettings.Secure.BERRY_BLACK_THEME),
                false,
                new ContentObserver(mBgHandler) {
                    @Override
                    public void onChange(boolean selfChange, Collection<Uri> collection, int flags,
                            int userId) {
                        if (DEBUG) Log.d(TAG, "Overlay changed for user: " + userId);
                        if (mUserTracker.getUserId() != userId) {
                            return;
                        }
                        if (!mDeviceProvisionedController.isUserSetup(userId)) {
                            Log.i(TAG, "Theme application deferred when setting changed.");
                            mDeferredThemeEvaluation = true;
                            return;
                        }
                        reevaluateSystemTheme(true /* forceReload */);
                    }
                },
                UserHandle.USER_ALL);

        // All wallpaper color and keyguard logic only applies when Monet is enabled.
        if (!mIsMonetEnabled) {
            return;
@@ -819,10 +842,14 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {
            }
        }

        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) {
                && mNeutralOverlay != null && !isBlackMode) {
            categoryToPackage.put(OVERLAY_CATEGORY_SYSTEM_PALETTE,
                    mNeutralOverlay.getIdentifier());
        }
@@ -858,9 +885,13 @@ public class ThemeOverlayController implements CoreStartable, Dumpable {

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

        mThemeManager.applyCurrentUserOverlays(categoryToPackage, fOverlays, currentUser,