2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2011-04-13 11:27:36 -07:00
|
|
|
import java.util.Random;
|
|
|
|
|
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Bitmap;
|
2009-09-15 15:07:25 -04:00
|
|
|
import android.graphics.BlurMaskFilter;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Canvas;
|
2010-03-07 14:32:10 -05:00
|
|
|
import android.graphics.ColorMatrix;
|
|
|
|
|
import android.graphics.ColorMatrixColorFilter;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Paint;
|
2009-08-07 14:33:40 -07:00
|
|
|
import android.graphics.PaintFlagsDrawFilter;
|
2009-09-15 15:07:25 -04:00
|
|
|
import android.graphics.PorterDuff;
|
2009-03-03 19:32:27 -08:00
|
|
|
import android.graphics.Rect;
|
2010-06-11 17:34:16 -07:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
|
import android.graphics.drawable.Drawable;
|
|
|
|
|
import android.graphics.drawable.PaintDrawable;
|
2009-07-22 21:56:50 -07:00
|
|
|
import android.util.DisplayMetrics;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
import com.android.launcher3.R;
|
2010-03-04 13:03:17 -08:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Various utilities shared amongst the Launcher's classes.
|
|
|
|
|
*/
|
|
|
|
|
final class Utilities {
|
2012-04-13 14:44:29 -07:00
|
|
|
@SuppressWarnings("unused")
|
2009-09-15 15:07:25 -04:00
|
|
|
private static final String TAG = "Launcher.Utilities";
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
private static int sIconWidth = -1;
|
|
|
|
|
private static int sIconHeight = -1;
|
2009-09-02 15:27:24 -07:00
|
|
|
private static int sIconTextureWidth = -1;
|
|
|
|
|
private static int sIconTextureHeight = -1;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2009-09-15 15:07:25 -04:00
|
|
|
private static final Paint sBlurPaint = new Paint();
|
2009-11-08 13:20:30 -05:00
|
|
|
private static final Paint sGlowColorPressedPaint = new Paint();
|
2009-11-08 11:54:39 -05:00
|
|
|
private static final Paint sGlowColorFocusedPaint = new Paint();
|
2010-03-07 14:32:10 -05:00
|
|
|
private static final Paint sDisabledPaint = new Paint();
|
2009-03-03 19:32:27 -08:00
|
|
|
private static final Rect sOldBounds = new Rect();
|
2009-09-28 18:48:49 -07:00
|
|
|
private static final Canvas sCanvas = new Canvas();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
|
|
|
|
|
Paint.FILTER_BITMAP_FLAG));
|
|
|
|
|
}
|
2009-09-02 15:27:24 -07:00
|
|
|
static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
|
|
|
|
|
static int sColorIndex = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-05 15:08:15 -07:00
|
|
|
* Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
|
|
|
|
|
* icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
|
|
|
|
|
* to the proper size (48dp)
|
|
|
|
|
*/
|
|
|
|
|
static Bitmap createIconBitmap(Bitmap icon, Context context) {
|
|
|
|
|
int textureWidth = sIconTextureWidth;
|
|
|
|
|
int textureHeight = sIconTextureHeight;
|
|
|
|
|
int sourceWidth = icon.getWidth();
|
|
|
|
|
int sourceHeight = icon.getHeight();
|
|
|
|
|
if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
|
|
|
|
|
// Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
|
|
|
|
|
return Bitmap.createBitmap(icon,
|
|
|
|
|
(sourceWidth - textureWidth) / 2,
|
|
|
|
|
(sourceHeight - textureHeight) / 2,
|
|
|
|
|
textureWidth, textureHeight);
|
|
|
|
|
} else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
|
|
|
|
|
// Icon is the right size, no need to change it
|
|
|
|
|
return icon;
|
|
|
|
|
} else {
|
|
|
|
|
// Icon is too small, render to a larger bitmap
|
2012-04-13 14:44:29 -07:00
|
|
|
final Resources resources = context.getResources();
|
|
|
|
|
return createIconBitmap(new BitmapDrawable(resources, icon), context);
|
2011-08-05 15:08:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a bitmap suitable for the all apps view.
|
2009-09-02 15:27:24 -07:00
|
|
|
*/
|
2010-02-08 13:44:00 -08:00
|
|
|
static Bitmap createIconBitmap(Drawable icon, Context context) {
|
2009-09-02 15:27:24 -07:00
|
|
|
synchronized (sCanvas) { // we share the statics :-(
|
|
|
|
|
if (sIconWidth == -1) {
|
|
|
|
|
initStatics(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int width = sIconWidth;
|
|
|
|
|
int height = sIconHeight;
|
|
|
|
|
|
|
|
|
|
if (icon instanceof PaintDrawable) {
|
|
|
|
|
PaintDrawable painter = (PaintDrawable) icon;
|
|
|
|
|
painter.setIntrinsicWidth(width);
|
|
|
|
|
painter.setIntrinsicHeight(height);
|
|
|
|
|
} else if (icon instanceof BitmapDrawable) {
|
|
|
|
|
// Ensure the bitmap has a density.
|
|
|
|
|
BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
|
|
|
|
|
Bitmap bitmap = bitmapDrawable.getBitmap();
|
|
|
|
|
if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
|
|
|
|
|
bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int sourceWidth = icon.getIntrinsicWidth();
|
|
|
|
|
int sourceHeight = icon.getIntrinsicHeight();
|
2011-08-05 15:08:15 -07:00
|
|
|
if (sourceWidth > 0 && sourceHeight > 0) {
|
2009-09-02 15:27:24 -07:00
|
|
|
// There are intrinsic sizes.
|
2009-09-28 18:48:49 -07:00
|
|
|
if (width < sourceWidth || height < sourceHeight) {
|
2009-09-02 15:27:24 -07:00
|
|
|
// It's too big, scale it down.
|
|
|
|
|
final float ratio = (float) sourceWidth / sourceHeight;
|
|
|
|
|
if (sourceWidth > sourceHeight) {
|
|
|
|
|
height = (int) (width / ratio);
|
|
|
|
|
} else if (sourceHeight > sourceWidth) {
|
|
|
|
|
width = (int) (height * ratio);
|
|
|
|
|
}
|
|
|
|
|
} else if (sourceWidth < width && sourceHeight < height) {
|
2011-06-19 12:41:22 -07:00
|
|
|
// Don't scale up the icon
|
2009-09-02 15:27:24 -07:00
|
|
|
width = sourceWidth;
|
2009-09-28 18:48:49 -07:00
|
|
|
height = sourceHeight;
|
2009-09-02 15:27:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no intrinsic size --> use default size
|
|
|
|
|
int textureWidth = sIconTextureWidth;
|
|
|
|
|
int textureHeight = sIconTextureHeight;
|
|
|
|
|
|
|
|
|
|
final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
|
|
|
|
|
Bitmap.Config.ARGB_8888);
|
|
|
|
|
final Canvas canvas = sCanvas;
|
|
|
|
|
canvas.setBitmap(bitmap);
|
|
|
|
|
|
|
|
|
|
final int left = (textureWidth-width) / 2;
|
|
|
|
|
final int top = (textureHeight-height) / 2;
|
|
|
|
|
|
2012-04-13 14:44:29 -07:00
|
|
|
@SuppressWarnings("all") // suppress dead code warning
|
|
|
|
|
final boolean debug = false;
|
|
|
|
|
if (debug) {
|
2009-09-02 15:27:24 -07:00
|
|
|
// draw a big box for the icon for debugging
|
|
|
|
|
canvas.drawColor(sColors[sColorIndex]);
|
|
|
|
|
if (++sColorIndex >= sColors.length) sColorIndex = 0;
|
|
|
|
|
Paint debugPaint = new Paint();
|
|
|
|
|
debugPaint.setColor(0xffcccc00);
|
|
|
|
|
canvas.drawRect(left, top, left+width, top+height, debugPaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sOldBounds.set(icon.getBounds());
|
|
|
|
|
icon.setBounds(left, top, left+width, top+height);
|
|
|
|
|
icon.draw(canvas);
|
|
|
|
|
icon.setBounds(sOldBounds);
|
2011-08-03 12:02:47 -07:00
|
|
|
canvas.setBitmap(null);
|
2009-09-02 15:27:24 -07:00
|
|
|
|
|
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-08 11:54:39 -05:00
|
|
|
static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
|
2009-11-08 13:20:30 -05:00
|
|
|
boolean pressed, Bitmap src) {
|
2009-09-15 15:07:25 -04:00
|
|
|
synchronized (sCanvas) { // we share the statics :-(
|
|
|
|
|
if (sIconWidth == -1) {
|
|
|
|
|
// We can't have gotten to here without src being initialized, which
|
|
|
|
|
// comes from this file already. So just assert.
|
|
|
|
|
//initStatics(context);
|
|
|
|
|
throw new RuntimeException("Assertion failed: Utilities not initialized");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dest.drawColor(0, PorterDuff.Mode.CLEAR);
|
2010-01-19 16:43:26 -08:00
|
|
|
|
2009-09-15 15:07:25 -04:00
|
|
|
int[] xy = new int[2];
|
2009-10-29 17:27:55 -04:00
|
|
|
Bitmap mask = src.extractAlpha(sBlurPaint, xy);
|
2009-09-15 15:07:25 -04:00
|
|
|
|
2009-10-29 17:27:55 -04:00
|
|
|
float px = (destWidth - src.getWidth()) / 2;
|
|
|
|
|
float py = (destHeight - src.getHeight()) / 2;
|
2009-11-08 11:54:39 -05:00
|
|
|
dest.drawBitmap(mask, px + xy[0], py + xy[1],
|
2009-11-08 13:20:30 -05:00
|
|
|
pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
|
2009-09-15 15:07:25 -04:00
|
|
|
|
|
|
|
|
mask.recycle();
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-02 15:27:24 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Returns a Bitmap representing the thumbnail of the specified Bitmap.
|
|
|
|
|
* The size of the thumbnail is defined by the dimension
|
|
|
|
|
* android.R.dimen.launcher_application_icon_size.
|
|
|
|
|
*
|
|
|
|
|
* @param bitmap The bitmap to get a thumbnail of.
|
|
|
|
|
* @param context The application's context.
|
|
|
|
|
*
|
|
|
|
|
* @return A thumbnail for the specified bitmap or the bitmap itself if the
|
|
|
|
|
* thumbnail could not be created.
|
|
|
|
|
*/
|
2010-02-08 13:44:00 -08:00
|
|
|
static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
|
2009-08-17 11:03:03 -04:00
|
|
|
synchronized (sCanvas) { // we share the statics :-(
|
|
|
|
|
if (sIconWidth == -1) {
|
2009-09-02 15:27:24 -07:00
|
|
|
initStatics(context);
|
2009-08-17 11:03:03 -04:00
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2010-02-08 13:44:00 -08:00
|
|
|
if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
|
|
|
|
|
return bitmap;
|
|
|
|
|
} else {
|
2012-04-13 14:44:29 -07:00
|
|
|
final Resources resources = context.getResources();
|
|
|
|
|
return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-07 14:33:40 -07:00
|
|
|
|
2010-03-07 14:32:10 -05:00
|
|
|
static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
|
|
|
|
|
synchronized (sCanvas) { // we share the statics :-(
|
|
|
|
|
if (sIconWidth == -1) {
|
|
|
|
|
initStatics(context);
|
|
|
|
|
}
|
|
|
|
|
final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
|
|
|
|
|
Bitmap.Config.ARGB_8888);
|
|
|
|
|
final Canvas canvas = sCanvas;
|
|
|
|
|
canvas.setBitmap(disabled);
|
|
|
|
|
|
|
|
|
|
canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
|
|
|
|
|
|
2011-08-03 12:02:47 -07:00
|
|
|
canvas.setBitmap(null);
|
|
|
|
|
|
2010-03-07 14:32:10 -05:00
|
|
|
return disabled;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-02 15:27:24 -07:00
|
|
|
private static void initStatics(Context context) {
|
|
|
|
|
final Resources resources = context.getResources();
|
2009-09-15 15:07:25 -04:00
|
|
|
final DisplayMetrics metrics = resources.getDisplayMetrics();
|
|
|
|
|
final float density = metrics.density;
|
|
|
|
|
|
2010-11-01 11:52:08 -07:00
|
|
|
sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
|
2011-06-19 12:41:22 -07:00
|
|
|
sIconTextureWidth = sIconTextureHeight = sIconWidth;
|
2009-09-15 15:07:25 -04:00
|
|
|
|
2009-11-02 10:42:02 -05:00
|
|
|
sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
|
2009-11-08 13:20:30 -05:00
|
|
|
sGlowColorPressedPaint.setColor(0xffffc300);
|
2009-11-08 11:54:39 -05:00
|
|
|
sGlowColorFocusedPaint.setColor(0xffff8e00);
|
2010-03-07 14:32:10 -05:00
|
|
|
|
|
|
|
|
ColorMatrix cm = new ColorMatrix();
|
|
|
|
|
cm.setSaturation(0.2f);
|
|
|
|
|
sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
|
|
|
|
|
sDisabledPaint.setAlpha(0x88);
|
2009-09-02 15:27:24 -07:00
|
|
|
}
|
|
|
|
|
|
2009-08-07 14:33:40 -07:00
|
|
|
/** Only works for positive numbers. */
|
|
|
|
|
static int roundToPow2(int n) {
|
|
|
|
|
int orig = n;
|
|
|
|
|
n >>= 1;
|
|
|
|
|
int mask = 0x8000000;
|
|
|
|
|
while (mask != 0 && (n & mask) == 0) {
|
|
|
|
|
mask >>= 1;
|
|
|
|
|
}
|
|
|
|
|
while (mask != 0) {
|
|
|
|
|
n |= mask;
|
|
|
|
|
mask >>= 1;
|
|
|
|
|
}
|
|
|
|
|
n += 1;
|
|
|
|
|
if (n != orig) {
|
|
|
|
|
n <<= 1;
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
}
|
2011-04-13 11:27:36 -07:00
|
|
|
|
|
|
|
|
static int generateRandomId() {
|
|
|
|
|
return new Random(System.currentTimeMillis()).nextInt(1 << 24);
|
|
|
|
|
}
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|