blob: 0a711c5dd151de6e73fd40c722cc2e16e5840cc1 [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;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070021import android.content.ComponentName;
Winson Chungaafa03c2010-06-11 17:34:16 -070022import android.content.Context;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020023import android.content.Intent;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070024import android.content.pm.ApplicationInfo;
25import android.content.pm.PackageInfo;
26import android.content.pm.PackageManager;
27import android.content.pm.PackageManager.NameNotFoundException;
28import android.content.pm.ResolveInfo;
Winson Chungaafa03c2010-06-11 17:34:16 -070029import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.graphics.Bitmap;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Canvas;
Winson Chungc763c4e2013-07-19 13:49:06 -070032import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070034import android.graphics.PaintFlagsDrawFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.graphics.Rect;
Winson Chungaafa03c2010-06-11 17:34:16 -070036import android.graphics.drawable.BitmapDrawable;
37import android.graphics.drawable.Drawable;
38import android.graphics.drawable.PaintDrawable;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070039import android.os.Build;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070040import android.util.DisplayMetrics;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020041import android.util.Log;
Winson Chungc763c4e2013-07-19 13:49:06 -070042import android.view.View;
Michael Jurkaa805e1a2013-08-22 15:00:33 +020043import android.widget.Toast;
Winson Chungc763c4e2013-07-19 13:49:06 -070044
45import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046
47/**
48 * Various utilities shared amongst the Launcher's classes.
49 */
Mathew Inwood72fbec12013-11-19 15:45:07 +000050public final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040051 private static final String TAG = "Launcher.Utilities";
52
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 private static int sIconWidth = -1;
54 private static int sIconHeight = -1;
Adam Cohen61f560d2013-09-30 15:58:20 -070055 public static int sIconTextureWidth = -1;
56 public static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
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 */
Sunny Goyalc5c60ad2014-07-14 12:02:01 -070077 static FastBitmapDrawable createIconDrawable(Bitmap icon) {
Winson Chung0dbd7342013-10-13 22:46:20 -070078 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 /**
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -0700102 * Indicates if the device is running LMP or not.
103 * TODO(sansid): Change the check to a VERSION_CODES code check once we have a version for L.
104 */
105 public static boolean isLmp() {
106 return "L".equals(Build.VERSION.CODENAME);
107 }
108
109 /**
Michael Jurka931dc972011-08-05 15:08:15 -0700110 * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
111 * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
112 * to the proper size (48dp)
113 */
114 static Bitmap createIconBitmap(Bitmap icon, Context context) {
115 int textureWidth = sIconTextureWidth;
116 int textureHeight = sIconTextureHeight;
117 int sourceWidth = icon.getWidth();
118 int sourceHeight = icon.getHeight();
119 if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
120 // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
121 return Bitmap.createBitmap(icon,
122 (sourceWidth - textureWidth) / 2,
123 (sourceHeight - textureHeight) / 2,
124 textureWidth, textureHeight);
125 } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
126 // Icon is the right size, no need to change it
127 return icon;
128 } else {
129 // Icon is too small, render to a larger bitmap
Michael Jurka3a9fced2012-04-13 14:44:29 -0700130 final Resources resources = context.getResources();
131 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -0700132 }
133 }
134
135 /**
136 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -0700137 */
Mathew Inwood72fbec12013-11-19 15:45:07 +0000138 public static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700139 synchronized (sCanvas) { // we share the statics :-(
140 if (sIconWidth == -1) {
141 initStatics(context);
142 }
143
144 int width = sIconWidth;
145 int height = sIconHeight;
146
Joe Onorato6665c0f2009-09-02 15:27:24 -0700147 if (icon instanceof PaintDrawable) {
148 PaintDrawable painter = (PaintDrawable) icon;
149 painter.setIntrinsicWidth(width);
150 painter.setIntrinsicHeight(height);
151 } else if (icon instanceof BitmapDrawable) {
152 // Ensure the bitmap has a density.
153 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
154 Bitmap bitmap = bitmapDrawable.getBitmap();
155 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
156 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
157 }
158 }
159 int sourceWidth = icon.getIntrinsicWidth();
160 int sourceHeight = icon.getIntrinsicHeight();
Michael Jurka931dc972011-08-05 15:08:15 -0700161 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700162 // Scale the icon proportionally to the icon dimensions
163 final float ratio = (float) sourceWidth / sourceHeight;
164 if (sourceWidth > sourceHeight) {
165 height = (int) (width / ratio);
166 } else if (sourceHeight > sourceWidth) {
167 width = (int) (height * ratio);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700168 }
169 }
170
171 // no intrinsic size --> use default size
172 int textureWidth = sIconTextureWidth;
173 int textureHeight = sIconTextureHeight;
174
175 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
176 Bitmap.Config.ARGB_8888);
177 final Canvas canvas = sCanvas;
178 canvas.setBitmap(bitmap);
179
180 final int left = (textureWidth-width) / 2;
181 final int top = (textureHeight-height) / 2;
182
Michael Jurka3a9fced2012-04-13 14:44:29 -0700183 @SuppressWarnings("all") // suppress dead code warning
184 final boolean debug = false;
185 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700186 // draw a big box for the icon for debugging
187 canvas.drawColor(sColors[sColorIndex]);
188 if (++sColorIndex >= sColors.length) sColorIndex = 0;
189 Paint debugPaint = new Paint();
190 debugPaint.setColor(0xffcccc00);
191 canvas.drawRect(left, top, left+width, top+height, debugPaint);
192 }
193
194 sOldBounds.set(icon.getBounds());
195 icon.setBounds(left, top, left+width, top+height);
196 icon.draw(canvas);
197 icon.setBounds(sOldBounds);
Adam Cohenaaf473c2011-08-03 12:02:47 -0700198 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700199
200 return bitmap;
201 }
202 }
203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 /**
205 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 * @param bitmap The bitmap to get a thumbnail of.
208 * @param context The application's context.
209 *
210 * @return A thumbnail for the specified bitmap or the bitmap itself if the
211 * thumbnail could not be created.
212 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 synchronized (sCanvas) { // we share the statics :-(
215 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700216 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 }
218
Joe Onorato0589f0f2010-02-08 13:44:00 -0800219 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
220 return bitmap;
221 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700222 final Resources resources = context.getResources();
223 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700227
Winson Chungc763c4e2013-07-19 13:49:06 -0700228 /**
229 * Given a coordinate relative to the descendant, find the coordinate in a parent view's
230 * coordinates.
231 *
232 * @param descendant The descendant to which the passed coordinate is relative.
233 * @param root The root view to make the coordinates relative to.
234 * @param coord The coordinate that we want mapped.
235 * @param includeRootScroll Whether or not to account for the scroll of the descendant:
236 * sometimes this is relevant as in a child's coordinates within the descendant.
237 * @return The factor by which this descendant is scaled relative to this DragLayer. Caution
238 * this scale factor is assumed to be equal in X and Y, and so if at any point this
239 * assumption fails, we will need to return a pair of scale factors.
240 */
241 public static float getDescendantCoordRelativeToParent(View descendant, View root,
242 int[] coord, boolean includeRootScroll) {
243 ArrayList<View> ancestorChain = new ArrayList<View>();
244
245 float[] pt = {coord[0], coord[1]};
246
247 View v = descendant;
248 while(v != root && v != null) {
249 ancestorChain.add(v);
250 v = (View) v.getParent();
251 }
252 ancestorChain.add(root);
253
254 float scale = 1.0f;
255 int count = ancestorChain.size();
256 for (int i = 0; i < count; i++) {
257 View v0 = ancestorChain.get(i);
Winson Chungc763c4e2013-07-19 13:49:06 -0700258 // For TextViews, scroll has a meaning which relates to the text position
259 // which is very strange... ignore the scroll.
260 if (v0 != descendant || includeRootScroll) {
261 pt[0] -= v0.getScrollX();
262 pt[1] -= v0.getScrollY();
263 }
264
265 v0.getMatrix().mapPoints(pt);
266 pt[0] += v0.getLeft();
267 pt[1] += v0.getTop();
268 scale *= v0.getScaleX();
269 }
270
271 coord[0] = (int) Math.round(pt[0]);
272 coord[1] = (int) Math.round(pt[1]);
273 return scale;
274 }
275
276 /**
277 * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}.
278 */
279 public static float mapCoordInSelfToDescendent(View descendant, View root,
280 int[] coord) {
281 ArrayList<View> ancestorChain = new ArrayList<View>();
282
283 float[] pt = {coord[0], coord[1]};
284
285 View v = descendant;
286 while(v != root) {
287 ancestorChain.add(v);
288 v = (View) v.getParent();
289 }
290 ancestorChain.add(root);
291
292 float scale = 1.0f;
293 Matrix inverse = new Matrix();
294 int count = ancestorChain.size();
295 for (int i = count - 1; i >= 0; i--) {
296 View ancestor = ancestorChain.get(i);
297 View next = i > 0 ? ancestorChain.get(i-1) : null;
298
299 pt[0] += ancestor.getScrollX();
300 pt[1] += ancestor.getScrollY();
301
302 if (next != null) {
303 pt[0] -= next.getLeft();
304 pt[1] -= next.getTop();
305 next.getMatrix().invert(inverse);
306 inverse.mapPoints(pt);
307 scale *= next.getScaleX();
308 }
309 }
310
311 coord[0] = (int) Math.round(pt[0]);
312 coord[1] = (int) Math.round(pt[1]);
313 return scale;
314 }
315
Jason Monk02dd7ae2014-04-15 15:23:31 -0400316 /**
317 * Utility method to determine whether the given point, in local coordinates,
318 * is inside the view, where the area of the view is expanded by the slop factor.
319 * This method is called while processing touch-move events to determine if the event
320 * is still within the view.
321 */
322 public static boolean pointInView(View v, float localX, float localY, float slop) {
323 return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) &&
324 localY < (v.getHeight() + slop);
325 }
326
Joe Onorato6665c0f2009-09-02 15:27:24 -0700327 private static void initStatics(Context context) {
328 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400329 final DisplayMetrics metrics = resources.getDisplayMetrics();
330 final float density = metrics.density;
331
Michael Jurkac9a96192010-11-01 11:52:08 -0700332 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700333 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700334 }
335
Winson Chung5f8afe62013-08-12 16:19:28 -0700336 public static void setIconSize(int widthPx) {
337 sIconWidth = sIconHeight = widthPx;
338 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700339 }
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200340
Winson Chung3a6e7f32013-10-09 15:50:52 -0700341 public static void scaleRect(Rect r, float scale) {
342 if (scale != 1.0f) {
343 r.left = (int) (r.left * scale + 0.5f);
344 r.top = (int) (r.top * scale + 0.5f);
345 r.right = (int) (r.right * scale + 0.5f);
346 r.bottom = (int) (r.bottom * scale + 0.5f);
347 }
348 }
349
350 public static void scaleRectAboutCenter(Rect r, float scale) {
351 int cx = r.centerX();
352 int cy = r.centerY();
353 r.offset(-cx, -cy);
354 Utilities.scaleRect(r, scale);
355 r.offset(cx, cy);
356 }
357
Michael Jurkaa805e1a2013-08-22 15:00:33 +0200358 public static void startActivityForResultSafely(
359 Activity activity, Intent intent, int requestCode) {
360 try {
361 activity.startActivityForResult(intent, requestCode);
362 } catch (ActivityNotFoundException e) {
363 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
364 } catch (SecurityException e) {
365 Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
366 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
367 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
368 "or use the exported attribute for this activity.", e);
369 }
370 }
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700371
372 static boolean isSystemApp(Context context, Intent intent) {
373 PackageManager pm = context.getPackageManager();
374 ComponentName cn = intent.getComponent();
375 String packageName = null;
376 if (cn == null) {
377 ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
378 if ((info != null) && (info.activityInfo != null)) {
379 packageName = info.activityInfo.packageName;
380 }
381 } else {
382 packageName = cn.getPackageName();
383 }
384 if (packageName != null) {
385 try {
386 PackageInfo info = pm.getPackageInfo(packageName, 0);
387 return (info != null) && (info.applicationInfo != null) &&
388 ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
389 } catch (NameNotFoundException e) {
390 return false;
391 }
392 } else {
393 return false;
394 }
395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396}