From 94568f1e74e08e8397338d232eac80a866971301 Mon Sep 17 00:00:00 2001 From: Ching-Sung Li Date: Wed, 27 Oct 2021 23:44:30 +0800 Subject: Fix specified wallpaper color option missing after reboot Not only check latest wallpaper type (FLAG_SYSTEM, FLAG_LOCK) but also check if the wallpaper color is specified to determine if we reset the theme overlay setting. Bug: 198449419 Test: atest ThemeOverlayControllerTest Change-Id: Ic10c10c6fbccee0f330c67ae8b9bacb947c5e035 --- .../systemui/theme/ThemeOverlayController.java | 33 ++++++++++- .../systemui/theme/ThemeOverlayControllerTest.java | 69 +++++++++++++++++----- 2 files changed, 85 insertions(+), 17 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java index 426bc91a606d..592fa152b406 100644 --- a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java +++ b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java @@ -176,6 +176,34 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { ? WallpaperManager.FLAG_LOCK : WallpaperManager.FLAG_SYSTEM; } + private boolean isSeedColorSet(JSONObject jsonObject, WallpaperColors newWallpaperColors) { + if (newWallpaperColors == null) { + return false; + } + // Gets the color that was overridden in the theme setting if any. + String sysPaletteColor = (String) jsonObject.opt(OVERLAY_CATEGORY_SYSTEM_PALETTE); + if (sysPaletteColor == null) { + return false; + } + if (!sysPaletteColor.startsWith("#")) { + sysPaletteColor = "#" + sysPaletteColor; + } + final int systemPaletteColorArgb = Color.parseColor(sysPaletteColor); + // Gets seed colors from incoming {@link WallpaperColors} instance. + List seedColors = ColorScheme.getSeedColors(newWallpaperColors); + for (int seedColor : seedColors) { + // The seed color from incoming {@link WallpaperColors} instance + // was set as color override. + if (seedColor == systemPaletteColorArgb) { + if (DEBUG) { + Log.d(TAG, "Same as previous set system palette: " + sysPaletteColor); + } + return true; + } + } + return false; + } + private void handleWallpaperColors(WallpaperColors wallpaperColors, int flags) { final boolean hadWallpaperColors = mCurrentColors != null; int latestWallpaperType = getLatestWallpaperType(); @@ -213,8 +241,11 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { try { JSONObject jsonObject = (overlayPackageJson == null) ? new JSONObject() : new JSONObject(overlayPackageJson); + // The latest applied wallpaper should be the source of system colors when: + // There is not preset color applied and the incoming wallpaper color is not applied if (!COLOR_SOURCE_PRESET.equals(jsonObject.optString(OVERLAY_COLOR_SOURCE)) - && ((flags & latestWallpaperType) != 0)) { + && ((flags & latestWallpaperType) != 0 && !isSeedColorSet(jsonObject, + wallpaperColors))) { mSkipSettingChange = true; if (jsonObject.has(OVERLAY_CATEGORY_ACCENT_COLOR) || jsonObject.has( OVERLAY_CATEGORY_SYSTEM_PALETTE)) { diff --git a/packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java index 766471b9a695..3ff5666271bf 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/theme/ThemeOverlayControllerTest.java @@ -248,8 +248,9 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( @@ -274,14 +275,15 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { } @Test - public void onWallpaperColorsChanged_ResetThemeWithDifferentWallpapers() { + public void onWallpaperColorsChanged_ResetThemeWithNewHomeWallpapers() { // Should ask for a new theme when wallpaper colors 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( @@ -304,14 +306,15 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { } @Test - public void onWallpaperColorsChanged_ResetThemeWithSameWallpaper() { + public void onWallpaperColorsChanged_ResetThemeWithNewHomeAndLockWallpaper() { // Should ask for a new theme when wallpaper colors 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( @@ -339,8 +342,9 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt())) @@ -366,8 +370,9 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { 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\":\"lock_wallpaper\"," + "{\"android.theme.customization.color_source\":\"lock_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), anyInt())) @@ -394,8 +399,9 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( @@ -415,6 +421,36 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { .applyCurrentUserOverlays(any(), any(), anyInt(), any()); } + @Test + public void onWallpaperColorsChanged_keepThemeWhenFromLatestWallpaperAndSpecifiedColor() { + // Shouldn't ask for a new theme when the colors of the last applied wallpaper change + // with the same specified system palette one. + WallpaperColors mainColors = new WallpaperColors(Color.valueOf(Color.RED), + Color.valueOf(0xffa16b00), null); + + String jsonString = + "{\"android.theme.customization.color_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + + "\"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 updatedSetting = ArgumentCaptor.forClass(String.class); + verify(mSecureSettings, never()).putString( + eq(Settings.Secure.THEME_CUSTOMIZATION_OVERLAY_PACKAGES), updatedSetting.capture()); + + // Apply overlay by existing theme from secure setting + 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 @@ -423,8 +459,9 @@ public class ThemeOverlayControllerTest extends SysuiTestCase { 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_source\":\"home_wallpaper\"," + + "\"android.theme.customization.system_palette\":\"A16B00\"," + + "\"android.theme.customization.accent_color\":\"A16B00\"," + "\"android.theme.customization.color_index\":\"2\"}"; when(mSecureSettings.getStringForUser( -- cgit v1.2.3-59-g8ed1b