blob: 60f71f575d6011ccb4b0bb0623875de5f2a956df [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
Winson Chungaafa03c2010-06-11 17:34:16 -070019import android.content.Context;
20import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080021import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040022import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Canvas;
Joe Onorato56d82912010-03-07 14:32:10 -050024import android.graphics.ColorMatrix;
25import android.graphics.ColorMatrixColorFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080026import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070027import android.graphics.PaintFlagsDrawFilter;
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;
Winson Chungaafa03c2010-06-11 17:34:16 -070033import android.graphics.drawable.BitmapDrawable;
34import android.graphics.drawable.Drawable;
35import android.graphics.drawable.PaintDrawable;
Joe Onoratobf15cb42009-08-07 14:33:40 -070036import android.text.StaticLayout;
37import android.text.TextPaint;
Winson Chungaafa03c2010-06-11 17:34:16 -070038import android.text.Layout.Alignment;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070039import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070040import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041
Romain Guyedcce092010-03-04 13:03:17 -080042import com.android.launcher.R;
43
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044/**
45 * Various utilities shared amongst the Launcher's classes.
46 */
47final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040048 private static final String TAG = "Launcher.Utilities";
49
Joe Onorato9392a752009-09-15 17:13:09 -040050 private static final boolean TEXT_BURN = false;
51
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052 private static int sIconWidth = -1;
53 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070054 private static int sIconTextureWidth = -1;
55 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056
Joe Onorato1291a8c2009-09-15 15:07:25 -040057 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050058 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050059 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050060 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070062 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063
64 static {
65 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
66 Paint.FILTER_BITMAP_FLAG));
67 }
68
69 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
70 final int bitmapWidth = bitmap.getWidth();
71 final int bitmapHeight = bitmap.getHeight();
72
73 if (bitmapWidth < width || bitmapHeight < height) {
74 int color = context.getResources().getColor(R.color.window_background);
75
76 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
77 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070078 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079 Canvas canvas = new Canvas(centered);
80 canvas.drawColor(color);
81 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
82 null);
83
84 bitmap = centered;
85 }
86
87 return bitmap;
88 }
89
Joe Onorato6665c0f2009-09-02 15:27:24 -070090 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
91 static int sColorIndex = 0;
92
93 /**
94 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
95 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
96 */
Joe Onorato0589f0f2010-02-08 13:44:00 -080097 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -070098 synchronized (sCanvas) { // we share the statics :-(
99 if (sIconWidth == -1) {
100 initStatics(context);
101 }
102
103 int width = sIconWidth;
104 int height = sIconHeight;
105
Joe Onorato6665c0f2009-09-02 15:27:24 -0700106 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());
116 }
117 }
118 int sourceWidth = icon.getIntrinsicWidth();
119 int sourceHeight = icon.getIntrinsicHeight();
120
121 if (sourceWidth > 0 && sourceWidth > 0) {
122 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700123 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700124 // It's too big, scale it down.
125 final float ratio = (float) sourceWidth / sourceHeight;
126 if (sourceWidth > sourceHeight) {
127 height = (int) (width / ratio);
128 } else if (sourceHeight > sourceWidth) {
129 width = (int) (height * ratio);
130 }
131 } else if (sourceWidth < width && sourceHeight < height) {
132 // It's small, use the size they gave us.
133 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700134 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700135 }
136 }
137
138 // no intrinsic size --> use default size
139 int textureWidth = sIconTextureWidth;
140 int textureHeight = sIconTextureHeight;
141
142 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
143 Bitmap.Config.ARGB_8888);
144 final Canvas canvas = sCanvas;
145 canvas.setBitmap(bitmap);
146
147 final int left = (textureWidth-width) / 2;
148 final int top = (textureHeight-height) / 2;
149
150 if (false) {
151 // draw a big box for the icon for debugging
152 canvas.drawColor(sColors[sColorIndex]);
153 if (++sColorIndex >= sColors.length) sColorIndex = 0;
154 Paint debugPaint = new Paint();
155 debugPaint.setColor(0xffcccc00);
156 canvas.drawRect(left, top, left+width, top+height, debugPaint);
157 }
158
159 sOldBounds.set(icon.getBounds());
160 icon.setBounds(left, top, left+width, top+height);
161 icon.draw(canvas);
162 icon.setBounds(sOldBounds);
163
164 return bitmap;
165 }
166 }
167
Joe Onoratoc61cff92009-11-08 11:54:39 -0500168 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500169 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400170 synchronized (sCanvas) { // we share the statics :-(
171 if (sIconWidth == -1) {
172 // We can't have gotten to here without src being initialized, which
173 // comes from this file already. So just assert.
174 //initStatics(context);
175 throw new RuntimeException("Assertion failed: Utilities not initialized");
176 }
177
178 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800179
Joe Onorato1291a8c2009-09-15 15:07:25 -0400180 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400181 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400182
Mike Reedcdd11792009-10-29 17:27:55 -0400183 float px = (destWidth - src.getWidth()) / 2;
184 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500185 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500186 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400187
188 mask.recycle();
189 }
190 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 /**
193 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
194 * The size of the thumbnail is defined by the dimension
195 * android.R.dimen.launcher_application_icon_size.
196 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 * @param bitmap The bitmap to get a thumbnail of.
198 * @param context The application's context.
199 *
200 * @return A thumbnail for the specified bitmap or the bitmap itself if the
201 * thumbnail could not be created.
202 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800203 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 synchronized (sCanvas) { // we share the statics :-(
205 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700206 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 }
208
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
210 return bitmap;
211 } else {
212 return createIconBitmap(new BitmapDrawable(bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700216
Joe Onorato56d82912010-03-07 14:32:10 -0500217 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
218 synchronized (sCanvas) { // we share the statics :-(
219 if (sIconWidth == -1) {
220 initStatics(context);
221 }
222 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
223 Bitmap.Config.ARGB_8888);
224 final Canvas canvas = sCanvas;
225 canvas.setBitmap(disabled);
226
227 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
228
229 return disabled;
230 }
231 }
232
Joe Onorato6665c0f2009-09-02 15:27:24 -0700233 private static void initStatics(Context context) {
234 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400235 final DisplayMetrics metrics = resources.getDisplayMetrics();
236 final float density = metrics.density;
237
Michael Jurkac9a96192010-11-01 11:52:08 -0700238 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Jason Samsb4ecab22010-01-19 16:43:26 -0800239 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400240
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500241 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500242 sGlowColorPressedPaint.setColor(0xffffc300);
243 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500244 sGlowColorFocusedPaint.setColor(0xffff8e00);
245 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato56d82912010-03-07 14:32:10 -0500246
247 ColorMatrix cm = new ColorMatrix();
248 cm.setSaturation(0.2f);
249 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
250 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700251 }
252
Joe Onoratobf15cb42009-08-07 14:33:40 -0700253 static class BubbleText {
254 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700255
Romain Guy88f38d12010-01-26 14:50:34 -0800256 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700257
Romain Guy88f38d12010-01-26 14:50:34 -0800258 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700259
Romain Guy88f38d12010-01-26 14:50:34 -0800260 private final float mTextWidth;
261 private final int mLeading;
262 private final int mFirstLineY;
263 private final int mLineHeight;
264
265 private final int mBitmapWidth;
266 private final int mBitmapHeight;
267 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700268
269 BubbleText(Context context) {
270 final Resources resources = context.getResources();
271
Romain Guy88f38d12010-01-26 14:50:34 -0800272 final DisplayMetrics metrics = resources.getDisplayMetrics();
273 final float scale = metrics.density;
274 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700275
Kenny Root97051642010-03-05 13:24:08 -0800276 final float paddingLeft = 2.0f * scale;
277 final float paddingRight = 2.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800278 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700279
280 RectF bubbleRect = mBubbleRect;
281 bubbleRect.left = 0;
282 bubbleRect.top = 0;
Romain Guyc0c4fe32010-02-12 23:20:25 +0100283 bubbleRect.right = (int) cellWidth;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700284
Kenny Root97051642010-03-05 13:24:08 -0800285 mTextWidth = cellWidth - paddingLeft - paddingRight;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700286
Joe Onoratobf15cb42009-08-07 14:33:40 -0700287 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700288 textPaint.setTypeface(Typeface.DEFAULT);
289 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700290 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700291 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400292 if (TEXT_BURN) {
293 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
294 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700295
296 float ascent = -textPaint.ascent();
297 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700298 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700299 mLeading = (int)(leading + 0.5f);
300 mFirstLineY = (int)(leading + ascent + 0.5f);
301 mLineHeight = (int)(leading + ascent + descent + 0.5f);
302
Romain Guy442eda22010-02-04 15:59:37 -0800303 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700304 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700305
Joe Onoratofccc8df2009-10-01 14:30:16 -0700306 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
307
Joe Onorato080d9b62009-11-02 12:01:11 -0500308 if (false) {
309 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
310 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
311 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
312 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700313 }
314
315 /** You own the bitmap after this and you must call recycle on it. */
316 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800317 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800318 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700319 Canvas c = new Canvas(b);
320
321 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
322 Alignment.ALIGN_CENTER, 1, 0, true);
323 int lineCount = layout.getLineCount();
324 if (lineCount > MAX_LINES) {
325 lineCount = MAX_LINES;
326 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700327 //if (!TEXT_BURN && lineCount > 0) {
328 //RectF bubbleRect = mBubbleRect;
329 //bubbleRect.bottom = height(lineCount);
330 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
331 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700332 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700333 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
334 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800335 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700336 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800337 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700338 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800339 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700340 }
341
342 return b;
343 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700344
345 private int height(int lineCount) {
346 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
347 }
348
349 int getBubbleWidth() {
350 return (int)(mBubbleRect.width() + 0.5f);
351 }
352
353 int getMaxBubbleHeight() {
354 return height(MAX_LINES);
355 }
356
357 int getBitmapWidth() {
358 return mBitmapWidth;
359 }
360
361 int getBitmapHeight() {
362 return mBitmapHeight;
363 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700364 }
365
366 /** Only works for positive numbers. */
367 static int roundToPow2(int n) {
368 int orig = n;
369 n >>= 1;
370 int mask = 0x8000000;
371 while (mask != 0 && (n & mask) == 0) {
372 mask >>= 1;
373 }
374 while (mask != 0) {
375 n |= mask;
376 mask >>= 1;
377 }
378 n += 1;
379 if (n != orig) {
380 n <<= 1;
381 }
382 return n;
383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384}