Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: List two more apps in the "open mail app" button #298

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!--For the "open mail app" button-->
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="mailto" />
</intent>
</queries>

<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,41 @@ fun Context.openAppNotificationSettings() {
}.also(::startActivity)
}


/**
* This method opens a chooser to let the user open an email app as if he clicked on it on his home screen, with an ACTION_MAIN.
*
* Create a chooser with two parts:
* 1) The mainActionIntent which lists email apps with a single intent
* 2) A list of additional intents `extraEmailAppIntents` added using [Intent.EXTRA_INITIAL_INTENTS]
*
* When creating a chooser after Android 10 a maximum of 3 intents can be provided to the chooser. Relying on 1) lets us display
* more than 3 apps but not many email apps support it. To counter this, we add two more intents from the list of all available
* email apps using [Intent.EXTRA_INITIAL_INTENTS].
*/
fun Context.openMailApp() {
val intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_EMAIL).apply {
// Returns the list of unique package names from the list of ResolveInfo that support the input Intent
fun queryIntentSupportedPackageNames(intent: Intent): Set<String> = buildSet {
packageManager
.queryIntentActivities(intent, 0)
.forEach { add(it.activityInfo.packageName) }
}

val mainActionIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_EMAIL).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
val mainPackageNames = queryIntentSupportedPackageNames(mainActionIntent)

val extraEmailAppIntents = queryIntentSupportedPackageNames(Intent(Intent.ACTION_VIEW, Uri.parse("mailto:")))
.filter { it !in mainPackageNames }
.mapNotNull { packageName ->
packageManager.getLaunchIntentForPackage(packageName)
}

safeStartActivity(Intent.createChooser(intent, getString(R.string.buttonOpenMailApp)))
val chooserIntent = Intent.createChooser(mainActionIntent, getString(R.string.buttonOpenMailApp)).apply {
putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailAppIntents.toTypedArray())
}
safeStartActivity(chooserIntent)
}

fun Context.copyText(text: String, showSnackbar: (String) -> Unit) {
Expand Down
Loading