Using BitmapRegionDecoder to only parse partial bitmap during color extraction

Change-Id: I3474c5e2f880144379b1810ccaef247f4a8d1330
This commit is contained in:
Sunny Goyal committed 2016-11-07 10:08:21 -08:00
1 parent bf7d8121ea
commit 1b997d3383
2 files changed
+43 -38

No files matched your search

@@ -16,24 +16,35 @@
package com.android.launcher3.dynamicui;
import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.v7.graphics.Palette;
import android.util.Log;
import com.android.launcher3.LauncherProvider;
import com.android.launcher3.LauncherSettings;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags;
import java.io.IOException;
/**
* Extracts colors from the wallpaper, and saves results to {@link LauncherProvider}.
*/
public class ColorExtractionService extends IntentService {
private static final String TAG = "ColorExtractionService";
/** The fraction of the wallpaper to extract colors for use on the hotseat. */
private static final float HOTSEAT_FRACTION = 1f / 4;
@@ -45,27 +56,20 @@ public class ColorExtractionService extends IntentService {
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);
extractedColors.updateHotseatPalette(null);
} else {
Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
Palette palette = Palette.from(wallpaper).generate();
extractedColors.updatePalette(palette);
// We extract colors for the hotseat and status bar separately,
// since they only consider part of the wallpaper.
Palette hotseatPalette = Palette.from(wallpaper)
.setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)),
wallpaper.getWidth(), wallpaper.getHeight())
.clearFilters()
.generate();
extractedColors.updateHotseatPalette(hotseatPalette);
extractedColors.updateHotseatPalette(getHotseatPallete());
if (FeatureFlags.LIGHT_STATUS_BAR) {
int statusBarHeight = getResources()
.getDimensionPixelSize(R.dimen.status_bar_height);
Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
Palette statusBarPalette = Palette.from(wallpaper)
.setRegion(0, 0, wallpaper.getWidth(), statusBarHeight)
.clearFilters()
@@ -84,4 +88,33 @@ public class ColorExtractionService extends IntentService {
LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID,
null, extras);
}
@TargetApi(Build.VERSION_CODES.N)
private Palette getHotseatPallete() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
if (Utilities.ATLEAST_NOUGAT) {
try (ParcelFileDescriptor fd = wallpaperManager
.getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
BitmapRegionDecoder decoder = BitmapRegionDecoder
.newInstance(fd.getFileDescriptor(), false);
int height = decoder.getHeight();
Rect decodeRegion = new Rect(0, (int) (height * (1f - HOTSEAT_FRACTION)),
decoder.getWidth(), height);
Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
decoder.recycle();
if (bitmap != null) {
return Palette.from(bitmap).clearFilters().generate();
}
} catch (IOException e) {
Log.e(TAG, "Fetching partial bitmap failed, trying old method", e);
}
}
Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
return Palette.from(wallpaper)
.setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)),
wallpaper.getWidth(), wallpaper.getHeight())
.clearFilters()
.generate();
}
}
@@ -112,34 +112,6 @@ public class ExtractedColors {
return defaultColor;
}
/**
* 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 {
// We currently don't use any of the colors defined by the Palette API,
// but this is how we would add them if we ever need them.
// 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(DEFAULT_COLOR));
// setColorAtIndex(ExtractedColors.MUTED_DARK_INDEX,
// palette.getDarkMutedColor(ExtractedColors.DEFAULT_DARK));
// setColorAtIndex(ExtractedColors.MUTED_LIGHT_INDEX,
// palette.getLightVibrantColor(ExtractedColors.DEFAULT_LIGHT));
}
}
/**
* The hotseat's color is defined as follows:
* - 12% black for super light wallpaper