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