The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 1 | /* |
| 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 Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame^] | 19 | import android.app.Activity; |
| 20 | import android.content.ActivityNotFoundException; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame^] | 22 | import android.content.Intent; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 25 | import android.graphics.BlurMaskFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 27 | import android.graphics.ColorMatrix; |
| 28 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 29 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 31 | import android.graphics.PaintFlagsDrawFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.graphics.Rect; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.BitmapDrawable; |
| 34 | import android.graphics.drawable.Drawable; |
| 35 | import android.graphics.drawable.PaintDrawable; |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 36 | import android.util.DisplayMetrics; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame^] | 37 | import android.util.Log; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 38 | import android.view.View; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame^] | 39 | import android.widget.Toast; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 40 | |
| 41 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Various utilities shared amongst the Launcher's classes. |
| 45 | */ |
| 46 | final class Utilities { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 47 | @SuppressWarnings("unused") |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 48 | private static final String TAG = "Launcher.Utilities"; |
| 49 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | private static int sIconWidth = -1; |
| 51 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 52 | private static int sIconTextureWidth = -1; |
| 53 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 55 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 56 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 57 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 58 | private static final Paint sDisabledPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 60 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
| 62 | static { |
| 63 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 64 | Paint.FILTER_BITMAP_FLAG)); |
| 65 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 66 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 67 | static int sColorIndex = 0; |
| 68 | |
| 69 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 70 | * 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 Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 90 | final Resources resources = context.getResources(); |
| 91 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 97 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 98 | static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 99 | 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 Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 107 | 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 Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 121 | if (sourceWidth > 0 && sourceHeight > 0) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 122 | // 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 Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 128 | } |
| 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 Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 143 | @SuppressWarnings("all") // suppress dead code warning |
| 144 | final boolean debug = false; |
| 145 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 146 | // 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 Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 158 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 159 | |
| 160 | return bitmap; |
| 161 | } |
| 162 | } |
| 163 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 164 | /** |
| 165 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 166 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 167 | * @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 Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 173 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 174 | synchronized (sCanvas) { // we share the statics :-( |
| 175 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 176 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 179 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 180 | return bitmap; |
| 181 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 182 | final Resources resources = context.getResources(); |
| 183 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 184 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 185 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 186 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 187 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 188 | /** |
| 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 Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 278 | private static void initStatics(Context context) { |
| 279 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 280 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 281 | final float density = metrics.density; |
| 282 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 283 | sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 284 | sIconTextureWidth = sIconTextureHeight = sIconWidth; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 285 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 286 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 287 | sGlowColorPressedPaint.setColor(0xffffc300); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 288 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 289 | |
| 290 | ColorMatrix cm = new ColorMatrix(); |
| 291 | cm.setSaturation(0.2f); |
| 292 | sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); |
| 293 | sDisabledPaint.setAlpha(0x88); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 296 | public static void setIconSize(int widthPx) { |
| 297 | sIconWidth = sIconHeight = widthPx; |
| 298 | sIconTextureWidth = sIconTextureHeight = widthPx; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 299 | } |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame^] | 300 | |
| 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 Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 314 | } |