blob: fbe489ec5be7aaa9efc769db8fd22c7112579c3f [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;
Mike Reedcdd11792009-10-29 17:27:55 -040031import android.graphics.TableMaskFilter;
Joe Onoratobf15cb42009-08-07 14:33:40 -070032import android.graphics.Typeface;
33import android.text.Layout.Alignment;
34import android.text.StaticLayout;
35import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070036import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070037import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080038import android.content.res.Resources;
39import android.content.Context;
40
41/**
42 * Various utilities shared amongst the Launcher's classes.
43 */
44final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040045 private static final String TAG = "Launcher.Utilities";
46
Joe Onorato9392a752009-09-15 17:13:09 -040047 private static final boolean TEXT_BURN = false;
48
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049 private static int sIconWidth = -1;
50 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070051 private static int sIconTextureWidth = -1;
52 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053
54 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040055 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050056 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050057 private static final Paint sGlowColorFocusedPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058 private static final Rect sBounds = new Rect();
59 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070060 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061
62 static {
63 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
64 Paint.FILTER_BITMAP_FLAG));
65 }
66
67 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
68 final int bitmapWidth = bitmap.getWidth();
69 final int bitmapHeight = bitmap.getHeight();
70
71 if (bitmapWidth < width || bitmapHeight < height) {
72 int color = context.getResources().getColor(R.color.window_background);
73
74 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
75 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070076 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077 Canvas canvas = new Canvas(centered);
78 canvas.drawColor(color);
79 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
80 null);
81
82 bitmap = centered;
83 }
84
85 return bitmap;
86 }
87
Joe Onorato6665c0f2009-09-02 15:27:24 -070088 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
89 static int sColorIndex = 0;
90
91 /**
92 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
93 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
94 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080095 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070096 synchronized (sCanvas) { // we share the statics :-(
97 if (sIconWidth == -1) {
98 initStatics(context);
99 }
100
101 int width = sIconWidth;
102 int height = sIconHeight;
103
Joe Onorato6665c0f2009-09-02 15:27:24 -0700104 if (icon instanceof PaintDrawable) {
105 PaintDrawable painter = (PaintDrawable) icon;
106 painter.setIntrinsicWidth(width);
107 painter.setIntrinsicHeight(height);
108 } else if (icon instanceof BitmapDrawable) {
109 // Ensure the bitmap has a density.
110 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
111 Bitmap bitmap = bitmapDrawable.getBitmap();
112 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
113 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
114 }
115 }
116 int sourceWidth = icon.getIntrinsicWidth();
117 int sourceHeight = icon.getIntrinsicHeight();
118
119 if (sourceWidth > 0 && sourceWidth > 0) {
120 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700121 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700122 // It's too big, scale it down.
123 final float ratio = (float) sourceWidth / sourceHeight;
124 if (sourceWidth > sourceHeight) {
125 height = (int) (width / ratio);
126 } else if (sourceHeight > sourceWidth) {
127 width = (int) (height * ratio);
128 }
129 } else if (sourceWidth < width && sourceHeight < height) {
130 // It's small, use the size they gave us.
131 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700132 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700133 }
134 }
135
136 // no intrinsic size --> use default size
137 int textureWidth = sIconTextureWidth;
138 int textureHeight = sIconTextureHeight;
139
140 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
141 Bitmap.Config.ARGB_8888);
142 final Canvas canvas = sCanvas;
143 canvas.setBitmap(bitmap);
144
145 final int left = (textureWidth-width) / 2;
146 final int top = (textureHeight-height) / 2;
147
148 if (false) {
149 // draw a big box for the icon for debugging
150 canvas.drawColor(sColors[sColorIndex]);
151 if (++sColorIndex >= sColors.length) sColorIndex = 0;
152 Paint debugPaint = new Paint();
153 debugPaint.setColor(0xffcccc00);
154 canvas.drawRect(left, top, left+width, top+height, debugPaint);
155 }
156
157 sOldBounds.set(icon.getBounds());
158 icon.setBounds(left, top, left+width, top+height);
159 icon.draw(canvas);
160 icon.setBounds(sOldBounds);
161
162 return bitmap;
163 }
164 }
165
Joe Onoratoc61cff92009-11-08 11:54:39 -0500166 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500167 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400168 synchronized (sCanvas) { // we share the statics :-(
169 if (sIconWidth == -1) {
170 // We can't have gotten to here without src being initialized, which
171 // comes from this file already. So just assert.
172 //initStatics(context);
173 throw new RuntimeException("Assertion failed: Utilities not initialized");
174 }
175
176 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800177
Joe Onorato1291a8c2009-09-15 15:07:25 -0400178 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400179 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400180
Mike Reedcdd11792009-10-29 17:27:55 -0400181 float px = (destWidth - src.getWidth()) / 2;
182 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500183 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500184 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400185
186 mask.recycle();
187 }
188 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 /**
191 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
192 * The size of the thumbnail is defined by the dimension
193 * android.R.dimen.launcher_application_icon_size.
194 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 * @param bitmap The bitmap to get a thumbnail of.
196 * @param context The application's context.
197 *
198 * @return A thumbnail for the specified bitmap or the bitmap itself if the
199 * thumbnail could not be created.
200 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800201 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 synchronized (sCanvas) { // we share the statics :-(
203 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700204 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 }
206
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
208 return bitmap;
209 } else {
210 return createIconBitmap(new BitmapDrawable(bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700214
Joe Onorato6665c0f2009-09-02 15:27:24 -0700215 private static void initStatics(Context context) {
216 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400217 final DisplayMetrics metrics = resources.getDisplayMetrics();
218 final float density = metrics.density;
219
Joe Onorato6665c0f2009-09-02 15:27:24 -0700220 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
Jason Samsb4ecab22010-01-19 16:43:26 -0800221 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400222
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500223 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500224 sGlowColorPressedPaint.setColor(0xffffc300);
225 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500226 sGlowColorFocusedPaint.setColor(0xffff8e00);
227 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato6665c0f2009-09-02 15:27:24 -0700228 }
229
Joe Onoratobf15cb42009-08-07 14:33:40 -0700230 static class BubbleText {
231 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700232
Romain Guy88f38d12010-01-26 14:50:34 -0800233 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700234
Romain Guy88f38d12010-01-26 14:50:34 -0800235 private final float mBubblePadding;
236 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700237
Romain Guy88f38d12010-01-26 14:50:34 -0800238 private final float mTextWidth;
239 private final int mLeading;
240 private final int mFirstLineY;
241 private final int mLineHeight;
242
243 private final int mBitmapWidth;
244 private final int mBitmapHeight;
245 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700246
247 BubbleText(Context context) {
248 final Resources resources = context.getResources();
249
Romain Guy88f38d12010-01-26 14:50:34 -0800250 final DisplayMetrics metrics = resources.getDisplayMetrics();
251 final float scale = metrics.density;
252 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700253
254 final float paddingLeft = 5.0f * scale;
255 final float paddingRight = 5.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800256 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700257 final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700258 mBubblePadding = 3.0f * scale;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700259
260 RectF bubbleRect = mBubbleRect;
261 bubbleRect.left = 0;
262 bubbleRect.top = 0;
Romain Guyc0c4fe32010-02-12 23:20:25 +0100263 bubbleRect.right = (int) cellWidth;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700264
Joe Onoratobf15cb42009-08-07 14:33:40 -0700265 mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
266
Joe Onoratobf15cb42009-08-07 14:33:40 -0700267 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700268 textPaint.setTypeface(Typeface.DEFAULT);
269 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700270 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700271 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400272 if (TEXT_BURN) {
273 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
274 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700275
276 float ascent = -textPaint.ascent();
277 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700278 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700279 mLeading = (int)(leading + 0.5f);
280 mFirstLineY = (int)(leading + ascent + 0.5f);
281 mLineHeight = (int)(leading + ascent + descent + 0.5f);
282
Romain Guy442eda22010-02-04 15:59:37 -0800283 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700284 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700285
Joe Onoratofccc8df2009-10-01 14:30:16 -0700286 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
287
Joe Onorato080d9b62009-11-02 12:01:11 -0500288 if (false) {
289 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
290 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
291 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
292 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700293 }
294
295 /** You own the bitmap after this and you must call recycle on it. */
296 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800297 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800298 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700299 Canvas c = new Canvas(b);
300
301 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
302 Alignment.ALIGN_CENTER, 1, 0, true);
303 int lineCount = layout.getLineCount();
304 if (lineCount > MAX_LINES) {
305 lineCount = MAX_LINES;
306 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700307 //if (!TEXT_BURN && lineCount > 0) {
308 //RectF bubbleRect = mBubbleRect;
309 //bubbleRect.bottom = height(lineCount);
310 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
311 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700312 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700313 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
314 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800315 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700316 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800317 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700318 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800319 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700320 }
321
322 return b;
323 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700324
325 private int height(int lineCount) {
326 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
327 }
328
329 int getBubbleWidth() {
330 return (int)(mBubbleRect.width() + 0.5f);
331 }
332
333 int getMaxBubbleHeight() {
334 return height(MAX_LINES);
335 }
336
337 int getBitmapWidth() {
338 return mBitmapWidth;
339 }
340
341 int getBitmapHeight() {
342 return mBitmapHeight;
343 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700344 }
345
346 /** Only works for positive numbers. */
347 static int roundToPow2(int n) {
348 int orig = n;
349 n >>= 1;
350 int mask = 0x8000000;
351 while (mask != 0 && (n & mask) == 0) {
352 mask >>= 1;
353 }
354 while (mask != 0) {
355 n |= mask;
356 mask >>= 1;
357 }
358 n += 1;
359 if (n != orig) {
360 n <<= 1;
361 }
362 return n;
363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364}