131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1997d85d23b013347bead4e2f5fa430a79ce69431eWinson Chungimport java.util.Random;
2097d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
21aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.Context;
22aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.res.Resources;
2331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Bitmap;
241291a8c236c84451321438cb68855f6f2eb24959Joe Onoratoimport android.graphics.BlurMaskFilter;
2531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Canvas;
2656d8291af6a28c6ba64113120efdf84a785e816cJoe Onoratoimport android.graphics.ColorMatrix;
2756d8291af6a28c6ba64113120efdf84a785e816cJoe Onoratoimport android.graphics.ColorMatrixColorFilter;
2831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Paint;
29bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onoratoimport android.graphics.PaintFlagsDrawFilter;
301291a8c236c84451321438cb68855f6f2eb24959Joe Onoratoimport android.graphics.PorterDuff;
3131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.graphics.Rect;
32cdd117935f51d66f6f212387300d851f81aa071fMike Reedimport android.graphics.TableMaskFilter;
33aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.graphics.drawable.BitmapDrawable;
34aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.graphics.drawable.Drawable;
35aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.graphics.drawable.PaintDrawable;
3632ce7f17a85abf9b175f773371308876f7401547Dianne Hackbornimport android.util.DisplayMetrics;
3731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
38edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guyimport com.android.launcher.R;
39edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
4031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
4131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Various utilities shared amongst the Launcher's classes.
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
4331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectfinal class Utilities {
441291a8c236c84451321438cb68855f6f2eb24959Joe Onorato    private static final String TAG = "Launcher.Utilities";
451291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
4631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static int sIconWidth = -1;
4731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static int sIconHeight = -1;
486665c0f1fec6ec0962896622986340081df992e7Joe Onorato    private static int sIconTextureWidth = -1;
496665c0f1fec6ec0962896622986340081df992e7Joe Onorato    private static int sIconTextureHeight = -1;
5031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
511291a8c236c84451321438cb68855f6f2eb24959Joe Onorato    private static final Paint sBlurPaint = new Paint();
52eb8325a9e24cda47720152e1e95f5d86ab11f200Joe Onorato    private static final Paint sGlowColorPressedPaint = new Paint();
53c61cff9299a54599c2b214de7c819b6be0412f4eJoe Onorato    private static final Paint sGlowColorFocusedPaint = new Paint();
5456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    private static final Paint sDisabledPaint = new Paint();
5531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final Rect sOldBounds = new Rect();
5689911d2450f5db92d79e3bcefa9d324e9e8c4900Romain Guy    private static final Canvas sCanvas = new Canvas();
5731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static {
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
6031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                Paint.FILTER_BITMAP_FLAG));
6131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
626665c0f1fec6ec0962896622986340081df992e7Joe Onorato    static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
636665c0f1fec6ec0962896622986340081df992e7Joe Onorato    static int sColorIndex = 0;
646665c0f1fec6ec0962896622986340081df992e7Joe Onorato
656665c0f1fec6ec0962896622986340081df992e7Joe Onorato    /**
66931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka     * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
67931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka     * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
68931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka     * to the proper size (48dp)
69931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka     */
70931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    static Bitmap createIconBitmap(Bitmap icon, Context context) {
71931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        int textureWidth = sIconTextureWidth;
72931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        int textureHeight = sIconTextureHeight;
73931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        int sourceWidth = icon.getWidth();
74931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        int sourceHeight = icon.getHeight();
75931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
76931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
77931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return Bitmap.createBitmap(icon,
78931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    (sourceWidth - textureWidth) / 2,
79931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    (sourceHeight - textureHeight) / 2,
80931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka                    textureWidth, textureHeight);
81931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
82931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            // Icon is the right size, no need to change it
83931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return icon;
84931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        } else {
85931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            // Icon is too small, render to a larger bitmap
86931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            return createIconBitmap(new BitmapDrawable(icon), context);
87931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka        }
88931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    }
89931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka
90931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka    /**
91931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka     * Returns a bitmap suitable for the all apps view.
926665c0f1fec6ec0962896622986340081df992e7Joe Onorato     */
930589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    static Bitmap createIconBitmap(Drawable icon, Context context) {
946665c0f1fec6ec0962896622986340081df992e7Joe Onorato        synchronized (sCanvas) { // we share the statics :-(
956665c0f1fec6ec0962896622986340081df992e7Joe Onorato            if (sIconWidth == -1) {
966665c0f1fec6ec0962896622986340081df992e7Joe Onorato                initStatics(context);
976665c0f1fec6ec0962896622986340081df992e7Joe Onorato            }
986665c0f1fec6ec0962896622986340081df992e7Joe Onorato
996665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int width = sIconWidth;
1006665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int height = sIconHeight;
1016665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1026665c0f1fec6ec0962896622986340081df992e7Joe Onorato            if (icon instanceof PaintDrawable) {
1036665c0f1fec6ec0962896622986340081df992e7Joe Onorato                PaintDrawable painter = (PaintDrawable) icon;
1046665c0f1fec6ec0962896622986340081df992e7Joe Onorato                painter.setIntrinsicWidth(width);
1056665c0f1fec6ec0962896622986340081df992e7Joe Onorato                painter.setIntrinsicHeight(height);
1066665c0f1fec6ec0962896622986340081df992e7Joe Onorato            } else if (icon instanceof BitmapDrawable) {
1076665c0f1fec6ec0962896622986340081df992e7Joe Onorato                // Ensure the bitmap has a density.
1086665c0f1fec6ec0962896622986340081df992e7Joe Onorato                BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
1096665c0f1fec6ec0962896622986340081df992e7Joe Onorato                Bitmap bitmap = bitmapDrawable.getBitmap();
1106665c0f1fec6ec0962896622986340081df992e7Joe Onorato                if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
1116665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
1126665c0f1fec6ec0962896622986340081df992e7Joe Onorato                }
1136665c0f1fec6ec0962896622986340081df992e7Joe Onorato            }
1146665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int sourceWidth = icon.getIntrinsicWidth();
1156665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int sourceHeight = icon.getIntrinsicHeight();
1166665c0f1fec6ec0962896622986340081df992e7Joe Onorato
117931dc9779dab5071efc21163647f5f004991bfb3Michael Jurka            if (sourceWidth > 0 && sourceHeight > 0) {
1186665c0f1fec6ec0962896622986340081df992e7Joe Onorato                // There are intrinsic sizes.
11989911d2450f5db92d79e3bcefa9d324e9e8c4900Romain Guy                if (width < sourceWidth || height < sourceHeight) {
1206665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    // It's too big, scale it down.
1216665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    final float ratio = (float) sourceWidth / sourceHeight;
1226665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    if (sourceWidth > sourceHeight) {
1236665c0f1fec6ec0962896622986340081df992e7Joe Onorato                        height = (int) (width / ratio);
1246665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    } else if (sourceHeight > sourceWidth) {
1256665c0f1fec6ec0962896622986340081df992e7Joe Onorato                        width = (int) (height * ratio);
1266665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    }
1276665c0f1fec6ec0962896622986340081df992e7Joe Onorato                } else if (sourceWidth < width && sourceHeight < height) {
1284b825dcd5f64a5ebb60271844fbc5257374422bcWinson Chung                    // Don't scale up the icon
1296665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    width = sourceWidth;
13089911d2450f5db92d79e3bcefa9d324e9e8c4900Romain Guy                    height = sourceHeight;
1316665c0f1fec6ec0962896622986340081df992e7Joe Onorato                }
1326665c0f1fec6ec0962896622986340081df992e7Joe Onorato            }
1336665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1346665c0f1fec6ec0962896622986340081df992e7Joe Onorato            // no intrinsic size --> use default size
1356665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int textureWidth = sIconTextureWidth;
1366665c0f1fec6ec0962896622986340081df992e7Joe Onorato            int textureHeight = sIconTextureHeight;
1376665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1386665c0f1fec6ec0962896622986340081df992e7Joe Onorato            final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
1396665c0f1fec6ec0962896622986340081df992e7Joe Onorato                    Bitmap.Config.ARGB_8888);
1406665c0f1fec6ec0962896622986340081df992e7Joe Onorato            final Canvas canvas = sCanvas;
1416665c0f1fec6ec0962896622986340081df992e7Joe Onorato            canvas.setBitmap(bitmap);
1426665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1436665c0f1fec6ec0962896622986340081df992e7Joe Onorato            final int left = (textureWidth-width) / 2;
1446665c0f1fec6ec0962896622986340081df992e7Joe Onorato            final int top = (textureHeight-height) / 2;
1456665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1466665c0f1fec6ec0962896622986340081df992e7Joe Onorato            if (false) {
1476665c0f1fec6ec0962896622986340081df992e7Joe Onorato                // draw a big box for the icon for debugging
1486665c0f1fec6ec0962896622986340081df992e7Joe Onorato                canvas.drawColor(sColors[sColorIndex]);
1496665c0f1fec6ec0962896622986340081df992e7Joe Onorato                if (++sColorIndex >= sColors.length) sColorIndex = 0;
1506665c0f1fec6ec0962896622986340081df992e7Joe Onorato                Paint debugPaint = new Paint();
1516665c0f1fec6ec0962896622986340081df992e7Joe Onorato                debugPaint.setColor(0xffcccc00);
1526665c0f1fec6ec0962896622986340081df992e7Joe Onorato                canvas.drawRect(left, top, left+width, top+height, debugPaint);
1536665c0f1fec6ec0962896622986340081df992e7Joe Onorato            }
1546665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1556665c0f1fec6ec0962896622986340081df992e7Joe Onorato            sOldBounds.set(icon.getBounds());
1566665c0f1fec6ec0962896622986340081df992e7Joe Onorato            icon.setBounds(left, top, left+width, top+height);
1576665c0f1fec6ec0962896622986340081df992e7Joe Onorato            icon.draw(canvas);
1586665c0f1fec6ec0962896622986340081df992e7Joe Onorato            icon.setBounds(sOldBounds);
159aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen            canvas.setBitmap(null);
1606665c0f1fec6ec0962896622986340081df992e7Joe Onorato
1616665c0f1fec6ec0962896622986340081df992e7Joe Onorato            return bitmap;
1626665c0f1fec6ec0962896622986340081df992e7Joe Onorato        }
1636665c0f1fec6ec0962896622986340081df992e7Joe Onorato    }
1646665c0f1fec6ec0962896622986340081df992e7Joe Onorato
165c61cff9299a54599c2b214de7c819b6be0412f4eJoe Onorato    static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
166eb8325a9e24cda47720152e1e95f5d86ab11f200Joe Onorato            boolean pressed, Bitmap src) {
1671291a8c236c84451321438cb68855f6f2eb24959Joe Onorato        synchronized (sCanvas) { // we share the statics :-(
1681291a8c236c84451321438cb68855f6f2eb24959Joe Onorato            if (sIconWidth == -1) {
1691291a8c236c84451321438cb68855f6f2eb24959Joe Onorato                // We can't have gotten to here without src being initialized, which
1701291a8c236c84451321438cb68855f6f2eb24959Joe Onorato                // comes from this file already.  So just assert.
1711291a8c236c84451321438cb68855f6f2eb24959Joe Onorato                //initStatics(context);
1721291a8c236c84451321438cb68855f6f2eb24959Joe Onorato                throw new RuntimeException("Assertion failed: Utilities not initialized");
1731291a8c236c84451321438cb68855f6f2eb24959Joe Onorato            }
1741291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
1751291a8c236c84451321438cb68855f6f2eb24959Joe Onorato            dest.drawColor(0, PorterDuff.Mode.CLEAR);
176b4ecab24dbff6e6dc9e7929655a55199fc32e8ddJason Sams
1771291a8c236c84451321438cb68855f6f2eb24959Joe Onorato            int[] xy = new int[2];
178cdd117935f51d66f6f212387300d851f81aa071fMike Reed            Bitmap mask = src.extractAlpha(sBlurPaint, xy);
1791291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
180cdd117935f51d66f6f212387300d851f81aa071fMike Reed            float px = (destWidth - src.getWidth()) / 2;
181cdd117935f51d66f6f212387300d851f81aa071fMike Reed            float py = (destHeight - src.getHeight()) / 2;
182c61cff9299a54599c2b214de7c819b6be0412f4eJoe Onorato            dest.drawBitmap(mask, px + xy[0], py + xy[1],
183eb8325a9e24cda47720152e1e95f5d86ab11f200Joe Onorato                    pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
1841291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
1851291a8c236c84451321438cb68855f6f2eb24959Joe Onorato            mask.recycle();
1861291a8c236c84451321438cb68855f6f2eb24959Joe Onorato        }
1871291a8c236c84451321438cb68855f6f2eb24959Joe Onorato    }
1886665c0f1fec6ec0962896622986340081df992e7Joe Onorato
18931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
19031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Returns a Bitmap representing the thumbnail of the specified Bitmap.
19131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * The size of the thumbnail is defined by the dimension
19231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * android.R.dimen.launcher_application_icon_size.
19331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
19431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param bitmap The bitmap to get a thumbnail of.
19531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param context The application's context.
19631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
19731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return A thumbnail for the specified bitmap or the bitmap itself if the
19831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *         thumbnail could not be created.
19931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
2019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        synchronized (sCanvas) { // we share the statics :-(
2029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (sIconWidth == -1) {
2036665c0f1fec6ec0962896622986340081df992e7Joe Onorato                initStatics(context);
2049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
20531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2060589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
2070589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                return bitmap;
2080589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            } else {
2090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato                return createIconBitmap(new BitmapDrawable(bitmap), context);
21031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
21131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
21231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
213bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato
21456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
21556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        synchronized (sCanvas) { // we share the statics :-(
21656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            if (sIconWidth == -1) {
21756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                initStatics(context);
21856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            }
21956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
22056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato                    Bitmap.Config.ARGB_8888);
22156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            final Canvas canvas = sCanvas;
22256d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            canvas.setBitmap(disabled);
22356d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
22456d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
22556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
226aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen            canvas.setBitmap(null);
227aaf473c2bb6329b3b09ed2e19de6aae26077050cAdam Cohen
22856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato            return disabled;
22956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        }
23056d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato    }
23156d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
2326665c0f1fec6ec0962896622986340081df992e7Joe Onorato    private static void initStatics(Context context) {
2336665c0f1fec6ec0962896622986340081df992e7Joe Onorato        final Resources resources = context.getResources();
2341291a8c236c84451321438cb68855f6f2eb24959Joe Onorato        final DisplayMetrics metrics = resources.getDisplayMetrics();
2351291a8c236c84451321438cb68855f6f2eb24959Joe Onorato        final float density = metrics.density;
2361291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
237c9a961952d1a057029874f8426b90181f6876034Michael Jurka        sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
2384b825dcd5f64a5ebb60271844fbc5257374422bcWinson Chung        sIconTextureWidth = sIconTextureHeight = sIconWidth;
2391291a8c236c84451321438cb68855f6f2eb24959Joe Onorato
240a4c0cb965575b7a0832d92bc0b33e06c7c66cb10Joe Onorato        sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
241eb8325a9e24cda47720152e1e95f5d86ab11f200Joe Onorato        sGlowColorPressedPaint.setColor(0xffffc300);
242eb8325a9e24cda47720152e1e95f5d86ab11f200Joe Onorato        sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
243c61cff9299a54599c2b214de7c819b6be0412f4eJoe Onorato        sGlowColorFocusedPaint.setColor(0xffff8e00);
244c61cff9299a54599c2b214de7c819b6be0412f4eJoe Onorato        sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
24556d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato
24656d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        ColorMatrix cm = new ColorMatrix();
24756d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        cm.setSaturation(0.2f);
24856d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
24956d8291af6a28c6ba64113120efdf84a785e816cJoe Onorato        sDisabledPaint.setAlpha(0x88);
2506665c0f1fec6ec0962896622986340081df992e7Joe Onorato    }
2516665c0f1fec6ec0962896622986340081df992e7Joe Onorato
252bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato    /** Only works for positive numbers. */
253bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato    static int roundToPow2(int n) {
254bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        int orig = n;
255bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        n >>= 1;
256bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        int mask = 0x8000000;
257bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        while (mask != 0 && (n & mask) == 0) {
258bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato            mask >>= 1;
259bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        }
260bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        while (mask != 0) {
261bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato            n |= mask;
262bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato            mask >>= 1;
263bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        }
264bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        n += 1;
265bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        if (n != orig) {
266bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato            n <<= 1;
267bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        }
268bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato        return n;
269bf15cb44cc8e5ea9a19ab8aaee8e02c96bd6130cJoe Onorato    }
27097d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
27197d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    static int generateRandomId() {
27297d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        return new Random(System.currentTimeMillis()).nextInt(1 << 24);
27397d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung    }
27431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
275