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 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 19 | import java.util.Random; |
| 20 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 24 | import android.graphics.BlurMaskFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 26 | import android.graphics.ColorMatrix; |
| 27 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 28 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 30 | import android.graphics.PaintFlagsDrawFilter; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 31 | import android.graphics.PorterDuff; |
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; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 37 | import android.view.View; |
| 38 | import android.view.ViewGroup; |
| 39 | |
| 40 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 42 | import com.android.launcher3.R; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Various utilities shared amongst the Launcher's classes. |
| 46 | */ |
| 47 | final class Utilities { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 48 | @SuppressWarnings("unused") |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 49 | private static final String TAG = "Launcher.Utilities"; |
| 50 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | private static int sIconWidth = -1; |
| 52 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 53 | private static int sIconTextureWidth = -1; |
| 54 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 56 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 57 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 58 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 59 | private static final Paint sDisabledPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 61 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | |
| 63 | static { |
| 64 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 65 | Paint.FILTER_BITMAP_FLAG)); |
| 66 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 67 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 68 | static int sColorIndex = 0; |
| 69 | |
| 70 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 71 | * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS |
| 72 | * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) |
| 73 | * to the proper size (48dp) |
| 74 | */ |
| 75 | static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 76 | int textureWidth = sIconTextureWidth; |
| 77 | int textureHeight = sIconTextureHeight; |
| 78 | int sourceWidth = icon.getWidth(); |
| 79 | int sourceHeight = icon.getHeight(); |
| 80 | if (sourceWidth > textureWidth && sourceHeight > textureHeight) { |
| 81 | // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) |
| 82 | return Bitmap.createBitmap(icon, |
| 83 | (sourceWidth - textureWidth) / 2, |
| 84 | (sourceHeight - textureHeight) / 2, |
| 85 | textureWidth, textureHeight); |
| 86 | } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { |
| 87 | // Icon is the right size, no need to change it |
| 88 | return icon; |
| 89 | } else { |
| 90 | // Icon is too small, render to a larger bitmap |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 91 | final Resources resources = context.getResources(); |
| 92 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 98 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 99 | static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 100 | synchronized (sCanvas) { // we share the statics :-( |
| 101 | if (sIconWidth == -1) { |
| 102 | initStatics(context); |
| 103 | } |
| 104 | |
| 105 | int width = sIconWidth; |
| 106 | int height = sIconHeight; |
| 107 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 108 | if (icon instanceof PaintDrawable) { |
| 109 | PaintDrawable painter = (PaintDrawable) icon; |
| 110 | painter.setIntrinsicWidth(width); |
| 111 | painter.setIntrinsicHeight(height); |
| 112 | } else if (icon instanceof BitmapDrawable) { |
| 113 | // Ensure the bitmap has a density. |
| 114 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 115 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 116 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 117 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 118 | } |
| 119 | } |
| 120 | int sourceWidth = icon.getIntrinsicWidth(); |
| 121 | int sourceHeight = icon.getIntrinsicHeight(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 122 | if (sourceWidth > 0 && sourceHeight > 0) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame^] | 123 | // Scale the icon proportionally to the icon dimensions |
| 124 | final float ratio = (float) sourceWidth / sourceHeight; |
| 125 | if (sourceWidth > sourceHeight) { |
| 126 | height = (int) (width / ratio); |
| 127 | } else if (sourceHeight > sourceWidth) { |
| 128 | width = (int) (height * ratio); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | // no intrinsic size --> use default size |
| 133 | int textureWidth = sIconTextureWidth; |
| 134 | int textureHeight = sIconTextureHeight; |
| 135 | |
| 136 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 137 | Bitmap.Config.ARGB_8888); |
| 138 | final Canvas canvas = sCanvas; |
| 139 | canvas.setBitmap(bitmap); |
| 140 | |
| 141 | final int left = (textureWidth-width) / 2; |
| 142 | final int top = (textureHeight-height) / 2; |
| 143 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 144 | @SuppressWarnings("all") // suppress dead code warning |
| 145 | final boolean debug = false; |
| 146 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 147 | // draw a big box for the icon for debugging |
| 148 | canvas.drawColor(sColors[sColorIndex]); |
| 149 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 150 | Paint debugPaint = new Paint(); |
| 151 | debugPaint.setColor(0xffcccc00); |
| 152 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 153 | } |
| 154 | |
| 155 | sOldBounds.set(icon.getBounds()); |
| 156 | icon.setBounds(left, top, left+width, top+height); |
| 157 | icon.draw(canvas); |
| 158 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 159 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 160 | |
| 161 | return bitmap; |
| 162 | } |
| 163 | } |
| 164 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 165 | /** |
| 166 | * 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] | 167 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 168 | * @param bitmap The bitmap to get a thumbnail of. |
| 169 | * @param context The application's context. |
| 170 | * |
| 171 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 172 | * thumbnail could not be created. |
| 173 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 174 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 175 | synchronized (sCanvas) { // we share the statics :-( |
| 176 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 177 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 180 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 181 | return bitmap; |
| 182 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 183 | final Resources resources = context.getResources(); |
| 184 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 185 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 186 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 187 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 188 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Given a coordinate relative to the descendant, find the coordinate in a parent view's |
| 191 | * coordinates. |
| 192 | * |
| 193 | * @param descendant The descendant to which the passed coordinate is relative. |
| 194 | * @param root The root view to make the coordinates relative to. |
| 195 | * @param coord The coordinate that we want mapped. |
| 196 | * @param includeRootScroll Whether or not to account for the scroll of the descendant: |
| 197 | * sometimes this is relevant as in a child's coordinates within the descendant. |
| 198 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 199 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 200 | * assumption fails, we will need to return a pair of scale factors. |
| 201 | */ |
| 202 | public static float getDescendantCoordRelativeToParent(View descendant, View root, |
| 203 | int[] coord, boolean includeRootScroll) { |
| 204 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 205 | |
| 206 | float[] pt = {coord[0], coord[1]}; |
| 207 | |
| 208 | View v = descendant; |
| 209 | while(v != root && v != null) { |
| 210 | ancestorChain.add(v); |
| 211 | v = (View) v.getParent(); |
| 212 | } |
| 213 | ancestorChain.add(root); |
| 214 | |
| 215 | float scale = 1.0f; |
| 216 | int count = ancestorChain.size(); |
| 217 | for (int i = 0; i < count; i++) { |
| 218 | View v0 = ancestorChain.get(i); |
| 219 | View v1 = i < count -1 ? ancestorChain.get(i + 1) : null; |
| 220 | |
| 221 | // For TextViews, scroll has a meaning which relates to the text position |
| 222 | // which is very strange... ignore the scroll. |
| 223 | if (v0 != descendant || includeRootScroll) { |
| 224 | pt[0] -= v0.getScrollX(); |
| 225 | pt[1] -= v0.getScrollY(); |
| 226 | } |
| 227 | |
| 228 | v0.getMatrix().mapPoints(pt); |
| 229 | pt[0] += v0.getLeft(); |
| 230 | pt[1] += v0.getTop(); |
| 231 | scale *= v0.getScaleX(); |
| 232 | } |
| 233 | |
| 234 | coord[0] = (int) Math.round(pt[0]); |
| 235 | coord[1] = (int) Math.round(pt[1]); |
| 236 | return scale; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}. |
| 241 | */ |
| 242 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 243 | int[] coord) { |
| 244 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 245 | |
| 246 | float[] pt = {coord[0], coord[1]}; |
| 247 | |
| 248 | View v = descendant; |
| 249 | while(v != root) { |
| 250 | ancestorChain.add(v); |
| 251 | v = (View) v.getParent(); |
| 252 | } |
| 253 | ancestorChain.add(root); |
| 254 | |
| 255 | float scale = 1.0f; |
| 256 | Matrix inverse = new Matrix(); |
| 257 | int count = ancestorChain.size(); |
| 258 | for (int i = count - 1; i >= 0; i--) { |
| 259 | View ancestor = ancestorChain.get(i); |
| 260 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 261 | |
| 262 | pt[0] += ancestor.getScrollX(); |
| 263 | pt[1] += ancestor.getScrollY(); |
| 264 | |
| 265 | if (next != null) { |
| 266 | pt[0] -= next.getLeft(); |
| 267 | pt[1] -= next.getTop(); |
| 268 | next.getMatrix().invert(inverse); |
| 269 | inverse.mapPoints(pt); |
| 270 | scale *= next.getScaleX(); |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | coord[0] = (int) Math.round(pt[0]); |
| 275 | coord[1] = (int) Math.round(pt[1]); |
| 276 | return scale; |
| 277 | } |
| 278 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 279 | private static void initStatics(Context context) { |
| 280 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 281 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 282 | final float density = metrics.density; |
| 283 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 284 | sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 285 | sIconTextureWidth = sIconTextureHeight = sIconWidth; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 286 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 287 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 288 | sGlowColorPressedPaint.setColor(0xffffc300); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 289 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 290 | |
| 291 | ColorMatrix cm = new ColorMatrix(); |
| 292 | cm.setSaturation(0.2f); |
| 293 | sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); |
| 294 | sDisabledPaint.setAlpha(0x88); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame^] | 297 | public static void setIconSize(int widthPx) { |
| 298 | sIconWidth = sIconHeight = widthPx; |
| 299 | sIconTextureWidth = sIconTextureHeight = widthPx; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 300 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 301 | } |