blob: cd98cab7f7519266b2c29b0ac237a663da879fd4 [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 Chung97d85d22011-04-13 11:27:36 -070019import java.util.Random;
20
Winson Chungaafa03c2010-06-11 17:34:16 -070021import android.content.Context;
22import android.content.res.Resources;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080023import android.graphics.Bitmap;
Joe Onorato1291a8c2009-09-15 15:07:25 -040024import android.graphics.BlurMaskFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080025import android.graphics.Canvas;
Joe Onorato56d82912010-03-07 14:32:10 -050026import android.graphics.ColorMatrix;
27import android.graphics.ColorMatrixColorFilter;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.graphics.Paint;
Joe Onoratobf15cb42009-08-07 14:33:40 -070029import android.graphics.PaintFlagsDrawFilter;
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;
Winson Chungaafa03c2010-06-11 17:34:16 -070035import android.graphics.drawable.BitmapDrawable;
36import android.graphics.drawable.Drawable;
37import android.graphics.drawable.PaintDrawable;
Joe Onoratobf15cb42009-08-07 14:33:40 -070038import android.text.StaticLayout;
39import android.text.TextPaint;
Winson Chungaafa03c2010-06-11 17:34:16 -070040import android.text.Layout.Alignment;
Dianne Hackborn32ce7f12009-07-22 21:56:50 -070041import android.util.DisplayMetrics;
Joe Onoratobf15cb42009-08-07 14:33:40 -070042import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043
Romain Guyedcce092010-03-04 13:03:17 -080044import com.android.launcher.R;
45
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046/**
47 * Various utilities shared amongst the Launcher's classes.
48 */
49final class Utilities {
Joe Onorato1291a8c2009-09-15 15:07:25 -040050 private static final String TAG = "Launcher.Utilities";
51
Joe Onorato9392a752009-09-15 17:13:09 -040052 private static final boolean TEXT_BURN = false;
53
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054 private static int sIconWidth = -1;
55 private static int sIconHeight = -1;
Michael Jurka7ef959b2011-02-23 11:48:32 -080056 private static int sIconContentSize = -1;
Joe Onorato6665c0f2009-09-02 15:27:24 -070057 private static int sIconTextureWidth = -1;
58 private static int sIconTextureHeight = -1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059
Joe Onorato1291a8c2009-09-15 15:07:25 -040060 private static final Paint sBlurPaint = new Paint();
Joe Onoratoeb8325a2009-11-08 13:20:30 -050061 private static final Paint sGlowColorPressedPaint = new Paint();
Joe Onoratoc61cff92009-11-08 11:54:39 -050062 private static final Paint sGlowColorFocusedPaint = new Paint();
Joe Onorato56d82912010-03-07 14:32:10 -050063 private static final Paint sDisabledPaint = new Paint();
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064 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
Michael Jurka7ef959b2011-02-23 11:48:32 -080096 static int getIconContentSize() {
97 return sIconContentSize;
98 }
99
Joe Onorato6665c0f2009-09-02 15:27:24 -0700100 /**
101 * Returns a bitmap suitable for the all apps view. The bitmap will be a power
102 * of two sized ARGB_8888 bitmap that can be used as a gl texture.
103 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800104 static Bitmap createIconBitmap(Drawable icon, Context context) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700105 synchronized (sCanvas) { // we share the statics :-(
106 if (sIconWidth == -1) {
107 initStatics(context);
108 }
109
110 int width = sIconWidth;
111 int height = sIconHeight;
112
Joe Onorato6665c0f2009-09-02 15:27:24 -0700113 if (icon instanceof PaintDrawable) {
114 PaintDrawable painter = (PaintDrawable) icon;
115 painter.setIntrinsicWidth(width);
116 painter.setIntrinsicHeight(height);
117 } else if (icon instanceof BitmapDrawable) {
118 // Ensure the bitmap has a density.
119 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
120 Bitmap bitmap = bitmapDrawable.getBitmap();
121 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
122 bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
123 }
124 }
125 int sourceWidth = icon.getIntrinsicWidth();
126 int sourceHeight = icon.getIntrinsicHeight();
127
128 if (sourceWidth > 0 && sourceWidth > 0) {
129 // There are intrinsic sizes.
Romain Guy89911d22009-09-28 18:48:49 -0700130 if (width < sourceWidth || height < sourceHeight) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700131 // It's too big, scale it down.
132 final float ratio = (float) sourceWidth / sourceHeight;
133 if (sourceWidth > sourceHeight) {
134 height = (int) (width / ratio);
135 } else if (sourceHeight > sourceWidth) {
136 width = (int) (height * ratio);
137 }
138 } else if (sourceWidth < width && sourceHeight < height) {
139 // It's small, use the size they gave us.
140 width = sourceWidth;
Romain Guy89911d22009-09-28 18:48:49 -0700141 height = sourceHeight;
Joe Onorato6665c0f2009-09-02 15:27:24 -0700142 }
143 }
144
145 // no intrinsic size --> use default size
146 int textureWidth = sIconTextureWidth;
147 int textureHeight = sIconTextureHeight;
148
149 final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
150 Bitmap.Config.ARGB_8888);
151 final Canvas canvas = sCanvas;
152 canvas.setBitmap(bitmap);
153
154 final int left = (textureWidth-width) / 2;
155 final int top = (textureHeight-height) / 2;
156
157 if (false) {
158 // draw a big box for the icon for debugging
159 canvas.drawColor(sColors[sColorIndex]);
160 if (++sColorIndex >= sColors.length) sColorIndex = 0;
161 Paint debugPaint = new Paint();
162 debugPaint.setColor(0xffcccc00);
163 canvas.drawRect(left, top, left+width, top+height, debugPaint);
164 }
165
166 sOldBounds.set(icon.getBounds());
167 icon.setBounds(left, top, left+width, top+height);
168 icon.draw(canvas);
169 icon.setBounds(sOldBounds);
170
171 return bitmap;
172 }
173 }
174
Joe Onoratoc61cff92009-11-08 11:54:39 -0500175 static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500176 boolean pressed, Bitmap src) {
Joe Onorato1291a8c2009-09-15 15:07:25 -0400177 synchronized (sCanvas) { // we share the statics :-(
178 if (sIconWidth == -1) {
179 // We can't have gotten to here without src being initialized, which
180 // comes from this file already. So just assert.
181 //initStatics(context);
182 throw new RuntimeException("Assertion failed: Utilities not initialized");
183 }
184
185 dest.drawColor(0, PorterDuff.Mode.CLEAR);
Jason Samsb4ecab22010-01-19 16:43:26 -0800186
Joe Onorato1291a8c2009-09-15 15:07:25 -0400187 int[] xy = new int[2];
Mike Reedcdd11792009-10-29 17:27:55 -0400188 Bitmap mask = src.extractAlpha(sBlurPaint, xy);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400189
Mike Reedcdd11792009-10-29 17:27:55 -0400190 float px = (destWidth - src.getWidth()) / 2;
191 float py = (destHeight - src.getHeight()) / 2;
Joe Onoratoc61cff92009-11-08 11:54:39 -0500192 dest.drawBitmap(mask, px + xy[0], py + xy[1],
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500193 pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
Joe Onorato1291a8c2009-09-15 15:07:25 -0400194
195 mask.recycle();
196 }
197 }
Joe Onorato6665c0f2009-09-02 15:27:24 -0700198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 /**
200 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
201 * The size of the thumbnail is defined by the dimension
202 * android.R.dimen.launcher_application_icon_size.
203 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 * @param bitmap The bitmap to get a thumbnail of.
205 * @param context The application's context.
206 *
207 * @return A thumbnail for the specified bitmap or the bitmap itself if the
208 * thumbnail could not be created.
209 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 synchronized (sCanvas) { // we share the statics :-(
212 if (sIconWidth == -1) {
Joe Onorato6665c0f2009-09-02 15:27:24 -0700213 initStatics(context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 }
215
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
217 return bitmap;
218 } else {
219 return createIconBitmap(new BitmapDrawable(bitmap), context);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700223
Joe Onorato56d82912010-03-07 14:32:10 -0500224 static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
225 synchronized (sCanvas) { // we share the statics :-(
226 if (sIconWidth == -1) {
227 initStatics(context);
228 }
229 final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
230 Bitmap.Config.ARGB_8888);
231 final Canvas canvas = sCanvas;
232 canvas.setBitmap(disabled);
233
234 canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
235
236 return disabled;
237 }
238 }
239
Joe Onorato6665c0f2009-09-02 15:27:24 -0700240 private static void initStatics(Context context) {
241 final Resources resources = context.getResources();
Joe Onorato1291a8c2009-09-15 15:07:25 -0400242 final DisplayMetrics metrics = resources.getDisplayMetrics();
243 final float density = metrics.density;
244
Michael Jurkac9a96192010-11-01 11:52:08 -0700245 sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800246 if (LauncherApplication.isScreenXLarge()) {
247 sIconContentSize = (int) resources.getDimension(R.dimen.app_icon_content_size);
248 }
Jason Samsb4ecab22010-01-19 16:43:26 -0800249 sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
Joe Onorato1291a8c2009-09-15 15:07:25 -0400250
Joe Onoratoa4c0cb92009-11-02 10:42:02 -0500251 sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
Joe Onoratoeb8325a2009-11-08 13:20:30 -0500252 sGlowColorPressedPaint.setColor(0xffffc300);
253 sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onoratoc61cff92009-11-08 11:54:39 -0500254 sGlowColorFocusedPaint.setColor(0xffff8e00);
255 sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
Joe Onorato56d82912010-03-07 14:32:10 -0500256
257 ColorMatrix cm = new ColorMatrix();
258 cm.setSaturation(0.2f);
259 sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
260 sDisabledPaint.setAlpha(0x88);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700261 }
262
Joe Onoratobf15cb42009-08-07 14:33:40 -0700263 static class BubbleText {
264 private static final int MAX_LINES = 2;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700265
Romain Guy88f38d12010-01-26 14:50:34 -0800266 private final TextPaint mTextPaint;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700267
Romain Guy88f38d12010-01-26 14:50:34 -0800268 private final RectF mBubbleRect = new RectF();
Joe Onoratobf15cb42009-08-07 14:33:40 -0700269
Romain Guy88f38d12010-01-26 14:50:34 -0800270 private final float mTextWidth;
271 private final int mLeading;
272 private final int mFirstLineY;
273 private final int mLineHeight;
274
275 private final int mBitmapWidth;
276 private final int mBitmapHeight;
277 private final int mDensity;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700278
279 BubbleText(Context context) {
280 final Resources resources = context.getResources();
281
Romain Guy88f38d12010-01-26 14:50:34 -0800282 final DisplayMetrics metrics = resources.getDisplayMetrics();
283 final float scale = metrics.density;
284 mDensity = metrics.densityDpi;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700285
Kenny Root97051642010-03-05 13:24:08 -0800286 final float paddingLeft = 2.0f * scale;
287 final float paddingRight = 2.0f * scale;
Romain Guy442eda22010-02-04 15:59:37 -0800288 final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700289
290 RectF bubbleRect = mBubbleRect;
291 bubbleRect.left = 0;
292 bubbleRect.top = 0;
Romain Guyc0c4fe32010-02-12 23:20:25 +0100293 bubbleRect.right = (int) cellWidth;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700294
Kenny Root97051642010-03-05 13:24:08 -0800295 mTextWidth = cellWidth - paddingLeft - paddingRight;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700296
Joe Onoratobf15cb42009-08-07 14:33:40 -0700297 TextPaint textPaint = mTextPaint = new TextPaint();
Joe Onoratoefabe002009-08-28 09:38:18 -0700298 textPaint.setTypeface(Typeface.DEFAULT);
299 textPaint.setTextSize(13*scale);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700300 textPaint.setColor(0xffffffff);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700301 textPaint.setAntiAlias(true);
Joe Onorato9392a752009-09-15 17:13:09 -0400302 if (TEXT_BURN) {
303 textPaint.setShadowLayer(8, 0, 0, 0xff000000);
304 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700305
306 float ascent = -textPaint.ascent();
307 float descent = textPaint.descent();
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700308 float leading = 0.0f;//(ascent+descent) * 0.1f;
Joe Onoratobf15cb42009-08-07 14:33:40 -0700309 mLeading = (int)(leading + 0.5f);
310 mFirstLineY = (int)(leading + ascent + 0.5f);
311 mLineHeight = (int)(leading + ascent + descent + 0.5f);
312
Romain Guy442eda22010-02-04 15:59:37 -0800313 mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700314 mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700315
Joe Onoratofccc8df2009-10-01 14:30:16 -0700316 mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
317
Joe Onorato080d9b62009-11-02 12:01:11 -0500318 if (false) {
319 Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
320 + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
321 + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
322 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700323 }
324
325 /** You own the bitmap after this and you must call recycle on it. */
326 Bitmap createTextBitmap(String text) {
Jason Sams6ec11bc2010-01-19 17:56:52 -0800327 Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
Romain Guy88f38d12010-01-26 14:50:34 -0800328 b.setDensity(mDensity);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700329 Canvas c = new Canvas(b);
330
331 StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
332 Alignment.ALIGN_CENTER, 1, 0, true);
333 int lineCount = layout.getLineCount();
334 if (lineCount > MAX_LINES) {
335 lineCount = MAX_LINES;
336 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700337 //if (!TEXT_BURN && lineCount > 0) {
338 //RectF bubbleRect = mBubbleRect;
339 //bubbleRect.bottom = height(lineCount);
340 //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
341 //}
Joe Onoratobf15cb42009-08-07 14:33:40 -0700342 for (int i=0; i<lineCount; i++) {
Joe Onoratofccc8df2009-10-01 14:30:16 -0700343 //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
344 //int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800345 final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
Joe Onoratofccc8df2009-10-01 14:30:16 -0700346 int x = (int)(mBubbleRect.left
Romain Guy88f38d12010-01-26 14:50:34 -0800347 + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
Joe Onoratobf15cb42009-08-07 14:33:40 -0700348 int y = mFirstLineY + (i * mLineHeight);
Romain Guy88f38d12010-01-26 14:50:34 -0800349 c.drawText(lineText, x, y, mTextPaint);
Joe Onoratobf15cb42009-08-07 14:33:40 -0700350 }
351
352 return b;
353 }
Joe Onorato43e7bcf2009-08-08 18:53:53 -0700354
355 private int height(int lineCount) {
356 return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
357 }
358
359 int getBubbleWidth() {
360 return (int)(mBubbleRect.width() + 0.5f);
361 }
362
363 int getMaxBubbleHeight() {
364 return height(MAX_LINES);
365 }
366
367 int getBitmapWidth() {
368 return mBitmapWidth;
369 }
370
371 int getBitmapHeight() {
372 return mBitmapHeight;
373 }
Joe Onoratobf15cb42009-08-07 14:33:40 -0700374 }
375
376 /** Only works for positive numbers. */
377 static int roundToPow2(int n) {
378 int orig = n;
379 n >>= 1;
380 int mask = 0x8000000;
381 while (mask != 0 && (n & mask) == 0) {
382 mask >>= 1;
383 }
384 while (mask != 0) {
385 n |= mask;
386 mask >>= 1;
387 }
388 n += 1;
389 if (n != orig) {
390 n <<= 1;
391 }
392 return n;
393 }
Winson Chung97d85d22011-04-13 11:27:36 -0700394
395 static int generateRandomId() {
396 return new Random(System.currentTimeMillis()).nextInt(1 << 24);
397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398}