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; |
Sunny Goyal | 1ed6c4a | 2016-04-21 15:16:11 -0700 | [diff] [blame] | 21 | import android.app.WallpaperManager; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 25 | import android.content.Intent; |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.PackageInfo; |
| 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.PackageManager.NameNotFoundException; |
| 31 | import android.content.pm.ResolveInfo; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 33 | import android.content.res.TypedArray; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 34 | import android.database.Cursor; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 35 | import android.graphics.Bitmap; |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 36 | import android.graphics.BitmapFactory; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 37 | import android.graphics.Canvas; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 38 | import android.graphics.Color; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 39 | import android.graphics.Matrix; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 40 | import android.graphics.Paint; |
Joe Onorato | bf15cb4 | 2009-08-07 14:33:40 -0700 | [diff] [blame] | 41 | import android.graphics.PaintFlagsDrawFilter; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 42 | import android.graphics.Rect; |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 43 | import android.graphics.RectF; |
Winson Chung | aafa03c | 2010-06-11 17:34:16 -0700 | [diff] [blame] | 44 | import android.graphics.drawable.BitmapDrawable; |
| 45 | import android.graphics.drawable.Drawable; |
| 46 | import android.graphics.drawable.PaintDrawable; |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame] | 47 | import android.os.Build; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 48 | import android.os.Bundle; |
Tony Wickham | 112ac95 | 2015-11-12 12:31:50 -0800 | [diff] [blame] | 49 | import android.os.PowerManager; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 50 | import android.text.Spannable; |
| 51 | import android.text.SpannableString; |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 52 | import android.text.TextUtils; |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 53 | import android.text.style.TtsSpan; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 54 | import android.util.DisplayMetrics; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 55 | import android.util.Log; |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 56 | import android.util.Pair; |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 57 | import android.util.SparseArray; |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 58 | import android.util.TypedValue; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 59 | import android.view.MotionEvent; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 60 | import android.view.View; |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 61 | import android.widget.Toast; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 62 | |
Sunny Goyal | b84046d | 2015-10-19 14:03:30 -0700 | [diff] [blame] | 63 | import com.android.launcher3.compat.UserHandleCompat; |
Sunny Goyal | d896ee4 | 2015-12-17 11:24:31 -0800 | [diff] [blame] | 64 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 65 | import com.android.launcher3.config.ProviderConfig; |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 66 | import com.android.launcher3.graphics.ShadowGenerator; |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 67 | import com.android.launcher3.util.IconNormalizer; |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 68 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 69 | import java.io.ByteArrayOutputStream; |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 70 | import java.io.Closeable; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 71 | import java.io.IOException; |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 72 | import java.lang.reflect.Method; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 73 | import java.util.ArrayList; |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 74 | import java.util.Collection; |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 75 | import java.util.Locale; |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 76 | import java.util.Set; |
Sunny Goyal | 8ac727b | 2015-09-23 15:38:09 -0700 | [diff] [blame] | 77 | import java.util.concurrent.Executor; |
| 78 | import java.util.concurrent.LinkedBlockingQueue; |
| 79 | import java.util.concurrent.ThreadPoolExecutor; |
| 80 | import java.util.concurrent.TimeUnit; |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 81 | import java.util.regex.Matcher; |
| 82 | import java.util.regex.Pattern; |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Various utilities shared amongst the Launcher's classes. |
| 86 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 87 | public final class Utilities { |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 88 | |
Joe Onorato | 1291a8c | 2009-09-15 15:07:25 -0400 | [diff] [blame] | 89 | private static final String TAG = "Launcher.Utilities"; |
| 90 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 91 | private static final Rect sOldBounds = new Rect(); |
Romain Guy | 89911d2 | 2009-09-28 18:48:49 -0700 | [diff] [blame] | 92 | private static final Canvas sCanvas = new Canvas(); |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 93 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 94 | private static final Pattern sTrimPattern = |
| 95 | Pattern.compile("^[\\s|\\p{javaSpaceChar}]*(.*)[\\s|\\p{javaSpaceChar}]*$"); |
| 96 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 97 | static { |
| 98 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 99 | Paint.FILTER_BITMAP_FLAG)); |
| 100 | } |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 101 | static int sColors[] = { 0xffff0000, 0xff00ff00, 0xff0000ff }; |
| 102 | static int sColorIndex = 0; |
| 103 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 104 | private static final int[] sLoc0 = new int[2]; |
| 105 | private static final int[] sLoc1 = new int[2]; |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 106 | |
Sunny Goyal | 9994b2b | 2016-06-23 14:17:24 -0700 | [diff] [blame] | 107 | public static boolean isNycMR1OrAbove() { |
Sunny Goyal | a920612 | 2016-08-15 19:34:37 -0700 | [diff] [blame] | 108 | return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1; |
Sunny Goyal | 9994b2b | 2016-06-23 14:17:24 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Winson | be9798b | 2016-07-20 12:55:49 -0700 | [diff] [blame] | 111 | public static final boolean ATLEAST_MARSHMALLOW = |
| 112 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 113 | |
| 114 | public static final boolean ATLEAST_LOLLIPOP_MR1 = |
| 115 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1; |
| 116 | |
| 117 | public static final boolean ATLEAST_LOLLIPOP = |
| 118 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| 119 | |
| 120 | public static final boolean ATLEAST_KITKAT = |
| 121 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; |
| 122 | |
| 123 | public static final boolean ATLEAST_JB_MR1 = |
| 124 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 125 | |
| 126 | public static final boolean ATLEAST_JB_MR2 = |
| 127 | Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2; |
| 128 | |
Sunny Goyal | 6f86609 | 2016-03-17 17:04:15 -0700 | [diff] [blame] | 129 | // An intent extra to indicate the horizontal scroll of the wallpaper. |
| 130 | public static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET"; |
| 131 | |
Sunny Goyal | 8ac727b | 2015-09-23 15:38:09 -0700 | [diff] [blame] | 132 | // These values are same as that in {@link AsyncTask}. |
| 133 | private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); |
| 134 | private static final int CORE_POOL_SIZE = CPU_COUNT + 1; |
| 135 | private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1; |
| 136 | private static final int KEEP_ALIVE = 1; |
| 137 | /** |
| 138 | * An {@link Executor} to be used with async task with no limit on the queue size. |
| 139 | */ |
| 140 | public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor( |
| 141 | CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, |
| 142 | TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()); |
| 143 | |
Rahul Chaturvedi | 7fc77ca | 2015-05-19 18:02:16 -0700 | [diff] [blame] | 144 | public static final String ALLOW_ROTATION_PREFERENCE_KEY = "pref_allowRotation"; |
| 145 | |
Sunny Goyal | 4bbf419 | 2014-11-11 12:23:59 -0800 | [diff] [blame] | 146 | public static boolean isPropertyEnabled(String propertyName) { |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 147 | return Log.isLoggable(propertyName, Log.VERBOSE); |
| 148 | } |
| 149 | |
Sunny Goyal | 21bf531 | 2015-08-21 11:08:24 -0700 | [diff] [blame] | 150 | public static boolean isAllowRotationPrefEnabled(Context context) { |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 151 | return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, |
| 152 | getAllowRotationDefaultValue(context)); |
| 153 | } |
| 154 | |
| 155 | public static boolean getAllowRotationDefaultValue(Context context) { |
Sunny Goyal | 8f3819b | 2016-02-23 14:49:22 -0800 | [diff] [blame] | 156 | if (isNycOrAbove()) { |
| 157 | // If the device was scaled, used the original dimensions to determine if rotation |
| 158 | // is allowed of not. |
| 159 | try { |
| 160 | // TODO: Use the actual field when the API is finalized. |
| 161 | int originalDensity = |
| 162 | DisplayMetrics.class.getField("DENSITY_DEVICE_STABLE").getInt(null); |
| 163 | Resources res = context.getResources(); |
| 164 | int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp |
| 165 | * res.getDisplayMetrics().densityDpi / originalDensity; |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 166 | return originalSmallestWidth >= 600; |
Sunny Goyal | 8f3819b | 2016-02-23 14:49:22 -0800 | [diff] [blame] | 167 | } catch (Exception e) { |
| 168 | // Ignore |
| 169 | } |
| 170 | } |
Sunny Goyal | f48e592 | 2016-05-12 15:36:20 -0700 | [diff] [blame] | 171 | return false; |
Michael Jurka | 7ad868b | 2013-12-12 15:04:25 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Vadim Tryshev | 9de9127 | 2015-08-03 18:37:17 -0700 | [diff] [blame] | 174 | public static boolean isNycOrAbove() { |
| 175 | // TODO(vadimt): Replace using reflection with looking at the API version once |
| 176 | // Build.VERSION.SDK_INT gets bumped to 24. b/22942492. |
| 177 | try { |
| 178 | View.class.getDeclaredField("DRAG_FLAG_OPAQUE"); |
| 179 | // View.DRAG_FLAG_OPAQUE doesn't exist in M-release, so it's an indication of N+. |
| 180 | return true; |
| 181 | } catch (NoSuchFieldException e) { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 186 | public static Bitmap createIconBitmap(Cursor c, int iconIndex, Context context) { |
Sunny Goyal | 4fbc382 | 2015-02-18 16:46:50 -0800 | [diff] [blame] | 187 | byte[] data = c.getBlob(iconIndex); |
| 188 | try { |
| 189 | return createIconBitmap(BitmapFactory.decodeByteArray(data, 0, data.length), context); |
| 190 | } catch (Exception e) { |
| 191 | return null; |
| 192 | } |
| 193 | } |
| 194 | |
Sandeep Siddhartha | 2efc7d9 | 2014-05-16 17:21:15 -0700 | [diff] [blame] | 195 | /** |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 196 | * Returns a bitmap suitable for the all apps view. If the package or the resource do not |
| 197 | * exist, it returns null. |
| 198 | */ |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 199 | public static Bitmap createIconBitmap(String packageName, String resourceName, |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 200 | Context context) { |
| 201 | PackageManager packageManager = context.getPackageManager(); |
| 202 | // the resource |
| 203 | try { |
| 204 | Resources resources = packageManager.getResourcesForApplication(packageName); |
| 205 | if (resources != null) { |
| 206 | final int id = resources.getIdentifier(resourceName, null, null); |
| 207 | return createIconBitmap( |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 208 | resources.getDrawableForDensity(id, LauncherAppState.getInstance() |
| 209 | .getInvariantDeviceProfile().fillResIconDpi), context); |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 210 | } |
| 211 | } catch (Exception e) { |
| 212 | // Icon not found. |
| 213 | } |
| 214 | return null; |
| 215 | } |
| 216 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 217 | private static int getIconBitmapSize() { |
| 218 | return LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize; |
| 219 | } |
| 220 | |
Sunny Goyal | b50cc8c | 2014-10-06 16:23:56 -0700 | [diff] [blame] | 221 | /** |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 222 | * 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] | 223 | */ |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 224 | public static Bitmap createIconBitmap(Bitmap icon, Context context) { |
| 225 | final int iconBitmapSize = getIconBitmapSize(); |
| 226 | if (iconBitmapSize == icon.getWidth() && iconBitmapSize == icon.getHeight()) { |
Sunny Goyal | 2fce90c | 2014-10-07 12:01:58 -0700 | [diff] [blame] | 227 | return icon; |
| 228 | } |
| 229 | return createIconBitmap(new BitmapDrawable(context.getResources(), icon), context); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 233 | * Returns a bitmap suitable for the all apps view. The icon is badged for {@param user}. |
| 234 | * The bitmap is also visually normalized with other icons. |
Sunny Goyal | b84046d | 2015-10-19 14:03:30 -0700 | [diff] [blame] | 235 | */ |
| 236 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 237 | public static Bitmap createBadgedIconBitmap( |
| 238 | Drawable icon, UserHandleCompat user, Context context) { |
Sunny Goyal | 531cda6 | 2016-03-03 13:19:24 -0800 | [diff] [blame] | 239 | float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ? |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 240 | 1 : IconNormalizer.getInstance().getScale(icon, null); |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 241 | Bitmap bitmap = createIconBitmap(icon, context, scale); |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 242 | return badgeIconForUser(bitmap, user, context); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Badges the provided icon with the user badge if required. |
| 247 | */ |
| 248 | public static Bitmap badgeIconForUser(Bitmap icon, UserHandleCompat user, Context context) { |
Sunny Goyal | b84046d | 2015-10-19 14:03:30 -0700 | [diff] [blame] | 249 | if (Utilities.ATLEAST_LOLLIPOP && user != null |
| 250 | && !UserHandleCompat.myUserHandle().equals(user)) { |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 251 | BitmapDrawable drawable = new FixedSizeBitmapDrawable(icon); |
Sunny Goyal | b84046d | 2015-10-19 14:03:30 -0700 | [diff] [blame] | 252 | Drawable badged = context.getPackageManager().getUserBadgedIcon( |
| 253 | drawable, user.getUser()); |
| 254 | if (badged instanceof BitmapDrawable) { |
| 255 | return ((BitmapDrawable) badged).getBitmap(); |
| 256 | } else { |
| 257 | return createIconBitmap(badged, context); |
| 258 | } |
| 259 | } else { |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 260 | return icon; |
Sunny Goyal | b84046d | 2015-10-19 14:03:30 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
Sunny Goyal | 10923b3 | 2016-07-20 15:42:44 -0700 | [diff] [blame] | 265 | * Creates a normalized bitmap suitable for the all apps view. The bitmap is also visually |
| 266 | * normalized with other icons and has enough spacing to add shadow. |
| 267 | */ |
| 268 | public static Bitmap createScaledBitmapWithoutShadow(Drawable icon, Context context) { |
| 269 | RectF iconBounds = new RectF(); |
| 270 | float scale = FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION ? |
| 271 | 1 : IconNormalizer.getInstance().getScale(icon, iconBounds); |
| 272 | scale = Math.min(scale, ShadowGenerator.getScaleForBounds(iconBounds)); |
| 273 | return createIconBitmap(icon, context, scale); |
| 274 | } |
| 275 | |
| 276 | /** |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 277 | * Adds a shadow to the provided icon. It assumes that the icon has already been scaled using |
| 278 | * {@link #createScaledBitmapWithoutShadow(Drawable, Context)} |
| 279 | */ |
| 280 | public static Bitmap addShadowToIcon(Bitmap icon) { |
| 281 | return ShadowGenerator.getInstance().recreateIcon(icon); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Adds the {@param badge} on top of {@param srcTgt} using the badge dimensions. |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 286 | */ |
| 287 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 288 | public static Bitmap badgeWithBitmap(Bitmap srcTgt, Bitmap badge, Context context) { |
| 289 | int badgeSize = context.getResources().getDimensionPixelSize(R.dimen.profile_badge_size); |
| 290 | synchronized (sCanvas) { |
| 291 | sCanvas.setBitmap(srcTgt); |
| 292 | sCanvas.drawBitmap(badge, new Rect(0, 0, badge.getWidth(), badge.getHeight()), |
| 293 | new Rect(srcTgt.getWidth() - badgeSize, |
| 294 | srcTgt.getHeight() - badgeSize, srcTgt.getWidth(), srcTgt.getHeight()), |
| 295 | new Paint(Paint.FILTER_BITMAP_FLAG)); |
| 296 | sCanvas.setBitmap(null); |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 297 | } |
Sunny Goyal | 79cf718 | 2016-07-27 17:32:34 -0700 | [diff] [blame] | 298 | return srcTgt; |
Sunny Goyal | 4a46479 | 2016-07-19 14:19:53 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 302 | * Returns a bitmap suitable for the all apps view. |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 303 | */ |
Mathew Inwood | 72fbec1 | 2013-11-19 15:45:07 +0000 | [diff] [blame] | 304 | public static Bitmap createIconBitmap(Drawable icon, Context context) { |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 305 | return createIconBitmap(icon, context, 1.0f /* scale */); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * @param scale the scale to apply before drawing {@param icon} on the canvas |
| 310 | */ |
| 311 | public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) { |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 312 | synchronized (sCanvas) { |
| 313 | final int iconBitmapSize = getIconBitmapSize(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 314 | |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 315 | int width = iconBitmapSize; |
| 316 | int height = iconBitmapSize; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 317 | |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 318 | if (icon instanceof PaintDrawable) { |
| 319 | PaintDrawable painter = (PaintDrawable) icon; |
| 320 | painter.setIntrinsicWidth(width); |
| 321 | painter.setIntrinsicHeight(height); |
| 322 | } else if (icon instanceof BitmapDrawable) { |
| 323 | // Ensure the bitmap has a density. |
| 324 | BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; |
| 325 | Bitmap bitmap = bitmapDrawable.getBitmap(); |
Sunny Goyal | de9590b | 2015-09-23 12:56:51 -0700 | [diff] [blame] | 326 | if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 327 | bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics()); |
| 328 | } |
| 329 | } |
| 330 | int sourceWidth = icon.getIntrinsicWidth(); |
| 331 | int sourceHeight = icon.getIntrinsicHeight(); |
Michael Jurka | 931dc97 | 2011-08-05 15:08:15 -0700 | [diff] [blame] | 332 | if (sourceWidth > 0 && sourceHeight > 0) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 333 | // Scale the icon proportionally to the icon dimensions |
| 334 | final float ratio = (float) sourceWidth / sourceHeight; |
| 335 | if (sourceWidth > sourceHeight) { |
| 336 | height = (int) (width / ratio); |
| 337 | } else if (sourceHeight > sourceWidth) { |
| 338 | width = (int) (height * ratio); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
| 342 | // no intrinsic size --> use default size |
Sunny Goyal | 53d7ee4 | 2015-05-22 12:25:45 -0700 | [diff] [blame] | 343 | int textureWidth = iconBitmapSize; |
| 344 | int textureHeight = iconBitmapSize; |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 345 | |
| 346 | final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, |
| 347 | Bitmap.Config.ARGB_8888); |
| 348 | final Canvas canvas = sCanvas; |
| 349 | canvas.setBitmap(bitmap); |
| 350 | |
| 351 | final int left = (textureWidth-width) / 2; |
| 352 | final int top = (textureHeight-height) / 2; |
| 353 | |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 354 | @SuppressWarnings("all") // suppress dead code warning |
| 355 | final boolean debug = false; |
| 356 | if (debug) { |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 357 | // draw a big box for the icon for debugging |
| 358 | canvas.drawColor(sColors[sColorIndex]); |
| 359 | if (++sColorIndex >= sColors.length) sColorIndex = 0; |
| 360 | Paint debugPaint = new Paint(); |
| 361 | debugPaint.setColor(0xffcccc00); |
| 362 | canvas.drawRect(left, top, left+width, top+height, debugPaint); |
| 363 | } |
| 364 | |
| 365 | sOldBounds.set(icon.getBounds()); |
| 366 | icon.setBounds(left, top, left+width, top+height); |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 367 | canvas.save(Canvas.MATRIX_SAVE_FLAG); |
| 368 | canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 369 | icon.draw(canvas); |
Sunny Goyal | fc21830 | 2015-09-17 14:59:10 -0700 | [diff] [blame] | 370 | canvas.restore(); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 371 | icon.setBounds(sOldBounds); |
Adam Cohen | aaf473c | 2011-08-03 12:02:47 -0700 | [diff] [blame] | 372 | canvas.setBitmap(null); |
Joe Onorato | 6665c0f | 2009-09-02 15:27:24 -0700 | [diff] [blame] | 373 | |
| 374 | return bitmap; |
| 375 | } |
| 376 | } |
| 377 | |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 378 | /** |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 379 | * Given a coordinate relative to the descendant, find the coordinate in a parent view's |
| 380 | * coordinates. |
| 381 | * |
| 382 | * @param descendant The descendant to which the passed coordinate is relative. |
Sunny Goyal | e8b5d20 | 2016-06-13 16:36:59 -0700 | [diff] [blame] | 383 | * @param ancestor The root view to make the coordinates relative to. |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 384 | * @param coord The coordinate that we want mapped. |
| 385 | * @param includeRootScroll Whether or not to account for the scroll of the descendant: |
| 386 | * sometimes this is relevant as in a child's coordinates within the descendant. |
| 387 | * @return The factor by which this descendant is scaled relative to this DragLayer. Caution |
| 388 | * this scale factor is assumed to be equal in X and Y, and so if at any point this |
| 389 | * assumption fails, we will need to return a pair of scale factors. |
| 390 | */ |
Sunny Goyal | e8b5d20 | 2016-06-13 16:36:59 -0700 | [diff] [blame] | 391 | public static float getDescendantCoordRelativeToAncestor( |
| 392 | View descendant, View ancestor, int[] coord, boolean includeRootScroll) { |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 393 | float[] pt = {coord[0], coord[1]}; |
Sunny Goyal | 8cf47a5 | 2016-06-15 10:11:30 -0700 | [diff] [blame] | 394 | float scale = 1.0f; |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 395 | View v = descendant; |
Sunny Goyal | e8b5d20 | 2016-06-13 16:36:59 -0700 | [diff] [blame] | 396 | while(v != ancestor && v != null) { |
Sunny Goyal | 8cf47a5 | 2016-06-15 10:11:30 -0700 | [diff] [blame] | 397 | // For TextViews, scroll has a meaning which relates to the text position |
| 398 | // which is very strange... ignore the scroll. |
| 399 | if (v != descendant || includeRootScroll) { |
| 400 | pt[0] -= v.getScrollX(); |
| 401 | pt[1] -= v.getScrollY(); |
| 402 | } |
| 403 | |
| 404 | v.getMatrix().mapPoints(pt); |
| 405 | pt[0] += v.getLeft(); |
| 406 | pt[1] += v.getTop(); |
| 407 | scale *= v.getScaleX(); |
| 408 | |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 409 | v = (View) v.getParent(); |
| 410 | } |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 411 | |
Sunny Goyal | 8cf47a5 | 2016-06-15 10:11:30 -0700 | [diff] [blame] | 412 | coord[0] = Math.round(pt[0]); |
| 413 | coord[1] = Math.round(pt[1]); |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 414 | return scale; |
| 415 | } |
| 416 | |
| 417 | /** |
Sunny Goyal | e8b5d20 | 2016-06-13 16:36:59 -0700 | [diff] [blame] | 418 | * Inverse of {@link #getDescendantCoordRelativeToAncestor(View, View, int[], boolean)}. |
Winson Chung | c763c4e | 2013-07-19 13:49:06 -0700 | [diff] [blame] | 419 | */ |
| 420 | public static float mapCoordInSelfToDescendent(View descendant, View root, |
| 421 | int[] coord) { |
| 422 | ArrayList<View> ancestorChain = new ArrayList<View>(); |
| 423 | |
| 424 | float[] pt = {coord[0], coord[1]}; |
| 425 | |
| 426 | View v = descendant; |
| 427 | while(v != root) { |
| 428 | ancestorChain.add(v); |
| 429 | v = (View) v.getParent(); |
| 430 | } |
| 431 | ancestorChain.add(root); |
| 432 | |
| 433 | float scale = 1.0f; |
| 434 | Matrix inverse = new Matrix(); |
| 435 | int count = ancestorChain.size(); |
| 436 | for (int i = count - 1; i >= 0; i--) { |
| 437 | View ancestor = ancestorChain.get(i); |
| 438 | View next = i > 0 ? ancestorChain.get(i-1) : null; |
| 439 | |
| 440 | pt[0] += ancestor.getScrollX(); |
| 441 | pt[1] += ancestor.getScrollY(); |
| 442 | |
| 443 | if (next != null) { |
| 444 | pt[0] -= next.getLeft(); |
| 445 | pt[1] -= next.getTop(); |
| 446 | next.getMatrix().invert(inverse); |
| 447 | inverse.mapPoints(pt); |
| 448 | scale *= next.getScaleX(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | coord[0] = (int) Math.round(pt[0]); |
| 453 | coord[1] = (int) Math.round(pt[1]); |
| 454 | return scale; |
| 455 | } |
| 456 | |
Jason Monk | 02dd7ae | 2014-04-15 15:23:31 -0400 | [diff] [blame] | 457 | /** |
| 458 | * Utility method to determine whether the given point, in local coordinates, |
| 459 | * is inside the view, where the area of the view is expanded by the slop factor. |
| 460 | * This method is called while processing touch-move events to determine if the event |
| 461 | * is still within the view. |
| 462 | */ |
| 463 | public static boolean pointInView(View v, float localX, float localY, float slop) { |
| 464 | return localX >= -slop && localY >= -slop && localX < (v.getWidth() + slop) && |
| 465 | localY < (v.getHeight() + slop); |
| 466 | } |
| 467 | |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 468 | /** Translates MotionEvents from src's coordinate system to dst's. */ |
| 469 | public static void translateEventCoordinates(View src, View dst, MotionEvent dstEvent) { |
| 470 | toGlobalMotionEvent(src, dstEvent); |
| 471 | toLocalMotionEvent(dst, dstEvent); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Emulates View.toGlobalMotionEvent(). This implementation does not handle transformations |
| 476 | * (scaleX, scaleY, etc). |
| 477 | */ |
| 478 | private static void toGlobalMotionEvent(View view, MotionEvent event) { |
| 479 | view.getLocationOnScreen(sLoc0); |
| 480 | event.offsetLocation(sLoc0[0], sLoc0[1]); |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Emulates View.toLocalMotionEvent(). This implementation does not handle transformations |
| 485 | * (scaleX, scaleY, etc). |
| 486 | */ |
| 487 | private static void toLocalMotionEvent(View view, MotionEvent event) { |
| 488 | view.getLocationOnScreen(sLoc0); |
| 489 | event.offsetLocation(-sLoc0[0], -sLoc0[1]); |
| 490 | } |
| 491 | |
Adam Cohen | 63f1ec0 | 2014-08-12 09:23:13 -0700 | [diff] [blame] | 492 | public static int[] getCenterDeltaInScreenSpace(View v0, View v1, int[] delta) { |
| 493 | v0.getLocationInWindow(sLoc0); |
| 494 | v1.getLocationInWindow(sLoc1); |
| 495 | |
| 496 | sLoc0[0] += (v0.getMeasuredWidth() * v0.getScaleX()) / 2; |
| 497 | sLoc0[1] += (v0.getMeasuredHeight() * v0.getScaleY()) / 2; |
| 498 | sLoc1[0] += (v1.getMeasuredWidth() * v1.getScaleX()) / 2; |
| 499 | sLoc1[1] += (v1.getMeasuredHeight() * v1.getScaleY()) / 2; |
| 500 | |
| 501 | if (delta == null) { |
| 502 | delta = new int[2]; |
| 503 | } |
| 504 | |
| 505 | delta[0] = sLoc1[0] - sLoc0[0]; |
| 506 | delta[1] = sLoc1[1] - sLoc0[1]; |
| 507 | |
| 508 | return delta; |
| 509 | } |
| 510 | |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 511 | public static void scaleRectAboutCenter(Rect r, float scale) { |
Sunny Goyal | 106bf64 | 2015-07-16 12:18:06 -0700 | [diff] [blame] | 512 | if (scale != 1.0f) { |
| 513 | int cx = r.centerX(); |
| 514 | int cy = r.centerY(); |
| 515 | r.offset(-cx, -cy); |
| 516 | |
| 517 | r.left = (int) (r.left * scale + 0.5f); |
| 518 | r.top = (int) (r.top * scale + 0.5f); |
| 519 | r.right = (int) (r.right * scale + 0.5f); |
| 520 | r.bottom = (int) (r.bottom * scale + 0.5f); |
| 521 | |
| 522 | r.offset(cx, cy); |
| 523 | } |
Winson Chung | 3a6e7f3 | 2013-10-09 15:50:52 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Michael Jurka | a805e1a | 2013-08-22 15:00:33 +0200 | [diff] [blame] | 526 | public static void startActivityForResultSafely( |
| 527 | Activity activity, Intent intent, int requestCode) { |
| 528 | try { |
| 529 | activity.startActivityForResult(intent, requestCode); |
| 530 | } catch (ActivityNotFoundException e) { |
| 531 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 532 | } catch (SecurityException e) { |
| 533 | Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 534 | Log.e(TAG, "Launcher does not have the permission to launch " + intent + |
| 535 | ". Make sure to create a MAIN intent-filter for the corresponding activity " + |
| 536 | "or use the exported attribute for this activity.", e); |
| 537 | } |
| 538 | } |
Sunny Goyal | c5c60ad | 2014-07-14 12:02:01 -0700 | [diff] [blame] | 539 | |
| 540 | static boolean isSystemApp(Context context, Intent intent) { |
| 541 | PackageManager pm = context.getPackageManager(); |
| 542 | ComponentName cn = intent.getComponent(); |
| 543 | String packageName = null; |
| 544 | if (cn == null) { |
| 545 | ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); |
| 546 | if ((info != null) && (info.activityInfo != null)) { |
| 547 | packageName = info.activityInfo.packageName; |
| 548 | } |
| 549 | } else { |
| 550 | packageName = cn.getPackageName(); |
| 551 | } |
| 552 | if (packageName != null) { |
| 553 | try { |
| 554 | PackageInfo info = pm.getPackageInfo(packageName, 0); |
| 555 | return (info != null) && (info.applicationInfo != null) && |
| 556 | ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0); |
| 557 | } catch (NameNotFoundException e) { |
| 558 | return false; |
| 559 | } |
| 560 | } else { |
| 561 | return false; |
| 562 | } |
| 563 | } |
Sunny Goyal | 95abbb3 | 2014-08-04 10:53:22 -0700 | [diff] [blame] | 564 | |
| 565 | /** |
| 566 | * This picks a dominant color, looking for high-saturation, high-value, repeated hues. |
| 567 | * @param bitmap The bitmap to scan |
| 568 | * @param samples The approximate max number of samples to use. |
| 569 | */ |
| 570 | static int findDominantColorByHue(Bitmap bitmap, int samples) { |
| 571 | final int height = bitmap.getHeight(); |
| 572 | final int width = bitmap.getWidth(); |
| 573 | int sampleStride = (int) Math.sqrt((height * width) / samples); |
| 574 | if (sampleStride < 1) { |
| 575 | sampleStride = 1; |
| 576 | } |
| 577 | |
| 578 | // This is an out-param, for getting the hsv values for an rgb |
| 579 | float[] hsv = new float[3]; |
| 580 | |
| 581 | // First get the best hue, by creating a histogram over 360 hue buckets, |
| 582 | // where each pixel contributes a score weighted by saturation, value, and alpha. |
| 583 | float[] hueScoreHistogram = new float[360]; |
| 584 | float highScore = -1; |
| 585 | int bestHue = -1; |
| 586 | |
| 587 | for (int y = 0; y < height; y += sampleStride) { |
| 588 | for (int x = 0; x < width; x += sampleStride) { |
| 589 | int argb = bitmap.getPixel(x, y); |
| 590 | int alpha = 0xFF & (argb >> 24); |
| 591 | if (alpha < 0x80) { |
| 592 | // Drop mostly-transparent pixels. |
| 593 | continue; |
| 594 | } |
| 595 | // Remove the alpha channel. |
| 596 | int rgb = argb | 0xFF000000; |
| 597 | Color.colorToHSV(rgb, hsv); |
| 598 | // Bucket colors by the 360 integer hues. |
| 599 | int hue = (int) hsv[0]; |
| 600 | if (hue < 0 || hue >= hueScoreHistogram.length) { |
| 601 | // Defensively avoid array bounds violations. |
| 602 | continue; |
| 603 | } |
| 604 | float score = hsv[1] * hsv[2]; |
| 605 | hueScoreHistogram[hue] += score; |
| 606 | if (hueScoreHistogram[hue] > highScore) { |
| 607 | highScore = hueScoreHistogram[hue]; |
| 608 | bestHue = hue; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | SparseArray<Float> rgbScores = new SparseArray<Float>(); |
| 614 | int bestColor = 0xff000000; |
| 615 | highScore = -1; |
| 616 | // Go back over the RGB colors that match the winning hue, |
| 617 | // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets. |
| 618 | // The highest-scoring RGB color wins. |
| 619 | for (int y = 0; y < height; y += sampleStride) { |
| 620 | for (int x = 0; x < width; x += sampleStride) { |
| 621 | int rgb = bitmap.getPixel(x, y) | 0xff000000; |
| 622 | Color.colorToHSV(rgb, hsv); |
| 623 | int hue = (int) hsv[0]; |
| 624 | if (hue == bestHue) { |
| 625 | float s = hsv[1]; |
| 626 | float v = hsv[2]; |
| 627 | int bucket = (int) (s * 100) + (int) (v * 10000); |
| 628 | // Score by cumulative saturation * value. |
| 629 | float score = s * v; |
| 630 | Float oldTotal = rgbScores.get(bucket); |
| 631 | float newTotal = oldTotal == null ? score : oldTotal + score; |
| 632 | rgbScores.put(bucket, newTotal); |
| 633 | if (newTotal > highScore) { |
| 634 | highScore = newTotal; |
| 635 | // All the colors in the winning bucket are very similar. Last in wins. |
| 636 | bestColor = rgb; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | return bestColor; |
| 642 | } |
Sunny Goyal | 0fe505b | 2014-08-06 09:55:36 -0700 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * Finds a system apk which had a broadcast receiver listening to a particular action. |
| 646 | * @param action intent action used to find the apk |
| 647 | * @return a pair of apk package name and the resources. |
| 648 | */ |
| 649 | static Pair<String, Resources> findSystemApk(String action, PackageManager pm) { |
| 650 | final Intent intent = new Intent(action); |
| 651 | for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) { |
| 652 | if (info.activityInfo != null && |
| 653 | (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 654 | final String packageName = info.activityInfo.packageName; |
| 655 | try { |
| 656 | final Resources res = pm.getResourcesForApplication(packageName); |
| 657 | return Pair.create(packageName, res); |
| 658 | } catch (NameNotFoundException e) { |
| 659 | Log.w(TAG, "Failed to find resources for " + packageName); |
| 660 | } |
| 661 | } |
| 662 | } |
| 663 | return null; |
| 664 | } |
Sunny Goyal | fafca52 | 2014-11-03 11:30:01 -0800 | [diff] [blame] | 665 | |
Sunny Goyal | 594d76d | 2014-11-06 10:12:54 -0800 | [diff] [blame] | 666 | /** |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 667 | * Compresses the bitmap to a byte array for serialization. |
| 668 | */ |
| 669 | public static byte[] flattenBitmap(Bitmap bitmap) { |
| 670 | // Try go guesstimate how much space the icon will take when serialized |
| 671 | // to avoid unnecessary allocations/copies during the write. |
| 672 | int size = bitmap.getWidth() * bitmap.getHeight() * 4; |
| 673 | ByteArrayOutputStream out = new ByteArrayOutputStream(size); |
| 674 | try { |
| 675 | bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); |
| 676 | out.flush(); |
| 677 | out.close(); |
| 678 | return out.toByteArray(); |
| 679 | } catch (IOException e) { |
| 680 | Log.w(TAG, "Could not write bitmap"); |
| 681 | return null; |
| 682 | } |
| 683 | } |
| 684 | |
Sunny Goyal | f7a29e8 | 2015-04-24 15:20:43 -0700 | [diff] [blame] | 685 | /** |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 686 | * Trims the string, removing all whitespace at the beginning and end of the string. |
| 687 | * Non-breaking whitespaces are also removed. |
| 688 | */ |
| 689 | public static String trim(CharSequence s) { |
Winson Chung | afa77e9 | 2015-05-15 12:04:03 -0700 | [diff] [blame] | 690 | if (s == null) { |
| 691 | return null; |
| 692 | } |
| 693 | |
Winson Chung | 82b016c | 2015-05-08 17:00:10 -0700 | [diff] [blame] | 694 | // Just strip any sequence of whitespace or java space characters from the beginning and end |
| 695 | Matcher m = sTrimPattern.matcher(s); |
| 696 | return m.replaceAll("$1"); |
| 697 | } |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 698 | |
Winson Chung | 2c6e5cc | 2015-06-01 14:38:24 -0700 | [diff] [blame] | 699 | /** |
| 700 | * Calculates the height of a given string at a specific text size. |
| 701 | */ |
| 702 | public static float calculateTextHeight(float textSizePx) { |
| 703 | Paint p = new Paint(); |
| 704 | p.setTextSize(textSizePx); |
| 705 | Paint.FontMetrics fm = p.getFontMetrics(); |
| 706 | return -fm.top + fm.bottom; |
| 707 | } |
| 708 | |
Winson Chung | b177744 | 2015-06-16 13:35:04 -0700 | [diff] [blame] | 709 | /** |
| 710 | * Convenience println with multiple args. |
| 711 | */ |
| 712 | public static void println(String key, Object... args) { |
| 713 | StringBuilder b = new StringBuilder(); |
| 714 | b.append(key); |
| 715 | b.append(": "); |
| 716 | boolean isFirstArgument = true; |
| 717 | for (Object arg : args) { |
| 718 | if (isFirstArgument) { |
| 719 | isFirstArgument = false; |
| 720 | } else { |
| 721 | b.append(", "); |
| 722 | } |
| 723 | b.append(arg); |
| 724 | } |
| 725 | System.out.println(b.toString()); |
| 726 | } |
| 727 | |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 728 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
| 729 | public static boolean isRtl(Resources res) { |
Sunny Goyal | 9fc953b | 2015-08-17 12:24:25 -0700 | [diff] [blame] | 730 | return ATLEAST_JB_MR1 && |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 731 | (res.getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL); |
| 732 | } |
Hyunyoung Song | 2bd3d7d | 2015-05-21 13:04:53 -0700 | [diff] [blame] | 733 | |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 734 | /** |
| 735 | * Returns true if the intent is a valid launch intent for a launcher activity of an app. |
| 736 | * This is used to identify shortcuts which are different from the ones exposed by the |
| 737 | * applications' manifest file. |
| 738 | * |
| 739 | * @param launchIntent The intent that will be launched when the shortcut is clicked. |
| 740 | */ |
| 741 | public static boolean isLauncherAppTarget(Intent launchIntent) { |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 742 | if (launchIntent != null |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 743 | && Intent.ACTION_MAIN.equals(launchIntent.getAction()) |
| 744 | && launchIntent.getComponent() != null |
| 745 | && launchIntent.getCategories() != null |
| 746 | && launchIntent.getCategories().size() == 1 |
| 747 | && launchIntent.hasCategory(Intent.CATEGORY_LAUNCHER) |
Sunny Goyal | 4e5cc64 | 2015-06-25 16:37:44 -0700 | [diff] [blame] | 748 | && TextUtils.isEmpty(launchIntent.getDataString())) { |
| 749 | // An app target can either have no extra or have ItemInfo.EXTRA_PROFILE. |
| 750 | Bundle extras = launchIntent.getExtras(); |
| 751 | if (extras == null) { |
| 752 | return true; |
| 753 | } else { |
| 754 | Set<String> keys = extras.keySet(); |
| 755 | return keys.size() == 1 && keys.contains(ItemInfo.EXTRA_PROFILE); |
| 756 | } |
| 757 | }; |
| 758 | return false; |
Sunny Goyal | 5c97f51 | 2015-05-19 16:03:28 -0700 | [diff] [blame] | 759 | } |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 760 | |
| 761 | public static float dpiFromPx(int size, DisplayMetrics metrics){ |
| 762 | float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT; |
| 763 | return (size / densityRatio); |
| 764 | } |
| 765 | public static int pxFromDp(float size, DisplayMetrics metrics) { |
| 766 | return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, |
| 767 | size, metrics)); |
| 768 | } |
| 769 | public static int pxFromSp(float size, DisplayMetrics metrics) { |
| 770 | return (int) Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, |
| 771 | size, metrics)); |
| 772 | } |
Sunny Goyal | b1622cc | 2015-06-10 16:00:42 -0700 | [diff] [blame] | 773 | |
| 774 | public static String createDbSelectionQuery(String columnName, Iterable<?> values) { |
| 775 | return String.format(Locale.ENGLISH, "%s IN (%s)", columnName, TextUtils.join(", ", values)); |
| 776 | } |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 777 | |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 778 | public static boolean isBootCompleted() { |
Sunny Goyal | dfc8b66 | 2016-08-30 12:05:48 -0700 | [diff] [blame^] | 779 | return "1".equals(getSystemProperty("sys.boot_completed", "1")); |
| 780 | } |
| 781 | |
| 782 | public static String getSystemProperty(String property, String defaultValue) { |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 783 | try { |
| 784 | Class clazz = Class.forName("android.os.SystemProperties"); |
| 785 | Method getter = clazz.getDeclaredMethod("get", String.class); |
Sunny Goyal | dfc8b66 | 2016-08-30 12:05:48 -0700 | [diff] [blame^] | 786 | String value = (String) getter.invoke(null, property); |
| 787 | if (!TextUtils.isEmpty(value)) { |
| 788 | return value; |
| 789 | } |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 790 | } catch (Exception e) { |
| 791 | Log.d(TAG, "Unable to read system properties"); |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 792 | } |
Sunny Goyal | dfc8b66 | 2016-08-30 12:05:48 -0700 | [diff] [blame^] | 793 | return defaultValue; |
Sunny Goyal | 25aba0a | 2015-07-16 15:07:47 -0700 | [diff] [blame] | 794 | } |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 795 | |
| 796 | /** |
| 797 | * Ensures that a value is within given bounds. Specifically: |
| 798 | * If value is less than lowerBound, return lowerBound; else if value is greater than upperBound, |
| 799 | * return upperBound; else return value unchanged. |
| 800 | */ |
Tony Wickham | f549dab | 2016-05-16 09:54:06 -0700 | [diff] [blame] | 801 | public static int boundToRange(int value, int lowerBound, int upperBound) { |
| 802 | return Math.max(lowerBound, Math.min(value, upperBound)); |
| 803 | } |
| 804 | |
| 805 | /** |
| 806 | * @see #boundToRange(int, int, int). |
| 807 | */ |
| 808 | public static float boundToRange(float value, float lowerBound, float upperBound) { |
Tony Wickham | 29d853c | 2015-09-08 10:35:56 -0700 | [diff] [blame] | 809 | return Math.max(lowerBound, Math.min(value, upperBound)); |
| 810 | } |
Sunny Goyal | 9d43808 | 2015-09-25 11:50:16 -0700 | [diff] [blame] | 811 | |
Sunny Goyal | e78e3d7 | 2015-09-24 11:23:31 -0700 | [diff] [blame] | 812 | /** |
| 813 | * Wraps a message with a TTS span, so that a different message is spoken than |
| 814 | * what is getting displayed. |
| 815 | * @param msg original message |
| 816 | * @param ttsMsg message to be spoken |
| 817 | */ |
| 818 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 819 | public static CharSequence wrapForTts(CharSequence msg, String ttsMsg) { |
| 820 | if (Utilities.ATLEAST_LOLLIPOP) { |
| 821 | SpannableString spanned = new SpannableString(msg); |
| 822 | spanned.setSpan(new TtsSpan.TextBuilder(ttsMsg).build(), |
| 823 | 0, spanned.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); |
| 824 | return spanned; |
| 825 | } else { |
| 826 | return msg; |
| 827 | } |
| 828 | } |
Winson | 12fb9fc | 2015-10-01 15:34:08 -0700 | [diff] [blame] | 829 | |
| 830 | /** |
| 831 | * Replacement for Long.compare() which was added in API level 19. |
| 832 | */ |
| 833 | public static int longCompare(long lhs, long rhs) { |
| 834 | return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1); |
| 835 | } |
Sunny Goyal | f725824 | 2015-10-19 16:59:07 -0700 | [diff] [blame] | 836 | |
| 837 | public static SharedPreferences getPrefs(Context context) { |
| 838 | return context.getSharedPreferences( |
| 839 | LauncherFiles.SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE); |
| 840 | } |
Tony Wickham | 112ac95 | 2015-11-12 12:31:50 -0800 | [diff] [blame] | 841 | |
| 842 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 843 | public static boolean isPowerSaverOn(Context context) { |
| 844 | PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 845 | return ATLEAST_LOLLIPOP && powerManager.isPowerSaveMode(); |
| 846 | } |
Sunny Goyal | e1483fb | 2016-01-08 12:16:15 -0800 | [diff] [blame] | 847 | |
Sunny Goyal | 1ed6c4a | 2016-04-21 15:16:11 -0700 | [diff] [blame] | 848 | public static boolean isWallapaperAllowed(Context context) { |
| 849 | if (isNycOrAbove()) { |
| 850 | try { |
| 851 | WallpaperManager wm = context.getSystemService(WallpaperManager.class); |
Kenny Guy | 367da9f | 2016-05-25 19:22:14 +0100 | [diff] [blame] | 852 | return (Boolean) wm.getClass().getDeclaredMethod("isSetWallpaperAllowed") |
Sunny Goyal | 1ed6c4a | 2016-04-21 15:16:11 -0700 | [diff] [blame] | 853 | .invoke(wm); |
| 854 | } catch (Exception e) { } |
| 855 | } |
| 856 | return true; |
| 857 | } |
| 858 | |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 859 | public static void closeSilently(Closeable c) { |
| 860 | if (c != null) { |
| 861 | try { |
| 862 | c.close(); |
| 863 | } catch (IOException e) { |
| 864 | if (ProviderConfig.IS_DOGFOOD_BUILD) { |
| 865 | Log.d(TAG, "Error closing", e); |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
Sunny Goyal | e1483fb | 2016-01-08 12:16:15 -0800 | [diff] [blame] | 871 | /** |
Sunny Goyal | 3f73219 | 2016-05-13 13:48:44 -0700 | [diff] [blame] | 872 | * Returns true if {@param original} contains all entries defined in {@param updates} and |
| 873 | * have the same value. |
| 874 | * The comparison uses {@link Object#equals(Object)} to compare the values. |
| 875 | */ |
| 876 | public static boolean containsAll(Bundle original, Bundle updates) { |
| 877 | for (String key : updates.keySet()) { |
| 878 | Object value1 = updates.get(key); |
| 879 | Object value2 = original.get(key); |
| 880 | if (value1 == null) { |
| 881 | if (value2 != null) { |
| 882 | return false; |
| 883 | } |
| 884 | } else if (!value1.equals(value2)) { |
| 885 | return false; |
| 886 | } |
| 887 | } |
| 888 | return true; |
| 889 | } |
| 890 | |
Tony Wickham | 1bce7fd | 2016-04-28 17:39:03 -0700 | [diff] [blame] | 891 | /** Returns whether the collection is null or empty. */ |
| 892 | public static boolean isEmpty(Collection c) { |
| 893 | return c == null || c.isEmpty(); |
| 894 | } |
| 895 | |
Sunny Goyal | 3f73219 | 2016-05-13 13:48:44 -0700 | [diff] [blame] | 896 | /** |
Sunny Goyal | e1483fb | 2016-01-08 12:16:15 -0800 | [diff] [blame] | 897 | * An extension of {@link BitmapDrawable} which returns the bitmap pixel size as intrinsic size. |
| 898 | * This allows the badging to be done based on the action bitmap size rather than |
| 899 | * the scaled bitmap size. |
| 900 | */ |
| 901 | private static class FixedSizeBitmapDrawable extends BitmapDrawable { |
| 902 | |
| 903 | public FixedSizeBitmapDrawable(Bitmap bitmap) { |
| 904 | super(null, bitmap); |
| 905 | } |
| 906 | |
| 907 | @Override |
| 908 | public int getIntrinsicHeight() { |
| 909 | return getBitmap().getWidth(); |
| 910 | } |
| 911 | |
| 912 | @Override |
| 913 | public int getIntrinsicWidth() { |
| 914 | return getBitmap().getWidth(); |
| 915 | } |
| 916 | } |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 917 | |
| 918 | public static int getColorAccent(Context context) { |
| 919 | TypedArray ta = context.obtainStyledAttributes(new int[]{android.R.attr.colorAccent}); |
| 920 | int colorAccent = ta.getColor(0, 0); |
| 921 | ta.recycle(); |
| 922 | return colorAccent; |
| 923 | } |
The Android Open Source Project | 31dd503 | 2009-03-03 19:32:27 -0800 | [diff] [blame] | 924 | } |