blob: 9ffd044b4993e4ff3d99c70382eca0691993c806 [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
Michael Jurkaa805e1a2013-08-22 15:00:33 +020019import android.app.Activity;
20import android.content.ActivityNotFoundException;
Winson Chungaafa03c2010-06-11 17:34:16 -070021import android.content.Context;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020022import android.content.Intent;
Winson Chungaafa03c2010-06-11 17:34:16 -070023import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040025import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Canvas;
Joe Onorato56d82912010-03-07 14:32:10 -050027import android.graphics.ColorMatrix;
28import android.graphics.ColorMatrixColorFilter;
Winson Chungc763c4e2013-07-19 13:49:06 -070029import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070031import android.graphics.PaintFlagsDrawFilter;
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;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020037import android.util.Log;
Winson Chungc763c4e2013-07-19 13:49:06 -070038import android.view.View;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020039import android.widget.Toast;
Winson Chungc763c4e2013-07-19 13:49:06 -070040
41import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042
43/**
44 * Various utilities shared amongst the Launcher's classes.
45 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000046public final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040047 private static final String TAG = "Launcher.Utilities";
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 private static int sIconWidth = -1;
50 private static int sIconHeight = -1;
Adam Cohen61f560d2013-09-30 15:58:20 -070051 public static int sIconTextureWidth = -1;
52 public static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
Joe Onorato1291a8c2009-09-15 15:07:25 -040054 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050055 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050056 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050057 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070059 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060
61 static {
62 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
63 Paint.FILTER_BITMAP_FLAG));
64 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070065 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
66 static int sColorIndex = 0;
67
68 /**
Winson Chung0dbd7342013-10-13 22:46:20 -070069 * Returns a FastBitmapDrawable with the icon, accurately sized.
70 */
71 static Drawable createIconDrawable(Bitmap icon) {
72 FastBitmapDrawable d = new FastBitmapDrawable(icon);
Winson Chung54000492013-10-14 16:29:29 -070073 d.setFilterBitmap(true);
Winson Chung0dbd7342013-10-13 22:46:20 -070074 resizeIconDrawable(d);
75 return d;
76 }
77
78 /**
79 * Resizes an icon drawable to the correct icon size.
80 */
81 static void resizeIconDrawable(Drawable icon) {
82 icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight);
83 }
84
85 /**
Michael Jurka931dc972011-08-05 15:08:15 -070086 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
87 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
88 * to the proper size (48dp)
89 */
90 static Bitmap createIconBitmap(Bitmap icon, Context context) {
91 int textureWidth = sIconTextureWidth;
92 int textureHeight = sIconTextureHeight;
93 int sourceWidth = icon.getWidth();
94 int sourceHeight = icon.getHeight();
95 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
96 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
97 return Bitmap.createBitmap(icon,
98 (sourceWidth - textureWidth) / 2,
99 (sourceHeight - textureHeight) / 2,
100 textureWidth, textureHeight);
101 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
102 // Icon is the right size, no need to change it
103 return icon;
104 } else {
105 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -0700106 final Resources resources = context.getResources();
107 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700108 }
109 }
110
111 /**
112 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700113 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000114 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700115 synchronized (sCanvas) { // we share the statics :-(
116 if (sIconWidth == -1) {
117 initStatics(context);
118 }
119
120 int width = sIconWidth;
121 int height = sIconHeight;
122
Joe Onorato6665c0f2009-09-02 15:27:24 -0700123 if (icon instanceof PaintDrawable) {
124 PaintDrawable painter = (PaintDrawable) icon;
125 painter.setIntrinsicWidth(width);
126 painter.setIntrinsicHeight(height);
127 } else if (icon instanceof BitmapDrawable) {
128 // Ensure the bitmap has a density.
129 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
130 Bitmap bitmap = bitmapDrawable.getBitmap();
131 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
132 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
133 }
134 }
135 int sourceWidth = icon.getIntrinsicWidth();
136 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700137 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700138 // Scale the icon proportionally to the icon dimensions
139 final float ratio = (float) sourceWidth / sourceHeight;
140 if (sourceWidth > sourceHeight) {
141 height = (int) (width / ratio);
142 } else if (sourceHeight > sourceWidth) {
143 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700144 }
145 }
146
147 // no intrinsic size --> use default size
148 int textureWidth = sIconTextureWidth;
149 int textureHeight = sIconTextureHeight;
150
151 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
152 Bitmap.Config.ARGB_8888);
153 final Canvas canvas = sCanvas;
154 canvas.setBitmap(bitmap);
155
156 final int left = (textureWidth-width) / 2;
157 final int top = (textureHeight-height) / 2;
158
Michael Jurka3a9fced2012-04-13 14:44:29 -0700159 @SuppressWarnings("all") // suppress dead code warning
160 final boolean debug = false;
161 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700162 // draw a big box for the icon for debugging
163 canvas.drawColor(sColors[sColorIndex]);
164 if (++sColorIndex >= sColors.length) sColorIndex = 0;
165 Paint debugPaint = new Paint();
166 debugPaint.setColor(0xffcccc00);
167 canvas.drawRect(left, top, left+width, top+height, debugPaint);
168 }
169
170 sOldBounds.set(icon.getBounds());
171 icon.setBounds(left, top, left+width, top+height);
172 icon.draw(canvas);
173 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700174 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700175
176 return bitmap;
177 }
178 }
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 /**
181 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 * @param bitmap The bitmap to get a thumbnail of.
184 * @param context The application's context.
185 *
186 * @return A thumbnail for the specified bitmap or the bitmap itself if the
187 * thumbnail could not be created.
188 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800189 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 synchronized (sCanvas) { // we share the statics :-(
191 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700192 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 }
194
Joe Onorato0589f0f2010-02-08 13:44:00 -0800195 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
196 return bitmap;
197 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700198 final Resources resources = context.getResources();
199 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700203
Winson Chungc763c4e2013-07-19 13:49:06 -0700204 /**
205 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
206 * coordinates.
207 *
208 * @param descendant The descendant to which the passed coordinate is relative.
209 * @param root The root view to make the coordinates relative to.
210 * @param coord The coordinate that we want mapped.
211 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
212 * sometimes this is relevant as in a child's coordinates within the descendant.
213 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
214 * this scale factor is assumed to be equal in X and Y, and so if at any point this
215 * assumption fails, we will need to return a pair of scale factors.
216 */
217 public static float getDescendantCoordRelativeToParent(View descendant, View root,
218 int[] coord, boolean includeRootScroll) {
219 ArrayList<View> ancestorChain = new ArrayList<View>();
220
221 float[] pt = {coord[0], coord[1]};
222
223 View v = descendant;
224 while(v != root && v != null) {
225 ancestorChain.add(v);
226 v = (View) v.getParent();
227 }
228 ancestorChain.add(root);
229
230 float scale = 1.0f;
231 int count = ancestorChain.size();
232 for (int i = 0; i < count; i++) {
233 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700234 // For TextViews, scroll has a meaning which relates to the text position
235 // which is very strange... ignore the scroll.
236 if (v0 != descendant || includeRootScroll) {
237 pt[0] -= v0.getScrollX();
238 pt[1] -= v0.getScrollY();
239 }
240
241 v0.getMatrix().mapPoints(pt);
242 pt[0] += v0.getLeft();
243 pt[1] += v0.getTop();
244 scale *= v0.getScaleX();
245 }
246
247 coord[0] = (int) Math.round(pt[0]);
248 coord[1] = (int) Math.round(pt[1]);
249 return scale;
250 }
251
252 /**
253 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
254 */
255 public static float mapCoordInSelfToDescendent(View descendant, View root,
256 int[] coord) {
257 ArrayList<View> ancestorChain = new ArrayList<View>();
258
259 float[] pt = {coord[0], coord[1]};
260
261 View v = descendant;
262 while(v != root) {
263 ancestorChain.add(v);
264 v = (View) v.getParent();
265 }
266 ancestorChain.add(root);
267
268 float scale = 1.0f;
269 Matrix inverse = new Matrix();
270 int count = ancestorChain.size();
271 for (int i = count - 1; i >= 0; i--) {
272 View ancestor = ancestorChain.get(i);
273 View next = i > 0 ? ancestorChain.get(i-1) : null;
274
275 pt[0] += ancestor.getScrollX();
276 pt[1] += ancestor.getScrollY();
277
278 if (next != null) {
279 pt[0] -= next.getLeft();
280 pt[1] -= next.getTop();
281 next.getMatrix().invert(inverse);
282 inverse.mapPoints(pt);
283 scale *= next.getScaleX();
284 }
285 }
286
287 coord[0] = (int) Math.round(pt[0]);
288 coord[1] = (int) Math.round(pt[1]);
289 return scale;
290 }
291
Joe Onorato6665c0f2009-09-02 15:27:24 -0700292 private static void initStatics(Context context) {
293 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400294 final DisplayMetrics metrics = resources.getDisplayMetrics();
295 final float density = metrics.density;
296
Michael Jurkac9a96192010-11-01 11:52:08 -0700297 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700298 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400299
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500300 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500301 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500302 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500303
304 ColorMatrix cm = new ColorMatrix();
305 cm.setSaturation(0.2f);
306 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
307 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700308 }
309
Winson Chung5f8afe62013-08-12 16:19:28 -0700310 public static void setIconSize(int widthPx) {
311 sIconWidth = sIconHeight = widthPx;
312 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700313 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200314
Winson Chung3a6e7f32013-10-09 15:50:52 -0700315 public static void scaleRect(Rect r, float scale) {
316 if (scale != 1.0f) {
317 r.left = (int) (r.left * scale + 0.5f);
318 r.top = (int) (r.top * scale + 0.5f);
319 r.right = (int) (r.right * scale + 0.5f);
320 r.bottom = (int) (r.bottom * scale + 0.5f);
321 }
322 }
323
324 public static void scaleRectAboutCenter(Rect r, float scale) {
325 int cx = r.centerX();
326 int cy = r.centerY();
327 r.offset(-cx, -cy);
328 Utilities.scaleRect(r, scale);
329 r.offset(cx, cy);
330 }
331
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200332 public static void startActivityForResultSafely(
333 Activity activity, Intent intent, int requestCode) {
334 try {
335 activity.startActivityForResult(intent, requestCode);
336 } catch (ActivityNotFoundException e) {
337 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
338 } catch (SecurityException e) {
339 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
340 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
341 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
342 "or use the exported attribute for this activity.", e);
343 }
344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345}