-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOSIABEvents.kt
More file actions
91 lines (77 loc) · 3.62 KB
/
OSIABEvents.kt
File metadata and controls
91 lines (77 loc) · 3.62 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
package com.outsystems.plugins.inappbrowser.osinappbrowserlib
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABWebViewActivity
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import java.io.Serializable
@RequiresOptIn(
level = RequiresOptIn.Level.WARNING,
message = "This API requires a prior call to OSIABEvents.registerReceiver(context) to work correctly with process isolation on Android 9+."
)
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
annotation class RequiresEventBridgeRegistration
sealed class OSIABEvents : Serializable {
abstract val browserId: String
data class BrowserPageLoaded(override val browserId: String) : OSIABEvents()
data class BrowserFinished(override val browserId: String) : OSIABEvents()
data class BrowserPageNavigationCompleted(override val browserId: String, val url: String?) : OSIABEvents()
data class OSIABCustomTabsEvent(
override val browserId: String,
val action: String,
val context: Context
) : OSIABEvents()
data class OSIABWebViewEvent(
override val browserId: String,
val activity: OSIABWebViewActivity? = null
) : OSIABEvents()
companion object {
const val EXTRA_BROWSER_ID = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_BROWSER_ID"
const val ACTION_IAB_EVENT = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.ACTION_IAB_EVENT"
const val EXTRA_EVENT_DATA = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_EVENT_DATA"
private val _events = MutableSharedFlow<OSIABEvents>(extraBufferCapacity = 64)
val events = _events.asSharedFlow()
private var receiver: BroadcastReceiver? = null
@SuppressLint("UnspecifiedRegisterReceiverFlag")
fun registerReceiver(context: Context) {
if (receiver != null) return
val appContext = context.applicationContext
receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == ACTION_IAB_EVENT) {
@Suppress("DEPRECATION")
val event = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
intent.getSerializableExtra(EXTRA_EVENT_DATA, OSIABEvents::class.java)
} else {
intent.getSerializableExtra(EXTRA_EVENT_DATA) as? OSIABEvents
}
event?.let {
_events.tryEmit(it)
}
}
}
}
val filter = IntentFilter(ACTION_IAB_EVENT)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
appContext.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED)
} else {
appContext.registerReceiver(receiver, filter)
}
}
suspend fun postEvent(event: OSIABEvents) {
_events.emit(event)
}
fun broadcastEvent(context: Context, event: OSIABEvents) {
val intent = Intent(ACTION_IAB_EVENT).apply {
setPackage(context.packageName)
putExtra(EXTRA_EVENT_DATA, event)
}
context.sendBroadcast(intent)
}
}
}