Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package android.content.pm; |
| 18 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| 23 | import android.annotation.SdkConstant.SdkConstantType; |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 24 | import android.annotation.SystemService; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 26 | import android.app.PendingIntent; |
| 27 | import android.appwidget.AppWidgetManager; |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 28 | import android.appwidget.AppWidgetProviderInfo; |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 29 | import android.content.ActivityNotFoundException; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 33 | import android.content.IntentSender; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 34 | import android.content.pm.PackageManager.ApplicationInfoFlags; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 35 | import android.content.pm.PackageManager.NameNotFoundException; |
| 36 | import android.content.res.Resources; |
| 37 | import android.graphics.Bitmap; |
| 38 | import android.graphics.BitmapFactory; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 39 | import android.graphics.Rect; |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 40 | import android.graphics.drawable.AdaptiveIconDrawable; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 41 | import android.graphics.drawable.BitmapDrawable; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 42 | import android.graphics.drawable.Drawable; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 43 | import android.graphics.drawable.Icon; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 44 | import android.os.Bundle; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 45 | import android.os.Handler; |
| 46 | import android.os.Looper; |
| 47 | import android.os.Message; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 48 | import android.os.Parcel; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 49 | import android.os.ParcelFileDescriptor; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 50 | import android.os.Parcelable; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 51 | import android.os.RemoteException; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 52 | import android.os.ServiceManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 53 | import android.os.UserHandle; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 54 | import android.os.UserManager; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 55 | import android.util.DisplayMetrics; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 56 | import android.util.Log; |
| 57 | |
Makoto Onuki | 7c7fbf6 | 2017-04-14 11:03:56 -0700 | [diff] [blame] | 58 | import com.android.internal.util.Preconditions; |
| 59 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 60 | import java.io.IOException; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 61 | import java.lang.annotation.Retention; |
| 62 | import java.lang.annotation.RetentionPolicy; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 63 | import java.util.ArrayList; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 64 | import java.util.Arrays; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 65 | import java.util.Collections; |
| 66 | import java.util.List; |
| 67 | |
| 68 | /** |
| 69 | * Class for retrieving a list of launchable activities for the current user and any associated |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 70 | * managed profiles that are visible to the current user, which can be retrieved with |
| 71 | * {@link #getProfiles}. This is mainly for use by launchers. |
| 72 | * |
| 73 | * Apps can be queried for each user profile. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 74 | * Since the PackageManager will not deliver package broadcasts for other profiles, you can register |
| 75 | * for package changes here. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 76 | * <p> |
| 77 | * To watch for managed profiles being added or removed, register for the following broadcasts: |
| 78 | * {@link Intent#ACTION_MANAGED_PROFILE_ADDED} and {@link Intent#ACTION_MANAGED_PROFILE_REMOVED}. |
| 79 | * <p> |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 80 | * Note as of Android O, apps on a managed profile are no longer allowed to access apps on the |
| 81 | * main profile. Apps can only access profiles returned by {@link #getProfiles()}. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 82 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 83 | @SystemService(Context.LAUNCHER_APPS_SERVICE) |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 84 | public class LauncherApps { |
| 85 | |
| 86 | static final String TAG = "LauncherApps"; |
| 87 | static final boolean DEBUG = false; |
| 88 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 89 | /** |
| 90 | * Activity Action: For the default launcher to show the confirmation dialog to create |
| 91 | * a pinned shortcut. |
| 92 | * |
| 93 | * <p>See the {@link ShortcutManager} javadoc for details. |
| 94 | * |
| 95 | * <p> |
| 96 | * Use {@link #getPinItemRequest(Intent)} to get a {@link PinItemRequest} object, |
| 97 | * and call {@link PinItemRequest#accept(Bundle)} |
| 98 | * if the user accepts. If the user doesn't accept, no further action is required. |
| 99 | * |
| 100 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 101 | */ |
| 102 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 103 | public static final String ACTION_CONFIRM_PIN_SHORTCUT = |
| 104 | "android.content.pm.action.CONFIRM_PIN_SHORTCUT"; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 105 | |
| 106 | /** |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 107 | * Activity Action: For the default launcher to show the confirmation dialog to create |
| 108 | * a pinned app widget. |
| 109 | * |
| 110 | * <p>See the {@link android.appwidget.AppWidgetManager#requestPinAppWidget} javadoc for |
| 111 | * details. |
| 112 | * |
| 113 | * <p> |
| 114 | * Use {@link #getPinItemRequest(Intent)} to get a {@link PinItemRequest} object, |
| 115 | * and call {@link PinItemRequest#accept(Bundle)} |
| 116 | * if the user accepts. If the user doesn't accept, no further action is required. |
| 117 | * |
| 118 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 119 | */ |
| 120 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 121 | public static final String ACTION_CONFIRM_PIN_APPWIDGET = |
| 122 | "android.content.pm.action.CONFIRM_PIN_APPWIDGET"; |
| 123 | |
| 124 | /** |
| 125 | * An extra for {@link #ACTION_CONFIRM_PIN_SHORTCUT} & {@link #ACTION_CONFIRM_PIN_APPWIDGET} |
| 126 | * containing a {@link PinItemRequest} of appropriate type asked to pin. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 127 | * |
| 128 | * <p>A helper function {@link #getPinItemRequest(Intent)} can be used |
| 129 | * instead of using this constant directly. |
| 130 | * |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 131 | * @see #ACTION_CONFIRM_PIN_SHORTCUT |
| 132 | * @see #ACTION_CONFIRM_PIN_APPWIDGET |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 133 | */ |
| 134 | public static final String EXTRA_PIN_ITEM_REQUEST = |
| 135 | "android.content.pm.extra.PIN_ITEM_REQUEST"; |
| 136 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 137 | private final Context mContext; |
| 138 | private final ILauncherApps mService; |
| 139 | private final PackageManager mPm; |
| 140 | private final UserManager mUserManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 141 | |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 142 | private List<CallbackMessageHandler> mCallbacks |
| 143 | = new ArrayList<CallbackMessageHandler>(); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Callbacks for package changes to this and related managed profiles. |
| 147 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 148 | public static abstract class Callback { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 149 | /** |
| 150 | * Indicates that a package was removed from the specified profile. |
| 151 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 152 | * If a package is removed while being updated onPackageChanged will be |
| 153 | * called instead. |
| 154 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 155 | * @param packageName The name of the package that was removed. |
| 156 | * @param user The UserHandle of the profile that generated the change. |
| 157 | */ |
| 158 | abstract public void onPackageRemoved(String packageName, UserHandle user); |
| 159 | |
| 160 | /** |
| 161 | * Indicates that a package was added to the specified profile. |
| 162 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 163 | * If a package is added while being updated then onPackageChanged will be |
| 164 | * called instead. |
| 165 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 166 | * @param packageName The name of the package that was added. |
| 167 | * @param user The UserHandle of the profile that generated the change. |
| 168 | */ |
| 169 | abstract public void onPackageAdded(String packageName, UserHandle user); |
| 170 | |
| 171 | /** |
| 172 | * Indicates that a package was modified in the specified profile. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 173 | * This can happen, for example, when the package is updated or when |
| 174 | * one or more components are enabled or disabled. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 175 | * |
| 176 | * @param packageName The name of the package that has changed. |
| 177 | * @param user The UserHandle of the profile that generated the change. |
| 178 | */ |
| 179 | abstract public void onPackageChanged(String packageName, UserHandle user); |
| 180 | |
| 181 | /** |
| 182 | * Indicates that one or more packages have become available. For |
| 183 | * example, this can happen when a removable storage card has |
| 184 | * reappeared. |
| 185 | * |
| 186 | * @param packageNames The names of the packages that have become |
| 187 | * available. |
| 188 | * @param user The UserHandle of the profile that generated the change. |
| 189 | * @param replacing Indicates whether these packages are replacing |
| 190 | * existing ones. |
| 191 | */ |
| 192 | abstract public void onPackagesAvailable(String[] packageNames, UserHandle user, |
| 193 | boolean replacing); |
| 194 | |
| 195 | /** |
| 196 | * Indicates that one or more packages have become unavailable. For |
| 197 | * example, this can happen when a removable storage card has been |
| 198 | * removed. |
| 199 | * |
| 200 | * @param packageNames The names of the packages that have become |
| 201 | * unavailable. |
| 202 | * @param user The UserHandle of the profile that generated the change. |
| 203 | * @param replacing Indicates whether the packages are about to be |
| 204 | * replaced with new versions. |
| 205 | */ |
| 206 | abstract public void onPackagesUnavailable(String[] packageNames, UserHandle user, |
| 207 | boolean replacing); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Indicates that one or more packages have been suspended. For |
| 211 | * example, this can happen when a Device Administrator suspends |
| 212 | * an applicaton. |
| 213 | * |
| 214 | * @param packageNames The names of the packages that have just been |
| 215 | * suspended. |
| 216 | * @param user The UserHandle of the profile that generated the change. |
| 217 | */ |
| 218 | public void onPackagesSuspended(String[] packageNames, UserHandle user) { |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Indicates that one or more packages have been unsuspended. For |
| 223 | * example, this can happen when a Device Administrator unsuspends |
| 224 | * an applicaton. |
| 225 | * |
| 226 | * @param packageNames The names of the packages that have just been |
| 227 | * unsuspended. |
| 228 | * @param user The UserHandle of the profile that generated the change. |
| 229 | */ |
| 230 | public void onPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 231 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 232 | |
| 233 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 234 | * Indicates that one or more shortcuts of any kind (dynamic, pinned, or manifest) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 235 | * have been added, updated or removed. |
| 236 | * |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 237 | * <p>Only the applications that are allowed to access the shortcut information, |
| 238 | * as defined in {@link #hasShortcutHostPermission()}, will receive it. |
| 239 | * |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 240 | * @param packageName The name of the package that has the shortcuts. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 241 | * @param shortcuts All shortcuts from the package (dynamic, manifest and/or pinned). |
| 242 | * Only "key" information will be provided, as defined in |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 243 | * {@link ShortcutInfo#hasKeyFieldsOnly()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 244 | * @param user The UserHandle of the profile that generated the change. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 245 | * |
| 246 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 247 | */ |
| 248 | public void onShortcutsChanged(@NonNull String packageName, |
| 249 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) { |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Represents a query passed to {@link #getShortcuts(ShortcutQuery, UserHandle)}. |
| 255 | */ |
| 256 | public static class ShortcutQuery { |
| 257 | /** |
| 258 | * Include dynamic shortcuts in the result. |
| 259 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 260 | public static final int FLAG_MATCH_DYNAMIC = 1 << 0; |
| 261 | |
| 262 | /** @hide kept for unit tests */ |
| 263 | @Deprecated |
| 264 | public static final int FLAG_GET_DYNAMIC = FLAG_MATCH_DYNAMIC; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Include pinned shortcuts in the result. |
| 268 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 269 | public static final int FLAG_MATCH_PINNED = 1 << 1; |
| 270 | |
| 271 | /** @hide kept for unit tests */ |
| 272 | @Deprecated |
| 273 | public static final int FLAG_GET_PINNED = FLAG_MATCH_PINNED; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 274 | |
| 275 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 276 | * Include manifest shortcuts in the result. |
| 277 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 278 | public static final int FLAG_MATCH_MANIFEST = 1 << 3; |
| 279 | |
| 280 | /** @hide kept for unit tests */ |
| 281 | @Deprecated |
| 282 | public static final int FLAG_GET_MANIFEST = FLAG_MATCH_MANIFEST; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 283 | |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 284 | /** |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 285 | * @hide include all pinned shortcuts by any launchers, not just by the caller, |
| 286 | * in the result. |
| 287 | * If the caller doesn't havve the {@link android.Manifest.permission#ACCESS_SHORTCUTS} |
| 288 | * permission, this flag will be ignored. |
| 289 | */ |
| 290 | @TestApi |
| 291 | public static final int FLAG_MATCH_ALL_PINNED = 1 << 10; |
| 292 | |
| 293 | /** |
| 294 | * FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 295 | * @hide |
| 296 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 297 | public static final int FLAG_MATCH_ALL_KINDS = |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 298 | FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST; |
| 299 | |
| 300 | /** |
| 301 | * FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST | FLAG_MATCH_ALL_PINNED |
| 302 | * @hide |
| 303 | */ |
| 304 | public static final int FLAG_MATCH_ALL_KINDS_WITH_ALL_PINNED = |
| 305 | FLAG_MATCH_ALL_KINDS | FLAG_MATCH_ALL_PINNED; |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 306 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 307 | /** @hide kept for unit tests */ |
| 308 | @Deprecated |
| 309 | public static final int FLAG_GET_ALL_KINDS = FLAG_MATCH_ALL_KINDS; |
| 310 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 311 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 312 | * Requests "key" fields only. See {@link ShortcutInfo#hasKeyFieldsOnly()}'s javadoc to |
| 313 | * see which fields fields "key". |
| 314 | * This allows quicker access to shortcut information in order to |
| 315 | * determine whether the caller's in-memory cache needs to be updated. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 316 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 317 | * <p>Typically, launcher applications cache all or most shortcut information |
| 318 | * in memory in order to show shortcuts without a delay. |
| 319 | * |
| 320 | * When a given launcher application wants to update its cache, such as when its process |
| 321 | * restarts, it can fetch shortcut information with this flag. |
| 322 | * The application can then check {@link ShortcutInfo#getLastChangedTimestamp()} for each |
| 323 | * shortcut, fetching a shortcut's non-key information only if that shortcut has been |
| 324 | * updated. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 325 | * |
| 326 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 327 | */ |
| 328 | public static final int FLAG_GET_KEY_FIELDS_ONLY = 1 << 2; |
| 329 | |
| 330 | /** @hide */ |
| 331 | @IntDef(flag = true, |
| 332 | value = { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 333 | FLAG_MATCH_DYNAMIC, |
| 334 | FLAG_MATCH_PINNED, |
| 335 | FLAG_MATCH_MANIFEST, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 336 | FLAG_GET_KEY_FIELDS_ONLY, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 337 | FLAG_MATCH_MANIFEST, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 338 | }) |
| 339 | @Retention(RetentionPolicy.SOURCE) |
| 340 | public @interface QueryFlags {} |
| 341 | |
| 342 | long mChangedSince; |
| 343 | |
| 344 | @Nullable |
| 345 | String mPackage; |
| 346 | |
| 347 | @Nullable |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 348 | List<String> mShortcutIds; |
| 349 | |
| 350 | @Nullable |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 351 | ComponentName mActivity; |
| 352 | |
| 353 | @QueryFlags |
| 354 | int mQueryFlags; |
| 355 | |
| 356 | public ShortcutQuery() { |
| 357 | } |
| 358 | |
| 359 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 360 | * If non-zero, returns only shortcuts that have been added or updated |
| 361 | * since the given timestamp, expressed in milliseconds since the Epoch—see |
| 362 | * {@link System#currentTimeMillis()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 363 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 364 | public ShortcutQuery setChangedSince(long changedSince) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 365 | mChangedSince = changedSince; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 366 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /** |
| 370 | * If non-null, returns only shortcuts from the package. |
| 371 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 372 | public ShortcutQuery setPackage(@Nullable String packageName) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 373 | mPackage = packageName; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 374 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /** |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 378 | * If non-null, return only the specified shortcuts by ID. When setting this field, |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 379 | * a package name must also be set with {@link #setPackage}. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 380 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 381 | public ShortcutQuery setShortcutIds(@Nullable List<String> shortcutIds) { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 382 | mShortcutIds = shortcutIds; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 383 | return this; |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 387 | * If non-null, returns only shortcuts associated with the activity; i.e. |
| 388 | * {@link ShortcutInfo}s whose {@link ShortcutInfo#getActivity()} are equal |
| 389 | * to {@code activity}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 390 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 391 | public ShortcutQuery setActivity(@Nullable ComponentName activity) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 392 | mActivity = activity; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 393 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 394 | } |
| 395 | |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 396 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 397 | * Set query options. At least one of the {@code MATCH} flags should be set. Otherwise, |
| 398 | * no shortcuts will be returned. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 399 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 400 | * <ul> |
| 401 | * <li>{@link #FLAG_MATCH_DYNAMIC} |
| 402 | * <li>{@link #FLAG_MATCH_PINNED} |
| 403 | * <li>{@link #FLAG_MATCH_MANIFEST} |
| 404 | * <li>{@link #FLAG_GET_KEY_FIELDS_ONLY} |
| 405 | * </ul> |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 406 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 407 | public ShortcutQuery setQueryFlags(@QueryFlags int queryFlags) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 408 | mQueryFlags = queryFlags; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 409 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 410 | } |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 411 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 412 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 413 | /** @hide */ |
| 414 | public LauncherApps(Context context, ILauncherApps service) { |
| 415 | mContext = context; |
| 416 | mService = service; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 417 | mPm = context.getPackageManager(); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 418 | mUserManager = context.getSystemService(UserManager.class); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 421 | /** @hide */ |
| 422 | @TestApi |
| 423 | public LauncherApps(Context context) { |
| 424 | this(context, ILauncherApps.Stub.asInterface( |
| 425 | ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE))); |
| 426 | } |
| 427 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 428 | /** |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 429 | * Show an error log on logcat, when the calling user is a managed profile, and the target |
| 430 | * user is different from the calling user, in order to help developers to detect it. |
| 431 | */ |
| 432 | private void logErrorForInvalidProfileAccess(@NonNull UserHandle target) { |
| 433 | if (UserHandle.myUserId() != target.getIdentifier() && mUserManager.isManagedProfile()) { |
Makoto Onuki | 3cc7cd1 | 2017-04-03 12:48:42 -0700 | [diff] [blame] | 434 | Log.w(TAG, "Accessing other profiles/users from managed profile is no longer allowed."); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
| 438 | /** |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 439 | * Return a list of profiles that the caller can access via the {@link LauncherApps} APIs. |
| 440 | * |
| 441 | * <p>If the caller is running on a managed profile, it'll return only the current profile. |
| 442 | * Otherwise it'll return the same list as {@link UserManager#getUserProfiles()} would. |
| 443 | */ |
| 444 | public List<UserHandle> getProfiles() { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 445 | if (mUserManager.isManagedProfile()) { |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 446 | // If it's a managed profile, only return the current profile. |
| 447 | final List result = new ArrayList(1); |
| 448 | result.add(android.os.Process.myUserHandle()); |
| 449 | return result; |
| 450 | } else { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 451 | return mUserManager.getUserProfiles(); |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | /** |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 456 | * Retrieves a list of launchable activities that match {@link Intent#ACTION_MAIN} and |
| 457 | * {@link Intent#CATEGORY_LAUNCHER}, for a specified user. |
| 458 | * |
| 459 | * @param packageName The specific package to query. If null, it checks all installed packages |
| 460 | * in the profile. |
| 461 | * @param user The UserHandle of the profile. |
| 462 | * @return List of launchable activities. Can be an empty list but will not be null. |
| 463 | */ |
| 464 | public List<LauncherActivityInfo> getActivityList(String packageName, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 465 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 466 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 467 | return convertToActivityList(mService.getLauncherActivities(mContext.getPackageName(), |
| 468 | packageName, user), user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 469 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 470 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 471 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 474 | /** |
| 475 | * Returns the activity info for a given intent and user handle, if it resolves. Otherwise it |
| 476 | * returns null. |
| 477 | * |
| 478 | * @param intent The intent to find a match for. |
| 479 | * @param user The profile to look in for a match. |
| 480 | * @return An activity info object if there is a match. |
| 481 | */ |
| 482 | public LauncherActivityInfo resolveActivity(Intent intent, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 483 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 484 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 485 | ActivityInfo ai = mService.resolveActivity(mContext.getPackageName(), |
| 486 | intent.getComponent(), user); |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 487 | if (ai != null) { |
| 488 | LauncherActivityInfo info = new LauncherActivityInfo(mContext, ai, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 489 | return info; |
| 490 | } |
| 491 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 492 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 493 | } |
| 494 | return null; |
| 495 | } |
| 496 | |
| 497 | /** |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 498 | * Starts a Main activity in the specified profile. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 499 | * |
| 500 | * @param component The ComponentName of the activity to launch |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 501 | * @param user The UserHandle of the profile |
| 502 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 503 | * @param opts Options to pass to startActivity |
| 504 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 505 | public void startMainActivity(ComponentName component, UserHandle user, Rect sourceBounds, |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 506 | Bundle opts) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 507 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 508 | if (DEBUG) { |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 509 | Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier()); |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 510 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 511 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 512 | mService.startActivityAsUser(mContext.getPackageName(), |
| 513 | component, sourceBounds, opts, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 514 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 515 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /** |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 520 | * Starts the settings activity to show the application details for a |
| 521 | * package in the specified profile. |
| 522 | * |
| 523 | * @param component The ComponentName of the package to launch settings for. |
| 524 | * @param user The UserHandle of the profile |
| 525 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 526 | * @param opts Options to pass to startActivity |
| 527 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 528 | public void startAppDetailsActivity(ComponentName component, UserHandle user, |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 529 | Rect sourceBounds, Bundle opts) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 530 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 531 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 532 | mService.showAppDetailsAsUser(mContext.getPackageName(), |
| 533 | component, sourceBounds, opts, user); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 534 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 535 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | /** |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 540 | * Retrieves a list of config activities for creating {@link ShortcutInfo}. |
| 541 | * |
| 542 | * @param packageName The specific package to query. If null, it checks all installed packages |
| 543 | * in the profile. |
| 544 | * @param user The UserHandle of the profile. |
| 545 | * @return List of config activities. Can be an empty list but will not be null. |
| 546 | * |
| 547 | * @see Intent#ACTION_CREATE_SHORTCUT |
| 548 | * @see #getShortcutConfigActivityIntent(LauncherActivityInfo) |
| 549 | */ |
| 550 | public List<LauncherActivityInfo> getShortcutConfigActivityList(@Nullable String packageName, |
| 551 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 552 | logErrorForInvalidProfileAccess(user); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 553 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 554 | return convertToActivityList(mService.getShortcutConfigActivities( |
| 555 | mContext.getPackageName(), packageName, user), |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 556 | user); |
| 557 | } catch (RemoteException re) { |
| 558 | throw re.rethrowFromSystemServer(); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | private List<LauncherActivityInfo> convertToActivityList( |
| 563 | @Nullable ParceledListSlice<ResolveInfo> activities, UserHandle user) { |
| 564 | if (activities == null) { |
| 565 | return Collections.EMPTY_LIST; |
| 566 | } |
| 567 | ArrayList<LauncherActivityInfo> lais = new ArrayList<>(); |
| 568 | for (ResolveInfo ri : activities.getList()) { |
| 569 | LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri.activityInfo, user); |
| 570 | if (DEBUG) { |
| 571 | Log.v(TAG, "Returning activity for profile " + user + " : " |
| 572 | + lai.getComponentName()); |
| 573 | } |
| 574 | lais.add(lai); |
| 575 | } |
| 576 | return lais; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Returns an intent sender which can be used to start the configure activity for creating |
| 581 | * custom shortcuts. Use this method if the provider is in another profile as you are not |
| 582 | * allowed to start an activity in another profile. |
| 583 | * |
| 584 | * <p>The caller should receive {@link PinItemRequest} in onActivityResult on |
| 585 | * {@link android.app.Activity#RESULT_OK}. |
| 586 | * |
| 587 | * <p>Callers must be allowed to access the shortcut information, as defined in {@link |
| 588 | * #hasShortcutHostPermission()}. |
| 589 | * |
| 590 | * @param info a configuration activity returned by {@link #getShortcutConfigActivityList} |
| 591 | * |
| 592 | * @throws IllegalStateException when the user is locked or not running. |
| 593 | * @throws SecurityException if {@link #hasShortcutHostPermission()} is false. |
| 594 | * |
| 595 | * @see #getPinItemRequest(Intent) |
| 596 | * @see Intent#ACTION_CREATE_SHORTCUT |
| 597 | * @see android.app.Activity#startIntentSenderForResult |
| 598 | */ |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 599 | @Nullable |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 600 | public IntentSender getShortcutConfigActivityIntent(@NonNull LauncherActivityInfo info) { |
| 601 | try { |
| 602 | return mService.getShortcutConfigActivityIntent( |
| 603 | mContext.getPackageName(), info.getComponentName(), info.getUser()); |
| 604 | } catch (RemoteException re) { |
| 605 | throw re.rethrowFromSystemServer(); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 610 | * Checks if the package is installed and enabled for a profile. |
| 611 | * |
| 612 | * @param packageName The package to check. |
| 613 | * @param user The UserHandle of the profile. |
| 614 | * |
| 615 | * @return true if the package exists and is enabled. |
| 616 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 617 | public boolean isPackageEnabled(String packageName, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 618 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 619 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 620 | return mService.isPackageEnabled(mContext.getPackageName(), packageName, user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 621 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 622 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
Benjamin Miller | 7afa84c | 2017-07-17 13:01:35 +0200 | [diff] [blame] | 627 | * Returns {@link ApplicationInfo} about an application installed for a specific user profile. |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 628 | * |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 629 | * @param packageName The package name of the application |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 630 | * @param flags Additional option flags {@link PackageManager#getApplicationInfo} |
| 631 | * @param user The UserHandle of the profile. |
| 632 | * |
Benjamin Miller | 7afa84c | 2017-07-17 13:01:35 +0200 | [diff] [blame] | 633 | * @return {@link ApplicationInfo} containing information about the package. Returns |
| 634 | * {@code null} if the package isn't installed for the given profile, or the profile |
| 635 | * isn't enabled. |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 636 | */ |
Makoto Onuki | 7c7fbf6 | 2017-04-14 11:03:56 -0700 | [diff] [blame] | 637 | public ApplicationInfo getApplicationInfo(@NonNull String packageName, |
| 638 | @ApplicationInfoFlags int flags, @NonNull UserHandle user) |
| 639 | throws PackageManager.NameNotFoundException { |
| 640 | Preconditions.checkNotNull(packageName, "packageName"); |
| 641 | Preconditions.checkNotNull(packageName, "user"); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 642 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 643 | try { |
Makoto Onuki | 7c7fbf6 | 2017-04-14 11:03:56 -0700 | [diff] [blame] | 644 | final ApplicationInfo ai = mService |
| 645 | .getApplicationInfo(mContext.getPackageName(), packageName, flags, user); |
| 646 | if (ai == null) { |
| 647 | throw new NameNotFoundException("Package " + packageName + " not found for user " |
| 648 | + user.getIdentifier()); |
| 649 | } |
| 650 | return ai; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 651 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 652 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
| 656 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 657 | * Checks if the activity exists and it enabled for a profile. |
| 658 | * |
| 659 | * @param component The activity to check. |
| 660 | * @param user The UserHandle of the profile. |
| 661 | * |
| 662 | * @return true if the activity exists and is enabled. |
| 663 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 664 | public boolean isActivityEnabled(ComponentName component, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 665 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 666 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 667 | return mService.isActivityEnabled(mContext.getPackageName(), component, user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 668 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 669 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 673 | /** |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 674 | * Returns whether the caller can access the shortcut information. |
| 675 | * |
| 676 | * <p>Only the default launcher can access the shortcut information. |
| 677 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 678 | * <p>Note when this method returns {@code false}, it may be a temporary situation because |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 679 | * the user is trying a new launcher application. The user may decide to change the default |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 680 | * launcher back to the calling application again, so even if a launcher application loses |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 681 | * this permission, it does <b>not</b> have to purge pinned shortcut information. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 682 | * If the calling launcher application contains pinned shortcuts, they will still work, |
| 683 | * even though the caller no longer has the shortcut host permission. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 684 | * |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 685 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 686 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 687 | * @see ShortcutManager |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 688 | */ |
| 689 | public boolean hasShortcutHostPermission() { |
| 690 | try { |
| 691 | return mService.hasShortcutHostPermission(mContext.getPackageName()); |
| 692 | } catch (RemoteException re) { |
| 693 | throw re.rethrowFromSystemServer(); |
| 694 | } |
| 695 | } |
| 696 | |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 697 | private List<ShortcutInfo> maybeUpdateDisabledMessage(List<ShortcutInfo> shortcuts) { |
| 698 | if (shortcuts == null) { |
| 699 | return null; |
| 700 | } |
| 701 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 702 | final ShortcutInfo si = shortcuts.get(i); |
| 703 | final String message = ShortcutInfo.getDisabledReasonForRestoreIssue(mContext, |
| 704 | si.getDisabledReason()); |
| 705 | if (message != null) { |
| 706 | si.setDisabledMessage(message); |
| 707 | } |
| 708 | } |
| 709 | return shortcuts; |
| 710 | } |
| 711 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 712 | /** |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 713 | * Returns {@link ShortcutInfo}s that match {@code query}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 714 | * |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 715 | * <p>Callers must be allowed to access the shortcut information, as defined in {@link |
| 716 | * #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 717 | * |
| 718 | * @param query result includes shortcuts matching this query. |
| 719 | * @param user The UserHandle of the profile. |
| 720 | * |
| 721 | * @return the IDs of {@link ShortcutInfo}s that match the query. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 722 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 723 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 724 | * |
| 725 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 726 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 727 | @Nullable |
| 728 | public List<ShortcutInfo> getShortcuts(@NonNull ShortcutQuery query, |
| 729 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 730 | logErrorForInvalidProfileAccess(user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 731 | try { |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 732 | // Note this is the only case we need to update the disabled message for shortcuts |
| 733 | // that weren't restored. |
| 734 | // The restore problem messages are only shown by the user, and publishers will never |
| 735 | // see them. The only other API that the launcher gets shortcuts is the shortcut |
| 736 | // changed callback, but that only returns shortcuts with the "key" information, so |
| 737 | // that won't return disabled message. |
| 738 | return maybeUpdateDisabledMessage(mService.getShortcuts(mContext.getPackageName(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 739 | query.mChangedSince, query.mPackage, query.mShortcutIds, query.mActivity, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 740 | query.mQueryFlags, user) |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 741 | .getList()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 742 | } catch (RemoteException e) { |
| 743 | throw e.rethrowFromSystemServer(); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 748 | * @hide // No longer used. Use getShortcuts() instead. Kept for unit tests. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 749 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 750 | @Nullable |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 751 | @Deprecated |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 752 | public List<ShortcutInfo> getShortcutInfo(@NonNull String packageName, |
| 753 | @NonNull List<String> ids, @NonNull UserHandle user) { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 754 | final ShortcutQuery q = new ShortcutQuery(); |
| 755 | q.setPackage(packageName); |
| 756 | q.setShortcutIds(ids); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 757 | q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 758 | return getShortcuts(q, user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 759 | } |
| 760 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 761 | /** |
| 762 | * Pin shortcuts on a package. |
| 763 | * |
| 764 | * <p>This API is <b>NOT</b> cumulative; this will replace all pinned shortcuts for the package. |
| 765 | * However, different launchers may have different set of pinned shortcuts. |
| 766 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 767 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 768 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 769 | * |
| 770 | * @param packageName The target package name. |
| 771 | * @param shortcutIds The IDs of the shortcut to be pinned. |
| 772 | * @param user The UserHandle of the profile. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 773 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 774 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 775 | * |
| 776 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 777 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 778 | public void pinShortcuts(@NonNull String packageName, @NonNull List<String> shortcutIds, |
| 779 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 780 | logErrorForInvalidProfileAccess(user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 781 | try { |
| 782 | mService.pinShortcuts(mContext.getPackageName(), packageName, shortcutIds, user); |
| 783 | } catch (RemoteException e) { |
| 784 | throw e.rethrowFromSystemServer(); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 789 | * @hide kept for testing. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 790 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 791 | @Deprecated |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 792 | public int getShortcutIconResId(@NonNull ShortcutInfo shortcut) { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 793 | return shortcut.getIconResourceId(); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /** |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 797 | * @hide kept for testing. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 798 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 799 | @Deprecated |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 800 | public int getShortcutIconResId(@NonNull String packageName, @NonNull String shortcutId, |
| 801 | @NonNull UserHandle user) { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 802 | final ShortcutQuery q = new ShortcutQuery(); |
| 803 | q.setPackage(packageName); |
| 804 | q.setShortcutIds(Arrays.asList(shortcutId)); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 805 | q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 806 | final List<ShortcutInfo> shortcuts = getShortcuts(q, user); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 807 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 808 | return shortcuts.size() > 0 ? shortcuts.get(0).getIconResourceId() : 0; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 812 | * @hide internal/unit tests only |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 813 | */ |
| 814 | public ParcelFileDescriptor getShortcutIconFd( |
| 815 | @NonNull ShortcutInfo shortcut) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 816 | return getShortcutIconFd(shortcut.getPackage(), shortcut.getId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 817 | shortcut.getUserId()); |
| 818 | } |
| 819 | |
| 820 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 821 | * @hide internal/unit tests only |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 822 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 823 | public ParcelFileDescriptor getShortcutIconFd( |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 824 | @NonNull String packageName, @NonNull String shortcutId, @NonNull UserHandle user) { |
| 825 | return getShortcutIconFd(packageName, shortcutId, user.getIdentifier()); |
| 826 | } |
| 827 | |
| 828 | private ParcelFileDescriptor getShortcutIconFd( |
| 829 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 830 | try { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 831 | return mService.getShortcutIconFd(mContext.getPackageName(), |
| 832 | packageName, shortcutId, userId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 833 | } catch (RemoteException e) { |
| 834 | throw e.rethrowFromSystemServer(); |
| 835 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 836 | } |
| 837 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 838 | /** |
| 839 | * Returns the icon for this shortcut, without any badging for the profile. |
| 840 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 841 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 842 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 843 | * |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 844 | * @param density The preferred density of the icon, zero for default density. Use |
| 845 | * density DPI values from {@link DisplayMetrics}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 846 | * |
| 847 | * @return The drawable associated with the shortcut. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 848 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 849 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 850 | * |
| 851 | * @see ShortcutManager |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 852 | * @see #getShortcutBadgedIconDrawable(ShortcutInfo, int) |
| 853 | * @see DisplayMetrics |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 854 | */ |
| 855 | public Drawable getShortcutIconDrawable(@NonNull ShortcutInfo shortcut, int density) { |
| 856 | if (shortcut.hasIconFile()) { |
| 857 | final ParcelFileDescriptor pfd = getShortcutIconFd(shortcut); |
| 858 | if (pfd == null) { |
| 859 | return null; |
| 860 | } |
| 861 | try { |
| 862 | final Bitmap bmp = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor()); |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 863 | if (bmp != null) { |
| 864 | BitmapDrawable dr = new BitmapDrawable(mContext.getResources(), bmp); |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 865 | if (shortcut.hasAdaptiveBitmap()) { |
Hyunyoung Song | be8835e | 2017-02-17 11:25:08 -0800 | [diff] [blame] | 866 | return new AdaptiveIconDrawable(null, dr); |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 867 | } else { |
| 868 | return dr; |
| 869 | } |
| 870 | } |
| 871 | return null; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 872 | } finally { |
| 873 | try { |
| 874 | pfd.close(); |
| 875 | } catch (IOException ignore) { |
| 876 | } |
| 877 | } |
| 878 | } else if (shortcut.hasIconResource()) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 879 | return loadDrawableResourceFromPackage(shortcut.getPackage(), |
| 880 | shortcut.getIconResourceId(), shortcut.getUserHandle(), density); |
| 881 | } else if (shortcut.getIcon() != null) { |
| 882 | // This happens if a shortcut is pending-approval. |
| 883 | final Icon icon = shortcut.getIcon(); |
| 884 | switch (icon.getType()) { |
| 885 | case Icon.TYPE_RESOURCE: { |
| 886 | return loadDrawableResourceFromPackage(shortcut.getPackage(), |
| 887 | icon.getResId(), shortcut.getUserHandle(), density); |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 888 | } |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 889 | case Icon.TYPE_BITMAP: |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 890 | case Icon.TYPE_ADAPTIVE_BITMAP: { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 891 | return icon.loadDrawable(mContext); |
| 892 | } |
| 893 | default: |
| 894 | return null; // Shouldn't happen though. |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 895 | } |
| 896 | } else { |
| 897 | return null; // Has no icon. |
| 898 | } |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 901 | private Drawable loadDrawableResourceFromPackage(String packageName, int resId, |
| 902 | UserHandle user, int density) { |
| 903 | try { |
| 904 | if (resId == 0) { |
| 905 | return null; // Shouldn't happen but just in case. |
| 906 | } |
| 907 | final ApplicationInfo ai = getApplicationInfo(packageName, /* flags =*/ 0, user); |
| 908 | final Resources res = mContext.getPackageManager().getResourcesForApplication(ai); |
| 909 | return res.getDrawableForDensity(resId, density); |
| 910 | } catch (NameNotFoundException | Resources.NotFoundException e) { |
| 911 | return null; |
| 912 | } |
| 913 | } |
| 914 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 915 | /** |
| 916 | * Returns the shortcut icon with badging appropriate for the profile. |
| 917 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 918 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 919 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 920 | * |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 921 | * @param density Optional density for the icon, or 0 to use the default density. Use |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 922 | * @return A badged icon for the shortcut. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 923 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 924 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 925 | * |
| 926 | * @see ShortcutManager |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 927 | * @see #getShortcutIconDrawable(ShortcutInfo, int) |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 928 | * @see DisplayMetrics |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 929 | */ |
| 930 | public Drawable getShortcutBadgedIconDrawable(ShortcutInfo shortcut, int density) { |
| 931 | final Drawable originalIcon = getShortcutIconDrawable(shortcut, density); |
| 932 | |
| 933 | return (originalIcon == null) ? null : mContext.getPackageManager().getUserBadgedIcon( |
| 934 | originalIcon, shortcut.getUserHandle()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 937 | /** |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 938 | * Starts a shortcut. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 939 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 940 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 941 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 942 | * |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 943 | * @param packageName The target shortcut package name. |
| 944 | * @param shortcutId The target shortcut ID. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 945 | * @param sourceBounds The Rect containing the source bounds of the clicked icon. |
| 946 | * @param startActivityOptions Options to pass to startActivity. |
| 947 | * @param user The UserHandle of the profile. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 948 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 949 | * is locked or not running. |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 950 | * |
| 951 | * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g. |
| 952 | * the shortcut no longer exists, is disabled, the intent receiver activity doesn't exist, etc) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 953 | */ |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 954 | public void startShortcut(@NonNull String packageName, @NonNull String shortcutId, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 955 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions, |
| 956 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 957 | logErrorForInvalidProfileAccess(user); |
| 958 | |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 959 | startShortcut(packageName, shortcutId, sourceBounds, startActivityOptions, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 960 | user.getIdentifier()); |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Launches a shortcut. |
| 965 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 966 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 967 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 968 | * |
| 969 | * @param shortcut The target shortcut. |
| 970 | * @param sourceBounds The Rect containing the source bounds of the clicked icon. |
| 971 | * @param startActivityOptions Options to pass to startActivity. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 972 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 973 | * is locked or not running. |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 974 | * |
| 975 | * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g. |
| 976 | * the shortcut no longer exists, is disabled, the intent receiver activity doesn't exist, etc) |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 977 | */ |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 978 | public void startShortcut(@NonNull ShortcutInfo shortcut, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 979 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions) { |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 980 | startShortcut(shortcut.getPackage(), shortcut.getId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 981 | sourceBounds, startActivityOptions, |
| 982 | shortcut.getUserId()); |
| 983 | } |
| 984 | |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 985 | private void startShortcut(@NonNull String packageName, @NonNull String shortcutId, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 986 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions, |
| 987 | int userId) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 988 | try { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 989 | final boolean success = |
| 990 | mService.startShortcut(mContext.getPackageName(), packageName, shortcutId, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 991 | sourceBounds, startActivityOptions, userId); |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 992 | if (!success) { |
| 993 | throw new ActivityNotFoundException("Shortcut could not be started"); |
| 994 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 995 | } catch (RemoteException e) { |
| 996 | throw e.rethrowFromSystemServer(); |
| 997 | } |
| 998 | } |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 999 | |
| 1000 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1001 | * Registers a callback for changes to packages in current and managed profiles. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1002 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1003 | * @param callback The callback to register. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1004 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1005 | public void registerCallback(Callback callback) { |
| 1006 | registerCallback(callback, null); |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1010 | * Registers a callback for changes to packages in current and managed profiles. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1011 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1012 | * @param callback The callback to register. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1013 | * @param handler that should be used to post callbacks on, may be null. |
| 1014 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1015 | public void registerCallback(Callback callback, Handler handler) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1016 | synchronized (this) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1017 | if (callback != null && findCallbackLocked(callback) < 0) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1018 | boolean addedFirstCallback = mCallbacks.size() == 0; |
| 1019 | addCallbackLocked(callback, handler); |
| 1020 | if (addedFirstCallback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1021 | try { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1022 | mService.addOnAppsChangedListener(mContext.getPackageName(), |
| 1023 | mAppsChangedListener); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1024 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1025 | throw re.rethrowFromSystemServer(); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1033 | * Unregisters a callback that was previously registered. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1034 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1035 | * @param callback The callback to unregister. |
| 1036 | * @see #registerCallback(Callback) |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1037 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1038 | public void unregisterCallback(Callback callback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1039 | synchronized (this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1040 | removeCallbackLocked(callback); |
Kenny Guy | 44b6dee | 2014-07-10 18:10:14 +0100 | [diff] [blame] | 1041 | if (mCallbacks.size() == 0) { |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 1042 | try { |
| 1043 | mService.removeOnAppsChangedListener(mAppsChangedListener); |
| 1044 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1045 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 1046 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1051 | /** @return position in mCallbacks for callback or -1 if not present. */ |
| 1052 | private int findCallbackLocked(Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1053 | if (callback == null) { |
| 1054 | throw new IllegalArgumentException("Callback cannot be null"); |
| 1055 | } |
| 1056 | final int size = mCallbacks.size(); |
| 1057 | for (int i = 0; i < size; ++i) { |
| 1058 | if (mCallbacks.get(i).mCallback == callback) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1059 | return i; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1060 | } |
| 1061 | } |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1062 | return -1; |
| 1063 | } |
| 1064 | |
| 1065 | private void removeCallbackLocked(Callback callback) { |
| 1066 | int pos = findCallbackLocked(callback); |
| 1067 | if (pos >= 0) { |
| 1068 | mCallbacks.remove(pos); |
| 1069 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1072 | private void addCallbackLocked(Callback callback, Handler handler) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1073 | // Remove if already present. |
| 1074 | removeCallbackLocked(callback); |
| 1075 | if (handler == null) { |
| 1076 | handler = new Handler(); |
| 1077 | } |
| 1078 | CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback); |
| 1079 | mCallbacks.add(toAdd); |
| 1080 | } |
| 1081 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1082 | private IOnAppsChangedListener.Stub mAppsChangedListener = new IOnAppsChangedListener.Stub() { |
| 1083 | |
| 1084 | @Override |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1085 | public void onPackageRemoved(UserHandle user, String packageName) |
| 1086 | throws RemoteException { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1087 | if (DEBUG) { |
| 1088 | Log.d(TAG, "onPackageRemoved " + user.getIdentifier() + "," + packageName); |
| 1089 | } |
| 1090 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1091 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1092 | callback.postOnPackageRemoved(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1093 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | @Override |
| 1098 | public void onPackageChanged(UserHandle user, String packageName) throws RemoteException { |
| 1099 | if (DEBUG) { |
| 1100 | Log.d(TAG, "onPackageChanged " + user.getIdentifier() + "," + packageName); |
| 1101 | } |
| 1102 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1103 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1104 | callback.postOnPackageChanged(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1105 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | @Override |
| 1110 | public void onPackageAdded(UserHandle user, String packageName) throws RemoteException { |
| 1111 | if (DEBUG) { |
| 1112 | Log.d(TAG, "onPackageAdded " + user.getIdentifier() + "," + packageName); |
| 1113 | } |
| 1114 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1115 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1116 | callback.postOnPackageAdded(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1117 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | @Override |
| 1122 | public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing) |
| 1123 | throws RemoteException { |
| 1124 | if (DEBUG) { |
| 1125 | Log.d(TAG, "onPackagesAvailable " + user.getIdentifier() + "," + packageNames); |
| 1126 | } |
| 1127 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1128 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1129 | callback.postOnPackagesAvailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1130 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | @Override |
| 1135 | public void onPackagesUnavailable(UserHandle user, String[] packageNames, boolean replacing) |
| 1136 | throws RemoteException { |
| 1137 | if (DEBUG) { |
| 1138 | Log.d(TAG, "onPackagesUnavailable " + user.getIdentifier() + "," + packageNames); |
| 1139 | } |
| 1140 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1141 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1142 | callback.postOnPackagesUnavailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1143 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | @Override |
| 1148 | public void onPackagesSuspended(UserHandle user, String[] packageNames) |
| 1149 | throws RemoteException { |
| 1150 | if (DEBUG) { |
| 1151 | Log.d(TAG, "onPackagesSuspended " + user.getIdentifier() + "," + packageNames); |
| 1152 | } |
| 1153 | synchronized (LauncherApps.this) { |
| 1154 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1155 | callback.postOnPackagesSuspended(packageNames, user); |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | @Override |
| 1161 | public void onPackagesUnsuspended(UserHandle user, String[] packageNames) |
| 1162 | throws RemoteException { |
| 1163 | if (DEBUG) { |
| 1164 | Log.d(TAG, "onPackagesUnsuspended " + user.getIdentifier() + "," + packageNames); |
| 1165 | } |
| 1166 | synchronized (LauncherApps.this) { |
| 1167 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1168 | callback.postOnPackagesUnsuspended(packageNames, user); |
| 1169 | } |
| 1170 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1171 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1172 | |
| 1173 | @Override |
| 1174 | public void onShortcutChanged(UserHandle user, String packageName, |
| 1175 | ParceledListSlice shortcuts) { |
| 1176 | if (DEBUG) { |
| 1177 | Log.d(TAG, "onShortcutChanged " + user.getIdentifier() + "," + packageName); |
| 1178 | } |
| 1179 | final List<ShortcutInfo> list = shortcuts.getList(); |
| 1180 | synchronized (LauncherApps.this) { |
| 1181 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1182 | callback.postOnShortcutChanged(packageName, user, list); |
| 1183 | } |
| 1184 | } |
| 1185 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1186 | }; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1187 | |
| 1188 | private static class CallbackMessageHandler extends Handler { |
| 1189 | private static final int MSG_ADDED = 1; |
| 1190 | private static final int MSG_REMOVED = 2; |
| 1191 | private static final int MSG_CHANGED = 3; |
| 1192 | private static final int MSG_AVAILABLE = 4; |
| 1193 | private static final int MSG_UNAVAILABLE = 5; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1194 | private static final int MSG_SUSPENDED = 6; |
| 1195 | private static final int MSG_UNSUSPENDED = 7; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1196 | private static final int MSG_SHORTCUT_CHANGED = 8; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1197 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1198 | private LauncherApps.Callback mCallback; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1199 | |
| 1200 | private static class CallbackInfo { |
| 1201 | String[] packageNames; |
| 1202 | String packageName; |
| 1203 | boolean replacing; |
| 1204 | UserHandle user; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1205 | List<ShortcutInfo> shortcuts; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1206 | } |
| 1207 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1208 | public CallbackMessageHandler(Looper looper, LauncherApps.Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1209 | super(looper, null, true); |
| 1210 | mCallback = callback; |
| 1211 | } |
| 1212 | |
| 1213 | @Override |
| 1214 | public void handleMessage(Message msg) { |
| 1215 | if (mCallback == null || !(msg.obj instanceof CallbackInfo)) { |
| 1216 | return; |
| 1217 | } |
| 1218 | CallbackInfo info = (CallbackInfo) msg.obj; |
| 1219 | switch (msg.what) { |
| 1220 | case MSG_ADDED: |
| 1221 | mCallback.onPackageAdded(info.packageName, info.user); |
| 1222 | break; |
| 1223 | case MSG_REMOVED: |
| 1224 | mCallback.onPackageRemoved(info.packageName, info.user); |
| 1225 | break; |
| 1226 | case MSG_CHANGED: |
| 1227 | mCallback.onPackageChanged(info.packageName, info.user); |
| 1228 | break; |
| 1229 | case MSG_AVAILABLE: |
| 1230 | mCallback.onPackagesAvailable(info.packageNames, info.user, info.replacing); |
| 1231 | break; |
| 1232 | case MSG_UNAVAILABLE: |
| 1233 | mCallback.onPackagesUnavailable(info.packageNames, info.user, info.replacing); |
| 1234 | break; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1235 | case MSG_SUSPENDED: |
| 1236 | mCallback.onPackagesSuspended(info.packageNames, info.user); |
| 1237 | break; |
| 1238 | case MSG_UNSUSPENDED: |
| 1239 | mCallback.onPackagesUnsuspended(info.packageNames, info.user); |
| 1240 | break; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1241 | case MSG_SHORTCUT_CHANGED: |
| 1242 | mCallback.onShortcutsChanged(info.packageName, info.shortcuts, info.user); |
| 1243 | break; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | public void postOnPackageAdded(String packageName, UserHandle user) { |
| 1248 | CallbackInfo info = new CallbackInfo(); |
| 1249 | info.packageName = packageName; |
| 1250 | info.user = user; |
| 1251 | obtainMessage(MSG_ADDED, info).sendToTarget(); |
| 1252 | } |
| 1253 | |
| 1254 | public void postOnPackageRemoved(String packageName, UserHandle user) { |
| 1255 | CallbackInfo info = new CallbackInfo(); |
| 1256 | info.packageName = packageName; |
| 1257 | info.user = user; |
| 1258 | obtainMessage(MSG_REMOVED, info).sendToTarget(); |
| 1259 | } |
| 1260 | |
| 1261 | public void postOnPackageChanged(String packageName, UserHandle user) { |
| 1262 | CallbackInfo info = new CallbackInfo(); |
| 1263 | info.packageName = packageName; |
| 1264 | info.user = user; |
| 1265 | obtainMessage(MSG_CHANGED, info).sendToTarget(); |
| 1266 | } |
| 1267 | |
| 1268 | public void postOnPackagesAvailable(String[] packageNames, UserHandle user, |
| 1269 | boolean replacing) { |
| 1270 | CallbackInfo info = new CallbackInfo(); |
| 1271 | info.packageNames = packageNames; |
| 1272 | info.replacing = replacing; |
| 1273 | info.user = user; |
| 1274 | obtainMessage(MSG_AVAILABLE, info).sendToTarget(); |
| 1275 | } |
| 1276 | |
| 1277 | public void postOnPackagesUnavailable(String[] packageNames, UserHandle user, |
| 1278 | boolean replacing) { |
| 1279 | CallbackInfo info = new CallbackInfo(); |
| 1280 | info.packageNames = packageNames; |
| 1281 | info.replacing = replacing; |
| 1282 | info.user = user; |
| 1283 | obtainMessage(MSG_UNAVAILABLE, info).sendToTarget(); |
| 1284 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1285 | |
| 1286 | public void postOnPackagesSuspended(String[] packageNames, UserHandle user) { |
| 1287 | CallbackInfo info = new CallbackInfo(); |
| 1288 | info.packageNames = packageNames; |
| 1289 | info.user = user; |
| 1290 | obtainMessage(MSG_SUSPENDED, info).sendToTarget(); |
| 1291 | } |
| 1292 | |
| 1293 | public void postOnPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 1294 | CallbackInfo info = new CallbackInfo(); |
| 1295 | info.packageNames = packageNames; |
| 1296 | info.user = user; |
| 1297 | obtainMessage(MSG_UNSUSPENDED, info).sendToTarget(); |
| 1298 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1299 | |
| 1300 | public void postOnShortcutChanged(String packageName, UserHandle user, |
| 1301 | List<ShortcutInfo> shortcuts) { |
| 1302 | CallbackInfo info = new CallbackInfo(); |
| 1303 | info.packageName = packageName; |
| 1304 | info.user = user; |
| 1305 | info.shortcuts = shortcuts; |
| 1306 | obtainMessage(MSG_SHORTCUT_CHANGED, info).sendToTarget(); |
| 1307 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1308 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1309 | |
| 1310 | /** |
| 1311 | * A helper method to extract a {@link PinItemRequest} set to |
| 1312 | * the {@link #EXTRA_PIN_ITEM_REQUEST} extra. |
| 1313 | */ |
| 1314 | public PinItemRequest getPinItemRequest(Intent intent) { |
| 1315 | return intent.getParcelableExtra(EXTRA_PIN_ITEM_REQUEST); |
| 1316 | } |
| 1317 | |
| 1318 | /** |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 1319 | * Represents a "pin shortcut" or a "pin appwidget" request made by an app, which is sent with |
| 1320 | * an {@link #ACTION_CONFIRM_PIN_SHORTCUT} or {@link #ACTION_CONFIRM_PIN_APPWIDGET} intent |
| 1321 | * respectively to the default launcher app. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1322 | * |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1323 | * <h3>Request of the {@link #REQUEST_TYPE_SHORTCUT} type. |
Makoto Onuki | 0c28071 | 2017-01-19 15:14:27 -0800 | [diff] [blame] | 1324 | * |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1325 | * <p>A {@link #REQUEST_TYPE_SHORTCUT} request represents a request to pin a |
| 1326 | * {@link ShortcutInfo}. If the launcher accepts a request, call {@link #accept()}, |
| 1327 | * or {@link #accept(Bundle)} with a null or empty Bundle. No options are defined for |
| 1328 | * pin-shortcuts requests. |
| 1329 | * |
| 1330 | * <p>{@link #getShortcutInfo()} always returns a non-null {@link ShortcutInfo} for this type. |
| 1331 | * |
| 1332 | * <p>The launcher may receive a request with a {@link ShortcutInfo} that is already pinned, in |
| 1333 | * which case {@link ShortcutInfo#isPinned()} returns true. This means the user wants to create |
| 1334 | * another pinned shortcut for a shortcut that's already pinned. If the launcher accepts it, |
| 1335 | * {@link #accept()} must still be called even though the shortcut is already pinned, and |
| 1336 | * create a new pinned shortcut icon for it. |
| 1337 | * |
| 1338 | * <p>See also {@link ShortcutManager} for more details. |
| 1339 | * |
| 1340 | * <h3>Request of the {@link #REQUEST_TYPE_APPWIDGET} type. |
| 1341 | * |
| 1342 | * <p>A {@link #REQUEST_TYPE_SHORTCUT} request represents a request to pin a |
| 1343 | * an AppWidget. If the launcher accepts a request, call {@link #accept(Bundle)} with |
| 1344 | * the appwidget integer ID set to the |
| 1345 | * {@link android.appwidget.AppWidgetManager#EXTRA_APPWIDGET_ID} extra. |
| 1346 | * |
| 1347 | * <p>{@link #getAppWidgetProviderInfo(Context)} always returns a non-null |
| 1348 | * {@link AppWidgetProviderInfo} for this type. |
| 1349 | * |
| 1350 | * <p>See also {@link AppWidgetManager} for more details. |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 1351 | * |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1352 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 1353 | * @see #getPinItemRequest(Intent) |
| 1354 | */ |
| 1355 | public static final class PinItemRequest implements Parcelable { |
| 1356 | |
| 1357 | /** This is a request to pin shortcut. */ |
| 1358 | public static final int REQUEST_TYPE_SHORTCUT = 1; |
| 1359 | |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1360 | /** This is a request to pin app widget. */ |
| 1361 | public static final int REQUEST_TYPE_APPWIDGET = 2; |
| 1362 | |
Makoto Onuki | f5663b1 | 2017-01-09 14:09:56 -0800 | [diff] [blame] | 1363 | /** @hide */ |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1364 | @IntDef(value = {REQUEST_TYPE_SHORTCUT}) |
| 1365 | @Retention(RetentionPolicy.SOURCE) |
| 1366 | public @interface RequestType {} |
| 1367 | |
| 1368 | private final int mRequestType; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1369 | private final IPinItemRequest mInner; |
| 1370 | |
| 1371 | /** |
| 1372 | * @hide |
| 1373 | */ |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1374 | public PinItemRequest(IPinItemRequest inner, int type) { |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1375 | mInner = inner; |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1376 | mRequestType = type; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1380 | * Represents the type of a request, which is one of the {@code REQUEST_TYPE_} constants. |
| 1381 | * |
| 1382 | * @return one of the {@code REQUEST_TYPE_} constants. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1383 | */ |
| 1384 | @RequestType |
| 1385 | public int getRequestType() { |
| 1386 | return mRequestType; |
| 1387 | } |
| 1388 | |
| 1389 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1390 | * {@link ShortcutInfo} sent by the requesting app. |
| 1391 | * Always non-null for a {@link #REQUEST_TYPE_SHORTCUT} request, and always null for a |
| 1392 | * different request type. |
| 1393 | * |
| 1394 | * @return requested {@link ShortcutInfo} when a request is of the |
| 1395 | * {@link #REQUEST_TYPE_SHORTCUT} type. Null otherwise. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1396 | */ |
| 1397 | @Nullable |
| 1398 | public ShortcutInfo getShortcutInfo() { |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1399 | try { |
| 1400 | return mInner.getShortcutInfo(); |
| 1401 | } catch (RemoteException e) { |
| 1402 | throw e.rethrowAsRuntimeException(); |
| 1403 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1407 | * {@link AppWidgetProviderInfo} sent by the requesting app. |
| 1408 | * Always non-null for a {@link #REQUEST_TYPE_APPWIDGET} request, and always null for a |
| 1409 | * different request type. |
| 1410 | * |
| 1411 | * @return requested {@link AppWidgetProviderInfo} when a request is of the |
| 1412 | * {@link #REQUEST_TYPE_APPWIDGET} type. Null otherwise. |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1413 | */ |
| 1414 | @Nullable |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1415 | public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) { |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1416 | try { |
| 1417 | final AppWidgetProviderInfo info = mInner.getAppWidgetProviderInfo(); |
| 1418 | if (info == null) { |
| 1419 | return null; |
| 1420 | } |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1421 | info.updateDimensions(context.getResources().getDisplayMetrics()); |
| 1422 | return info; |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1423 | } catch (RemoteException e) { |
| 1424 | throw e.rethrowAsRuntimeException(); |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1425 | } |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /** |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1429 | * Any extras sent by the requesting app. |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1430 | * |
| 1431 | * @return For a shortcut request, this method always return null. For an AppWidget |
| 1432 | * request, this method returns the extras passed to the |
| 1433 | * {@link android.appwidget.AppWidgetManager#requestPinAppWidget( |
| 1434 | * ComponentName, Bundle, PendingIntent)} API. See {@link AppWidgetManager} for details. |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1435 | */ |
| 1436 | @Nullable |
| 1437 | public Bundle getExtras() { |
| 1438 | try { |
| 1439 | return mInner.getExtras(); |
| 1440 | } catch (RemoteException e) { |
| 1441 | throw e.rethrowAsRuntimeException(); |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1446 | * Return whether a request is still valid. |
| 1447 | * |
| 1448 | * @return {@code TRUE} if a request is valid and {@link #accept(Bundle)} may be called. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1449 | */ |
| 1450 | public boolean isValid() { |
| 1451 | try { |
| 1452 | return mInner.isValid(); |
| 1453 | } catch (RemoteException e) { |
| 1454 | return false; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * Called by the receiving launcher app when the user accepts the request. |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1460 | * |
| 1461 | * @param options must be set for a {@link #REQUEST_TYPE_APPWIDGET} request. |
| 1462 | * |
| 1463 | * @return {@code TRUE} if the shortcut or the AppWidget has actually been pinned. |
| 1464 | * {@code FALSE} if the item hasn't been pinned, for example, because the request had |
| 1465 | * already been canceled, in which case the launcher must not pin the requested item. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1466 | */ |
| 1467 | public boolean accept(@Nullable Bundle options) { |
| 1468 | try { |
| 1469 | return mInner.accept(options); |
| 1470 | } catch (RemoteException e) { |
| 1471 | throw e.rethrowFromSystemServer(); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1476 | * Called by the receiving launcher app when the user accepts the request, with no options. |
| 1477 | * |
| 1478 | * @return {@code TRUE} if the shortcut or the AppWidget has actually been pinned. |
| 1479 | * {@code FALSE} if the item hasn't been pinned, for example, because the request had |
| 1480 | * already been canceled, in which case the launcher must not pin the requested item. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1481 | */ |
| 1482 | public boolean accept() { |
| 1483 | return accept(/* options= */ null); |
| 1484 | } |
| 1485 | |
| 1486 | private PinItemRequest(Parcel source) { |
| 1487 | final ClassLoader cl = getClass().getClassLoader(); |
| 1488 | |
| 1489 | mRequestType = source.readInt(); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1490 | mInner = IPinItemRequest.Stub.asInterface(source.readStrongBinder()); |
| 1491 | } |
| 1492 | |
| 1493 | @Override |
| 1494 | public void writeToParcel(Parcel dest, int flags) { |
| 1495 | dest.writeInt(mRequestType); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1496 | dest.writeStrongBinder(mInner.asBinder()); |
| 1497 | } |
| 1498 | |
| 1499 | public static final Creator<PinItemRequest> CREATOR = |
| 1500 | new Creator<PinItemRequest>() { |
| 1501 | public PinItemRequest createFromParcel(Parcel source) { |
| 1502 | return new PinItemRequest(source); |
| 1503 | } |
| 1504 | public PinItemRequest[] newArray(int size) { |
| 1505 | return new PinItemRequest[size]; |
| 1506 | } |
| 1507 | }; |
| 1508 | |
| 1509 | @Override |
| 1510 | public int describeContents() { |
| 1511 | return 0; |
| 1512 | } |
| 1513 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1514 | } |