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 manages an app's <em>shortcuts</em>. Shortcuts provide users
40  * with quick access to activities other than an app's main activity in the currently-active
41  * launcher.  For example,
42  * an email app may publish the "compose new email" action, which will directly open the
43  * compose activity.  The {@link ShortcutInfo} class contains information about each of the
44  * shortcuts themselves.
45  *
46  * <h3>Static Shortcuts and Dynamic Shortcuts</h3>
47  *
48  * <p>
49  * There are several different types of shortcuts:
50  *
51  * <ul>
52  * <li><p>Static shortcuts are declared in a resource XML file, which is referenced in the publisher
53  * app's <code>AndroidManifest.xml</code> file. These shortcuts are visually associated with an
54  * app's launcher icon.
55  * <p>Static shortcuts are published when an app is installed, and the details of these shortcuts
56  * change when an app is upgraded with an updated XML file. Static shortcuts are immutable, and
57  * their definitions, such as icons and labels, cannot be changed dynamically without upgrading the
58  * publisher app.</li>
59  *
60  * <li>Dynamic shortcuts are published at runtime using this class's APIs. These shortcuts are
61  * visually associated with an app's launcher icon. Apps can publish, update, and remove dynamic
62  * shortcuts at runtime.
63  * </ul>
64  *
65  * <p>Only main activities&mdash;activities that handle the {@code MAIN} action and the
66  * {@code LAUNCHER} category&mdash;can have shortcuts.
67  * If an app has multiple main activities, these activities have different sets
68  * of shortcuts.
69  *
70  * <p>Static shortcuts and dynamic shortcuts are shown in a supported launcher when the user
71  * long-presses on an app's launcher icon. Note that the actual gesture may be different
72  * depending on the launcher app.
73  *
74  * <p>Each launcher icon can have at most {@link #getMaxShortcutCountPerActivity()} number of
75  * static and dynamic shortcuts combined.
76  *
77  *
78  * <h3>Pinning Shortcuts</h3>
79  *
80  * <p>Apps running in the foreground can also <em>pin</em> shortcuts at runtime, subject to user
81  * permission, using this class's APIs. Each pinned shortcut is a copy of a static shortcut or a
82  * dynamic shortcut. Although users can pin a shortcut multiple times, the system calls the pinning
83  * API only once to complete the pinning process. Unlike static and dynamic shortcuts, pinned
84  * shortcuts appear as separate icons, visually distinct from the app's launcher icon, in the
85  * launcher. There is no limit to the number of pinned shortcuts that an app can create.
86  *
87  * <p>Pinned shortcuts <strong>cannot</strong> be removed by publisher apps. They're removed only
88  * when the user removes them, when the publisher app is uninstalled, or when the user performs the
89  * clear data action on the publisher app from the device's <b>Settings</b> app.
90  *
91  * <p>However, the publisher app can <em>disable</em> pinned shortcuts so they cannot be started.
92  * See the following sections for details.
93  *
94  * <h3>Updating and Disabling Shortcuts</h3>
95  *
96  * <p>When a dynamic shortcut is pinned, even when the publisher removes it as a dynamic shortcut,
97  * the pinned shortcut will still be visible and launchable.  This allows an app to have
98  * more than {@link #getMaxShortcutCountPerActivity()} number of shortcuts.
99  *
100  * <p>For example, suppose {@link #getMaxShortcutCountPerActivity()} is 5:
101  * <ol>
102  *     <li>A chat app publishes 5 dynamic shortcuts for the 5 most recent
103  *     conversations (c1, c2, ..., c5).
104  *
105  *     <li>The user pins all 5 of the shortcuts.
106  *
107  *     <li>Later, the user has started 3 additional conversations (c6, c7, and c8),
108  *     so the publisher app
109  *     re-publishes its dynamic shortcuts.  The new dynamic shortcut list is:
110  *     c4, c5, ..., c8.
111  *     The publisher app has to remove c1, c2, and c3 because it can't have more than
112  *     5 dynamic shortcuts.
113  *
114  *     <li>However, even though c1, c2, and c3 are no longer dynamic shortcuts, the pinned
115  *     shortcuts for these conversations are still available and launchable.
116  *
117  *     <li>At this point, the user can access a total of 8 shortcuts that link to activities in
118  *     the publisher app, including the 3 pinned
119  *     shortcuts, even though an app can have at most 5 dynamic shortcuts.
120  *
121  *     <li>The app can use {@link #updateShortcuts(List)} to update <em>any</em> of the existing
122  *     8 shortcuts, when, for example, the chat peers' icons have changed.
123  * </ol>
124  * The {@link #addDynamicShortcuts(List)} and {@link #setDynamicShortcuts(List)} methods
125  * can also be used
126  * to update existing shortcuts with the same IDs, but they <b>cannot</b> be used
127  * for updating non-dynamic, pinned shortcuts because these two methods try to convert the given
128  * lists of shortcuts to dynamic shortcuts.
129  *
130  *
131  * <h4>Disabling Static Shortcuts</h4>
132  * <p>When an app is upgraded and the new version
133  * no longer uses a static shortcut that appeared in the previous version, this deprecated
134  * shortcut isn't published as a static shortcut.
135  *
136  * <p>If the deprecated shortcut is pinned, then the pinned shortcut will remain on the launcher,
137  * but it's disabled automatically. When a pinned shortcut is disabled, this class's APIs cannot
138  * update it.
139  *
140  * <h4>Disabling Dynamic Shortcuts</h4>
141  * Sometimes pinned shortcuts become obsolete and may not be usable.  For example, a pinned shortcut
142  * to a group chat becomes unusable when the associated group chat is deleted.  In cases like this,
143  * apps should use {@link #disableShortcuts(List)}, which removes the specified dynamic
144  * shortcuts and also makes any specified pinned shortcuts un-launchable.
145  * The {@link #disableShortcuts(List, CharSequence)} method can also be used to disable shortcuts
146  * and show users a custom error message when they attempt to launch the disabled shortcuts.
147  *
148  *
149  * <h3>Publishing Static Shortcuts</h3>
150  *
151  * <p>
152  * In order to add static shortcuts to your app, first add
153  * {@code <meta-data android:name="android.app.shortcuts" />} to your main activity in
154  * AndroidManifest.xml:
155  * <pre>
156  *&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
157  *             package="com.example.myapplication"&gt;
158  *  &lt;application ... &gt;
159  *    &lt;activity android:name="Main"&gt;
160  *      &lt;intent-filter&gt;
161  *        &lt;action android:name="android.intent.action.MAIN" /&gt;
162  *        &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
163  *      &lt;/intent-filter&gt;
164  *      <strong>&lt;meta-data android:name="android.app.shortcuts"
165  *                 android:resource="@xml/shortcuts" /&gt;</strong>
166  *    &lt;/activity&gt;
167  *  &lt;/application&gt;
168  *&lt;/manifest&gt;
169  * </pre>
170  *
171  * Then, define your app's static shortcuts in the <code>res/xml/shortcuts.xml</code>
172  * file:
173  * <pre>
174  *&lt;shortcuts xmlns:android="http://schemas.android.com/apk/res/android"&gt;
175  *  &lt;shortcut
176  *    android:shortcutId="compose"
177  *    android:enabled="true"
178  *    android:icon="@drawable/compose_icon"
179  *    android:shortcutShortLabel="@string/compose_shortcut_short_label1"
180  *    android:shortcutLongLabel="@string/compose_shortcut_long_label1"
181  *    android:shortcutDisabledMessage="@string/compose_disabled_message1"&gt;
182  *    &lt;intent
183  *      android:action="android.intent.action.VIEW"
184  *      android:targetPackage="com.example.myapplication"
185  *      android:targetClass="com.example.myapplication.ComposeActivity" /&gt;
186  *    &lt;!-- If your shortcut is associated with multiple intents, include them
187  *         here. The last intent in the list is what the user sees when they
188  *         launch this shortcut. --&gt;
189  *    &lt;categories android:name="android.shortcut.conversation" /&gt;
190  *  &lt;/shortcut&gt;
191  *  &lt;!-- Specify more shortcuts here. --&gt;
192  *&lt;/shortcuts&gt;
193  * </pre>
194  *
195  * The following list includes descriptions for the different attributes within a static shortcut:
196  * <dl>
197  *   <dt>{@code android:shortcutId}</dt>
198  *   <dd>Mandatory shortcut ID.
199  *   <p>
200  *   This must be a string literal.
201  *   A resource string, such as <code>@string/foo</code>, cannot be used.
202  *   </dd>
203  *
204  *   <dt>{@code android:enabled}</dt>
205  *   <dd>Default is {@code true}.  Can be set to {@code false} in order
206  *   to disable a static shortcut that was published in a previous version and set a custom
207  *   disabled message.  If a custom disabled message is not needed, then a static shortcut can
208  *   be simply removed from the XML file rather than keeping it with {@code enabled="false"}.</dd>
209  *
210  *   <dt>{@code android:icon}</dt>
211  *   <dd>Shortcut icon.</dd>
212  *
213  *   <dt>{@code android:shortcutShortLabel}</dt>
214  *   <dd>Mandatory shortcut short label.
215  *   See {@link ShortcutInfo.Builder#setShortLabel(CharSequence)}.
216  *   <p>
217  *   This must be a resource string, such as <code>@string/shortcut_label</code>.
218  *   </dd>
219  *
220  *   <dt>{@code android:shortcutLongLabel}</dt>
221  *   <dd>Shortcut long label.
222  *   See {@link ShortcutInfo.Builder#setLongLabel(CharSequence)}.
223  *   <p>
224  *   This must be a resource string, such as <code>@string/shortcut_long_label</code>.
225  *   </dd>
226  *
227  *   <dt>{@code android:shortcutDisabledMessage}</dt>
228  *   <dd>When {@code android:enabled} is set to
229  *   {@code false}, this attribute is used to display a custom disabled message.
230  *   <p>
231  *   This must be a resource string, such as <code>@string/shortcut_disabled_message</code>.
232  *   </dd>
233  *
234  *   <dt>{@code intent}</dt>
235  *   <dd>Intent to launch when the user selects the shortcut.
236  *   {@code android:action} is mandatory.
237  *   See <a href="{@docRoot}guide/topics/ui/settings.html#Intents">Using intents</a> for the
238  *   other supported tags.
239  *   You can provide multiple intents for a single shortcut so that the last defined activity is launched
240  *   with the other activities in the <a href="/guide/components/tasks-and-back-stack.html">back stack</a>.
241  *   See {@link android.app.TaskStackBuilder} for details.
242  *   <p><b>Note:</b> String resources may not be used within an {@code <intent>} element.
243  *   </dd>
244  *   <dt>{@code categories}</dt>
245  *   <dd>Specify shortcut categories.  Currently only
246  *   {@link ShortcutInfo#SHORTCUT_CATEGORY_CONVERSATION} is defined in the framework.
247  *   </dd>
248  * </dl>
249  *
250  * <h3>Publishing Dynamic Shortcuts</h3>
251  *
252  * <p>
253  * Apps can publish dynamic shortcuts with {@link #setDynamicShortcuts(List)}
254  * or {@link #addDynamicShortcuts(List)}.  The {@link #updateShortcuts(List)} method can also be
255  * used to update existing, mutable shortcuts.
256  * Use {@link #removeDynamicShortcuts(List)} or {@link #removeAllDynamicShortcuts()} to remove
257  * dynamic shortcuts.
258  *
259  * <p>The following code snippet shows how to create a single dynamic shortcut:
260  * <pre>
261  *ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
262  *
263  *ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
264  *    .setShortLabel("Web site")
265  *    .setLongLabel("Open the web site")
266  *    .setIcon(Icon.createWithResource(context, R.drawable.icon_website))
267  *    .setIntent(new Intent(Intent.ACTION_VIEW,
268  *                   Uri.parse("https://www.mysite.example.com/")))
269  *    .build();
270  *
271  *shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
272  * </pre>
273  *
274  * <h3>Publishing Pinned Shortcuts</h3>
275  *
276  * <p>Apps can pin an existing shortcut (either static or dynamic) or an entirely new shortcut to a
277  * supported launcher programatically using {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.
278  * You pass two arguments into this method:
279  *
280  * <ul>
281  *   <li>A {@link ShortcutInfo} object &ndash; If the shortcut already exists, this object should
282  *   contain only the shortcut's ID. Otherwise, the new {@link ShortcutInfo} object must contain an
283  *   ID, an intent, and a short label for the new shortcut.
284  *   <li><p>A {@link android.app.PendingIntent} object &ndash; This intent represents the callback
285  *   that your app receives if the shortcut is successfully pinned to the device's launcher.
286  *   <p><b>Note:</b> If the user doesn't allow the shortcut to be pinned to the launcher, the
287  *   pinning process fails, and the {@link Intent} object that is passed into this
288  *   {@link android.app.PendingIntent} object isn't executed.
289  *   <div class="note"><p><b>Note:</b> Due to background execution limits introduced in Android
290  *   {@link VERSION_CODES#O}, it's best to use a
291  *   <a href="{@docRoot}guide/components/broadcasts.html#manifest-declared_receivers">
292  *   manifest-declared receiver</a> to receive a callback.
293  *   <p>Also, to prevent other apps from invoking the receiver, add the attribute assignment
294  *   <code>android:exported="false"</code> to the receiver's manifest entry.</p></div>
295  * </ul>
296  *
297  * The following code snippet shows how to pin a single shortcut that already exists and is enabled:
298  *
299  * <pre>
300  *ShortcutManager mShortcutManager =
301  *        context.getSystemService(ShortcutManager.class);
302  *
303  *if (mShortcutManager.isRequestPinShortcutSupported()) {
304  *
305  *    // This example defines a new shortcut; that is, this shortcut hasn't
306  *    // been published before.
307  *    ShortcutInfo pinShortcutInfo = new ShortcutInfo.Builder()
308  *            .setIcon(myIcon)
309  *            .setShortLabel("My awesome shortcut")
310  *            .setIntent(myIntent)
311  *            .build();
312  *
313  *    PendingIntent resultPendingIntent = null;
314  *
315  *    // Create the following Intent and PendingIntent objects only if your app
316  *    // needs to be notified that the user allowed the shortcut to be pinned.
317  *    // Use a boolean value, such as "appNeedsNotifying", to define this behavior.
318  *    if (appNeedsNotifying) {
319  *        // We assume here the app has a manifest-declared receiver "MyReceiver".
320  *        Intent pinnedShortcutCallbackIntent = new Intent(context, MyReceiver.class);
321  *
322  *        // Configure the intent so that your app's broadcast receiver gets
323  *        // the callback successfully.
324  *        PendingIntent successCallback = PendingIntent.createBroadcast(context, 0,
325  *                pinnedShortcutCallbackIntent);
326  *
327  *        resultPendingIntent = successCallback.getIntentSender();
328  *    }
329  *
330  *    mShortcutManager.requestPinShortcut(pinShortcutInfo, resultPendingIntent);
331  *}
332  * </pre>
333  *
334  * <p class="note"><strong>Note:</strong> As you add logic in your app to make requests to pin
335  * shortcuts, keep in mind that not all launchers support pinning of shortcuts. To determine whether
336  * your app can complete this process on a particular device, check the return value of
337  * {@link #isRequestPinShortcutSupported()}. Based on this return value, you might decide to hide
338  * the option in your app that allows users to pin a shortcut.
339  *
340  * <p class="note"><strong>Note:</strong> See also the support library APIs
341  * {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(
342  * Context)} and
343  * {@link android.support.v4.content.pm.ShortcutManagerCompat#requestPinShortcut(
344  * Context, ShortcutInfoCompat, IntentSender)}, which works on Android versions lower than
345  * {@link VERSION_CODES#O} by falling back to the deprecated private intent
346  * {@code com.android.launcher.action.INSTALL_SHORTCUT}.
347  *
348  * <h4>Custom Activity for Pinning Shortcuts</h4>
349  *
350  * <p>You can also create a specialized activity that helps users create shortcuts, complete with
351  * custom options and a confirmation button. In your app's manifest file, add
352  * {@link Intent#ACTION_CREATE_SHORTCUT} to the activity's <code>&lt;intent-filter&gt;</code>
353  * element, as shown in the following snippet:
354  *
355  * <pre>
356  *&lt;manifest&gt;
357  *    ...
358  *    &lt;application&gt;
359  *        &lt;activity android:name="com.example.MyCustomPromptToPinShortcut" ... &gt;
360  *            &lt;intent-filter
361  *                    action android:name="android.intent.action.ACTION_CREATE_SHORTCUT"&gt;
362  *            ...
363  *            &lt;/intent-filter&gt;
364  *        &lt;/activity&gt;
365  *        ...
366  *    &lt;/application&gt;
367  *&lt;/manifest&gt;
368  * </pre>
369  *
370  * <p>When you use this specialized activity in your app, the following sequence of steps takes
371  * place:</p>
372  *
373  * <ol>
374  *   <li>The user attempts to create a shortcut, triggering the system to start the specialized
375  *   activity.</li>
376  *   <li>The user sets options for the shortcut.</li>
377  *   <li>The user selects the confirmation button, allowing your app to create the shortcut using
378  *   the {@link #createShortcutResultIntent(ShortcutInfo)} method. This method returns an
379  *   {@link Intent}, which your app relays back to the previously-executing activity using
380  *   {@link Activity#setResult(int)}.</li>
381  *   <li>Your app calls {@link Activity#finish()} on the activity used for creating the customized
382  *   shortcut.</li>
383  * </ol>
384  *
385  * <h3>Shortcut Intents</h3>
386  * <p>
387  * Dynamic shortcuts can be published with any set of {@link Intent#addFlags Intent} flags.
388  * Typically, {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} is specified, possibly along with other
389  * flags; otherwise, if the app is already running, the app is simply brought to
390  * the foreground, and the target activity may not appear.
391  *
392  * <p>The {@link ShortcutInfo.Builder#setIntents(Intent[])} method can be used instead of
393  * {@link ShortcutInfo.Builder#setIntent(Intent)} with {@link android.app.TaskStackBuilder}
394  * in order to launch an activity with other activities in the back stack.
395  * When the user selects a shortcut to load an activity with a back stack,
396  * then presses the back key, a parent activity from the same app will be shown
397  * instead of the user being navigated back to the launcher.
398  *
399  * <p>Static shortcuts can also have multiple intents to achieve the same effect.
400  * In order to associate multiple {@link Intent} objects with a shortcut, simply list multiple
401  * <code>&lt;intent&gt;</code> elements within a single <code>&lt;shortcut&gt;</code> element.
402  * The last intent specifies what the user sees when they launch a shortcut.
403  *
404  * <p>Static shortcuts <b>cannot</b> have custom intent flags.
405  * The first intent of a static shortcut will always have {@link Intent#FLAG_ACTIVITY_NEW_TASK}
406  * and {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} set.
407  * This means, when the app is already running, all the existing activities will be
408  * destroyed when a static shortcut is launched.
409  * If this behavior is not desirable, you can use a <em>trampoline activity</em>,
410  * or an invisible activity that starts another activity in {@link Activity#onCreate},
411  * then calls {@link Activity#finish()}.
412  * The first activity should include an attribute setting
413  * of {@code android:taskAffinity=""} in the app's <code>AndroidManifest.xml</code>
414  * file, and the intent within the static shortcut should point at this first activity.
415  *
416  *
417  * <h3>Showing New Information in a Shortcut</h3>
418  * <p>
419  * In order to avoid confusion, you should not use {@link #updateShortcuts(List)} to update
420  * a shortcut so that it contains conceptually different information.
421  *
422  * <p>For example, a phone app may publish the most frequently called contact as a dynamic
423  * shortcut.  Over time, this contact may change. When it does, the app should
424  * represent the changed contact with a new shortcut that contains a different ID, using either
425  * {@link #setDynamicShortcuts(List)} or {@link #addDynamicShortcuts(List)}, rather than updating
426  * the existing shortcut with {@link #updateShortcuts(List)}.
427  * This is because when the shortcut is pinned, changing
428  * it to reference a different contact will likely confuse the user.
429  *
430  * <p>On the other hand, when the
431  * contact's information has changed, such as the name or picture, the app should
432  * use {@link #updateShortcuts(List)} so that the pinned shortcut is updated too.
433  *
434  *
435  * <h3>Shortcut Display Order</h3>
436  * When the launcher displays the shortcuts that are associated with a particular launcher icon,
437  * the shortcuts should appear in the following order:
438  * <ul>
439  *   <li>First show static shortcuts
440  *   (if {@link ShortcutInfo#isDeclaredInManifest()} is {@code true}),
441  *   and then show dynamic shortcuts (if {@link ShortcutInfo#isDynamic()} is {@code true}).
442  *   <li>Within each category of shortcuts (static and dynamic), sort the shortcuts in order
443  *   of increasing rank according to {@link ShortcutInfo#getRank()}.
444  * </ul>
445  * <p>Shortcut ranks are non-negative, sequential integers
446  * that determine the order in which shortcuts appear, assuming that the shortcuts are all in
447  * the same category.
448  * Ranks of existing shortcuts can be updated with
449  * {@link #updateShortcuts(List)}. You can also use {@link #addDynamicShortcuts(List)} and
450  * {@link #setDynamicShortcuts(List)}.
451  *
452  * <p>Ranks are auto-adjusted so that they're unique for each target activity in each category
453  * (static or dynamic).  For example, if there are 3 dynamic shortcuts with ranks 0, 1 and 2,
454  * adding another dynamic shortcut with a rank of 1 represents a request to place this shortcut at
455  * the second position.
456  * In response, the third and fourth shortcuts move closer to the bottom of the shortcut list,
457  * with their ranks changing to 2 and 3, respectively.
458  *
459  * <h3>Rate Limiting</h3>
460  *
461  * <p>
462  * Calls to {@link #setDynamicShortcuts(List)}, {@link #addDynamicShortcuts(List)}, and
463  * {@link #updateShortcuts(List)} may be rate-limited when called by <em>background apps</em>, or
464  * apps with no foreground activity or service.  When you attempt to call these methods
465  * from a background app after exceeding the rate limit, these APIs return {@code false}.
466  *
467  * <p>Apps with a foreground activity or service are not rate-limited.
468  *
469  * <p>Rate-limiting is reset upon certain events, so that even background apps
470  * can call these APIs until the rate limit is reached again.
471  * These events include the following:
472  * <ul>
473  *   <li>An app comes to the foreground.
474  *   <li>The system locale changes.
475  *   <li>The user performs the <strong>inline reply</strong> action on a notification.
476  * </ul>
477  *
478  * <p>When rate-limiting is active, {@link #isRateLimitingActive()} returns {@code true}.
479  *
480  * <h4>Resetting rate-limiting for testing</h4>
481  *
482  * <p>
483  * If your app is rate-limited during development or testing, you can use the
484  * <strong>Reset ShortcutManager rate-limiting</strong> development option or
485  * the following {@code adb} command to reset it:
486  * <pre class="no-pretty-print">
487  *$ adb shell cmd shortcut reset-throttling [ --user USER-ID ]
488  * </pre>
489  *
490  * <h3>Handling System Locale Changes</h3>
491  *
492  * <p>
493  * Apps should update dynamic and pinned shortcuts when the system locale changes
494  * using the {@link Intent#ACTION_LOCALE_CHANGED} broadcast.
495  *
496  * <p>When the system locale changes, rate-limiting is reset, so even background apps
497  * can add and update dynamic shortcuts until the rate limit is reached again.
498  *
499  *
500  * <h3>Backup and Restore</h3>
501  *
502  * <p>
503  * When an app has the {@code android:allowBackup="true"} attribute assignment included
504  * in its <code>AndroidManifest.xml</code> file, pinned shortcuts are
505  * backed up automatically and are restored when the user sets up a new device.
506  *
507  * <h4>Categories of shortcuts that are backed up</h4>
508  *
509  * <ul>
510  *  <li>Pinned shortcuts are backed up.  Bitmap icons are not backed up by the system,
511  *  so launcher apps should back them up and restore them so that the user still sees icons
512  *  for pinned shortcuts on the launcher.  Apps can always use
513  *  {@link #updateShortcuts(List)} to re-publish icons.
514  *
515  *  <li>Static shortcuts aren't backed up, but when an app is re-installed on a new
516  *  device, they are re-published from the <code>AndroidManifest.xml</code> file.
517  *
518  *  <li>Dynamic shortcuts <b>aren't</b> backed up.
519  * </ul>
520  *
521  * <p>Because dynamic shortcuts are not restored, it is recommended that apps check
522  * currently-published dynamic shortcuts using {@link #getDynamicShortcuts()}
523  * each time they are launched, and they should re-publish
524  * dynamic shortcuts when necessary.
525  *
526  * <pre>
527  *public class MainActivity extends Activity {
528  *    public void onCreate(Bundle savedInstanceState) {
529  *        super.onCreate(savedInstanceState);
530  *        ShortcutManager shortcutManager =
531  *                getSystemService(ShortcutManager.class);
532  *
533  *        if (shortcutManager.getDynamicShortcuts().size() == 0) {
534  *            // Application restored. Need to re-publish dynamic shortcuts.
535  *            if (shortcutManager.getPinnedShortcuts().size() > 0) {
536  *                // Pinned shortcuts have been restored. Use
537  *                // updateShortcuts() to make sure they contain
538  *                // up-to-date information.
539  *            }
540  *        }
541  *    }
542  *    // ...
543  *}
544  * </pre>
545  *
546  *
547  * <h4>Backup/restore and shortcut IDs</h4>
548  * <p>
549  * Because pinned shortcuts are backed up and restored on new devices, shortcut IDs
550  * should contain either stable, constant strings or server-side identifiers,
551  * rather than identifiers generated locally that might not make sense on other devices.
552  *
553  *
554  * <h3>Report Shortcut Usage and Prediction</h3>
555  * <p>
556  * Launcher apps may be capable of predicting which shortcuts will most likely be
557  * used at a given time by examining the shortcut usage history data.
558  *
559  * <p>In order to provide launchers with such data, publisher apps should
560  * report the shortcuts that are used with {@link #reportShortcutUsed(String)}
561  * when a shortcut is selected,
562  * <b>or when an action equivalent to a shortcut is taken by the user even if it wasn't started
563  * with the shortcut</b>.
564  *
565  * <p>For example, suppose a navigation app supports "navigate to work" as a shortcut.
566  * It should then report when the user selects this shortcut <b>and</b> when the user chooses
567  * to navigate to work within the app itself.
568  * This helps the launcher app
569  * learn that the user wants to navigate to work at a certain time every
570  * weekday, and it can then show this shortcut in a suggestion list at the right time.
571  *
572  * <h3>Launcher API</h3>
573  *
574  * The {@link LauncherApps} class provides APIs for launcher apps to access shortcuts.
575  *
576  *
577  * <h3>Direct Boot and Shortcuts</h3>
578  *
579  * All shortcut information is stored in credential encrypted storage, so no shortcuts can be
580  * accessed when the user is locked.
581  */
582 @SystemService(Context.SHORTCUT_SERVICE)
583 public class ShortcutManager {
584     private static final String TAG = "ShortcutManager";
585 
586     private final Context mContext;
587     private final IShortcutService mService;
588 
589     /**
590      * @hide
591      */
ShortcutManager(Context context, IShortcutService service)592     public ShortcutManager(Context context, IShortcutService service) {
593         mContext = context;
594         mService = service;
595     }
596 
597     /**
598      * @hide
599      */
600     @TestApi
ShortcutManager(Context context)601     public ShortcutManager(Context context) {
602         this(context, IShortcutService.Stub.asInterface(
603                 ServiceManager.getService(Context.SHORTCUT_SERVICE)));
604     }
605 
606     /**
607      * Publish the list of shortcuts.  All existing dynamic shortcuts from the caller app
608      * will be replaced.  If there are already pinned shortcuts with the same IDs,
609      * the mutable pinned shortcuts are updated.
610      *
611      * <p>This API will be rate-limited.
612      *
613      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
614      *
615      * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
616      * or when trying to update immutable shortcuts.
617      *
618      * @throws IllegalStateException when the user is locked.
619      */
setDynamicShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)620     public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
621         try {
622             return mService.setDynamicShortcuts(mContext.getPackageName(),
623                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
624         } catch (RemoteException e) {
625             throw e.rethrowFromSystemServer();
626         }
627     }
628 
629     /**
630      * Return all dynamic shortcuts from the caller app.
631      *
632      * <p>This API is intended to be used for examining what shortcuts are currently published.
633      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
634      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
635      *
636      * @throws IllegalStateException when the user is locked.
637      */
638     @NonNull
getDynamicShortcuts()639     public List<ShortcutInfo> getDynamicShortcuts() {
640         try {
641             return mService.getDynamicShortcuts(mContext.getPackageName(), injectMyUserId())
642                     .getList();
643         } catch (RemoteException e) {
644             throw e.rethrowFromSystemServer();
645         }
646     }
647 
648     /**
649      * Return all static (manifest) shortcuts from the caller app.
650      *
651      * <p>This API is intended to be used for examining what shortcuts are currently published.
652      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
653      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
654      *
655      * @throws IllegalStateException when the user is locked.
656      */
657     @NonNull
getManifestShortcuts()658     public List<ShortcutInfo> getManifestShortcuts() {
659         try {
660             return mService.getManifestShortcuts(mContext.getPackageName(), injectMyUserId())
661                     .getList();
662         } catch (RemoteException e) {
663             throw e.rethrowFromSystemServer();
664         }
665     }
666 
667     /**
668      * Publish the list of dynamic shortcuts.  If there are already dynamic or pinned shortcuts with
669      * the same IDs, each mutable shortcut is updated.
670      *
671      * <p>This API will be rate-limited.
672      *
673      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
674      *
675      * @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
676      * or when trying to update immutable shortcuts.
677      *
678      * @throws IllegalStateException when the user is locked.
679      */
addDynamicShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)680     public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
681         try {
682             return mService.addDynamicShortcuts(mContext.getPackageName(),
683                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
684         } catch (RemoteException e) {
685             throw e.rethrowFromSystemServer();
686         }
687     }
688 
689     /**
690      * Delete dynamic shortcuts by ID.
691      *
692      * @throws IllegalStateException when the user is locked.
693      */
removeDynamicShortcuts(@onNull List<String> shortcutIds)694     public void removeDynamicShortcuts(@NonNull List<String> shortcutIds) {
695         try {
696             mService.removeDynamicShortcuts(mContext.getPackageName(), shortcutIds,
697                     injectMyUserId());
698         } catch (RemoteException e) {
699             throw e.rethrowFromSystemServer();
700         }
701     }
702 
703     /**
704      * Delete all dynamic shortcuts from the caller app.
705      *
706      * @throws IllegalStateException when the user is locked.
707      */
removeAllDynamicShortcuts()708     public void removeAllDynamicShortcuts() {
709         try {
710             mService.removeAllDynamicShortcuts(mContext.getPackageName(), injectMyUserId());
711         } catch (RemoteException e) {
712             throw e.rethrowFromSystemServer();
713         }
714     }
715 
716     /**
717      * Return all pinned shortcuts from the caller app.
718      *
719      * <p>This API is intended to be used for examining what shortcuts are currently published.
720      * Re-publishing returned {@link ShortcutInfo}s via APIs such as
721      * {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
722      *
723      * @throws IllegalStateException when the user is locked.
724      */
725     @NonNull
getPinnedShortcuts()726     public List<ShortcutInfo> getPinnedShortcuts() {
727         try {
728             return mService.getPinnedShortcuts(mContext.getPackageName(), injectMyUserId())
729                     .getList();
730         } catch (RemoteException e) {
731             throw e.rethrowFromSystemServer();
732         }
733     }
734 
735     /**
736      * Update all existing shortcuts with the same IDs.  Target shortcuts may be pinned and/or
737      * dynamic, but they must not be immutable.
738      *
739      * <p>This API will be rate-limited.
740      *
741      * @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
742      *
743      * @throws IllegalArgumentException If trying to update immutable shortcuts.
744      *
745      * @throws IllegalStateException when the user is locked.
746      */
updateShortcuts(@onNull List<ShortcutInfo> shortcutInfoList)747     public boolean updateShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
748         try {
749             return mService.updateShortcuts(mContext.getPackageName(),
750                     new ParceledListSlice(shortcutInfoList), injectMyUserId());
751         } catch (RemoteException e) {
752             throw e.rethrowFromSystemServer();
753         }
754     }
755 
756     /**
757      * Disable pinned shortcuts.  For more details, see the Javadoc for the {@link ShortcutManager}
758      * class.
759      *
760      * @throws IllegalArgumentException If trying to disable immutable shortcuts.
761      *
762      * @throws IllegalStateException when the user is locked.
763      */
disableShortcuts(@onNull List<String> shortcutIds)764     public void disableShortcuts(@NonNull List<String> shortcutIds) {
765         try {
766             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
767                     /* disabledMessage =*/ null, /* disabledMessageResId =*/ 0,
768                     injectMyUserId());
769         } catch (RemoteException e) {
770             throw e.rethrowFromSystemServer();
771         }
772     }
773 
774     /**
775      * @hide old signature, kept for unit testing.
776      */
disableShortcuts(@onNull List<String> shortcutIds, int disabledMessageResId)777     public void disableShortcuts(@NonNull List<String> shortcutIds, int disabledMessageResId) {
778         try {
779             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
780                     /* disabledMessage =*/ null, disabledMessageResId,
781                     injectMyUserId());
782         } catch (RemoteException e) {
783             throw e.rethrowFromSystemServer();
784         }
785     }
786 
787     /**
788      * @hide old signature, kept for unit testing.
789      */
disableShortcuts(@onNull List<String> shortcutIds, String disabledMessage)790     public void disableShortcuts(@NonNull List<String> shortcutIds, String disabledMessage) {
791         disableShortcuts(shortcutIds, (CharSequence) disabledMessage);
792     }
793 
794     /**
795      * Disable pinned shortcuts, showing the user a custom error message when they try to select
796      * the disabled shortcuts.
797      * For more details, see the Javadoc for the {@link ShortcutManager} class.
798      *
799      * @throws IllegalArgumentException If trying to disable immutable shortcuts.
800      *
801      * @throws IllegalStateException when the user is locked.
802      */
disableShortcuts(@onNull List<String> shortcutIds, CharSequence disabledMessage)803     public void disableShortcuts(@NonNull List<String> shortcutIds, CharSequence disabledMessage) {
804         try {
805             mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
806                     disabledMessage, /* disabledMessageResId =*/ 0,
807                     injectMyUserId());
808         } catch (RemoteException e) {
809             throw e.rethrowFromSystemServer();
810         }
811     }
812 
813     /**
814      * Re-enable pinned shortcuts that were previously disabled.  If the target shortcuts
815      * are already enabled, this method does nothing.
816      *
817      * @throws IllegalArgumentException If trying to enable immutable shortcuts.
818      *
819      * @throws IllegalStateException when the user is locked.
820      */
enableShortcuts(@onNull List<String> shortcutIds)821     public void enableShortcuts(@NonNull List<String> shortcutIds) {
822         try {
823             mService.enableShortcuts(mContext.getPackageName(), shortcutIds, injectMyUserId());
824         } catch (RemoteException e) {
825             throw e.rethrowFromSystemServer();
826         }
827     }
828 
829 
830     /**
831      * @hide old signature, kept for unit testing.
832      */
getMaxShortcutCountForActivity()833     public int getMaxShortcutCountForActivity() {
834         return getMaxShortcutCountPerActivity();
835     }
836 
837     /**
838      * Return the maximum number of static and dynamic shortcuts that each launcher icon
839      * can have at a time.
840      */
getMaxShortcutCountPerActivity()841     public int getMaxShortcutCountPerActivity() {
842         try {
843             return mService.getMaxShortcutCountPerActivity(
844                     mContext.getPackageName(), injectMyUserId());
845         } catch (RemoteException e) {
846             throw e.rethrowFromSystemServer();
847         }
848     }
849 
850     /**
851      * Return the number of times the caller app can call the rate-limited APIs
852      * before the rate limit counter is reset.
853      *
854      * @see #getRateLimitResetTime()
855      *
856      * @hide
857      */
getRemainingCallCount()858     public int getRemainingCallCount() {
859         try {
860             return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId());
861         } catch (RemoteException e) {
862             throw e.rethrowFromSystemServer();
863         }
864     }
865 
866     /**
867      * Return when the rate limit count will be reset next time, in milliseconds since the epoch.
868      *
869      * @see #getRemainingCallCount()
870      * @see System#currentTimeMillis()
871      *
872      * @hide
873      */
getRateLimitResetTime()874     public long getRateLimitResetTime() {
875         try {
876             return mService.getRateLimitResetTime(mContext.getPackageName(), injectMyUserId());
877         } catch (RemoteException e) {
878             throw e.rethrowFromSystemServer();
879         }
880     }
881 
882     /**
883      * Return {@code true} when rate-limiting is active for the caller app.
884      *
885      * <p>See the class level javadoc for details.
886      *
887      * @throws IllegalStateException when the user is locked.
888      */
isRateLimitingActive()889     public boolean isRateLimitingActive() {
890         try {
891             return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId())
892                     == 0;
893         } catch (RemoteException e) {
894             throw e.rethrowFromSystemServer();
895         }
896     }
897 
898     /**
899      * Return the max width for icons, in pixels.
900      *
901      * <p> Note that this method returns max width of icon's visible part. Hence, it does not take
902      * into account the inset introduced by {@link AdaptiveIconDrawable}. To calculate bitmap image
903      * to function as {@link AdaptiveIconDrawable}, multiply
904      * 1 + 2 * {@link AdaptiveIconDrawable#getExtraInsetFraction()} to the returned size.
905      */
getIconMaxWidth()906     public int getIconMaxWidth() {
907         try {
908             // TODO Implement it properly using xdpi.
909             return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
910         } catch (RemoteException e) {
911             throw e.rethrowFromSystemServer();
912         }
913     }
914 
915     /**
916      * Return the max height for icons, in pixels.
917      */
getIconMaxHeight()918     public int getIconMaxHeight() {
919         try {
920             // TODO Implement it properly using ydpi.
921             return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
922         } catch (RemoteException e) {
923             throw e.rethrowFromSystemServer();
924         }
925     }
926 
927     /**
928      * Apps that publish shortcuts should call this method whenever the user
929      * selects the shortcut containing the given ID or when the user completes
930      * an action in the app that is equivalent to selecting the shortcut.
931      * For more details, see the Javadoc for the {@link ShortcutManager} class
932      *
933      * <p>The information is accessible via {@link UsageStatsManager#queryEvents}
934      * Typically, launcher apps use this information to build a prediction model
935      * so that they can promote the shortcuts that are likely to be used at the moment.
936      *
937      * @throws IllegalStateException when the user is locked.
938      */
reportShortcutUsed(String shortcutId)939     public void reportShortcutUsed(String shortcutId) {
940         try {
941             mService.reportShortcutUsed(mContext.getPackageName(), shortcutId,
942                     injectMyUserId());
943         } catch (RemoteException e) {
944             throw e.rethrowFromSystemServer();
945         }
946     }
947 
948     /**
949      * Return {@code TRUE} if the app is running on a device whose default launcher supports
950      * {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.
951      *
952      * <p>The return value may change in subsequent calls if the user changes the default launcher
953      * app.
954      *
955      * <p><b>Note:</b> See also the support library counterpart
956      * {@link android.support.v4.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(
957      * Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the
958      * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
959      *
960      * @see #requestPinShortcut(ShortcutInfo, IntentSender)
961      */
isRequestPinShortcutSupported()962     public boolean isRequestPinShortcutSupported() {
963         try {
964             return mService.isRequestPinItemSupported(injectMyUserId(),
965                     LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT);
966         } catch (RemoteException e) {
967             throw e.rethrowFromSystemServer();
968         }
969     }
970 
971     /**
972      * Request to create a pinned shortcut.  The default launcher will receive this request and
973      * ask the user for approval.  If the user approves it, the shortcut will be created, and
974      * {@code resultIntent} will be sent. If a request is denied by the user, however, no response
975      * will be sent to the caller.
976      *
977      * <p>Only apps with a foreground activity or a foreground service can call this method.
978      * Otherwise, it'll throw {@link IllegalStateException}.
979      *
980      * <p>It's up to the launcher to decide how to handle previous pending requests when the same
981      * package calls this API multiple times in a row. One possible strategy is to ignore any
982      * previous requests.
983      *
984      * <p><b>Note:</b> See also the support library counterpart
985      * {@link android.support.v4.content.pm.ShortcutManagerCompat#requestPinShortcut(
986      * Context, ShortcutInfoCompat, IntentSender)},
987      * which supports Android versions lower than {@link VERSION_CODES#O} using the
988      * legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
989      *
990      * @param shortcut Shortcut to pin.  If an app wants to pin an existing (either static
991      *     or dynamic) shortcut, then it only needs to have an ID. Although other fields don't have
992      *     to be set, the target shortcut must be enabled.
993      *
994      *     <p>If it's a new shortcut, all the mandatory fields, such as a short label, must be
995      *     set.
996      * @param resultIntent If not null, this intent will be sent when the shortcut is pinned.
997      *    Use {@link android.app.PendingIntent#getIntentSender()} to create an {@link IntentSender}.
998      *    To avoid background execution limits, use an unexported, manifest-declared receiver.
999      *    For more details, see the overview documentation for the {@link ShortcutManager} class.
1000      *
1001      * @return {@code TRUE} if the launcher supports this feature.  Note the API will return without
1002      *    waiting for the user to respond, so getting {@code TRUE} from this API does *not* mean
1003      *    the shortcut was pinned successfully.  {@code FALSE} if the launcher doesn't support this
1004      *    feature.
1005      *
1006      * @see #isRequestPinShortcutSupported()
1007      * @see IntentSender
1008      * @see android.app.PendingIntent#getIntentSender()
1009      *
1010      * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
1011      * @throws IllegalStateException The caller doesn't have a foreground activity or a foreground
1012      * service, or the device is locked.
1013      */
requestPinShortcut(@onNull ShortcutInfo shortcut, @Nullable IntentSender resultIntent)1014     public boolean requestPinShortcut(@NonNull ShortcutInfo shortcut,
1015             @Nullable IntentSender resultIntent) {
1016         try {
1017             return mService.requestPinShortcut(mContext.getPackageName(), shortcut,
1018                     resultIntent, injectMyUserId());
1019         } catch (RemoteException e) {
1020             throw e.rethrowFromSystemServer();
1021         }
1022     }
1023 
1024     /**
1025      * Returns an Intent which can be used by the default launcher to pin a shortcut containing the
1026      * given {@link ShortcutInfo}. This method should be used by an Activity to set a result in
1027      * response to {@link Intent#ACTION_CREATE_SHORTCUT}.
1028      *
1029      * @param shortcut New shortcut to pin.  If an app wants to pin an existing (either dynamic
1030      *     or manifest) shortcut, then it only needs to have an ID, and other fields don't have to
1031      *     be set, in which case, the target shortcut must be enabled.
1032      *     If it's a new shortcut, all the mandatory fields, such as a short label, must be
1033      *     set.
1034      * @return The intent that should be set as the result for the calling activity, or
1035      *     <code>null</code> if the current launcher doesn't support shortcuts.
1036      *
1037      * @see Intent#ACTION_CREATE_SHORTCUT
1038      *
1039      * @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
1040      */
createShortcutResultIntent(@onNull ShortcutInfo shortcut)1041     public Intent createShortcutResultIntent(@NonNull ShortcutInfo shortcut) {
1042         try {
1043             return mService.createShortcutResultIntent(mContext.getPackageName(), shortcut,
1044                     injectMyUserId());
1045         } catch (RemoteException e) {
1046             throw e.rethrowFromSystemServer();
1047         }
1048     }
1049 
1050     /**
1051      * Called internally when an app is considered to have come to the foreground
1052      * even when technically it's not.  This method resets the throttling for this package.
1053      * For example, when the user sends an "inline reply" on a notification, the system UI will
1054      * call it.
1055      *
1056      * @hide
1057      */
onApplicationActive(@onNull String packageName, @UserIdInt int userId)1058     public void onApplicationActive(@NonNull String packageName, @UserIdInt int userId) {
1059         try {
1060             mService.onApplicationActive(packageName, userId);
1061         } catch (RemoteException e) {
1062             throw e.rethrowFromSystemServer();
1063         }
1064     }
1065 
1066     /** @hide injection point */
1067     @VisibleForTesting
injectMyUserId()1068     protected int injectMyUserId() {
1069         return UserHandle.myUserId();
1070     }
1071 }
1072