blob: 8deadf1d9bc7466ebf0c6d405bb26c53656e2ff8 [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
Michael Jurka7ad868b2013-12-12 15:04:25 +010068
69 // To turn on these properties, type
70 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
71 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
72 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
73
Joe Onorato6665c0f2009-09-02 15:27:24 -070074 /**
Winson Chung0dbd7342013-10-13 22:46:20 -070075 * Returns a FastBitmapDrawable with the icon, accurately sized.
76 */
77 static Drawable createIconDrawable(Bitmap icon) {
78 FastBitmapDrawable d = new FastBitmapDrawable(icon);
Winson Chung54000492013-10-14 16:29:29 -070079 d.setFilterBitmap(true);
Winson Chung0dbd7342013-10-13 22:46:20 -070080 resizeIconDrawable(d);
81 return d;
82 }
83
84 /**
85 * Resizes an icon drawable to the correct icon size.
86 */
87 static void resizeIconDrawable(Drawable icon) {
88 icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight);
89 }
90
Michael Jurka7ad868b2013-12-12 15:04:25 +010091 private static boolean isPropertyEnabled(String propertyName) {
92 return Log.isLoggable(propertyName, Log.VERBOSE);
93 }
94
95 public static boolean isRotationEnabled(Context c) {
96 boolean enableRotation = sForceEnableRotation ||
97 c.getResources().getBoolean(R.bool.allow_rotation);
98 return enableRotation;
99 }
100
Winson Chung0dbd7342013-10-13 22:46:20 -0700101 /**
Michael Jurka931dc972011-08-05 15:08:15 -0700102 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
103 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
104 * to the proper size (48dp)
105 */
106 static Bitmap createIconBitmap(Bitmap icon, Context context) {
107 int textureWidth = sIconTextureWidth;
108 int textureHeight = sIconTextureHeight;
109 int sourceWidth = icon.getWidth();
110 int sourceHeight = icon.getHeight();
111 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
112 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
113 return Bitmap.createBitmap(icon,
114 (sourceWidth - textureWidth) / 2,
115 (sourceHeight - textureHeight) / 2,
116 textureWidth, textureHeight);
117 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
118 // Icon is the right size, no need to change it
119 return icon;
120 } else {
121 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -0700122 final Resources resources = context.getResources();
123 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700124 }
125 }
126
127 /**
128 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700129 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000130 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 synchronized (sCanvas) { // we share the statics :-(
132 if (sIconWidth == -1) {
133 initStatics(context);
134 }
135
136 int width = sIconWidth;
137 int height = sIconHeight;
138
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139 if (icon instanceof PaintDrawable) {
140 PaintDrawable painter = (PaintDrawable) icon;
141 painter.setIntrinsicWidth(width);
142 painter.setIntrinsicHeight(height);
143 } else if (icon instanceof BitmapDrawable) {
144 // Ensure the bitmap has a density.
145 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
146 Bitmap bitmap = bitmapDrawable.getBitmap();
147 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
148 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
149 }
150 }
151 int sourceWidth = icon.getIntrinsicWidth();
152 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700153 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700154 // Scale the icon proportionally to the icon dimensions
155 final float ratio = (float) sourceWidth / sourceHeight;
156 if (sourceWidth > sourceHeight) {
157 height = (int) (width / ratio);
158 } else if (sourceHeight > sourceWidth) {
159 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700160 }
161 }
162
163 // no intrinsic size --> use default size
164 int textureWidth = sIconTextureWidth;
165 int textureHeight = sIconTextureHeight;
166
167 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
168 Bitmap.Config.ARGB_8888);
169 final Canvas canvas = sCanvas;
170 canvas.setBitmap(bitmap);
171
172 final int left = (textureWidth-width) / 2;
173 final int top = (textureHeight-height) / 2;
174
Michael Jurka3a9fced2012-04-13 14:44:29 -0700175 @SuppressWarnings("all") // suppress dead code warning
176 final boolean debug = false;
177 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700178 // draw a big box for the icon for debugging
179 canvas.drawColor(sColors[sColorIndex]);
180 if (++sColorIndex >= sColors.length) sColorIndex = 0;
181 Paint debugPaint = new Paint();
182 debugPaint.setColor(0xffcccc00);
183 canvas.drawRect(left, top, left+width, top+height, debugPaint);
184 }
185
186 sOldBounds.set(icon.getBounds());
187 icon.setBounds(left, top, left+width, top+height);
188 icon.draw(canvas);
189 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700190 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700191
192 return bitmap;
193 }
194 }
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 /**
197 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 * @param bitmap The bitmap to get a thumbnail of.
200 * @param context The application's context.
201 *
202 * @return A thumbnail for the specified bitmap or the bitmap itself if the
203 * thumbnail could not be created.
204 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800205 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 synchronized (sCanvas) { // we share the statics :-(
207 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700208 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 }
210
Joe Onorato0589f0f2010-02-08 13:44:00 -0800211 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
212 return bitmap;
213 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700214 final Resources resources = context.getResources();
215 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700219
Winson Chungc763c4e2013-07-19 13:49:06 -0700220 /**
221 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
222 * coordinates.
223 *
224 * @param descendant The descendant to which the passed coordinate is relative.
225 * @param root The root view to make the coordinates relative to.
226 * @param coord The coordinate that we want mapped.
227 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
228 * sometimes this is relevant as in a child's coordinates within the descendant.
229 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
230 * this scale factor is assumed to be equal in X and Y, and so if at any point this
231 * assumption fails, we will need to return a pair of scale factors.
232 */
233 public static float getDescendantCoordRelativeToParent(View descendant, View root,
234 int[] coord, boolean includeRootScroll) {
235 ArrayList<View> ancestorChain = new ArrayList<View>();
236
237 float[] pt = {coord[0], coord[1]};
238
239 View v = descendant;
240 while(v != root && v != null) {
241 ancestorChain.add(v);
242 v = (View) v.getParent();
243 }
244 ancestorChain.add(root);
245
246 float scale = 1.0f;
247 int count = ancestorChain.size();
248 for (int i = 0; i < count; i++) {
249 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700250 // For TextViews, scroll has a meaning which relates to the text position
251 // which is very strange... ignore the scroll.
252 if (v0 != descendant || includeRootScroll) {
253 pt[0] -= v0.getScrollX();
254 pt[1] -= v0.getScrollY();
255 }
256
257 v0.getMatrix().mapPoints(pt);
258 pt[0] += v0.getLeft();
259 pt[1] += v0.getTop();
260 scale *= v0.getScaleX();
261 }
262
263 coord[0] = (int) Math.round(pt[0]);
264 coord[1] = (int) Math.round(pt[1]);
265 return scale;
266 }
267
268 /**
269 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
270 */
271 public static float mapCoordInSelfToDescendent(View descendant, View root,
272 int[] coord) {
273 ArrayList<View> ancestorChain = new ArrayList<View>();
274
275 float[] pt = {coord[0], coord[1]};
276
277 View v = descendant;
278 while(v != root) {
279 ancestorChain.add(v);
280 v = (View) v.getParent();
281 }
282 ancestorChain.add(root);
283
284 float scale = 1.0f;
285 Matrix inverse = new Matrix();
286 int count = ancestorChain.size();
287 for (int i = count - 1; i >= 0; i--) {
288 View ancestor = ancestorChain.get(i);
289 View next = i > 0 ? ancestorChain.get(i-1) : null;
290
291 pt[0] += ancestor.getScrollX();
292 pt[1] += ancestor.getScrollY();
293
294 if (next != null) {
295 pt[0] -= next.getLeft();
296 pt[1] -= next.getTop();
297 next.getMatrix().invert(inverse);
298 inverse.mapPoints(pt);
299 scale *= next.getScaleX();
300 }
301 }
302
303 coord[0] = (int) Math.round(pt[0]);
304 coord[1] = (int) Math.round(pt[1]);
305 return scale;
306 }
307
Jason Monk02dd7ae2014-04-15 15:23:31 -0400308 /**
309 * Utility method to determine whether the given point, in local coordinates,
310 * is inside the view, where the area of the view is expanded by the slop factor.
311 * This method is called while processing touch-move events to determine if the event
312 * is still within the view.
313 */
314 public static boolean pointInView(View v, float localX, float localY, float slop) {
315 return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
316 localY < (v.getHeight() + slop);
317 }
318
Joe Onorato6665c0f2009-09-02 15:27:24 -0700319 private static void initStatics(Context context) {
320 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400321 final DisplayMetrics metrics = resources.getDisplayMetrics();
322 final float density = metrics.density;
323
Michael Jurkac9a96192010-11-01 11:52:08 -0700324 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700325 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400326
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500327 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500328 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500329 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500330
331 ColorMatrix cm = new ColorMatrix();
332 cm.setSaturation(0.2f);
333 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
334 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700335 }
336
Winson Chung5f8afe62013-08-12 16:19:28 -0700337 public static void setIconSize(int widthPx) {
338 sIconWidth = sIconHeight = widthPx;
339 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700340 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200341
Winson Chung3a6e7f32013-10-09 15:50:52 -0700342 public static void scaleRect(Rect r, float scale) {
343 if (scale != 1.0f) {
344 r.left = (int) (r.left * scale + 0.5f);
345 r.top = (int) (r.top * scale + 0.5f);
346 r.right = (int) (r.right * scale + 0.5f);
347 r.bottom = (int) (r.bottom * scale + 0.5f);
348 }
349 }
350
351 public static void scaleRectAboutCenter(Rect r, float scale) {
352 int cx = r.centerX();
353 int cy = r.centerY();
354 r.offset(-cx, -cy);
355 Utilities.scaleRect(r, scale);
356 r.offset(cx, cy);
357 }
358
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200359 public static void startActivityForResultSafely(
360 Activity activity, Intent intent, int requestCode) {
361 try {
362 activity.startActivityForResult(intent, requestCode);
363 } catch (ActivityNotFoundException e) {
364 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
365 } catch (SecurityException e) {
366 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
367 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
368 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
369 "or use the exported attribute for this activity.", e);
370 }
371 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372}