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 | |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.ActivityNotFoundException; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 22 | import android.content.Intent; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 25 | import android.graphics.BlurMaskFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 26 | import android.graphics.Canvas; |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 27 | import android.graphics.ColorMatrix; |
| 28 | import android.graphics.ColorMatrixColorFilter; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 29 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 31 | import android.graphics.PaintFlagsDrawFilter; |
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; |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame^] | 36 | import android.os.Build; |
Dianne Hackborn | 32ce7f1 | 2009-07-22 21:56:50 -0700 | [diff] [blame] | 37 | import android.util.DisplayMetrics; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 38 | import android.util.Log; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 39 | import android.view.View; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 40 | import android.widget.Toast; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 41 | |
| 42 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Various utilities shared amongst the Launcher's classes. |
| 46 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 47 | public final class Utilities { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 48 | private static final String TAG = "Launcher.Utilities"; |
| 49 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 50 | private static int sIconWidth = -1; |
| 51 | private static int sIconHeight = -1; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 52 | public static int sIconTextureWidth = -1; |
| 53 | public static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 54 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 55 | private static final Paint sBlurPaint = new Paint(); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 56 | private static final Paint sGlowColorPressedPaint = new Paint(); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 57 | private static final Paint sGlowColorFocusedPaint = new Paint(); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 58 | private static final Paint sDisabledPaint = new Paint(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 60 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 61 | |
| 62 | static { |
| 63 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 64 | Paint.FILTER_BITMAP_FLAG)); |
| 65 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 66 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 67 | static int sColorIndex = 0; |
| 68 | |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 69 | |
| 70 | // To turn on these properties, type |
| 71 | // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS] |
| 72 | static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate"; |
| 73 | public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY); |
| 74 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 75 | /** |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 76 | * Returns a FastBitmapDrawable with the icon, accurately sized. |
| 77 | */ |
| 78 | static Drawable createIconDrawable(Bitmap icon) { |
| 79 | FastBitmapDrawable d = new FastBitmapDrawable(icon); |
Winson Chung | 5400049 | 2013-10-14 16:29:29 -0700 | [diff] [blame] | 80 | d.setFilterBitmap(true); |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 81 | resizeIconDrawable(d); |
| 82 | return d; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Resizes an icon drawable to the correct icon size. |
| 87 | */ |
| 88 | static void resizeIconDrawable(Drawable icon) { |
| 89 | icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight); |
| 90 | } |
| 91 | |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 92 | private static boolean isPropertyEnabled(String propertyName) { |
| 93 | return Log.isLoggable(propertyName, Log.VERBOSE); |
| 94 | } |
| 95 | |
| 96 | public static boolean isRotationEnabled(Context c) { |
| 97 | boolean enableRotation = sForceEnableRotation || |
| 98 | c.getResources().getBoolean(R.bool.allow_rotation); |
| 99 | return enableRotation; |
| 100 | } |
| 101 | |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 102 | /** |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame^] | 103 | * Indicates if the device is running LMP or not. |
| 104 | * TODO(sansid): Change the check to a VERSION_CODES code check once we have a version for L. |
| 105 | */ |
| 106 | public static boolean isLmp() { |
| 107 | return "L".equals(Build.VERSION.CODENAME); |
| 108 | } |
| 109 | |
| 110 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 111 | * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS |
| 112 | * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) |
| 113 | * to the proper size (48dp) |
| 114 | */ |
| 115 | static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 116 | int textureWidth = sIconTextureWidth; |
| 117 | int textureHeight = sIconTextureHeight; |
| 118 | int sourceWidth = icon.getWidth(); |
| 119 | int sourceHeight = icon.getHeight(); |
| 120 | if (sourceWidth > textureWidth && sourceHeight > textureHeight) { |
| 121 | // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) |
| 122 | return Bitmap.createBitmap(icon, |
| 123 | (sourceWidth - textureWidth) / 2, |
| 124 | (sourceHeight - textureHeight) / 2, |
| 125 | textureWidth, textureHeight); |
| 126 | } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { |
| 127 | // Icon is the right size, no need to change it |
| 128 | return icon; |
| 129 | } else { |
| 130 | // Icon is too small, render to a larger bitmap |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 131 | final Resources resources = context.getResources(); |
| 132 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 138 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 139 | public static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 140 | synchronized (sCanvas) { // we share the statics :-( |
| 141 | if (sIconWidth == -1) { |
| 142 | initStatics(context); |
| 143 | } |
| 144 | |
| 145 | int width = sIconWidth; |
| 146 | int height = sIconHeight; |
| 147 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 148 | if (icon instanceof PaintDrawable) { |
| 149 | PaintDrawable painter = (PaintDrawable) icon; |
| 150 | painter.setIntrinsicWidth(width); |
| 151 | painter.setIntrinsicHeight(height); |
| 152 | } else if (icon instanceof BitmapDrawable) { |
| 153 | // Ensure the bitmap has a density. |
| 154 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 155 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 156 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 157 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 158 | } |
| 159 | } |
| 160 | int sourceWidth = icon.getIntrinsicWidth(); |
| 161 | int sourceHeight = icon.getIntrinsicHeight(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 162 | if (sourceWidth > 0 && sourceHeight > 0) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 163 | // Scale the icon proportionally to the icon dimensions |
| 164 | final float ratio = (float) sourceWidth / sourceHeight; |
| 165 | if (sourceWidth > sourceHeight) { |
| 166 | height = (int) (width / ratio); |
| 167 | } else if (sourceHeight > sourceWidth) { |
| 168 | width = (int) (height * ratio); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | // no intrinsic size --> use default size |
| 173 | int textureWidth = sIconTextureWidth; |
| 174 | int textureHeight = sIconTextureHeight; |
| 175 | |
| 176 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 177 | Bitmap.Config.ARGB_8888); |
| 178 | final Canvas canvas = sCanvas; |
| 179 | canvas.setBitmap(bitmap); |
| 180 | |
| 181 | final int left = (textureWidth-width) / 2; |
| 182 | final int top = (textureHeight-height) / 2; |
| 183 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 184 | @SuppressWarnings("all") // suppress dead code warning |
| 185 | final boolean debug = false; |
| 186 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 187 | // draw a big box for the icon for debugging |
| 188 | canvas.drawColor(sColors[sColorIndex]); |
| 189 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 190 | Paint debugPaint = new Paint(); |
| 191 | debugPaint.setColor(0xffcccc00); |
| 192 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 193 | } |
| 194 | |
| 195 | sOldBounds.set(icon.getBounds()); |
| 196 | icon.setBounds(left, top, left+width, top+height); |
| 197 | icon.draw(canvas); |
| 198 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 199 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 200 | |
| 201 | return bitmap; |
| 202 | } |
| 203 | } |
| 204 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 205 | /** |
| 206 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 207 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 208 | * @param bitmap The bitmap to get a thumbnail of. |
| 209 | * @param context The application's context. |
| 210 | * |
| 211 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 212 | * thumbnail could not be created. |
| 213 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 214 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 215 | synchronized (sCanvas) { // we share the statics :-( |
| 216 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 217 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 220 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 221 | return bitmap; |
| 222 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 223 | final Resources resources = context.getResources(); |
| 224 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 225 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 226 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 227 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 228 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Given a coordinate relative to the descendant, find the coordinate in a parent view's |
| 231 | * coordinates. |
| 232 | * |
| 233 | * @param descendant The descendant to which the passed coordinate is relative. |
| 234 | * @param root The root view to make the coordinates relative to. |
| 235 | * @param coord The coordinate that we want mapped. |
| 236 | * @param includeRootScroll Whether or not to account for the scroll of the descendant: |
| 237 | * sometimes this is relevant as in a child's coordinates within the descendant. |
| 238 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 239 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 240 | * assumption fails, we will need to return a pair of scale factors. |
| 241 | */ |
| 242 | public static float getDescendantCoordRelativeToParent(View descendant, View root, |
| 243 | int[] coord, boolean includeRootScroll) { |
| 244 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 245 | |
| 246 | float[] pt = {coord[0], coord[1]}; |
| 247 | |
| 248 | View v = descendant; |
| 249 | while(v != root && v != null) { |
| 250 | ancestorChain.add(v); |
| 251 | v = (View) v.getParent(); |
| 252 | } |
| 253 | ancestorChain.add(root); |
| 254 | |
| 255 | float scale = 1.0f; |
| 256 | int count = ancestorChain.size(); |
| 257 | for (int i = 0; i < count; i++) { |
| 258 | View v0 = ancestorChain.get(i); |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 259 | // For TextViews, scroll has a meaning which relates to the text position |
| 260 | // which is very strange... ignore the scroll. |
| 261 | if (v0 != descendant || includeRootScroll) { |
| 262 | pt[0] -= v0.getScrollX(); |
| 263 | pt[1] -= v0.getScrollY(); |
| 264 | } |
| 265 | |
| 266 | v0.getMatrix().mapPoints(pt); |
| 267 | pt[0] += v0.getLeft(); |
| 268 | pt[1] += v0.getTop(); |
| 269 | scale *= v0.getScaleX(); |
| 270 | } |
| 271 | |
| 272 | coord[0] = (int) Math.round(pt[0]); |
| 273 | coord[1] = (int) Math.round(pt[1]); |
| 274 | return scale; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}. |
| 279 | */ |
| 280 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 281 | int[] coord) { |
| 282 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 283 | |
| 284 | float[] pt = {coord[0], coord[1]}; |
| 285 | |
| 286 | View v = descendant; |
| 287 | while(v != root) { |
| 288 | ancestorChain.add(v); |
| 289 | v = (View) v.getParent(); |
| 290 | } |
| 291 | ancestorChain.add(root); |
| 292 | |
| 293 | float scale = 1.0f; |
| 294 | Matrix inverse = new Matrix(); |
| 295 | int count = ancestorChain.size(); |
| 296 | for (int i = count - 1; i >= 0; i--) { |
| 297 | View ancestor = ancestorChain.get(i); |
| 298 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 299 | |
| 300 | pt[0] += ancestor.getScrollX(); |
| 301 | pt[1] += ancestor.getScrollY(); |
| 302 | |
| 303 | if (next != null) { |
| 304 | pt[0] -= next.getLeft(); |
| 305 | pt[1] -= next.getTop(); |
| 306 | next.getMatrix().invert(inverse); |
| 307 | inverse.mapPoints(pt); |
| 308 | scale *= next.getScaleX(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | coord[0] = (int) Math.round(pt[0]); |
| 313 | coord[1] = (int) Math.round(pt[1]); |
| 314 | return scale; |
| 315 | } |
| 316 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 317 | /** |
| 318 | * Utility method to determine whether the given point, in local coordinates, |
| 319 | * is inside the view, where the area of the view is expanded by the slop factor. |
| 320 | * This method is called while processing touch-move events to determine if the event |
| 321 | * is still within the view. |
| 322 | */ |
| 323 | public static boolean pointInView(View v, float localX, float localY, float slop) { |
| 324 | return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) && |
| 325 | localY < (v.getHeight() + slop); |
| 326 | } |
| 327 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 328 | private static void initStatics(Context context) { |
| 329 | final Resources resources = context.getResources(); |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 330 | final DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 331 | final float density = metrics.density; |
| 332 | |
Michael Jurka | c9a9619 | 2010-11-01 11:52:08 -0700 | [diff] [blame] | 333 | sIconWidth = sIconHeight = (int) resources.getDimension(R.dimen.app_icon_size); |
Winson Chung | 4b825dcd | 2011-06-19 12:41:22 -0700 | [diff] [blame] | 334 | sIconTextureWidth = sIconTextureHeight = sIconWidth; |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 335 | |
Joe Onorato | a4c0cb9 | 2009-11-02 10:42:02 -0500 | [diff] [blame] | 336 | sBlurPaint.setMaskFilter(new BlurMaskFilter(5 * density, BlurMaskFilter.Blur.NORMAL)); |
Joe Onorato | eb8325a | 2009-11-08 13:20:30 -0500 | [diff] [blame] | 337 | sGlowColorPressedPaint.setColor(0xffffc300); |
Joe Onorato | c61cff9 | 2009-11-08 11:54:39 -0500 | [diff] [blame] | 338 | sGlowColorFocusedPaint.setColor(0xffff8e00); |
Joe Onorato | 56d8291 | 2010-03-07 14:32:10 -0500 | [diff] [blame] | 339 | |
| 340 | ColorMatrix cm = new ColorMatrix(); |
| 341 | cm.setSaturation(0.2f); |
| 342 | sDisabledPaint.setColorFilter(new ColorMatrixColorFilter(cm)); |
| 343 | sDisabledPaint.setAlpha(0x88); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 346 | public static void setIconSize(int widthPx) { |
| 347 | sIconWidth = sIconHeight = widthPx; |
| 348 | sIconTextureWidth = sIconTextureHeight = widthPx; |
Winson Chung | 97d85d2 | 2011-04-13 11:27:36 -0700 | [diff] [blame] | 349 | } |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 350 | |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 351 | public static void scaleRect(Rect r, float scale) { |
| 352 | if (scale != 1.0f) { |
| 353 | r.left = (int) (r.left * scale + 0.5f); |
| 354 | r.top = (int) (r.top * scale + 0.5f); |
| 355 | r.right = (int) (r.right * scale + 0.5f); |
| 356 | r.bottom = (int) (r.bottom * scale + 0.5f); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | public static void scaleRectAboutCenter(Rect r, float scale) { |
| 361 | int cx = r.centerX(); |
| 362 | int cy = r.centerY(); |
| 363 | r.offset(-cx, -cy); |
| 364 | Utilities.scaleRect(r, scale); |
| 365 | r.offset(cx, cy); |
| 366 | } |
| 367 | |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 368 | public static void startActivityForResultSafely( |
| 369 | Activity activity, Intent intent, int requestCode) { |
| 370 | try { |
| 371 | activity.startActivityForResult(intent, requestCode); |
| 372 | } catch (ActivityNotFoundException e) { |
| 373 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 374 | } catch (SecurityException e) { |
| 375 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 376 | Log.e(TAG, "Launcher does not have the permission to launch " + intent + |
| 377 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 378 | "or use the exported attribute for this activity.", e); |
| 379 | } |
| 380 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 381 | } |