blob: cc22bb5db7e86c4d077eb1ef6a33d18767075972 [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;
Winson Chungc763c4e2013-07-19 13:49:06 -070028import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070030import android.graphics.PaintFlagsDrawFilter;
Joe Onorato1291a8c2009-09-15 15:07:25 -040031import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.graphics.Rect;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.drawable.BitmapDrawable;
34import android.graphics.drawable.Drawable;
35import android.graphics.drawable.PaintDrawable;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070036import android.util.DisplayMetrics;
Winson Chungc763c4e2013-07-19 13:49:06 -070037import android.view.View;
38import android.view.ViewGroup;
39
40import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
Daniel Sandler325dc232013-06-05 22:57:57 -040042import com.android.launcher3.R;
Romain Guyedcce092010-03-04 13:03:17 -080043
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044/**
45 * Various utilities shared amongst the Launcher's classes.
46 */
47final class Utilities {
Michael Jurka3a9fced2012-04-13 14:44:29 -070048 @SuppressWarnings("unused")
Joe Onorato1291a8c2009-09-15 15:07:25 -040049 private static final String TAG = "Launcher.Utilities";
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private static int sIconWidth = -1;
52 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070053 private static int sIconTextureWidth = -1;
54 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Joe Onorato1291a8c2009-09-15 15:07:25 -040056 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050057 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050058 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050059 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070061 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 static {
64 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
65 Paint.FILTER_BITMAP_FLAG));
66 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070067 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
68 static int sColorIndex = 0;
69
70 /**
Michael Jurka931dc972011-08-05 15:08:15 -070071 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
72 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
73 * to the proper size (48dp)
74 */
75 static Bitmap createIconBitmap(Bitmap icon, Context context) {
76 int textureWidth = sIconTextureWidth;
77 int textureHeight = sIconTextureHeight;
78 int sourceWidth = icon.getWidth();
79 int sourceHeight = icon.getHeight();
80 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
81 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
82 return Bitmap.createBitmap(icon,
83 (sourceWidth - textureWidth) / 2,
84 (sourceHeight - textureHeight) / 2,
85 textureWidth, textureHeight);
86 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
87 // Icon is the right size, no need to change it
88 return icon;
89 } else {
90 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -070091 final Resources resources = context.getResources();
92 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -070093 }
94 }
95
96 /**
97 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080099 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 synchronized (sCanvas) { // we share the statics :-(
101 if (sIconWidth == -1) {
102 initStatics(context);
103 }
104
105 int width = sIconWidth;
106 int height = sIconHeight;
107
Joe Onorato6665c0f2009-09-02 15:27:24 -0700108 if (icon instanceof PaintDrawable) {
109 PaintDrawable painter = (PaintDrawable) icon;
110 painter.setIntrinsicWidth(width);
111 painter.setIntrinsicHeight(height);
112 } else if (icon instanceof BitmapDrawable) {
113 // Ensure the bitmap has a density.
114 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
115 Bitmap bitmap = bitmapDrawable.getBitmap();
116 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
117 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
118 }
119 }
120 int sourceWidth = icon.getIntrinsicWidth();
121 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700122 if (sourceWidth > 0 && sourceHeight > 0) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700124 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700125 // It's too big, scale it down.
126 final float ratio = (float) sourceWidth / sourceHeight;
127 if (sourceWidth > sourceHeight) {
128 height = (int) (width / ratio);
129 } else if (sourceHeight > sourceWidth) {
130 width = (int) (height * ratio);
131 }
132 } else if (sourceWidth < width && sourceHeight < height) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700133 // Don't scale up the icon
Joe Onorato6665c0f2009-09-02 15:27:24 -0700134 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700135 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700136 }
137 }
138
139 // no intrinsic size --> use default size
140 int textureWidth = sIconTextureWidth;
141 int textureHeight = sIconTextureHeight;
142
143 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
144 Bitmap.Config.ARGB_8888);
145 final Canvas canvas = sCanvas;
146 canvas.setBitmap(bitmap);
147
148 final int left = (textureWidth-width) / 2;
149 final int top = (textureHeight-height) / 2;
150
Michael Jurka3a9fced2012-04-13 14:44:29 -0700151 @SuppressWarnings("all") // suppress dead code warning
152 final boolean debug = false;
153 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700154 // draw a big box for the icon for debugging
155 canvas.drawColor(sColors[sColorIndex]);
156 if (++sColorIndex >= sColors.length) sColorIndex = 0;
157 Paint debugPaint = new Paint();
158 debugPaint.setColor(0xffcccc00);
159 canvas.drawRect(left, top, left+width, top+height, debugPaint);
160 }
161
162 sOldBounds.set(icon.getBounds());
163 icon.setBounds(left, top, left+width, top+height);
164 icon.draw(canvas);
165 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700166 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700167
168 return bitmap;
169 }
170 }
171
Joe Onoratoc61cff92009-11-08 11:54:39 -0500172 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500173 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400174 synchronized (sCanvas) { // we share the statics :-(
175 if (sIconWidth == -1) {
176 // We can't have gotten to here without src being initialized, which
177 // comes from this file already. So just assert.
178 //initStatics(context);
179 throw new RuntimeException("Assertion failed: Utilities not initialized");
180 }
181
182 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800183
Joe Onorato1291a8c2009-09-15 15:07:25 -0400184 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400185 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400186
Mike Reedcdd11792009-10-29 17:27:55 -0400187 float px = (destWidth - src.getWidth()) / 2;
188 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500189 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500190 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400191
192 mask.recycle();
193 }
194 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 /**
197 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
198 * The size of the thumbnail is defined by the dimension
199 * android.R.dimen.launcher_application_icon_size.
200 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 * @param bitmap The bitmap to get a thumbnail of.
202 * @param context The application's context.
203 *
204 * @return A thumbnail for the specified bitmap or the bitmap itself if the
205 * thumbnail could not be created.
206 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 synchronized (sCanvas) { // we share the statics :-(
209 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700210 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 }
212
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
214 return bitmap;
215 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700216 final Resources resources = context.getResources();
217 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700221
Joe Onorato56d82912010-03-07 14:32:10 -0500222 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
223 synchronized (sCanvas) { // we share the statics :-(
224 if (sIconWidth == -1) {
225 initStatics(context);
226 }
227 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
228 Bitmap.Config.ARGB_8888);
229 final Canvas canvas = sCanvas;
230 canvas.setBitmap(disabled);
231
232 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
233
Adam Cohenaaf473c2011-08-03 12:02:47 -0700234 canvas.setBitmap(null);
235
Joe Onorato56d82912010-03-07 14:32:10 -0500236 return disabled;
237 }
238 }
239
Winson Chungc763c4e2013-07-19 13:49:06 -0700240 /**
241 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
242 * coordinates.
243 *
244 * @param descendant The descendant to which the passed coordinate is relative.
245 * @param root The root view to make the coordinates relative to.
246 * @param coord The coordinate that we want mapped.
247 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
248 * sometimes this is relevant as in a child's coordinates within the descendant.
249 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
250 * this scale factor is assumed to be equal in X and Y, and so if at any point this
251 * assumption fails, we will need to return a pair of scale factors.
252 */
253 public static float getDescendantCoordRelativeToParent(View descendant, View root,
254 int[] coord, boolean includeRootScroll) {
255 ArrayList<View> ancestorChain = new ArrayList<View>();
256
257 float[] pt = {coord[0], coord[1]};
258
259 View v = descendant;
260 while(v != root && v != null) {
261 ancestorChain.add(v);
262 v = (View) v.getParent();
263 }
264 ancestorChain.add(root);
265
266 float scale = 1.0f;
267 int count = ancestorChain.size();
268 for (int i = 0; i < count; i++) {
269 View v0 = ancestorChain.get(i);
270 View v1 = i < count -1 ? ancestorChain.get(i + 1) : null;
271
272 // For TextViews, scroll has a meaning which relates to the text position
273 // which is very strange... ignore the scroll.
274 if (v0 != descendant || includeRootScroll) {
275 pt[0] -= v0.getScrollX();
276 pt[1] -= v0.getScrollY();
277 }
278
279 v0.getMatrix().mapPoints(pt);
280 pt[0] += v0.getLeft();
281 pt[1] += v0.getTop();
282 scale *= v0.getScaleX();
283 }
284
285 coord[0] = (int) Math.round(pt[0]);
286 coord[1] = (int) Math.round(pt[1]);
287 return scale;
288 }
289
290 /**
291 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
292 */
293 public static float mapCoordInSelfToDescendent(View descendant, View root,
294 int[] coord) {
295 ArrayList<View> ancestorChain = new ArrayList<View>();
296
297 float[] pt = {coord[0], coord[1]};
298
299 View v = descendant;
300 while(v != root) {
301 ancestorChain.add(v);
302 v = (View) v.getParent();
303 }
304 ancestorChain.add(root);
305
306 float scale = 1.0f;
307 Matrix inverse = new Matrix();
308 int count = ancestorChain.size();
309 for (int i = count - 1; i >= 0; i--) {
310 View ancestor = ancestorChain.get(i);
311 View next = i > 0 ? ancestorChain.get(i-1) : null;
312
313 pt[0] += ancestor.getScrollX();
314 pt[1] += ancestor.getScrollY();
315
316 if (next != null) {
317 pt[0] -= next.getLeft();
318 pt[1] -= next.getTop();
319 next.getMatrix().invert(inverse);
320 inverse.mapPoints(pt);
321 scale *= next.getScaleX();
322 }
323 }
324
325 coord[0] = (int) Math.round(pt[0]);
326 coord[1] = (int) Math.round(pt[1]);
327 return scale;
328 }
329
Joe Onorato6665c0f2009-09-02 15:27:24 -0700330 private static void initStatics(Context context) {
331 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400332 final DisplayMetrics metrics = resources.getDisplayMetrics();
333 final float density = metrics.density;
334
Michael Jurkac9a96192010-11-01 11:52:08 -0700335 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700336 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400337
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500338 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500339 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500340 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500341
342 ColorMatrix cm = new ColorMatrix();
343 cm.setSaturation(0.2f);
344 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
345 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700346 }
347
Joe Onoratobf15cb42009-08-07 14:33:40 -0700348 /** Only works for positive numbers. */
349 static int roundToPow2(int n) {
350 int orig = n;
351 n >>= 1;
352 int mask = 0x8000000;
353 while (mask != 0 && (n & mask) == 0) {
354 mask >>= 1;
355 }
356 while (mask != 0) {
357 n |= mask;
358 mask >>= 1;
359 }
360 n += 1;
361 if (n != orig) {
362 n <<= 1;
363 }
364 return n;
365 }
Winson Chung97d85d22011-04-13 11:27:36 -0700366
367 static int generateRandomId() {
368 return new Random(System.currentTimeMillis()).nextInt(1 << 24);
369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370}