1<?xml version="1.0" encoding="UTF-8"?><!--
2 Copyright 2013 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
17<sample>
18    <name>SharingShortcuts</name>
19    <group>Content</group>
20    <package>com.example.android.sharingshortcuts</package>
21
22    <minSdk>21</minSdk>
23
24    <!-- Include additional dependencies here.-->
25    <dependency>androidx.appcompat:appcompat:1.1.0-alpha03</dependency>
26    <dependency>androidx.sharetarget:sharetarget:1.0.0-alpha01</dependency>
27    <dependency>androidx.recyclerview:recyclerview:1.0.0</dependency>
28
29    <strings>
30        <intro>
31            <![CDATA[
32This sample demonstrates how to provide the Sharing Shortcuts - Direct Share feature. The app shows
33some options directly in the list of share intent candidates and launcher shortcuts.
34            ]]>
35        </intro>
36    </strings>
37
38    <template src="base" />
39    <androidX>true</androidX>
40
41    <metadata>
42        <!-- Values: {DRAFT | PUBLISHED | INTERNAL | DEPRECATED | SUPERCEDED} -->
43        <status>DRAFT</status>
44        <categories>Content</categories>
45        <technologies>Android</technologies>
46        <languages>Java</languages>
47        <solutions>Mobile</solutions>
48        <level>INTERMEDIATE</level>
49        <icon>screenshots/icon-web.png</icon>
50        <screenshots>
51            <img>screenshots/1-main.png</img>
52            <img>screenshots/2-intent.png</img>
53            <img>screenshots/3-message.png</img>
54            <img>screenshots/4-static_shortcuts.png</img>
55        </screenshots>
56
57        <api_refs>
58            <androidx>androidx.core.content.pm.ShortcutManagerCompat</androidx>
59            <androidx>androidx.core.content.pm.ShortcutInfoCompat</androidx>
60        </api_refs>
61        <description>
62            <![CDATA[
63Sample demonstrating how to show some options directly in the list of share intent candidates.
64            ]]>
65        </description>
66
67        <intro>
68            <![CDATA[
69Direct Share is a feature that allows apps to show their internal options directly in the
70system Intent chooser dialog. This sample is a dummy messaging app, and just like any other
71messaging apps, it receives intents for sharing a plain text. When a user shares some text from some
72other app, this sample app will be listed as an option. Using the Direct Share feature, this app
73also shows some of contacts directly in the chooser dialog.
74
75Direct Share was [first introduced][1] in Android M where you had to implement a service to provide
76direct share targets on demand. The way to do that changed in Android Q, you need to provide your
77direct share targets in advance. First, you have to declare share-target elements in the same
78application's resource file than [static shortcuts][2]. Then, you need to publish dynamic
79shortcuts with the same category you declared in the share-target with the [ShortcutManager API][3].
80You will need to manually update the list of shortcuts every time you consider it appropriate.
81The API offers methods to update, remove or add shortcuts. You can use the
82[ShortcutInfoCompat.Builder][4] to customize your shortcut. If you don't want to block the UI thread
83doing these operations, interactions with the ShortcutManager can also happen on a background thread.
84
85There are three ways the app is sending/receiving intents:
86- Dynamic Share: The user selected the app in the sharesheet to receive the text. After this,
87the user will have to select the contact to share the text with. In our case, the app receives an
88intent of type Intent.ACTION_SEND.
89- Direct Share: The user selected a person of your app in the sharesheet to share the text with.
90The received intent of type Intent.ACTION_SEND will contain a String EXTRA_SHORTCUT_ID that will
91have the id of the shortcut that was selected. After this, the app is ready to send the text.
92- Launcher shortcut: When the user taps on a launcher shortcut, the intent that was
93added to the shortcut will get fired. In our case, it triggers an intent of type Intent.ACTION_DEFAULT.
94
95To make Direct Share backwards compatible with older Android versions, you need to add the AndroidX
96sharetarget library and in your AndroidManifest.xml, add a meta-data tag in your Activity that
97receives the Intent. Specify android:name as android.service.chooser.chooser_target_service and
98android:value as androidx.sharetarget.ChooserTargetServiceCompat.
99
100The way to share text has also changed. Before, you could specify a title in the
101[Intent.createChooser()][5] method itself. That is deprecated and unused in Android Q. You can achieve
102the same behavior by adding an Intent.EXTRA_TITLE extra to the intent. Similarly, if you want a
103preview thumbnail to appear, you can create a content URI and set a ClipData object in the intent.
104You can see how to do that in our example, open the MainActivity.java file for more details.
105
106[1]: https://developer.android.com/about/versions/marshmallow/android-6.0#direct-share
107[2]: https://developer.android.com/guide/topics/ui/shortcuts/creating-shortcuts
108[3]: https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat.html
109[4]: https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder.html
110[5]: https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence)
111            ]]>
112        </intro>
113    </metadata>
114</sample>
115