diff options
Diffstat (limited to 'src/com/android/launcher3/dynamicui/ExtractedColors.java')
-rw-r--r-- | src/com/android/launcher3/dynamicui/ExtractedColors.java | 128 |
1 files changed, 95 insertions, 33 deletions
diff --git a/src/com/android/launcher3/dynamicui/ExtractedColors.java b/src/com/android/launcher3/dynamicui/ExtractedColors.java index 711508ea5..e60a1bd7d 100644 --- a/src/com/android/launcher3/dynamicui/ExtractedColors.java +++ b/src/com/android/launcher3/dynamicui/ExtractedColors.java @@ -16,13 +16,20 @@ package com.android.launcher3.dynamicui; +import android.app.WallpaperManager; import android.content.Context; import android.graphics.Color; +import android.support.annotation.Nullable; import android.support.v4.graphics.ColorUtils; import android.support.v7.graphics.Palette; import android.util.Log; import com.android.launcher3.Utilities; +import com.android.launcher3.config.FeatureFlags; +import com.android.launcher3.dynamicui.colorextraction.ColorExtractor; + +import java.util.ArrayList; +import java.util.Arrays; /** * Saves and loads colors extracted from the wallpaper, as well as the associated wallpaper id. @@ -32,31 +39,56 @@ public class ExtractedColors { public static final int DEFAULT_LIGHT = Color.WHITE; public static final int DEFAULT_DARK = Color.BLACK; - public static final int DEFAULT_COLOR = DEFAULT_LIGHT; // These color profile indices should NOT be changed, since they are used when saving and // loading extracted colors. New colors should always be added at the end. public static final int VERSION_INDEX = 0; public static final int HOTSEAT_INDEX = 1; public static final int STATUS_BAR_INDEX = 2; - // public static final int VIBRANT_INDEX = 2; - // public static final int VIBRANT_DARK_INDEX = 3; - // public static final int VIBRANT_LIGHT_INDEX = 4; - // public static final int MUTED_INDEX = 5; - // public static final int MUTED_DARK_INDEX = 6; - // public static final int MUTED_LIGHT_INDEX = 7; - - public static final int NUM_COLOR_PROFILES = 2; - private static final int VERSION = 1; + public static final int WALLPAPER_VIBRANT_INDEX = 3; + public static final int ALLAPPS_GRADIENT_MAIN_INDEX = 4; + public static final int ALLAPPS_GRADIENT_SECONDARY_INDEX = 5; + + private static final int VERSION; + private static final int[] DEFAULT_VALUES; + + static { + if (FeatureFlags.LAUNCHER3_GRADIENT_ALL_APPS) { + VERSION = 3; + DEFAULT_VALUES = new int[] { + VERSION, // VERSION_INDEX + 0x40FFFFFF, // HOTSEAT_INDEX: White with 25% alpha + DEFAULT_DARK, // STATUS_BAR_INDEX + 0xFFCCCCCC, // WALLPAPER_VIBRANT_INDEX + 0xFF000000, // ALLAPPS_GRADIENT_MAIN_INDEX + 0xFF000000 // ALLAPPS_GRADIENT_SECONDARY_INDEX + }; + } else if (FeatureFlags.QSB_IN_HOTSEAT) { + VERSION = 2; + DEFAULT_VALUES = new int[] { + VERSION, // VERSION_INDEX + 0x40FFFFFF, // HOTSEAT_INDEX: White with 25% alpha + DEFAULT_DARK, // STATUS_BAR_INDEX + 0xFFCCCCCC, // WALLPAPER_VIBRANT_INDEX + }; + } else { + VERSION = 1; + DEFAULT_VALUES = new int[] { + VERSION, // VERSION_INDEX + 0x40FFFFFF, // HOTSEAT_INDEX: White with 25% alpha + DEFAULT_DARK, // STATUS_BAR_INDEX + }; + } + } private static final String COLOR_SEPARATOR = ","; - private int[] mColors; + private final ArrayList<OnChangeListener> mListeners = new ArrayList<>(); + private final int[] mColors; public ExtractedColors() { // The first entry is reserved for the version number. - mColors = new int[NUM_COLOR_PROFILES + 1]; - mColors[VERSION_INDEX] = VERSION; + mColors = Arrays.copyOf(DEFAULT_VALUES, DEFAULT_VALUES.length); } public void setColorAtIndex(int index, int color) { @@ -79,17 +111,6 @@ public class ExtractedColors { } /** - * Decodes a comma-separated String into {@link #mColors}. - */ - void decodeFromString(String colorsString) { - String[] splitColorsString = colorsString.split(COLOR_SEPARATOR); - mColors = new int[splitColorsString.length]; - for (int i = 0; i < mColors.length; i++) { - mColors[i] = Integer.parseInt(splitColorsString[i]); - } - } - - /** * Loads colors and wallpaper id from {@link Utilities#getPrefs(Context)}. * These were saved there in {@link ColorExtractionService}. */ @@ -97,19 +118,22 @@ public class ExtractedColors { String encodedString = Utilities.getPrefs(context).getString( ExtractionUtils.EXTRACTED_COLORS_PREFERENCE_KEY, VERSION + ""); - decodeFromString(encodedString); - - if (mColors[VERSION_INDEX] != VERSION) { + String[] splitColorsString = encodedString.split(COLOR_SEPARATOR); + if (splitColorsString.length == DEFAULT_VALUES.length && + Integer.parseInt(splitColorsString[VERSION_INDEX]) == VERSION) { + // Parse and apply the saved values. + for (int i = 0; i < mColors.length; i++) { + mColors[i] = Integer.parseInt(splitColorsString[i]); + } + } else { + // Leave the values as default values as the saved values may not be compatible. ExtractionUtils.startColorExtractionService(context); } } /** @param index must be one of the index values defined at the top of this class. */ - public int getColor(int index, int defaultColor) { - if (index > VERSION_INDEX && index < mColors.length) { - return mColors[index]; - } - return defaultColor; + public int getColor(int index) { + return mColors[index]; } /** @@ -125,7 +149,7 @@ public class ExtractedColors { } else if (hotseatPalette != null && ExtractionUtils.isSuperDark(hotseatPalette)) { hotseatColor = ColorUtils.setAlphaComponent(Color.WHITE, (int) (0.18f * 255)); } else { - hotseatColor = ColorUtils.setAlphaComponent(Color.WHITE, (int) (0.25f * 255)); + hotseatColor = DEFAULT_VALUES[HOTSEAT_INDEX]; } setColorAtIndex(HOTSEAT_INDEX, hotseatColor); } @@ -134,4 +158,42 @@ public class ExtractedColors { setColorAtIndex(STATUS_BAR_INDEX, ExtractionUtils.isSuperLight(statusBarPalette) ? DEFAULT_LIGHT : DEFAULT_DARK); } + + public void updateWallpaperThemePalette(@Nullable Palette wallpaperPalette) { + int defaultColor = DEFAULT_VALUES[WALLPAPER_VIBRANT_INDEX]; + setColorAtIndex(WALLPAPER_VIBRANT_INDEX, wallpaperPalette == null + ? defaultColor : wallpaperPalette.getVibrantColor(defaultColor)); + } + + public void updateAllAppsGradientPalette(Context context) { + // TODO use isAtLeastO when available + try { + WallpaperManager.class.getDeclaredMethod("getWallpaperColors", int.class); + ColorExtractor extractor = new ColorExtractor(context); + ColorExtractor.GradientColors colors = extractor.getColors(WallpaperManager.FLAG_SYSTEM); + setColorAtIndex(ALLAPPS_GRADIENT_MAIN_INDEX, colors.getMainColor()); + setColorAtIndex(ALLAPPS_GRADIENT_SECONDARY_INDEX, colors.getSecondaryColor()); + } catch (NoSuchMethodException e) { + setColorAtIndex(ALLAPPS_GRADIENT_MAIN_INDEX, Color.WHITE); + setColorAtIndex(ALLAPPS_GRADIENT_SECONDARY_INDEX, Color.WHITE); + } + } + + public void addOnChangeListener(OnChangeListener listener) { + mListeners.add(listener); + } + + public void notifyChange() { + for (OnChangeListener listener : mListeners) { + listener.onExtractedColorsChanged(); + } + } + + /** + * Interface for listening for extracted color changes + */ + public interface OnChangeListener { + + void onExtractedColorsChanged(); + } } |