blob: 2cb9314e17d47ed344f8007bc2d581a7dbdb52b2 [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 */
46final 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 /**
Michael Jurka931dc972011-08-05 15:08:15 -070069 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
70 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
71 * to the proper size (48dp)
72 */
73 static Bitmap createIconBitmap(Bitmap icon, Context context) {
74 int textureWidth = sIconTextureWidth;
75 int textureHeight = sIconTextureHeight;
76 int sourceWidth = icon.getWidth();
77 int sourceHeight = icon.getHeight();
78 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
79 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
80 return Bitmap.createBitmap(icon,
81 (sourceWidth - textureWidth) / 2,
82 (sourceHeight - textureHeight) / 2,
83 textureWidth, textureHeight);
84 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
85 // Icon is the right size, no need to change it
86 return icon;
87 } else {
88 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -070089 final Resources resources = context.getResources();
90 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -070091 }
92 }
93
94 /**
95 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -070096 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080097 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 synchronized (sCanvas) { // we share the statics :-(
99 if (sIconWidth == -1) {
100 initStatics(context);
101 }
102
103 int width = sIconWidth;
104 int height = sIconHeight;
105
Joe Onorato6665c0f2009-09-02 15:27:24 -0700106 if (icon instanceof PaintDrawable) {
107 PaintDrawable painter = (PaintDrawable) icon;
108 painter.setIntrinsicWidth(width);
109 painter.setIntrinsicHeight(height);
110 } else if (icon instanceof BitmapDrawable) {
111 // Ensure the bitmap has a density.
112 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
113 Bitmap bitmap = bitmapDrawable.getBitmap();
114 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
115 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
116 }
117 }
118 int sourceWidth = icon.getIntrinsicWidth();
119 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700120 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700121 // Scale the icon proportionally to the icon dimensions
122 final float ratio = (float) sourceWidth / sourceHeight;
123 if (sourceWidth > sourceHeight) {
124 height = (int) (width / ratio);
125 } else if (sourceHeight > sourceWidth) {
126 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700127 }
128 }
129
130 // no intrinsic size --> use default size
131 int textureWidth = sIconTextureWidth;
132 int textureHeight = sIconTextureHeight;
133
134 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
135 Bitmap.Config.ARGB_8888);
136 final Canvas canvas = sCanvas;
137 canvas.setBitmap(bitmap);
138
139 final int left = (textureWidth-width) / 2;
140 final int top = (textureHeight-height) / 2;
141
Michael Jurka3a9fced2012-04-13 14:44:29 -0700142 @SuppressWarnings("all") // suppress dead code warning
143 final boolean debug = false;
144 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700145 // draw a big box for the icon for debugging
146 canvas.drawColor(sColors[sColorIndex]);
147 if (++sColorIndex >= sColors.length) sColorIndex = 0;
148 Paint debugPaint = new Paint();
149 debugPaint.setColor(0xffcccc00);
150 canvas.drawRect(left, top, left+width, top+height, debugPaint);
151 }
152
153 sOldBounds.set(icon.getBounds());
154 icon.setBounds(left, top, left+width, top+height);
155 icon.draw(canvas);
156 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700157 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700158
159 return bitmap;
160 }
161 }
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 /**
164 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 * @param bitmap The bitmap to get a thumbnail of.
167 * @param context The application's context.
168 *
169 * @return A thumbnail for the specified bitmap or the bitmap itself if the
170 * thumbnail could not be created.
171 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800172 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400173 synchronized (sCanvas) { // we share the statics :-(
174 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700175 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
177
Joe Onorato0589f0f2010-02-08 13:44:00 -0800178 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
179 return bitmap;
180 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700181 final Resources resources = context.getResources();
182 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700186
Winson Chungc763c4e2013-07-19 13:49:06 -0700187 /**
188 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
189 * coordinates.
190 *
191 * @param descendant The descendant to which the passed coordinate is relative.
192 * @param root The root view to make the coordinates relative to.
193 * @param coord The coordinate that we want mapped.
194 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
195 * sometimes this is relevant as in a child's coordinates within the descendant.
196 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
197 * this scale factor is assumed to be equal in X and Y, and so if at any point this
198 * assumption fails, we will need to return a pair of scale factors.
199 */
200 public static float getDescendantCoordRelativeToParent(View descendant, View root,
201 int[] coord, boolean includeRootScroll) {
202 ArrayList<View> ancestorChain = new ArrayList<View>();
203
204 float[] pt = {coord[0], coord[1]};
205
206 View v = descendant;
207 while(v != root && v != null) {
208 ancestorChain.add(v);
209 v = (View) v.getParent();
210 }
211 ancestorChain.add(root);
212
213 float scale = 1.0f;
214 int count = ancestorChain.size();
215 for (int i = 0; i < count; i++) {
216 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700217 // For TextViews, scroll has a meaning which relates to the text position
218 // which is very strange... ignore the scroll.
219 if (v0 != descendant || includeRootScroll) {
220 pt[0] -= v0.getScrollX();
221 pt[1] -= v0.getScrollY();
222 }
223
224 v0.getMatrix().mapPoints(pt);
225 pt[0] += v0.getLeft();
226 pt[1] += v0.getTop();
227 scale *= v0.getScaleX();
228 }
229
230 coord[0] = (int) Math.round(pt[0]);
231 coord[1] = (int) Math.round(pt[1]);
232 return scale;
233 }
234
235 /**
236 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
237 */
238 public static float mapCoordInSelfToDescendent(View descendant, View root,
239 int[] coord) {
240 ArrayList<View> ancestorChain = new ArrayList<View>();
241
242 float[] pt = {coord[0], coord[1]};
243
244 View v = descendant;
245 while(v != root) {
246 ancestorChain.add(v);
247 v = (View) v.getParent();
248 }
249 ancestorChain.add(root);
250
251 float scale = 1.0f;
252 Matrix inverse = new Matrix();
253 int count = ancestorChain.size();
254 for (int i = count - 1; i >= 0; i--) {
255 View ancestor = ancestorChain.get(i);
256 View next = i > 0 ? ancestorChain.get(i-1) : null;
257
258 pt[0] += ancestor.getScrollX();
259 pt[1] += ancestor.getScrollY();
260
261 if (next != null) {
262 pt[0] -= next.getLeft();
263 pt[1] -= next.getTop();
264 next.getMatrix().invert(inverse);
265 inverse.mapPoints(pt);
266 scale *= next.getScaleX();
267 }
268 }
269
270 coord[0] = (int) Math.round(pt[0]);
271 coord[1] = (int) Math.round(pt[1]);
272 return scale;
273 }
274
Joe Onorato6665c0f2009-09-02 15:27:24 -0700275 private static void initStatics(Context context) {
276 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400277 final DisplayMetrics metrics = resources.getDisplayMetrics();
278 final float density = metrics.density;
279
Michael Jurkac9a96192010-11-01 11:52:08 -0700280 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700281 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400282
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500283 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500284 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500285 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500286
287 ColorMatrix cm = new ColorMatrix();
288 cm.setSaturation(0.2f);
289 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
290 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700291 }
292
Winson Chung5f8afe62013-08-12 16:19:28 -0700293 public static void setIconSize(int widthPx) {
294 sIconWidth = sIconHeight = widthPx;
295 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700296 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200297
Winson Chung3a6e7f32013-10-09 15:50:52 -0700298 public static void scaleRect(Rect r, float scale) {
299 if (scale != 1.0f) {
300 r.left = (int) (r.left * scale + 0.5f);
301 r.top = (int) (r.top * scale + 0.5f);
302 r.right = (int) (r.right * scale + 0.5f);
303 r.bottom = (int) (r.bottom * scale + 0.5f);
304 }
305 }
306
307 public static void scaleRectAboutCenter(Rect r, float scale) {
308 int cx = r.centerX();
309 int cy = r.centerY();
310 r.offset(-cx, -cy);
311 Utilities.scaleRect(r, scale);
312 r.offset(cx, cy);
313 }
314
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200315 public static void startActivityForResultSafely(
316 Activity activity, Intent intent, int requestCode) {
317 try {
318 activity.startActivityForResult(intent, requestCode);
319 } catch (ActivityNotFoundException e) {
320 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
321 } catch (SecurityException e) {
322 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
323 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
324 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
325 "or use the exported attribute for this activity.", e);
326 }
327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328}