Skip to content

Commit 99edba4

Browse files
Merge pull request #13 from torusresearch/feat/android-inApp-browser-support
Adding support for android in-app browser
2 parents de88326 + 6f7e33e commit 99edba4

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

android/src/main/java/com/reactnativewebbrowser/CustomTabsActivitiesHelper.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.reactnativewebbrowser
22

33
import android.content.Intent
4+
import android.net.Uri
45
import java.util.ArrayList
56

67

android/src/main/java/com/reactnativewebbrowser/InternalCustomTabsActivitiesHelper.kt

+22-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import java.util.LinkedHashSet
1616
import androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
1717
import com.reactnativewebbrowser.error.CurrentActivityNotFoundException
1818
import com.reactnativewebbrowser.error.PackageManagerNotFoundException
19+
import com.reactnativewebbrowser.utilities.getCustomTabsBrowsers
20+
import com.reactnativewebbrowser.utilities.getDefaultBrowser
1921

2022
class InternalCustomTabsActivitiesHelper(val activityProvider: ActivityProvider) :
2123
CustomTabsActivitiesHelper {
@@ -55,7 +57,26 @@ class InternalCustomTabsActivitiesHelper(val activityProvider: ActivityProvider)
5557
}
5658

5759
override fun startCustomTabs(intent: Intent) {
58-
currentActivity.startActivity(intent)
60+
val defaultBrowser = currentActivity.getDefaultBrowser()
61+
val customTabsBrowsers = currentActivity.getCustomTabsBrowsers()
62+
63+
val url = intent.data
64+
if (customTabsBrowsers.contains(defaultBrowser)) {
65+
val customTabs = CustomTabsIntent.Builder().build()
66+
customTabs.intent.setPackage(defaultBrowser)
67+
if (url != null) {
68+
customTabs.launchUrl(currentActivity, url)
69+
}
70+
} else if (customTabsBrowsers.isNotEmpty()) {
71+
val customTabs = CustomTabsIntent.Builder().build()
72+
customTabs.intent.setPackage(customTabsBrowsers[0])
73+
if (url != null) {
74+
customTabs.launchUrl(currentActivity, url)
75+
}
76+
} else {
77+
// Open in browser externally
78+
currentActivity.startActivity(Intent(Intent.ACTION_VIEW, url))
79+
}
5980
}
6081

6182
private fun getResolvingActivities(intent: Intent): List<ResolveInfo> {

android/src/main/java/com/reactnativewebbrowser/utilities/KotlinUtilities.kt

+36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.reactnativewebbrowser.utilities
22

3+
import android.content.Context
4+
import android.content.Intent
5+
import android.content.pm.PackageManager
6+
import android.net.Uri
7+
import androidx.browser.customtabs.CustomTabsService
38
import com.facebook.react.bridge.Arguments
49
import com.facebook.react.bridge.WritableMap
510

@@ -21,3 +26,34 @@ fun WritableMap.putStringArrayList(name: String, strArr: ArrayList<String>) {
2126
}
2227
this.putArray(name, arr)
2328
}
29+
30+
val ALLOWED_CUSTOM_TABS_PACKAGES =
31+
arrayOf(
32+
"com.android.chrome", // Chrome stable
33+
"com.google.android.apps.chrome", // Chrome system
34+
"com.chrome.beta",// Chrome beta
35+
"com.chrome.dev" // Chrome dev
36+
)
37+
38+
fun Context.getDefaultBrowser(): String? {
39+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://web3auth.io"))
40+
val resolveInfo = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
41+
?: return null
42+
val activityInfo = resolveInfo.activityInfo ?: return null
43+
return activityInfo.packageName
44+
}
45+
46+
fun Context.getCustomTabsBrowsers(): List<String> {
47+
val customTabsBrowsers: MutableList<String> = java.util.ArrayList()
48+
for (browser in ALLOWED_CUSTOM_TABS_PACKAGES) {
49+
val customTabsIntent = Intent()
50+
customTabsIntent.action = CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
51+
customTabsIntent.setPackage(browser)
52+
53+
// Check if this package also resolves the Custom Tabs service.
54+
if (packageManager.resolveService(customTabsIntent, 0) != null) {
55+
customTabsBrowsers.add(browser)
56+
}
57+
}
58+
return customTabsBrowsers
59+
}

0 commit comments

Comments
 (0)