Skip to content

Commit dfd6592

Browse files
committed
Enhance Android browser fallback logic and add package resolution for Chrome
1 parent 001c69c commit dfd6592

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ Lightning-fast, modern in-app browser for React Native powered by Nitro Modules.
4747

4848
> **Expo** is not supported because Nitro modules require native compilation.
4949
50+
## Android Browser Fallback
51+
52+
<details>
53+
<summary>Browser selection logic on Android</summary>
54+
55+
On Android, the library prioritizes Chrome Custom Tabs for the best user experience when Chrome is installed. If Chrome is not available (e.g., on Samsung devices), it falls back to opening the URL in the device's default web browser using the standard `Intent.ACTION_VIEW` mechanism.
56+
57+
This ensures compatibility across devices while maintaining optimal performance with Chrome when possible.
58+
</details>
59+
5060
## Installation
5161

5262
```sh

android/src/main/java/com/inappbrowsernitro/HybridInappbrowserNitro.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,16 @@ class HybridInappbrowserNitro : HybridInappbrowserNitroSpec() {
6262
val parsedUri = runCatching { Uri.parse(url) }.getOrNull()
6363
?: return dismiss("Invalid URL: $url")
6464

65-
val intent = CustomTabsIntentFactory(context, null).create(options)
65+
val customTabsPackage = CustomTabsPackageHelper.resolvePackage(context, null)
6666
val launchContext = reactContext?.currentActivity ?: context
67-
val launched = launchCustomTab(intent, launchContext, parsedUri)
6867

69-
if (launched) {
70-
return InAppBrowserResult(BrowserResultType.SUCCESS, parsedUri.toString(), null)
68+
if (customTabsPackage == "com.android.chrome") {
69+
val intent = CustomTabsIntentFactory(context, null).create(options)
70+
intent.intent.setPackage(customTabsPackage)
71+
val launched = launchCustomTab(intent, launchContext, parsedUri)
72+
if (launched) {
73+
return InAppBrowserResult(BrowserResultType.SUCCESS, parsedUri.toString(), null)
74+
}
7175
}
7276

7377
val fallbackLaunched = launchFallback(launchContext, url)

android/src/main/java/com/inappbrowsernitro/browser/CustomTabsPackageHelper.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.inappbrowsernitro.browser
22

33
import android.content.Context
4+
import android.content.pm.PackageManager
45
import androidx.browser.customtabs.CustomTabsClient
56

67
internal object CustomTabsPackageHelper {
@@ -9,6 +10,20 @@ internal object CustomTabsPackageHelper {
910
return preferred
1011
}
1112

13+
val chromePackage = "com.android.chrome"
14+
if (isPackageInstalled(context, chromePackage)) {
15+
return chromePackage
16+
}
17+
1218
return CustomTabsClient.getPackageName(context, null)
1319
}
20+
21+
private fun isPackageInstalled(context: Context, packageName: String): Boolean {
22+
return try {
23+
context.packageManager.getPackageInfo(packageName, 0)
24+
true
25+
} catch (e: PackageManager.NameNotFoundException) {
26+
false
27+
}
28+
}
1429
}

0 commit comments

Comments
 (0)