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