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