blob: dbb5ac663e354709cde2a40772e7b88181c25288 [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
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070019import android.graphics.drawable.BitmapDrawable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080020import android.graphics.drawable.Drawable;
21import android.graphics.drawable.PaintDrawable;
22import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040023import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070026import android.graphics.PaintFlagsDrawFilter;
27import android.graphics.PixelFormat;
Joe Onorato1291a8c2009-09-15 15:07:25 -040028import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070030import android.graphics.RectF;
31import android.graphics.Typeface;
32import android.text.Layout.Alignment;
33import android.text.StaticLayout;
34import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070035import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070036import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.res.Resources;
38import android.content.Context;
39
40/**
41 * Various utilities shared amongst the Launcher's classes.
42 */
43final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040044 private static final String TAG = "Launcher.Utilities";
45
Joe Onorato9392a752009-09-15 17:13:09 -040046 private static final boolean TEXT_BURN = false;
47
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048 private static int sIconWidth = -1;
49 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070050 private static int sIconTextureWidth = -1;
51 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052
53 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040054 private static final Paint sBlurPaint = new Paint();
55 private static final Paint sGlowColorPaint = new Paint();
56 private static final Paint sEmptyPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057 private static final Rect sBounds = new Rect();
58 private static final Rect sOldBounds = new Rect();
59 private static Canvas sCanvas = new Canvas();
60
61 static {
62 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
63 Paint.FILTER_BITMAP_FLAG));
64 }
65
66 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
67 final int bitmapWidth = bitmap.getWidth();
68 final int bitmapHeight = bitmap.getHeight();
69
70 if (bitmapWidth < width || bitmapHeight < height) {
71 int color = context.getResources().getColor(R.color.window_background);
72
73 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
74 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070075 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076 Canvas canvas = new Canvas(centered);
77 canvas.drawColor(color);
78 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
79 null);
80
81 bitmap = centered;
82 }
83
84 return bitmap;
85 }
86
87 /**
88 * Returns a Drawable representing the thumbnail of the specified Drawable.
89 * The size of the thumbnail is defined by the dimension
90 * android.R.dimen.launcher_application_icon_size.
91 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092 * @param icon The icon to get a thumbnail of.
93 * @param context The application's context.
94 *
95 * @return A thumbnail for the specified icon or the icon itself if the
Jason Samsfd22dac2009-09-20 17:24:16 -070096 * thumbnail could not be created.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097 */
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 static Drawable createIconThumbnail(Drawable icon, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -040099 synchronized (sCanvas) { // we share the statics :-(
100 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 initStatics(context);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103
Joe Onorato9c1289c2009-08-17 11:03:03 -0400104 int width = sIconWidth;
105 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 float scale = 1.0f;
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());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 }
120 int iconWidth = icon.getIntrinsicWidth();
121 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 if (iconWidth > 0 && iconWidth > 0) {
124 if (width < iconWidth || height < iconHeight || scale != 1.0f) {
125 final float ratio = (float) iconWidth / iconHeight;
126
127 if (iconWidth > iconHeight) {
128 height = (int) (width / ratio);
129 } else if (iconHeight > iconWidth) {
130 width = (int) (height * ratio);
131 }
132
133 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
134 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
135 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
136 final Canvas canvas = sCanvas;
137 canvas.setBitmap(thumb);
138 // Copy the old bounds to restore them later
139 // If we were to do oldBounds = icon.getBounds(),
140 // the call to setBounds() that follows would
141 // change the same instance and we would lose the
142 // old bounds
143 sOldBounds.set(icon.getBounds());
144 final int x = (sIconWidth - width) / 2;
145 final int y = (sIconHeight - height) / 2;
146 icon.setBounds(x, y, x + width, y + height);
147 icon.draw(canvas);
148 icon.setBounds(sOldBounds);
149 icon = new FastBitmapDrawable(thumb);
150 } else if (iconWidth < width && iconHeight < height) {
151 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
152 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
153 final Canvas canvas = sCanvas;
154 canvas.setBitmap(thumb);
155 sOldBounds.set(icon.getBounds());
156 final int x = (width - iconWidth) / 2;
157 final int y = (height - iconHeight) / 2;
158 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
159 icon.draw(canvas);
160 icon.setBounds(sOldBounds);
161 icon = new FastBitmapDrawable(thumb);
162 }
163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 return icon;
166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 }
168
Joe Onorato6665c0f2009-09-02 15:27:24 -0700169 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
170 static int sColorIndex = 0;
171
172 /**
173 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
174 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
175 */
176 static Bitmap createAllAppsBitmap(Drawable icon, Context context) {
177 synchronized (sCanvas) { // we share the statics :-(
178 if (sIconWidth == -1) {
179 initStatics(context);
180 }
181
182 int width = sIconWidth;
183 int height = sIconHeight;
184
185 float scale = 1.0f;
186 if (icon instanceof PaintDrawable) {
187 PaintDrawable painter = (PaintDrawable) icon;
188 painter.setIntrinsicWidth(width);
189 painter.setIntrinsicHeight(height);
190 } else if (icon instanceof BitmapDrawable) {
191 // Ensure the bitmap has a density.
192 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
193 Bitmap bitmap = bitmapDrawable.getBitmap();
194 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
195 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
196 }
197 }
198 int sourceWidth = icon.getIntrinsicWidth();
199 int sourceHeight = icon.getIntrinsicHeight();
200
201 if (sourceWidth > 0 && sourceWidth > 0) {
202 // There are intrinsic sizes.
203 if (width < sourceWidth || height < sourceHeight || scale != 1.0f) {
204 // It's too big, scale it down.
205 final float ratio = (float) sourceWidth / sourceHeight;
206 if (sourceWidth > sourceHeight) {
207 height = (int) (width / ratio);
208 } else if (sourceHeight > sourceWidth) {
209 width = (int) (height * ratio);
210 }
211 } else if (sourceWidth < width && sourceHeight < height) {
212 // It's small, use the size they gave us.
213 width = sourceWidth;
214 height = sourceWidth;
215 }
216 }
217
218 // no intrinsic size --> use default size
219 int textureWidth = sIconTextureWidth;
220 int textureHeight = sIconTextureHeight;
221
222 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
223 Bitmap.Config.ARGB_8888);
224 final Canvas canvas = sCanvas;
225 canvas.setBitmap(bitmap);
226
227 final int left = (textureWidth-width) / 2;
228 final int top = (textureHeight-height) / 2;
229
230 if (false) {
231 // draw a big box for the icon for debugging
232 canvas.drawColor(sColors[sColorIndex]);
233 if (++sColorIndex >= sColors.length) sColorIndex = 0;
234 Paint debugPaint = new Paint();
235 debugPaint.setColor(0xffcccc00);
236 canvas.drawRect(left, top, left+width, top+height, debugPaint);
237 }
238
239 sOldBounds.set(icon.getBounds());
240 icon.setBounds(left, top, left+width, top+height);
241 icon.draw(canvas);
242 icon.setBounds(sOldBounds);
243
244 return bitmap;
245 }
246 }
247
Joe Onorato1291a8c2009-09-15 15:07:25 -0400248 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, Bitmap src) {
249 synchronized (sCanvas) { // we share the statics :-(
250 if (sIconWidth == -1) {
251 // We can't have gotten to here without src being initialized, which
252 // comes from this file already. So just assert.
253 //initStatics(context);
254 throw new RuntimeException("Assertion failed: Utilities not initialized");
255 }
256
257 dest.drawColor(0, PorterDuff.Mode.CLEAR);
258
259 final float scale = 1.55f;
260 Bitmap scaled = Bitmap.createScaledBitmap(src, (int)(src.getWidth()*scale),
261 (int)(src.getHeight()*scale), true);
262
263 int[] xy = new int[2];
264 Bitmap mask = scaled.extractAlpha(sBlurPaint, xy);
265
266 dest.drawBitmap(mask, (destWidth - mask.getWidth()) / 2,
267 (destHeight - mask.getHeight()) / 2, sGlowColorPaint);
268 dest.drawBitmap(src, (destWidth - src.getWidth()) / 2,
269 (destHeight - src.getHeight()) / 2, sEmptyPaint);
270
271 mask.recycle();
272 }
273 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 /**
276 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
277 * The size of the thumbnail is defined by the dimension
278 * android.R.dimen.launcher_application_icon_size.
279 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 * @param bitmap The bitmap to get a thumbnail of.
281 * @param context The application's context.
282 *
283 * @return A thumbnail for the specified bitmap or the bitmap itself if the
284 * thumbnail could not be created.
285 */
286 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 synchronized (sCanvas) { // we share the statics :-(
288 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700289 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 int width = sIconWidth;
293 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 final int bitmapWidth = bitmap.getWidth();
296 final int bitmapHeight = bitmap.getHeight();
297
298 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
299 final float ratio = (float) bitmapWidth / bitmapHeight;
300
301 if (bitmapWidth > bitmapHeight) {
302 height = (int) (width / ratio);
303 } else if (bitmapHeight > bitmapWidth) {
304 width = (int) (height * ratio);
305 }
306
307 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
308 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
309 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
310 final Canvas canvas = sCanvas;
311 final Paint paint = sPaint;
312 canvas.setBitmap(thumb);
313 paint.setDither(false);
314 paint.setFilterBitmap(true);
315 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
316 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
317 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
318 return thumb;
319 }
320
321 return bitmap;
322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700324
Joe Onorato6665c0f2009-09-02 15:27:24 -0700325 private static void initStatics(Context context) {
326 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400327 final DisplayMetrics metrics = resources.getDisplayMetrics();
328 final float density = metrics.density;
329
Joe Onorato6665c0f2009-09-02 15:27:24 -0700330 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
331 sIconTextureWidth = sIconTextureHeight = roundToPow2(sIconWidth);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400332
333 sBlurPaint.setMaskFilter(new BlurMaskFilter(7 * density, BlurMaskFilter.Blur.NORMAL));
334 sGlowColorPaint.setColor(0xffff9000);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700335 }
336
Joe Onoratobf15cb42009-08-07 14:33:40 -0700337 static class BubbleText {
338 private static final int MAX_LINES = 2;
339 private TextPaint mTextPaint;
340 private Paint mRectPaint;
341
342 private float mBubblePadding;
343 private float mCornerRadius;
344 private RectF mBubbleRect = new RectF();
345
346 private float mTextWidth;
347 private int mLeading;
348 private int mFirstLineY;
349 private int mLineHeight;
350
351 private int mBitmapWidth;
352 private int mBitmapHeight;
353
354 BubbleText(Context context) {
355 final Resources resources = context.getResources();
356
357 final float scale = resources.getDisplayMetrics().density;
358
359 final float paddingLeft = 5.0f * scale;
360 final float paddingRight = 5.0f * scale;
361 final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
362 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700363 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700364
365 RectF bubbleRect = mBubbleRect;
366 bubbleRect.left = 0;
367 bubbleRect.top = 0;
368 bubbleRect.right = (int)(bubbleWidth+0.5f);
369
370 mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
371 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
372
373 Paint rectPaint = mRectPaint = new Paint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700374 rectPaint.setColor(0xff000000);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700375 rectPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700376
Joe Onorato1291a8c2009-09-15 15:07:25 -0400377 Log.d(TAG, "scale=" + scale + " textSize=" + (13*scale));
Joe Onoratoefabe002009-08-28 09:38:18 -0700378
Joe Onoratobf15cb42009-08-07 14:33:40 -0700379 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700380 textPaint.setTypeface(Typeface.DEFAULT);
381 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700382 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700383 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400384 if (TEXT_BURN) {
385 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
386 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700387
388 float ascent = -textPaint.ascent();
389 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700390 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700391 mLeading = (int)(leading + 0.5f);
392 mFirstLineY = (int)(leading + ascent + 0.5f);
393 mLineHeight = (int)(leading + ascent + descent + 0.5f);
394
Joe Onoratobf15cb42009-08-07 14:33:40 -0700395 mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700396 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700397
Joe Onorato1291a8c2009-09-15 15:07:25 -0400398 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
Joe Onoratobf15cb42009-08-07 14:33:40 -0700399 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700400 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700401 }
402
403 /** You own the bitmap after this and you must call recycle on it. */
404 Bitmap createTextBitmap(String text) {
405 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
406 Canvas c = new Canvas(b);
407
408 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
409 Alignment.ALIGN_CENTER, 1, 0, true);
410 int lineCount = layout.getLineCount();
411 if (lineCount > MAX_LINES) {
412 lineCount = MAX_LINES;
413 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700414 //if (!TEXT_BURN && lineCount > 0) {
415 //RectF bubbleRect = mBubbleRect;
416 //bubbleRect.bottom = height(lineCount);
417 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
418 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700419 for (int i=0; i<lineCount; i++) {
420 int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
421 int y = mFirstLineY + (i * mLineHeight);
422 c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
423 x, y, mTextPaint);
424 }
425
426 return b;
427 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700428
429 private int height(int lineCount) {
430 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
431 }
432
433 int getBubbleWidth() {
434 return (int)(mBubbleRect.width() + 0.5f);
435 }
436
437 int getMaxBubbleHeight() {
438 return height(MAX_LINES);
439 }
440
441 int getBitmapWidth() {
442 return mBitmapWidth;
443 }
444
445 int getBitmapHeight() {
446 return mBitmapHeight;
447 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700448 }
449
450 /** Only works for positive numbers. */
451 static int roundToPow2(int n) {
452 int orig = n;
453 n >>= 1;
454 int mask = 0x8000000;
455 while (mask != 0 && (n & mask) == 0) {
456 mask >>= 1;
457 }
458 while (mask != 0) {
459 n |= mask;
460 mask >>= 1;
461 }
462 n += 1;
463 if (n != orig) {
464 n <<= 1;
465 }
466 return n;
467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468}