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