blob: 50757ca59b9002b68fb348d1207cd25cc7ec1815 [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
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046 private static int sIconWidth = -1;
47 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070048 private static int sIconTextureWidth = -1;
49 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050
51 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040052 private static final Paint sBlurPaint = new Paint();
53 private static final Paint sGlowColorPaint = new Paint();
54 private static final Paint sEmptyPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055 private static final Rect sBounds = new Rect();
56 private static final Rect sOldBounds = new Rect();
57 private static Canvas sCanvas = new Canvas();
58
59 static {
60 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
61 Paint.FILTER_BITMAP_FLAG));
62 }
63
64 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
65 final int bitmapWidth = bitmap.getWidth();
66 final int bitmapHeight = bitmap.getHeight();
67
68 if (bitmapWidth < width || bitmapHeight < height) {
69 int color = context.getResources().getColor(R.color.window_background);
70
71 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
72 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070073 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074 Canvas canvas = new Canvas(centered);
75 canvas.drawColor(color);
76 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
77 null);
78
79 bitmap = centered;
80 }
81
82 return bitmap;
83 }
84
85 /**
86 * Returns a Drawable representing the thumbnail of the specified Drawable.
87 * The size of the thumbnail is defined by the dimension
88 * android.R.dimen.launcher_application_icon_size.
89 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090 * @param icon The icon to get a thumbnail of.
91 * @param context The application's context.
92 *
93 * @return A thumbnail for the specified icon or the icon itself if the
94 * thumbnail could not be created.
95 */
Joe Onorato6665c0f2009-09-02 15:27:24 -070096 static Drawable createIconThumbnail(Drawable icon, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -040097 synchronized (sCanvas) { // we share the statics :-(
98 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070099 initStatics(context);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101
Joe Onorato9c1289c2009-08-17 11:03:03 -0400102 int width = sIconWidth;
103 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104
Joe Onorato9c1289c2009-08-17 11:03:03 -0400105 float scale = 1.0f;
106 if (icon instanceof PaintDrawable) {
107 PaintDrawable painter = (PaintDrawable) icon;
108 painter.setIntrinsicWidth(width);
109 painter.setIntrinsicHeight(height);
110 } else if (icon instanceof BitmapDrawable) {
111 // Ensure the bitmap has a density.
112 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
113 Bitmap bitmap = bitmapDrawable.getBitmap();
114 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
115 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 }
118 int iconWidth = icon.getIntrinsicWidth();
119 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 if (iconWidth > 0 && iconWidth > 0) {
122 if (width < iconWidth || height < iconHeight || scale != 1.0f) {
123 final float ratio = (float) iconWidth / iconHeight;
124
125 if (iconWidth > iconHeight) {
126 height = (int) (width / ratio);
127 } else if (iconHeight > iconWidth) {
128 width = (int) (height * ratio);
129 }
130
131 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
132 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
133 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
134 final Canvas canvas = sCanvas;
135 canvas.setBitmap(thumb);
136 // Copy the old bounds to restore them later
137 // If we were to do oldBounds = icon.getBounds(),
138 // the call to setBounds() that follows would
139 // change the same instance and we would lose the
140 // old bounds
141 sOldBounds.set(icon.getBounds());
142 final int x = (sIconWidth - width) / 2;
143 final int y = (sIconHeight - height) / 2;
144 icon.setBounds(x, y, x + width, y + height);
145 icon.draw(canvas);
146 icon.setBounds(sOldBounds);
147 icon = new FastBitmapDrawable(thumb);
148 } else if (iconWidth < width && iconHeight < height) {
149 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
150 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
151 final Canvas canvas = sCanvas;
152 canvas.setBitmap(thumb);
153 sOldBounds.set(icon.getBounds());
154 final int x = (width - iconWidth) / 2;
155 final int y = (height - iconHeight) / 2;
156 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
157 icon.draw(canvas);
158 icon.setBounds(sOldBounds);
159 icon = new FastBitmapDrawable(thumb);
160 }
161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Joe Onorato9c1289c2009-08-17 11:03:03 -0400163 return icon;
164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 }
166
Joe Onorato6665c0f2009-09-02 15:27:24 -0700167 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
168 static int sColorIndex = 0;
169
170 /**
171 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
172 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
173 */
174 static Bitmap createAllAppsBitmap(Drawable icon, Context context) {
175 synchronized (sCanvas) { // we share the statics :-(
176 if (sIconWidth == -1) {
177 initStatics(context);
178 }
179
180 int width = sIconWidth;
181 int height = sIconHeight;
182
183 float scale = 1.0f;
184 if (icon instanceof PaintDrawable) {
185 PaintDrawable painter = (PaintDrawable) icon;
186 painter.setIntrinsicWidth(width);
187 painter.setIntrinsicHeight(height);
188 } else if (icon instanceof BitmapDrawable) {
189 // Ensure the bitmap has a density.
190 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
191 Bitmap bitmap = bitmapDrawable.getBitmap();
192 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
193 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
194 }
195 }
196 int sourceWidth = icon.getIntrinsicWidth();
197 int sourceHeight = icon.getIntrinsicHeight();
198
199 if (sourceWidth > 0 && sourceWidth > 0) {
200 // There are intrinsic sizes.
201 if (width < sourceWidth || height < sourceHeight || scale != 1.0f) {
202 // It's too big, scale it down.
203 final float ratio = (float) sourceWidth / sourceHeight;
204 if (sourceWidth > sourceHeight) {
205 height = (int) (width / ratio);
206 } else if (sourceHeight > sourceWidth) {
207 width = (int) (height * ratio);
208 }
209 } else if (sourceWidth < width && sourceHeight < height) {
210 // It's small, use the size they gave us.
211 width = sourceWidth;
212 height = sourceWidth;
213 }
214 }
215
216 // no intrinsic size --> use default size
217 int textureWidth = sIconTextureWidth;
218 int textureHeight = sIconTextureHeight;
219
220 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
221 Bitmap.Config.ARGB_8888);
222 final Canvas canvas = sCanvas;
223 canvas.setBitmap(bitmap);
224
225 final int left = (textureWidth-width) / 2;
226 final int top = (textureHeight-height) / 2;
227
228 if (false) {
229 // draw a big box for the icon for debugging
230 canvas.drawColor(sColors[sColorIndex]);
231 if (++sColorIndex >= sColors.length) sColorIndex = 0;
232 Paint debugPaint = new Paint();
233 debugPaint.setColor(0xffcccc00);
234 canvas.drawRect(left, top, left+width, top+height, debugPaint);
235 }
236
237 sOldBounds.set(icon.getBounds());
238 icon.setBounds(left, top, left+width, top+height);
239 icon.draw(canvas);
240 icon.setBounds(sOldBounds);
241
242 return bitmap;
243 }
244 }
245
Joe Onorato1291a8c2009-09-15 15:07:25 -0400246 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, Bitmap src) {
247 synchronized (sCanvas) { // we share the statics :-(
248 if (sIconWidth == -1) {
249 // We can't have gotten to here without src being initialized, which
250 // comes from this file already. So just assert.
251 //initStatics(context);
252 throw new RuntimeException("Assertion failed: Utilities not initialized");
253 }
254
255 dest.drawColor(0, PorterDuff.Mode.CLEAR);
256
257 final float scale = 1.55f;
258 Bitmap scaled = Bitmap.createScaledBitmap(src, (int)(src.getWidth()*scale),
259 (int)(src.getHeight()*scale), true);
260
261 int[] xy = new int[2];
262 Bitmap mask = scaled.extractAlpha(sBlurPaint, xy);
263
264 dest.drawBitmap(mask, (destWidth - mask.getWidth()) / 2,
265 (destHeight - mask.getHeight()) / 2, sGlowColorPaint);
266 dest.drawBitmap(src, (destWidth - src.getWidth()) / 2,
267 (destHeight - src.getHeight()) / 2, sEmptyPaint);
268
269 mask.recycle();
270 }
271 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 /**
274 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
275 * The size of the thumbnail is defined by the dimension
276 * android.R.dimen.launcher_application_icon_size.
277 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 * @param bitmap The bitmap to get a thumbnail of.
279 * @param context The application's context.
280 *
281 * @return A thumbnail for the specified bitmap or the bitmap itself if the
282 * thumbnail could not be created.
283 */
284 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 synchronized (sCanvas) { // we share the statics :-(
286 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700287 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 }
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 int width = sIconWidth;
291 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 final int bitmapWidth = bitmap.getWidth();
294 final int bitmapHeight = bitmap.getHeight();
295
296 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
297 final float ratio = (float) bitmapWidth / bitmapHeight;
298
299 if (bitmapWidth > bitmapHeight) {
300 height = (int) (width / ratio);
301 } else if (bitmapHeight > bitmapWidth) {
302 width = (int) (height * ratio);
303 }
304
305 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
306 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
307 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
308 final Canvas canvas = sCanvas;
309 final Paint paint = sPaint;
310 canvas.setBitmap(thumb);
311 paint.setDither(false);
312 paint.setFilterBitmap(true);
313 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
314 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
315 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
316 return thumb;
317 }
318
319 return bitmap;
320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700322
Joe Onorato6665c0f2009-09-02 15:27:24 -0700323 private static void initStatics(Context context) {
324 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400325 final DisplayMetrics metrics = resources.getDisplayMetrics();
326 final float density = metrics.density;
327
Joe Onorato6665c0f2009-09-02 15:27:24 -0700328 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
329 sIconTextureWidth = sIconTextureHeight = roundToPow2(sIconWidth);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400330
331 sBlurPaint.setMaskFilter(new BlurMaskFilter(7 * density, BlurMaskFilter.Blur.NORMAL));
332 sGlowColorPaint.setColor(0xffff9000);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700333 }
334
Joe Onoratobf15cb42009-08-07 14:33:40 -0700335 static class BubbleText {
336 private static final int MAX_LINES = 2;
337 private TextPaint mTextPaint;
338 private Paint mRectPaint;
339
340 private float mBubblePadding;
341 private float mCornerRadius;
342 private RectF mBubbleRect = new RectF();
343
344 private float mTextWidth;
345 private int mLeading;
346 private int mFirstLineY;
347 private int mLineHeight;
348
349 private int mBitmapWidth;
350 private int mBitmapHeight;
351
352 BubbleText(Context context) {
353 final Resources resources = context.getResources();
354
355 final float scale = resources.getDisplayMetrics().density;
356
357 final float paddingLeft = 5.0f * scale;
358 final float paddingRight = 5.0f * scale;
359 final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
360 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700361 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700362
363 RectF bubbleRect = mBubbleRect;
364 bubbleRect.left = 0;
365 bubbleRect.top = 0;
366 bubbleRect.right = (int)(bubbleWidth+0.5f);
367
368 mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
369 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
370
371 Paint rectPaint = mRectPaint = new Paint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700372 rectPaint.setColor(0xff000000);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700373 rectPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700374
Joe Onorato1291a8c2009-09-15 15:07:25 -0400375 Log.d(TAG, "scale=" + scale + " textSize=" + (13*scale));
Joe Onoratoefabe002009-08-28 09:38:18 -0700376
Joe Onoratobf15cb42009-08-07 14:33:40 -0700377 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700378 textPaint.setTypeface(Typeface.DEFAULT);
379 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700380 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700381 textPaint.setAntiAlias(true);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700382 //textPaint.setShadowLayer(8, 0, 0, 0xff000000);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700383
384 float ascent = -textPaint.ascent();
385 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700386 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700387 mLeading = (int)(leading + 0.5f);
388 mFirstLineY = (int)(leading + ascent + 0.5f);
389 mLineHeight = (int)(leading + ascent + descent + 0.5f);
390
Joe Onoratobf15cb42009-08-07 14:33:40 -0700391 mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700392 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700393
Joe Onorato1291a8c2009-09-15 15:07:25 -0400394 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
Joe Onoratobf15cb42009-08-07 14:33:40 -0700395 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700396 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700397 }
398
399 /** You own the bitmap after this and you must call recycle on it. */
400 Bitmap createTextBitmap(String text) {
401 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
402 Canvas c = new Canvas(b);
403
404 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
405 Alignment.ALIGN_CENTER, 1, 0, true);
406 int lineCount = layout.getLineCount();
407 if (lineCount > MAX_LINES) {
408 lineCount = MAX_LINES;
409 }
410 if (lineCount > 0) {
411 RectF bubbleRect = mBubbleRect;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700412 bubbleRect.bottom = height(lineCount);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700413 c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700414 }
415 for (int i=0; i<lineCount; i++) {
416 int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
417 int y = mFirstLineY + (i * mLineHeight);
418 c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
419 x, y, mTextPaint);
420 }
421
422 return b;
423 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700424
425 private int height(int lineCount) {
426 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
427 }
428
429 int getBubbleWidth() {
430 return (int)(mBubbleRect.width() + 0.5f);
431 }
432
433 int getMaxBubbleHeight() {
434 return height(MAX_LINES);
435 }
436
437 int getBitmapWidth() {
438 return mBitmapWidth;
439 }
440
441 int getBitmapHeight() {
442 return mBitmapHeight;
443 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700444 }
445
446 /** Only works for positive numbers. */
447 static int roundToPow2(int n) {
448 int orig = n;
449 n >>= 1;
450 int mask = 0x8000000;
451 while (mask != 0 && (n & mask) == 0) {
452 mask >>= 1;
453 }
454 while (mask != 0) {
455 n |= mask;
456 mask >>= 1;
457 }
458 n += 1;
459 if (n != orig) {
460 n <<= 1;
461 }
462 return n;
463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464}