Utilities.java revision aafa03cbb925c74be1c13f8bb99d928be429e62f
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.content.Context;
20import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.BlurMaskFilter;
23import android.graphics.Canvas;
24import android.graphics.ColorMatrix;
25import android.graphics.ColorMatrixColorFilter;
26import android.graphics.Paint;
27import android.graphics.PaintFlagsDrawFilter;
28import android.graphics.PorterDuff;
29import android.graphics.Rect;
30import android.graphics.RectF;
31import android.graphics.TableMaskFilter;
32import android.graphics.Typeface;
33import android.graphics.drawable.BitmapDrawable;
34import android.graphics.drawable.Drawable;
35import android.graphics.drawable.PaintDrawable;
36import android.text.StaticLayout;
37import android.text.TextPaint;
38import android.text.Layout.Alignment;
39import android.util.DisplayMetrics;
40import android.util.Log;
41
42import com.android.launcher.R;
43
44/**
45 * Various utilities shared amongst the Launcher's classes.
46 */
47final class Utilities {
48    private static final String TAG = "Launcher.Utilities";
49
50    private static final boolean TEXT_BURN = false;
51
52    private static int sIconWidth = -1;
53    private static int sIconHeight = -1;
54    private static int sIconTextureWidth = -1;
55    private static int sIconTextureHeight = -1;
56
57    private static final Paint sPaint = new Paint();
58    private static final Paint sBlurPaint = new Paint();
59    private static final Paint sGlowColorPressedPaint = new Paint();
60    private static final Paint sGlowColorFocusedPaint = new Paint();
61    private static final Paint sDisabledPaint = new Paint();
62    private static final Rect sBounds = new Rect();
63    private static final Rect sOldBounds = new Rect();
64    private static final Canvas sCanvas = new Canvas();
65
66    static {
67        sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
68                Paint.FILTER_BITMAP_FLAG));
69    }
70
71    static Bitmap centerToFit(Bitmap bitmap, int width, int height, Context context) {
72        final int bitmapWidth = bitmap.getWidth();
73        final int bitmapHeight = bitmap.getHeight();
74
75        if (bitmapWidth < width || bitmapHeight < height) {
76            int color = context.getResources().getColor(R.color.window_background);
77
78            Bitmap centered = Bitmap.createBitmap(bitmapWidth < width ? width : bitmapWidth,
79                    bitmapHeight < height ? height : bitmapHeight, Bitmap.Config.RGB_565);
80            centered.setDensity(bitmap.getDensity());
81            Canvas canvas = new Canvas(centered);
82            canvas.drawColor(color);
83            canvas.drawBitmap(bitmap, (width - bitmapWidth) / 2.0f, (height - bitmapHeight) / 2.0f,
84                    null);
85
86            bitmap = centered;
87        }
88
89        return bitmap;
90    }
91
92    static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
93    static int sColorIndex = 0;
94
95    /**
96     * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
97     * of two sized ARGB_8888 bitmap that can be used as a gl texture.
98     */
99    static Bitmap createIconBitmap(Drawable icon, Context context) {
100        synchronized (sCanvas) { // we share the statics :-(
101            if (sIconWidth == -1) {
102                initStatics(context);
103            }
104
105            int width = sIconWidth;
106            int height = sIconHeight;
107
108            if (icon instanceof PaintDrawable) {
109                PaintDrawable painter = (PaintDrawable) icon;
110                painter.setIntrinsicWidth(width);
111                painter.setIntrinsicHeight(height);
112            } else if (icon instanceof BitmapDrawable) {
113                // Ensure the bitmap has a density.
114                BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
115                Bitmap bitmap = bitmapDrawable.getBitmap();
116                if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
117                    bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
118                }
119            }
120            int sourceWidth = icon.getIntrinsicWidth();
121            int sourceHeight = icon.getIntrinsicHeight();
122
123            if (sourceWidth > 0 && sourceWidth > 0) {
124                // There are intrinsic sizes.
125                if (width < sourceWidth || height < sourceHeight) {
126                    // It's too big, scale it down.
127                    final float ratio = (float) sourceWidth / sourceHeight;
128                    if (sourceWidth > sourceHeight) {
129                        height = (int) (width / ratio);
130                    } else if (sourceHeight > sourceWidth) {
131                        width = (int) (height * ratio);
132                    }
133                } else if (sourceWidth < width && sourceHeight < height) {
134                    // It's small, use the size they gave us.
135                    width = sourceWidth;
136                    height = sourceHeight;
137                }
138            }
139
140            // no intrinsic size --> use default size
141            int textureWidth = sIconTextureWidth;
142            int textureHeight = sIconTextureHeight;
143
144            final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
145                    Bitmap.Config.ARGB_8888);
146            final Canvas canvas = sCanvas;
147            canvas.setBitmap(bitmap);
148
149            final int left = (textureWidth-width) / 2;
150            final int top = (textureHeight-height) / 2;
151
152            if (false) {
153                // draw a big box for the icon for debugging
154                canvas.drawColor(sColors[sColorIndex]);
155                if (++sColorIndex >= sColors.length) sColorIndex = 0;
156                Paint debugPaint = new Paint();
157                debugPaint.setColor(0xffcccc00);
158                canvas.drawRect(left, top, left+width, top+height, debugPaint);
159            }
160
161            sOldBounds.set(icon.getBounds());
162            icon.setBounds(left, top, left+width, top+height);
163            icon.draw(canvas);
164            icon.setBounds(sOldBounds);
165
166            return bitmap;
167        }
168    }
169
170    static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
171            boolean pressed, Bitmap src) {
172        synchronized (sCanvas) { // we share the statics :-(
173            if (sIconWidth == -1) {
174                // We can't have gotten to here without src being initialized, which
175                // comes from this file already.  So just assert.
176                //initStatics(context);
177                throw new RuntimeException("Assertion failed: Utilities not initialized");
178            }
179
180            dest.drawColor(0, PorterDuff.Mode.CLEAR);
181
182            int[] xy = new int[2];
183            Bitmap mask = src.extractAlpha(sBlurPaint, xy);
184
185            float px = (destWidth - src.getWidth()) / 2;
186            float py = (destHeight - src.getHeight()) / 2;
187            dest.drawBitmap(mask, px + xy[0], py + xy[1],
188                    pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
189
190            mask.recycle();
191        }
192    }
193
194    /**
195     * Returns a Bitmap representing the thumbnail of the specified Bitmap.
196     * The size of the thumbnail is defined by the dimension
197     * android.R.dimen.launcher_application_icon_size.
198     *
199     * @param bitmap The bitmap to get a thumbnail of.
200     * @param context The application's context.
201     *
202     * @return A thumbnail for the specified bitmap or the bitmap itself if the
203     *         thumbnail could not be created.
204     */
205    static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
206        synchronized (sCanvas) { // we share the statics :-(
207            if (sIconWidth == -1) {
208                initStatics(context);
209            }
210
211            if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
212                return bitmap;
213            } else {
214                return createIconBitmap(new BitmapDrawable(bitmap), context);
215            }
216        }
217    }
218
219    static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
220        synchronized (sCanvas) { // we share the statics :-(
221            if (sIconWidth == -1) {
222                initStatics(context);
223            }
224            final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
225                    Bitmap.Config.ARGB_8888);
226            final Canvas canvas = sCanvas;
227            canvas.setBitmap(disabled);
228
229            canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
230
231            return disabled;
232        }
233    }
234
235    private static void initStatics(Context context) {
236        final Resources resources = context.getResources();
237        final DisplayMetrics metrics = resources.getDisplayMetrics();
238        final float density = metrics.density;
239
240        sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
241        sIconTextureWidth = sIconTextureHeight = sIconWidth + 2;
242
243        sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
244        sGlowColorPressedPaint.setColor(0xffffc300);
245        sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
246        sGlowColorFocusedPaint.setColor(0xffff8e00);
247        sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
248
249        ColorMatrix cm = new ColorMatrix();
250        cm.setSaturation(0.2f);
251        sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
252        sDisabledPaint.setAlpha(0x88);
253    }
254
255    static class BubbleText {
256        private static final int MAX_LINES = 2;
257
258        private final TextPaint mTextPaint;
259
260        private final RectF mBubbleRect = new RectF();
261
262        private final float mTextWidth;
263        private final int mLeading;
264        private final int mFirstLineY;
265        private final int mLineHeight;
266
267        private final int mBitmapWidth;
268        private final int mBitmapHeight;
269        private final int mDensity;
270
271        BubbleText(Context context) {
272            final Resources resources = context.getResources();
273
274            final DisplayMetrics metrics = resources.getDisplayMetrics();
275            final float scale = metrics.density;
276            mDensity = metrics.densityDpi;
277
278            final float paddingLeft = 2.0f * scale;
279            final float paddingRight = 2.0f * scale;
280            final float cellWidth = resources.getDimension(R.dimen.title_texture_width);
281
282            RectF bubbleRect = mBubbleRect;
283            bubbleRect.left = 0;
284            bubbleRect.top = 0;
285            bubbleRect.right = (int) cellWidth;
286
287            mTextWidth = cellWidth - paddingLeft - paddingRight;
288
289            TextPaint textPaint = mTextPaint = new TextPaint();
290            textPaint.setTypeface(Typeface.DEFAULT);
291            textPaint.setTextSize(13*scale);
292            textPaint.setColor(0xffffffff);
293            textPaint.setAntiAlias(true);
294            if (TEXT_BURN) {
295                textPaint.setShadowLayer(8, 0, 0, 0xff000000);
296            }
297
298            float ascent = -textPaint.ascent();
299            float descent = textPaint.descent();
300            float leading = 0.0f;//(ascent+descent) * 0.1f;
301            mLeading = (int)(leading + 0.5f);
302            mFirstLineY = (int)(leading + ascent + 0.5f);
303            mLineHeight = (int)(leading + ascent + descent + 0.5f);
304
305            mBitmapWidth = (int)(mBubbleRect.width() + 0.5f);
306            mBitmapHeight = roundToPow2((int)((MAX_LINES * mLineHeight) + leading + 0.5f));
307
308            mBubbleRect.offsetTo((mBitmapWidth-mBubbleRect.width())/2, 0);
309
310            if (false) {
311                Log.d(TAG, "mBitmapWidth=" + mBitmapWidth + " mBitmapHeight="
312                        + mBitmapHeight + " w=" + ((int)(mBubbleRect.width() + 0.5f))
313                        + " h=" + ((int)((MAX_LINES * mLineHeight) + leading + 0.5f)));
314            }
315        }
316
317        /** You own the bitmap after this and you must call recycle on it. */
318        Bitmap createTextBitmap(String text) {
319            Bitmap b = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.ALPHA_8);
320            b.setDensity(mDensity);
321            Canvas c = new Canvas(b);
322
323            StaticLayout layout = new StaticLayout(text, mTextPaint, (int)mTextWidth,
324                    Alignment.ALIGN_CENTER, 1, 0, true);
325            int lineCount = layout.getLineCount();
326            if (lineCount > MAX_LINES) {
327                lineCount = MAX_LINES;
328            }
329            //if (!TEXT_BURN && lineCount > 0) {
330                //RectF bubbleRect = mBubbleRect;
331                //bubbleRect.bottom = height(lineCount);
332                //c.drawRoundRect(bubbleRect, mCornerRadius, mCornerRadius, mRectPaint);
333            //}
334            for (int i=0; i<lineCount; i++) {
335                //int x = (int)((mBubbleRect.width() - layout.getLineMax(i)) / 2.0f);
336                //int y = mFirstLineY + (i * mLineHeight);
337                final String lineText = text.substring(layout.getLineStart(i), layout.getLineEnd(i));
338                int x = (int)(mBubbleRect.left
339                        + ((mBubbleRect.width() - mTextPaint.measureText(lineText)) * 0.5f));
340                int y = mFirstLineY + (i * mLineHeight);
341                c.drawText(lineText, x, y, mTextPaint);
342            }
343
344            return b;
345        }
346
347        private int height(int lineCount) {
348            return (int)((lineCount * mLineHeight) + mLeading + mLeading + 0.0f);
349        }
350
351        int getBubbleWidth() {
352            return (int)(mBubbleRect.width() + 0.5f);
353        }
354
355        int getMaxBubbleHeight() {
356            return height(MAX_LINES);
357        }
358
359        int getBitmapWidth() {
360            return mBitmapWidth;
361        }
362
363        int getBitmapHeight() {
364            return mBitmapHeight;
365        }
366    }
367
368    /** Only works for positive numbers. */
369    static int roundToPow2(int n) {
370        int orig = n;
371        n >>= 1;
372        int mask = 0x8000000;
373        while (mask != 0 && (n & mask) == 0) {
374            mask >>= 1;
375        }
376        while (mask != 0) {
377            n |= mask;
378            mask >>= 1;
379        }
380        n += 1;
381        if (n != orig) {
382            n <<= 1;
383        }
384        return n;
385    }
386}
387