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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
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; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 28 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 30 | import android.graphics.PaintFlagsDrawFilter; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 31 | import android.graphics.PorterDuff; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 32 | import android.graphics.Rect; |
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; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 37 | import android.view.View; |
| 38 | import android.view.ViewGroup; |
| 39 | |
| 40 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 41 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 42 | import com.android.launcher3.R; |
Romain Guy | edcce09 | 2010-03-04 13:03:17 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Various utilities shared amongst the Launcher's classes. |
| 46 | */ |
| 47 | final class Utilities { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 48 | @SuppressWarnings("unused") |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 49 | private static final String TAG = "Launcher.Utilities"; |
| 50 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 51 | private static int sIconWidth = -1; |
| 52 | private static int sIconHeight = -1; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 53 | private static int sIconTextureWidth = -1; |
| 54 | private static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 56 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 57 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 58 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 59 | private static final Paint sDisabledPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 60 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 61 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 62 | |
| 63 | static { |
| 64 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 65 | Paint.FILTER_BITMAP_FLAG)); |
| 66 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 67 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 68 | static int sColorIndex = 0; |
| 69 | |
| 70 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 71 | * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS |
| 72 | * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) |
| 73 | * to the proper size (48dp) |
| 74 | */ |
| 75 | static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 76 | int textureWidth = sIconTextureWidth; |
| 77 | int textureHeight = sIconTextureHeight; |
| 78 | int sourceWidth = icon.getWidth(); |
| 79 | int sourceHeight = icon.getHeight(); |
| 80 | if (sourceWidth > textureWidth && sourceHeight > textureHeight) { |
| 81 | // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) |
| 82 | return Bitmap.createBitmap(icon, |
| 83 | (sourceWidth - textureWidth) / 2, |
| 84 | (sourceHeight - textureHeight) / 2, |
| 85 | textureWidth, textureHeight); |
| 86 | } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { |
| 87 | // Icon is the right size, no need to change it |
| 88 | return icon; |
| 89 | } else { |
| 90 | // Icon is too small, render to a larger bitmap |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 91 | final Resources resources = context.getResources(); |
| 92 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 98 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 99 | static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 100 | synchronized (sCanvas) { // we share the statics :-( |
| 101 | if (sIconWidth == -1) { |
| 102 | initStatics(context); |
| 103 | } |
| 104 | |
| 105 | int width = sIconWidth; |
| 106 | int height = sIconHeight; |
| 107 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 108 | if (icon instanceof PaintDrawable) { |
| 109 | PaintDrawable painter = (PaintDrawable) icon; |
| 110 | painter.setIntrinsicWidth(width); |
| 111 | painter.setIntrinsicHeight(height); |
| 112 | } else if (icon instanceof BitmapDrawable) { |
| 113 | // Ensure the bitmap has a density. |
| 114 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 115 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 116 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 117 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 118 | } |
| 119 | } |
| 120 | int sourceWidth = icon.getIntrinsicWidth(); |
| 121 | int sourceHeight = icon.getIntrinsicHeight(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 122 | if (sourceWidth > 0 && sourceHeight > 0) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 123 | // There are intrinsic sizes. |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 124 | if (width < sourceWidth || height < sourceHeight) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 125 | // It's too big, scale it down. |
| 126 | final float ratio = (float) sourceWidth / sourceHeight; |
| 127 | if (sourceWidth > sourceHeight) { |
| 128 | height = (int) (width / ratio); |
| 129 | } else if (sourceHeight > sourceWidth) { |
| 130 | width = (int) (height * ratio); |
| 131 | } |
| 132 | } else if (sourceWidth < width && sourceHeight < height) { |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 133 | // Don't scale up the icon |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 134 | width = sourceWidth; |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 135 | height = sourceHeight; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | // no intrinsic size --> use default size |
| 140 | int textureWidth = sIconTextureWidth; |
| 141 | int textureHeight = sIconTextureHeight; |
| 142 | |
| 143 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 144 | Bitmap.Config.ARGB_8888); |
| 145 | final Canvas canvas = sCanvas; |
| 146 | canvas.setBitmap(bitmap); |
| 147 | |
| 148 | final int left = (textureWidth-width) / 2; |
| 149 | final int top = (textureHeight-height) / 2; |
| 150 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 151 | @SuppressWarnings("all") // suppress dead code warning |
| 152 | final boolean debug = false; |
| 153 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 154 | // draw a big box for the icon for debugging |
| 155 | canvas.drawColor(sColors[sColorIndex]); |
| 156 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 157 | Paint debugPaint = new Paint(); |
| 158 | debugPaint.setColor(0xffcccc00); |
| 159 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 160 | } |
| 161 | |
| 162 | sOldBounds.set(icon.getBounds()); |
| 163 | icon.setBounds(left, top, left+width, top+height); |
| 164 | icon.draw(canvas); |
| 165 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 166 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 167 | |
| 168 | return bitmap; |
| 169 | } |
| 170 | } |
| 171 | |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 172 | static void drawSelectedAllAppsBitmap(Canvas dest, int destWidth, int destHeight, |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 173 | boolean pressed, Bitmap src) { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 174 | synchronized (sCanvas) { // we share the statics :-( |
| 175 | if (sIconWidth == -1) { |
| 176 | // We can't have gotten to here without src being initialized, which |
| 177 | // comes from this file already. So just assert. |
| 178 | //initStatics(context); |
| 179 | throw new RuntimeException("Assertion failed: Utilities not initialized"); |
| 180 | } |
| 181 | |
| 182 | dest.drawColor(0, PorterDuff.Mode.CLEAR); |
Jason Sams | b4ecab2 | 2010-01-19 16:43:26 -0800 | [diff] [blame] | 183 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 184 | int[] xy = new int[2]; |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 185 | Bitmap mask = src.extractAlpha(sBlurPaint, xy); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 186 | |
Mike Reed | cdd1179 | 2009-10-29 17:27:55 -0400 | [diff] [blame] | 187 | float px = (destWidth - src.getWidth()) / 2; |
| 188 | float py = (destHeight - src.getHeight()) / 2; |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 189 | dest.drawBitmap(mask, px + xy[0], py + xy[1], |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 190 | pressed ? sGlowColorPressedPaint : sGlowColorFocusedPaint); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 191 | |
| 192 | mask.recycle(); |
| 193 | } |
| 194 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 195 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
| 198 | * The size of the thumbnail is defined by the dimension |
| 199 | * android.R.dimen.launcher_application_icon_size. |
| 200 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 201 | * @param bitmap The bitmap to get a thumbnail of. |
| 202 | * @param context The application's context. |
| 203 | * |
| 204 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 205 | * thumbnail could not be created. |
| 206 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 207 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 208 | synchronized (sCanvas) { // we share the statics :-( |
| 209 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 210 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 213 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 214 | return bitmap; |
| 215 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 216 | final Resources resources = context.getResources(); |
| 217 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 218 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 219 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 220 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 221 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 222 | static Bitmap drawDisabledBitmap(Bitmap bitmap, Context context) { |
| 223 | synchronized (sCanvas) { // we share the statics :-( |
| 224 | if (sIconWidth == -1) { |
| 225 | initStatics(context); |
| 226 | } |
| 227 | final Bitmap disabled = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), |
| 228 | Bitmap.Config.ARGB_8888); |
| 229 | final Canvas canvas = sCanvas; |
| 230 | canvas.setBitmap(disabled); |
| 231 | |
| 232 | canvas.drawBitmap(bitmap, 0.0f, 0.0f, sDisabledPaint); |
| 233 | |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 234 | canvas.setBitmap(null); |
| 235 | |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 236 | return disabled; |
| 237 | } |
| 238 | } |
| 239 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 240 | /** |
| 241 | * Given a coordinate relative to the descendant, find the coordinate in a parent view's |
| 242 | * coordinates. |
| 243 | * |
| 244 | * @param descendant The descendant to which the passed coordinate is relative. |
| 245 | * @param root The root view to make the coordinates relative to. |
| 246 | * @param coord The coordinate that we want mapped. |
| 247 | * @param includeRootScroll Whether or not to account for the scroll of the descendant: |
| 248 | * sometimes this is relevant as in a child's coordinates within the descendant. |
| 249 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 250 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 251 | * assumption fails, we will need to return a pair of scale factors. |
| 252 | */ |
| 253 | public static float getDescendantCoordRelativeToParent(View descendant, View root, |
| 254 | int[] coord, boolean includeRootScroll) { |
| 255 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 256 | |
| 257 | float[] pt = {coord[0], coord[1]}; |
| 258 | |
| 259 | View v = descendant; |
| 260 | while(v != root && v != null) { |
| 261 | ancestorChain.add(v); |
| 262 | v = (View) v.getParent(); |
| 263 | } |
| 264 | ancestorChain.add(root); |
| 265 | |
| 266 | float scale = 1.0f; |
| 267 | int count = ancestorChain.size(); |
| 268 | for (int i = 0; i < count; i++) { |
| 269 | View v0 = ancestorChain.get(i); |
| 270 | View v1 = i < count -1 ? ancestorChain.get(i + 1) : null; |
| 271 | |
| 272 | // For TextViews, scroll has a meaning which relates to the text position |
| 273 | // which is very strange... ignore the scroll. |
| 274 | if (v0 != descendant || includeRootScroll) { |
| 275 | pt[0] -= v0.getScrollX(); |
| 276 | pt[1] -= v0.getScrollY(); |
| 277 | } |
| 278 | |
| 279 | v0.getMatrix().mapPoints(pt); |
| 280 | pt[0] += v0.getLeft(); |
| 281 | pt[1] += v0.getTop(); |
| 282 | scale *= v0.getScaleX(); |
| 283 | } |
| 284 | |
| 285 | coord[0] = (int) Math.round(pt[0]); |
| 286 | coord[1] = (int) Math.round(pt[1]); |
| 287 | return scale; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}. |
| 292 | */ |
| 293 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 294 | int[] coord) { |
| 295 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 296 | |
| 297 | float[] pt = {coord[0], coord[1]}; |
| 298 | |
| 299 | View v = descendant; |
| 300 | while(v != root) { |
| 301 | ancestorChain.add(v); |
| 302 | v = (View) v.getParent(); |
| 303 | } |
| 304 | ancestorChain.add(root); |
| 305 | |
| 306 | float scale = 1.0f; |
| 307 | Matrix inverse = new Matrix(); |
| 308 | int count = ancestorChain.size(); |
| 309 | for (int i = count - 1; i >= 0; i--) { |
| 310 | View ancestor = ancestorChain.get(i); |
| 311 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 312 | |
| 313 | pt[0] += ancestor.getScrollX(); |
| 314 | pt[1] += ancestor.getScrollY(); |
| 315 | |
| 316 | if (next != null) { |
| 317 | pt[0] -= next.getLeft(); |
| 318 | pt[1] -= next.getTop(); |
| 319 | next.getMatrix().invert(inverse); |
| 320 | inverse.mapPoints(pt); |
| 321 | scale *= next.getScaleX(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | coord[0] = (int) Math.round(pt[0]); |
| 326 | coord[1] = (int) Math.round(pt[1]); |
| 327 | return scale; |
| 328 | } |
| 329 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 330 | private static void initStatics(Context context) { |
| 331 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 332 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 333 | final float density = metrics.density; |
| 334 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 335 | sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 336 | sIconTextureWidth = sIconTextureHeight = sIconWidth; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 337 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 338 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 339 | sGlowColorPressedPaint.setColor(0xffffc300); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 340 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 341 | |
| 342 | ColorMatrix cm = new ColorMatrix(); |
| 343 | cm.setSaturation(0.2f); |
| 344 | sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); |
| 345 | sDisabledPaint.setAlpha(0x88); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 348 | /** Only works for positive numbers. */ |
| 349 | static int roundToPow2(int n) { |
| 350 | int orig = n; |
| 351 | n >>= 1; |
| 352 | int mask = 0x8000000; |
| 353 | while (mask != 0 && (n & mask) == 0) { |
| 354 | mask >>= 1; |
| 355 | } |
| 356 | while (mask != 0) { |
| 357 | n |= mask; |
| 358 | mask >>= 1; |
| 359 | } |
| 360 | n += 1; |
| 361 | if (n != orig) { |
| 362 | n <<= 1; |
| 363 | } |
| 364 | return n; |
| 365 | } |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 366 | |
| 367 | static int generateRandomId() { |
| 368 | return new Random(System.currentTimeMillis()).nextInt(1 << 24); |
| 369 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 370 | } |