1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content.pm;
import android.Manifest;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.annotation.UserIdInt;
import android.annotation.WorkerThread;
import android.app.Notification;
import android.app.usage.UsageStatsManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.graphics.drawable.AdaptiveIconDrawable;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.infra.AndroidFuture;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.concurrent.ExecutionException;
/**
* <p><code>ShortcutManager</code> executes operations on an app's set of <i>shortcuts</i>, which
* represent specific tasks and actions that users can perform within your app. This page lists
* components of the <code>ShortcutManager</code> class that you can use to create and manage
* sets of shortcuts.
*
* <p>To learn about methods that retrieve information about a single shortcut—including
* identifiers, type, and status—read the <code>
* <a href="/reference/android/content/pm/ShortcutInfo.html">ShortcutInfo</a></code> reference.
*
* <p>For guidance about using shortcuts, see
* <a href="/guide/topics/ui/shortcuts/index.html">App shortcuts</a>.
*
* <h3>Retrieving class instances</h3>
* <!-- Provides a heading for the content filled in by the @SystemService annotation below -->
*/
@SystemService(Context.SHORTCUT_SERVICE)
public class ShortcutManager {
private static final String TAG = "ShortcutManager";
/**
* Include manifest shortcuts in the result.
*
* @see #getShortcuts(int)
*/
public static final int FLAG_MATCH_MANIFEST = 1 << 0;
/**
* Include dynamic shortcuts in the result.
*
* @see #getShortcuts(int)
*/
public static final int FLAG_MATCH_DYNAMIC = 1 << 1;
/**
* Include pinned shortcuts in the result.
*
* @see #getShortcuts(int)
*/
public static final int FLAG_MATCH_PINNED = 1 << 2;
/**
* Include cached shortcuts in the result.
*
* @see #getShortcuts(int)
*/
public static final int FLAG_MATCH_CACHED = 1 << 3;
/** @hide */
@IntDef(flag = true, prefix = { "FLAG_MATCH_" }, value = {
FLAG_MATCH_MANIFEST,
FLAG_MATCH_DYNAMIC,
FLAG_MATCH_PINNED,
FLAG_MATCH_CACHED,
})
@Retention(RetentionPolicy.SOURCE)
public @interface ShortcutMatchFlags {}
private final Context mContext;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
private final IShortcutService mService;
/**
* @hide
*/
public ShortcutManager(Context context, IShortcutService service) {
mContext = context;
mService = service;
}
/**
* @hide
*/
@TestApi
public ShortcutManager(Context context) {
this(context, IShortcutService.Stub.asInterface(
ServiceManager.getService(Context.SHORTCUT_SERVICE)));
}
/**
* Publish the list of shortcuts. All existing dynamic shortcuts from the caller app
* will be replaced. If there are already pinned shortcuts with the same IDs,
* the mutable pinned shortcuts are updated.
*
* <p>This API will be rate-limited.
*
* @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
*
* @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
* or when trying to update immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
public boolean setDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
try {
return mService.setDynamicShortcuts(mContext.getPackageName(), new ParceledListSlice(
shortcutInfoList), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return all dynamic shortcuts from the caller app.
*
* <p>This API is intended to be used for examining what shortcuts are currently published.
* Re-publishing returned {@link ShortcutInfo}s via APIs such as
* {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
@NonNull
public List<ShortcutInfo> getDynamicShortcuts() {
try {
return mService.getShortcuts(mContext.getPackageName(),
FLAG_MATCH_DYNAMIC, injectMyUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return all static (manifest) shortcuts from the caller app.
*
* <p>This API is intended to be used for examining what shortcuts are currently published.
* Re-publishing returned {@link ShortcutInfo}s via APIs such as
* {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
@NonNull
public List<ShortcutInfo> getManifestShortcuts() {
try {
return mService.getShortcuts(mContext.getPackageName(),
FLAG_MATCH_MANIFEST, injectMyUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns {@link ShortcutInfo}s that match {@code matchFlags}.
*
* @param matchFlags result includes shortcuts matching this flags. Any combination of:
* <ul>
* <li>{@link #FLAG_MATCH_MANIFEST}
* <li>{@link #FLAG_MATCH_DYNAMIC}
* <li>{@link #FLAG_MATCH_PINNED}
* <li>{@link #FLAG_MATCH_CACHED}
* </ul>
* @return list of {@link ShortcutInfo}s that match the flag.
*
* <p>At least one of the {@code MATCH} flags should be set. Otherwise no shortcuts will be
* returned.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
@NonNull
public List<ShortcutInfo> getShortcuts(@ShortcutMatchFlags int matchFlags) {
try {
return mService.getShortcuts(mContext.getPackageName(), matchFlags,
injectMyUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Publish the list of dynamic shortcuts. If there are already dynamic or pinned shortcuts with
* the same IDs, each mutable shortcut is updated.
*
* <p>This API will be rate-limited.
*
* @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
*
* @throws IllegalArgumentException if {@link #getMaxShortcutCountPerActivity()} is exceeded,
* or when trying to update immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
public boolean addDynamicShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
try {
return mService.addDynamicShortcuts(mContext.getPackageName(),
new ParceledListSlice(shortcutInfoList), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Delete dynamic shortcuts by ID.
*
* @throws IllegalStateException when the user is locked.
*/
public void removeDynamicShortcuts(@NonNull List<String> shortcutIds) {
try {
mService.removeDynamicShortcuts(mContext.getPackageName(), shortcutIds,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Delete all dynamic shortcuts from the caller app.
*
* @throws IllegalStateException when the user is locked.
*/
public void removeAllDynamicShortcuts() {
try {
mService.removeAllDynamicShortcuts(mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Delete long lived shortcuts by ID.
*
* @throws IllegalStateException when the user is locked.
*/
public void removeLongLivedShortcuts(@NonNull List<String> shortcutIds) {
try {
mService.removeLongLivedShortcuts(mContext.getPackageName(), shortcutIds,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return all pinned shortcuts from the caller app.
*
* <p>This API is intended to be used for examining what shortcuts are currently published.
* Re-publishing returned {@link ShortcutInfo}s via APIs such as
* {@link #setDynamicShortcuts(List)} may cause loss of information such as icons.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
@NonNull
public List<ShortcutInfo> getPinnedShortcuts() {
try {
return mService.getShortcuts(mContext.getPackageName(), FLAG_MATCH_PINNED,
injectMyUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Update all existing shortcuts with the same IDs. Target shortcuts may be pinned and/or
* dynamic, but they must not be immutable.
*
* <p>This API will be rate-limited.
*
* @return {@code true} if the call has succeeded. {@code false} if the call is rate-limited.
*
* @throws IllegalArgumentException If trying to update immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
@WorkerThread
public boolean updateShortcuts(@NonNull List<ShortcutInfo> shortcutInfoList) {
try {
return mService.updateShortcuts(mContext.getPackageName(),
new ParceledListSlice(shortcutInfoList), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Disable pinned shortcuts. For more details, read
* <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#disable-shortcuts">
* Disable shortcuts</a>.
*
* @throws IllegalArgumentException If trying to disable immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
public void disableShortcuts(@NonNull List<String> shortcutIds) {
try {
mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
/* disabledMessage =*/ null, /* disabledMessageResId =*/ 0,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide old signature, kept for unit testing.
*/
public void disableShortcuts(@NonNull List<String> shortcutIds, int disabledMessageResId) {
try {
mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
/* disabledMessage =*/ null, disabledMessageResId,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide old signature, kept for unit testing.
*/
public void disableShortcuts(@NonNull List<String> shortcutIds, String disabledMessage) {
disableShortcuts(shortcutIds, (CharSequence) disabledMessage);
}
/**
* Disable pinned shortcuts, showing the user a custom error message when they try to select
* the disabled shortcuts.
* For more details, read
* <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#disable-shortcuts">
* Disable shortcuts</a>.
*
* @throws IllegalArgumentException If trying to disable immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
public void disableShortcuts(@NonNull List<String> shortcutIds, CharSequence disabledMessage) {
try {
mService.disableShortcuts(mContext.getPackageName(), shortcutIds,
disabledMessage, /* disabledMessageResId =*/ 0,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Re-enable pinned shortcuts that were previously disabled. If the target shortcuts
* are already enabled, this method does nothing.
*
* @throws IllegalArgumentException If trying to enable immutable shortcuts.
*
* @throws IllegalStateException when the user is locked.
*/
public void enableShortcuts(@NonNull List<String> shortcutIds) {
try {
mService.enableShortcuts(mContext.getPackageName(), shortcutIds, injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* @hide old signature, kept for unit testing.
*/
public int getMaxShortcutCountForActivity() {
return getMaxShortcutCountPerActivity();
}
/**
* Return the maximum number of static and dynamic shortcuts that each launcher icon
* can have at a time.
*/
public int getMaxShortcutCountPerActivity() {
try {
return mService.getMaxShortcutCountPerActivity(
mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return the number of times the caller app can call the rate-limited APIs
* before the rate limit counter is reset.
*
* @see #getRateLimitResetTime()
*
* @hide
*/
public int getRemainingCallCount() {
try {
return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return when the rate limit count will be reset next time, in milliseconds since the epoch.
*
* @see #getRemainingCallCount()
* @see System#currentTimeMillis()
*
* @hide
*/
public long getRateLimitResetTime() {
try {
return mService.getRateLimitResetTime(mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return {@code true} when rate-limiting is active for the caller app.
*
* <p>For details, see <a href="/guide/topics/ui/shortcuts/managing-shortcuts#rate-limiting">
* Rate limiting</a>.
*
* @throws IllegalStateException when the user is locked.
*/
public boolean isRateLimitingActive() {
try {
return mService.getRemainingCallCount(mContext.getPackageName(), injectMyUserId())
== 0;
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return the max width for icons, in pixels.
*
* <p> Note that this method returns max width of icon's visible part. Hence, it does not take
* into account the inset introduced by {@link AdaptiveIconDrawable}. To calculate bitmap image
* to function as {@link AdaptiveIconDrawable}, multiply
* 1 + 2 * {@link AdaptiveIconDrawable#getExtraInsetFraction()} to the returned size.
*/
public int getIconMaxWidth() {
try {
// TODO Implement it properly using xdpi.
return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return the max height for icons, in pixels.
*/
public int getIconMaxHeight() {
try {
// TODO Implement it properly using ydpi.
return mService.getIconMaxDimensions(mContext.getPackageName(), injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Apps that publish shortcuts should call this method whenever the user
* selects the shortcut containing the given ID or when the user completes
* an action in the app that is equivalent to selecting the shortcut.
* For more details, read about
* <a href="/guide/topics/ui/shortcuts/managing-shortcuts.html#track-usage">
* tracking shortcut usage</a>.
*
* <p>The information is accessible via {@link UsageStatsManager#queryEvents}
* Typically, launcher apps use this information to build a prediction model
* so that they can promote the shortcuts that are likely to be used at the moment.
*
* @throws IllegalStateException when the user is locked.
*/
public void reportShortcutUsed(String shortcutId) {
try {
mService.reportShortcutUsed(mContext.getPackageName(), shortcutId, injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Return {@code TRUE} if the app is running on a device whose default launcher supports
* {@link #requestPinShortcut(ShortcutInfo, IntentSender)}.
*
* <p>The return value may change in subsequent calls if the user changes the default launcher
* app.
*
* <p><b>Note:</b> See also the support library counterpart
* {@link androidx.core.content.pm.ShortcutManagerCompat#isRequestPinShortcutSupported(
* Context)}, which supports Android versions lower than {@link VERSION_CODES#O} using the
* legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
*
* @see #requestPinShortcut(ShortcutInfo, IntentSender)
*/
public boolean isRequestPinShortcutSupported() {
try {
return mService.isRequestPinItemSupported(injectMyUserId(),
LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Request to create a pinned shortcut. The default launcher will receive this request and
* ask the user for approval. If the user approves it, the shortcut will be created, and
* {@code resultIntent} will be sent. If a request is denied by the user, however, no response
* will be sent to the caller.
*
* <p>Only apps with a foreground activity or a foreground service can call this method.
* Otherwise, it'll throw {@link IllegalStateException}.
*
* <p>It's up to the launcher to decide how to handle previous pending requests when the same
* package calls this API multiple times in a row. One possible strategy is to ignore any
* previous requests.
*
* <p><b>Note:</b> See also the support library counterpart
* {@link androidx.core.content.pm.ShortcutManagerCompat#requestPinShortcut(
* Context, ShortcutInfoCompat, IntentSender)},
* which supports Android versions lower than {@link VERSION_CODES#O} using the
* legacy private intent {@code com.android.launcher.action.INSTALL_SHORTCUT}.
*
* @param shortcut Shortcut to pin. If an app wants to pin an existing (either static
* or dynamic) shortcut, then it only needs to have an ID. Although other fields don't have
* to be set, the target shortcut must be enabled.
*
* <p>If it's a new shortcut, all the mandatory fields, such as a short label, must be
* set.
* @param resultIntent If not null, this intent will be sent when the shortcut is pinned.
* Use {@link android.app.PendingIntent#getIntentSender()} to create an {@link IntentSender}.
* To avoid background execution limits, use an unexported, manifest-declared receiver.
* For more details, see
* <a href="/guide/topics/ui/shortcuts/creating-shortcuts.html#pinned">
* Creating pinned shortcuts</a>.
*
* @return {@code TRUE} if the launcher supports this feature. Note the API will return without
* waiting for the user to respond, so getting {@code TRUE} from this API does *not* mean
* the shortcut was pinned successfully. {@code FALSE} if the launcher doesn't support this
* feature.
*
* @see #isRequestPinShortcutSupported()
* @see IntentSender
* @see android.app.PendingIntent#getIntentSender()
*
* @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
* @throws IllegalStateException The caller doesn't have a foreground activity or a foreground
* service, or the device is locked.
*/
@WorkerThread
public boolean requestPinShortcut(@NonNull ShortcutInfo shortcut,
@Nullable IntentSender resultIntent) {
try {
AndroidFuture<String> ret = new AndroidFuture<>();
mService.requestPinShortcut(mContext.getPackageName(), shortcut, resultIntent,
injectMyUserId(), ret);
return Boolean.parseBoolean(getFutureOrThrow(ret));
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Returns an Intent which can be used by the default launcher to pin a shortcut containing the
* given {@link ShortcutInfo}. This method should be used by an Activity to set a result in
* response to {@link Intent#ACTION_CREATE_SHORTCUT}.
*
* @param shortcut New shortcut to pin. If an app wants to pin an existing (either dynamic
* or manifest) shortcut, then it only needs to have an ID, and other fields don't have to
* be set, in which case, the target shortcut must be enabled.
* If it's a new shortcut, all the mandatory fields, such as a short label, must be
* set.
* @return The intent that should be set as the result for the calling activity, or
* <code>null</code> if the current launcher doesn't support shortcuts.
*
* @see Intent#ACTION_CREATE_SHORTCUT
*
* @throws IllegalArgumentException if a shortcut with the same ID exists and is disabled.
*/
@WorkerThread
public Intent createShortcutResultIntent(@NonNull ShortcutInfo shortcut) {
final AndroidFuture<Intent> ret = new AndroidFuture<>();
try {
mService.createShortcutResultIntent(mContext.getPackageName(),
shortcut, injectMyUserId(), ret);
return getFutureOrThrow(ret);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Called internally when an app is considered to have come to the foreground
* even when technically it's not. This method resets the throttling for this package.
* For example, when the user sends an "inline reply" on a notification, the system UI will
* call it.
*
* @hide
*/
public void onApplicationActive(@NonNull String packageName, @UserIdInt int userId) {
try {
mService.onApplicationActive(packageName, userId);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** @hide injection point */
@VisibleForTesting
protected int injectMyUserId() {
return mContext.getUserId();
}
/**
* Used by framework's ShareSheet (ChooserActivity.java) to retrieve all of the direct share
* targets that match the given IntentFilter.
*
* @param filter IntentFilter that will be used to retrieve the matching {@link ShortcutInfo}s.
* @return List of {@link ShareShortcutInfo}s that match the given IntentFilter.
* @hide
*/
@WorkerThread
@NonNull
@SystemApi
@RequiresPermission(Manifest.permission.MANAGE_APP_PREDICTIONS)
public List<ShareShortcutInfo> getShareTargets(@NonNull IntentFilter filter) {
try {
return mService.getShareTargets(
mContext.getPackageName(), filter, injectMyUserId()).getList();
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Represents the result of a query return by {@link #getShareTargets(IntentFilter)}.
*
* @hide
*/
@SystemApi
public static final class ShareShortcutInfo implements Parcelable {
private final ShortcutInfo mShortcutInfo;
private final ComponentName mTargetComponent;
/**
* @hide
*/
public ShareShortcutInfo(@NonNull ShortcutInfo shortcutInfo,
@NonNull ComponentName targetComponent) {
if (shortcutInfo == null) {
throw new NullPointerException("shortcut info is null");
}
if (targetComponent == null) {
throw new NullPointerException("target component is null");
}
mShortcutInfo = shortcutInfo;
mTargetComponent = targetComponent;
}
private ShareShortcutInfo(@NonNull Parcel in) {
mShortcutInfo = in.readParcelable(ShortcutInfo.class.getClassLoader(), android.content.pm.ShortcutInfo.class);
mTargetComponent = in.readParcelable(ComponentName.class.getClassLoader(), android.content.ComponentName.class);
}
@NonNull
public ShortcutInfo getShortcutInfo() {
return mShortcutInfo;
}
@NonNull
public ComponentName getTargetComponent() {
return mTargetComponent;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
dest.writeParcelable(mShortcutInfo, flags);
dest.writeParcelable(mTargetComponent, flags);
}
public static final @NonNull Parcelable.Creator<ShareShortcutInfo> CREATOR =
new Parcelable.Creator<ShareShortcutInfo>() {
public ShareShortcutInfo createFromParcel(Parcel in) {
return new ShareShortcutInfo(in);
}
public ShareShortcutInfo[] newArray(int size) {
return new ShareShortcutInfo[size];
}
};
}
/**
* Used by framework's ShareSheet (ChooserActivity.java) to check if a given package has share
* target definitions in it's resources.
*
* @param packageName Package to check for share targets.
* @return True if the package has any share target definitions, False otherwise.
* @hide
*/
@SystemApi
public boolean hasShareTargets(@NonNull String packageName) {
try {
return mService.hasShareTargets(mContext.getPackageName(), packageName,
injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/**
* Publish a single dynamic shortcut. If there are already dynamic or pinned shortcuts with the
* same ID, each mutable shortcut is updated.
*
* <p>This method is useful when posting notifications which are tagged with shortcut IDs; In
* order to make sure shortcuts exist and are up-to-date, without the need to explicitly handle
* the shortcut count limit.
* @see android.app.NotificationManager#notify(int, Notification)
* @see android.app.Notification.Builder#setShortcutId(String)
*
* <p>If {@link #getMaxShortcutCountPerActivity()} is already reached, an existing shortcut with
* the lowest rank will be removed to add space for the new shortcut.
*
* <p>If the rank of the shortcut is not explicitly set, it will be set to zero, and shortcut
* will be added to the top of the list.
*
* @throws IllegalArgumentException if trying to update an immutable shortcut.
*
* @throws IllegalStateException when the user is locked.
*/
public void pushDynamicShortcut(@NonNull ShortcutInfo shortcut) {
try {
mService.pushDynamicShortcut(mContext.getPackageName(), shortcut, injectMyUserId());
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
private static <T> T getFutureOrThrow(@NonNull AndroidFuture<T> future) {
try {
return future.get();
} catch (Throwable e) {
if (e instanceof ExecutionException) {
e = e.getCause();
}
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
if (e instanceof Error) {
throw (Error) e;
}
throw new RuntimeException(e);
}
}
}
|