Skip to content

Commit bea98fa

Browse files
authored
Merge pull request #367 from alabiaga/main
Add share callback example
2 parents c452fac + 65edf7c commit bea98fa

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

samples/user-interface/share/src/main/java/com/example/platform/ui/share/sender/ShareResultReceiver.kt

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,54 @@ import android.content.BroadcastReceiver
2020
import android.content.ComponentName
2121
import android.content.Context
2222
import android.content.Intent
23+
import android.os.Build
24+
import android.service.chooser.ChooserResult
25+
import android.service.chooser.ChooserResult.CHOOSER_RESULT_COPY
26+
import android.service.chooser.ChooserResult.CHOOSER_RESULT_EDIT
27+
import android.service.chooser.ChooserResult.CHOOSER_RESULT_SELECTED_COMPONENT
28+
import android.service.chooser.ChooserResult.CHOOSER_RESULT_UNKNOWN
2329
import android.util.Log
2430
import androidx.annotation.RequiresApi
2531
import androidx.core.content.IntentCompat
2632

33+
2734
private const val TAG = "ShareResultReceiver"
2835

2936
@RequiresApi(22)
3037
class ShareResultReceiver : BroadcastReceiver() {
3138

3239
override fun onReceive(context: Context, intent: Intent) {
33-
// This ComponentName represents the Activity that has received the data we shared.
34-
val componentName: ComponentName? = IntentCompat.getParcelableExtra(
35-
intent,
36-
Intent.EXTRA_CHOSEN_COMPONENT,
37-
ComponentName::class.java,
38-
)
39-
// ...
40-
Log.d(TAG, "componentName: $componentName")
40+
if (Build.VERSION.SDK_INT >= 35) {
41+
val chooserResult: ChooserResult? = IntentCompat.getParcelableExtra(
42+
intent,
43+
Intent.EXTRA_CHOOSER_RESULT,
44+
ChooserResult::class.java,
45+
)
46+
if (chooserResult != null) {
47+
Log.i(TAG, "isShortcut: ${chooserResult.isShortcut}")
48+
Log.i(TAG, "type: ${typeToString(chooserResult.type)}")
49+
Log.i(TAG, "componentName: ${chooserResult.selectedComponent}")
50+
} else {
51+
Log.i(TAG, "chooserResult is null")
52+
}
53+
} else {
54+
// This ComponentName represents the Activity that has received the data we shared.
55+
val componentName: ComponentName? = IntentCompat.getParcelableExtra(
56+
intent,
57+
Intent.EXTRA_CHOSEN_COMPONENT,
58+
ComponentName::class.java,
59+
)
60+
Log.d(TAG, "componentName: $componentName")
61+
}
62+
}
63+
64+
private fun typeToString(type: Int): String {
65+
return when (type) {
66+
CHOOSER_RESULT_SELECTED_COMPONENT -> "SELECTED_COMPONENT"
67+
CHOOSER_RESULT_COPY -> "COPY"
68+
CHOOSER_RESULT_EDIT -> "EDIT"
69+
CHOOSER_RESULT_UNKNOWN -> "UNKNOWN"
70+
else -> "UNKNOWN"
71+
}
4172
}
4273
}

samples/user-interface/share/src/main/java/com/example/platform/ui/share/sender/ShareSender.kt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fun ShareSender() {
5252
) {
5353
val context = LocalContext.current
5454
Title(text = "Send various data")
55-
ShareButton(text = "Share plain text") { sharePlainText(context) }
55+
ShareButton(text = "Share plain text (Demonstrates share callback)") { sharePlainText(context) }
5656
ShareButton(text = "Share rich text") { shareRichText(context) }
5757
ShareButton(text = "Share image") { shareImage(context) }
5858
ShareButton(text = "Share multiple images") { shareMultipleImages(context) }
@@ -99,13 +99,30 @@ private val filenames = listOf(
9999
"night_highrise.jpg",
100100
)
101101

102+
/**
103+
* Shares a text message and demonstrates how to set up a BroadcastReceiver to be
104+
* notified who has received the data we shared. See [ShareResultReceiver].
105+
*/
102106
private fun sharePlainText(context: Context) {
103-
context.startActivity(
104-
ShareCompat.IntentBuilder(context)
107+
if (Build.VERSION.SDK_INT >= 22) {
108+
val share = Intent(Intent.ACTION_SEND)
109+
share.setType("text/plain")
110+
share.putExtra(Intent.EXTRA_TEXT,"Hello, world!")
111+
val pendingIntent = PendingIntent.getBroadcast(
112+
context, 1234,
113+
Intent(context, ShareResultReceiver::class.java),
114+
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
115+
)
116+
context.startActivity(
117+
Intent.createChooser(share, null, pendingIntent.intentSender)
118+
)
119+
} else {
120+
context.startActivity(ShareCompat.IntentBuilder(context)
105121
.setType("text/plain")
106122
.setText("Hello, world!")
107-
.createChooserIntent(),
108-
)
123+
.createChooserIntent()
124+
)
125+
}
109126
}
110127

111128
private fun shareRichText(context: Context) {

0 commit comments

Comments
 (0)