Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package android.content.pm; |
| 17 | |
| 18 | import android.annotation.NonNull; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 20 | import android.annotation.SystemService; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Mathew Inwood | 1c77a11 | 2018-08-14 14:06:26 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 23 | import android.annotation.UserIdInt; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 24 | import android.app.usage.UsageStatsManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 25 | import android.content.Context; |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 26 | import android.content.Intent; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 27 | import android.content.IntentSender; |
Makoto Onuki | 9aeff8a | 2017-05-30 16:50:20 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.AdaptiveIconDrawable; |
Mathew Inwood | 45d2c25 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 29 | import android.os.Build; |
Makoto Onuki | 16e83d0 | 2017-04-17 10:55:21 -0700 | [diff] [blame] | 30 | import android.os.Build.VERSION_CODES; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 31 | import android.os.RemoteException; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 32 | import android.os.ServiceManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 33 | |
| 34 | import com.android.internal.annotations.VisibleForTesting; |
| 35 | |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 39 | * <p><code>ShortcutManager</code> executes operations on an app's set of <i>shortcuts</i>, which |
| 40 | * represent specific tasks and actions that users can perform within your app. This page lists |
| 41 | * components of the <code>ShortcutManager</code> class that you can use to create and manage |
| 42 | * sets of shortcuts. |
Kevin Hufnagle | b45780b | 2017-12-01 16:01:36 -0800 | [diff] [blame] | 43 | * |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 44 | * <p>To learn about methods that retrieve information about a single shortcut—including |
| 45 | * identifiers, type, and status—read the <code> |
| 46 | * <a href="/reference/android/content/pm/ShortcutInfo.html">ShortcutInfo</a></code> reference. |
Kevin Hufnagle | b45780b | 2017-12-01 16:01:36 -0800 | [diff] [blame] | 47 | * |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 48 | * <p>For guidance about using shortcuts, see |
| 49 | * <a href="/guide/topics/ui/shortcuts/index.html">App shortcuts</a>. |
Kevin Hufnagle | b45780b | 2017-12-01 16:01:36 -0800 | [diff] [blame] | 50 | * |
| 51 | * <h3>Retrieving class instances</h3> |
| 52 | * <!-- Provides a heading for the content filled in by the @SystemService annotation below --> |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 53 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 54 | @SystemService(Context.SHORTCUT_SERVICE) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 55 | public class ShortcutManager { |
| 56 | private static final String TAG = "ShortcutManager"; |
| 57 | |
| 58 | private final Context mContext; |
Mathew Inwood | 45d2c25 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 59 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 60 | private final IShortcutService mService; |
| 61 | |
| 62 | /** |
| 63 | * @hide |
| 64 | */ |
| 65 | public ShortcutManager(Context context, IShortcutService service) { |
| 66 | mContext = context; |
| 67 | mService = service; |
| 68 | } |
| 69 | |
| 70 | /** |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 71 | * @hide |
| 72 | */ |
| 73 | @TestApi |
| 74 | public ShortcutManager(Context context) { |
| 75 | this(context, IShortcutService.Stub.asInterface( |
| 76 | ServiceManager.getService(Context.SHORTCUT_SERVICE))); |
| 77 | } |
| 78 | |
| 79 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 80 | * Publish the list of shortcuts. All existing dynamic shortcuts from the caller app |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 81 | * will be replaced. If there are already pinned shortcuts with the same IDs, |
| 82 | * the mutable pinned shortcuts are updated. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 83 | * |
| 84 | * <p>This API will be rate-limited. |
| 85 | * |
| 86 | * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited. |
| 87 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 88 | * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded, |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 89 | * or when trying to update immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 90 | * |
| 91 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 92 | */ |
| 93 | public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) { |
| 94 | try { |
| 95 | return mService.setDynamicShortcuts(mContext.getPackageName(), |
| 96 | new ParceledListSlice(shortcutInfoList), injectMyUserId()); |
| 97 | } catch (RemoteException e) { |
| 98 | throw e.rethrowFromSystemServer(); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 103 | * Return all dynamic shortcuts from the caller app. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 104 | * |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 105 | * <p>This API is intended to be used for examining what shortcuts are currently published. |
| 106 | * Re-publishing returned {@link ShortcutInfo}s via APIs such as |
| 107 | * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons. |
| 108 | * |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 109 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 110 | */ |
| 111 | @NonNull |
| 112 | public List<ShortcutInfo> getDynamicShortcuts() { |
| 113 | try { |
| 114 | return mService.getDynamicShortcuts(mContext.getPackageName(), injectMyUserId()) |
| 115 | .getList(); |
| 116 | } catch (RemoteException e) { |
| 117 | throw e.rethrowFromSystemServer(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 122 | * Return all static (manifest) shortcuts from the caller app. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 123 | * |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 124 | * <p>This API is intended to be used for examining what shortcuts are currently published. |
| 125 | * Re-publishing returned {@link ShortcutInfo}s via APIs such as |
| 126 | * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons. |
| 127 | * |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 128 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 129 | */ |
| 130 | @NonNull |
| 131 | public List<ShortcutInfo> getManifestShortcuts() { |
| 132 | try { |
| 133 | return mService.getManifestShortcuts(mContext.getPackageName(), injectMyUserId()) |
| 134 | .getList(); |
| 135 | } catch (RemoteException e) { |
| 136 | throw e.rethrowFromSystemServer(); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 141 | * Publish the list of dynamic shortcuts. If there are already dynamic or pinned shortcuts with |
| 142 | * the same IDs, each mutable shortcut is updated. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 143 | * |
| 144 | * <p>This API will be rate-limited. |
| 145 | * |
| 146 | * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited. |
| 147 | * |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 148 | * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded, |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 149 | * or when trying to update immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 150 | * |
| 151 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 152 | */ |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 153 | public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 154 | try { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 155 | return mService.addDynamicShortcuts(mContext.getPackageName(), |
| 156 | new ParceledListSlice(shortcutInfoList), injectMyUserId()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 157 | } catch (RemoteException e) { |
| 158 | throw e.rethrowFromSystemServer(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 163 | * Delete dynamic shortcuts by ID. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 164 | * |
| 165 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 166 | */ |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 167 | public void removeDynamicShortcuts(@NonNull List<String> shortcutIds) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 168 | try { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 169 | mService.removeDynamicShortcuts(mContext.getPackageName(), shortcutIds, |
| 170 | injectMyUserId()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 171 | } catch (RemoteException e) { |
| 172 | throw e.rethrowFromSystemServer(); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 177 | * Delete all dynamic shortcuts from the caller app. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 178 | * |
| 179 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 180 | */ |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 181 | public void removeAllDynamicShortcuts() { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 182 | try { |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 183 | mService.removeAllDynamicShortcuts(mContext.getPackageName(), injectMyUserId()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 184 | } catch (RemoteException e) { |
| 185 | throw e.rethrowFromSystemServer(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 190 | * Return all pinned shortcuts from the caller app. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 191 | * |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 192 | * <p>This API is intended to be used for examining what shortcuts are currently published. |
| 193 | * Re-publishing returned {@link ShortcutInfo}s via APIs such as |
| 194 | * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons. |
| 195 | * |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 196 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 197 | */ |
| 198 | @NonNull |
| 199 | public List<ShortcutInfo> getPinnedShortcuts() { |
| 200 | try { |
| 201 | return mService.getPinnedShortcuts(mContext.getPackageName(), injectMyUserId()) |
| 202 | .getList(); |
| 203 | } catch (RemoteException e) { |
| 204 | throw e.rethrowFromSystemServer(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 209 | * Update all existing shortcuts with the same IDs. Target shortcuts may be pinned and/or |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 210 | * dynamic, but they must not be immutable. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 211 | * |
| 212 | * <p>This API will be rate-limited. |
| 213 | * |
| 214 | * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited. |
Makoto Onuki | 4a91096 | 2016-07-07 13:57:34 -0700 | [diff] [blame] | 215 | * |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 216 | * @throws IllegalArgumentException If trying to update immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 217 | * |
| 218 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 219 | */ |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 220 | public boolean updateShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 221 | try { |
| 222 | return mService.updateShortcuts(mContext.getPackageName(), |
| 223 | new ParceledListSlice(shortcutInfoList), injectMyUserId()); |
| 224 | } catch (RemoteException e) { |
| 225 | throw e.rethrowFromSystemServer(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 230 | * Disable pinned shortcuts. For more details, read |
| 231 | * <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#disable-shortcuts"> |
| 232 | * Disable shortcuts</a>. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 233 | * |
| 234 | * @throws IllegalArgumentException If trying to disable immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 235 | * |
| 236 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 237 | */ |
| 238 | public void disableShortcuts(@NonNull List<String> shortcutIds) { |
| 239 | try { |
| 240 | mService.disableShortcuts(mContext.getPackageName(), shortcutIds, |
| 241 | /* disabledMessage =*/ null, /* disabledMessageResId =*/ 0, |
| 242 | injectMyUserId()); |
| 243 | } catch (RemoteException e) { |
| 244 | throw e.rethrowFromSystemServer(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /** |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 249 | * @hide old signature, kept for unit testing. |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 250 | */ |
| 251 | public void disableShortcuts(@NonNull List<String> shortcutIds, int disabledMessageResId) { |
| 252 | try { |
| 253 | mService.disableShortcuts(mContext.getPackageName(), shortcutIds, |
| 254 | /* disabledMessage =*/ null, disabledMessageResId, |
| 255 | injectMyUserId()); |
| 256 | } catch (RemoteException e) { |
| 257 | throw e.rethrowFromSystemServer(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 262 | * @hide old signature, kept for unit testing. |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 263 | */ |
| 264 | public void disableShortcuts(@NonNull List<String> shortcutIds, String disabledMessage) { |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 265 | disableShortcuts(shortcutIds, (CharSequence) disabledMessage); |
| 266 | } |
| 267 | |
| 268 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 269 | * Disable pinned shortcuts, showing the user a custom error message when they try to select |
| 270 | * the disabled shortcuts. |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 271 | * For more details, read |
| 272 | * <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#disable-shortcuts"> |
| 273 | * Disable shortcuts</a>. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 274 | * |
| 275 | * @throws IllegalArgumentException If trying to disable immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 276 | * |
| 277 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 278 | */ |
| 279 | public void disableShortcuts(@NonNull List<String> shortcutIds, CharSequence disabledMessage) { |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 280 | try { |
| 281 | mService.disableShortcuts(mContext.getPackageName(), shortcutIds, |
| 282 | disabledMessage, /* disabledMessageResId =*/ 0, |
| 283 | injectMyUserId()); |
| 284 | } catch (RemoteException e) { |
| 285 | throw e.rethrowFromSystemServer(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 290 | * Re-enable pinned shortcuts that were previously disabled. If the target shortcuts |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 291 | * are already enabled, this method does nothing. |
Makoto Onuki | fe9c966 | 2016-07-25 15:12:23 -0700 | [diff] [blame] | 292 | * |
| 293 | * @throws IllegalArgumentException If trying to enable immutable shortcuts. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 294 | * |
| 295 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 296 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 297 | public void enableShortcuts(@NonNull List<String> shortcutIds) { |
| 298 | try { |
| 299 | mService.enableShortcuts(mContext.getPackageName(), shortcutIds, injectMyUserId()); |
| 300 | } catch (RemoteException e) { |
| 301 | throw e.rethrowFromSystemServer(); |
| 302 | } |
| 303 | } |
| 304 | |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 305 | |
| 306 | /** |
| 307 | * @hide old signature, kept for unit testing. |
| 308 | */ |
| 309 | public int getMaxShortcutCountForActivity() { |
| 310 | return getMaxShortcutCountPerActivity(); |
| 311 | } |
| 312 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 313 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 314 | * Return the maximum number of static and dynamic shortcuts that each launcher icon |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 315 | * can have at a time. |
| 316 | */ |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 317 | public int getMaxShortcutCountPerActivity() { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 318 | try { |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 319 | return mService.getMaxShortcutCountPerActivity( |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 320 | mContext.getPackageName(), injectMyUserId()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 321 | } catch (RemoteException e) { |
| 322 | throw e.rethrowFromSystemServer(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 327 | * Return the number of times the caller app can call the rate-limited APIs |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 328 | * before the rate limit counter is reset. |
| 329 | * |
| 330 | * @see #getRateLimitResetTime() |
Makoto Onuki | f805b43 | 2016-07-07 11:51:52 -0700 | [diff] [blame] | 331 | * |
| 332 | * @hide |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 333 | */ |
| 334 | public int getRemainingCallCount() { |
| 335 | try { |
| 336 | return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId()); |
| 337 | } catch (RemoteException e) { |
| 338 | throw e.rethrowFromSystemServer(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Return when the rate limit count will be reset next time, in milliseconds since the epoch. |
| 344 | * |
| 345 | * @see #getRemainingCallCount() |
| 346 | * @see System#currentTimeMillis() |
Makoto Onuki | f805b43 | 2016-07-07 11:51:52 -0700 | [diff] [blame] | 347 | * |
| 348 | * @hide |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 349 | */ |
| 350 | public long getRateLimitResetTime() { |
| 351 | try { |
| 352 | return mService.getRateLimitResetTime(mContext.getPackageName(), injectMyUserId()); |
| 353 | } catch (RemoteException e) { |
| 354 | throw e.rethrowFromSystemServer(); |
| 355 | } |
| 356 | } |
| 357 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 358 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 359 | * Return {@code true} when rate-limiting is active for the caller app. |
Makoto Onuki | 347a6bd | 2016-07-19 11:13:08 -0700 | [diff] [blame] | 360 | * |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 361 | * <p>For details, see <a href="/guide/topics/ui/shortcuts/managing-shortcuts#rate-limiting"> |
| 362 | * Rate limiting</a>. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 363 | * |
| 364 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | 347a6bd | 2016-07-19 11:13:08 -0700 | [diff] [blame] | 365 | */ |
| 366 | public boolean isRateLimitingActive() { |
| 367 | try { |
| 368 | return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId()) |
| 369 | == 0; |
| 370 | } catch (RemoteException e) { |
| 371 | throw e.rethrowFromSystemServer(); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /** |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 376 | * Return the max width for icons, in pixels. |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 377 | * |
| 378 | * <p> Note that this method returns max width of icon's visible part. Hence, it does not take |
| 379 | * into account the inset introduced by {@link AdaptiveIconDrawable}. To calculate bitmap image |
Makoto Onuki | 9aeff8a | 2017-05-30 16:50:20 -0700 | [diff] [blame] | 380 | * to function as {@link AdaptiveIconDrawable}, multiply |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 381 | * 1 + 2 * {@link AdaptiveIconDrawable#getExtraInsetFraction()} to the returned size. |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 382 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 383 | public int getIconMaxWidth() { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 384 | try { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 385 | // TODO Implement it properly using xdpi. |
| 386 | return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId()); |
| 387 | } catch (RemoteException e) { |
| 388 | throw e.rethrowFromSystemServer(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Return the max height for icons, in pixels. |
| 394 | */ |
| 395 | public int getIconMaxHeight() { |
| 396 | try { |
| 397 | // TODO Implement it properly using ydpi. |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 398 | return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId()); |
| 399 | } catch (RemoteException e) { |
| 400 | throw e.rethrowFromSystemServer(); |
| 401 | } |
| 402 | } |
| 403 | |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 404 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 405 | * Apps that publish shortcuts should call this method whenever the user |
| 406 | * selects the shortcut containing the given ID or when the user completes |
| 407 | * an action in the app that is equivalent to selecting the shortcut. |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 408 | * For more details, read about |
| 409 | * <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#track-usage"> |
| 410 | * tracking shortcut usage</a>. |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 411 | * |
| 412 | * <p>The information is accessible via {@link UsageStatsManager#queryEvents} |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 413 | * Typically, launcher apps use this information to build a prediction model |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 414 | * so that they can promote the shortcuts that are likely to be used at the moment. |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 415 | * |
| 416 | * @throws IllegalStateException when the user is locked. |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 417 | */ |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 418 | public void reportShortcutUsed(String shortcutId) { |
| 419 | try { |
| 420 | mService.reportShortcutUsed(mContext.getPackageName(), shortcutId, |
| 421 | injectMyUserId()); |
| 422 | } catch (RemoteException e) { |
| 423 | throw e.rethrowFromSystemServer(); |
| 424 | } |
| 425 | } |
| 426 | |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 427 | /** |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 428 | * Return {@code TRUE} if the app is running on a device whose default launcher supports |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 429 | * {@link #requestPinShortcut(ShortcutInfo, IntentSender)}. |
Makoto Onuki | 16e83d0 | 2017-04-17 10:55:21 -0700 | [diff] [blame] | 430 | * |
Makoto Onuki | 9aeff8a | 2017-05-30 16:50:20 -0700 | [diff] [blame] | 431 | * <p>The return value may change in subsequent calls if the user changes the default launcher |
| 432 | * app. |
| 433 | * |
| 434 | * <p><b>Note:</b> See also the support library counterpart |
| 435 | * {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported( |
| 436 | * Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the |
| 437 | * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}. |
| 438 | * |
| 439 | * @see #requestPinShortcut(ShortcutInfo, IntentSender) |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 440 | */ |
| 441 | public boolean isRequestPinShortcutSupported() { |
| 442 | try { |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 443 | return mService.isRequestPinItemSupported(injectMyUserId(), |
| 444 | LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 445 | } catch (RemoteException e) { |
| 446 | throw e.rethrowFromSystemServer(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Request to create a pinned shortcut. The default launcher will receive this request and |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 452 | * ask the user for approval. If the user approves it, the shortcut will be created, and |
| 453 | * {@code resultIntent} will be sent. If a request is denied by the user, however, no response |
| 454 | * will be sent to the caller. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 455 | * |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 456 | * <p>Only apps with a foreground activity or a foreground service can call this method. |
| 457 | * Otherwise, it'll throw {@link IllegalStateException}. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 458 | * |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 459 | * <p>It's up to the launcher to decide how to handle previous pending requests when the same |
| 460 | * package calls this API multiple times in a row. One possible strategy is to ignore any |
| 461 | * previous requests. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 462 | * |
Makoto Onuki | 9aeff8a | 2017-05-30 16:50:20 -0700 | [diff] [blame] | 463 | * <p><b>Note:</b> See also the support library counterpart |
| 464 | * {@link android.support.v4.content.pm.ShortcutManagerCompat#requestPinShortcut( |
| 465 | * Context, ShortcutInfoCompat, IntentSender)}, |
| 466 | * which supports Android versions lower than {@link VERSION_CODES#O} using the |
| 467 | * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}. |
| 468 | * |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 469 | * @param shortcut Shortcut to pin. If an app wants to pin an existing (either static |
| 470 | * or dynamic) shortcut, then it only needs to have an ID. Although other fields don't have |
| 471 | * to be set, the target shortcut must be enabled. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 472 | * |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 473 | * <p>If it's a new shortcut, all the mandatory fields, such as a short label, must be |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 474 | * set. |
| 475 | * @param resultIntent If not null, this intent will be sent when the shortcut is pinned. |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 476 | * Use {@link android.app.PendingIntent#getIntentSender()} to create an {@link IntentSender}. |
Makoto Onuki | 16e83d0 | 2017-04-17 10:55:21 -0700 | [diff] [blame] | 477 | * To avoid background execution limits, use an unexported, manifest-declared receiver. |
Laura Davis | b93f8dd | 2018-08-07 09:53:52 -0700 | [diff] [blame] | 478 | * For more details, see |
| 479 | * <a href="/guide/topics/ui/shortcuts/creating-shortcuts.html#pinned"> |
| 480 | * Creating pinned shortcuts</a>. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 481 | * |
| 482 | * @return {@code TRUE} if the launcher supports this feature. Note the API will return without |
| 483 | * waiting for the user to respond, so getting {@code TRUE} from this API does *not* mean |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 484 | * the shortcut was pinned successfully. {@code FALSE} if the launcher doesn't support this |
| 485 | * feature. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 486 | * |
| 487 | * @see #isRequestPinShortcutSupported() |
| 488 | * @see IntentSender |
| 489 | * @see android.app.PendingIntent#getIntentSender() |
| 490 | * |
| 491 | * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled. |
| 492 | * @throws IllegalStateException The caller doesn't have a foreground activity or a foreground |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 493 | * service, or the device is locked. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 494 | */ |
| 495 | public boolean requestPinShortcut(@NonNull ShortcutInfo shortcut, |
| 496 | @Nullable IntentSender resultIntent) { |
| 497 | try { |
| 498 | return mService.requestPinShortcut(mContext.getPackageName(), shortcut, |
| 499 | resultIntent, injectMyUserId()); |
| 500 | } catch (RemoteException e) { |
| 501 | throw e.rethrowFromSystemServer(); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /** |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 506 | * Returns an Intent which can be used by the default launcher to pin a shortcut containing the |
| 507 | * given {@link ShortcutInfo}. This method should be used by an Activity to set a result in |
| 508 | * response to {@link Intent#ACTION_CREATE_SHORTCUT}. |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 509 | * |
| 510 | * @param shortcut New shortcut to pin. If an app wants to pin an existing (either dynamic |
| 511 | * or manifest) shortcut, then it only needs to have an ID, and other fields don't have to |
| 512 | * be set, in which case, the target shortcut must be enabled. |
| 513 | * If it's a new shortcut, all the mandatory fields, such as a short label, must be |
| 514 | * set. |
Kevin Hufnagle | 652438c | 2017-03-17 18:11:20 -0700 | [diff] [blame] | 515 | * @return The intent that should be set as the result for the calling activity, or |
| 516 | * <code>null</code> if the current launcher doesn't support shortcuts. |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 517 | * |
| 518 | * @see Intent#ACTION_CREATE_SHORTCUT |
| 519 | * |
| 520 | * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled. |
| 521 | */ |
| 522 | public Intent createShortcutResultIntent(@NonNull ShortcutInfo shortcut) { |
| 523 | try { |
| 524 | return mService.createShortcutResultIntent(mContext.getPackageName(), shortcut, |
| 525 | injectMyUserId()); |
| 526 | } catch (RemoteException e) { |
| 527 | throw e.rethrowFromSystemServer(); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /** |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 532 | * Called internally when an app is considered to have come to the foreground |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 533 | * even when technically it's not. This method resets the throttling for this package. |
Kevin Hufnagle | 68d699d | 2016-10-14 19:04:51 -0700 | [diff] [blame] | 534 | * For example, when the user sends an "inline reply" on a notification, the system UI will |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 535 | * call it. |
| 536 | * |
| 537 | * @hide |
| 538 | */ |
| 539 | public void onApplicationActive(@NonNull String packageName, @UserIdInt int userId) { |
| 540 | try { |
| 541 | mService.onApplicationActive(packageName, userId); |
| 542 | } catch (RemoteException e) { |
| 543 | throw e.rethrowFromSystemServer(); |
| 544 | } |
| 545 | } |
| 546 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 547 | /** @hide injection point */ |
| 548 | @VisibleForTesting |
| 549 | protected int injectMyUserId() { |
Jeff Sharkey | ad357d1 | 2018-02-02 13:25:31 -0700 | [diff] [blame] | 550 | return mContext.getUserId(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 551 | } |
| 552 | } |