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

Commit e331fe8d authored by Santiago Etchebehere's avatar Santiago Etchebehere
Browse files

Use the colors from the last wallpaper applied

The latest applied wallpaper should be the source of system
colors

Bug: 186498019
Test: atest ThemeOverlayControllerTest
Change-Id: Ie76163a7fa124f499759043f261ad5a1ce700cd9
parent 76660ef7
Loading
Loading
Loading
Loading
+19 −11
Original line number Original line Diff line number Diff line
@@ -103,11 +103,11 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
    private final boolean mIsMonetEnabled;
    private final boolean mIsMonetEnabled;
    private UserTracker mUserTracker;
    private UserTracker mUserTracker;
    private DeviceProvisionedController mDeviceProvisionedController;
    private DeviceProvisionedController mDeviceProvisionedController;
    private WallpaperColors mSystemColors;
    private WallpaperColors mCurrentColors;
    private WallpaperManager mWallpaperManager;
    private WallpaperManager mWallpaperManager;
    // If fabricated overlays were already created for the current theme.
    // If fabricated overlays were already created for the current theme.
    private boolean mNeedsOverlayCreation;
    private boolean mNeedsOverlayCreation;
    // Dominant olor extracted from wallpaper, NOT the color used on the overlay
    // Dominant color extracted from wallpaper, NOT the color used on the overlay
    protected int mMainWallpaperColor = Color.TRANSPARENT;
    protected int mMainWallpaperColor = Color.TRANSPARENT;
    // Accent color extracted from wallpaper, NOT the color used on the overlay
    // Accent color extracted from wallpaper, NOT the color used on the overlay
    protected int mWallpaperAccentColor = Color.TRANSPARENT;
    protected int mWallpaperAccentColor = Color.TRANSPARENT;
@@ -162,10 +162,17 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
        handleWallpaperColors(wallpaperColors, which);
        handleWallpaperColors(wallpaperColors, which);
    };
    };


    private int getLatestWallpaperType() {
        return mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK)
                > mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM)
                ? WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM;
    }

    private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags) {
    private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags) {
        final boolean hadWallpaperColors = mSystemColors != null;
        final boolean hadWallpaperColors = mCurrentColors != null;
        if ((flags & WallpaperManager.FLAG_SYSTEM) != 0) {
        int latestWallpaperType = getLatestWallpaperType();
            mSystemColors = wallpaperColors;
        if ((flags & latestWallpaperType) != 0) {
            mCurrentColors = wallpaperColors;
            if (DEBUG) Log.d(TAG, "got new colors: " + wallpaperColors + " where: " + flags);
            if (DEBUG) Log.d(TAG, "got new colors: " + wallpaperColors + " where: " + flags);
        }
        }


@@ -183,7 +190,7 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
            } else {
            } else {
                if (DEBUG) {
                if (DEBUG) {
                    Log.i(TAG, "During user setup, but allowing first color event: had? "
                    Log.i(TAG, "During user setup, but allowing first color event: had? "
                            + hadWallpaperColors + " has? " + (mSystemColors != null));
                            + hadWallpaperColors + " has? " + (mCurrentColors != null));
                }
                }
            }
            }
        }
        }
@@ -198,7 +205,8 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
        if (!TextUtils.isEmpty(overlayPackageJson)) {
        if (!TextUtils.isEmpty(overlayPackageJson)) {
            try {
            try {
                JSONObject jsonObject = new JSONObject(overlayPackageJson);
                JSONObject jsonObject = new JSONObject(overlayPackageJson);
                if (!COLOR_SOURCE_PRESET.equals(jsonObject.optString(OVERLAY_COLOR_SOURCE))) {
                if (!COLOR_SOURCE_PRESET.equals(jsonObject.optString(OVERLAY_COLOR_SOURCE))
                        && ((flags & latestWallpaperType) != 0)) {
                    mSkipSettingChange = true;
                    mSkipSettingChange = true;
                    if (jsonObject.has(OVERLAY_CATEGORY_ACCENT_COLOR) || jsonObject.has(
                    if (jsonObject.has(OVERLAY_CATEGORY_ACCENT_COLOR) || jsonObject.has(
                            OVERLAY_CATEGORY_SYSTEM_PALETTE)) {
                            OVERLAY_CATEGORY_SYSTEM_PALETTE)) {
@@ -314,10 +322,10 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
        // Upon boot, make sure we have the most up to date colors
        // Upon boot, make sure we have the most up to date colors
        Runnable updateColors = () -> {
        Runnable updateColors = () -> {
            WallpaperColors systemColor = mWallpaperManager.getWallpaperColors(
            WallpaperColors systemColor = mWallpaperManager.getWallpaperColors(
                    WallpaperManager.FLAG_SYSTEM);
                    getLatestWallpaperType());
            mMainExecutor.execute(() -> {
            mMainExecutor.execute(() -> {
                if (DEBUG) Log.d(TAG, "Boot colors: " + systemColor);
                if (DEBUG) Log.d(TAG, "Boot colors: " + systemColor);
                mSystemColors = systemColor;
                mCurrentColors = systemColor;
                reevaluateSystemTheme(false /* forceReload */);
                reevaluateSystemTheme(false /* forceReload */);
            });
            });
        };
        };
@@ -348,7 +356,7 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {
    }
    }


    private void reevaluateSystemTheme(boolean forceReload) {
    private void reevaluateSystemTheme(boolean forceReload) {
        final WallpaperColors currentColors = mSystemColors;
        final WallpaperColors currentColors = mCurrentColors;
        final int mainColor;
        final int mainColor;
        final int accentCandidate;
        final int accentCandidate;
        if (currentColors == null) {
        if (currentColors == null) {
@@ -506,7 +514,7 @@ public class ThemeOverlayController extends SystemUI implements Dumpable {


    @Override
    @Override
    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
        pw.println("mSystemColors=" + mSystemColors);
        pw.println("mSystemColors=" + mCurrentColors);
        pw.println("mMainWallpaperColor=" + Integer.toHexString(mMainWallpaperColor));
        pw.println("mMainWallpaperColor=" + Integer.toHexString(mMainWallpaperColor));
        pw.println("mWallpaperAccentColor=" + Integer.toHexString(mWallpaperAccentColor));
        pw.println("mWallpaperAccentColor=" + Integer.toHexString(mWallpaperAccentColor));
        pw.println("mSecondaryOverlay=" + mSecondaryOverlay);
        pw.println("mSecondaryOverlay=" + mSecondaryOverlay);
+58 −0
Original line number Original line Diff line number Diff line
@@ -258,6 +258,7 @@ public class ThemeOverlayControllerTest extends SysuiTestCase {
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt()))
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt()))
                .thenReturn(jsonString);
                .thenReturn(jsonString);
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK)).thenReturn(20);
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK)).thenReturn(20);
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM)).thenReturn(21);


        mColorsListener.getValue().onColorsChanged(mainColors, WallpaperManager.FLAG_SYSTEM);
        mColorsListener.getValue().onColorsChanged(mainColors, WallpaperManager.FLAG_SYSTEM);


@@ -301,6 +302,63 @@ public class ThemeOverlayControllerTest extends SysuiTestCase {
                .applyCurrentUserOverlays(any(), any(), anyInt(), any());
                .applyCurrentUserOverlays(any(), any(), anyInt(), any());
    }
    }


    @Test
    public void onWallpaperColorsChanged_ResetThemeWhenFromLatestWallpaper() {
        // Should ask for a new theme when the colors of the last applied wallpaper change
        WallpaperColors mainColors = new WallpaperColors(Color.valueOf(Color.RED),
                Color.valueOf(Color.BLUE), null);

        String jsonString =
                "{\"android.theme.customization.system_palette\":\"override.package.name\","
                        + "\"android.theme.customization.color_source\":\"home_wallpaper\","
                        + "\"android.theme.customization.color_index\":\"2\"}";

        when(mSecureSettings.getStringForUser(
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt()))
                .thenReturn(jsonString);
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK)).thenReturn(1);
        // SYSTEM wallpaper is the last applied one
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM)).thenReturn(2);

        mColorsListener.getValue().onColorsChanged(mainColors, WallpaperManager.FLAG_SYSTEM);

        ArgumentCaptor<String> updatedSetting = ArgumentCaptor.forClass(String.class);
        verify(mSecureSettings).putString(
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), updatedSetting.capture());

        verify(mThemeOverlayApplier)
                .applyCurrentUserOverlays(any(), any(), anyInt(), any());
    }

    @Test
    public void onWallpaperColorsChanged_keepThemeIfNotLatestWallpaper() {
        // Shouldn't ask for a new theme when the colors of the wallpaper that is not the last
        // applied one change
        WallpaperColors mainColors = new WallpaperColors(Color.valueOf(Color.RED),
                Color.valueOf(Color.BLUE), null);

        String jsonString =
                "{\"android.theme.customization.system_palette\":\"override.package.name\","
                        + "\"android.theme.customization.color_source\":\"home_wallpaper\","
                        + "\"android.theme.customization.color_index\":\"2\"}";

        when(mSecureSettings.getStringForUser(
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt()))
                .thenReturn(jsonString);
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_LOCK)).thenReturn(1);
        // SYSTEM wallpaper is the last applied one
        when(mWallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM)).thenReturn(2);

        mColorsListener.getValue().onColorsChanged(mainColors, WallpaperManager.FLAG_LOCK);

        verify(mSecureSettings, never()).putString(
                eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), any());


        verify(mThemeOverlayApplier, never())
                .applyCurrentUserOverlays(any(), any(), anyInt(), any());
    }

    @Test
    @Test
    public void onProfileAdded_setsTheme() {
    public void onProfileAdded_setsTheme() {
        mBroadcastReceiver.getValue().onReceive(null,
        mBroadcastReceiver.getValue().onReceive(null,