Skip to content

Commit 139e64d

Browse files
committed
feat: building android browser
1 parent 7cc9a41 commit 139e64d

File tree

1 file changed

+55
-52
lines changed

1 file changed

+55
-52
lines changed

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

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,32 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
4444
return "NativeWebBrowser"
4545
}
4646

47-
// Example method
48-
// See https://reactnative.dev/docs/native-modules-android
4947
@ReactMethod
50-
fun multiply(a: Int, b: Int, promise: Promise) {
51-
52-
promise.resolve(a * b)
53-
54-
}
55-
56-
@ExpoMethod
5748
fun warmUpAsync(packageName: String?, promise: Promise) {
5849
try {
59-
val packageName = givenOrPreferredPackageName(packageName)
60-
customTabsConnectionHelper.warmUp(packageName)
50+
val processedPackageName = givenOrPreferredPackageName(packageName)
51+
customTabsConnectionHelper.warmUp(processedPackageName)
6152
val result = Bundle()
6253
result.putString(
63-
expo.modules.webbrowser.WebBrowserModule.SERVICE_PACKAGE_KEY,
64-
packageName
54+
SERVICE_PACKAGE_KEY,
55+
processedPackageName
6556
)
6657
promise.resolve(result)
6758
} catch (ex: NoPreferredPackageFound) {
6859
promise.reject(ex)
6960
}
7061
}
7162

72-
@ExpoMethod
63+
@ReactMethod
7364
fun coolDownAsync(packageName: String?, promise: Promise) {
74-
var packageName = packageName
65+
var processedPackageName = packageName
7566
try {
76-
packageName = givenOrPreferredPackageName(packageName)
77-
if (mConnectionHelper.coolDown(packageName)) {
67+
processedPackageName = givenOrPreferredPackageName(processedPackageName)
68+
if (customTabsConnectionHelper.coolDown(processedPackageName)) {
7869
val result = Bundle()
7970
result.putString(
80-
expo.modules.webbrowser.WebBrowserModule.SERVICE_PACKAGE_KEY,
81-
packageName
71+
SERVICE_PACKAGE_KEY,
72+
processedPackageName
8273
)
8374
promise.resolve(result)
8475
} else {
@@ -89,57 +80,62 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
8980
}
9081
}
9182

92-
@ExpoMethod
83+
@ReactMethod
9384
fun mayInitWithUrlAsync(
9485
url: String?,
9586
packageName: String?,
9687
promise: Promise
9788
) {
98-
var packageName = packageName
89+
var processedPackageName = packageName
9990
try {
100-
packageName = givenOrPreferredPackageName(packageName)
101-
mConnectionHelper.mayInitWithUrl(packageName, Uri.parse(url))
91+
processedPackageName = givenOrPreferredPackageName(processedPackageName)
92+
customTabsConnectionHelper.mayInitWithUrl(
93+
processedPackageName,
94+
Uri.parse(url)
95+
)
10296
val result = Bundle()
10397
result.putString(
104-
expo.modules.webbrowser.WebBrowserModule.SERVICE_PACKAGE_KEY,
105-
packageName
98+
SERVICE_PACKAGE_KEY,
99+
processedPackageName
106100
)
107101
promise.resolve(result)
108102
} catch (ex: NoPreferredPackageFound) {
109103
promise.reject(ex)
110104
}
111105
}
112106

113-
@ExpoMethod
107+
@ReactMethod
114108
fun getCustomTabsSupportingBrowsersAsync(promise: Promise) {
115109
try {
116110
val activities: ArrayList<String> =
117-
mCustomTabsResolver.getCustomTabsResolvingActivities()
111+
customTabsActivitiesHelper.customTabsResolvingActivities
118112
val services: ArrayList<String> =
119-
mCustomTabsResolver.getCustomTabsResolvingServices()
120-
val preferredPackage: String =
121-
mCustomTabsResolver.getPreferredCustomTabsResolvingActivity(activities)
122-
val defaultPackage: String =
123-
mCustomTabsResolver.getDefaultCustomTabsResolvingActivity()
113+
customTabsActivitiesHelper.customTabsResolvingServices
114+
val preferredPackage: String? =
115+
customTabsActivitiesHelper.getPreferredCustomTabsResolvingActivity(
116+
activities
117+
)
118+
val defaultPackage: String? =
119+
customTabsActivitiesHelper.defaultCustomTabsResolvingActivity
124120
var defaultCustomTabsPackage: String? = null
125121
if (activities.contains(defaultPackage)) { // It might happen, that default activity does not support Chrome Tabs. Then it will be ResolvingActivity and we don't want to return it as a result.
126122
defaultCustomTabsPackage = defaultPackage
127123
}
128124
val result = Bundle()
129125
result.putStringArrayList(
130-
expo.modules.webbrowser.WebBrowserModule.BROWSER_PACKAGES_KEY,
126+
BROWSER_PACKAGES_KEY,
131127
activities
132128
)
133129
result.putStringArrayList(
134-
expo.modules.webbrowser.WebBrowserModule.SERVICE_PACKAGES_KEY,
130+
SERVICE_PACKAGES_KEY,
135131
services
136132
)
137133
result.putString(
138-
expo.modules.webbrowser.WebBrowserModule.PREFERRED_BROWSER_PACKAGE,
134+
PREFERRED_BROWSER_PACKAGE,
139135
preferredPackage
140136
)
141137
result.putString(
142-
expo.modules.webbrowser.WebBrowserModule.DEFAULT_BROWSER_PACKAGE,
138+
DEFAULT_BROWSER_PACKAGE,
143139
defaultCustomTabsPackage
144140
)
145141
promise.resolve(result)
@@ -161,23 +157,23 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
161157
* showInRecents: Boolean;
162158
* @param promise
163159
*/
164-
@ExpoMethod
160+
@ReactMethod
165161
fun openBrowserAsync(
166162
url: String?,
167-
arguments: ReadableArguments,
163+
arguments: ReadableMap,
168164
promise: Promise
169165
) {
170166
val intent = createCustomTabsIntent(arguments)
171167
intent.data = Uri.parse(url)
172168
try {
173-
if (mCustomTabsResolver.canResolveIntent(intent)) {
174-
mCustomTabsResolver.startCustomTabs(intent)
169+
if (customTabsActivitiesHelper.canResolveIntent(intent)) {
170+
customTabsActivitiesHelper.startCustomTabs(intent)
175171
val result = Bundle()
176172
result.putString("type", "opened")
177173
promise.resolve(result)
178174
} else {
179175
promise.reject(
180-
expo.modules.webbrowser.WebBrowserModule.ERROR_CODE,
176+
ERROR_CODE,
181177
"No matching activity!"
182178
)
183179
}
@@ -208,13 +204,10 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
208204
} catch (ignored: IllegalArgumentException) {
209205
}
210206
builder.setShowTitle(
211-
if (!arguments.hasKey(SHOW_TITLE_KEY) || arguments.isNull(SHOW_TITLE_KEY)) {
207+
arguments.getBooleanWithDefault(
208+
SHOW_TITLE_KEY,
212209
false
213-
} else {
214-
arguments.getBoolean(
215-
SHOW_TITLE_KEY
216-
)
217-
}
210+
)
218211
)
219212
if (arguments.hasKey(DEFAULT_SHARE_MENU_ITEM) && arguments.getBoolean(
220213
DEFAULT_SHARE_MENU_ITEM
@@ -227,21 +220,21 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
227220
// We cannot use builder's method enableUrlBarHiding, because there is no corresponding disable method and some browsers enables it by default.
228221
intent.putExtra(
229222
CustomTabsIntent.EXTRA_ENABLE_URLBAR_HIDING,
230-
arguments.getBoolean(
223+
arguments.getBooleanWithDefault(
231224
ENABLE_BAR_COLLAPSING_KEY,
232225
false
233226
)
234227
)
235228
if (!TextUtils.isEmpty(packageName)) {
236229
intent.setPackage(packageName)
237230
}
238-
if (arguments.getBoolean(
231+
if (arguments.getBooleanWithDefault(
239232
CREATE_TASK,
240233
true
241234
)
242235
) {
243236
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
244-
if (!arguments.getBoolean(
237+
if (!arguments.getBooleanWithDefault(
245238
SHOW_IN_RECENTS,
246239
false
247240
)
@@ -253,7 +246,7 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
253246
return intent
254247
}
255248

256-
private fun givenOrPreferredPackageName(packageName: String?): String? {
249+
private fun givenOrPreferredPackageName(packageName: String?): String {
257250
var processedPackageName = packageName
258251
try {
259252
if (TextUtils.isEmpty(processedPackageName)) {
@@ -267,11 +260,21 @@ class NativeWebBrowserModule(reactContext: ReactApplicationContext) :
267260
} catch (ex: PackageManagerNotFoundException) {
268261
throw NoPreferredPackageFound(NO_PREFERRED_PACKAGE_MSG)
269262
}
270-
if (TextUtils.isEmpty(processedPackageName)) {
263+
if (TextUtils.isEmpty(processedPackageName) || processedPackageName == null) {
271264
throw NoPreferredPackageFound(NO_PREFERRED_PACKAGE_MSG)
272265
}
273266
return processedPackageName
274267
}
275268

276269

277270
}
271+
272+
fun ReadableMap.getBooleanWithDefault(
273+
name: String,
274+
defaultValue: Boolean
275+
): Boolean {
276+
if (!this.hasKey(name) || this.isNull(name)) {
277+
return defaultValue
278+
}
279+
return this.getBoolean(name)
280+
}

0 commit comments

Comments
 (0)