Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 99014e5c authored by Sunny Goyal's avatar Sunny Goyal Committed by Android (Google) Code Review
Browse files

Merge "Using BitmapRegionDecoder to only parse partial bitmap during color...

Merge "Using BitmapRegionDecoder to only parse partial bitmap during color extraction" into ub-launcher3-master
parents 2b073be6 1b997d33
Loading
Loading
Loading
Loading
+43 −10
Original line number Diff line number Diff line
@@ -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();
    }
}
+0 −28
Original line number Diff line number Diff line
@@ -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