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;
19 import android.annotation.Nullable;
20 import android.annotation.SystemService;
21 import android.annotation.TestApi;
22 import android.annotation.UserIdInt;
23 import android.app.Activity;
24 import android.app.usage.UsageStatsManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentSender;
28 import android.graphics.drawable.AdaptiveIconDrawable;
29 import android.os.Build.VERSION_CODES;
30 import android.os.RemoteException;
31 import android.os.ServiceManager;
32 import android.os.UserHandle;
33 
34 import com.android.internal.annotations.VisibleForTesting;
35 
36 import java.util.List;
37 
38 /**
39  * The ShortcutManager performs operations on an app's set of <em>shortcuts</em>. The
40  * {@link ShortcutInfo} class contains information about each of the shortcuts themselves.
41  *
42  * <p>An app's shortcuts represent specific tasks and actions that users can perform within your
43  * app. When a user selects a shortcut in the currently-active launcher, your app opens an activity
44  * other than the app's starting activity, provided that the currently-active launcher supports app
45  * shortcuts.</p>
46  *
47  * <p>The types of shortcuts that you create for your app depend on the app's key use cases. For
48  * example, an email app may publish the "compose new email" shortcut, which allows the app to
49  * directly open the compose activity.</p>
50  *
51  * <p class="note"><b>Note:</b> Only main activities&mdash;activities that handle the
52  * {@link Intent#ACTION_MAIN} action and the {@link Intent#CATEGORY_LAUNCHER} category&mdash;can
53  * have shortcuts. If an app has multiple main activities, you need to define the set of shortcuts
54  * for <em>each</em> activity.
55  *
56  * <p>This page discusses the implementation details of the <code>ShortcutManager</code> class. For
57  * definitions of key terms and guidance on performing operations on shortcuts within your app, see
58  * the <a href="/guide/topics/ui/shortcuts.html">App Shortcuts</a> feature guide.
59  *
60  * <h3>Shortcut characteristics</h3>
61  *
62  * This section describes in-depth details about each shortcut type's usage and availability.
63  *
64  * <p class="note"><b>Important security note:</b> All shortcut information is stored in
65  * <a href="/training/articles/direct-boot.html">credential encrypted storage</a>, so your app
66  * cannot access a user's shortcuts until after they've unlocked the device.
67  *
68  * <h4>Static and dynamic shortcuts</h4>
69  *
70  * <p>Static shortcuts and dynamic shortcuts are shown in a supported launcher when the user
71  * performs a specific gesture. On currently-supported launchers, the gesture is a long-press on the
72  * app's launcher icon, but the actual gesture may be different on other launcher apps.
73  *
74  * <p>The {@link LauncherApps} class provides APIs for launcher apps to access shortcuts.
75  *
76  * <h4>Pinned shortcuts</h4>
77  *
78  * <p>Because pinned shortcuts appear in the launcher itself, they're always visible. A pinned
79  * shortcut is removed from the launcher only in the following situations:
80  * <ul>
81  *     <li>The user removes it.
82  *     <li>The publisher app associated with the shortcut is uninstalled.
83  *     <li>The user selects <b>Clear data</b> from the publisher app's <i>Storage</i> screen, within
84  *     the system's <b>Settings</b> app.
85  * </ul>
86  *
87  * <p>Because the system performs
88  * <a href="/guide/topics/ui/shortcuts.html#backup-and-restore">backup and restore</a> on pinned
89  * shortcuts automatically, these shortcuts' IDs should contain either stable, constant strings or
90  * server-side identifiers, rather than identifiers generated locally that might not make sense on
91  * other devices.
92  *
93  * <h3>Shortcut display order</h3>
94  *
95  * <p>When the launcher displays an app's shortcuts, they should appear in the following order:
96  *
97  * <ol>
98  *   <li><b>Static shortcuts:</b> Shortcuts whose {@link ShortcutInfo#isDeclaredInManifest()} method
99  *   returns {@code true}.</li>
100  *   <li><b>Dynamic shortcuts:</b> Shortcuts whose {@link ShortcutInfo#isDynamic()} method returns
101  *   {@code true}.</li>
102  * </ol>
103  *
104  * <p>Within each shortcut type (static and dynamic), shortcuts are sorted in order of increasing
105  * rank according to {@link ShortcutInfo#getRank()}.</p>
106  *
107  * <h4>Shortcut ranks</h4>
108  *
109  * <p>Shortcut ranks are non-negative, sequential integers that determine the order in which
110  * shortcuts appear, assuming that the shortcuts are all in the same category. You can update ranks
111  * of existing shortcuts when you call {@link #updateShortcuts(List)},
112  * {@link #addDynamicShortcuts(List)}, or {@link #setDynamicShortcuts(List)}.
113  *
114  * <p class="note"><b>Note:</b> Ranks are auto-adjusted so that they're unique for each type of
115  * shortcut (static or dynamic). For example, if there are 3 dynamic shortcuts with ranks 0, 1 and
116  * 2, adding another dynamic shortcut with a rank of 1 represents a request to place this shortcut
117  * at the second position. In response, the third and fourth shortcuts move closer to the bottom of
118  * the shortcut list, with their ranks changing to 2 and 3, respectively.
119  *
120  * <h3>Options for static shortcuts</h3>
121  *
122  * The following list includes descriptions for the different attributes within a static shortcut.
123  * You must provide a value for {@code android:shortcutId} and {@code android:shortcutShortLabel};
124  * all other values are optional.
125  *
126  * <dl>
127  *   <dt>{@code android:shortcutId}</dt>
128  *   <dd><p>A string literal, which represents the shortcut when a {@code ShortcutManager} object
129  *   performs operations on it.</p>
130  *   <p class="note"><b>Note: </b>You cannot set this attribute's value to a resource string, such
131  *   as <code>@string/foo</code>.</p>
132  *   </dd>
133  *
134  *   <dt>{@code android:enabled}</dt>
135  *   <dd><p>Whether the user can interact with the shortcut from a supported launcher.</p>
136  *   <p>The default value is {@code true}. If you set it to {@code false}, you should also set
137  *   {@code android:shortcutDisabledMessage} to a message that explains why you've disabled the
138  *   shortcut. If you don't think you need to provide such a message, it's easiest to just remove
139  *   the shortcut from the XML file entirely, rather than changing the values of the shortcut's
140  *   {@code android:enabled} and {@code android:shortcutDisabledMessage} attributes.
141  *   </dd>
142  *
143  *   <dt>{@code android:icon}</dt>
144  *   <dd><p>The <a href="/topic/performance/graphics/index.html">bitmap</a> or
145  *   <a href="/guide/practices/ui_guidelines/icon_design_adaptive.html">adaptive icon</a> that the
146  *   launcher uses when displaying the shortcut to the user. This value can be either the path to an
147  *   image or the resource file that contains the image. Use adaptive icons whenever possible to
148  *   improve performance and consistency.</p>
149  *   <p class="note"><b>Note: </b>Shortcut icons cannot include
150  *   <a href="/training/material/drawables.html#DrawableTint">tints</a>.
151  *   </dd>
152  *
153  *   <dt>{@code android:shortcutShortLabel}</dt>
154  *   <dd><p>A concise phrase that describes the shortcut's purpose. For more information, see
155  *   {@link ShortcutInfo.Builder#setShortLabel(CharSequence)}.</p>
156  *   <p class="note"><b>Note: </b>This attribute's value must be a resource string, such as
157  *   <code>@string/shortcut_short_label</code>.</p>
158  *   </dd>
159  *
160  *   <dt>{@code android:shortcutLongLabel}</dt>
161  *   <dd><p>An extended phrase that describes the shortcut's purpose. If there's enough space, the
162  *   launcher displays this value instead of {@code android:shortcutShortLabel}. For more
163  *   information, see {@link ShortcutInfo.Builder#setLongLabel(CharSequence)}.</p>
164  *   <p class="note"><b>Note: </b>This attribute's value must be a resource string, such as
165  *   <code>@string/shortcut_long_label</code>.</p>
166  *   </dd>
167  *
168  *   <dt>{@code android:shortcutDisabledMessage}</dt>
169  *   <dd><p>The message that appears in a supported launcher when the user attempts to launch a
170  *   disabled shortcut. The message should explain to the user why the shortcut is now disabled.
171  *   This attribute's value has no effect if {@code android:enabled} is {@code true}.</p>
172  *   <p class="note"><b>Note: </b>This attribute's value must be a resource string, such as
173  *   <code>@string/shortcut_disabled_message</code>.</p>
174  *   </dd>
175  * </dl>
176  *
177  * <h3>Inner elements that define static shortcuts</h3>
178  *
179  * <p>The XML file that lists an app's static shortcuts supports the following elements inside each
180  * {@code <shortcut>} element. You must include an {@code intent} inner element for each
181  * static shortcut that you define.</p>
182  *
183  * <dl>
184  *   <dt>{@code intent}</dt>
185  *   <dd><p>The action that the system launches when the user selects the shortcut. This intent must
186  *   provide a value for the {@code android:action} attribute.</p>
187  *   <p>You can provide multiple intents for a single shortcut. If you do so, the last defined
188  *   activity is launched, and the other activities are placed in the
189  *   <a href="/guide/components/tasks-and-back-stack.html">back stack</a>. See
190  *   <a href="/guide/topics/ui/shortcuts.html#static">Using Static Shortcuts</a> and the
191  *   {@link android.app.TaskStackBuilder} class reference for details.</p>
192  *   <p class="note"><b>Note:</b> This {@code intent} element cannot include string resources.</p>
193  *   <p>To learn more about how to configure intents, see
194  *   <a href="{@docRoot}guide/topics/ui/settings.html#Intents">Using intents</a>.</p>
195  *   </dd>
196  *
197  *   <dt>{@code categories}</dt>
198  *   <dd><p>Provides a grouping for the types of actions that your app's shortcuts perform, such as
199  *   creating new chat messages.</p>
200  *   <p>For a list of supported shortcut categories, see the {@link ShortcutInfo} class reference
201  *   for a list of supported shortcut categories.
202  *   </dd>
203  * </dl>
204  *
205  * <h3>Updating shortcuts</h3>
206  *
207  * <p>Each app's launcher icon can contain at most {@link #getMaxShortcutCountPerActivity()} number
208  * of static and dynamic shortcuts combined. There is no limit to the number of pinned shortcuts
209  * that an app can create, though.
210  *
211  * <p>When a dynamic shortcut is pinned, even when the publisher removes it as a dynamic shortcut,
212  * the pinned shortcut is still visible and launchable.  This allows an app to have more than
213  * {@link #getMaxShortcutCountPerActivity()} number of shortcuts.
214  *
215  * <p>As an example, suppose {@link #getMaxShortcutCountPerActivity()} is 5:
216  * <ol>
217  *     <li>A chat app publishes 5 dynamic shortcuts for the 5 most recent
218  *     conversations (c1, c2, ..., c5).
219  *
220  *     <li>The user pins all 5 of the shortcuts.
221  *
222  *     <li>Later, the user has started 3 additional conversations (c6, c7, and c8), so the publisher
223  *     app re-publishes its dynamic shortcuts. The new dynamic shortcut list is: c4, c5, ..., c8.
224  *     <p>The publisher app has to remove c1, c2, and c3 because it can't have more than 5 dynamic
225  *     shortcuts. However, c1, c2, and c3 are still pinned shortcuts that the user can access and
226  *     launch.
227  *     <p>At this point, the user can access a total of 8 shortcuts that link to activities in the
228  *     publisher app, including the 3 pinned shortcuts, even though an app can have at most 5
229  *     dynamic shortcuts.
230  *
231  *     <li>The app can use {@link #updateShortcuts(List)} to update <em>any</em> of the existing
232  *     8 shortcuts, when, for example, the chat peers' icons have changed.
233  *     <p>The {@link #addDynamicShortcuts(List)} and {@link #setDynamicShortcuts(List)} methods
234  *     can also be used to update existing shortcuts with the same IDs, but they <b>cannot</b> be
235  *     used for updating non-dynamic, pinned shortcuts because these 2 methods try to convert the
236  *     given lists of shortcuts to dynamic shortcuts.
237  * </ol>
238  *
239  * <h3>Shortcut intents</h3>
240  *
241  * <p>
242  * Dynamic shortcuts can be published with any set of {@link Intent#addFlags Intent} flags.
243  * Typically, {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} is specified, possibly along with other
244  * flags; otherwise, if the app is already running, the app is simply brought to
245  * the foreground, and the target activity might not appear.
246  *
247  * <p>Static shortcuts <b>cannot</b> have custom intent flags.
248  * The first intent of a static shortcut will always have {@link Intent#FLAG_ACTIVITY_NEW_TASK}
249  * and {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} set. This means, when the app is already running, all
250  * the existing activities in your app are destroyed when a static shortcut is launched.
251  * If this behavior is not desirable, you can use a <em>trampoline activity</em>, or an invisible
252  * activity that starts another activity in {@link Activity#onCreate}, then calls
253  * {@link Activity#finish()}:
254  * <ol>
255  *     <li>In the <code>AndroidManifest.xml</code> file, the trampoline activity should include the
256  *     attribute assignment {@code android:taskAffinity=""}.
257  *     <li>In the shortcuts resource file, the intent within the static shortcut should reference
258  *     the trampoline activity.
259  * </ol>
260  *
261  * <h3>Rate limiting</h3>
262  *
263  * <p>When <a href="/guide/topics/ui/shortcuts.html#rate-limit">rate limiting</a> is active,
264  * {@link #isRateLimitingActive()} returns {@code true}.
265  *
266  * <p>Rate limiting is reset upon certain events, so even background apps can call these APIs until
267  * the rate limit is reached again. These events include the following:
268  * <ul>
269  *   <li>An app comes to the foreground.
270  *   <li>The system locale changes.
271  *   <li>The user performs the <a href="/guide/topics/ui/notifiers/notifications.html#direct">inline
272  *   reply</a> action on a notification.
273  * </ul>
274  *
275  * <h3>Handling system locale changes</h3>
276  *
277  * <p>Apps should update dynamic and pinned shortcuts when they receive the
278  * {@link Intent#ACTION_LOCALE_CHANGED} broadcast, indicating that the system locale has changed.
279  * <p>When the system locale changes, <a href="/guide/topics/ui/shortcuts.html#rate-limit">rate
280  * limiting</a> is reset, so even background apps can add and update dynamic shortcuts until the
281  * rate limit is reached again.
282  *
283  * <h3>Retrieving class instances</h3>
284  * <!-- Provides a heading for the content filled in by the @SystemService annotation below -->
285  */
286 @SystemService(Context.SHORTCUT_SERVICE)
287 public class ShortcutManager {
288     private static final String TAG = "ShortcutManager";
289 
290     private final Context mContext;
291     private final IShortcutService mService;
292 
293     /**
294      * @hide
295      */
ShortcutManager(Context context, IShortcutService service)296     public ShortcutManager(Context context, IShortcutService service) {
297         mContext = context;
298         mService = service;
299     }
300 
301     /**
302      * @hide
303      */
304     @TestApi
ShortcutManager(Context context)305     public ShortcutManager(Context context) {
306         this(context, IShortcutService.Stub.asInterface(
307                 ServiceManager.getService(Context.SHORTCUT_SERVICE)));
308     }
309 
310     /**
311      * Publish the list of shortcuts.  All existing dynamic shortcuts from the caller app
312      * will be replaced.  If there are already pinned shortcuts with the same IDs,
313      * the mutable pinned shortcuts are updated.
314      *
315      * <p>This API will be rate-limited.
316      *
317      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
318      *
319      * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
320      * or when trying to update immutable shortcuts.
321      *
322      * @throws IllegalStateException when the user is locked.
323      */
setDynamicShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)324     public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
325         try {
326             return mService.setDynamicShortcuts(mContext.getPackageName(),
327                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
328         } catch (RemoteException e) {
329             throw e.rethrowFromSystemServer();
330         }
331     }
332 
333     /**
334      * Return all dynamic shortcuts from the caller app.
335      *
336      * <p>This API is intended to be used for examining what shortcuts are currently published.
337      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
338      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
339      *
340      * @throws IllegalStateException when the user is locked.
341      */
342     @NonNull
getDynamicShortcuts()343     public List<ShortcutInfo> getDynamicShortcuts() {
344         try {
345             return mService.getDynamicShortcuts(mContext.getPackageName(), injectMyUserId())
346                     .getList();
347         } catch (RemoteException e) {
348             throw e.rethrowFromSystemServer();
349         }
350     }
351 
352     /**
353      * Return all static (manifest) shortcuts from the caller app.
354      *
355      * <p>This API is intended to be used for examining what shortcuts are currently published.
356      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
357      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
358      *
359      * @throws IllegalStateException when the user is locked.
360      */
361     @NonNull
getManifestShortcuts()362     public List<ShortcutInfo> getManifestShortcuts() {
363         try {
364             return mService.getManifestShortcuts(mContext.getPackageName(), injectMyUserId())
365                     .getList();
366         } catch (RemoteException e) {
367             throw e.rethrowFromSystemServer();
368         }
369     }
370 
371     /**
372      * Publish the list of dynamic shortcuts.  If there are already dynamic or pinned shortcuts with
373      * the same IDs, each mutable shortcut is updated.
374      *
375      * <p>This API will be rate-limited.
376      *
377      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
378      *
379      * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
380      * or when trying to update immutable shortcuts.
381      *
382      * @throws IllegalStateException when the user is locked.
383      */
addDynamicShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)384     public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
385         try {
386             return mService.addDynamicShortcuts(mContext.getPackageName(),
387                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
388         } catch (RemoteException e) {
389             throw e.rethrowFromSystemServer();
390         }
391     }
392 
393     /**
394      * Delete dynamic shortcuts by ID.
395      *
396      * @throws IllegalStateException when the user is locked.
397      */
removeDynamicShortcuts(@onNull List<String> shortcutIds)398     public void removeDynamicShortcuts(@NonNull List<String> shortcutIds) {
399         try {
400             mService.removeDynamicShortcuts(mContext.getPackageName(), shortcutIds,
401                     injectMyUserId());
402         } catch (RemoteException e) {
403             throw e.rethrowFromSystemServer();
404         }
405     }
406 
407     /**
408      * Delete all dynamic shortcuts from the caller app.
409      *
410      * @throws IllegalStateException when the user is locked.
411      */
removeAllDynamicShortcuts()412     public void removeAllDynamicShortcuts() {
413         try {
414             mService.removeAllDynamicShortcuts(mContext.getPackageName(), injectMyUserId());
415         } catch (RemoteException e) {
416             throw e.rethrowFromSystemServer();
417         }
418     }
419 
420     /**
421      * Return all pinned shortcuts from the caller app.
422      *
423      * <p>This API is intended to be used for examining what shortcuts are currently published.
424      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
425      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
426      *
427      * @throws IllegalStateException when the user is locked.
428      */
429     @NonNull
getPinnedShortcuts()430     public List<ShortcutInfo> getPinnedShortcuts() {
431         try {
432             return mService.getPinnedShortcuts(mContext.getPackageName(), injectMyUserId())
433                     .getList();
434         } catch (RemoteException e) {
435             throw e.rethrowFromSystemServer();
436         }
437     }
438 
439     /**
440      * Update all existing shortcuts with the same IDs.  Target shortcuts may be pinned and/or
441      * dynamic, but they must not be immutable.
442      *
443      * <p>This API will be rate-limited.
444      *
445      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
446      *
447      * @throws IllegalArgumentException If trying to update immutable shortcuts.
448      *
449      * @throws IllegalStateException when the user is locked.
450      */
updateShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)451     public boolean updateShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
452         try {
453             return mService.updateShortcuts(mContext.getPackageName(),
454                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
455         } catch (RemoteException e) {
456             throw e.rethrowFromSystemServer();
457         }
458     }
459 
460     /**
461      * Disable pinned shortcuts.  For more details, see the Javadoc for the {@link ShortcutManager}
462      * class.
463      *
464      * @throws IllegalArgumentException If trying to disable immutable shortcuts.
465      *
466      * @throws IllegalStateException when the user is locked.
467      */
disableShortcuts(@onNull List<String> shortcutIds)468     public void disableShortcuts(@NonNull List<String> shortcutIds) {
469         try {
470             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
471                     /* disabledMessage =*/ null, /* disabledMessageResId =*/ 0,
472                     injectMyUserId());
473         } catch (RemoteException e) {
474             throw e.rethrowFromSystemServer();
475         }
476     }
477 
478     /**
479      * @hide old signature, kept for unit testing.
480      */
disableShortcuts(@onNull List<String> shortcutIds, int disabledMessageResId)481     public void disableShortcuts(@NonNull List<String> shortcutIds, int disabledMessageResId) {
482         try {
483             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
484                     /* disabledMessage =*/ null, disabledMessageResId,
485                     injectMyUserId());
486         } catch (RemoteException e) {
487             throw e.rethrowFromSystemServer();
488         }
489     }
490 
491     /**
492      * @hide old signature, kept for unit testing.
493      */
disableShortcuts(@onNull List<String> shortcutIds, String disabledMessage)494     public void disableShortcuts(@NonNull List<String> shortcutIds, String disabledMessage) {
495         disableShortcuts(shortcutIds, (CharSequence) disabledMessage);
496     }
497 
498     /**
499      * Disable pinned shortcuts, showing the user a custom error message when they try to select
500      * the disabled shortcuts.
501      * For more details, see the Javadoc for the {@link ShortcutManager} class.
502      *
503      * @throws IllegalArgumentException If trying to disable immutable shortcuts.
504      *
505      * @throws IllegalStateException when the user is locked.
506      */
disableShortcuts(@onNull List<String> shortcutIds, CharSequence disabledMessage)507     public void disableShortcuts(@NonNull List<String> shortcutIds, CharSequence disabledMessage) {
508         try {
509             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
510                     disabledMessage, /* disabledMessageResId =*/ 0,
511                     injectMyUserId());
512         } catch (RemoteException e) {
513             throw e.rethrowFromSystemServer();
514         }
515     }
516 
517     /**
518      * Re-enable pinned shortcuts that were previously disabled.  If the target shortcuts
519      * are already enabled, this method does nothing.
520      *
521      * @throws IllegalArgumentException If trying to enable immutable shortcuts.
522      *
523      * @throws IllegalStateException when the user is locked.
524      */
enableShortcuts(@onNull List<String> shortcutIds)525     public void enableShortcuts(@NonNull List<String> shortcutIds) {
526         try {
527             mService.enableShortcuts(mContext.getPackageName(), shortcutIds, injectMyUserId());
528         } catch (RemoteException e) {
529             throw e.rethrowFromSystemServer();
530         }
531     }
532 
533 
534     /**
535      * @hide old signature, kept for unit testing.
536      */
getMaxShortcutCountForActivity()537     public int getMaxShortcutCountForActivity() {
538         return getMaxShortcutCountPerActivity();
539     }
540 
541     /**
542      * Return the maximum number of static and dynamic shortcuts that each launcher icon
543      * can have at a time.
544      */
getMaxShortcutCountPerActivity()545     public int getMaxShortcutCountPerActivity() {
546         try {
547             return mService.getMaxShortcutCountPerActivity(
548                     mContext.getPackageName(), injectMyUserId());
549         } catch (RemoteException e) {
550             throw e.rethrowFromSystemServer();
551         }
552     }
553 
554     /**
555      * Return the number of times the caller app can call the rate-limited APIs
556      * before the rate limit counter is reset.
557      *
558      * @see #getRateLimitResetTime()
559      *
560      * @hide
561      */
getRemainingCallCount()562     public int getRemainingCallCount() {
563         try {
564             return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId());
565         } catch (RemoteException e) {
566             throw e.rethrowFromSystemServer();
567         }
568     }
569 
570     /**
571      * Return when the rate limit count will be reset next time, in milliseconds since the epoch.
572      *
573      * @see #getRemainingCallCount()
574      * @see System#currentTimeMillis()
575      *
576      * @hide
577      */
getRateLimitResetTime()578     public long getRateLimitResetTime() {
579         try {
580             return mService.getRateLimitResetTime(mContext.getPackageName(), injectMyUserId());
581         } catch (RemoteException e) {
582             throw e.rethrowFromSystemServer();
583         }
584     }
585 
586     /**
587      * Return {@code true} when rate-limiting is active for the caller app.
588      *
589      * <p>See the class level javadoc for details.
590      *
591      * @throws IllegalStateException when the user is locked.
592      */
isRateLimitingActive()593     public boolean isRateLimitingActive() {
594         try {
595             return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId())
596                     == 0;
597         } catch (RemoteException e) {
598             throw e.rethrowFromSystemServer();
599         }
600     }
601 
602     /**
603      * Return the max width for icons, in pixels.
604      *
605      * <p> Note that this method returns max width of icon's visible part. Hence, it does not take
606      * into account the inset introduced by {@link AdaptiveIconDrawable}. To calculate bitmap image
607      * to function as {@link AdaptiveIconDrawable}, multiply
608      * 1 + 2 * {@link AdaptiveIconDrawable#getExtraInsetFraction()} to the returned size.
609      */
getIconMaxWidth()610     public int getIconMaxWidth() {
611         try {
612             // TODO Implement it properly using xdpi.
613             return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
614         } catch (RemoteException e) {
615             throw e.rethrowFromSystemServer();
616         }
617     }
618 
619     /**
620      * Return the max height for icons, in pixels.
621      */
getIconMaxHeight()622     public int getIconMaxHeight() {
623         try {
624             // TODO Implement it properly using ydpi.
625             return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
626         } catch (RemoteException e) {
627             throw e.rethrowFromSystemServer();
628         }
629     }
630 
631     /**
632      * Apps that publish shortcuts should call this method whenever the user
633      * selects the shortcut containing the given ID or when the user completes
634      * an action in the app that is equivalent to selecting the shortcut.
635      * For more details, see the Javadoc for the {@link ShortcutManager} class
636      *
637      * <p>The information is accessible via {@link UsageStatsManager#queryEvents}
638      * Typically, launcher apps use this information to build a prediction model
639      * so that they can promote the shortcuts that are likely to be used at the moment.
640      *
641      * @throws IllegalStateException when the user is locked.
642      */
reportShortcutUsed(String shortcutId)643     public void reportShortcutUsed(String shortcutId) {
644         try {
645             mService.reportShortcutUsed(mContext.getPackageName(), shortcutId,
646                     injectMyUserId());
647         } catch (RemoteException e) {
648             throw e.rethrowFromSystemServer();
649         }
650     }
651 
652     /**
653      * Return {@code TRUE} if the app is running on a device whose default launcher supports
654      * {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.
655      *
656      * <p>The return value may change in subsequent calls if the user changes the default launcher
657      * app.
658      *
659      * <p><b>Note:</b> See also the support library counterpart
660      * {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(
661      * Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the
662      * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
663      *
664      * @see #requestPinShortcut(ShortcutInfo, IntentSender)
665      */
isRequestPinShortcutSupported()666     public boolean isRequestPinShortcutSupported() {
667         try {
668             return mService.isRequestPinItemSupported(injectMyUserId(),
669                     LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT);
670         } catch (RemoteException e) {
671             throw e.rethrowFromSystemServer();
672         }
673     }
674 
675     /**
676      * Request to create a pinned shortcut.  The default launcher will receive this request and
677      * ask the user for approval.  If the user approves it, the shortcut will be created, and
678      * {@code resultIntent} will be sent. If a request is denied by the user, however, no response
679      * will be sent to the caller.
680      *
681      * <p>Only apps with a foreground activity or a foreground service can call this method.
682      * Otherwise, it'll throw {@link IllegalStateException}.
683      *
684      * <p>It's up to the launcher to decide how to handle previous pending requests when the same
685      * package calls this API multiple times in a row. One possible strategy is to ignore any
686      * previous requests.
687      *
688      * <p><b>Note:</b> See also the support library counterpart
689      * {@link android.support.v4.content.pm.ShortcutManagerCompat#requestPinShortcut(
690      * Context, ShortcutInfoCompat, IntentSender)},
691      * which supports Android versions lower than {@link VERSION_CODES#O} using the
692      * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
693      *
694      * @param shortcut Shortcut to pin.  If an app wants to pin an existing (either static
695      *     or dynamic) shortcut, then it only needs to have an ID. Although other fields don't have
696      *     to be set, the target shortcut must be enabled.
697      *
698      *     <p>If it's a new shortcut, all the mandatory fields, such as a short label, must be
699      *     set.
700      * @param resultIntent If not null, this intent will be sent when the shortcut is pinned.
701      *    Use {@link android.app.PendingIntent#getIntentSender()} to create an {@link IntentSender}.
702      *    To avoid background execution limits, use an unexported, manifest-declared receiver.
703      *    For more details, see the overview documentation for the {@link ShortcutManager} class.
704      *
705      * @return {@code TRUE} if the launcher supports this feature.  Note the API will return without
706      *    waiting for the user to respond, so getting {@code TRUE} from this API does *not* mean
707      *    the shortcut was pinned successfully.  {@code FALSE} if the launcher doesn't support this
708      *    feature.
709      *
710      * @see #isRequestPinShortcutSupported()
711      * @see IntentSender
712      * @see android.app.PendingIntent#getIntentSender()
713      *
714      * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
715      * @throws IllegalStateException The caller doesn't have a foreground activity or a foreground
716      * service, or the device is locked.
717      */
requestPinShortcut(@onNull ShortcutInfo shortcut, @Nullable IntentSender resultIntent)718     public boolean requestPinShortcut(@NonNull ShortcutInfo shortcut,
719             @Nullable IntentSender resultIntent) {
720         try {
721             return mService.requestPinShortcut(mContext.getPackageName(), shortcut,
722                     resultIntent, injectMyUserId());
723         } catch (RemoteException e) {
724             throw e.rethrowFromSystemServer();
725         }
726     }
727 
728     /**
729      * Returns an Intent which can be used by the default launcher to pin a shortcut containing the
730      * given {@link ShortcutInfo}. This method should be used by an Activity to set a result in
731      * response to {@link Intent#ACTION_CREATE_SHORTCUT}.
732      *
733      * @param shortcut New shortcut to pin.  If an app wants to pin an existing (either dynamic
734      *     or manifest) shortcut, then it only needs to have an ID, and other fields don't have to
735      *     be set, in which case, the target shortcut must be enabled.
736      *     If it's a new shortcut, all the mandatory fields, such as a short label, must be
737      *     set.
738      * @return The intent that should be set as the result for the calling activity, or
739      *     <code>null</code> if the current launcher doesn't support shortcuts.
740      *
741      * @see Intent#ACTION_CREATE_SHORTCUT
742      *
743      * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
744      */
createShortcutResultIntent(@onNull ShortcutInfo shortcut)745     public Intent createShortcutResultIntent(@NonNull ShortcutInfo shortcut) {
746         try {
747             return mService.createShortcutResultIntent(mContext.getPackageName(), shortcut,
748                     injectMyUserId());
749         } catch (RemoteException e) {
750             throw e.rethrowFromSystemServer();
751         }
752     }
753 
754     /**
755      * Called internally when an app is considered to have come to the foreground
756      * even when technically it's not.  This method resets the throttling for this package.
757      * For example, when the user sends an "inline reply" on a notification, the system UI will
758      * call it.
759      *
760      * @hide
761      */
onApplicationActive(@onNull String packageName, @UserIdInt int userId)762     public void onApplicationActive(@NonNull String packageName, @UserIdInt int userId) {
763         try {
764             mService.onApplicationActive(packageName, userId);
765         } catch (RemoteException e) {
766             throw e.rethrowFromSystemServer();
767         }
768     }
769 
770     /** @hide injection point */
771     @VisibleForTesting
injectMyUserId()772     protected int injectMyUserId() {
773         return mContext.getUserId();
774     }
775 }
776