|
1 | 1 | package com.outsystems.plugins.inappbrowser.osinappbrowserlib |
2 | 2 |
|
| 3 | +import android.content.BroadcastReceiver |
3 | 4 | import android.content.Context |
4 | | -import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABWebViewActivity |
| 5 | +import android.content.Intent |
| 6 | +import android.content.IntentFilter |
| 7 | +import androidx.core.content.ContextCompat |
| 8 | +import androidx.core.content.IntentCompat |
5 | 9 | import kotlinx.coroutines.flow.MutableSharedFlow |
6 | 10 | import kotlinx.coroutines.flow.asSharedFlow |
| 11 | +import java.io.Serializable |
7 | 12 |
|
8 | | -sealed class OSIABEvents { |
| 13 | +@RequiresOptIn( |
| 14 | + level = RequiresOptIn.Level.WARNING, |
| 15 | + message = "This API requires a prior call to OSIABEvents.registerReceiver(context) to work correctly with process isolation on Android 9+." |
| 16 | +) |
| 17 | +@Retention(AnnotationRetention.BINARY) |
| 18 | +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) |
| 19 | +annotation class RequiresEventBridgeRegistration |
| 20 | + |
| 21 | +sealed class OSIABEvents : Serializable { |
9 | 22 | abstract val browserId: String |
10 | 23 |
|
11 | 24 | data class BrowserPageLoaded(override val browserId: String) : OSIABEvents() |
12 | 25 | data class BrowserFinished(override val browserId: String) : OSIABEvents() |
13 | 26 | data class BrowserPageNavigationCompleted(override val browserId: String, val url: String?) : OSIABEvents() |
14 | | - |
15 | 27 | data class OSIABCustomTabsEvent( |
16 | 28 | override val browserId: String, |
17 | 29 | val action: String, |
18 | | - val context: Context |
| 30 | + @Transient val context: Context? = null |
19 | 31 | ) : OSIABEvents() |
| 32 | + |
20 | 33 | data class OSIABWebViewEvent( |
21 | | - override val browserId: String, |
22 | | - val activity: OSIABWebViewActivity |
| 34 | + override val browserId: String |
23 | 35 | ) : OSIABEvents() |
24 | 36 |
|
25 | 37 | companion object { |
26 | 38 | const val EXTRA_BROWSER_ID = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_BROWSER_ID" |
| 39 | + const val ACTION_IAB_EVENT = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.ACTION_IAB_EVENT" |
| 40 | + const val ACTION_CLOSE_WEBVIEW = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.ACTION_CLOSE_WEBVIEW" |
| 41 | + const val EXTRA_EVENT_DATA = "com.outsystems.plugins.inappbrowser.osinappbrowserlib.EXTRA_EVENT_DATA" |
27 | 42 |
|
28 | | - private val _events = MutableSharedFlow<OSIABEvents>() |
| 43 | + // Buffer capacity is required because BroadcastReceiver.onReceive() is synchronous. |
| 44 | + // We must use tryEmit() which would drop events without buffer space. |
| 45 | + private val _events = MutableSharedFlow<OSIABEvents>(extraBufferCapacity = 64) |
29 | 46 | val events = _events.asSharedFlow() |
30 | 47 |
|
| 48 | + private var receiver: BroadcastReceiver? = null |
| 49 | + private var receiverRefCount = 0 |
| 50 | + |
| 51 | + /** |
| 52 | + * Registers a BroadcastReceiver to listen for events from the isolated WebView process. |
| 53 | + * This must be called before opening a WebView on Android 9+ to ensure events are received. |
| 54 | + */ |
| 55 | + @Synchronized |
| 56 | + fun registerReceiver(context: Context) { |
| 57 | + receiverRefCount++ |
| 58 | + if (receiver != null) return |
| 59 | + |
| 60 | + val appContext = context.applicationContext |
| 61 | + receiver = object : BroadcastReceiver() { |
| 62 | + override fun onReceive(context: Context?, intent: Intent?) { |
| 63 | + if (intent?.action == ACTION_IAB_EVENT) { |
| 64 | + val event = IntentCompat.getSerializableExtra( |
| 65 | + intent, |
| 66 | + EXTRA_EVENT_DATA, |
| 67 | + OSIABEvents::class.java |
| 68 | + ) |
| 69 | + event?.let { |
| 70 | + _events.tryEmit(it) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + val filter = IntentFilter(ACTION_IAB_EVENT) |
| 77 | + ContextCompat.registerReceiver( |
| 78 | + appContext, |
| 79 | + receiver, |
| 80 | + filter, |
| 81 | + ContextCompat.RECEIVER_NOT_EXPORTED |
| 82 | + ) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Unregisters the BroadcastReceiver. Should be called when the browser is closed. |
| 87 | + * The receiver is only truly unregistered when all registered 'users' have unregistered. |
| 88 | + */ |
| 89 | + @Synchronized |
| 90 | + fun unregisterReceiver(context: Context) { |
| 91 | + if (receiverRefCount > 0) { |
| 92 | + receiverRefCount-- |
| 93 | + } |
| 94 | + |
| 95 | + if (receiverRefCount == 0) { |
| 96 | + receiver?.let { |
| 97 | + try { |
| 98 | + context.applicationContext.unregisterReceiver(it) |
| 99 | + } catch (e: Exception) { |
| 100 | + // Receiver may not be registered, ignore |
| 101 | + } |
| 102 | + receiver = null |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
31 | 107 | suspend fun postEvent(event: OSIABEvents) { |
32 | 108 | _events.emit(event) |
33 | 109 | } |
| 110 | + |
| 111 | + /** |
| 112 | + * Broadcasts an event from the isolated WebView process to the main process. |
| 113 | + * Only data-only events should be broadcast (BrowserPageLoaded, BrowserFinished, etc.). |
| 114 | + */ |
| 115 | + fun broadcastEvent(context: Context, event: OSIABEvents) { |
| 116 | + val intent = Intent(ACTION_IAB_EVENT).apply { |
| 117 | + setPackage(context.packageName) |
| 118 | + putExtra(EXTRA_EVENT_DATA, event) |
| 119 | + } |
| 120 | + context.sendBroadcast(intent) |
| 121 | + } |
34 | 122 | } |
35 | 123 |
|
36 | 124 | } |
0 commit comments