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; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 23 | import android.content.Intent; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
| 25 | import android.content.pm.PackageInfo; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.PackageManager.NameNotFoundException; |
| 28 | import android.content.pm.ResolveInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 31 | import android.graphics.Canvas; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 32 | import android.graphics.Color; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 33 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 34 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 35 | import android.graphics.PaintFlagsDrawFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 36 | import android.graphics.Rect; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 37 | import android.graphics.drawable.BitmapDrawable; |
| 38 | import android.graphics.drawable.Drawable; |
| 39 | import android.graphics.drawable.PaintDrawable; |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame] | 40 | import android.os.Build; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 41 | import android.util.Log; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 42 | import android.util.Pair; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 43 | import android.util.SparseArray; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 44 | import android.view.View; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 45 | import android.widget.Toast; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 46 | |
| 47 | import java.util.ArrayList; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Various utilities shared amongst the Launcher's classes. |
| 51 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 52 | public final class Utilities { |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 53 | private static final String TAG = "Launcher.Utilities"; |
| 54 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 55 | private static int sIconWidth = -1; |
| 56 | private static int sIconHeight = -1; |
Adam Cohen | 61f560d | 2013-09-30 15:58:20 -0700 | [diff] [blame] | 57 | public static int sIconTextureWidth = -1; |
| 58 | public static int sIconTextureHeight = -1; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 59 | |
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 | |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame^] | 70 | static int[] sLoc0 = new int[2]; |
| 71 | static int[] sLoc1 = new int[2]; |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 72 | |
| 73 | // To turn on these properties, type |
| 74 | // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS] |
| 75 | static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate"; |
| 76 | public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY); |
| 77 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 78 | /** |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 79 | * Returns a FastBitmapDrawable with the icon, accurately sized. |
| 80 | */ |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 81 | static FastBitmapDrawable createIconDrawable(Bitmap icon) { |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 82 | FastBitmapDrawable d = new FastBitmapDrawable(icon); |
Winson Chung | 5400049 | 2013-10-14 16:29:29 -0700 | [diff] [blame] | 83 | d.setFilterBitmap(true); |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 84 | resizeIconDrawable(d); |
| 85 | return d; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Resizes an icon drawable to the correct icon size. |
| 90 | */ |
| 91 | static void resizeIconDrawable(Drawable icon) { |
| 92 | icon.setBounds(0, 0, sIconTextureWidth, sIconTextureHeight); |
| 93 | } |
| 94 | |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 95 | private static boolean isPropertyEnabled(String propertyName) { |
| 96 | return Log.isLoggable(propertyName, Log.VERBOSE); |
| 97 | } |
| 98 | |
| 99 | public static boolean isRotationEnabled(Context c) { |
| 100 | boolean enableRotation = sForceEnableRotation || |
| 101 | c.getResources().getBoolean(R.bool.allow_rotation); |
| 102 | return enableRotation; |
| 103 | } |
| 104 | |
Winson Chung | 0dbd734 | 2013-10-13 22:46:20 -0700 | [diff] [blame] | 105 | /** |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame] | 106 | * Indicates if the device is running LMP or not. |
| 107 | * TODO(sansid): Change the check to a VERSION_CODES code check once we have a version for L. |
| 108 | */ |
| 109 | public static boolean isLmp() { |
| 110 | return "L".equals(Build.VERSION.CODENAME); |
| 111 | } |
| 112 | |
| 113 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 114 | * Returns a bitmap suitable for the all apps view. Used to convert pre-ICS |
| 115 | * icon bitmaps that are stored in the database (which were 74x74 pixels at hdpi size) |
| 116 | * to the proper size (48dp) |
| 117 | */ |
| 118 | static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 119 | int textureWidth = sIconTextureWidth; |
| 120 | int textureHeight = sIconTextureHeight; |
| 121 | int sourceWidth = icon.getWidth(); |
| 122 | int sourceHeight = icon.getHeight(); |
| 123 | if (sourceWidth > textureWidth && sourceHeight > textureHeight) { |
| 124 | // Icon is bigger than it should be; clip it (solves the GB->ICS migration case) |
| 125 | return Bitmap.createBitmap(icon, |
| 126 | (sourceWidth - textureWidth) / 2, |
| 127 | (sourceHeight - textureHeight) / 2, |
| 128 | textureWidth, textureHeight); |
| 129 | } else if (sourceWidth == textureWidth && sourceHeight == textureHeight) { |
| 130 | // Icon is the right size, no need to change it |
| 131 | return icon; |
| 132 | } else { |
| 133 | // Icon is too small, render to a larger bitmap |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 134 | final Resources resources = context.getResources(); |
| 135 | return createIconBitmap(new BitmapDrawable(resources, icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 141 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 142 | public static Bitmap createIconBitmap(Drawable icon, Context context) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 143 | synchronized (sCanvas) { // we share the statics :-( |
| 144 | if (sIconWidth == -1) { |
| 145 | initStatics(context); |
| 146 | } |
| 147 | |
| 148 | int width = sIconWidth; |
| 149 | int height = sIconHeight; |
| 150 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 151 | if (icon instanceof PaintDrawable) { |
| 152 | PaintDrawable painter = (PaintDrawable) icon; |
| 153 | painter.setIntrinsicWidth(width); |
| 154 | painter.setIntrinsicHeight(height); |
| 155 | } else if (icon instanceof BitmapDrawable) { |
| 156 | // Ensure the bitmap has a density. |
| 157 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 158 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
| 159 | if (bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
| 160 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 161 | } |
| 162 | } |
| 163 | int sourceWidth = icon.getIntrinsicWidth(); |
| 164 | int sourceHeight = icon.getIntrinsicHeight(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 165 | if (sourceWidth > 0 && sourceHeight > 0) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 166 | // Scale the icon proportionally to the icon dimensions |
| 167 | final float ratio = (float) sourceWidth / sourceHeight; |
| 168 | if (sourceWidth > sourceHeight) { |
| 169 | height = (int) (width / ratio); |
| 170 | } else if (sourceHeight > sourceWidth) { |
| 171 | width = (int) (height * ratio); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | // no intrinsic size --> use default size |
| 176 | int textureWidth = sIconTextureWidth; |
| 177 | int textureHeight = sIconTextureHeight; |
| 178 | |
| 179 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 180 | Bitmap.Config.ARGB_8888); |
| 181 | final Canvas canvas = sCanvas; |
| 182 | canvas.setBitmap(bitmap); |
| 183 | |
| 184 | final int left = (textureWidth-width) / 2; |
| 185 | final int top = (textureHeight-height) / 2; |
| 186 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 187 | @SuppressWarnings("all") // suppress dead code warning |
| 188 | final boolean debug = false; |
| 189 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 190 | // draw a big box for the icon for debugging |
| 191 | canvas.drawColor(sColors[sColorIndex]); |
| 192 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 193 | Paint debugPaint = new Paint(); |
| 194 | debugPaint.setColor(0xffcccc00); |
| 195 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 196 | } |
| 197 | |
| 198 | sOldBounds.set(icon.getBounds()); |
| 199 | icon.setBounds(left, top, left+width, top+height); |
| 200 | icon.draw(canvas); |
| 201 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 202 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 203 | |
| 204 | return bitmap; |
| 205 | } |
| 206 | } |
| 207 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 208 | /** |
| 209 | * 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] | 210 | * |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 211 | * @param bitmap The bitmap to get a thumbnail of. |
| 212 | * @param context The application's context. |
| 213 | * |
| 214 | * @return A thumbnail for the specified bitmap or the bitmap itself if the |
| 215 | * thumbnail could not be created. |
| 216 | */ |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 217 | static Bitmap resampleIconBitmap(Bitmap bitmap, Context context) { |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 218 | synchronized (sCanvas) { // we share the statics :-( |
| 219 | if (sIconWidth == -1) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 220 | initStatics(context); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Joe Onorato | 0589f0f | 2010-02-08 13:44:00 -0800 | [diff] [blame] | 223 | if (bitmap.getWidth() == sIconWidth && bitmap.getHeight() == sIconHeight) { |
| 224 | return bitmap; |
| 225 | } else { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 226 | final Resources resources = context.getResources(); |
| 227 | return createIconBitmap(new BitmapDrawable(resources, bitmap), context); |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 228 | } |
Joe Onorato | 9c1289c | 2009-08-17 11:03:03 -0400 | [diff] [blame] | 229 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 230 | } |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 231 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Given a coordinate relative to the descendant, find the coordinate in a parent view's |
| 234 | * coordinates. |
| 235 | * |
| 236 | * @param descendant The descendant to which the passed coordinate is relative. |
| 237 | * @param root The root view to make the coordinates relative to. |
| 238 | * @param coord The coordinate that we want mapped. |
| 239 | * @param includeRootScroll Whether or not to account for the scroll of the descendant: |
| 240 | * sometimes this is relevant as in a child's coordinates within the descendant. |
| 241 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 242 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 243 | * assumption fails, we will need to return a pair of scale factors. |
| 244 | */ |
| 245 | public static float getDescendantCoordRelativeToParent(View descendant, View root, |
| 246 | int[] coord, boolean includeRootScroll) { |
| 247 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 248 | |
| 249 | float[] pt = {coord[0], coord[1]}; |
| 250 | |
| 251 | View v = descendant; |
| 252 | while(v != root && v != null) { |
| 253 | ancestorChain.add(v); |
| 254 | v = (View) v.getParent(); |
| 255 | } |
| 256 | ancestorChain.add(root); |
| 257 | |
| 258 | float scale = 1.0f; |
| 259 | int count = ancestorChain.size(); |
| 260 | for (int i = 0; i < count; i++) { |
| 261 | View v0 = ancestorChain.get(i); |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 262 | // For TextViews, scroll has a meaning which relates to the text position |
| 263 | // which is very strange... ignore the scroll. |
| 264 | if (v0 != descendant || includeRootScroll) { |
| 265 | pt[0] -= v0.getScrollX(); |
| 266 | pt[1] -= v0.getScrollY(); |
| 267 | } |
| 268 | |
| 269 | v0.getMatrix().mapPoints(pt); |
| 270 | pt[0] += v0.getLeft(); |
| 271 | pt[1] += v0.getTop(); |
| 272 | scale *= v0.getScaleX(); |
| 273 | } |
| 274 | |
| 275 | coord[0] = (int) Math.round(pt[0]); |
| 276 | coord[1] = (int) Math.round(pt[1]); |
| 277 | return scale; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Inverse of {@link #getDescendantCoordRelativeToSelf(View, int[])}. |
| 282 | */ |
| 283 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 284 | int[] coord) { |
| 285 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 286 | |
| 287 | float[] pt = {coord[0], coord[1]}; |
| 288 | |
| 289 | View v = descendant; |
| 290 | while(v != root) { |
| 291 | ancestorChain.add(v); |
| 292 | v = (View) v.getParent(); |
| 293 | } |
| 294 | ancestorChain.add(root); |
| 295 | |
| 296 | float scale = 1.0f; |
| 297 | Matrix inverse = new Matrix(); |
| 298 | int count = ancestorChain.size(); |
| 299 | for (int i = count - 1; i >= 0; i--) { |
| 300 | View ancestor = ancestorChain.get(i); |
| 301 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 302 | |
| 303 | pt[0] += ancestor.getScrollX(); |
| 304 | pt[1] += ancestor.getScrollY(); |
| 305 | |
| 306 | if (next != null) { |
| 307 | pt[0] -= next.getLeft(); |
| 308 | pt[1] -= next.getTop(); |
| 309 | next.getMatrix().invert(inverse); |
| 310 | inverse.mapPoints(pt); |
| 311 | scale *= next.getScaleX(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | coord[0] = (int) Math.round(pt[0]); |
| 316 | coord[1] = (int) Math.round(pt[1]); |
| 317 | return scale; |
| 318 | } |
| 319 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 320 | /** |
| 321 | * Utility method to determine whether the given point, in local coordinates, |
| 322 | * is inside the view, where the area of the view is expanded by the slop factor. |
| 323 | * This method is called while processing touch-move events to determine if the event |
| 324 | * is still within the view. |
| 325 | */ |
| 326 | public static boolean pointInView(View v, float localX, float localY, float slop) { |
| 327 | return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) && |
| 328 | localY < (v.getHeight() + slop); |
| 329 | } |
| 330 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 331 | private static void initStatics(Context context) { |
| 332 | final Resources resources = context.getResources(); |
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 | 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 | |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame^] | 351 | public static int[] getCenterDeltaInScreenSpace(View v0, View v1, int[] delta) { |
| 352 | v0.getLocationInWindow(sLoc0); |
| 353 | v1.getLocationInWindow(sLoc1); |
| 354 | |
| 355 | sLoc0[0] += (v0.getMeasuredWidth() * v0.getScaleX()) / 2; |
| 356 | sLoc0[1] += (v0.getMeasuredHeight() * v0.getScaleY()) / 2; |
| 357 | sLoc1[0] += (v1.getMeasuredWidth() * v1.getScaleX()) / 2; |
| 358 | sLoc1[1] += (v1.getMeasuredHeight() * v1.getScaleY()) / 2; |
| 359 | |
| 360 | if (delta == null) { |
| 361 | delta = new int[2]; |
| 362 | } |
| 363 | |
| 364 | delta[0] = sLoc1[0] - sLoc0[0]; |
| 365 | delta[1] = sLoc1[1] - sLoc0[1]; |
| 366 | |
| 367 | return delta; |
| 368 | } |
| 369 | |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 370 | public static void scaleRectAboutCenter(Rect r, float scale) { |
| 371 | int cx = r.centerX(); |
| 372 | int cy = r.centerY(); |
| 373 | r.offset(-cx, -cy); |
| 374 | Utilities.scaleRect(r, scale); |
| 375 | r.offset(cx, cy); |
| 376 | } |
| 377 | |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 378 | public static void startActivityForResultSafely( |
| 379 | Activity activity, Intent intent, int requestCode) { |
| 380 | try { |
| 381 | activity.startActivityForResult(intent, requestCode); |
| 382 | } catch (ActivityNotFoundException e) { |
| 383 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 384 | } catch (SecurityException e) { |
| 385 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 386 | Log.e(TAG, "Launcher does not have the permission to launch " + intent + |
| 387 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 388 | "or use the exported attribute for this activity.", e); |
| 389 | } |
| 390 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 391 | |
| 392 | static boolean isSystemApp(Context context, Intent intent) { |
| 393 | PackageManager pm = context.getPackageManager(); |
| 394 | ComponentName cn = intent.getComponent(); |
| 395 | String packageName = null; |
| 396 | if (cn == null) { |
| 397 | ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 398 | if ((info != null) && (info.activityInfo != null)) { |
| 399 | packageName = info.activityInfo.packageName; |
| 400 | } |
| 401 | } else { |
| 402 | packageName = cn.getPackageName(); |
| 403 | } |
| 404 | if (packageName != null) { |
| 405 | try { |
| 406 | PackageInfo info = pm.getPackageInfo(packageName, 0); |
| 407 | return (info != null) && (info.applicationInfo != null) && |
| 408 | ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); |
| 409 | } catch (NameNotFoundException e) { |
| 410 | return false; |
| 411 | } |
| 412 | } else { |
| 413 | return false; |
| 414 | } |
| 415 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * This picks a dominant color, looking for high-saturation, high-value, repeated hues. |
| 419 | * @param bitmap The bitmap to scan |
| 420 | * @param samples The approximate max number of samples to use. |
| 421 | */ |
| 422 | static int findDominantColorByHue(Bitmap bitmap, int samples) { |
| 423 | final int height = bitmap.getHeight(); |
| 424 | final int width = bitmap.getWidth(); |
| 425 | int sampleStride = (int) Math.sqrt((height * width) / samples); |
| 426 | if (sampleStride < 1) { |
| 427 | sampleStride = 1; |
| 428 | } |
| 429 | |
| 430 | // This is an out-param, for getting the hsv values for an rgb |
| 431 | float[] hsv = new float[3]; |
| 432 | |
| 433 | // First get the best hue, by creating a histogram over 360 hue buckets, |
| 434 | // where each pixel contributes a score weighted by saturation, value, and alpha. |
| 435 | float[] hueScoreHistogram = new float[360]; |
| 436 | float highScore = -1; |
| 437 | int bestHue = -1; |
| 438 | |
| 439 | for (int y = 0; y < height; y += sampleStride) { |
| 440 | for (int x = 0; x < width; x += sampleStride) { |
| 441 | int argb = bitmap.getPixel(x, y); |
| 442 | int alpha = 0xFF & (argb >> 24); |
| 443 | if (alpha < 0x80) { |
| 444 | // Drop mostly-transparent pixels. |
| 445 | continue; |
| 446 | } |
| 447 | // Remove the alpha channel. |
| 448 | int rgb = argb | 0xFF000000; |
| 449 | Color.colorToHSV(rgb, hsv); |
| 450 | // Bucket colors by the 360 integer hues. |
| 451 | int hue = (int) hsv[0]; |
| 452 | if (hue < 0 || hue >= hueScoreHistogram.length) { |
| 453 | // Defensively avoid array bounds violations. |
| 454 | continue; |
| 455 | } |
| 456 | float score = hsv[1] * hsv[2]; |
| 457 | hueScoreHistogram[hue] += score; |
| 458 | if (hueScoreHistogram[hue] > highScore) { |
| 459 | highScore = hueScoreHistogram[hue]; |
| 460 | bestHue = hue; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | SparseArray<Float> rgbScores = new SparseArray<Float>(); |
| 466 | int bestColor = 0xff000000; |
| 467 | highScore = -1; |
| 468 | // Go back over the RGB colors that match the winning hue, |
| 469 | // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets. |
| 470 | // The highest-scoring RGB color wins. |
| 471 | for (int y = 0; y < height; y += sampleStride) { |
| 472 | for (int x = 0; x < width; x += sampleStride) { |
| 473 | int rgb = bitmap.getPixel(x, y) | 0xff000000; |
| 474 | Color.colorToHSV(rgb, hsv); |
| 475 | int hue = (int) hsv[0]; |
| 476 | if (hue == bestHue) { |
| 477 | float s = hsv[1]; |
| 478 | float v = hsv[2]; |
| 479 | int bucket = (int) (s * 100) + (int) (v * 10000); |
| 480 | // Score by cumulative saturation * value. |
| 481 | float score = s * v; |
| 482 | Float oldTotal = rgbScores.get(bucket); |
| 483 | float newTotal = oldTotal == null ? score : oldTotal + score; |
| 484 | rgbScores.put(bucket, newTotal); |
| 485 | if (newTotal > highScore) { |
| 486 | highScore = newTotal; |
| 487 | // All the colors in the winning bucket are very similar. Last in wins. |
| 488 | bestColor = rgb; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | return bestColor; |
| 494 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Finds a system apk which had a broadcast receiver listening to a particular action. |
| 498 | * @param action intent action used to find the apk |
| 499 | * @return a pair of apk package name and the resources. |
| 500 | */ |
| 501 | static Pair<String, Resources> findSystemApk(String action, PackageManager pm) { |
| 502 | final Intent intent = new Intent(action); |
| 503 | for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { |
| 504 | if (info.activityInfo != null && |
| 505 | (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 506 | final String packageName = info.activityInfo.packageName; |
| 507 | try { |
| 508 | final Resources res = pm.getResourcesForApplication(packageName); |
| 509 | return Pair.create(packageName, res); |
| 510 | } catch (NameNotFoundException e) { |
| 511 | Log.w(TAG, "Failed to find resources for " + packageName); |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | return null; |
| 516 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 517 | } |