Skip to content

Commit 04f4a4a

Browse files
adding support for android in-app browser
1 parent de88326 commit 04f4a4a

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

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

+2-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

@@ -13,7 +14,7 @@ interface CustomTabsActivitiesHelper {
1314

1415
val defaultCustomTabsResolvingActivity: String?
1516

16-
fun startCustomTabs(intent: Intent)
17+
fun startCustomTabs(intent: Intent, url: Uri?)
1718

1819
fun canResolveIntent(intent: Intent): Boolean
1920
}

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

+22-2
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 {
@@ -54,8 +56,26 @@ class InternalCustomTabsActivitiesHelper(val activityProvider: ActivityProvider)
5456
return getResolvingActivities(intent).isNotEmpty()
5557
}
5658

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

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
166166
intent.data = Uri.parse(url)
167167
try {
168168
if (customTabsActivitiesHelper.canResolveIntent(intent)) {
169-
customTabsActivitiesHelper.startCustomTabs(intent)
169+
customTabsActivitiesHelper.startCustomTabs(intent, Uri.parse(url))
170170
val result = Arguments.createMap()
171171
result.putString("type", "opened")
172172
promise.resolve(result)

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)