Skip to content

Commit

Permalink
Let ShareCompat.IntentBuilder create the chooser intent for sharing c…
Browse files Browse the repository at this point in the history
…ontent.

+ Although the system's intent chooser normally takes care of cases when
  no app is found we still keep the safeguard here to be prepared for
  exotic devices behaving different.

Signed-off-by: Tobias Preuss <[email protected]>
  • Loading branch information
johnjohndoe committed Dec 31, 2021
1 parent 70b0747 commit 74363ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.d/4745.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Open share UI provides by the system when sharing media or text.
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,28 @@ fun shareMedia(context: Context, file: File, mediaMimeType: String?) {
return
}

val sendIntent = ShareCompat.IntentBuilder(context)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType(mediaMimeType)
.setStream(mediaUri)
.intent
.setChooserTitle(R.string.share)
.createChooserIntent()

sendShareIntent(context, sendIntent)
createChooser(context, chooserIntent)
}

fun shareText(context: Context, text: String) {
val sendIntent = ShareCompat.IntentBuilder(context)
val chooserIntent = ShareCompat.IntentBuilder(context)
.setType("text/plain")
.setText(text)
.intent
.setChooserTitle(R.string.share)
.createChooserIntent()

sendShareIntent(context, sendIntent)
createChooser(context, chooserIntent)
}

private fun sendShareIntent(context: Context, intent: Intent) {
private fun createChooser(context: Context, intent: Intent) {
try {
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)))
context.startActivity(intent)
} catch (activityNotFoundException: ActivityNotFoundException) {
context.toast(R.string.error_no_external_application_found)
}
Expand Down

0 comments on commit 74363ff

Please sign in to comment.