commit 47f91f31f7d2e658979121f5e5590c02791577fa
parent d8245f2e3bda3f5abb99f285bcfd76d23174f640
Author: Iván Ávalos <avalos@disroot.org>
Date: Wed, 10 Jul 2024 13:00:29 -0600
[wallet] Hide “Open in banking app” button if no app (excl. Taler) can handle it
bug 0008996
Diffstat:
2 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt b/taler-kotlin-android/src/main/java/net/taler/common/AndroidUtils.kt
@@ -21,6 +21,7 @@ import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Context.CONNECTIVITY_SERVICE
import android.content.Intent
+import android.content.Intent.EXTRA_INITIAL_INTENTS
import android.net.ConnectivityManager
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
import android.net.Uri
@@ -121,12 +122,46 @@ fun Context.startActivitySafe(intent: Intent) {
}
}
-fun Context.openUri(uri: String, title: String) {
+fun Context.canAppHandleUri(uri: String): Boolean {
val intent = Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(uri)
}
- startActivitySafe(Intent.createChooser(intent, title))
+ return packageManager.queryIntentActivities(intent, 0).any {
+ it.activityInfo.packageName != packageName
+ }
+}
+
+fun Context.openUri(uri: String, title: String, excludeOwn: Boolean = true) {
+ val intent = Intent(Intent.ACTION_VIEW).apply {
+ data = Uri.parse(uri)
+ }
+
+ if (excludeOwn) {
+ val possiblePackageNames = mutableListOf<String>()
+ val possibleIntents = packageManager.queryIntentActivities(intent, 0).filter {
+ it.activityInfo.packageName != packageName
+ }.map {
+ val possibleIntent = Intent(intent)
+ possibleIntent.`package` = it.activityInfo.packageName
+ possiblePackageNames.add(it.activityInfo.packageName)
+ return@map possibleIntent
+ }
+
+ val defaultResolveInfo = packageManager.resolveActivity(intent, 0)
+ if (defaultResolveInfo == null || possiblePackageNames.isEmpty()) return
+
+ // If there is a default app to handle the intent (which is not the app), use it.
+ if (possiblePackageNames.contains(defaultResolveInfo.activityInfo.packageName)) {
+ startActivitySafe(intent)
+ } else {
+ val chooser = Intent.createChooser(possibleIntents[0], title)
+ chooser.putExtra(EXTRA_INITIAL_INTENTS, possibleIntents.drop(1).toTypedArray())
+ startActivitySafe(chooser)
+ }
+ } else {
+ startActivitySafe(Intent.createChooser(intent, title))
+ }
}
fun Context.shareText(text: String) {
diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenTransfer.kt b/wallet/src/main/java/net/taler/wallet/withdraw/manual/ScreenTransfer.kt
@@ -48,6 +48,7 @@ import net.taler.common.Amount
import net.taler.wallet.CURRENCY_BTC
import net.taler.wallet.R
import net.taler.common.CurrencySpecification
+import net.taler.common.canAppHandleUri
import net.taler.wallet.compose.ShareButton
import net.taler.wallet.compose.copyToClipBoard
import net.taler.wallet.transactions.AmountType
@@ -118,7 +119,8 @@ fun ScreenTransfer(
)
}
- if (bankAppClick != null) {
+ val paytoUri = selectedTransfer.withdrawalAccount.paytoUri
+ if (bankAppClick != null && LocalContext.current.canAppHandleUri(paytoUri)) {
Button(
onClick = { bankAppClick(selectedTransfer) },
modifier = Modifier