Utilities.java revision 1291a8c236c84451321438cb68855f6f2eb24959
1/*
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
17package com.android.launcher2;
18
19import android.graphics.drawable.BitmapDrawable;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.PaintDrawable;
22import android.graphics.Bitmap;
23import android.graphics.BlurMaskFilter;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.PaintFlagsDrawFilter;
27import android.graphics.PixelFormat;
28import android.graphics.PorterDuff;
29import android.graphics.Rect;
30import android.graphics.RectF;
31import android.graphics.Typeface;
32import android.text.Layout.Alignment;
33import android.text.StaticLayout;
34import android.text.TextPaint;
35import android.util.DisplayMetrics;
36import android.util.Log;
37import android.content.res.Resources;
38import android.content.Context;
39
40/**
41 * Various utilities shared amongst the Launcher's classes.
42 */
43final class Utilities {
44    private static final String TAG = "Launcher.Utilities";
45
46    private static int sIconWidth = -1;
47    private static int sIconHeight = -1;
48    private static int sIconTextureWidth = -1;
49    private static int sIconTextureHeight = -1;
50
51    private static final Paint sPaint = new Paint();
52    private static final Paint sBlurPaint = new Paint();
53    private static final Paint sGlowColorPaint = new Paint();
54    private static final Paint sEmptyPaint = new Paint();
55    private static final Rect sBounds = new Rect();
56    private static final Rect sOldBounds = new Rect();
57    private static Canvas sCanvas = new Canvas();
58
59    static {
60        sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
61                Paint.FILTER_BITMAP_FLAG));
62    }
63
64    static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
65        final int bitmapWidth = bitmap.getWidth();
66        final int bitmapHeight = bitmap.getHeight();
67
68        if (bitmapWidth < width || bitmapHeight < height) {
69            int color = context.getResources().getColor(R.color.window_background);
70
71            Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
72                    bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
73            centered.setDensity(bitmap.getDensity());
74            Canvas canvas = new Canvas(centered);
75            canvas.drawColor(color);
76            canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
77                    null);
78
79            bitmap = centered;
80        }
81
82        return bitmap;
83    }
84
85    /**
86     * Returns a Drawable representing the thumbnail of the specified Drawable.
87     * The size of the thumbnail is defined by the dimension
88     * android.R.dimen.launcher_application_icon_size.
89     *
90     * @param icon The icon to get a thumbnail of.
91     * @param context The application's context.
92     *
93     * @return A thumbnail for the specified icon or the icon itself if the
94     *         thumbnail could not be created.
95     */
96    static Drawable createIconThumbnail(Drawable icon, Context context) {
97        synchronized (sCanvas) { // we share the statics :-(
98            if (sIconWidth == -1) {
99                initStatics(context);
100            }
101
102            int width = sIconWidth;
103            int height = sIconHeight;
104
105            float scale = 1.0f;
106            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 iconWidth = icon.getIntrinsicWidth();
119            int iconHeight = icon.getIntrinsicHeight();
120
121            if (iconWidth > 0 && iconWidth > 0) {
122                if (width < iconWidth || height < iconHeight || scale != 1.0f) {
123                    final float ratio = (float) iconWidth / iconHeight;
124
125                    if (iconWidth > iconHeight) {
126                        height = (int) (width / ratio);
127                    } else if (iconHeight > iconWidth) {
128                        width = (int) (height * ratio);
129                    }
130
131                    final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ?
132                                Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
133                    final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
134                    final Canvas canvas = sCanvas;
135                    canvas.setBitmap(thumb);
136                    // Copy the old bounds to restore them later
137                    // If we were to do oldBounds = icon.getBounds(),
138                    // the call to setBounds() that follows would
139                    // change the same instance and we would lose the
140                    // old bounds
141                    sOldBounds.set(icon.getBounds());
142                    final int x = (sIconWidth - width) / 2;
143                    final int y = (sIconHeight - height) / 2;
144                    icon.setBounds(x, y, x + width, y + height);
145                    icon.draw(canvas);
146                    icon.setBounds(sOldBounds);
147                    icon = new FastBitmapDrawable(thumb);
148                } else if (iconWidth < width && iconHeight < height) {
149                    final Bitmap.Config c = Bitmap.Config.ARGB_8888;
150                    final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
151                    final Canvas canvas = sCanvas;
152                    canvas.setBitmap(thumb);
153                    sOldBounds.set(icon.getBounds());
154                    final int x = (width - iconWidth) / 2;
155                    final int y = (height - iconHeight) / 2;
156                    icon.setBounds(x, y, x + iconWidth, y + iconHeight);
157                    icon.draw(canvas);
158                    icon.setBounds(sOldBounds);
159                    icon = new FastBitmapDrawable(thumb);
160                }
161            }
162
163            return icon;
164        }
165    }
166
167    static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
168    static int sColorIndex = 0;
169
170    /**
171     * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
172     * of two sized ARGB_8888 bitmap that can be used as a gl texture.
173     */
174    static Bitmap createAllAppsBitmap(Drawable icon, Context context) {
175        synchronized (sCanvas) { // we share the statics :-(
176            if (sIconWidth == -1) {
177                initStatics(context);
178            }
179
180            int width = sIconWidth;
181            int height = sIconHeight;
182
183            float scale = 1.0f;
184            if (icon instanceof PaintDrawable) {
185                PaintDrawable painter = (PaintDrawable) icon;
186                painter.setIntrinsicWidth(width);
187                painter.setIntrinsicHeight(height);
188            } else if (icon instanceof BitmapDrawable) {
189                // Ensure the bitmap has a density.
190                BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
191                Bitmap bitmap = bitmapDrawable.getBitmap();
192                if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
193                    bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
194                }
195            }
196            int sourceWidth = icon.getIntrinsicWidth();
197            int sourceHeight = icon.getIntrinsicHeight();
198
199            if (sourceWidth > 0 && sourceWidth > 0) {
200                // There are intrinsic sizes.
201                if (width < sourceWidth || height < sourceHeight || scale != 1.0f) {
202                    // It's too big, scale it down.
203                    final float ratio = (float) sourceWidth / sourceHeight;
204                    if (sourceWidth > sourceHeight) {
205                        height = (int) (width / ratio);
206                    } else if (sourceHeight > sourceWidth) {
207                        width = (int) (height * ratio);
208                    }
209                } else if (sourceWidth < width && sourceHeight < height) {
210                    // It's small, use the size they gave us.
211                    width = sourceWidth;
212                    height = sourceWidth;
213                }
214            }
215
216            // no intrinsic size --> use default size
217            int textureWidth = sIconTextureWidth;
218            int textureHeight = sIconTextureHeight;
219
220            final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
221                    Bitmap.Config.ARGB_8888);
222            final Canvas canvas = sCanvas;
223            canvas.setBitmap(bitmap);
224
225            final int left = (textureWidth-width) / 2;
226            final int top = (textureHeight-height) / 2;
227
228            if (false) {
229                // draw a big box for the icon for debugging
230                canvas.drawColor(sColors[sColorIndex]);
231                if (++sColorIndex >= sColors.length) sColorIndex = 0;
232                Paint debugPaint = new Paint();
233                debugPaint.setColor(0xffcccc00);
234                canvas.drawRect(left, top, left+width, top+height, debugPaint);
235            }
236
237            sOldBounds.set(icon.getBounds());
238            icon.setBounds(left, top, left+width, top+height);
239            icon.draw(canvas);
240            icon.setBounds(sOldBounds);
241
242            return bitmap;
243        }
244    }
245
246    static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, Bitmap src) {
247        synchronized (sCanvas) { // we share the statics :-(
248            if (sIconWidth == -1) {
249                // We can't have gotten to here without src being initialized, which
250                // comes from this file already.  So just assert.
251                //initStatics(context);
252                throw new RuntimeException("Assertion failed: Utilities not initialized");
253            }
254
255            dest.drawColor(0, PorterDuff.Mode.CLEAR);
256
257            final float scale = 1.55f;
258            Bitmap scaled = Bitmap.createScaledBitmap(src, (int)(src.getWidth()*scale),
259                    (int)(src.getHeight()*scale), true);
260
261            int[] xy = new int[2];
262            Bitmap mask = scaled.extractAlpha(sBlurPaint, xy);
263
264            dest.drawBitmap(mask, (destWidth - mask.getWidth()) / 2,
265                    (destHeight - mask.getHeight()) / 2, sGlowColorPaint);
266            dest.drawBitmap(src, (destWidth - src.getWidth()) / 2,
267                    (destHeight - src.getHeight()) / 2, sEmptyPaint);
268
269            mask.recycle();
270        }
271    }
272
273    /**
274     * Returns a Bitmap representing the thumbnail of the specified Bitmap.
275     * The size of the thumbnail is defined by the dimension
276     * android.R.dimen.launcher_application_icon_size.
277     *
278     * @param bitmap The bitmap to get a thumbnail of.
279     * @param context The application's context.
280     *
281     * @return A thumbnail for the specified bitmap or the bitmap itself if the
282     *         thumbnail could not be created.
283     */
284    static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context) {
285        synchronized (sCanvas) { // we share the statics :-(
286            if (sIconWidth == -1) {
287                initStatics(context);
288            }
289
290            int width = sIconWidth;
291            int height = sIconHeight;
292
293            final int bitmapWidth = bitmap.getWidth();
294            final int bitmapHeight = bitmap.getHeight();
295
296            if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
297                final float ratio = (float) bitmapWidth / bitmapHeight;
298
299                if (bitmapWidth > bitmapHeight) {
300                    height = (int) (width / ratio);
301                } else if (bitmapHeight > bitmapWidth) {
302                    width = (int) (height * ratio);
303                }
304
305                final Bitmap.Config c = (width == sIconWidth && height == sIconHeight) ?
306                        bitmap.getConfig() : Bitmap.Config.ARGB_8888;
307                final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
308                final Canvas canvas = sCanvas;
309                final Paint paint = sPaint;
310                canvas.setBitmap(thumb);
311                paint.setDither(false);
312                paint.setFilterBitmap(true);
313                sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
314                sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
315                canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
316                return thumb;
317            }
318
319            return bitmap;
320        }
321    }
322
323    private static void initStatics(Context context) {
324        final Resources resources = context.getResources();
325        final DisplayMetrics metrics = resources.getDisplayMetrics();
326        final float density = metrics.density;
327
328        sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
329        sIconTextureWidth = sIconTextureHeight = roundToPow2(sIconWidth);
330
331        sBlurPaint.setMaskFilter(new BlurMaskFilter(7 * density, BlurMaskFilter.Blur.NORMAL));
332        sGlowColorPaint.setColor(0xffff9000);
333    }
334
335    static class BubbleText {
336        private static final int MAX_LINES = 2;
337        private TextPaint mTextPaint;
338        private Paint mRectPaint;
339
340        private float mBubblePadding;
341        private float mCornerRadius;
342        private RectF mBubbleRect = new RectF();
343
344        private float mTextWidth;
345        private int mLeading;
346        private int mFirstLineY;
347        private int mLineHeight;
348
349        private int mBitmapWidth;
350        private int mBitmapHeight;
351
352        BubbleText(Context context) {
353            final Resources resources = context.getResources();
354
355            final float scale = resources.getDisplayMetrics().density;
356
357            final float paddingLeft = 5.0f * scale;
358            final float paddingRight = 5.0f * scale;
359            final float cellWidth = resources.getDimension(R.dimen.workspace_cell_width);
360            final float bubbleWidth = cellWidth - paddingLeft - paddingRight;
361            mBubblePadding = 3.0f * scale;
362
363            RectF bubbleRect = mBubbleRect;
364            bubbleRect.left = 0;
365            bubbleRect.top = 0;
366            bubbleRect.right = (int)(bubbleWidth+0.5f);
367
368            mCornerRadius = BubbleTextView.CORNER_RADIUS * scale;
369            mTextWidth = bubbleWidth - mBubblePadding - mBubblePadding;
370
371            Paint rectPaint = mRectPaint = new Paint();
372            rectPaint.setColor(0xff000000);
373            rectPaint.setAntiAlias(true);
374
375            Log.d(TAG, "scale=" + scale + " textSize=" + (13*scale));
376
377            TextPaint textPaint = mTextPaint = new TextPaint();
378            textPaint.setTypeface(Typeface.DEFAULT);
379            textPaint.setTextSize(13*scale);
380            textPaint.setColor(0xffffffff);
381            textPaint.setAntiAlias(true);
382            //textPaint.setShadowLayer(8, 0, 0, 0xff000000);
383
384            float ascent = -textPaint.ascent();
385            float descent = textPaint.descent();
386            float leading = 0.0f;//(ascent+descent) * 0.1f;
387            mLeading = (int)(leading + 0.5f);
388            mFirstLineY = (int)(leading + ascent + 0.5f);
389            mLineHeight = (int)(leading + ascent + descent + 0.5f);
390
391            mBitmapWidth = roundToPow2((int)(mBubbleRect.width() + 0.5f));
392            mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
393
394            Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
395                    + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
396                    + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
397        }
398
399        /** You own the bitmap after this and you must call recycle on it. */
400        Bitmap createTextBitmap(String text) {
401            Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ARGB_8888);
402            Canvas c = new Canvas(b);
403
404            StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
405                    Alignment.ALIGN_CENTER, 1, 0, true);
406            int lineCount = layout.getLineCount();
407            if (lineCount > MAX_LINES) {
408                lineCount = MAX_LINES;
409            }
410            if (lineCount > 0) {
411                RectF bubbleRect = mBubbleRect;
412                bubbleRect.bottom = height(lineCount);
413                c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
414            }
415            for (int i=0; i<lineCount; i++) {
416                int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
417                int y = mFirstLineY + (i * mLineHeight);
418                c.drawText(text.substring(layout.getLineStart(i), layout.getLineEnd(i)),
419                        x, y, mTextPaint);
420            }
421
422            return b;
423        }
424
425        private int height(int lineCount) {
426            return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
427        }
428
429        int getBubbleWidth() {
430            return (int)(mBubbleRect.width() + 0.5f);
431        }
432
433        int getMaxBubbleHeight() {
434            return height(MAX_LINES);
435        }
436
437        int getBitmapWidth() {
438            return mBitmapWidth;
439        }
440
441        int getBitmapHeight() {
442            return mBitmapHeight;
443        }
444    }
445
446    /** Only works for positive numbers. */
447    static int roundToPow2(int n) {
448        int orig = n;
449        n >>= 1;
450        int mask = 0x8000000;
451        while (mask != 0 && (n & mask) == 0) {
452            mask >>= 1;
453        }
454        while (mask != 0) {
455            n |= mask;
456            mask >>= 1;
457        }
458        n += 1;
459        if (n != orig) {
460            n <<= 1;
461        }
462        return n;
463    }
464}
465