blob: 0529cfb7452aa6c546f503cf0393efea0e65649f [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
Winson Chung97d85d22011-04-13 11:27:36 -070019import java.util.Random;
20
Winson Chungaafa03c2010-06-11 17:34:16 -070021import android.content.Context;
22import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040024import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Joe Onorato56d82912010-03-07 14:32:10 -050026import android.graphics.ColorMatrix;
27import android.graphics.ColorMatrixColorFilter;
Winson Chungc763c4e2013-07-19 13:49:06 -070028import android.graphics.Matrix;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070030import android.graphics.PaintFlagsDrawFilter;
Joe Onorato1291a8c2009-09-15 15:07:25 -040031import android.graphics.PorterDuff;
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;
Winson Chungc763c4e2013-07-19 13:49:06 -070037import android.view.View;
38import android.view.ViewGroup;
39
40import java.util.ArrayList;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
Daniel Sandler325dc232013-06-05 22:57:57 -040042import com.android.launcher3.R;
Romain Guyedcce092010-03-04 13:03:17 -080043
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044/**
45 * Various utilities shared amongst the Launcher's classes.
46 */
47final class Utilities {
Michael Jurka3a9fced2012-04-13 14:44:29 -070048 @SuppressWarnings("unused")
Joe Onorato1291a8c2009-09-15 15:07:25 -040049 private static final String TAG = "Launcher.Utilities";
50
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051 private static int sIconWidth = -1;
52 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070053 private static int sIconTextureWidth = -1;
54 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055
Joe Onorato1291a8c2009-09-15 15:07:25 -040056 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050057 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050058 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050059 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070061 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062
63 static {
64 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
65 Paint.FILTER_BITMAP_FLAG));
66 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070067 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
68 static int sColorIndex = 0;
69
70 /**
Michael Jurka931dc972011-08-05 15:08:15 -070071 * 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 Jurka3a9fced2012-04-13 14:44:29 -070091 final Resources resources = context.getResources();
92 return createIconBitmap(new BitmapDrawable(resources, icon), context);
Michael Jurka931dc972011-08-05 15:08:15 -070093 }
94 }
95
96 /**
97 * Returns a bitmap suitable for the all apps view.
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080099 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 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 Onorato6665c0f2009-09-02 15:27:24 -0700108 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 Jurka931dc972011-08-05 15:08:15 -0700122 if (sourceWidth > 0 && sourceHeight > 0) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700123 // 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 Onorato6665c0f2009-09-02 15:27:24 -0700129 }
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 Jurka3a9fced2012-04-13 14:44:29 -0700144 @SuppressWarnings("all") // suppress dead code warning
145 final boolean debug = false;
146 if (debug) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700147 // 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 Cohenaaf473c2011-08-03 12:02:47 -0700159 canvas.setBitmap(null);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700160
161 return bitmap;
162 }
163 }
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 /**
166 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 * @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 Onorato0589f0f2010-02-08 13:44:00 -0800174 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400175 synchronized (sCanvas) { // we share the statics :-(
176 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700177 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 }
179
Joe Onorato0589f0f2010-02-08 13:44:00 -0800180 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
181 return bitmap;
182 } else {
Michael Jurka3a9fced2012-04-13 14:44:29 -0700183 final Resources resources = context.getResources();
184 return createIconBitmap(new BitmapDrawable(resources, bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700188
Winson Chungc763c4e2013-07-19 13:49:06 -0700189 /**
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 Onorato6665c0f2009-09-02 15:27:24 -0700279 private static void initStatics(Context context) {
280 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400281 final DisplayMetrics metrics = resources.getDisplayMetrics();
282 final float density = metrics.density;
283
Michael Jurkac9a96192010-11-01 11:52:08 -0700284 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Winson Chung4b825dcd2011-06-19 12:41:22 -0700285 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400286
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500287 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500288 sGlowColorPressedPaint.setColor(0xffffc300);
Joe Onoratoc61cff92009-11-08 11:54:39 -0500289 sGlowColorFocusedPaint.setColor(0xffff8e00);
Joe Onorato56d82912010-03-07 14:32:10 -0500290
291 ColorMatrix cm = new ColorMatrix();
292 cm.setSaturation(0.2f);
293 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
294 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700295 }
296
Winson Chung5f8afe62013-08-12 16:19:28 -0700297 public static void setIconSize(int widthPx) {
298 sIconWidth = sIconHeight = widthPx;
299 sIconTextureWidth = sIconTextureHeight = widthPx;
Winson Chung97d85d22011-04-13 11:27:36 -0700300 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301}