blob: 9e715d1b71aa3f720b7ef5efd7b4f4c204a2a5b0 [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 *
81 * This method is not thread-safe and should be invoked on the UI thread only.
82 *
83 * @param icon The icon to get a thumbnail of.
84 * @param context The application's context.
85 *
86 * @return A thumbnail for the specified icon or the icon itself if the
87 * thumbnail could not be created.
88 */
89 static Drawable createIconThumbnail(Drawable icon, Context context) {
90 if (sIconWidth == -1) {
91 final Resources resources = context.getResources();
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070092 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093 }
94
95 int width = sIconWidth;
96 int height = sIconHeight;
97
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -070098 float scale = 1.0f;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099 if (icon instanceof PaintDrawable) {
100 PaintDrawable painter = (PaintDrawable) icon;
101 painter.setIntrinsicWidth(width);
102 painter.setIntrinsicHeight(height);
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700103 } else if (icon instanceof BitmapDrawable) {
Dianne Hackborn32ce7f12009-07-22 21:56:50 -0700104 // Ensure the bitmap has a density.
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700105 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
106 Bitmap bitmap = bitmapDrawable.getBitmap();
Dianne Hackborn32ce7f12009-07-22 21:56:50 -0700107 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
108 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 }
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700111 int iconWidth = icon.getIntrinsicWidth();
112 int iconHeight = icon.getIntrinsicHeight();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 if (width > 0 && height > 0) {
Mitsuru Oshima583ed3b2009-05-12 19:19:10 -0700115 if (width < iconWidth || height < iconHeight || scale != 1.0f) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 final float ratio = (float) iconWidth / iconHeight;
117
118 if (iconWidth > iconHeight) {
119 height = (int) (width / ratio);
120 } else if (iconHeight > iconWidth) {
121 width = (int) (height * ratio);
122 }
123
124 final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
125 Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
126 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
127 final Canvas canvas = sCanvas;
128 canvas.setBitmap(thumb);
129 // Copy the old bounds to restore them later
130 // If we were to do oldBounds = icon.getBounds(),
131 // the call to setBounds() that follows would
132 // change the same instance and we would lose the
133 // old bounds
134 sOldBounds.set(icon.getBounds());
135 final int x = (sIconWidth - width) / 2;
136 final int y = (sIconHeight - height) / 2;
137 icon.setBounds(x, y, x + width, y + height);
138 icon.draw(canvas);
139 icon.setBounds(sOldBounds);
140 icon = new FastBitmapDrawable(thumb);
141 } else if (iconWidth < width && iconHeight < height) {
142 final Bitmap.Config c = Bitmap.Config.ARGB_8888;
143 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
144 final Canvas canvas = sCanvas;
145 canvas.setBitmap(thumb);
146 sOldBounds.set(icon.getBounds());
147 final int x = (width - iconWidth) / 2;
148 final int y = (height - iconHeight) / 2;
149 icon.setBounds(x, y, x + iconWidth, y + iconHeight);
150 icon.draw(canvas);
151 icon.setBounds(sOldBounds);
152 icon = new FastBitmapDrawable(thumb);
153 }
154 }
155
156 return icon;
157 }
158
159 /**
160 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
161 * The size of the thumbnail is defined by the dimension
162 * android.R.dimen.launcher_application_icon_size.
163 *
164 * This method is not thread-safe and should be invoked on the UI thread only.
165 *
166 * @param bitmap The bitmap to get a thumbnail of.
167 * @param context The application's context.
168 *
169 * @return A thumbnail for the specified bitmap or the bitmap itself if the
170 * thumbnail could not be created.
171 */
172 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
173 if (sIconWidth == -1) {
174 final Resources resources = context.getResources();
175 sIconWidth = sIconHeight = (int) resources.getDimension(
176 android.R.dimen.app_icon_size);
177 }
178
179 int width = sIconWidth;
180 int height = sIconHeight;
181
182 final int bitmapWidth = bitmap.getWidth();
183 final int bitmapHeight = bitmap.getHeight();
184
185 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
186 final float ratio = (float) bitmapWidth / bitmapHeight;
187
188 if (bitmapWidth > bitmapHeight) {
189 height = (int) (width / ratio);
190 } else if (bitmapHeight > bitmapWidth) {
191 width = (int) (height * ratio);
192 }
193
194 final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
195 bitmap.getConfig() : Bitmap.Config.ARGB_8888;
196 final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
197 final Canvas canvas = sCanvas;
198 final Paint paint = sPaint;
199 canvas.setBitmap(thumb);
200 paint.setDither(false);
201 paint.setFilterBitmap(true);
202 sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
203 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
204 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
205 return thumb;
206 }
207
208 return bitmap;
209 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700210
211 static class BubbleText {
212 private static final int MAX_LINES = 2;
213 private TextPaint mTextPaint;
214 private Paint mRectPaint;
215
216 private float mBubblePadding;
217 private float mCornerRadius;
218 private RectF mBubbleRect = new RectF();
219
220 private float mTextWidth;
221 private int mLeading;
222 private int mFirstLineY;
223 private int mLineHeight;
224
225 private int mBitmapWidth;
226 private int mBitmapHeight;
227
228 BubbleText(Context context) {
229 final Resources resources = context.getResources();
230
231 final float scale = resources.getDisplayMetrics().density;
232
233 final float paddingLeft = 5.0f * scale;
234 final float paddingRight = 5.0f * scale;
235 final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
236 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700237 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700238
239 RectF bubbleRect = mBubbleRect;
240 bubbleRect.left = 0;
241 bubbleRect.top = 0;
242 bubbleRect.right = (int)(bubbleWidth+0.5f);
243
244 mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
245 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
246
247 Paint rectPaint = mRectPaint = new Paint();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700248 rectPaint.setColor(0xaa000000);
249 rectPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700250
251 TextPaint textPaint = mTextPaint = new TextPaint();
252 textPaint.setTypeface(Typeface.DEFAULT_BOLD);
253 textPaint.setTextSize(20);
254 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700255 textPaint.setAntiAlias(true);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700256
257 float ascent = -textPaint.ascent();
258 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700259 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700260 mLeading = (int)(leading + 0.5f);
261 mFirstLineY = (int)(leading + ascent + 0.5f);
262 mLineHeight = (int)(leading + ascent + descent + 0.5f);
263
264 roundToPow2(64);
265 mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700266 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700267
268 Log.d(Launcher.LOG_TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
269 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700270 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700271 }
272
273 /** You own the bitmap after this and you must call recycle on it. */
274 Bitmap createTextBitmap(String text) {
275 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
276 Canvas c = new Canvas(b);
277
278 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
279 Alignment.ALIGN_CENTER, 1, 0, true);
280 int lineCount = layout.getLineCount();
281 if (lineCount > MAX_LINES) {
282 lineCount = MAX_LINES;
283 }
284 if (lineCount > 0) {
285 RectF bubbleRect = mBubbleRect;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700286 bubbleRect.bottom = height(lineCount);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700287 c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700288 }
289 for (int i=0; i<lineCount; i++) {
290 int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
291 int y = mFirstLineY + (i * mLineHeight);
292 c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
293 x, y, mTextPaint);
294 }
295
296 return b;
297 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700298
299 private int height(int lineCount) {
300 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
301 }
302
303 int getBubbleWidth() {
304 return (int)(mBubbleRect.width() + 0.5f);
305 }
306
307 int getMaxBubbleHeight() {
308 return height(MAX_LINES);
309 }
310
311 int getBitmapWidth() {
312 return mBitmapWidth;
313 }
314
315 int getBitmapHeight() {
316 return mBitmapHeight;
317 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700318 }
319
320 /** Only works for positive numbers. */
321 static int roundToPow2(int n) {
322 int orig = n;
323 n >>= 1;
324 int mask = 0x8000000;
325 while (mask != 0 && (n & mask) == 0) {
326 mask >>= 1;
327 }
328 while (mask != 0) {
329 n |= mask;
330 mask >>= 1;
331 }
332 n += 1;
333 if (n != orig) {
334 n <<= 1;
335 }
336 return n;
337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338}