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