blob: 0cc29faa322962ef45bb9c9d838b00e2cd1fea40 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Winson Chung97d85d22011-04-13 11:27:36 -070019import java.util.Random;
20
Winson Chungaafa03c2010-06-11 17:34:16 -070021import android.content.Context;
22import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040024import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Joe Onorato56d82912010-03-07 14:32:10 -050026import android.graphics.ColorMatrix;
27import android.graphics.ColorMatrixColorFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070029import android.graphics.PaintFlagsDrawFilter;
Joe Onorato1291a8c2009-09-15 15:07:25 -040030import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Rect;
Winson Chungaafa03c2010-06-11 17:34:16 -070032import android.graphics.drawable.BitmapDrawable;
33import android.graphics.drawable.Drawable;
34import android.graphics.drawable.PaintDrawable;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070035import android.util.DisplayMetrics;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036
Daniel Sandler325dc232013-06-05 22:57:57 -040037import com.android.launcher3.R;
Romain Guyedcce092010-03-04 13:03:17 -080038
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039/**
40 * Various utilities shared amongst the Launcher's classes.
41 */
42final class Utilities {
Michael Jurka3a9fced2012-04-13 14:44:29 -070043 @SuppressWarnings("unused")
Joe Onorato1291a8c2009-09-15 15:07:25 -040044 private static final String TAG = "Launcher.Utilities";
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046 private static int sIconWidth = -1;
47 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070048 private static int sIconTextureWidth = -1;
49 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
Joe Onorato1291a8c2009-09-15 15:07:25 -040051 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050052 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050053 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050054 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070056 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
58 static {
59 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
60 Paint.FILTER_BITMAP_FLAG));
61 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070062 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
63 static int sColorIndex = 0;
64
65 /**
Michael Jurka931dc972011-08-05 15:08:15 -070066 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
67 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
68 * to the proper size (48dp)
69 */
70 static Bitmap createIconBitmap(Bitmap icon, Context context) {
71 int textureWidth = sIconTextureWidth;
72 int textureHeight = sIconTextureHeight;
73 int sourceWidth = icon.getWidth();
74 int sourceHeight = icon.getHeight();
75 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
76 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
77 return Bitmap.createBitmap(icon,
78 (sourceWidth - textureWidth) / 2,
79 (sourceHeight - textureHeight) / 2,
80 textureWidth, textureHeight);
81 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
82 // Icon is the right size, no need to change it
83 return icon;
84 } else {
85 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -070086 final Resources resources = context.getResources();
87 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -070088 }
89 }
90
91 /**
92 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -070093 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080094 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070095 synchronized (sCanvas) { // we share the statics :-(
96 if (sIconWidth == -1) {
97 initStatics(context);
98 }
99
100 int width = sIconWidth;
101 int height = sIconHeight;
102
Joe Onorato6665c0f2009-09-02 15:27:24 -0700103 if (icon instanceof PaintDrawable) {
104 PaintDrawable painter = (PaintDrawable) icon;
105 painter.setIntrinsicWidth(width);
106 painter.setIntrinsicHeight(height);
107 } else if (icon instanceof BitmapDrawable) {
108 // Ensure the bitmap has a density.
109 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
110 Bitmap bitmap = bitmapDrawable.getBitmap();
111 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
112 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
113 }
114 }
115 int sourceWidth = icon.getIntrinsicWidth();
116 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700117 if (sourceWidth > 0 && sourceHeight > 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700118 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700119 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700120 // It's too big, scale it down.
121 final float ratio = (float) sourceWidth / sourceHeight;
122 if (sourceWidth > sourceHeight) {
123 height = (int) (width / ratio);
124 } else if (sourceHeight > sourceWidth) {
125 width = (int) (height * ratio);
126 }
127 } else if (sourceWidth < width && sourceHeight < height) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700128 // Don't scale up the icon
Joe Onorato6665c0f2009-09-02 15:27:24 -0700129 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700130 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 }
132 }
133
134 // no intrinsic size --> use default size
135 int textureWidth = sIconTextureWidth;
136 int textureHeight = sIconTextureHeight;
137
138 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
139 Bitmap.Config.ARGB_8888);
140 final Canvas canvas = sCanvas;
141 canvas.setBitmap(bitmap);
142
143 final int left = (textureWidth-width) / 2;
144 final int top = (textureHeight-height) / 2;
145
Michael Jurka3a9fced2012-04-13 14:44:29 -0700146 @SuppressWarnings("all") // suppress dead code warning
147 final boolean debug = false;
148 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700149 // draw a big box for the icon for debugging
150 canvas.drawColor(sColors[sColorIndex]);
151 if (++sColorIndex >= sColors.length) sColorIndex = 0;
152 Paint debugPaint = new Paint();
153 debugPaint.setColor(0xffcccc00);
154 canvas.drawRect(left, top, left+width, top+height, debugPaint);
155 }
156
157 sOldBounds.set(icon.getBounds());
158 icon.setBounds(left, top, left+width, top+height);
159 icon.draw(canvas);
160 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700161 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700162
163 return bitmap;
164 }
165 }
166
Joe Onoratoc61cff92009-11-08 11:54:39 -0500167 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500168 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400169 synchronized (sCanvas) { // we share the statics :-(
170 if (sIconWidth == -1) {
171 // We can't have gotten to here without src being initialized, which
172 // comes from this file already. So just assert.
173 //initStatics(context);
174 throw new RuntimeException("Assertion failed: Utilities not initialized");
175 }
176
177 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800178
Joe Onorato1291a8c2009-09-15 15:07:25 -0400179 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400180 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400181
Mike Reedcdd11792009-10-29 17:27:55 -0400182 float px = (destWidth - src.getWidth()) / 2;
183 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500184 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500185 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400186
187 mask.recycle();
188 }
189 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 /**
192 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
193 * The size of the thumbnail is defined by the dimension
194 * android.R.dimen.launcher_application_icon_size.
195 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 * @param bitmap The bitmap to get a thumbnail of.
197 * @param context The application's context.
198 *
199 * @return A thumbnail for the specified bitmap or the bitmap itself if the
200 * thumbnail could not be created.
201 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800202 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 synchronized (sCanvas) { // we share the statics :-(
204 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700205 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 }
207
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
209 return bitmap;
210 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700211 final Resources resources = context.getResources();
212 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700216
Joe Onorato56d82912010-03-07 14:32:10 -0500217 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
218 synchronized (sCanvas) { // we share the statics :-(
219 if (sIconWidth == -1) {
220 initStatics(context);
221 }
222 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
223 Bitmap.Config.ARGB_8888);
224 final Canvas canvas = sCanvas;
225 canvas.setBitmap(disabled);
226
227 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
228
Adam Cohenaaf473c2011-08-03 12:02:47 -0700229 canvas.setBitmap(null);
230
Joe Onorato56d82912010-03-07 14:32:10 -0500231 return disabled;
232 }
233 }
234
Joe Onorato6665c0f2009-09-02 15:27:24 -0700235 private static void initStatics(Context context) {
236 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400237 final DisplayMetrics metrics = resources.getDisplayMetrics();
238 final float density = metrics.density;
239
Michael Jurkac9a96192010-11-01 11:52:08 -0700240 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700241 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400242
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500243 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500244 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500245 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500246
247 ColorMatrix cm = new ColorMatrix();
248 cm.setSaturation(0.2f);
249 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
250 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700251 }
252
Joe Onoratobf15cb42009-08-07 14:33:40 -0700253 /** Only works for positive numbers. */
254 static int roundToPow2(int n) {
255 int orig = n;
256 n >>= 1;
257 int mask = 0x8000000;
258 while (mask != 0 && (n & mask) == 0) {
259 mask >>= 1;
260 }
261 while (mask != 0) {
262 n |= mask;
263 mask >>= 1;
264 }
265 n += 1;
266 if (n != orig) {
267 n <<= 1;
268 }
269 return n;
270 }
Winson Chung97d85d22011-04-13 11:27:36 -0700271
272 static int generateRandomId() {
273 return new Random(System.currentTimeMillis()).nextInt(1 << 24);
274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275}