-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathVoipModule.kt
More file actions
180 lines (158 loc) · 5.89 KB
/
VoipModule.kt
File metadata and controls
180 lines (158 loc) · 5.89 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package chat.rocket.reactnative.voip
import android.util.Log
import chat.rocket.reactnative.BuildConfig
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableMap
import com.facebook.react.modules.core.DeviceEventManagerModule
import chat.rocket.reactnative.networking.NativeVoipSpec
import java.lang.ref.WeakReference
import java.util.concurrent.atomic.AtomicReference
/**
* Native module to expose VoIP call data to JavaScript.
* Used to retrieve pending VoIP call data when the app opens from a VoIP notification.
*/
class VoipModule(reactContext: ReactApplicationContext) : NativeVoipSpec(reactContext) {
companion object {
private const val TAG = "RocketChat.VoipModule"
private const val EVENT_VOIP_ACCEPT_SUCCEEDED = "VoipAcceptSucceeded"
private const val EVENT_VOIP_ACCEPT_FAILED = "VoipAcceptFailed"
private var reactContextRef: WeakReference<ReactApplicationContext>? = null
private val initialEventsData = AtomicReference<VoipPayload?>(null)
/**
* Sets the React context reference for event emission.
*/
@JvmStatic
fun setReactContext(context: ReactApplicationContext) {
reactContextRef = WeakReference(context)
}
/**
* Emits a VoIP call event to JavaScript when the app is running.
*/
@JvmStatic
fun emitInitialEventsEvent(voipPayload: VoipPayload) {
try {
reactContextRef?.get()?.let { context ->
if (context.hasActiveReactInstance()) {
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(EVENT_VOIP_ACCEPT_SUCCEEDED, voipPayload.toWritableMap())
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to emit VoIP call event", e)
}
}
/**
* Stores VoIP call data for JS to retrieve.
* Also emits an event if the app is running.
*/
@JvmStatic
fun storeInitialEvents(voipPayload: VoipPayload) {
initialEventsData.set(voipPayload)
emitInitialEventsEvent(voipPayload)
}
/**
* Stash native accept failure for cold start [getInitialEvents] and emit [EVENT_VOIP_ACCEPT_FAILED] when JS is running.
*/
@JvmStatic
fun storeAcceptFailureForJs(payload: VoipPayload) {
val failed = payload.copy(voipAcceptFailed = true)
initialEventsData.set(failed)
emitVoipAcceptFailedEvent(failed)
}
private fun emitVoipAcceptFailedEvent(voipPayload: VoipPayload) {
try {
reactContextRef?.get()?.let { context ->
if (context.hasActiveReactInstance()) {
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(EVENT_VOIP_ACCEPT_FAILED, voipPayload.toWritableMap())
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to emit VoipAcceptFailed", e)
}
}
@JvmStatic
fun clearInitialEventsInternal() {
try {
initialEventsData.set(null)
Log.d(TAG, "Cleared initial events")
} catch (e: Exception) {
Log.e(TAG, "Error clearing initial events", e)
}
}
}
init {
// Store reference for event emission
setReactContext(reactApplicationContext)
}
/**
* Gets any initial events.
* Returns null if no initial events.
*/
override fun getInitialEvents(): WritableMap? {
while (true) {
val data = initialEventsData.get() ?: return null
if (data.isExpired()) {
if (BuildConfig.DEBUG) {
Log.d(TAG, "Discarding expired VoIP initial event: ${data.callId}")
}
if (initialEventsData.compareAndSet(data, null)) {
return null
}
continue
}
val result = data.toWritableMap()
if (initialEventsData.compareAndSet(data, null)) {
return result
}
}
}
/**
* Clears any initial events.
*/
override fun clearInitialEvents() {
clearInitialEventsInternal()
}
// No-op on Android - FCM handles push notifications
override fun getLastVoipToken(): String = ""
/**
* Registers for VoIP push token.
* No-op on Android - uses FCM for push notifications.
*/
override fun registerVoipToken() {
// No-op on Android - FCM handles push notifications
Log.d(TAG, "registerVoipToken called (no-op on Android)")
}
override fun stopNativeDDPClient() {
Log.d(TAG, "stopNativeDDPClient called, stopping native DDP client")
VoipNotification.stopDDPClient()
}
override fun stopVoipCallService() {
try {
VoipCallService.stopService(reactApplicationContext)
Log.d(TAG, "stopVoipCallService: service stopped")
} catch (e: Exception) {
Log.e(TAG, "stopVoipCallService: failed to stop service", e)
}
}
/**
* Required for NativeEventEmitter in TurboModules.
* Called when JS starts listening to events.
*/
override fun addListener(eventName: String) {
// Keep track of listeners if needed
if (BuildConfig.DEBUG) {
Log.d(TAG, "addListener: $eventName")
}
}
/**
* Required for NativeEventEmitter in TurboModules.
* Called when JS stops listening to events.
*/
override fun removeListeners(count: Double) {
// Remove listeners if needed
Log.d(TAG, "removeListeners: $count")
}
}