Skip to content

Instantly share code, notes, and snippets.

@Kevinrob
Forked from mediavrog/gist:5625602
Last active September 30, 2020 16:16
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Filter out Intents you don"t want to show from a IntentChooser dialog. For example your own app, competing apps or just apps you have a share integration by SDK already :)Based on http://stackoverflow.com/questions/5734678/custom-filtering-of-intent-chooser-based-on-installed-android-package-name/8550043#8550043
fun Intent.createChooserFiltered(context: Context, filter: (ResolveInfo) -> Boolean): Intent {
val results = context.packageManager.queryIntentActivities(this, 0)
.filter { filter(it) }
.toMutableList()
if (results.isNotEmpty()) {
val intents = results.map {
(this.clone() as Intent).apply {
`package` = it.activityInfo.packageName
setClassName(
it.activityInfo.packageName,
it.activityInfo.name
)
}
}.toMutableList()
val chooserIntent = Intent.createChooser(
intents.removeAt(intents.size - 1), null
)
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
intents.toTypedArray()
)
return chooserIntent
} else {
return Intent.createChooser(this, null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment