The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 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 | |
Joe Onorato | a590252 | 2009-07-30 13:37:37 -0700 | [diff] [blame] | 17 | package com.android.launcher2; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 18 | |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 19 | import java.util.Random; |
| 20 | |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 24 | import android.graphics.BlurMaskFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 25 | import android.graphics.Canvas; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 26 | import android.graphics.ColorMatrix; |
| 27 | import android.graphics.ColorMatrixColorFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 28 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 29 | import android.graphics.PaintFlagsDrawFilter; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 30 | import android.graphics.PorterDuff; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.graphics.Rect; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 32 | import android.graphics.TableMaskFilter; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 33 | import android.graphics.drawable.BitmapDrawable; |
| 34 | import android.graphics.drawable.Drawable; |
| 35 | import android.graphics.drawable.PaintDrawable; |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 36 | import android.util.DisplayMetrics; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 38 | import com.android.launcher.R; |
| 39 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Various utilities shared amongst the Launcher's classes. |
| 42 | */ |
| 43 | final class Utilities { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 44 | @SuppressWarnings("unused") |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 45 | private static final String TAG = "Launcher.Utilities"; |
| 46 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 47 | private static int sIconWidth = -1; |
| 48 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 49 | private static int sIconTextureWidth = -1; |
| 50 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 52 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 53 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 54 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 55 | private static final Paint sDisabledPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 56 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 57 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 58 | |
| 59 | static { |
| 60 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 61 | Paint.FILTER_BITMAP_FLAG)); |
| 62 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 63 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 64 | static int sColorIndex = 0; |
| 65 | |
| 66 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 67 | * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS |
| 68 | * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) |
| 69 | * to the proper size (48dp) |
| 70 | */ |
| 71 | static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 72 | int textureWidth = sIconTextureWidth; |
| 73 | int textureHeight = sIconTextureHeight; |
| 74 | int sourceWidth = icon.getWidth(); |
| 75 | int sourceHeight = icon.getHeight(); |
| 76 | if (sourceWidth > textureWidth && sourceHeight > textureHeight) { |
| 77 | // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) |
| 78 | return Bitmap.createBitmap(icon, |
| 79 | (sourceWidth - textureWidth) / 2, |
| 80 | (sourceHeight - textureHeight) / 2, |
| 81 | textureWidth, textureHeight); |
| 82 | } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { |
| 83 | // Icon is the right size, no need to change it |
| 84 | return icon; |
| 85 | } else { |
| 86 | // Icon is too small, render to a larger bitmap |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 87 | final Resources resources = context.getResources(); |
| 88 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 94 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 95 | static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 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 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 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(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 118 | if (sourceWidth > 0 && sourceHeight > 0) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 119 | // There are intrinsic sizes. |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 120 | if (width < sourceWidth || height < sourceHeight) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 121 | // It's too big, scale it down. |
| 122 | final float ratio = (float) sourceWidth / sourceHeight; |
| 123 | if (sourceWidth > sourceHeight) { |
| 124 | height = (int) (width / ratio); |
| 125 | } else if (sourceHeight > sourceWidth) { |
| 126 | width = (int) (height * ratio); |
| 127 | } |
| 128 | } else if (sourceWidth < width && sourceHeight < height) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 129 | // Don't scale up the icon |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 130 | width = sourceWidth; |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 131 | height = sourceHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | // no intrinsic size --> use default size |
| 136 | int textureWidth = sIconTextureWidth; |
| 137 | int textureHeight = sIconTextureHeight; |
| 138 | |
| 139 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 140 | Bitmap.Config.ARGB_8888); |
| 141 | final Canvas canvas = sCanvas; |
| 142 | canvas.setBitmap(bitmap); |
| 143 | |
| 144 | final int left = (textureWidth-width) / 2; |
| 145 | final int top = (textureHeight-height) / 2; |
| 146 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 147 | @SuppressWarnings("all") // suppress dead code warning |
| 148 | final boolean debug = false; |
| 149 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 150 | // draw a big box for the icon for debugging |
| 151 | canvas.drawColor(sColors[sColorIndex]); |
| 152 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 153 | Paint debugPaint = new Paint(); |
| 154 | debugPaint.setColor(0xffcccc00); |
| 155 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 156 | } |
| 157 | |
| 158 | sOldBounds.set(icon.getBounds()); |
| 159 | icon.setBounds(left, top, left+width, top+height); |
| 160 | icon.draw(canvas); |
| 161 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 162 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 163 | |
| 164 | return bitmap; |
| 165 | } |
| 166 | } |
| 167 | |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 168 | static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 169 | boolean pressed, Bitmap src) { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 170 | synchronized (sCanvas) { // we share the statics :-( |
| 171 | if (sIconWidth == -1) { |
| 172 | // We can't have gotten to here without src being initialized, which |
| 173 | // comes from this file already. So just assert. |
| 174 | //initStatics(context); |
| 175 | throw new RuntimeException("Assertion failed: Utilities not initialized"); |
| 176 | } |
| 177 | |
| 178 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 179 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 180 | int[] xy = new int[2]; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 181 | Bitmap mask = src.extractAlpha(sBlurPaint, xy); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 182 | |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 183 | float px = (destWidth - src.getWidth()) / 2; |
| 184 | float py = (destHeight - src.getHeight()) / 2; |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 185 | dest.drawBitmap(mask, px + xy[0], py + xy[1], |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 186 | pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 187 | |
| 188 | mask.recycle(); |
| 189 | } |
| 190 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 192 | /** |
| 193 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
| 194 | * The size of the thumbnail is defined by the dimension |
| 195 | * android.R.dimen.launcher_application_icon_size. |
| 196 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 197 | * @param bitmap The bitmap to get a thumbnail of. |
| 198 | * @param context The application's context. |
| 199 | * |
| 200 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 201 | * thumbnail could not be created. |
| 202 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 203 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 204 | synchronized (sCanvas) { // we share the statics :-( |
| 205 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 206 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 209 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 210 | return bitmap; |
| 211 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 212 | final Resources resources = context.getResources(); |
| 213 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 214 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 215 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 216 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 217 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 218 | static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) { |
| 219 | synchronized (sCanvas) { // we share the statics :-( |
| 220 | if (sIconWidth == -1) { |
| 221 | initStatics(context); |
| 222 | } |
| 223 | final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), |
| 224 | Bitmap.Config.ARGB_8888); |
| 225 | final Canvas canvas = sCanvas; |
| 226 | canvas.setBitmap(disabled); |
| 227 | |
| 228 | canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint); |
| 229 | |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 230 | canvas.setBitmap(null); |
| 231 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 232 | return disabled; |
| 233 | } |
| 234 | } |
| 235 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 236 | private static void initStatics(Context context) { |
| 237 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 238 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 239 | final float density = metrics.density; |
| 240 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 241 | sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 242 | sIconTextureWidth = sIconTextureHeight = sIconWidth; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 243 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 244 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 245 | sGlowColorPressedPaint.setColor(0xffffc300); |
| 246 | sGlowColorPressedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 247 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
| 248 | sGlowColorFocusedPaint.setMaskFilter(TableMaskFilter.CreateClipTable(0, 30)); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 249 | |
| 250 | ColorMatrix cm = new ColorMatrix(); |
| 251 | cm.setSaturation(0.2f); |
| 252 | sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); |
| 253 | sDisabledPaint.setAlpha(0x88); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 256 | /** Only works for positive numbers. */ |
| 257 | static int roundToPow2(int n) { |
| 258 | int orig = n; |
| 259 | n >>= 1; |
| 260 | int mask = 0x8000000; |
| 261 | while (mask != 0 && (n & mask) == 0) { |
| 262 | mask >>= 1; |
| 263 | } |
| 264 | while (mask != 0) { |
| 265 | n |= mask; |
| 266 | mask >>= 1; |
| 267 | } |
| 268 | n += 1; |
| 269 | if (n != orig) { |
| 270 | n <<= 1; |
| 271 | } |
| 272 | return n; |
| 273 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 274 | |
| 275 | static int generateRandomId() { |
| 276 | return new Random(System.currentTimeMillis()).nextInt(1 << 24); |
| 277 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 278 | } |