in SharingShortcuts/Application/src/main/java/com/example/android/sharingshortcuts/SharingShortcutsManager.java [69:102]
public void pushDirectShareTargets(@NonNull Context context) {
ArrayList<ShortcutInfoCompat> shortcuts = new ArrayList<>();
// Category that our sharing shortcuts will be assigned to
Set<String> contactCategories = new HashSet<>();
contactCategories.add(CATEGORY_TEXT_SHARE_TARGET);
// Adding maximum number of shortcuts to the list
for (int id = 0; id < MAX_SHORTCUTS; ++id) {
Contact contact = Contact.byId(id);
// Item that will be sent if the shortcut is opened as a static launcher shortcut
Intent staticLauncherShortcutIntent = new Intent(Intent.ACTION_DEFAULT);
// Creates a new Sharing Shortcut and adds it to the list
// The id passed in the constructor will become EXTRA_SHORTCUT_ID in the received Intent
shortcuts.add(new ShortcutInfoCompat.Builder(context, Integer.toString(id))
.setShortLabel(contact.getName())
// Icon that will be displayed in the share target
.setIcon(IconCompat.createWithResource(context, contact.getIcon()))
.setIntent(staticLauncherShortcutIntent)
// Make this sharing shortcut cached by the system
// Even if it is unpublished, it can still appear on the sharesheet
.setLongLived()
.setCategories(contactCategories)
// Person objects are used to give better suggestions
.setPerson(new Person.Builder()
.setName(contact.getName())
.build())
.build());
}
ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts);
}