blob: 74b6e47f17444906f985049573cdf4d8b82e7e8a [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;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070036import android.os.Build;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070037import android.util.DisplayMetrics;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020038import android.util.Log;
Winson Chungc763c4e2013-07-19 13:49:06 -070039import android.view.View;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020040import android.widget.Toast;
Winson Chungc763c4e2013-07-19 13:49:06 -070041
42import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
44/**
45 * Various utilities shared amongst the Launcher's classes.
46 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000047public final class Utilities {
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;
Adam Cohen61f560d2013-09-30 15:58:20 -070052 public static int sIconTextureWidth = -1;
53 public 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
Michael Jurka7ad868b2013-12-12 15:04:25 +010069
70 // To turn on these properties, type
71 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
72 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
73 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
74
Joe Onorato6665c0f2009-09-02 15:27:24 -070075 /**
Winson Chung0dbd7342013-10-13 22:46:20 -070076 * Returns a FastBitmapDrawable with the icon, accurately sized.
77 */
78 static Drawable createIconDrawable(Bitmap icon) {
79 FastBitmapDrawable d = new FastBitmapDrawable(icon);
Winson Chung54000492013-10-14 16:29:29 -070080 d.setFilterBitmap(true);
Winson Chung0dbd7342013-10-13 22:46:20 -070081 resizeIconDrawable(d);
82 return d;
83 }
84
85 /**
86 * Resizes an icon drawable to the correct icon size.
87 */
88 static void resizeIconDrawable(Drawable icon) {
89 icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight);
90 }
91
Michael Jurka7ad868b2013-12-12 15:04:25 +010092 private static boolean isPropertyEnabled(String propertyName) {
93 return Log.isLoggable(propertyName, Log.VERBOSE);
94 }
95
96 public static boolean isRotationEnabled(Context c) {
97 boolean enableRotation = sForceEnableRotation ||
98 c.getResources().getBoolean(R.bool.allow_rotation);
99 return enableRotation;
100 }
101
Winson Chung0dbd7342013-10-13 22:46:20 -0700102 /**
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700103 * Indicates if the device is running LMP or not.
104 * TODO(sansid): Change the check to a VERSION_CODES code check once we have a version for L.
105 */
106 public static boolean isLmp() {
107 return "L".equals(Build.VERSION.CODENAME);
108 }
109
110 /**
Michael Jurka931dc972011-08-05 15:08:15 -0700111 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
112 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
113 * to the proper size (48dp)
114 */
115 static Bitmap createIconBitmap(Bitmap icon, Context context) {
116 int textureWidth = sIconTextureWidth;
117 int textureHeight = sIconTextureHeight;
118 int sourceWidth = icon.getWidth();
119 int sourceHeight = icon.getHeight();
120 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
121 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
122 return Bitmap.createBitmap(icon,
123 (sourceWidth - textureWidth) / 2,
124 (sourceHeight - textureHeight) / 2,
125 textureWidth, textureHeight);
126 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
127 // Icon is the right size, no need to change it
128 return icon;
129 } else {
130 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -0700131 final Resources resources = context.getResources();
132 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700133 }
134 }
135
136 /**
137 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700138 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000139 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700140 synchronized (sCanvas) { // we share the statics :-(
141 if (sIconWidth == -1) {
142 initStatics(context);
143 }
144
145 int width = sIconWidth;
146 int height = sIconHeight;
147
Joe Onorato6665c0f2009-09-02 15:27:24 -0700148 if (icon instanceof PaintDrawable) {
149 PaintDrawable painter = (PaintDrawable) icon;
150 painter.setIntrinsicWidth(width);
151 painter.setIntrinsicHeight(height);
152 } else if (icon instanceof BitmapDrawable) {
153 // Ensure the bitmap has a density.
154 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
155 Bitmap bitmap = bitmapDrawable.getBitmap();
156 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
157 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
158 }
159 }
160 int sourceWidth = icon.getIntrinsicWidth();
161 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700162 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700163 // Scale the icon proportionally to the icon dimensions
164 final float ratio = (float) sourceWidth / sourceHeight;
165 if (sourceWidth > sourceHeight) {
166 height = (int) (width / ratio);
167 } else if (sourceHeight > sourceWidth) {
168 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700169 }
170 }
171
172 // no intrinsic size --> use default size
173 int textureWidth = sIconTextureWidth;
174 int textureHeight = sIconTextureHeight;
175
176 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
177 Bitmap.Config.ARGB_8888);
178 final Canvas canvas = sCanvas;
179 canvas.setBitmap(bitmap);
180
181 final int left = (textureWidth-width) / 2;
182 final int top = (textureHeight-height) / 2;
183
Michael Jurka3a9fced2012-04-13 14:44:29 -0700184 @SuppressWarnings("all") // suppress dead code warning
185 final boolean debug = false;
186 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700187 // draw a big box for the icon for debugging
188 canvas.drawColor(sColors[sColorIndex]);
189 if (++sColorIndex >= sColors.length) sColorIndex = 0;
190 Paint debugPaint = new Paint();
191 debugPaint.setColor(0xffcccc00);
192 canvas.drawRect(left, top, left+width, top+height, debugPaint);
193 }
194
195 sOldBounds.set(icon.getBounds());
196 icon.setBounds(left, top, left+width, top+height);
197 icon.draw(canvas);
198 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700199 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700200
201 return bitmap;
202 }
203 }
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 /**
206 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 * @param bitmap The bitmap to get a thumbnail of.
209 * @param context The application's context.
210 *
211 * @return A thumbnail for the specified bitmap or the bitmap itself if the
212 * thumbnail could not be created.
213 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800214 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 synchronized (sCanvas) { // we share the statics :-(
216 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700217 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 }
219
Joe Onorato0589f0f2010-02-08 13:44:00 -0800220 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
221 return bitmap;
222 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700223 final Resources resources = context.getResources();
224 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700228
Winson Chungc763c4e2013-07-19 13:49:06 -0700229 /**
230 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
231 * coordinates.
232 *
233 * @param descendant The descendant to which the passed coordinate is relative.
234 * @param root The root view to make the coordinates relative to.
235 * @param coord The coordinate that we want mapped.
236 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
237 * sometimes this is relevant as in a child's coordinates within the descendant.
238 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
239 * this scale factor is assumed to be equal in X and Y, and so if at any point this
240 * assumption fails, we will need to return a pair of scale factors.
241 */
242 public static float getDescendantCoordRelativeToParent(View descendant, View root,
243 int[] coord, boolean includeRootScroll) {
244 ArrayList<View> ancestorChain = new ArrayList<View>();
245
246 float[] pt = {coord[0], coord[1]};
247
248 View v = descendant;
249 while(v != root && v != null) {
250 ancestorChain.add(v);
251 v = (View) v.getParent();
252 }
253 ancestorChain.add(root);
254
255 float scale = 1.0f;
256 int count = ancestorChain.size();
257 for (int i = 0; i < count; i++) {
258 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700259 // For TextViews, scroll has a meaning which relates to the text position
260 // which is very strange... ignore the scroll.
261 if (v0 != descendant || includeRootScroll) {
262 pt[0] -= v0.getScrollX();
263 pt[1] -= v0.getScrollY();
264 }
265
266 v0.getMatrix().mapPoints(pt);
267 pt[0] += v0.getLeft();
268 pt[1] += v0.getTop();
269 scale *= v0.getScaleX();
270 }
271
272 coord[0] = (int) Math.round(pt[0]);
273 coord[1] = (int) Math.round(pt[1]);
274 return scale;
275 }
276
277 /**
278 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
279 */
280 public static float mapCoordInSelfToDescendent(View descendant, View root,
281 int[] coord) {
282 ArrayList<View> ancestorChain = new ArrayList<View>();
283
284 float[] pt = {coord[0], coord[1]};
285
286 View v = descendant;
287 while(v != root) {
288 ancestorChain.add(v);
289 v = (View) v.getParent();
290 }
291 ancestorChain.add(root);
292
293 float scale = 1.0f;
294 Matrix inverse = new Matrix();
295 int count = ancestorChain.size();
296 for (int i = count - 1; i >= 0; i--) {
297 View ancestor = ancestorChain.get(i);
298 View next = i > 0 ? ancestorChain.get(i-1) : null;
299
300 pt[0] += ancestor.getScrollX();
301 pt[1] += ancestor.getScrollY();
302
303 if (next != null) {
304 pt[0] -= next.getLeft();
305 pt[1] -= next.getTop();
306 next.getMatrix().invert(inverse);
307 inverse.mapPoints(pt);
308 scale *= next.getScaleX();
309 }
310 }
311
312 coord[0] = (int) Math.round(pt[0]);
313 coord[1] = (int) Math.round(pt[1]);
314 return scale;
315 }
316
Jason Monk02dd7ae2014-04-15 15:23:31 -0400317 /**
318 * Utility method to determine whether the given point, in local coordinates,
319 * is inside the view, where the area of the view is expanded by the slop factor.
320 * This method is called while processing touch-move events to determine if the event
321 * is still within the view.
322 */
323 public static boolean pointInView(View v, float localX, float localY, float slop) {
324 return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
325 localY < (v.getHeight() + slop);
326 }
327
Joe Onorato6665c0f2009-09-02 15:27:24 -0700328 private static void initStatics(Context context) {
329 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400330 final DisplayMetrics metrics = resources.getDisplayMetrics();
331 final float density = metrics.density;
332
Michael Jurkac9a96192010-11-01 11:52:08 -0700333 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700334 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400335
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500336 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500337 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500338 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500339
340 ColorMatrix cm = new ColorMatrix();
341 cm.setSaturation(0.2f);
342 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
343 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700344 }
345
Winson Chung5f8afe62013-08-12 16:19:28 -0700346 public static void setIconSize(int widthPx) {
347 sIconWidth = sIconHeight = widthPx;
348 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700349 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200350
Winson Chung3a6e7f32013-10-09 15:50:52 -0700351 public static void scaleRect(Rect r, float scale) {
352 if (scale != 1.0f) {
353 r.left = (int) (r.left * scale + 0.5f);
354 r.top = (int) (r.top * scale + 0.5f);
355 r.right = (int) (r.right * scale + 0.5f);
356 r.bottom = (int) (r.bottom * scale + 0.5f);
357 }
358 }
359
360 public static void scaleRectAboutCenter(Rect r, float scale) {
361 int cx = r.centerX();
362 int cy = r.centerY();
363 r.offset(-cx, -cy);
364 Utilities.scaleRect(r, scale);
365 r.offset(cx, cy);
366 }
367
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200368 public static void startActivityForResultSafely(
369 Activity activity, Intent intent, int requestCode) {
370 try {
371 activity.startActivityForResult(intent, requestCode);
372 } catch (ActivityNotFoundException e) {
373 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
374 } catch (SecurityException e) {
375 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
376 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
377 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
378 "or use the exported attribute for this activity.", e);
379 }
380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381}