blob: 775833391b97d0a4c034636f948081f006bfee5b [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070029import android.graphics.PaintFlagsDrawFilter;
Joe Onorato1291a8c2009-09-15 15:07:25 -040030import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070032import android.graphics.RectF;
Mike Reedcdd11792009-10-29 17:27:55 -040033import android.graphics.TableMaskFilter;
Joe Onoratobf15cb42009-08-07 14:33:40 -070034import android.graphics.Typeface;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.graphics.drawable.BitmapDrawable;
36import android.graphics.drawable.Drawable;
37import android.graphics.drawable.PaintDrawable;
Joe Onoratobf15cb42009-08-07 14:33:40 -070038import android.text.StaticLayout;
39import android.text.TextPaint;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.text.Layout.Alignment;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070041import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
Romain Guyedcce092010-03-04 13:03:17 -080044import com.android.launcher.R;
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046/**
47 * Various utilities shared amongst the Launcher's classes.
48 */
49final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040050 private static final String TAG = "Launcher.Utilities";
51
Joe Onorato9392a752009-09-15 17:13:09 -040052 private static final boolean TEXT_BURN = false;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 private static int sIconWidth = -1;
55 private static int sIconHeight = -1;
Michael Jurka7ef959b2011-02-23 11:48:32 -080056 private static int sIconContentSize = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070057 private static int sIconTextureWidth = -1;
58 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
Joe Onorato1291a8c2009-09-15 15:07:25 -040060 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050061 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050062 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050063 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070065 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 static {
68 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
69 Paint.FILTER_BITMAP_FLAG));
70 }
Joe Onorato6665c0f2009-09-02 15:27:24 -070071 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
72 static int sColorIndex = 0;
73
Michael Jurka7ef959b2011-02-23 11:48:32 -080074 static int getIconContentSize() {
75 return sIconContentSize;
76 }
77
Joe Onorato6665c0f2009-09-02 15:27:24 -070078 /**
79 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
80 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
81 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080082 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070083 synchronized (sCanvas) { // we share the statics :-(
84 if (sIconWidth == -1) {
85 initStatics(context);
86 }
87
88 int width = sIconWidth;
89 int height = sIconHeight;
90
Joe Onorato6665c0f2009-09-02 15:27:24 -070091 if (icon instanceof PaintDrawable) {
92 PaintDrawable painter = (PaintDrawable) icon;
93 painter.setIntrinsicWidth(width);
94 painter.setIntrinsicHeight(height);
95 } else if (icon instanceof BitmapDrawable) {
96 // Ensure the bitmap has a density.
97 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
98 Bitmap bitmap = bitmapDrawable.getBitmap();
99 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
100 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
101 }
102 }
103 int sourceWidth = icon.getIntrinsicWidth();
104 int sourceHeight = icon.getIntrinsicHeight();
105
106 if (sourceWidth > 0 && sourceWidth > 0) {
107 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700108 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 // It's too big, scale it down.
110 final float ratio = (float) sourceWidth / sourceHeight;
111 if (sourceWidth > sourceHeight) {
112 height = (int) (width / ratio);
113 } else if (sourceHeight > sourceWidth) {
114 width = (int) (height * ratio);
115 }
116 } else if (sourceWidth < width && sourceHeight < height) {
Winson Chung4b825dcd2011-06-19 12:41:22 -0700117 // Don't scale up the icon
Joe Onorato6665c0f2009-09-02 15:27:24 -0700118 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700119 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700120 }
121 }
122
123 // no intrinsic size --> use default size
124 int textureWidth = sIconTextureWidth;
125 int textureHeight = sIconTextureHeight;
126
127 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
128 Bitmap.Config.ARGB_8888);
129 final Canvas canvas = sCanvas;
130 canvas.setBitmap(bitmap);
131
132 final int left = (textureWidth-width) / 2;
133 final int top = (textureHeight-height) / 2;
134
135 if (false) {
136 // draw a big box for the icon for debugging
137 canvas.drawColor(sColors[sColorIndex]);
138 if (++sColorIndex >= sColors.length) sColorIndex = 0;
139 Paint debugPaint = new Paint();
140 debugPaint.setColor(0xffcccc00);
141 canvas.drawRect(left, top, left+width, top+height, debugPaint);
142 }
143
144 sOldBounds.set(icon.getBounds());
145 icon.setBounds(left, top, left+width, top+height);
146 icon.draw(canvas);
147 icon.setBounds(sOldBounds);
148
149 return bitmap;
150 }
151 }
152
Joe Onoratoc61cff92009-11-08 11:54:39 -0500153 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500154 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400155 synchronized (sCanvas) { // we share the statics :-(
156 if (sIconWidth == -1) {
157 // We can't have gotten to here without src being initialized, which
158 // comes from this file already. So just assert.
159 //initStatics(context);
160 throw new RuntimeException("Assertion failed: Utilities not initialized");
161 }
162
163 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800164
Joe Onorato1291a8c2009-09-15 15:07:25 -0400165 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400166 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400167
Mike Reedcdd11792009-10-29 17:27:55 -0400168 float px = (destWidth - src.getWidth()) / 2;
169 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500170 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500171 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400172
173 mask.recycle();
174 }
175 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 /**
178 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
179 * The size of the thumbnail is defined by the dimension
180 * android.R.dimen.launcher_application_icon_size.
181 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 * @param bitmap The bitmap to get a thumbnail of.
183 * @param context The application's context.
184 *
185 * @return A thumbnail for the specified bitmap or the bitmap itself if the
186 * thumbnail could not be created.
187 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800188 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 synchronized (sCanvas) { // we share the statics :-(
190 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700191 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 }
193
Joe Onorato0589f0f2010-02-08 13:44:00 -0800194 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
195 return bitmap;
196 } else {
197 return createIconBitmap(new BitmapDrawable(bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700201
Joe Onorato56d82912010-03-07 14:32:10 -0500202 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
203 synchronized (sCanvas) { // we share the statics :-(
204 if (sIconWidth == -1) {
205 initStatics(context);
206 }
207 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
208 Bitmap.Config.ARGB_8888);
209 final Canvas canvas = sCanvas;
210 canvas.setBitmap(disabled);
211
212 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
213
214 return disabled;
215 }
216 }
217
Joe Onorato6665c0f2009-09-02 15:27:24 -0700218 private static void initStatics(Context context) {
219 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400220 final DisplayMetrics metrics = resources.getDisplayMetrics();
221 final float density = metrics.density;
222
Michael Jurkac9a96192010-11-01 11:52:08 -0700223 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Michael Jurkaa2eb1702011-05-12 14:57:05 -0700224 if (LauncherApplication.isScreenLarge()) {
Michael Jurka7ef959b2011-02-23 11:48:32 -0800225 sIconContentSize = (int) resources.getDimension(R.dimen.app_icon_content_size);
226 }
Winson Chung4b825dcd2011-06-19 12:41:22 -0700227 sIconTextureWidth = sIconTextureHeight = sIconWidth;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400228
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500229 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500230 sGlowColorPressedPaint.setColor(0xffffc300);
231 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500232 sGlowColorFocusedPaint.setColor(0xffff8e00);
233 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato56d82912010-03-07 14:32:10 -0500234
235 ColorMatrix cm = new ColorMatrix();
236 cm.setSaturation(0.2f);
237 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
238 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700239 }
240
Joe Onoratobf15cb42009-08-07 14:33:40 -0700241 static class BubbleText {
242 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700243
Romain Guy88f38d12010-01-26 14:50:34 -0800244 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700245
Romain Guy88f38d12010-01-26 14:50:34 -0800246 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700247
Romain Guy88f38d12010-01-26 14:50:34 -0800248 private final float mTextWidth;
249 private final int mLeading;
250 private final int mFirstLineY;
251 private final int mLineHeight;
252
253 private final int mBitmapWidth;
254 private final int mBitmapHeight;
255 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700256
257 BubbleText(Context context) {
258 final Resources resources = context.getResources();
259
Romain Guy88f38d12010-01-26 14:50:34 -0800260 final DisplayMetrics metrics = resources.getDisplayMetrics();
261 final float scale = metrics.density;
262 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700263
Kenny Root97051642010-03-05 13:24:08 -0800264 final float paddingLeft = 2.0f * scale;
265 final float paddingRight = 2.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800266 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700267
268 RectF bubbleRect = mBubbleRect;
269 bubbleRect.left = 0;
270 bubbleRect.top = 0;
Romain Guyc0c4fe32010-02-12 23:20:25 +0100271 bubbleRect.right = (int) cellWidth;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700272
Kenny Root97051642010-03-05 13:24:08 -0800273 mTextWidth = cellWidth - paddingLeft - paddingRight;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700274
Joe Onoratobf15cb42009-08-07 14:33:40 -0700275 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700276 textPaint.setTypeface(Typeface.DEFAULT);
277 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700278 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700279 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400280 if (TEXT_BURN) {
281 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
282 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700283
284 float ascent = -textPaint.ascent();
285 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700286 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700287 mLeading = (int)(leading + 0.5f);
288 mFirstLineY = (int)(leading + ascent + 0.5f);
289 mLineHeight = (int)(leading + ascent + descent + 0.5f);
290
Romain Guy442eda22010-02-04 15:59:37 -0800291 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700292 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700293
Joe Onoratofccc8df2009-10-01 14:30:16 -0700294 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
295
Joe Onorato080d9b62009-11-02 12:01:11 -0500296 if (false) {
297 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
298 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
299 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
300 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700301 }
302
303 /** You own the bitmap after this and you must call recycle on it. */
304 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800305 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800306 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700307 Canvas c = new Canvas(b);
308
309 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
310 Alignment.ALIGN_CENTER, 1, 0, true);
311 int lineCount = layout.getLineCount();
312 if (lineCount > MAX_LINES) {
313 lineCount = MAX_LINES;
314 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700315 //if (!TEXT_BURN && lineCount > 0) {
316 //RectF bubbleRect = mBubbleRect;
317 //bubbleRect.bottom = height(lineCount);
318 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
319 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700320 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700321 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
322 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800323 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700324 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800325 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700326 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800327 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700328 }
329
330 return b;
331 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700332
333 private int height(int lineCount) {
334 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
335 }
336
337 int getBubbleWidth() {
338 return (int)(mBubbleRect.width() + 0.5f);
339 }
340
341 int getMaxBubbleHeight() {
342 return height(MAX_LINES);
343 }
344
345 int getBitmapWidth() {
346 return mBitmapWidth;
347 }
348
349 int getBitmapHeight() {
350 return mBitmapHeight;
351 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700352 }
353
354 /** Only works for positive numbers. */
355 static int roundToPow2(int n) {
356 int orig = n;
357 n >>= 1;
358 int mask = 0x8000000;
359 while (mask != 0 && (n & mask) == 0) {
360 mask >>= 1;
361 }
362 while (mask != 0) {
363 n |= mask;
364 mask >>= 1;
365 }
366 n += 1;
367 if (n != orig) {
368 n <<= 1;
369 }
370 return n;
371 }
Winson Chung97d85d22011-04-13 11:27:36 -0700372
373 static int generateRandomId() {
374 return new Random(System.currentTimeMillis()).nextInt(1 << 24);
375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376}