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