blob: 90db5ab85cf2df9c2427ddef99d616799f007142 [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 {
Michael Jurka3a9fced2012-04-13 14:44:29 -070047 @SuppressWarnings("unused")
Joe Onorato1291a8c2009-09-15 15:07:25 -040048 private static final String TAG = "Launcher.Utilities";
49
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050 private static int sIconWidth = -1;
51 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070052 private static int sIconTextureWidth = -1;
53 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054
Joe Onorato1291a8c2009-09-15 15:07:25 -040055 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050056 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050057 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050058 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070060 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 static {
63 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
64 Paint.FILTER_BITMAP_FLAG));
65 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070066 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
67 static int sColorIndex = 0;
68
69 /**
Michael Jurka931dc972011-08-05 15:08:15 -070070 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
71 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
72 * to the proper size (48dp)
73 */
74 static Bitmap createIconBitmap(Bitmap icon, Context context) {
75 int textureWidth = sIconTextureWidth;
76 int textureHeight = sIconTextureHeight;
77 int sourceWidth = icon.getWidth();
78 int sourceHeight = icon.getHeight();
79 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
80 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
81 return Bitmap.createBitmap(icon,
82 (sourceWidth - textureWidth) / 2,
83 (sourceHeight - textureHeight) / 2,
84 textureWidth, textureHeight);
85 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
86 // Icon is the right size, no need to change it
87 return icon;
88 } else {
89 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -070090 final Resources resources = context.getResources();
91 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -070092 }
93 }
94
95 /**
96 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -070097 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080098 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070099 synchronized (sCanvas) { // we share the statics :-(
100 if (sIconWidth == -1) {
101 initStatics(context);
102 }
103
104 int width = sIconWidth;
105 int height = sIconHeight;
106
Joe Onorato6665c0f2009-09-02 15:27:24 -0700107 if (icon instanceof PaintDrawable) {
108 PaintDrawable painter = (PaintDrawable) icon;
109 painter.setIntrinsicWidth(width);
110 painter.setIntrinsicHeight(height);
111 } else if (icon instanceof BitmapDrawable) {
112 // Ensure the bitmap has a density.
113 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
114 Bitmap bitmap = bitmapDrawable.getBitmap();
115 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
116 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
117 }
118 }
119 int sourceWidth = icon.getIntrinsicWidth();
120 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700121 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700122 // Scale the icon proportionally to the icon dimensions
123 final float ratio = (float) sourceWidth / sourceHeight;
124 if (sourceWidth > sourceHeight) {
125 height = (int) (width / ratio);
126 } else if (sourceHeight > sourceWidth) {
127 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700128 }
129 }
130
131 // no intrinsic size --> use default size
132 int textureWidth = sIconTextureWidth;
133 int textureHeight = sIconTextureHeight;
134
135 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
136 Bitmap.Config.ARGB_8888);
137 final Canvas canvas = sCanvas;
138 canvas.setBitmap(bitmap);
139
140 final int left = (textureWidth-width) / 2;
141 final int top = (textureHeight-height) / 2;
142
Michael Jurka3a9fced2012-04-13 14:44:29 -0700143 @SuppressWarnings("all") // suppress dead code warning
144 final boolean debug = false;
145 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700146 // draw a big box for the icon for debugging
147 canvas.drawColor(sColors[sColorIndex]);
148 if (++sColorIndex >= sColors.length) sColorIndex = 0;
149 Paint debugPaint = new Paint();
150 debugPaint.setColor(0xffcccc00);
151 canvas.drawRect(left, top, left+width, top+height, debugPaint);
152 }
153
154 sOldBounds.set(icon.getBounds());
155 icon.setBounds(left, top, left+width, top+height);
156 icon.draw(canvas);
157 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700158 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700159
160 return bitmap;
161 }
162 }
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 /**
165 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 * @param bitmap The bitmap to get a thumbnail of.
168 * @param context The application's context.
169 *
170 * @return A thumbnail for the specified bitmap or the bitmap itself if the
171 * thumbnail could not be created.
172 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800173 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 synchronized (sCanvas) { // we share the statics :-(
175 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700176 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 }
178
Joe Onorato0589f0f2010-02-08 13:44:00 -0800179 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
180 return bitmap;
181 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700182 final Resources resources = context.getResources();
183 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700187
Winson Chungc763c4e2013-07-19 13:49:06 -0700188 /**
189 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
190 * coordinates.
191 *
192 * @param descendant The descendant to which the passed coordinate is relative.
193 * @param root The root view to make the coordinates relative to.
194 * @param coord The coordinate that we want mapped.
195 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
196 * sometimes this is relevant as in a child's coordinates within the descendant.
197 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
198 * this scale factor is assumed to be equal in X and Y, and so if at any point this
199 * assumption fails, we will need to return a pair of scale factors.
200 */
201 public static float getDescendantCoordRelativeToParent(View descendant, View root,
202 int[] coord, boolean includeRootScroll) {
203 ArrayList<View> ancestorChain = new ArrayList<View>();
204
205 float[] pt = {coord[0], coord[1]};
206
207 View v = descendant;
208 while(v != root && v != null) {
209 ancestorChain.add(v);
210 v = (View) v.getParent();
211 }
212 ancestorChain.add(root);
213
214 float scale = 1.0f;
215 int count = ancestorChain.size();
216 for (int i = 0; i < count; i++) {
217 View v0 = ancestorChain.get(i);
218 View v1 = i < count -1 ? ancestorChain.get(i + 1) : null;
219
220 // For TextViews, scroll has a meaning which relates to the text position
221 // which is very strange... ignore the scroll.
222 if (v0 != descendant || includeRootScroll) {
223 pt[0] -= v0.getScrollX();
224 pt[1] -= v0.getScrollY();
225 }
226
227 v0.getMatrix().mapPoints(pt);
228 pt[0] += v0.getLeft();
229 pt[1] += v0.getTop();
230 scale *= v0.getScaleX();
231 }
232
233 coord[0] = (int) Math.round(pt[0]);
234 coord[1] = (int) Math.round(pt[1]);
235 return scale;
236 }
237
238 /**
239 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
240 */
241 public static float mapCoordInSelfToDescendent(View descendant, View root,
242 int[] coord) {
243 ArrayList<View> ancestorChain = new ArrayList<View>();
244
245 float[] pt = {coord[0], coord[1]};
246
247 View v = descendant;
248 while(v != root) {
249 ancestorChain.add(v);
250 v = (View) v.getParent();
251 }
252 ancestorChain.add(root);
253
254 float scale = 1.0f;
255 Matrix inverse = new Matrix();
256 int count = ancestorChain.size();
257 for (int i = count - 1; i >= 0; i--) {
258 View ancestor = ancestorChain.get(i);
259 View next = i > 0 ? ancestorChain.get(i-1) : null;
260
261 pt[0] += ancestor.getScrollX();
262 pt[1] += ancestor.getScrollY();
263
264 if (next != null) {
265 pt[0] -= next.getLeft();
266 pt[1] -= next.getTop();
267 next.getMatrix().invert(inverse);
268 inverse.mapPoints(pt);
269 scale *= next.getScaleX();
270 }
271 }
272
273 coord[0] = (int) Math.round(pt[0]);
274 coord[1] = (int) Math.round(pt[1]);
275 return scale;
276 }
277
Joe Onorato6665c0f2009-09-02 15:27:24 -0700278 private static void initStatics(Context context) {
279 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400280 final DisplayMetrics metrics = resources.getDisplayMetrics();
281 final float density = metrics.density;
282
Michael Jurkac9a96192010-11-01 11:52:08 -0700283 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700284 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400285
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500286 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500287 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500288 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500289
290 ColorMatrix cm = new ColorMatrix();
291 cm.setSaturation(0.2f);
292 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
293 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700294 }
295
Winson Chung5f8afe62013-08-12 16:19:28 -0700296 public static void setIconSize(int widthPx) {
297 sIconWidth = sIconHeight = widthPx;
298 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700299 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200300
301 public static void startActivityForResultSafely(
302 Activity activity, Intent intent, int requestCode) {
303 try {
304 activity.startActivityForResult(intent, requestCode);
305 } catch (ActivityNotFoundException e) {
306 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
307 } catch (SecurityException e) {
308 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
309 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
310 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
311 "or use the exported attribute for this activity.", e);
312 }
313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314}