Home | History | Annotate | Download | only in policy
      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.server.policy;
     18 
     19 import android.graphics.ColorFilter;
     20 import android.graphics.ColorMatrixColorFilter;
     21 import android.graphics.drawable.BitmapDrawable;
     22 import android.graphics.drawable.Drawable;
     23 import android.graphics.drawable.PaintDrawable;
     24 import android.graphics.drawable.StateListDrawable;
     25 import android.graphics.Bitmap;
     26 import android.graphics.BlurMaskFilter;
     27 import android.graphics.Canvas;
     28 import android.graphics.ColorMatrix;
     29 import android.graphics.Paint;
     30 import android.graphics.PaintFlagsDrawFilter;
     31 import android.graphics.PorterDuff;
     32 import android.graphics.Rect;
     33 import android.graphics.TableMaskFilter;
     34 import android.util.DisplayMetrics;
     35 import android.util.TypedValue;
     36 import android.content.res.Resources;
     37 import android.content.Context;
     38 
     39 /**
     40  * Various utilities shared amongst the Launcher's classes.
     41  */
     42 public final class IconUtilities {
     43 
     44     private int mIconWidth = -1;
     45     private int mIconHeight = -1;
     46     private int mIconTextureWidth = -1;
     47     private int mIconTextureHeight = -1;
     48 
     49     private final Rect mOldBounds = new Rect();
     50     private final Canvas mCanvas = new Canvas();
     51     private final DisplayMetrics mDisplayMetrics;
     52 
     53     private ColorFilter mDisabledColorFilter;
     54 
     55     public IconUtilities(Context context) {
     56         final Resources resources = context.getResources();
     57         DisplayMetrics metrics = mDisplayMetrics = resources.getDisplayMetrics();
     58         final float density = metrics.density;
     59         final float blurPx = 5 * density;
     60 
     61         mIconWidth = mIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
     62         mIconTextureWidth = mIconTextureHeight = mIconWidth + (int)(blurPx*2);
     63         mCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
     64                 Paint.FILTER_BITMAP_FLAG));
     65     }
     66 
     67     /**
     68      * Returns a bitmap suitable for the all apps view.  The bitmap will be a power
     69      * of two sized ARGB_8888 bitmap that can be used as a gl texture.
     70      */
     71     public Bitmap createIconBitmap(Drawable icon) {
     72         int width = mIconWidth;
     73         int height = mIconHeight;
     74 
     75         if (icon instanceof PaintDrawable) {
     76             PaintDrawable painter = (PaintDrawable) icon;
     77             painter.setIntrinsicWidth(width);
     78             painter.setIntrinsicHeight(height);
     79         } else if (icon instanceof BitmapDrawable) {
     80             // Ensure the bitmap has a density.
     81             BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
     82             Bitmap bitmap = bitmapDrawable.getBitmap();
     83             if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
     84                 bitmapDrawable.setTargetDensity(mDisplayMetrics);
     85             }
     86         }
     87         int sourceWidth = icon.getIntrinsicWidth();
     88         int sourceHeight = icon.getIntrinsicHeight();
     89 
     90         if (sourceWidth > 0 && sourceHeight > 0) {
     91             // There are intrinsic sizes.
     92             if (width < sourceWidth || height < sourceHeight) {
     93                 // It's too big, scale it down.
     94                 final float ratio = (float) sourceWidth / sourceHeight;
     95                 if (sourceWidth > sourceHeight) {
     96                     height = (int) (width / ratio);
     97                 } else if (sourceHeight > sourceWidth) {
     98                     width = (int) (height * ratio);
     99                 }
    100             } else if (sourceWidth < width && sourceHeight < height) {
    101                 // It's small, use the size they gave us.
    102                 width = sourceWidth;
    103                 height = sourceHeight;
    104             }
    105         }
    106 
    107         // no intrinsic size --> use default size
    108         int textureWidth = mIconTextureWidth;
    109         int textureHeight = mIconTextureHeight;
    110 
    111         final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
    112                 Bitmap.Config.ARGB_8888);
    113         final Canvas canvas = mCanvas;
    114         canvas.setBitmap(bitmap);
    115 
    116         final int left = (textureWidth-width) / 2;
    117         final int top = (textureHeight-height) / 2;
    118 
    119         mOldBounds.set(icon.getBounds());
    120         icon.setBounds(left, top, left+width, top+height);
    121         icon.draw(canvas);
    122         icon.setBounds(mOldBounds);
    123 
    124         return bitmap;
    125     }
    126 
    127     public ColorFilter getDisabledColorFilter() {
    128         if (mDisabledColorFilter != null) {
    129             return mDisabledColorFilter;
    130         }
    131         ColorMatrix brightnessMatrix = new ColorMatrix();
    132         float brightnessF = 0.5f;
    133         int brightnessI = (int) (255 * brightnessF);
    134         // Brightness: C-new = C-old*(1-amount) + amount
    135         float scale = 1f - brightnessF;
    136         float[] mat = brightnessMatrix.getArray();
    137         mat[0] = scale;
    138         mat[6] = scale;
    139         mat[12] = scale;
    140         mat[4] = brightnessI;
    141         mat[9] = brightnessI;
    142         mat[14] = brightnessI;
    143 
    144         ColorMatrix filterMatrix = new ColorMatrix();
    145         filterMatrix.setSaturation(0);
    146         filterMatrix.preConcat(brightnessMatrix);
    147 
    148         mDisabledColorFilter = new ColorMatrixColorFilter(filterMatrix);
    149         return mDisabledColorFilter;
    150     }
    151 }
    152