blob: ca12a4d2942e4f2881e9bd139353e6181a0a7f76 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Canvas;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080024import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070025import android.graphics.PaintFlagsDrawFilter;
26import android.graphics.PixelFormat;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070028import android.graphics.RectF;
29import android.graphics.Typeface;
30import android.text.Layout.Alignment;
31import android.text.StaticLayout;
32import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070033import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070034import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.res.Resources;
36import android.content.Context;
37
38/**
39 * Various utilities shared amongst the Launcher's classes.
40 */
41final class Utilities {
42 private static int sIconWidth = -1;
43 private static int sIconHeight = -1;
44
45 private static final Paint sPaint = new Paint();
46 private static final Rect sBounds = new Rect();
47 private static final Rect sOldBounds = new Rect();
48 private static Canvas sCanvas = new Canvas();
49
50 static {
51 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
52 Paint.FILTER_BITMAP_FLAG));
53 }
54
55 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
56 final int bitmapWidth = bitmap.getWidth();
57 final int bitmapHeight = bitmap.getHeight();
58
59 if (bitmapWidth < width || bitmapHeight < height) {
60 int color = context.getResources().getColor(R.color.window_background);
61
62 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
63 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070064 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065 Canvas canvas = new Canvas(centered);
66 canvas.drawColor(color);
67 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
68 null);
69
70 bitmap = centered;
71 }
72
73 return bitmap;
74 }
75
76 /**
77 * Returns a Drawable representing the thumbnail of the specified Drawable.
78 * The size of the thumbnail is defined by the dimension
79 * android.R.dimen.launcher_application_icon_size.
80 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081 * @param icon The icon to get a thumbnail of.
82 * @param context The application's context.
Joe Onorato9c1289c2009-08-17 11:03:03 -040083 * @param forceBitmap If this is true, the drawable will always be redrawn
84 * into a new bitmap if it isn't already a BitmapDrawable or a
85 * FastBitmapDrawable.
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086 *
87 * @return A thumbnail for the specified icon or the icon itself if the
88 * thumbnail could not be created.
89 */
Joe Onorato9c1289c2009-08-17 11:03:03 -040090 static Drawable createIconThumbnail(Drawable icon, Context context, boolean forceBitmap) {
91 synchronized (sCanvas) { // we share the statics :-(
92 if (sIconWidth == -1) {
93 final Resources resources = context.getResources();
94 sIconWidth = (int)resources.getDimension(android.R.dimen.app_icon_size);
95 sIconHeight = sIconWidth;
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097
Joe Onorato9c1289c2009-08-17 11:03:03 -040098 int width = sIconWidth;
99 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100
Joe Onorato9c1289c2009-08-17 11:03:03 -0400101 float scale = 1.0f;
102 if (icon instanceof PaintDrawable) {
103 PaintDrawable painter = (PaintDrawable) icon;
104 painter.setIntrinsicWidth(width);
105 painter.setIntrinsicHeight(height);
106 } else if (icon instanceof BitmapDrawable) {
107 // Ensure the bitmap has a density.
108 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
109 Bitmap bitmap = bitmapDrawable.getBitmap();
110 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
111 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400113 }
114 int iconWidth = icon.getIntrinsicWidth();
115 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 if (iconWidth > 0 && iconWidth > 0) {
118 if (width < iconWidth || height < iconHeight || scale != 1.0f) {
119 final float ratio = (float) iconWidth / iconHeight;
120
121 if (iconWidth > iconHeight) {
122 height = (int) (width / ratio);
123 } else if (iconHeight > iconWidth) {
124 width = (int) (height * ratio);
125 }
126
127 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
128 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
129 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
130 final Canvas canvas = sCanvas;
131 canvas.setBitmap(thumb);
132 // Copy the old bounds to restore them later
133 // If we were to do oldBounds = icon.getBounds(),
134 // the call to setBounds() that follows would
135 // change the same instance and we would lose the
136 // old bounds
137 sOldBounds.set(icon.getBounds());
138 final int x = (sIconWidth - width) / 2;
139 final int y = (sIconHeight - height) / 2;
140 icon.setBounds(x, y, x + width, y + height);
141 icon.draw(canvas);
142 icon.setBounds(sOldBounds);
143 icon = new FastBitmapDrawable(thumb);
144 } else if (iconWidth < width && iconHeight < height) {
145 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
146 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
147 final Canvas canvas = sCanvas;
148 canvas.setBitmap(thumb);
149 sOldBounds.set(icon.getBounds());
150 final int x = (width - iconWidth) / 2;
151 final int y = (height - iconHeight) / 2;
152 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
153 icon.draw(canvas);
154 icon.setBounds(sOldBounds);
155 icon = new FastBitmapDrawable(thumb);
156 }
157 }
158
159 if (forceBitmap) {
160 // no intrinsic size --> use default size
161 int w = sIconWidth;
162 int h = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 final Bitmap thumb = Bitmap.createBitmap(roundToPow2(w), roundToPow2(h), c);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 final Canvas canvas = sCanvas;
166 canvas.setBitmap(thumb);
167 sOldBounds.set(icon.getBounds());
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 icon.setBounds(0, 0, w, h);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 icon.draw(canvas);
170 icon.setBounds(sOldBounds);
171 icon = new FastBitmapDrawable(thumb);
172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Joe Onorato9c1289c2009-08-17 11:03:03 -0400174 return icon;
175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 }
177
178 /**
179 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
180 * The size of the thumbnail is defined by the dimension
181 * android.R.dimen.launcher_application_icon_size.
182 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 * @param bitmap The bitmap to get a thumbnail of.
184 * @param context The application's context.
185 *
186 * @return A thumbnail for the specified bitmap or the bitmap itself if the
187 * thumbnail could not be created.
188 */
189 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 synchronized (sCanvas) { // we share the statics :-(
191 if (sIconWidth == -1) {
192 final Resources resources = context.getResources();
193 sIconWidth = sIconHeight = (int) resources.getDimension(
194 android.R.dimen.app_icon_size);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 }
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 int width = sIconWidth;
198 int height = sIconHeight;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 final int bitmapWidth = bitmap.getWidth();
201 final int bitmapHeight = bitmap.getHeight();
202
203 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
204 final float ratio = (float) bitmapWidth / bitmapHeight;
205
206 if (bitmapWidth > bitmapHeight) {
207 height = (int) (width / ratio);
208 } else if (bitmapHeight > bitmapWidth) {
209 width = (int) (height * ratio);
210 }
211
212 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
213 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
214 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
215 final Canvas canvas = sCanvas;
216 final Paint paint = sPaint;
217 canvas.setBitmap(thumb);
218 paint.setDither(false);
219 paint.setFilterBitmap(true);
220 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
221 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
222 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
223 return thumb;
224 }
225
226 return bitmap;
227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700229
230 static class BubbleText {
231 private static final int MAX_LINES = 2;
232 private TextPaint mTextPaint;
233 private Paint mRectPaint;
234
235 private float mBubblePadding;
236 private float mCornerRadius;
237 private RectF mBubbleRect = new RectF();
238
239 private float mTextWidth;
240 private int mLeading;
241 private int mFirstLineY;
242 private int mLineHeight;
243
244 private int mBitmapWidth;
245 private int mBitmapHeight;
246
247 BubbleText(Context context) {
248 final Resources resources = context.getResources();
249
250 final float scale = resources.getDisplayMetrics().density;
251
252 final float paddingLeft = 5.0f * scale;
253 final float paddingRight = 5.0f * scale;
254 final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
255 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700256 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700257
258 RectF bubbleRect = mBubbleRect;
259 bubbleRect.left = 0;
260 bubbleRect.top = 0;
261 bubbleRect.right = (int)(bubbleWidth+0.5f);
262
263 mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
264 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
265
266 Paint rectPaint = mRectPaint = new Paint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700267 rectPaint.setColor(0xff000000);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700268 rectPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700269
Joe Onoratoefabe002009-08-28 09:38:18 -0700270 Log.d(Launcher.LOG_TAG, "scale=" + scale + " textSize=" + (13*scale));
271
Joe Onoratobf15cb42009-08-07 14:33:40 -0700272 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700273 textPaint.setTypeface(Typeface.DEFAULT);
274 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700275 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700276 textPaint.setAntiAlias(true);
Joe Onoratoc567acb2009-08-31 14:34:43 -0700277 //textPaint.setShadowLayer(8, 0, 0, 0xff000000);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700278
279 float ascent = -textPaint.ascent();
280 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700281 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700282 mLeading = (int)(leading + 0.5f);
283 mFirstLineY = (int)(leading + ascent + 0.5f);
284 mLineHeight = (int)(leading + ascent + descent + 0.5f);
285
Joe Onoratobf15cb42009-08-07 14:33:40 -0700286 mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700287 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700288
289 Log.d(Launcher.LOG_TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
290 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700291 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700292 }
293
294 /** You own the bitmap after this and you must call recycle on it. */
295 Bitmap createTextBitmap(String text) {
296 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
297 Canvas c = new Canvas(b);
298
299 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
300 Alignment.ALIGN_CENTER, 1, 0, true);
301 int lineCount = layout.getLineCount();
302 if (lineCount > MAX_LINES) {
303 lineCount = MAX_LINES;
304 }
305 if (lineCount > 0) {
306 RectF bubbleRect = mBubbleRect;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700307 bubbleRect.bottom = height(lineCount);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700308 c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700309 }
310 for (int i=0; i<lineCount; i++) {
311 int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
312 int y = mFirstLineY + (i * mLineHeight);
313 c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
314 x, y, mTextPaint);
315 }
316
317 return b;
318 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700319
320 private int height(int lineCount) {
321 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
322 }
323
324 int getBubbleWidth() {
325 return (int)(mBubbleRect.width() + 0.5f);
326 }
327
328 int getMaxBubbleHeight() {
329 return height(MAX_LINES);
330 }
331
332 int getBitmapWidth() {
333 return mBitmapWidth;
334 }
335
336 int getBitmapHeight() {
337 return mBitmapHeight;
338 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700339 }
340
341 /** Only works for positive numbers. */
342 static int roundToPow2(int n) {
343 int orig = n;
344 n >>= 1;
345 int mask = 0x8000000;
346 while (mask != 0 && (n & mask) == 0) {
347 mask >>= 1;
348 }
349 while (mask != 0) {
350 n |= mask;
351 mask >>= 1;
352 }
353 n += 1;
354 if (n != orig) {
355 n <<= 1;
356 }
357 return n;
358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359}