From c390f83eb0a16d81949e92c8bdc213db04e2ebd6 Mon Sep 17 00:00:00 2001 From: Lucas Dupin Date: Mon, 14 Dec 2020 16:57:46 -0800 Subject: [PATCH] Allow custom implementation of color extraction Relevant methods can be overriden on an OEM defined implementation Test: atest ThemeOverlayControllerTest Test: dumpsys Bug: 174676673 Change-Id: Ifb6a35b347871af89b7fd25602bc242a90972945 --- .../theme/ThemeOverlayController.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java index 006ecb937922c..a287e57d36610 100644 --- a/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java +++ b/packages/SystemUI/src/com/android/systemui/theme/ThemeOverlayController.java @@ -98,9 +98,9 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { private WallpaperColors mLockColors; private WallpaperColors mSystemColors; // Color extracted from wallpaper, NOT the color used on the overlay - private int mMainWallpaperColor = Color.TRANSPARENT; + protected int mMainWallpaperColor = Color.TRANSPARENT; // Color extracted from wallpaper, NOT the color used on the overlay - private int mWallpaperAccentColor = Color.TRANSPARENT; + protected int mWallpaperAccentColor = Color.TRANSPARENT; // Main system color that maps to an overlay color private int mSystemOverlayColor = Color.TRANSPARENT; // Accent color that maps to an overlay color @@ -200,31 +200,26 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { } private void reevaluateSystemTheme() { - if (mLockColors == null && mSystemColors == null) { - Log.w(TAG, "Cannot update theme, colors are null"); - return; - } - WallpaperColors currentColor = + WallpaperColors currentColors = mKeyguardStateController.isShowing() && mLockColors != null ? mLockColors : mSystemColors; - int mainColor = currentColor.getPrimaryColor().toArgb(); - //TODO(b/172860591) implement more complex logic for picking accent color. - //For now, picking the secondary should be enough. - Color accentCandidate = currentColor.getSecondaryColor(); - if (accentCandidate == null) { - accentCandidate = currentColor.getTertiaryColor(); - } - if (accentCandidate == null) { - accentCandidate = currentColor.getPrimaryColor(); + final int mainColor; + final int accentCandidate; + if (currentColors == null) { + mainColor = Color.TRANSPARENT; + accentCandidate = Color.TRANSPARENT; + } else { + mainColor = getDominantColor(currentColors); + accentCandidate = getAccentColor(currentColors); } - if (mMainWallpaperColor == mainColor && mWallpaperAccentColor == accentCandidate.toArgb()) { + if (mMainWallpaperColor == mainColor && mWallpaperAccentColor == accentCandidate) { return; } mMainWallpaperColor = mainColor; - mWallpaperAccentColor = accentCandidate.toArgb(); + mWallpaperAccentColor = accentCandidate; // Let's compare these colors to our finite set of overlays, and then pick an overlay. List systemColors = mThemeManager.getAvailableSystemColors(); @@ -243,11 +238,29 @@ public class ThemeOverlayController extends SystemUI implements Dumpable { updateThemeOverlays(); } + /** + * Return the main theme color from a given {@link WallpaperColors} instance. + */ + protected int getDominantColor(@NonNull WallpaperColors wallpaperColors) { + return wallpaperColors.getPrimaryColor().toArgb(); + } + + protected int getAccentColor(@NonNull WallpaperColors wallpaperColors) { + Color accentCandidate = wallpaperColors.getSecondaryColor(); + if (accentCandidate == null) { + accentCandidate = wallpaperColors.getTertiaryColor(); + } + if (accentCandidate == null) { + accentCandidate = wallpaperColors.getPrimaryColor(); + } + return accentCandidate.toArgb(); + } + /** * Given a color and a list of candidates, return the candidate that's the most similar to the * given color. */ - private static int getClosest(List candidates, int color) { + protected int getClosest(List candidates, int color) { float[] hslMain = new float[3]; float[] hslCandidate = new float[3];