blob: 757e48e30f3716d93cd4dbfb8b2894d95ca5f47a [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;
Joe Onorato56d82912010-03-07 14:32:10 -050025import android.graphics.ColorMatrix;
26import android.graphics.ColorMatrixColorFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070028import android.graphics.PaintFlagsDrawFilter;
29import android.graphics.PixelFormat;
Joe Onorato1291a8c2009-09-15 15:07:25 -040030import android.graphics.PorterDuff;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.graphics.Rect;
Joe Onoratobf15cb42009-08-07 14:33:40 -070032import android.graphics.RectF;
Mike Reedcdd11792009-10-29 17:27:55 -040033import android.graphics.TableMaskFilter;
Joe Onoratobf15cb42009-08-07 14:33:40 -070034import android.graphics.Typeface;
35import android.text.Layout.Alignment;
36import android.text.StaticLayout;
37import android.text.TextPaint;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070038import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070039import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.res.Resources;
41import android.content.Context;
42
Romain Guyedcce092010-03-04 13:03:17 -080043import com.android.launcher.R;
44
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045/**
46 * Various utilities shared amongst the Launcher's classes.
47 */
48final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040049 private static final String TAG = "Launcher.Utilities";
50
Joe Onorato9392a752009-09-15 17:13:09 -040051 private static final boolean TEXT_BURN = false;
52
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053 private static int sIconWidth = -1;
54 private static int sIconHeight = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070055 private static int sIconTextureWidth = -1;
56 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057
58 private static final Paint sPaint = new Paint();
Joe Onorato1291a8c2009-09-15 15:07:25 -040059 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050060 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050061 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050062 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063 private static final Rect sBounds = new Rect();
64 private static final Rect sOldBounds = new Rect();
Romain Guy89911d22009-09-28 18:48:49 -070065 private static final Canvas sCanvas = new Canvas();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066
67 static {
68 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
69 Paint.FILTER_BITMAP_FLAG));
70 }
71
72 static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
73 final int bitmapWidth = bitmap.getWidth();
74 final int bitmapHeight = bitmap.getHeight();
75
76 if (bitmapWidth < width || bitmapHeight < height) {
77 int color = context.getResources().getColor(R.color.window_background);
78
79 Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
80 bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070081 centered.setDensity(bitmap.getDensity());
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082 Canvas canvas = new Canvas(centered);
83 canvas.drawColor(color);
84 canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
85 null);
86
87 bitmap = centered;
88 }
89
90 return bitmap;
91 }
92
Joe Onorato6665c0f2009-09-02 15:27:24 -070093 static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
94 static int sColorIndex = 0;
95
96 /**
97 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
98 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
99 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800100 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700101 synchronized (sCanvas) { // we share the statics :-(
102 if (sIconWidth == -1) {
103 initStatics(context);
104 }
105
106 int width = sIconWidth;
107 int height = sIconHeight;
108
Joe Onorato6665c0f2009-09-02 15:27:24 -0700109 if (icon instanceof PaintDrawable) {
110 PaintDrawable painter = (PaintDrawable) icon;
111 painter.setIntrinsicWidth(width);
112 painter.setIntrinsicHeight(height);
113 } else if (icon instanceof BitmapDrawable) {
114 // Ensure the bitmap has a density.
115 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
116 Bitmap bitmap = bitmapDrawable.getBitmap();
117 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
118 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
119 }
120 }
121 int sourceWidth = icon.getIntrinsicWidth();
122 int sourceHeight = icon.getIntrinsicHeight();
123
124 if (sourceWidth > 0 && sourceWidth > 0) {
125 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700126 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700127 // It's too big, scale it down.
128 final float ratio = (float) sourceWidth / sourceHeight;
129 if (sourceWidth > sourceHeight) {
130 height = (int) (width / ratio);
131 } else if (sourceHeight > sourceWidth) {
132 width = (int) (height * ratio);
133 }
134 } else if (sourceWidth < width && sourceHeight < height) {
135 // It's small, use the size they gave us.
136 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700137 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700138 }
139 }
140
141 // no intrinsic size --> use default size
142 int textureWidth = sIconTextureWidth;
143 int textureHeight = sIconTextureHeight;
144
145 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
146 Bitmap.Config.ARGB_8888);
147 final Canvas canvas = sCanvas;
148 canvas.setBitmap(bitmap);
149
150 final int left = (textureWidth-width) / 2;
151 final int top = (textureHeight-height) / 2;
152
153 if (false) {
154 // draw a big box for the icon for debugging
155 canvas.drawColor(sColors[sColorIndex]);
156 if (++sColorIndex >= sColors.length) sColorIndex = 0;
157 Paint debugPaint = new Paint();
158 debugPaint.setColor(0xffcccc00);
159 canvas.drawRect(left, top, left+width, top+height, debugPaint);
160 }
161
162 sOldBounds.set(icon.getBounds());
163 icon.setBounds(left, top, left+width, top+height);
164 icon.draw(canvas);
165 icon.setBounds(sOldBounds);
166
167 return bitmap;
168 }
169 }
170
Joe Onoratoc61cff92009-11-08 11:54:39 -0500171 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500172 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400173 synchronized (sCanvas) { // we share the statics :-(
174 if (sIconWidth == -1) {
175 // We can't have gotten to here without src being initialized, which
176 // comes from this file already. So just assert.
177 //initStatics(context);
178 throw new RuntimeException("Assertion failed: Utilities not initialized");
179 }
180
181 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800182
Joe Onorato1291a8c2009-09-15 15:07:25 -0400183 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400184 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400185
Mike Reedcdd11792009-10-29 17:27:55 -0400186 float px = (destWidth - src.getWidth()) / 2;
187 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500188 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500189 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400190
191 mask.recycle();
192 }
193 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 /**
196 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
197 * The size of the thumbnail is defined by the dimension
198 * android.R.dimen.launcher_application_icon_size.
199 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 * @param bitmap The bitmap to get a thumbnail of.
201 * @param context The application's context.
202 *
203 * @return A thumbnail for the specified bitmap or the bitmap itself if the
204 * thumbnail could not be created.
205 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 synchronized (sCanvas) { // we share the statics :-(
208 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700209 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 }
211
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
213 return bitmap;
214 } else {
215 return createIconBitmap(new BitmapDrawable(bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700219
Joe Onorato56d82912010-03-07 14:32:10 -0500220 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
221 synchronized (sCanvas) { // we share the statics :-(
222 if (sIconWidth == -1) {
223 initStatics(context);
224 }
225 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
226 Bitmap.Config.ARGB_8888);
227 final Canvas canvas = sCanvas;
228 canvas.setBitmap(disabled);
229
230 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
231
232 return disabled;
233 }
234 }
235
Joe Onorato6665c0f2009-09-02 15:27:24 -0700236 private static void initStatics(Context context) {
237 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400238 final DisplayMetrics metrics = resources.getDisplayMetrics();
239 final float density = metrics.density;
240
Joe Onorato6665c0f2009-09-02 15:27:24 -0700241 sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
Jason Samsb4ecab22010-01-19 16:43:26 -0800242 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400243
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500244 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500245 sGlowColorPressedPaint.setColor(0xffffc300);
246 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500247 sGlowColorFocusedPaint.setColor(0xffff8e00);
248 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato56d82912010-03-07 14:32:10 -0500249
250 ColorMatrix cm = new ColorMatrix();
251 cm.setSaturation(0.2f);
252 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
253 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700254 }
255
Joe Onoratobf15cb42009-08-07 14:33:40 -0700256 static class BubbleText {
257 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700258
Romain Guy88f38d12010-01-26 14:50:34 -0800259 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700260
Romain Guy88f38d12010-01-26 14:50:34 -0800261 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700262
Romain Guy88f38d12010-01-26 14:50:34 -0800263 private final float mTextWidth;
264 private final int mLeading;
265 private final int mFirstLineY;
266 private final int mLineHeight;
267
268 private final int mBitmapWidth;
269 private final int mBitmapHeight;
270 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700271
272 BubbleText(Context context) {
273 final Resources resources = context.getResources();
274
Romain Guy88f38d12010-01-26 14:50:34 -0800275 final DisplayMetrics metrics = resources.getDisplayMetrics();
276 final float scale = metrics.density;
277 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700278
Kenny Root97051642010-03-05 13:24:08 -0800279 final float paddingLeft = 2.0f * scale;
280 final float paddingRight = 2.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800281 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700282
283 RectF bubbleRect = mBubbleRect;
284 bubbleRect.left = 0;
285 bubbleRect.top = 0;
Romain Guyc0c4fe32010-02-12 23:20:25 +0100286 bubbleRect.right = (int) cellWidth;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700287
Kenny Root97051642010-03-05 13:24:08 -0800288 mTextWidth = cellWidth - paddingLeft - paddingRight;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700289
Joe Onoratobf15cb42009-08-07 14:33:40 -0700290 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700291 textPaint.setTypeface(Typeface.DEFAULT);
292 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700293 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700294 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400295 if (TEXT_BURN) {
296 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
297 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700298
299 float ascent = -textPaint.ascent();
300 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700301 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700302 mLeading = (int)(leading + 0.5f);
303 mFirstLineY = (int)(leading + ascent + 0.5f);
304 mLineHeight = (int)(leading + ascent + descent + 0.5f);
305
Romain Guy442eda22010-02-04 15:59:37 -0800306 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700307 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700308
Joe Onoratofccc8df2009-10-01 14:30:16 -0700309 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
310
Joe Onorato080d9b62009-11-02 12:01:11 -0500311 if (false) {
312 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
313 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
314 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
315 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700316 }
317
318 /** You own the bitmap after this and you must call recycle on it. */
319 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800320 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800321 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700322 Canvas c = new Canvas(b);
323
324 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
325 Alignment.ALIGN_CENTER, 1, 0, true);
326 int lineCount = layout.getLineCount();
327 if (lineCount > MAX_LINES) {
328 lineCount = MAX_LINES;
329 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700330 //if (!TEXT_BURN && lineCount > 0) {
331 //RectF bubbleRect = mBubbleRect;
332 //bubbleRect.bottom = height(lineCount);
333 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
334 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700335 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700336 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
337 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800338 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700339 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800340 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700341 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800342 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700343 }
344
345 return b;
346 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700347
348 private int height(int lineCount) {
349 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
350 }
351
352 int getBubbleWidth() {
353 return (int)(mBubbleRect.width() + 0.5f);
354 }
355
356 int getMaxBubbleHeight() {
357 return height(MAX_LINES);
358 }
359
360 int getBitmapWidth() {
361 return mBitmapWidth;
362 }
363
364 int getBitmapHeight() {
365 return mBitmapHeight;
366 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700367 }
368
369 /** Only works for positive numbers. */
370 static int roundToPow2(int n) {
371 int orig = n;
372 n >>= 1;
373 int mask = 0x8000000;
374 while (mask != 0 && (n & mask) == 0) {
375 mask >>= 1;
376 }
377 while (mask != 0) {
378 n |= mask;
379 mask >>= 1;
380 }
381 n += 1;
382 if (n != orig) {
383 n <<= 1;
384 }
385 return n;
386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387}