Home | History | Annotate | Download | only in launcher2
      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 
     17 package com.android.launcher2;
     18 
     19 import java.util.Random;
     20 
     21 import android.content.Context;
     22 import android.content.res.Resources;
     23 import android.graphics.Bitmap;
     24 import android.graphics.BlurMaskFilter;
     25 import android.graphics.Canvas;
     26 import android.graphics.ColorMatrix;
     27 import android.graphics.ColorMatrixColorFilter;
     28 import android.graphics.Paint;
     29 import android.graphics.PaintFlagsDrawFilter;
     30 import android.graphics.PorterDuff;
     31 import android.graphics.Rect;
     32 import android.graphics.TableMaskFilter;
     33 import android.graphics.drawable.BitmapDrawable;
     34 import android.graphics.drawable.Drawable;
     35 import android.graphics.drawable.PaintDrawable;
     36 import android.util.DisplayMetrics;
     37 
     38 import com.android.launcher.R;
     39 
     40 /**
     41  * Various utilities shared amongst the Launcher's classes.
     42  */
     43 final 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 sBlurPaint = new Paint();
     52     private static final Paint sGlowColorPressedPaint = new Paint();
     53     private static final Paint sGlowColorFocusedPaint = new Paint();
     54     private static final Paint sDisabledPaint = new Paint();
     55     private static final Rect sOldBounds = new Rect();
     56     private static final Canvas sCanvas = new Canvas();
     57 
     58     static {
     59         sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
     60                 Paint.FILTER_BITMAP_FLAG));
     61     }
     62     static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff };
     63     static int sColorIndex = 0;
     64 
     65     /**
     66      * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS
     67      * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size)
     68      * to the proper size (48dp)
     69      */
     70     static Bitmap createIconBitmap(Bitmap icon, Context context) {
     71         int textureWidth = sIconTextureWidth;
     72         int textureHeight = sIconTextureHeight;
     73         int sourceWidth = icon.getWidth();
     74         int sourceHeight = icon.getHeight();
     75         if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
     76             // Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
     77             return Bitmap.createBitmap(icon,
     78                     (sourceWidth - textureWidth) / 2,
     79                     (sourceHeight - textureHeight) / 2,
     80                     textureWidth, textureHeight);
     81         } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
     82             // Icon is the right size, no need to change it
     83             return icon;
     84         } else {
     85             // Icon is too small, render to a larger bitmap
     86             return createIconBitmap(new BitmapDrawable(icon), context);
     87         }
     88     }
     89 
     90     /**
     91      * Returns a bitmap suitable for the all apps view.
     92      */
     93     static Bitmap createIconBitmap(Drawable icon, Context context) {
     94         synchronized (sCanvas) { // we share the statics :-(
     95             if (sIconWidth == -1) {
     96                 initStatics(context);
     97             }
     98 
     99             int width = sIconWidth;
    100             int height = sIconHeight;
    101 
    102             if (icon instanceof PaintDrawable) {
    103                 PaintDrawable painter = (PaintDrawable) icon;
    104                 painter.setIntrinsicWidth(width);
    105                 painter.setIntrinsicHeight(height);
    106             } else if (icon instanceof BitmapDrawable) {
    107                 // Ensure the bitmap has a density.
    108                 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
    109                 Bitmap bitmap = bitmapDrawable.getBitmap();
    110                 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
    111                     bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
    112                 }
    113             }
    114             int sourceWidth = icon.getIntrinsicWidth();
    115             int sourceHeight = icon.getIntrinsicHeight();
    116 
    117             if (sourceWidth > 0 && sourceHeight > 0) {
    118                 // There are intrinsic sizes.
    119                 if (width < sourceWidth || height < sourceHeight) {
    120                     // It's too big, scale it down.
    121                     final float ratio = (float) sourceWidth / sourceHeight;
    122                     if (sourceWidth > sourceHeight) {
    123                         height = (int) (width / ratio);
    124                     } else if (sourceHeight > sourceWidth) {
    125                         width = (int) (height * ratio);
    126                     }
    127                 } else if (sourceWidth < width && sourceHeight < height) {
    128                     // Don't scale up the icon
    129                     width = sourceWidth;
    130                     height = sourceHeight;
    131                 }
    132             }
    133 
    134             // no intrinsic size --> use default size
    135             int textureWidth = sIconTextureWidth;
    136             int textureHeight = sIconTextureHeight;
    137 
    138             final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
    139                     Bitmap.Config.ARGB_8888);
    140             final Canvas canvas = sCanvas;
    141             canvas.setBitmap(bitmap);
    142 
    143             final int left = (textureWidth-width) / 2;
    144             final int top = (textureHeight-height) / 2;
    145 
    146             if (false) {
    147                 // draw a big box for the icon for debugging
    148                 canvas.drawColor(sColors[sColorIndex]);
    149                 if (++sColorIndex >= sColors.length) sColorIndex = 0;
    150                 Paint debugPaint = new Paint();
    151                 debugPaint.setColor(0xffcccc00);
    152                 canvas.drawRect(left, top, left+width, top+height, debugPaint);
    153             }
    154 
    155             sOldBounds.set(icon.getBounds());
    156             icon.setBounds(left, top, left+width, top+height);
    157             icon.draw(canvas);
    158             icon.setBounds(sOldBounds);
    159             canvas.setBitmap(null);
    160 
    161             return bitmap;
    162         }
    163     }
    164 
    165     static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight,
    166             boolean pressed, Bitmap src) {
    167         synchronized (sCanvas) { // we share the statics :-(
    168             if (sIconWidth == -1) {
    169                 // We can't have gotten to here without src being initialized, which
    170                 // comes from this file already.  So just assert.
    171                 //initStatics(context);
    172                 throw new RuntimeException("Assertion failed: Utilities not initialized");
    173             }
    174 
    175             dest.drawColor(0, PorterDuff.Mode.CLEAR);
    176 
    177             int[] xy = new int[2];
    178             Bitmap mask = src.extractAlpha(sBlurPaint, xy);
    179 
    180             float px = (destWidth - src.getWidth()) / 2;
    181             float py = (destHeight - src.getHeight()) / 2;
    182             dest.drawBitmap(mask, px + xy[0], py + xy[1],
    183                     pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint);
    184 
    185             mask.recycle();
    186         }
    187     }
    188 
    189     /**
    190      * Returns a Bitmap representing the thumbnail of the specified Bitmap.
    191      * The size of the thumbnail is defined by the dimension
    192      * android.R.dimen.launcher_application_icon_size.
    193      *
    194      * @param bitmap The bitmap to get a thumbnail of.
    195      * @param context The application's context.
    196      *
    197      * @return A thumbnail for the specified bitmap or the bitmap itself if the
    198      *         thumbnail could not be created.
    199      */
    200     static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) {
    201         synchronized (sCanvas) { // we share the statics :-(
    202             if (sIconWidth == -1) {
    203                 initStatics(context);
    204             }
    205 
    206             if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) {
    207                 return bitmap;
    208             } else {
    209                 return createIconBitmap(new BitmapDrawable(bitmap), context);
    210             }
    211         }
    212     }
    213 
    214     static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) {
    215         synchronized (sCanvas) { // we share the statics :-(
    216             if (sIconWidth == -1) {
    217                 initStatics(context);
    218             }
    219             final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
    220                     Bitmap.Config.ARGB_8888);
    221             final Canvas canvas = sCanvas;
    222             canvas.setBitmap(disabled);
    223 
    224             canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint);
    225 
    226             canvas.setBitmap(null);
    227 
    228             return disabled;
    229         }
    230     }
    231 
    232     private static void initStatics(Context context) {
    233         final Resources resources = context.getResources();
    234         final DisplayMetrics metrics = resources.getDisplayMetrics();
    235         final float density = metrics.density;
    236 
    237         sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size);
    238         sIconTextureWidth = sIconTextureHeight = sIconWidth;
    239 
    240         sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL));
    241         sGlowColorPressedPaint.setColor(0xffffc300);
    242         sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
    243         sGlowColorFocusedPaint.setColor(0xffff8e00);
    244         sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30));
    245 
    246         ColorMatrix cm = new ColorMatrix();
    247         cm.setSaturation(0.2f);
    248         sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm));
    249         sDisabledPaint.setAlpha(0x88);
    250     }
    251 
    252     /** Only works for positive numbers. */
    253     static int roundToPow2(int n) {
    254         int orig = n;
    255         n >>= 1;
    256         int mask = 0x8000000;
    257         while (mask != 0 && (n & mask) == 0) {
    258             mask >>= 1;
    259         }
    260         while (mask != 0) {
    261             n |= mask;
    262             mask >>= 1;
    263         }
    264         n += 1;
    265         if (n != orig) {
    266             n <<= 1;
    267         }
    268         return n;
    269     }
    270 
    271     static int generateRandomId() {
    272         return new Random(System.currentTimeMillis()).nextInt(1 << 24);
    273     }
    274 }
    275