Updated on 2026-08-14

This commit is contained in:
Tangem 2024-12-16 16:34:32 +05:00
parent 12e06e5fc1
commit a2d3ac38c9
5 changed files with 61 additions and 4 deletions

View file

@ -1,8 +1,11 @@
package com.tangem.tap.common.url
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsClient
import androidx.browser.customtabs.CustomTabsIntent
import androidx.browser.customtabs.CustomTabsIntent.COLOR_SCHEME_DARK
import androidx.browser.customtabs.CustomTabsIntent.COLOR_SCHEME_LIGHT
@ -12,6 +15,7 @@ import com.tangem.tap.common.extensions.getColorCompat
import com.tangem.tap.foregroundActivityObserver
import com.tangem.tap.withForegroundActivity
import com.tangem.wallet.R
import timber.log.Timber
internal class CustomTabsUrlOpener : UrlOpener {
@ -34,6 +38,37 @@ internal class CustomTabsUrlOpener : UrlOpener {
)
.build()
customTabsIntent.launchUrl(context, Uri.parse(url))
customTabsIntent.intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
runCatching {
if (checkCustomTabsAvailability(context, browserIntent)) {
context.startActivity(browserIntent)
} else {
customTabsIntent.launchUrl(context, Uri.parse(url))
}
}.onFailure {
Timber.e(it.message)
}
}
/**
* Custom Tabs compatibility check. Returns flag whether custom tabs are supported
* @see "https://developer.chrome.com/docs/android/custom-tabs/howto-custom-tab-check"
*/
private fun checkCustomTabsAvailability(context: Context, browserIntent: Intent): Boolean {
// Get all apps that can handle VIEW intents and Custom Tab service connections.
val resolveInfos = context.packageManager.queryIntentActivities(browserIntent, PackageManager.MATCH_ALL)
// Extract package names from ResolveInfo objects
val packageNames = mutableListOf<String>()
for (info in resolveInfos) {
packageNames.add(info.activityInfo.packageName)
}
// Get a package that supports Custom Tabs
val packageName = CustomTabsClient.getPackageName(context, packageNames, true)
return packageName == null // Custom Tabs are not supported by any browser on the device
}
}