diff options
author | Tony Wickham <twickham@google.com> | 2016-03-17 15:39:39 -0700 |
---|---|---|
committer | Tony Wickham <twickham@google.com> | 2016-04-01 15:40:57 -0700 |
commit | 827cef203f386cb548b98a7fa9888b75478c8e20 (patch) | |
tree | f310b21ce3092156534cf94602a3ecf25283a44e /src/com/android/launcher3/dynamicui | |
parent | 855b1b5fff5f6f03a641b3b6973780a24fd9641e (diff) | |
download | android_packages_apps_Trebuchet-827cef203f386cb548b98a7fa9888b75478c8e20.tar.gz android_packages_apps_Trebuchet-827cef203f386cb548b98a7fa9888b75478c8e20.tar.bz2 android_packages_apps_Trebuchet-827cef203f386cb548b98a7fa9888b75478c8e20.zip |
Added ColorExtractionService and ExtractedColors.
- Launcher has an instance of ExtractedColors, which is loaded from
LauncherProvider in onCreate() and whenever the wallpaper changes.
- When the wallpaper changes, the ColorExtractionService is started
in the :wallpaper-chooser process.
- ColorExtractionService builds an ExtractedColors instance and saves
it as a String in LauncherProvider.
- When the results are saved, Launcher gets a callback through
LauncherProviderChangeListener and reloads the ExtractedColors.
- Whenever Launcher loads Extractecolors, it also re-colors items
(currently a no-op).
Change-Id: I319e2cfe0a86abcbc6bb39ef6b9fbbcad54ad743
Diffstat (limited to 'src/com/android/launcher3/dynamicui')
3 files changed, 269 insertions, 0 deletions
diff --git a/src/com/android/launcher3/dynamicui/ColorExtractionService.java b/src/com/android/launcher3/dynamicui/ColorExtractionService.java new file mode 100644 index 000000000..95a62b957 --- /dev/null +++ b/src/com/android/launcher3/dynamicui/ColorExtractionService.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.dynamicui; + +import android.app.IntentService; +import android.app.WallpaperManager; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.drawable.BitmapDrawable; +import android.os.Bundle; +import android.support.v7.graphics.Palette; + +import com.android.launcher3.LauncherProvider; +import com.android.launcher3.LauncherSettings; + +/** + * Extracts colors from the wallpaper, and saves results to {@link LauncherProvider}. + */ +public class ColorExtractionService extends IntentService { + + public ColorExtractionService() { + super("ColorExtractionService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); + int wallpaperId = ExtractionUtils.getWallpaperId(wallpaperManager); + ExtractedColors extractedColors = new ExtractedColors(); + if (wallpaperManager.getWallpaperInfo() != null) { + // We can't extract colors from live wallpapers, so just use the default color always. + extractedColors.updatePalette(null); + } else { + Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap(); + Palette palette = Palette.from(wallpaper).generate(); + extractedColors.updatePalette(palette); + } + + // Save the extracted colors and wallpaper id to LauncherProvider. + String colorsString = extractedColors.encodeAsString(); + Bundle extras = new Bundle(); + extras.putInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID, wallpaperId); + extras.putString(LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS, colorsString); + getContentResolver().call( + LauncherSettings.Settings.CONTENT_URI, + LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID, + null, extras); + } +} diff --git a/src/com/android/launcher3/dynamicui/ExtractedColors.java b/src/com/android/launcher3/dynamicui/ExtractedColors.java new file mode 100644 index 000000000..4d17ff764 --- /dev/null +++ b/src/com/android/launcher3/dynamicui/ExtractedColors.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.dynamicui; + +import android.content.Context; +import android.graphics.Color; +import android.support.v7.graphics.Palette; +import android.util.Log; + +import com.android.launcher3.Utilities; + +/** + * Saves and loads colors extracted from the wallpaper, as well as the associated wallpaper id. + */ +public class ExtractedColors { + private static final String TAG = "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 DEFAULT_INDEX = 0; + public static final int VIBRANT_INDEX = 1; + public static final int VIBRANT_DARK_INDEX = 2; + public static final int VIBRANT_LIGHT_INDEX = 3; + public static final int MUTED_INDEX = 4; + public static final int MUTED_DARK_INDEX = 5; + public static final int MUTED_LIGHT_INDEX = 6; + + public static final int NUM_COLOR_PROFILES = 7; + + private static final String COLOR_SEPARATOR = ","; + + private int[] mColors; + + public ExtractedColors() { + mColors = new int[NUM_COLOR_PROFILES]; + } + + public void setColorAtIndex(int index, int color) { + if (index >= 0 && index < mColors.length) { + mColors[index] = color; + } else { + Log.e(TAG, "Attempted to set a color at an invalid index " + index); + } + } + + /** + * Encodes {@link #mColors} as a comma-separated String. + */ + String encodeAsString() { + StringBuilder colorsStringBuilder = new StringBuilder(); + for (int color : mColors) { + colorsStringBuilder.append(color).append(COLOR_SEPARATOR); + } + return colorsStringBuilder.toString(); + } + + /** + * 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}. + */ + public void load(Context context) { + String encodedString = Utilities.getPrefs(context).getString( + ExtractionUtils.EXTRACTED_COLORS_PREFERENCE_KEY, DEFAULT_COLOR + ""); + + decodeFromString(encodedString); + } + + /** @param index must be one of the index values defined at the top of this class. */ + public int getColor(int index) { + if (index >= 0 && index < mColors.length) { + return mColors[index]; + } + return DEFAULT_COLOR; + } + + /** + * Updates colors based on the palette. + * If the palette is null, the default color is used in all cases. + */ + public void updatePalette(Palette palette) { + if (palette == null) { + for (int i = 0; i < NUM_COLOR_PROFILES; i++) { + setColorAtIndex(i, ExtractedColors.DEFAULT_COLOR); + } + } else { + setColorAtIndex(ExtractedColors.DEFAULT_INDEX, + ExtractedColors.DEFAULT_COLOR); + setColorAtIndex(ExtractedColors.VIBRANT_INDEX, + palette.getVibrantColor(ExtractedColors.DEFAULT_COLOR)); + setColorAtIndex(ExtractedColors.VIBRANT_DARK_INDEX, + palette.getDarkVibrantColor(ExtractedColors.DEFAULT_DARK)); + setColorAtIndex(ExtractedColors.VIBRANT_LIGHT_INDEX, + palette.getLightVibrantColor(ExtractedColors.DEFAULT_LIGHT)); + setColorAtIndex(ExtractedColors.MUTED_INDEX, + palette.getMutedColor(ExtractedColors.DEFAULT_COLOR)); + setColorAtIndex(ExtractedColors.MUTED_DARK_INDEX, + palette.getDarkMutedColor(ExtractedColors.DEFAULT_DARK)); + setColorAtIndex(ExtractedColors.MUTED_LIGHT_INDEX, + palette.getLightVibrantColor(ExtractedColors.DEFAULT_LIGHT)); + } + } +} diff --git a/src/com/android/launcher3/dynamicui/ExtractionUtils.java b/src/com/android/launcher3/dynamicui/ExtractionUtils.java new file mode 100644 index 000000000..a2ff60713 --- /dev/null +++ b/src/com/android/launcher3/dynamicui/ExtractionUtils.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher3.dynamicui; + +import android.app.WallpaperManager; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; + +import com.android.launcher3.Utilities; +import com.android.wallpaperpicker.common.WallpaperManagerCompat; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * Contains helper fields and methods related to extracting colors from the wallpaper. + */ +public class ExtractionUtils { + public static final String EXTRACTED_COLORS_PREFERENCE_KEY = "pref_extractedColors"; + public static final String WALLPAPER_ID_PREFERENCE_KEY = "pref_wallpaperId"; + + /** + * Extract colors in the :wallpaper-chooser process, if the wallpaper id has changed. + * When the new colors are saved in the LauncherProvider, + * Launcher will be notified in Launcher#onSettingsChanged(String, String). + */ + public static void startColorExtractionServiceIfNecessary(final Context context) { + // Run on a background thread, since the service is asynchronous anyway. + Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() { + @Override + public void run() { + if (hasWallpaperIdChanged(context)) { + context.startService(new Intent(context, ColorExtractionService.class)); + } + } + }); + } + + private static boolean hasWallpaperIdChanged(Context context) { + if (!Utilities.isNycOrAbove()) { + // TODO: update an id in sharedprefs in onWallpaperChanged broadcast, and read it here. + return false; + } + final SharedPreferences sharedPrefs = Utilities.getPrefs(context); + int wallpaperId = getWallpaperId(WallpaperManager.getInstance(context)); + int savedWallpaperId = sharedPrefs.getInt(ExtractionUtils.WALLPAPER_ID_PREFERENCE_KEY, -1); + return wallpaperId != savedWallpaperId; + } + + public static int getWallpaperId(WallpaperManager wallpaperManager) { + // TODO: use WallpaperManager#getWallpaperId(WallpaperManager.FLAG_SET_SYSTEM) directly. + try { + Method getWallpaperId = WallpaperManager.class.getMethod("getWallpaperId", int.class); + return (int) getWallpaperId.invoke(wallpaperManager, + WallpaperManagerCompat.FLAG_SET_SYSTEM); + } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) { + return -1; + } + } +} |