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 | |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SdkConstant.SdkConstantType; |
Sunny Goyal | beee197 | 2019-03-29 11:38:16 -0700 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 27 | import android.annotation.TestApi; |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 28 | import android.app.PendingIntent; |
| 29 | import android.appwidget.AppWidgetManager; |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 30 | import android.appwidget.AppWidgetProviderInfo; |
Artur Satayev | e23a0eb | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 31 | import android.compat.annotation.UnsupportedAppUsage; |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 32 | import android.content.ActivityNotFoundException; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 33 | import android.content.ComponentName; |
| 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 36 | import android.content.IntentSender; |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 37 | import android.content.LocusId; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageInstaller.SessionCallback; |
| 39 | import android.content.pm.PackageInstaller.SessionCallbackDelegate; |
| 40 | import android.content.pm.PackageInstaller.SessionInfo; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 41 | import android.content.pm.PackageManager.ApplicationInfoFlags; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageManager.NameNotFoundException; |
| 43 | import android.content.res.Resources; |
| 44 | import android.graphics.Bitmap; |
| 45 | import android.graphics.BitmapFactory; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 46 | import android.graphics.Rect; |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 47 | import android.graphics.drawable.AdaptiveIconDrawable; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 48 | import android.graphics.drawable.BitmapDrawable; |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 49 | import android.graphics.drawable.Drawable; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 50 | import android.graphics.drawable.Icon; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 51 | import android.os.Build; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 52 | import android.os.Bundle; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 53 | import android.os.Handler; |
| 54 | import android.os.Looper; |
| 55 | import android.os.Message; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 56 | import android.os.Parcel; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 57 | import android.os.ParcelFileDescriptor; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 58 | import android.os.Parcelable; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 59 | import android.os.RemoteException; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 60 | import android.os.ServiceManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 61 | import android.os.UserHandle; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 62 | import android.os.UserManager; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 63 | import android.util.DisplayMetrics; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 64 | import android.util.Log; |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 65 | import android.util.Pair; |
| 66 | |
| 67 | import com.android.internal.util.function.pooled.PooledLambda; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 68 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 69 | import java.io.IOException; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 70 | import java.lang.annotation.Retention; |
| 71 | import java.lang.annotation.RetentionPolicy; |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 72 | import java.lang.ref.WeakReference; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 73 | import java.util.ArrayList; |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 74 | import java.util.Arrays; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 75 | import java.util.Collections; |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 76 | import java.util.HashMap; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 77 | import java.util.Iterator; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 78 | import java.util.List; |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 79 | import java.util.Map; |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 80 | import java.util.Objects; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 81 | import java.util.concurrent.Executor; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * 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] | 85 | * managed profiles that are visible to the current user, which can be retrieved with |
| 86 | * {@link #getProfiles}. This is mainly for use by launchers. |
| 87 | * |
| 88 | * Apps can be queried for each user profile. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 89 | * Since the PackageManager will not deliver package broadcasts for other profiles, you can register |
| 90 | * for package changes here. |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 91 | * <p> |
| 92 | * To watch for managed profiles being added or removed, register for the following broadcasts: |
| 93 | * {@link Intent#ACTION_MANAGED_PROFILE_ADDED} and {@link Intent#ACTION_MANAGED_PROFILE_REMOVED}. |
| 94 | * <p> |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 95 | * Note as of Android O, apps on a managed profile are no longer allowed to access apps on the |
| 96 | * main profile. Apps can only access profiles returned by {@link #getProfiles()}. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 97 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 98 | @SystemService(Context.LAUNCHER_APPS_SERVICE) |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 99 | public class LauncherApps { |
| 100 | |
| 101 | static final String TAG = "LauncherApps"; |
| 102 | static final boolean DEBUG = false; |
| 103 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Activity Action: For the default launcher to show the confirmation dialog to create |
| 106 | * a pinned shortcut. |
| 107 | * |
| 108 | * <p>See the {@link ShortcutManager} javadoc for details. |
| 109 | * |
| 110 | * <p> |
| 111 | * Use {@link #getPinItemRequest(Intent)} to get a {@link PinItemRequest} object, |
| 112 | * and call {@link PinItemRequest#accept(Bundle)} |
| 113 | * if the user accepts. If the user doesn't accept, no further action is required. |
| 114 | * |
| 115 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 116 | */ |
| 117 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 118 | public static final String ACTION_CONFIRM_PIN_SHORTCUT = |
| 119 | "android.content.pm.action.CONFIRM_PIN_SHORTCUT"; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 120 | |
| 121 | /** |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 122 | * Activity Action: For the default launcher to show the confirmation dialog to create |
| 123 | * a pinned app widget. |
| 124 | * |
| 125 | * <p>See the {@link android.appwidget.AppWidgetManager#requestPinAppWidget} javadoc for |
| 126 | * details. |
| 127 | * |
| 128 | * <p> |
| 129 | * Use {@link #getPinItemRequest(Intent)} to get a {@link PinItemRequest} object, |
| 130 | * and call {@link PinItemRequest#accept(Bundle)} |
| 131 | * if the user accepts. If the user doesn't accept, no further action is required. |
| 132 | * |
| 133 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 134 | */ |
| 135 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 136 | public static final String ACTION_CONFIRM_PIN_APPWIDGET = |
| 137 | "android.content.pm.action.CONFIRM_PIN_APPWIDGET"; |
| 138 | |
| 139 | /** |
| 140 | * An extra for {@link #ACTION_CONFIRM_PIN_SHORTCUT} & {@link #ACTION_CONFIRM_PIN_APPWIDGET} |
| 141 | * containing a {@link PinItemRequest} of appropriate type asked to pin. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 142 | * |
| 143 | * <p>A helper function {@link #getPinItemRequest(Intent)} can be used |
| 144 | * instead of using this constant directly. |
| 145 | * |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 146 | * @see #ACTION_CONFIRM_PIN_SHORTCUT |
| 147 | * @see #ACTION_CONFIRM_PIN_APPWIDGET |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 148 | */ |
| 149 | public static final String EXTRA_PIN_ITEM_REQUEST = |
| 150 | "android.content.pm.extra.PIN_ITEM_REQUEST"; |
| 151 | |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 152 | private final Context mContext; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 153 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 154 | private final ILauncherApps mService; |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 155 | @UnsupportedAppUsage |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 156 | private final PackageManager mPm; |
| 157 | private final UserManager mUserManager; |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 158 | |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 159 | private final List<CallbackMessageHandler> mCallbacks = new ArrayList<>(); |
| 160 | private final List<SessionCallbackDelegate> mDelegates = new ArrayList<>(); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 161 | |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 162 | private final Map<Integer, Pair<Executor, ShortcutChangeCallback>> |
| 163 | mShortcutChangeCallbacks = new HashMap<>(); |
| 164 | |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 165 | /** |
| 166 | * Callbacks for package changes to this and related managed profiles. |
| 167 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 168 | public static abstract class Callback { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 169 | /** |
| 170 | * Indicates that a package was removed from the specified profile. |
| 171 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 172 | * If a package is removed while being updated onPackageChanged will be |
| 173 | * called instead. |
| 174 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 175 | * @param packageName The name of the package that was removed. |
| 176 | * @param user The UserHandle of the profile that generated the change. |
| 177 | */ |
| 178 | abstract public void onPackageRemoved(String packageName, UserHandle user); |
| 179 | |
| 180 | /** |
| 181 | * Indicates that a package was added to the specified profile. |
| 182 | * |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 183 | * If a package is added while being updated then onPackageChanged will be |
| 184 | * called instead. |
| 185 | * |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 186 | * @param packageName The name of the package that was added. |
| 187 | * @param user The UserHandle of the profile that generated the change. |
| 188 | */ |
| 189 | abstract public void onPackageAdded(String packageName, UserHandle user); |
| 190 | |
| 191 | /** |
| 192 | * Indicates that a package was modified in the specified profile. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 193 | * This can happen, for example, when the package is updated or when |
| 194 | * one or more components are enabled or disabled. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 195 | * |
| 196 | * @param packageName The name of the package that has changed. |
| 197 | * @param user The UserHandle of the profile that generated the change. |
| 198 | */ |
| 199 | abstract public void onPackageChanged(String packageName, UserHandle user); |
| 200 | |
| 201 | /** |
| 202 | * Indicates that one or more packages have become available. For |
| 203 | * example, this can happen when a removable storage card has |
| 204 | * reappeared. |
| 205 | * |
| 206 | * @param packageNames The names of the packages that have become |
| 207 | * available. |
| 208 | * @param user The UserHandle of the profile that generated the change. |
| 209 | * @param replacing Indicates whether these packages are replacing |
| 210 | * existing ones. |
| 211 | */ |
| 212 | abstract public void onPackagesAvailable(String[] packageNames, UserHandle user, |
| 213 | boolean replacing); |
| 214 | |
| 215 | /** |
| 216 | * Indicates that one or more packages have become unavailable. For |
| 217 | * example, this can happen when a removable storage card has been |
| 218 | * removed. |
| 219 | * |
| 220 | * @param packageNames The names of the packages that have become |
| 221 | * unavailable. |
| 222 | * @param user The UserHandle of the profile that generated the change. |
| 223 | * @param replacing Indicates whether the packages are about to be |
| 224 | * replaced with new versions. |
| 225 | */ |
| 226 | abstract public void onPackagesUnavailable(String[] packageNames, UserHandle user, |
| 227 | boolean replacing); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Indicates that one or more packages have been suspended. For |
| 231 | * example, this can happen when a Device Administrator suspends |
| 232 | * an applicaton. |
| 233 | * |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 234 | * <p>Note: On devices running {@link android.os.Build.VERSION_CODES#P Android P} or higher, |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 235 | * any apps that override {@link #onPackagesSuspended(String[], UserHandle, Bundle)} will |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 236 | * not receive this callback. |
| 237 | * |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 238 | * @param packageNames The names of the packages that have just been |
| 239 | * suspended. |
| 240 | * @param user The UserHandle of the profile that generated the change. |
| 241 | */ |
| 242 | public void onPackagesSuspended(String[] packageNames, UserHandle user) { |
| 243 | } |
| 244 | |
| 245 | /** |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 246 | * Indicates that one or more packages have been suspended. A device administrator or an app |
| 247 | * with {@code android.permission.SUSPEND_APPS} can do this. |
| 248 | * |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 249 | * <p>A suspending app with the permission {@code android.permission.SUSPEND_APPS} can |
| 250 | * optionally provide a {@link Bundle} of extra information that it deems helpful for the |
| 251 | * launcher to handle the suspended state of these packages. The contents of this |
Suprabh Shukla | 3e03ab9 | 2018-04-11 16:03:49 -0700 | [diff] [blame] | 252 | * {@link Bundle} are supposed to be a contract between the suspending app and the launcher. |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 253 | * |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 254 | * @param packageNames The names of the packages that have just been suspended. |
| 255 | * @param user the user for which the given packages were suspended. |
| 256 | * @param launcherExtras A {@link Bundle} of extras for the launcher, if provided to the |
| 257 | * system, {@code null} otherwise. |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 258 | * @see PackageManager#isPackageSuspended() |
| 259 | * @see #getSuspendedPackageLauncherExtras(String, UserHandle) |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 260 | * @deprecated {@code launcherExtras} should be obtained by using |
| 261 | * {@link #getSuspendedPackageLauncherExtras(String, UserHandle)}. For all other cases, |
| 262 | * {@link #onPackagesSuspended(String[], UserHandle)} should be used. |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 263 | */ |
Suprabh Shukla | d327844 | 2019-08-27 15:58:03 -0700 | [diff] [blame] | 264 | @Deprecated |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 265 | public void onPackagesSuspended(String[] packageNames, UserHandle user, |
| 266 | @Nullable Bundle launcherExtras) { |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 267 | onPackagesSuspended(packageNames, user); |
| 268 | } |
| 269 | |
| 270 | /** |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 271 | * Indicates that one or more packages have been unsuspended. For |
| 272 | * example, this can happen when a Device Administrator unsuspends |
| 273 | * an applicaton. |
| 274 | * |
| 275 | * @param packageNames The names of the packages that have just been |
| 276 | * unsuspended. |
| 277 | * @param user The UserHandle of the profile that generated the change. |
| 278 | */ |
| 279 | public void onPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 280 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 281 | |
| 282 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 283 | * 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] | 284 | * have been added, updated or removed. |
| 285 | * |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 286 | * <p>Only the applications that are allowed to access the shortcut information, |
| 287 | * as defined in {@link #hasShortcutHostPermission()}, will receive it. |
| 288 | * |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 289 | * @param packageName The name of the package that has the shortcuts. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 290 | * @param shortcuts All shortcuts from the package (dynamic, manifest and/or pinned). |
| 291 | * Only "key" information will be provided, as defined in |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 292 | * {@link ShortcutInfo#hasKeyFieldsOnly()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 293 | * @param user The UserHandle of the profile that generated the change. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 294 | * |
| 295 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 296 | */ |
| 297 | public void onShortcutsChanged(@NonNull String packageName, |
| 298 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) { |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Represents a query passed to {@link #getShortcuts(ShortcutQuery, UserHandle)}. |
| 304 | */ |
| 305 | public static class ShortcutQuery { |
| 306 | /** |
| 307 | * Include dynamic shortcuts in the result. |
| 308 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 309 | public static final int FLAG_MATCH_DYNAMIC = 1 << 0; |
| 310 | |
| 311 | /** @hide kept for unit tests */ |
| 312 | @Deprecated |
| 313 | public static final int FLAG_GET_DYNAMIC = FLAG_MATCH_DYNAMIC; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * Include pinned shortcuts in the result. |
Makoto Onuki | 88b4dcc | 2017-12-07 15:53:56 -0800 | [diff] [blame] | 317 | * |
| 318 | * <p>If you are the selected assistant app, and wishes to fetch all shortcuts that the |
| 319 | * user owns on the launcher (or by other launchers, in case the user has multiple), use |
| 320 | * {@link #FLAG_MATCH_PINNED_BY_ANY_LAUNCHER} instead. |
| 321 | * |
| 322 | * <p>If you're a regular launcher app, there's no way to get shortcuts pinned by other |
| 323 | * launchers, and {@link #FLAG_MATCH_PINNED_BY_ANY_LAUNCHER} will be ignored. So use this |
| 324 | * flag to get own pinned shortcuts. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 325 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 326 | public static final int FLAG_MATCH_PINNED = 1 << 1; |
| 327 | |
| 328 | /** @hide kept for unit tests */ |
| 329 | @Deprecated |
| 330 | public static final int FLAG_GET_PINNED = FLAG_MATCH_PINNED; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 331 | |
| 332 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 333 | * Include manifest shortcuts in the result. |
| 334 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 335 | public static final int FLAG_MATCH_MANIFEST = 1 << 3; |
| 336 | |
Mehdi Alizadeh | 505fb76 | 2020-01-21 17:26:24 -0800 | [diff] [blame] | 337 | /** |
| 338 | * Include cached shortcuts in the result. |
| 339 | */ |
| 340 | public static final int FLAG_MATCH_CACHED = 1 << 4; |
| 341 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 342 | /** @hide kept for unit tests */ |
| 343 | @Deprecated |
| 344 | public static final int FLAG_GET_MANIFEST = FLAG_MATCH_MANIFEST; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 345 | |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 346 | /** |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 347 | * Include all pinned shortcuts by any launchers, not just by the caller, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 348 | * in the result. |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 349 | * |
Makoto Onuki | 88b4dcc | 2017-12-07 15:53:56 -0800 | [diff] [blame] | 350 | * <p>The caller must be the selected assistant app to use this flag, or have the system |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 351 | * {@code ACCESS_SHORTCUTS} permission. |
Makoto Onuki | 88b4dcc | 2017-12-07 15:53:56 -0800 | [diff] [blame] | 352 | * |
| 353 | * <p>If you are the selected assistant app, and wishes to fetch all shortcuts that the |
| 354 | * user owns on the launcher (or by other launchers, in case the user has multiple), use |
| 355 | * {@link #FLAG_MATCH_PINNED_BY_ANY_LAUNCHER} instead. |
| 356 | * |
| 357 | * <p>If you're a regular launcher app (or any app that's not the selected assistant app) |
| 358 | * then this flag will be ignored. |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 359 | */ |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 360 | public static final int FLAG_MATCH_PINNED_BY_ANY_LAUNCHER = 1 << 10; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 361 | |
| 362 | /** |
Mehdi Alizadeh | 505fb76 | 2020-01-21 17:26:24 -0800 | [diff] [blame] | 363 | * FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST | FLAG_MATCH_CACHED |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 364 | * @hide |
| 365 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 366 | public static final int FLAG_MATCH_ALL_KINDS = |
Mehdi Alizadeh | 505fb76 | 2020-01-21 17:26:24 -0800 | [diff] [blame] | 367 | FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST | FLAG_MATCH_CACHED; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 368 | |
| 369 | /** |
| 370 | * FLAG_MATCH_DYNAMIC | FLAG_MATCH_PINNED | FLAG_MATCH_MANIFEST | FLAG_MATCH_ALL_PINNED |
| 371 | * @hide |
| 372 | */ |
| 373 | public static final int FLAG_MATCH_ALL_KINDS_WITH_ALL_PINNED = |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 374 | FLAG_MATCH_ALL_KINDS | FLAG_MATCH_PINNED_BY_ANY_LAUNCHER; |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 375 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 376 | /** @hide kept for unit tests */ |
| 377 | @Deprecated |
| 378 | public static final int FLAG_GET_ALL_KINDS = FLAG_MATCH_ALL_KINDS; |
| 379 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 380 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 381 | * Requests "key" fields only. See {@link ShortcutInfo#hasKeyFieldsOnly()}'s javadoc to |
| 382 | * see which fields fields "key". |
| 383 | * This allows quicker access to shortcut information in order to |
| 384 | * determine whether the caller's in-memory cache needs to be updated. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 385 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 386 | * <p>Typically, launcher applications cache all or most shortcut information |
| 387 | * in memory in order to show shortcuts without a delay. |
| 388 | * |
| 389 | * When a given launcher application wants to update its cache, such as when its process |
| 390 | * restarts, it can fetch shortcut information with this flag. |
| 391 | * The application can then check {@link ShortcutInfo#getLastChangedTimestamp()} for each |
| 392 | * shortcut, fetching a shortcut's non-key information only if that shortcut has been |
| 393 | * updated. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 394 | * |
| 395 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 396 | */ |
| 397 | public static final int FLAG_GET_KEY_FIELDS_ONLY = 1 << 2; |
| 398 | |
| 399 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 400 | @IntDef(flag = true, prefix = { "FLAG_" }, value = { |
| 401 | FLAG_MATCH_DYNAMIC, |
| 402 | FLAG_MATCH_PINNED, |
| 403 | FLAG_MATCH_MANIFEST, |
Mehdi Alizadeh | 505fb76 | 2020-01-21 17:26:24 -0800 | [diff] [blame] | 404 | FLAG_MATCH_CACHED, |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 405 | FLAG_GET_KEY_FIELDS_ONLY, |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 406 | }) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 407 | @Retention(RetentionPolicy.SOURCE) |
| 408 | public @interface QueryFlags {} |
| 409 | |
| 410 | long mChangedSince; |
| 411 | |
| 412 | @Nullable |
| 413 | String mPackage; |
| 414 | |
| 415 | @Nullable |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 416 | List<String> mShortcutIds; |
| 417 | |
| 418 | @Nullable |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 419 | List<LocusId> mLocusIds; |
| 420 | |
| 421 | @Nullable |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 422 | ComponentName mActivity; |
| 423 | |
| 424 | @QueryFlags |
| 425 | int mQueryFlags; |
| 426 | |
| 427 | public ShortcutQuery() { |
| 428 | } |
| 429 | |
| 430 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 431 | * If non-zero, returns only shortcuts that have been added or updated |
| 432 | * since the given timestamp, expressed in milliseconds since the Epoch—see |
| 433 | * {@link System#currentTimeMillis()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 434 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 435 | public ShortcutQuery setChangedSince(long changedSince) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 436 | mChangedSince = changedSince; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 437 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
| 441 | * If non-null, returns only shortcuts from the package. |
| 442 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 443 | public ShortcutQuery setPackage(@Nullable String packageName) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 444 | mPackage = packageName; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 445 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 449 | * 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] | 450 | * a package name must also be set with {@link #setPackage}. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 451 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 452 | public ShortcutQuery setShortcutIds(@Nullable List<String> shortcutIds) { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 453 | mShortcutIds = shortcutIds; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 454 | return this; |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /** |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 458 | * If non-null, return only the specified shortcuts by locus ID. When setting this field, |
| 459 | * a package name must also be set with {@link #setPackage}. |
| 460 | * |
| 461 | * @hide |
| 462 | */ |
| 463 | @SystemApi |
| 464 | @NonNull |
| 465 | public ShortcutQuery setLocusIds(@Nullable List<LocusId> locusIds) { |
| 466 | mLocusIds = locusIds; |
| 467 | return this; |
| 468 | } |
| 469 | |
| 470 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 471 | * If non-null, returns only shortcuts associated with the activity; i.e. |
| 472 | * {@link ShortcutInfo}s whose {@link ShortcutInfo#getActivity()} are equal |
| 473 | * to {@code activity}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 474 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 475 | public ShortcutQuery setActivity(@Nullable ComponentName activity) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 476 | mActivity = activity; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 477 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Hakan Seyalioglu | 58fc95d | 2016-12-13 15:23:22 -0800 | [diff] [blame] | 480 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 481 | * Set query options. At least one of the {@code MATCH} flags should be set. Otherwise, |
| 482 | * no shortcuts will be returned. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 483 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 484 | * <ul> |
| 485 | * <li>{@link #FLAG_MATCH_DYNAMIC} |
| 486 | * <li>{@link #FLAG_MATCH_PINNED} |
| 487 | * <li>{@link #FLAG_MATCH_MANIFEST} |
Mehdi Alizadeh | 505fb76 | 2020-01-21 17:26:24 -0800 | [diff] [blame] | 488 | * <li>{@link #FLAG_MATCH_CACHED} |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 489 | * <li>{@link #FLAG_GET_KEY_FIELDS_ONLY} |
| 490 | * </ul> |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 491 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 492 | public ShortcutQuery setQueryFlags(@QueryFlags int queryFlags) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 493 | mQueryFlags = queryFlags; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 494 | return this; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 495 | } |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 496 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 497 | |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 498 | /** |
| 499 | * Callbacks for shortcut changes to this and related managed profiles. |
| 500 | * |
| 501 | * @hide |
| 502 | */ |
| 503 | public interface ShortcutChangeCallback { |
| 504 | /** |
| 505 | * Indicates that one or more shortcuts, that match the {@link ShortcutQuery} used to |
| 506 | * register this callback, have been added or updated. |
| 507 | * @see LauncherApps#registerShortcutChangeCallback(ShortcutChangeCallback, ShortcutQuery) |
| 508 | * |
| 509 | * <p>Only the applications that are allowed to access the shortcut information, |
| 510 | * as defined in {@link #hasShortcutHostPermission()}, will receive it. |
| 511 | * |
| 512 | * @param packageName The name of the package that has the shortcuts. |
| 513 | * @param shortcuts Shortcuts from the package that have updated or added. Only "key" |
| 514 | * information will be provided, as defined in {@link ShortcutInfo#hasKeyFieldsOnly()}. |
| 515 | * @param user The UserHandle of the profile that generated the change. |
| 516 | * |
| 517 | * @see ShortcutManager |
| 518 | */ |
| 519 | default void onShortcutsAddedOrUpdated(@NonNull String packageName, |
| 520 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) {} |
| 521 | |
| 522 | /** |
| 523 | * Indicates that one or more shortcuts, that match the {@link ShortcutQuery} used to |
| 524 | * register this callback, have been removed. |
| 525 | * @see LauncherApps#registerShortcutChangeCallback(ShortcutChangeCallback, ShortcutQuery) |
| 526 | * |
| 527 | * <p>Only the applications that are allowed to access the shortcut information, |
| 528 | * as defined in {@link #hasShortcutHostPermission()}, will receive it. |
| 529 | * |
| 530 | * @param packageName The name of the package that has the shortcuts. |
| 531 | * @param shortcuts Shortcuts from the package that have been removed. Only "key" |
| 532 | * information will be provided, as defined in {@link ShortcutInfo#hasKeyFieldsOnly()}. |
| 533 | * @param user The UserHandle of the profile that generated the change. |
| 534 | * |
| 535 | * @see ShortcutManager |
| 536 | */ |
| 537 | default void onShortcutsRemoved(@NonNull String packageName, |
| 538 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) {} |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Callback proxy class for {@link ShortcutChangeCallback} |
| 543 | * |
| 544 | * @hide |
| 545 | */ |
| 546 | private static class ShortcutChangeCallbackProxy extends |
| 547 | android.content.pm.IShortcutChangeCallback.Stub { |
| 548 | private final WeakReference<Pair<Executor, ShortcutChangeCallback>> mRemoteReferences; |
| 549 | |
| 550 | ShortcutChangeCallbackProxy(Pair<Executor, ShortcutChangeCallback> remoteReferences) { |
| 551 | mRemoteReferences = new WeakReference<>(remoteReferences); |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public void onShortcutsAddedOrUpdated(@NonNull String packageName, |
| 556 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) { |
| 557 | Pair<Executor, ShortcutChangeCallback> remoteReferences = mRemoteReferences.get(); |
| 558 | if (remoteReferences == null) { |
| 559 | // Binder is dead. |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | final Executor executor = remoteReferences.first; |
| 564 | final ShortcutChangeCallback callback = remoteReferences.second; |
| 565 | executor.execute( |
| 566 | PooledLambda.obtainRunnable(ShortcutChangeCallback::onShortcutsAddedOrUpdated, |
| 567 | callback, packageName, shortcuts, user).recycleOnUse()); |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void onShortcutsRemoved(@NonNull String packageName, |
| 572 | @NonNull List<ShortcutInfo> shortcuts, @NonNull UserHandle user) { |
| 573 | Pair<Executor, ShortcutChangeCallback> remoteReferences = mRemoteReferences.get(); |
| 574 | if (remoteReferences == null) { |
| 575 | // Binder is dead. |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | final Executor executor = remoteReferences.first; |
| 580 | final ShortcutChangeCallback callback = remoteReferences.second; |
| 581 | executor.execute( |
| 582 | PooledLambda.obtainRunnable(ShortcutChangeCallback::onShortcutsRemoved, |
| 583 | callback, packageName, shortcuts, user).recycleOnUse()); |
| 584 | } |
| 585 | } |
| 586 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 587 | /** @hide */ |
| 588 | public LauncherApps(Context context, ILauncherApps service) { |
| 589 | mContext = context; |
| 590 | mService = service; |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 591 | mPm = context.getPackageManager(); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 592 | mUserManager = context.getSystemService(UserManager.class); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 595 | /** @hide */ |
| 596 | @TestApi |
| 597 | public LauncherApps(Context context) { |
| 598 | this(context, ILauncherApps.Stub.asInterface( |
| 599 | ServiceManager.getService(Context.LAUNCHER_APPS_SERVICE))); |
| 600 | } |
| 601 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 602 | /** |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 603 | * Show an error log on logcat, when the calling user is a managed profile, and the target |
| 604 | * user is different from the calling user, in order to help developers to detect it. |
| 605 | */ |
| 606 | private void logErrorForInvalidProfileAccess(@NonNull UserHandle target) { |
| 607 | if (UserHandle.myUserId() != target.getIdentifier() && mUserManager.isManagedProfile()) { |
Makoto Onuki | 3cc7cd1 | 2017-04-03 12:48:42 -0700 | [diff] [blame] | 608 | 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] | 609 | } |
| 610 | } |
| 611 | |
| 612 | /** |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 613 | * Return a list of profiles that the caller can access via the {@link LauncherApps} APIs. |
| 614 | * |
| 615 | * <p>If the caller is running on a managed profile, it'll return only the current profile. |
| 616 | * Otherwise it'll return the same list as {@link UserManager#getUserProfiles()} would. |
| 617 | */ |
| 618 | public List<UserHandle> getProfiles() { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 619 | if (mUserManager.isManagedProfile()) { |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 620 | // If it's a managed profile, only return the current profile. |
| 621 | final List result = new ArrayList(1); |
| 622 | result.add(android.os.Process.myUserHandle()); |
| 623 | return result; |
| 624 | } else { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 625 | return mUserManager.getUserProfiles(); |
Makoto Onuki | aecbd03 | 2017-01-19 12:11:11 -0800 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
Kevin Hufnagle | 25266af | 2019-08-14 17:23:58 -0700 | [diff] [blame] | 630 | * Retrieves a list of activities that specify {@link Intent#ACTION_MAIN} and |
| 631 | * {@link Intent#CATEGORY_LAUNCHER}, across all apps, for a specified user. If an app doesn't |
| 632 | * have any activities that specify <code>ACTION_MAIN</code> or <code>CATEGORY_LAUNCHER</code>, |
| 633 | * the system adds a synthesized activity to the list. This synthesized activity represents the |
| 634 | * app's details page within system settings. |
| 635 | * |
| 636 | * <p class="note"><b>Note: </b>It's possible for system apps, such as app stores, to prevent |
| 637 | * the system from adding synthesized activities to the returned list.</p> |
| 638 | * |
| 639 | * <p>As of <a href="/reference/android/os/Build.VERSION_CODES.html#Q">Android Q</a>, at least |
| 640 | * one of the app's activities or synthesized activities appears in the returned list unless the |
| 641 | * app satisfies at least one of the following conditions:</p> |
| 642 | * <ul> |
| 643 | * <li>The app is a system app.</li> |
| 644 | * <li>The app doesn't request any <a href="/guide/topics/permissions/overview">permissions</a>. |
| 645 | * </li> |
Kevin Hufnagle | b8779b8 | 2019-09-13 02:18:44 +0000 | [diff] [blame] | 646 | * <li>The app doesn't have a <em>launcher activity</em> that is enabled by default. A launcher |
| 647 | * activity has an intent containing the <code>ACTION_MAIN</code> action and the |
| 648 | * <code>CATEGORY_LAUNCHER</code> category.</li> |
Kevin Hufnagle | 25266af | 2019-08-14 17:23:58 -0700 | [diff] [blame] | 649 | * </ul> |
| 650 | * |
| 651 | * <p>Additionally, the system hides synthesized activities for some or all apps in the |
| 652 | * following enterprise-related cases:</p> |
| 653 | * <ul> |
| 654 | * <li>If the device is a |
| 655 | * <a href="https://developers.google.com/android/work/overview#company-owned-devices-for-knowledge-workers">fully |
| 656 | * managed device</a>, no synthesized activities for any app appear in the returned list.</li> |
| 657 | * <li>If the current user has a |
| 658 | * <a href="https://developers.google.com/android/work/overview#employee-owned-devices-byod">work |
| 659 | * profile</a>, no synthesized activities for the user's work apps appear in the returned |
| 660 | * list.</li> |
| 661 | * </ul> |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 662 | * |
| 663 | * @param packageName The specific package to query. If null, it checks all installed packages |
| 664 | * in the profile. |
| 665 | * @param user The UserHandle of the profile. |
| 666 | * @return List of launchable activities. Can be an empty list but will not be null. |
| 667 | */ |
| 668 | public List<LauncherActivityInfo> getActivityList(String packageName, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 669 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 670 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 671 | return convertToActivityList(mService.getLauncherActivities(mContext.getPackageName(), |
| 672 | packageName, user), user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 673 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 674 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 675 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 678 | /** |
| 679 | * Returns the activity info for a given intent and user handle, if it resolves. Otherwise it |
| 680 | * returns null. |
| 681 | * |
| 682 | * @param intent The intent to find a match for. |
| 683 | * @param user The profile to look in for a match. |
| 684 | * @return An activity info object if there is a match. |
| 685 | */ |
| 686 | public LauncherActivityInfo resolveActivity(Intent intent, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 687 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 688 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 689 | ActivityInfo ai = mService.resolveActivity(mContext.getPackageName(), |
| 690 | intent.getComponent(), user); |
Sunny Goyal | 45d3e97 | 2016-03-31 12:38:17 -0700 | [diff] [blame] | 691 | if (ai != null) { |
| 692 | LauncherActivityInfo info = new LauncherActivityInfo(mContext, ai, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 693 | return info; |
| 694 | } |
| 695 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 696 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 697 | } |
| 698 | return null; |
| 699 | } |
| 700 | |
| 701 | /** |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 702 | * Starts a Main activity in the specified profile. |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 703 | * |
| 704 | * @param component The ComponentName of the activity to launch |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 705 | * @param user The UserHandle of the profile |
| 706 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 707 | * @param opts Options to pass to startActivity |
| 708 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 709 | public void startMainActivity(ComponentName component, UserHandle user, Rect sourceBounds, |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 710 | Bundle opts) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 711 | logErrorForInvalidProfileAccess(user); |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 712 | if (DEBUG) { |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 713 | Log.i(TAG, "StartMainActivity " + component + " " + user.getIdentifier()); |
Amith Yamasani | 5abdbb6 | 2014-04-08 17:23:46 -0700 | [diff] [blame] | 714 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 715 | try { |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 716 | mService.startActivityAsUser(mContext.getIApplicationThread(), |
Philip P. Moltmann | 9c5226f | 2020-01-10 08:53:43 -0800 | [diff] [blame^] | 717 | mContext.getPackageName(), mContext.getFeatureId(), |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 718 | component, sourceBounds, opts, user); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 719 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 720 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | /** |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 725 | * Starts an activity to show the details of the specified session. |
| 726 | * |
| 727 | * @param sessionInfo The SessionInfo of the session |
| 728 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 729 | * @param opts Options to pass to startActivity |
| 730 | */ |
Jon Miranda | 9350598 | 2019-03-08 09:33:42 -0800 | [diff] [blame] | 731 | public void startPackageInstallerSessionDetailsActivity(@NonNull SessionInfo sessionInfo, |
| 732 | @Nullable Rect sourceBounds, @Nullable Bundle opts) { |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 733 | try { |
| 734 | mService.startSessionDetailsActivityAsUser(mContext.getIApplicationThread(), |
Philip P. Moltmann | 9c5226f | 2020-01-10 08:53:43 -0800 | [diff] [blame^] | 735 | mContext.getPackageName(), mContext.getFeatureId(), sessionInfo, sourceBounds, |
| 736 | opts, sessionInfo.getUser()); |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 737 | } catch (RemoteException re) { |
| 738 | throw re.rethrowFromSystemServer(); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /** |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 743 | * Starts the settings activity to show the application details for a |
| 744 | * package in the specified profile. |
| 745 | * |
| 746 | * @param component The ComponentName of the package to launch settings for. |
| 747 | * @param user The UserHandle of the profile |
| 748 | * @param sourceBounds The Rect containing the source bounds of the clicked icon |
| 749 | * @param opts Options to pass to startActivity |
| 750 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 751 | public void startAppDetailsActivity(ComponentName component, UserHandle user, |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 752 | Rect sourceBounds, Bundle opts) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 753 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 754 | try { |
Makoto Onuki | 1a34274 | 2018-04-26 14:56:59 -0700 | [diff] [blame] | 755 | mService.showAppDetailsAsUser(mContext.getIApplicationThread(), |
Philip P. Moltmann | 9c5226f | 2020-01-10 08:53:43 -0800 | [diff] [blame^] | 756 | mContext.getPackageName(), mContext.getFeatureId(), |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 757 | component, sourceBounds, opts, user); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 758 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 759 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 466d203 | 2014-07-23 12:23:35 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
| 763 | /** |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 764 | * Retrieves a list of config activities for creating {@link ShortcutInfo}. |
| 765 | * |
| 766 | * @param packageName The specific package to query. If null, it checks all installed packages |
| 767 | * in the profile. |
| 768 | * @param user The UserHandle of the profile. |
| 769 | * @return List of config activities. Can be an empty list but will not be null. |
| 770 | * |
| 771 | * @see Intent#ACTION_CREATE_SHORTCUT |
| 772 | * @see #getShortcutConfigActivityIntent(LauncherActivityInfo) |
| 773 | */ |
| 774 | public List<LauncherActivityInfo> getShortcutConfigActivityList(@Nullable String packageName, |
| 775 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 776 | logErrorForInvalidProfileAccess(user); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 777 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 778 | return convertToActivityList(mService.getShortcutConfigActivities( |
| 779 | mContext.getPackageName(), packageName, user), |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 780 | user); |
| 781 | } catch (RemoteException re) { |
| 782 | throw re.rethrowFromSystemServer(); |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | private List<LauncherActivityInfo> convertToActivityList( |
| 787 | @Nullable ParceledListSlice<ResolveInfo> activities, UserHandle user) { |
| 788 | if (activities == null) { |
| 789 | return Collections.EMPTY_LIST; |
| 790 | } |
| 791 | ArrayList<LauncherActivityInfo> lais = new ArrayList<>(); |
| 792 | for (ResolveInfo ri : activities.getList()) { |
| 793 | LauncherActivityInfo lai = new LauncherActivityInfo(mContext, ri.activityInfo, user); |
| 794 | if (DEBUG) { |
| 795 | Log.v(TAG, "Returning activity for profile " + user + " : " |
| 796 | + lai.getComponentName()); |
| 797 | } |
| 798 | lais.add(lai); |
| 799 | } |
| 800 | return lais; |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Returns an intent sender which can be used to start the configure activity for creating |
| 805 | * custom shortcuts. Use this method if the provider is in another profile as you are not |
| 806 | * allowed to start an activity in another profile. |
| 807 | * |
| 808 | * <p>The caller should receive {@link PinItemRequest} in onActivityResult on |
| 809 | * {@link android.app.Activity#RESULT_OK}. |
| 810 | * |
| 811 | * <p>Callers must be allowed to access the shortcut information, as defined in {@link |
| 812 | * #hasShortcutHostPermission()}. |
| 813 | * |
| 814 | * @param info a configuration activity returned by {@link #getShortcutConfigActivityList} |
| 815 | * |
| 816 | * @throws IllegalStateException when the user is locked or not running. |
| 817 | * @throws SecurityException if {@link #hasShortcutHostPermission()} is false. |
| 818 | * |
| 819 | * @see #getPinItemRequest(Intent) |
| 820 | * @see Intent#ACTION_CREATE_SHORTCUT |
| 821 | * @see android.app.Activity#startIntentSenderForResult |
| 822 | */ |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 823 | @Nullable |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 824 | public IntentSender getShortcutConfigActivityIntent(@NonNull LauncherActivityInfo info) { |
| 825 | try { |
| 826 | return mService.getShortcutConfigActivityIntent( |
| 827 | mContext.getPackageName(), info.getComponentName(), info.getUser()); |
| 828 | } catch (RemoteException re) { |
| 829 | throw re.rethrowFromSystemServer(); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 834 | * Checks if the package is installed and enabled for a profile. |
| 835 | * |
| 836 | * @param packageName The package to check. |
| 837 | * @param user The UserHandle of the profile. |
| 838 | * |
| 839 | * @return true if the package exists and is enabled. |
| 840 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 841 | public boolean isPackageEnabled(String packageName, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 842 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 843 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 844 | return mService.isPackageEnabled(mContext.getPackageName(), packageName, user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 845 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 846 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 847 | } |
| 848 | } |
| 849 | |
| 850 | /** |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 851 | * Gets the launcher extras supplied to the system when the given package was suspended via |
| 852 | * {@code PackageManager#setPackagesSuspended(String[], boolean, PersistableBundle, |
| 853 | * PersistableBundle, String)}. |
| 854 | * |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 855 | * <p>The contents of this {@link Bundle} are supposed to be a contract between the suspending |
| 856 | * app and the launcher. |
| 857 | * |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 858 | * <p>Note: This just returns whatever extras were provided to the system, <em>which might |
| 859 | * even be {@code null}.</em> |
| 860 | * |
| 861 | * @param packageName The package for which to fetch the launcher extras. |
| 862 | * @param user The {@link UserHandle} of the profile. |
| 863 | * @return A {@link Bundle} of launcher extras. Or {@code null} if the package is not currently |
| 864 | * suspended. |
| 865 | * |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 866 | * @see Callback#onPackagesSuspended(String[], UserHandle, Bundle) |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 867 | * @see PackageManager#isPackageSuspended() |
| 868 | */ |
| 869 | public @Nullable Bundle getSuspendedPackageLauncherExtras(String packageName, UserHandle user) { |
| 870 | logErrorForInvalidProfileAccess(user); |
| 871 | try { |
| 872 | return mService.getSuspendedPackageLauncherExtras(packageName, user); |
| 873 | } catch (RemoteException re) { |
| 874 | throw re.rethrowFromSystemServer(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
Suprabh Shukla | 7900049 | 2018-12-24 17:03:02 -0800 | [diff] [blame] | 879 | * Returns whether a package should be hidden from suggestions to the user. Currently, this |
| 880 | * could be done because the package was marked as distracting to the user via |
| 881 | * {@code PackageManager.setDistractingPackageRestrictions(String[], int)}. |
| 882 | * |
| 883 | * @param packageName The package for which to check. |
| 884 | * @param user the {@link UserHandle} of the profile. |
| 885 | * @return |
| 886 | */ |
| 887 | public boolean shouldHideFromSuggestions(@NonNull String packageName, |
| 888 | @NonNull UserHandle user) { |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 889 | Objects.requireNonNull(packageName, "packageName"); |
| 890 | Objects.requireNonNull(user, "user"); |
Suprabh Shukla | 7900049 | 2018-12-24 17:03:02 -0800 | [diff] [blame] | 891 | try { |
| 892 | return mService.shouldHideFromSuggestions(packageName, user); |
| 893 | } catch (RemoteException re) { |
| 894 | throw re.rethrowFromSystemServer(); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | /** |
Benjamin Miller | 7afa84c | 2017-07-17 13:01:35 +0200 | [diff] [blame] | 899 | * Returns {@link ApplicationInfo} about an application installed for a specific user profile. |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 900 | * |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 901 | * @param packageName The package name of the application |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 902 | * @param flags Additional option flags {@link PackageManager#getApplicationInfo} |
| 903 | * @param user The UserHandle of the profile. |
| 904 | * |
Benjamin Miller | 7afa84c | 2017-07-17 13:01:35 +0200 | [diff] [blame] | 905 | * @return {@link ApplicationInfo} containing information about the package. Returns |
| 906 | * {@code null} if the package isn't installed for the given profile, or the profile |
| 907 | * isn't enabled. |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 908 | */ |
Makoto Onuki | 7c7fbf6 | 2017-04-14 11:03:56 -0700 | [diff] [blame] | 909 | public ApplicationInfo getApplicationInfo(@NonNull String packageName, |
| 910 | @ApplicationInfoFlags int flags, @NonNull UserHandle user) |
| 911 | throws PackageManager.NameNotFoundException { |
Daulet Zhanguzin | a2044e1 | 2019-12-30 16:34:59 +0000 | [diff] [blame] | 912 | Objects.requireNonNull(packageName, "packageName"); |
| 913 | Objects.requireNonNull(user, "user"); |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 914 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 915 | try { |
Makoto Onuki | 7c7fbf6 | 2017-04-14 11:03:56 -0700 | [diff] [blame] | 916 | final ApplicationInfo ai = mService |
| 917 | .getApplicationInfo(mContext.getPackageName(), packageName, flags, user); |
| 918 | if (ai == null) { |
| 919 | throw new NameNotFoundException("Package " + packageName + " not found for user " |
| 920 | + user.getIdentifier()); |
| 921 | } |
| 922 | return ai; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 923 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 924 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
| 928 | /** |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 929 | * Returns an object describing the app usage limit for the given package. |
| 930 | * If there are multiple limits that apply to the package, the one with the smallest |
| 931 | * time remaining will be returned. |
| 932 | * |
| 933 | * @param packageName name of the package whose app usage limit will be returned |
| 934 | * @param user the user of the package |
| 935 | * |
| 936 | * @return an {@link AppUsageLimit} object describing the app time limit containing |
| 937 | * the given package with the smallest time remaining, or {@code null} if none exist. |
Varun Shah | e9abb75 | 2019-02-11 11:25:06 -0800 | [diff] [blame] | 938 | * @throws SecurityException when the caller is not the recents app. |
Sunny Goyal | beee197 | 2019-03-29 11:38:16 -0700 | [diff] [blame] | 939 | * @hide |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 940 | */ |
| 941 | @Nullable |
Sunny Goyal | beee197 | 2019-03-29 11:38:16 -0700 | [diff] [blame] | 942 | @SystemApi |
Varun Shah | 2c9263c | 2019-02-15 10:51:10 -0800 | [diff] [blame] | 943 | public LauncherApps.AppUsageLimit getAppUsageLimit(@NonNull String packageName, |
| 944 | @NonNull UserHandle user) { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 945 | try { |
| 946 | return mService.getAppUsageLimit(mContext.getPackageName(), packageName, user); |
| 947 | } catch (RemoteException re) { |
| 948 | throw re.rethrowFromSystemServer(); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | /** |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 953 | * Checks if the activity exists and it enabled for a profile. |
| 954 | * |
Makoto Onuki | 516020a | 2019-01-28 14:16:42 -0800 | [diff] [blame] | 955 | * <p>The activity may still not be exported, in which case {@link #startMainActivity} will |
| 956 | * throw a {@link SecurityException} unless the caller has the same UID as the target app's. |
| 957 | * |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 958 | * @param component The activity to check. |
| 959 | * @param user The UserHandle of the profile. |
| 960 | * |
| 961 | * @return true if the activity exists and is enabled. |
| 962 | */ |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 963 | public boolean isActivityEnabled(ComponentName component, UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 964 | logErrorForInvalidProfileAccess(user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 965 | try { |
Makoto Onuki | 3a2541e | 2017-01-24 09:00:15 -0800 | [diff] [blame] | 966 | return mService.isActivityEnabled(mContext.getPackageName(), component, user); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 967 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 968 | throw re.rethrowFromSystemServer(); |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 972 | /** |
Dianne Hackborn | c160fa4 | 2017-11-01 16:14:26 -0700 | [diff] [blame] | 973 | * Returns whether the caller can access the shortcut information. Access is currently |
| 974 | * available to: |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 975 | * |
Dianne Hackborn | c160fa4 | 2017-11-01 16:14:26 -0700 | [diff] [blame] | 976 | * <ul> |
| 977 | * <li>The current launcher (or default launcher if there is no set current launcher).</li> |
| 978 | * <li>The currently active voice interaction service.</li> |
| 979 | * </ul> |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 980 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 981 | * <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] | 982 | * 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] | 983 | * 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] | 984 | * 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] | 985 | * If the calling launcher application contains pinned shortcuts, they will still work, |
| 986 | * even though the caller no longer has the shortcut host permission. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 987 | * |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 988 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 989 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 990 | * @see ShortcutManager |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 991 | */ |
| 992 | public boolean hasShortcutHostPermission() { |
| 993 | try { |
| 994 | return mService.hasShortcutHostPermission(mContext.getPackageName()); |
| 995 | } catch (RemoteException re) { |
| 996 | throw re.rethrowFromSystemServer(); |
| 997 | } |
| 998 | } |
| 999 | |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 1000 | private List<ShortcutInfo> maybeUpdateDisabledMessage(List<ShortcutInfo> shortcuts) { |
| 1001 | if (shortcuts == null) { |
| 1002 | return null; |
| 1003 | } |
| 1004 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 1005 | final ShortcutInfo si = shortcuts.get(i); |
| 1006 | final String message = ShortcutInfo.getDisabledReasonForRestoreIssue(mContext, |
| 1007 | si.getDisabledReason()); |
| 1008 | if (message != null) { |
| 1009 | si.setDisabledMessage(message); |
| 1010 | } |
| 1011 | } |
| 1012 | return shortcuts; |
| 1013 | } |
| 1014 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 1015 | /** |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1016 | * Returns {@link ShortcutInfo}s that match {@code query}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1017 | * |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 1018 | * <p>Callers must be allowed to access the shortcut information, as defined in {@link |
| 1019 | * #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1020 | * |
| 1021 | * @param query result includes shortcuts matching this query. |
| 1022 | * @param user The UserHandle of the profile. |
| 1023 | * |
| 1024 | * @return the IDs of {@link ShortcutInfo}s that match the query. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1025 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1026 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1027 | * |
| 1028 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1029 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1030 | @Nullable |
| 1031 | public List<ShortcutInfo> getShortcuts(@NonNull ShortcutQuery query, |
| 1032 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 1033 | logErrorForInvalidProfileAccess(user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1034 | try { |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 1035 | // Note this is the only case we need to update the disabled message for shortcuts |
| 1036 | // that weren't restored. |
| 1037 | // The restore problem messages are only shown by the user, and publishers will never |
| 1038 | // see them. The only other API that the launcher gets shortcuts is the shortcut |
| 1039 | // changed callback, but that only returns shortcuts with the "key" information, so |
| 1040 | // that won't return disabled message. |
| 1041 | return maybeUpdateDisabledMessage(mService.getShortcuts(mContext.getPackageName(), |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 1042 | query.mChangedSince, query.mPackage, query.mShortcutIds, query.mLocusIds, |
| 1043 | query.mActivity, query.mQueryFlags, user) |
Makoto Onuki | b1588c0 | 2017-10-12 15:11:45 -0700 | [diff] [blame] | 1044 | .getList()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1045 | } catch (RemoteException e) { |
| 1046 | throw e.rethrowFromSystemServer(); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /** |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1051 | * @hide // No longer used. Use getShortcuts() instead. Kept for unit tests. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1052 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1053 | @Nullable |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1054 | @Deprecated |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1055 | public List<ShortcutInfo> getShortcutInfo(@NonNull String packageName, |
| 1056 | @NonNull List<String> ids, @NonNull UserHandle user) { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1057 | final ShortcutQuery q = new ShortcutQuery(); |
| 1058 | q.setPackage(packageName); |
| 1059 | q.setShortcutIds(ids); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 1060 | q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1061 | return getShortcuts(q, user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1062 | } |
| 1063 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1064 | /** |
| 1065 | * Pin shortcuts on a package. |
| 1066 | * |
| 1067 | * <p>This API is <b>NOT</b> cumulative; this will replace all pinned shortcuts for the package. |
| 1068 | * However, different launchers may have different set of pinned shortcuts. |
| 1069 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1070 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 1071 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1072 | * |
| 1073 | * @param packageName The target package name. |
| 1074 | * @param shortcutIds The IDs of the shortcut to be pinned. |
| 1075 | * @param user The UserHandle of the profile. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1076 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1077 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1078 | * |
| 1079 | * @see ShortcutManager |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1080 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1081 | public void pinShortcuts(@NonNull String packageName, @NonNull List<String> shortcutIds, |
| 1082 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 1083 | logErrorForInvalidProfileAccess(user); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1084 | try { |
| 1085 | mService.pinShortcuts(mContext.getPackageName(), packageName, shortcutIds, user); |
| 1086 | } catch (RemoteException e) { |
| 1087 | throw e.rethrowFromSystemServer(); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /** |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1092 | * @hide kept for testing. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1093 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1094 | @Deprecated |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1095 | public int getShortcutIconResId(@NonNull ShortcutInfo shortcut) { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1096 | return shortcut.getIconResourceId(); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /** |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1100 | * @hide kept for testing. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1101 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1102 | @Deprecated |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1103 | public int getShortcutIconResId(@NonNull String packageName, @NonNull String shortcutId, |
| 1104 | @NonNull UserHandle user) { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1105 | final ShortcutQuery q = new ShortcutQuery(); |
| 1106 | q.setPackage(packageName); |
| 1107 | q.setShortcutIds(Arrays.asList(shortcutId)); |
Makoto Onuki | 4d6b87f | 2016-06-17 13:47:40 -0700 | [diff] [blame] | 1108 | q.setQueryFlags(ShortcutQuery.FLAG_GET_ALL_KINDS); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1109 | final List<ShortcutInfo> shortcuts = getShortcuts(q, user); |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1110 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1111 | return shortcuts.size() > 0 ? shortcuts.get(0).getIconResourceId() : 0; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1115 | * @hide internal/unit tests only |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1116 | */ |
| 1117 | public ParcelFileDescriptor getShortcutIconFd( |
| 1118 | @NonNull ShortcutInfo shortcut) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1119 | return getShortcutIconFd(shortcut.getPackage(), shortcut.getId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1120 | shortcut.getUserId()); |
| 1121 | } |
| 1122 | |
| 1123 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1124 | * @hide internal/unit tests only |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1125 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1126 | public ParcelFileDescriptor getShortcutIconFd( |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1127 | @NonNull String packageName, @NonNull String shortcutId, @NonNull UserHandle user) { |
| 1128 | return getShortcutIconFd(packageName, shortcutId, user.getIdentifier()); |
| 1129 | } |
| 1130 | |
| 1131 | private ParcelFileDescriptor getShortcutIconFd( |
| 1132 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1133 | try { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1134 | return mService.getShortcutIconFd(mContext.getPackageName(), |
| 1135 | packageName, shortcutId, userId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1136 | } catch (RemoteException e) { |
| 1137 | throw e.rethrowFromSystemServer(); |
| 1138 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1139 | } |
| 1140 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1141 | /** |
| 1142 | * Returns the icon for this shortcut, without any badging for the profile. |
| 1143 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1144 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 1145 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1146 | * |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1147 | * @param density The preferred density of the icon, zero for default density. Use |
| 1148 | * density DPI values from {@link DisplayMetrics}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1149 | * |
| 1150 | * @return The drawable associated with the shortcut. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1151 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1152 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1153 | * |
| 1154 | * @see ShortcutManager |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1155 | * @see #getShortcutBadgedIconDrawable(ShortcutInfo, int) |
| 1156 | * @see DisplayMetrics |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1157 | */ |
| 1158 | public Drawable getShortcutIconDrawable(@NonNull ShortcutInfo shortcut, int density) { |
| 1159 | if (shortcut.hasIconFile()) { |
| 1160 | final ParcelFileDescriptor pfd = getShortcutIconFd(shortcut); |
| 1161 | if (pfd == null) { |
| 1162 | return null; |
| 1163 | } |
| 1164 | try { |
| 1165 | final Bitmap bmp = BitmapFactory.decodeFileDescriptor(pfd.getFileDescriptor()); |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1166 | if (bmp != null) { |
| 1167 | BitmapDrawable dr = new BitmapDrawable(mContext.getResources(), bmp); |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 1168 | if (shortcut.hasAdaptiveBitmap()) { |
Hyunyoung Song | be8835e | 2017-02-17 11:25:08 -0800 | [diff] [blame] | 1169 | return new AdaptiveIconDrawable(null, dr); |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1170 | } else { |
| 1171 | return dr; |
| 1172 | } |
| 1173 | } |
| 1174 | return null; |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1175 | } finally { |
| 1176 | try { |
| 1177 | pfd.close(); |
| 1178 | } catch (IOException ignore) { |
| 1179 | } |
| 1180 | } |
| 1181 | } else if (shortcut.hasIconResource()) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1182 | return loadDrawableResourceFromPackage(shortcut.getPackage(), |
| 1183 | shortcut.getIconResourceId(), shortcut.getUserHandle(), density); |
| 1184 | } else if (shortcut.getIcon() != null) { |
| 1185 | // This happens if a shortcut is pending-approval. |
| 1186 | final Icon icon = shortcut.getIcon(); |
| 1187 | switch (icon.getType()) { |
| 1188 | case Icon.TYPE_RESOURCE: { |
| 1189 | return loadDrawableResourceFromPackage(shortcut.getPackage(), |
| 1190 | icon.getResId(), shortcut.getUserHandle(), density); |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1191 | } |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1192 | case Icon.TYPE_BITMAP: |
Hyunyoung Song | e4179e2 | 2017-03-01 12:51:26 -0800 | [diff] [blame] | 1193 | case Icon.TYPE_ADAPTIVE_BITMAP: { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1194 | return icon.loadDrawable(mContext); |
| 1195 | } |
| 1196 | default: |
| 1197 | return null; // Shouldn't happen though. |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1198 | } |
| 1199 | } else { |
| 1200 | return null; // Has no icon. |
| 1201 | } |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1204 | private Drawable loadDrawableResourceFromPackage(String packageName, int resId, |
| 1205 | UserHandle user, int density) { |
| 1206 | try { |
| 1207 | if (resId == 0) { |
| 1208 | return null; // Shouldn't happen but just in case. |
| 1209 | } |
| 1210 | final ApplicationInfo ai = getApplicationInfo(packageName, /* flags =*/ 0, user); |
| 1211 | final Resources res = mContext.getPackageManager().getResourcesForApplication(ai); |
| 1212 | return res.getDrawableForDensity(resId, density); |
| 1213 | } catch (NameNotFoundException | Resources.NotFoundException e) { |
| 1214 | return null; |
| 1215 | } |
| 1216 | } |
| 1217 | |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1218 | /** |
| 1219 | * Returns the shortcut icon with badging appropriate for the profile. |
| 1220 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1221 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 1222 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1223 | * |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1224 | * @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] | 1225 | * @return A badged icon for the shortcut. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1226 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1227 | * is locked or not running. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1228 | * |
| 1229 | * @see ShortcutManager |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1230 | * @see #getShortcutIconDrawable(ShortcutInfo, int) |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1231 | * @see DisplayMetrics |
Makoto Onuki | 04b9aab | 2016-05-23 17:13:30 -0700 | [diff] [blame] | 1232 | */ |
| 1233 | public Drawable getShortcutBadgedIconDrawable(ShortcutInfo shortcut, int density) { |
| 1234 | final Drawable originalIcon = getShortcutIconDrawable(shortcut, density); |
| 1235 | |
| 1236 | return (originalIcon == null) ? null : mContext.getPackageManager().getUserBadgedIcon( |
| 1237 | originalIcon, shortcut.getUserHandle()); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1240 | /** |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 1241 | * Starts a shortcut. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1242 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1243 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 1244 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1245 | * |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 1246 | * @param packageName The target shortcut package name. |
| 1247 | * @param shortcutId The target shortcut ID. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1248 | * @param sourceBounds The Rect containing the source bounds of the clicked icon. |
| 1249 | * @param startActivityOptions Options to pass to startActivity. |
| 1250 | * @param user The UserHandle of the profile. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1251 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1252 | * is locked or not running. |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1253 | * |
| 1254 | * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g. |
| 1255 | * 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] | 1256 | */ |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1257 | public void startShortcut(@NonNull String packageName, @NonNull String shortcutId, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1258 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions, |
| 1259 | @NonNull UserHandle user) { |
Makoto Onuki | de3c16c | 2017-01-26 11:39:31 -0800 | [diff] [blame] | 1260 | logErrorForInvalidProfileAccess(user); |
| 1261 | |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1262 | startShortcut(packageName, shortcutId, sourceBounds, startActivityOptions, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1263 | user.getIdentifier()); |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * Launches a shortcut. |
| 1268 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 1269 | * <p>The calling launcher application must be allowed to access the shortcut information, |
| 1270 | * as defined in {@link #hasShortcutHostPermission()}. |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1271 | * |
| 1272 | * @param shortcut The target shortcut. |
| 1273 | * @param sourceBounds The Rect containing the source bounds of the clicked icon. |
| 1274 | * @param startActivityOptions Options to pass to startActivity. |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1275 | * @throws IllegalStateException when the user is locked, or when the {@code user} user |
| 1276 | * is locked or not running. |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1277 | * |
| 1278 | * @throws android.content.ActivityNotFoundException failed to start shortcut. (e.g. |
| 1279 | * 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] | 1280 | */ |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1281 | public void startShortcut(@NonNull ShortcutInfo shortcut, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1282 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions) { |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1283 | startShortcut(shortcut.getPackage(), shortcut.getId(), |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1284 | sourceBounds, startActivityOptions, |
| 1285 | shortcut.getUserId()); |
| 1286 | } |
| 1287 | |
Mathew Inwood | 5c0d354 | 2018-08-14 13:54:31 +0100 | [diff] [blame] | 1288 | @UnsupportedAppUsage |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 1289 | private void startShortcut(@NonNull String packageName, @NonNull String shortcutId, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1290 | @Nullable Rect sourceBounds, @Nullable Bundle startActivityOptions, |
| 1291 | int userId) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1292 | try { |
Philip P. Moltmann | 9c5226f | 2020-01-10 08:53:43 -0800 | [diff] [blame^] | 1293 | final boolean success = mService.startShortcut(mContext.getPackageName(), packageName, |
| 1294 | null /* default featureId */, shortcutId, sourceBounds, startActivityOptions, |
| 1295 | userId); |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 1296 | if (!success) { |
| 1297 | throw new ActivityNotFoundException("Shortcut could not be started"); |
| 1298 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1299 | } catch (RemoteException e) { |
| 1300 | throw e.rethrowFromSystemServer(); |
| 1301 | } |
| 1302 | } |
Kenny Guy | 53fa4ec | 2014-04-29 14:24:18 +0100 | [diff] [blame] | 1303 | |
| 1304 | /** |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1305 | * Registers a callback for changes to packages in this user and managed profiles. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1306 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1307 | * @param callback The callback to register. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1308 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1309 | public void registerCallback(Callback callback) { |
| 1310 | registerCallback(callback, null); |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /** |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1314 | * Registers a callback for changes to packages in this user and managed profiles. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1315 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1316 | * @param callback The callback to register. |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1317 | * @param handler that should be used to post callbacks on, may be null. |
| 1318 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1319 | public void registerCallback(Callback callback, Handler handler) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1320 | synchronized (this) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1321 | if (callback != null && findCallbackLocked(callback) < 0) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1322 | boolean addedFirstCallback = mCallbacks.size() == 0; |
| 1323 | addCallbackLocked(callback, handler); |
| 1324 | if (addedFirstCallback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1325 | try { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1326 | mService.addOnAppsChangedListener(mContext.getPackageName(), |
| 1327 | mAppsChangedListener); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1328 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1329 | throw re.rethrowFromSystemServer(); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1330 | } |
| 1331 | } |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | |
| 1336 | /** |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1337 | * Unregisters a callback that was previously registered. |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1338 | * |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1339 | * @param callback The callback to unregister. |
| 1340 | * @see #registerCallback(Callback) |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1341 | */ |
Kenny Guy | 10a574f | 2014-08-26 16:17:58 +0100 | [diff] [blame] | 1342 | public void unregisterCallback(Callback callback) { |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1343 | synchronized (this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1344 | removeCallbackLocked(callback); |
Kenny Guy | 44b6dee | 2014-07-10 18:10:14 +0100 | [diff] [blame] | 1345 | if (mCallbacks.size() == 0) { |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 1346 | try { |
| 1347 | mService.removeOnAppsChangedListener(mAppsChangedListener); |
| 1348 | } catch (RemoteException re) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1349 | throw re.rethrowFromSystemServer(); |
Amith Yamasani | e781c81 | 2014-05-28 15:28:18 -0700 | [diff] [blame] | 1350 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1355 | /** @return position in mCallbacks for callback or -1 if not present. */ |
| 1356 | private int findCallbackLocked(Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1357 | if (callback == null) { |
| 1358 | throw new IllegalArgumentException("Callback cannot be null"); |
| 1359 | } |
| 1360 | final int size = mCallbacks.size(); |
| 1361 | for (int i = 0; i < size; ++i) { |
| 1362 | if (mCallbacks.get(i).mCallback == callback) { |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1363 | return i; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1364 | } |
| 1365 | } |
Kenny Guy | 172a216 | 2015-06-19 17:21:28 +0100 | [diff] [blame] | 1366 | return -1; |
| 1367 | } |
| 1368 | |
| 1369 | private void removeCallbackLocked(Callback callback) { |
| 1370 | int pos = findCallbackLocked(callback); |
| 1371 | if (pos >= 0) { |
| 1372 | mCallbacks.remove(pos); |
| 1373 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1376 | private void addCallbackLocked(Callback callback, Handler handler) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1377 | // Remove if already present. |
| 1378 | removeCallbackLocked(callback); |
| 1379 | if (handler == null) { |
| 1380 | handler = new Handler(); |
| 1381 | } |
| 1382 | CallbackMessageHandler toAdd = new CallbackMessageHandler(handler.getLooper(), callback); |
| 1383 | mCallbacks.add(toAdd); |
| 1384 | } |
| 1385 | |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1386 | private IOnAppsChangedListener.Stub mAppsChangedListener = new IOnAppsChangedListener.Stub() { |
| 1387 | |
| 1388 | @Override |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1389 | public void onPackageRemoved(UserHandle user, String packageName) |
| 1390 | throws RemoteException { |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1391 | if (DEBUG) { |
| 1392 | Log.d(TAG, "onPackageRemoved " + user.getIdentifier() + "," + packageName); |
| 1393 | } |
| 1394 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1395 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1396 | callback.postOnPackageRemoved(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1397 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | @Override |
| 1402 | public void onPackageChanged(UserHandle user, String packageName) throws RemoteException { |
| 1403 | if (DEBUG) { |
| 1404 | Log.d(TAG, "onPackageChanged " + user.getIdentifier() + "," + packageName); |
| 1405 | } |
| 1406 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1407 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1408 | callback.postOnPackageChanged(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1409 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | @Override |
| 1414 | public void onPackageAdded(UserHandle user, String packageName) throws RemoteException { |
| 1415 | if (DEBUG) { |
| 1416 | Log.d(TAG, "onPackageAdded " + user.getIdentifier() + "," + packageName); |
| 1417 | } |
| 1418 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1419 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1420 | callback.postOnPackageAdded(packageName, user); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1421 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | @Override |
| 1426 | public void onPackagesAvailable(UserHandle user, String[] packageNames, boolean replacing) |
| 1427 | throws RemoteException { |
| 1428 | if (DEBUG) { |
| 1429 | Log.d(TAG, "onPackagesAvailable " + user.getIdentifier() + "," + packageNames); |
| 1430 | } |
| 1431 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1432 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1433 | callback.postOnPackagesAvailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1434 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | @Override |
| 1439 | public void onPackagesUnavailable(UserHandle user, String[] packageNames, boolean replacing) |
| 1440 | throws RemoteException { |
| 1441 | if (DEBUG) { |
| 1442 | Log.d(TAG, "onPackagesUnavailable " + user.getIdentifier() + "," + packageNames); |
| 1443 | } |
| 1444 | synchronized (LauncherApps.this) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1445 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1446 | callback.postOnPackagesUnavailable(packageNames, user, replacing); |
Kenny Guy | c0154537 | 2014-06-16 14:17:26 +0100 | [diff] [blame] | 1447 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | @Override |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 1452 | public void onPackagesSuspended(UserHandle user, String[] packageNames, |
| 1453 | Bundle launcherExtras) |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1454 | throws RemoteException { |
| 1455 | if (DEBUG) { |
| 1456 | Log.d(TAG, "onPackagesSuspended " + user.getIdentifier() + "," + packageNames); |
| 1457 | } |
| 1458 | synchronized (LauncherApps.this) { |
| 1459 | for (CallbackMessageHandler callback : mCallbacks) { |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 1460 | callback.postOnPackagesSuspended(packageNames, launcherExtras, user); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | @Override |
| 1466 | public void onPackagesUnsuspended(UserHandle user, String[] packageNames) |
| 1467 | throws RemoteException { |
| 1468 | if (DEBUG) { |
| 1469 | Log.d(TAG, "onPackagesUnsuspended " + user.getIdentifier() + "," + packageNames); |
| 1470 | } |
| 1471 | synchronized (LauncherApps.this) { |
| 1472 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1473 | callback.postOnPackagesUnsuspended(packageNames, user); |
| 1474 | } |
| 1475 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1476 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1477 | |
| 1478 | @Override |
| 1479 | public void onShortcutChanged(UserHandle user, String packageName, |
| 1480 | ParceledListSlice shortcuts) { |
| 1481 | if (DEBUG) { |
| 1482 | Log.d(TAG, "onShortcutChanged " + user.getIdentifier() + "," + packageName); |
| 1483 | } |
| 1484 | final List<ShortcutInfo> list = shortcuts.getList(); |
| 1485 | synchronized (LauncherApps.this) { |
| 1486 | for (CallbackMessageHandler callback : mCallbacks) { |
| 1487 | callback.postOnShortcutChanged(packageName, user, list); |
| 1488 | } |
| 1489 | } |
| 1490 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 1491 | }; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1492 | |
| 1493 | private static class CallbackMessageHandler extends Handler { |
| 1494 | private static final int MSG_ADDED = 1; |
| 1495 | private static final int MSG_REMOVED = 2; |
| 1496 | private static final int MSG_CHANGED = 3; |
| 1497 | private static final int MSG_AVAILABLE = 4; |
| 1498 | private static final int MSG_UNAVAILABLE = 5; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1499 | private static final int MSG_SUSPENDED = 6; |
| 1500 | private static final int MSG_UNSUSPENDED = 7; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1501 | private static final int MSG_SHORTCUT_CHANGED = 8; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1502 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1503 | private LauncherApps.Callback mCallback; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1504 | |
| 1505 | private static class CallbackInfo { |
| 1506 | String[] packageNames; |
| 1507 | String packageName; |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 1508 | Bundle launcherExtras; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1509 | boolean replacing; |
| 1510 | UserHandle user; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1511 | List<ShortcutInfo> shortcuts; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1512 | } |
| 1513 | |
Kenny Guy | f939dba | 2014-08-15 15:32:34 +0100 | [diff] [blame] | 1514 | public CallbackMessageHandler(Looper looper, LauncherApps.Callback callback) { |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1515 | super(looper, null, true); |
| 1516 | mCallback = callback; |
| 1517 | } |
| 1518 | |
| 1519 | @Override |
| 1520 | public void handleMessage(Message msg) { |
| 1521 | if (mCallback == null || !(msg.obj instanceof CallbackInfo)) { |
| 1522 | return; |
| 1523 | } |
| 1524 | CallbackInfo info = (CallbackInfo) msg.obj; |
| 1525 | switch (msg.what) { |
| 1526 | case MSG_ADDED: |
| 1527 | mCallback.onPackageAdded(info.packageName, info.user); |
| 1528 | break; |
| 1529 | case MSG_REMOVED: |
| 1530 | mCallback.onPackageRemoved(info.packageName, info.user); |
| 1531 | break; |
| 1532 | case MSG_CHANGED: |
| 1533 | mCallback.onPackageChanged(info.packageName, info.user); |
| 1534 | break; |
| 1535 | case MSG_AVAILABLE: |
| 1536 | mCallback.onPackagesAvailable(info.packageNames, info.user, info.replacing); |
| 1537 | break; |
| 1538 | case MSG_UNAVAILABLE: |
| 1539 | mCallback.onPackagesUnavailable(info.packageNames, info.user, info.replacing); |
| 1540 | break; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1541 | case MSG_SUSPENDED: |
Suprabh Shukla | 96212bc | 2018-04-10 15:04:51 -0700 | [diff] [blame] | 1542 | mCallback.onPackagesSuspended(info.packageNames, info.user, info.launcherExtras |
| 1543 | ); |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1544 | break; |
| 1545 | case MSG_UNSUSPENDED: |
| 1546 | mCallback.onPackagesUnsuspended(info.packageNames, info.user); |
| 1547 | break; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1548 | case MSG_SHORTCUT_CHANGED: |
| 1549 | mCallback.onShortcutsChanged(info.packageName, info.shortcuts, info.user); |
| 1550 | break; |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | |
| 1554 | public void postOnPackageAdded(String packageName, UserHandle user) { |
| 1555 | CallbackInfo info = new CallbackInfo(); |
| 1556 | info.packageName = packageName; |
| 1557 | info.user = user; |
| 1558 | obtainMessage(MSG_ADDED, info).sendToTarget(); |
| 1559 | } |
| 1560 | |
| 1561 | public void postOnPackageRemoved(String packageName, UserHandle user) { |
| 1562 | CallbackInfo info = new CallbackInfo(); |
| 1563 | info.packageName = packageName; |
| 1564 | info.user = user; |
| 1565 | obtainMessage(MSG_REMOVED, info).sendToTarget(); |
| 1566 | } |
| 1567 | |
| 1568 | public void postOnPackageChanged(String packageName, UserHandle user) { |
| 1569 | CallbackInfo info = new CallbackInfo(); |
| 1570 | info.packageName = packageName; |
| 1571 | info.user = user; |
| 1572 | obtainMessage(MSG_CHANGED, info).sendToTarget(); |
| 1573 | } |
| 1574 | |
| 1575 | public void postOnPackagesAvailable(String[] packageNames, UserHandle user, |
| 1576 | boolean replacing) { |
| 1577 | CallbackInfo info = new CallbackInfo(); |
| 1578 | info.packageNames = packageNames; |
| 1579 | info.replacing = replacing; |
| 1580 | info.user = user; |
| 1581 | obtainMessage(MSG_AVAILABLE, info).sendToTarget(); |
| 1582 | } |
| 1583 | |
| 1584 | public void postOnPackagesUnavailable(String[] packageNames, UserHandle user, |
| 1585 | boolean replacing) { |
| 1586 | CallbackInfo info = new CallbackInfo(); |
| 1587 | info.packageNames = packageNames; |
| 1588 | info.replacing = replacing; |
| 1589 | info.user = user; |
| 1590 | obtainMessage(MSG_UNAVAILABLE, info).sendToTarget(); |
| 1591 | } |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1592 | |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 1593 | public void postOnPackagesSuspended(String[] packageNames, Bundle launcherExtras, |
| 1594 | UserHandle user) { |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1595 | CallbackInfo info = new CallbackInfo(); |
| 1596 | info.packageNames = packageNames; |
| 1597 | info.user = user; |
Suprabh Shukla | 19b41f3 | 2018-03-26 22:35:13 -0700 | [diff] [blame] | 1598 | info.launcherExtras = launcherExtras; |
Kenny Guy | 7724275 | 2016-01-15 13:29:06 +0000 | [diff] [blame] | 1599 | obtainMessage(MSG_SUSPENDED, info).sendToTarget(); |
| 1600 | } |
| 1601 | |
| 1602 | public void postOnPackagesUnsuspended(String[] packageNames, UserHandle user) { |
| 1603 | CallbackInfo info = new CallbackInfo(); |
| 1604 | info.packageNames = packageNames; |
| 1605 | info.user = user; |
| 1606 | obtainMessage(MSG_UNSUSPENDED, info).sendToTarget(); |
| 1607 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1608 | |
| 1609 | public void postOnShortcutChanged(String packageName, UserHandle user, |
| 1610 | List<ShortcutInfo> shortcuts) { |
| 1611 | CallbackInfo info = new CallbackInfo(); |
| 1612 | info.packageName = packageName; |
| 1613 | info.user = user; |
| 1614 | info.shortcuts = shortcuts; |
| 1615 | obtainMessage(MSG_SHORTCUT_CHANGED, info).sendToTarget(); |
| 1616 | } |
Kenny Guy | b42c89b | 2014-07-28 19:20:07 +0100 | [diff] [blame] | 1617 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1618 | |
| 1619 | /** |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1620 | * Register a callback to watch for session lifecycle events in this user and managed profiles. |
| 1621 | * @param callback The callback to register. |
| 1622 | * @param executor {@link Executor} to handle the callbacks, cannot be null. |
| 1623 | * |
| 1624 | * @see PackageInstaller#registerSessionCallback(SessionCallback) |
| 1625 | */ |
| 1626 | public void registerPackageInstallerSessionCallback( |
| 1627 | @NonNull @CallbackExecutor Executor executor, @NonNull SessionCallback callback) { |
| 1628 | if (executor == null) { |
| 1629 | throw new NullPointerException("Executor must not be null"); |
| 1630 | } |
| 1631 | |
| 1632 | synchronized (mDelegates) { |
| 1633 | final SessionCallbackDelegate delegate = new SessionCallbackDelegate(callback, |
| 1634 | executor); |
| 1635 | try { |
| 1636 | mService.registerPackageInstallerCallback(mContext.getPackageName(), |
| 1637 | delegate); |
| 1638 | } catch (RemoteException e) { |
| 1639 | throw e.rethrowFromSystemServer(); |
| 1640 | } |
| 1641 | mDelegates.add(delegate); |
| 1642 | } |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * Unregisters a callback that was previously registered. |
| 1647 | * |
| 1648 | * @param callback The callback to unregister. |
| 1649 | * @see #registerPackageInstallerSessionCallback(Executor, SessionCallback) |
| 1650 | */ |
Jon Miranda | 9350598 | 2019-03-08 09:33:42 -0800 | [diff] [blame] | 1651 | public void unregisterPackageInstallerSessionCallback(@NonNull SessionCallback callback) { |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1652 | synchronized (mDelegates) { |
| 1653 | for (Iterator<SessionCallbackDelegate> i = mDelegates.iterator(); i.hasNext();) { |
| 1654 | final SessionCallbackDelegate delegate = i.next(); |
| 1655 | if (delegate.mCallback == callback) { |
| 1656 | mPm.getPackageInstaller().unregisterSessionCallback(delegate.mCallback); |
| 1657 | i.remove(); |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | /** |
| 1664 | * Return list of all known install sessions in this user and managed profiles, regardless |
| 1665 | * of the installer. |
| 1666 | * |
| 1667 | * @see PackageInstaller#getAllSessions() |
| 1668 | */ |
| 1669 | public @NonNull List<SessionInfo> getAllPackageInstallerSessions() { |
| 1670 | try { |
| 1671 | return mService.getAllSessions(mContext.getPackageName()).getList(); |
| 1672 | } catch (RemoteException e) { |
| 1673 | throw e.rethrowFromSystemServer(); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | /** |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 1678 | * Register a callback to watch for shortcut change events in this user and managed profiles. |
| 1679 | * |
| 1680 | * @param callback The callback to register. |
| 1681 | * @param query {@link ShortcutQuery} to match and filter the shortcut events. Only matching |
| 1682 | * shortcuts will be returned by the callback. |
| 1683 | * @param executor {@link Executor} to handle the callbacks. To dispatch callbacks to the main |
| 1684 | * thread of your application, you can use {@link android.content.Context#getMainExecutor()}. |
| 1685 | * |
| 1686 | * @hide |
| 1687 | */ |
| 1688 | public void registerShortcutChangeCallback(@NonNull ShortcutChangeCallback callback, |
| 1689 | @NonNull ShortcutQuery query, @NonNull @CallbackExecutor Executor executor) { |
| 1690 | Objects.requireNonNull(callback, "Callback cannot be null"); |
| 1691 | Objects.requireNonNull(query, "Query cannot be null"); |
| 1692 | Objects.requireNonNull(executor, "Executor cannot be null"); |
| 1693 | |
| 1694 | synchronized (mShortcutChangeCallbacks) { |
| 1695 | final int callbackId = callback.hashCode(); |
| 1696 | final Pair<Executor, ShortcutChangeCallback> state = new Pair<>(executor, callback); |
| 1697 | mShortcutChangeCallbacks.put(callbackId, state); |
| 1698 | try { |
| 1699 | mService.registerShortcutChangeCallback(mContext.getPackageName(), |
Mehdi Alizadeh | ae808ff | 2020-01-21 13:39:53 -0800 | [diff] [blame] | 1700 | query.mChangedSince, query.mPackage, query.mShortcutIds, query.mLocusIds, |
| 1701 | query.mActivity, query.mQueryFlags, new ShortcutChangeCallbackProxy(state), |
| 1702 | callbackId); |
Mehdi Alizadeh | 9f68019 | 2020-01-16 18:09:24 -0800 | [diff] [blame] | 1703 | } catch (RemoteException e) { |
| 1704 | throw e.rethrowFromSystemServer(); |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * Unregisters a callback that was previously registered. |
| 1711 | * @see #registerShortcutChangeCallback(ShortcutChangeCallback, ShortcutQuery, Executor) |
| 1712 | * |
| 1713 | * @param callback Callback to be unregistered. |
| 1714 | * |
| 1715 | * @hide |
| 1716 | */ |
| 1717 | public void unregisterShortcutChangeCallback(@NonNull ShortcutChangeCallback callback) { |
| 1718 | Objects.requireNonNull(callback, "Callback cannot be null"); |
| 1719 | |
| 1720 | synchronized (mShortcutChangeCallbacks) { |
| 1721 | final int callbackId = callback.hashCode(); |
| 1722 | if (mShortcutChangeCallbacks.containsKey(callbackId)) { |
| 1723 | mShortcutChangeCallbacks.remove(callbackId); |
| 1724 | try { |
| 1725 | mService.unregisterShortcutChangeCallback(mContext.getPackageName(), |
| 1726 | callbackId); |
| 1727 | } catch (RemoteException e) { |
| 1728 | throw e.rethrowFromSystemServer(); |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | /** |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1735 | * A helper method to extract a {@link PinItemRequest} set to |
| 1736 | * the {@link #EXTRA_PIN_ITEM_REQUEST} extra. |
| 1737 | */ |
| 1738 | public PinItemRequest getPinItemRequest(Intent intent) { |
| 1739 | return intent.getParcelableExtra(EXTRA_PIN_ITEM_REQUEST); |
| 1740 | } |
| 1741 | |
| 1742 | /** |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 1743 | * Represents a "pin shortcut" or a "pin appwidget" request made by an app, which is sent with |
| 1744 | * an {@link #ACTION_CONFIRM_PIN_SHORTCUT} or {@link #ACTION_CONFIRM_PIN_APPWIDGET} intent |
| 1745 | * respectively to the default launcher app. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1746 | * |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1747 | * <h3>Request of the {@link #REQUEST_TYPE_SHORTCUT} type. |
Makoto Onuki | 0c28071 | 2017-01-19 15:14:27 -0800 | [diff] [blame] | 1748 | * |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1749 | * <p>A {@link #REQUEST_TYPE_SHORTCUT} request represents a request to pin a |
| 1750 | * {@link ShortcutInfo}. If the launcher accepts a request, call {@link #accept()}, |
| 1751 | * or {@link #accept(Bundle)} with a null or empty Bundle. No options are defined for |
| 1752 | * pin-shortcuts requests. |
| 1753 | * |
| 1754 | * <p>{@link #getShortcutInfo()} always returns a non-null {@link ShortcutInfo} for this type. |
| 1755 | * |
| 1756 | * <p>The launcher may receive a request with a {@link ShortcutInfo} that is already pinned, in |
| 1757 | * which case {@link ShortcutInfo#isPinned()} returns true. This means the user wants to create |
| 1758 | * another pinned shortcut for a shortcut that's already pinned. If the launcher accepts it, |
| 1759 | * {@link #accept()} must still be called even though the shortcut is already pinned, and |
| 1760 | * create a new pinned shortcut icon for it. |
| 1761 | * |
| 1762 | * <p>See also {@link ShortcutManager} for more details. |
| 1763 | * |
| 1764 | * <h3>Request of the {@link #REQUEST_TYPE_APPWIDGET} type. |
| 1765 | * |
| 1766 | * <p>A {@link #REQUEST_TYPE_SHORTCUT} request represents a request to pin a |
| 1767 | * an AppWidget. If the launcher accepts a request, call {@link #accept(Bundle)} with |
| 1768 | * the appwidget integer ID set to the |
| 1769 | * {@link android.appwidget.AppWidgetManager#EXTRA_APPWIDGET_ID} extra. |
| 1770 | * |
| 1771 | * <p>{@link #getAppWidgetProviderInfo(Context)} always returns a non-null |
| 1772 | * {@link AppWidgetProviderInfo} for this type. |
| 1773 | * |
| 1774 | * <p>See also {@link AppWidgetManager} for more details. |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 1775 | * |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1776 | * @see #EXTRA_PIN_ITEM_REQUEST |
| 1777 | * @see #getPinItemRequest(Intent) |
| 1778 | */ |
| 1779 | public static final class PinItemRequest implements Parcelable { |
| 1780 | |
| 1781 | /** This is a request to pin shortcut. */ |
| 1782 | public static final int REQUEST_TYPE_SHORTCUT = 1; |
| 1783 | |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1784 | /** This is a request to pin app widget. */ |
| 1785 | public static final int REQUEST_TYPE_APPWIDGET = 2; |
| 1786 | |
Makoto Onuki | f5663b1 | 2017-01-09 14:09:56 -0800 | [diff] [blame] | 1787 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 1788 | @IntDef(prefix = { "REQUEST_TYPE_" }, value = { |
| 1789 | REQUEST_TYPE_SHORTCUT, |
| 1790 | REQUEST_TYPE_APPWIDGET |
| 1791 | }) |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1792 | @Retention(RetentionPolicy.SOURCE) |
| 1793 | public @interface RequestType {} |
| 1794 | |
| 1795 | private final int mRequestType; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1796 | private final IPinItemRequest mInner; |
| 1797 | |
| 1798 | /** |
| 1799 | * @hide |
| 1800 | */ |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1801 | public PinItemRequest(IPinItemRequest inner, int type) { |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1802 | mInner = inner; |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1803 | mRequestType = type; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1807 | * Represents the type of a request, which is one of the {@code REQUEST_TYPE_} constants. |
| 1808 | * |
| 1809 | * @return one of the {@code REQUEST_TYPE_} constants. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1810 | */ |
| 1811 | @RequestType |
| 1812 | public int getRequestType() { |
| 1813 | return mRequestType; |
| 1814 | } |
| 1815 | |
| 1816 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1817 | * {@link ShortcutInfo} sent by the requesting app. |
| 1818 | * Always non-null for a {@link #REQUEST_TYPE_SHORTCUT} request, and always null for a |
| 1819 | * different request type. |
| 1820 | * |
| 1821 | * @return requested {@link ShortcutInfo} when a request is of the |
| 1822 | * {@link #REQUEST_TYPE_SHORTCUT} type. Null otherwise. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1823 | */ |
| 1824 | @Nullable |
| 1825 | public ShortcutInfo getShortcutInfo() { |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1826 | try { |
| 1827 | return mInner.getShortcutInfo(); |
| 1828 | } catch (RemoteException e) { |
| 1829 | throw e.rethrowAsRuntimeException(); |
| 1830 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1834 | * {@link AppWidgetProviderInfo} sent by the requesting app. |
| 1835 | * Always non-null for a {@link #REQUEST_TYPE_APPWIDGET} request, and always null for a |
| 1836 | * different request type. |
| 1837 | * |
Sunny Goyal | e7712ad | 2018-03-08 11:23:06 -0800 | [diff] [blame] | 1838 | * <p>Launcher should not show any configuration activity associated with the provider, and |
| 1839 | * assume that the widget is already fully configured. Upon accepting the widget, it should |
| 1840 | * pass the widgetId in {@link #accept(Bundle)}. |
| 1841 | * |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1842 | * @return requested {@link AppWidgetProviderInfo} when a request is of the |
| 1843 | * {@link #REQUEST_TYPE_APPWIDGET} type. Null otherwise. |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1844 | */ |
| 1845 | @Nullable |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1846 | public AppWidgetProviderInfo getAppWidgetProviderInfo(Context context) { |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1847 | try { |
| 1848 | final AppWidgetProviderInfo info = mInner.getAppWidgetProviderInfo(); |
| 1849 | if (info == null) { |
| 1850 | return null; |
| 1851 | } |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1852 | info.updateDimensions(context.getResources().getDisplayMetrics()); |
| 1853 | return info; |
Makoto Onuki | 8abba3a | 2017-02-24 11:34:54 -0800 | [diff] [blame] | 1854 | } catch (RemoteException e) { |
| 1855 | throw e.rethrowAsRuntimeException(); |
Sunny Goyal | 970d4b4 | 2017-01-19 15:07:36 -0800 | [diff] [blame] | 1856 | } |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /** |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1860 | * Any extras sent by the requesting app. |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1861 | * |
| 1862 | * @return For a shortcut request, this method always return null. For an AppWidget |
| 1863 | * request, this method returns the extras passed to the |
| 1864 | * {@link android.appwidget.AppWidgetManager#requestPinAppWidget( |
| 1865 | * ComponentName, Bundle, PendingIntent)} API. See {@link AppWidgetManager} for details. |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1866 | */ |
| 1867 | @Nullable |
| 1868 | public Bundle getExtras() { |
| 1869 | try { |
| 1870 | return mInner.getExtras(); |
| 1871 | } catch (RemoteException e) { |
| 1872 | throw e.rethrowAsRuntimeException(); |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1877 | * Return whether a request is still valid. |
| 1878 | * |
| 1879 | * @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] | 1880 | */ |
| 1881 | public boolean isValid() { |
| 1882 | try { |
| 1883 | return mInner.isValid(); |
| 1884 | } catch (RemoteException e) { |
| 1885 | return false; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /** |
| 1890 | * Called by the receiving launcher app when the user accepts the request. |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1891 | * |
| 1892 | * @param options must be set for a {@link #REQUEST_TYPE_APPWIDGET} request. |
| 1893 | * |
| 1894 | * @return {@code TRUE} if the shortcut or the AppWidget has actually been pinned. |
| 1895 | * {@code FALSE} if the item hasn't been pinned, for example, because the request had |
| 1896 | * 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] | 1897 | */ |
| 1898 | public boolean accept(@Nullable Bundle options) { |
| 1899 | try { |
| 1900 | return mInner.accept(options); |
| 1901 | } catch (RemoteException e) { |
| 1902 | throw e.rethrowFromSystemServer(); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | /** |
Makoto Onuki | a37ac3d | 2017-04-14 12:33:10 -0700 | [diff] [blame] | 1907 | * Called by the receiving launcher app when the user accepts the request, with no options. |
| 1908 | * |
| 1909 | * @return {@code TRUE} if the shortcut or the AppWidget has actually been pinned. |
| 1910 | * {@code FALSE} if the item hasn't been pinned, for example, because the request had |
| 1911 | * 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] | 1912 | */ |
| 1913 | public boolean accept() { |
| 1914 | return accept(/* options= */ null); |
| 1915 | } |
| 1916 | |
| 1917 | private PinItemRequest(Parcel source) { |
| 1918 | final ClassLoader cl = getClass().getClassLoader(); |
| 1919 | |
| 1920 | mRequestType = source.readInt(); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1921 | mInner = IPinItemRequest.Stub.asInterface(source.readStrongBinder()); |
| 1922 | } |
| 1923 | |
| 1924 | @Override |
| 1925 | public void writeToParcel(Parcel dest, int flags) { |
| 1926 | dest.writeInt(mRequestType); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1927 | dest.writeStrongBinder(mInner.asBinder()); |
| 1928 | } |
| 1929 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 1930 | public static final @android.annotation.NonNull Creator<PinItemRequest> CREATOR = |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1931 | new Creator<PinItemRequest>() { |
| 1932 | public PinItemRequest createFromParcel(Parcel source) { |
| 1933 | return new PinItemRequest(source); |
| 1934 | } |
| 1935 | public PinItemRequest[] newArray(int size) { |
| 1936 | return new PinItemRequest[size]; |
| 1937 | } |
| 1938 | }; |
| 1939 | |
| 1940 | @Override |
| 1941 | public int describeContents() { |
| 1942 | return 0; |
| 1943 | } |
| 1944 | } |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1945 | |
| 1946 | /** |
| 1947 | * A class that encapsulates information about the usage limit set for an app or |
| 1948 | * a group of apps. |
| 1949 | * |
Varun Shah | 9c6f72b | 2019-01-25 21:13:56 -0800 | [diff] [blame] | 1950 | * <p>The launcher can query specifics about the usage limit such as how much usage time |
| 1951 | * the limit has and how much of the total usage time is remaining via the APIs available |
| 1952 | * in this class. |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1953 | * |
| 1954 | * @see #getAppUsageLimit(String, UserHandle) |
Sunny Goyal | beee197 | 2019-03-29 11:38:16 -0700 | [diff] [blame] | 1955 | * @hide |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1956 | */ |
Sunny Goyal | beee197 | 2019-03-29 11:38:16 -0700 | [diff] [blame] | 1957 | @SystemApi |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1958 | public static final class AppUsageLimit implements Parcelable { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1959 | private final long mTotalUsageLimit; |
| 1960 | private final long mUsageRemaining; |
| 1961 | |
| 1962 | /** @hide */ |
Varun Shah | 9c6f72b | 2019-01-25 21:13:56 -0800 | [diff] [blame] | 1963 | public AppUsageLimit(long totalUsageLimit, long usageRemaining) { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1964 | this.mTotalUsageLimit = totalUsageLimit; |
| 1965 | this.mUsageRemaining = usageRemaining; |
| 1966 | } |
| 1967 | |
| 1968 | /** |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1969 | * Returns the total usage limit in milliseconds set for an app or a group of apps. |
| 1970 | * |
| 1971 | * @return the total usage limit in milliseconds |
| 1972 | */ |
| 1973 | public long getTotalUsageLimit() { |
| 1974 | return mTotalUsageLimit; |
| 1975 | } |
| 1976 | |
| 1977 | /** |
| 1978 | * Returns the usage remaining in milliseconds for an app or the group of apps |
| 1979 | * this limit refers to. |
| 1980 | * |
| 1981 | * @return the usage remaining in milliseconds |
| 1982 | */ |
| 1983 | public long getUsageRemaining() { |
| 1984 | return mUsageRemaining; |
| 1985 | } |
| 1986 | |
| 1987 | private AppUsageLimit(Parcel source) { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1988 | mTotalUsageLimit = source.readLong(); |
| 1989 | mUsageRemaining = source.readLong(); |
| 1990 | } |
| 1991 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 1992 | public static final @android.annotation.NonNull Creator<AppUsageLimit> CREATOR = new Creator<AppUsageLimit>() { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 1993 | @Override |
| 1994 | public AppUsageLimit createFromParcel(Parcel source) { |
| 1995 | return new AppUsageLimit(source); |
| 1996 | } |
| 1997 | |
| 1998 | @Override |
| 1999 | public AppUsageLimit[] newArray(int size) { |
| 2000 | return new AppUsageLimit[size]; |
| 2001 | } |
| 2002 | }; |
| 2003 | |
| 2004 | @Override |
| 2005 | public int describeContents() { |
| 2006 | return 0; |
| 2007 | } |
| 2008 | |
| 2009 | @Override |
| 2010 | public void writeToParcel(Parcel dest, int flags) { |
Varun Shah | 2546cef | 2019-01-11 15:50:54 -0800 | [diff] [blame] | 2011 | dest.writeLong(mTotalUsageLimit); |
| 2012 | dest.writeLong(mUsageRemaining); |
| 2013 | } |
| 2014 | } |
Amith Yamasani | 4f58263 | 2014-02-19 14:31:52 -0800 | [diff] [blame] | 2015 | } |