-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOSIABWebViewRouterAdapter.kt
More file actions
133 lines (122 loc) · 4.99 KB
/
OSIABWebViewRouterAdapter.kt
File metadata and controls
133 lines (122 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.outsystems.plugins.inappbrowser.osinappbrowserlib.routeradapters
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.RequiresEventBridgeRegistration
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.helpers.OSIABFlowHelperInterface
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.models.OSIABWebViewOptions
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABWebViewActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.util.UUID
class OSIABWebViewRouterAdapter(
context: Context,
lifecycleScope: CoroutineScope,
options: OSIABWebViewOptions,
flowHelper: OSIABFlowHelperInterface,
onBrowserPageLoaded: () -> Unit,
onBrowserFinished: () -> Unit,
private val onBrowserPageNavigationCompleted: (String?) -> Unit,
private val customHeaders: Map<String, String>? = null
) : OSIABBaseRouterAdapter<OSIABWebViewOptions, Boolean>(
context = context,
lifecycleScope = lifecycleScope,
options = options,
flowHelper = flowHelper,
onBrowserPageLoaded = onBrowserPageLoaded,
onBrowserFinished = onBrowserFinished,
) {
private val browserId = UUID.randomUUID().toString()
companion object {
const val WEB_VIEW_URL_EXTRA = "WEB_VIEW_URL_EXTRA"
const val WEB_VIEW_OPTIONS_EXTRA = "WEB_VIEW_OPTIONS_EXTRA"
const val CUSTOM_HEADERS_EXTRA = "CUSTOM_HEADERS_EXTRA"
}
private var isFinished = false
private fun finalizeBrowser() {
if (!isFinished) {
isFinished = true
onBrowserFinished()
OSIABEvents.unregisterReceiver(context)
}
}
/**
* Closes the WebView by sending a broadcast to the separate process.
* The WebView activity will receive this and call finish() on itself.
*/
@OptIn(RequiresEventBridgeRegistration::class)
override fun close(completionHandler: (Boolean) -> Unit) {
if (isFinished) {
completionHandler(true)
return
}
// Listen for the BrowserFinished event to confirm close
var closeJob: Job? = null
closeJob = flowHelper.listenToEvents(browserId, lifecycleScope) { event ->
if (event is OSIABEvents.BrowserFinished) {
finalizeBrowser()
completionHandler(true)
closeJob?.cancel()
}
}
// Send close broadcast to the WebView process
val closeIntent = Intent(OSIABEvents.ACTION_CLOSE_WEBVIEW).apply {
setPackage(context.packageName)
putExtra(OSIABEvents.EXTRA_BROWSER_ID, browserId)
}
context.sendBroadcast(closeIntent)
}
/**
* Handles opening the passed `url` in the WebView.
* @param url URL to be opened.
* @param completionHandler The callback with the result of opening the url.
*/
@OptIn(RequiresEventBridgeRegistration::class)
override fun handleOpen(url: String, completionHandler: (Boolean) -> Unit) {
lifecycleScope.launch {
var eventsJob: Job? = null
try {
// Collect the browser events
OSIABEvents.registerReceiver(context)
eventsJob = flowHelper.listenToEvents(browserId, lifecycleScope) { event ->
when (event) {
is OSIABEvents.OSIABWebViewEvent -> {
completionHandler(true)
}
is OSIABEvents.BrowserPageLoaded -> {
onBrowserPageLoaded()
}
is OSIABEvents.BrowserFinished -> {
finalizeBrowser()
eventsJob?.cancel()
}
is OSIABEvents.BrowserPageNavigationCompleted -> {
onBrowserPageNavigationCompleted(event.url)
}
else -> {}
}
}
context.startActivity(
Intent(
context, OSIABWebViewActivity::class.java
).apply {
putExtra(OSIABEvents.EXTRA_BROWSER_ID, browserId)
putExtra(WEB_VIEW_URL_EXTRA, url)
putExtra(WEB_VIEW_OPTIONS_EXTRA, options)
putExtra(CUSTOM_HEADERS_EXTRA, Bundle().apply {
customHeaders?.forEach { (key, value) ->
putString(key, value)
}
})
}
)
} catch (e: Exception) {
eventsJob?.cancel()
OSIABEvents.unregisterReceiver(context)
completionHandler(false)
}
}
}
}