Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / call / WhatsAppCallBridge.kt

WhatsAppCallBridge.kt103 lines · 4.9 KiBRaw
1package com.vayunmathur.communicate.data.whatsapp.call
2
3import android.content.Context
4import com.vayunmathur.communicate.data.CommunicateLine
5import com.vayunmathur.communicate.data.call.CallCapabilities
6import com.vayunmathur.communicate.data.call.InAppCallController
7import com.vayunmathur.communicate.data.call.InAppCallPhase
8import com.vayunmathur.communicate.data.call.InAppCallRegistry
9import com.vayunmathur.communicate.data.call.InAppCallSystemBridge
10import kotlinx.coroutines.CoroutineScope
11import kotlinx.coroutines.Dispatchers
12import kotlinx.coroutines.SupervisorJob
13import kotlinx.coroutines.launch
14import java.util.concurrent.atomic.AtomicBoolean
15
16/**
17 * Mirrors [WhatsAppCallManager]'s state into the shared [InAppCallRegistry] and hands calls to Telecom.
18 *
19 * An observer rather than a change inside the manager: its call flow already works, and the shared UI only
20 * needs a projection of the state it already publishes.
21 */
22object WhatsAppCallBridge {
23 private val started = AtomicBoolean(false)
24 private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
25
26 /** Idempotent; safe to call from both the activity and the sync service. */
27 fun ensureStarted(context: Context) {
28 if (!started.compareAndSet(false, true)) return
29 val appContext = context.applicationContext
30 scope.launch {
31 var lastPhase: WhatsAppCallPhase? = null
32 WhatsAppCallManager.state.collect { state ->
33 if (state.phase == lastPhase) return@collect
34 val previous = lastPhase
35 lastPhase = state.phase
36
37 when (state.phase) {
38 WhatsAppCallPhase.Outgoing -> {
39 InAppCallRegistry.bind(
40 CommunicateLine.WhatsApp,
41 controller,
42 CallCapabilities.AudioAndVideo,
43 )
44 InAppCallRegistry.onCallStarting(
45 line = CommunicateLine.WhatsApp,
46 peerId = state.peerJid,
47 peerName = state.peerName.ifBlank { state.peerJid },
48 isVideo = state.isVideo,
49 incoming = false,
50 capabilities = CallCapabilities.AudioAndVideo,
51 )
52 InAppCallSystemBridge.addOutgoing(appContext, state.peerJid)
53 }
54 WhatsAppCallPhase.Incoming -> {
55 InAppCallRegistry.bind(
56 CommunicateLine.WhatsApp,
57 controller,
58 CallCapabilities.AudioAndVideo,
59 )
60 InAppCallRegistry.onCallStarting(
61 line = CommunicateLine.WhatsApp,
62 peerId = state.peerJid,
63 peerName = state.peerName.ifBlank { state.peerJid },
64 isVideo = state.isVideo,
65 incoming = true,
66 capabilities = CallCapabilities.AudioAndVideo,
67 )
68 InAppCallSystemBridge.addIncoming(appContext, state.peerJid)
69 // Telecom can silently drop the mirror (no callback); the ringing
70 // surface still has to come up, so start it directly as well.
71 // Idempotent with the ConnectionService path — the service collects
72 // the same registry state and just re-posts the same notification.
73 runCatching {
74 InAppCallSystemBridge.startForegroundService(appContext)
75 }
76 }
77 WhatsAppCallPhase.Connecting -> InAppCallRegistry.onPhase(InAppCallPhase.Connecting)
78 WhatsAppCallPhase.Active -> InAppCallRegistry.onPhase(InAppCallPhase.Active)
79 WhatsAppCallPhase.Ended -> InAppCallRegistry.onEnded(null)
80 WhatsAppCallPhase.Idle ->
81 // Only meaningful as a return to rest after a call, not at startup.
82 if (previous != null) InAppCallRegistry.clearEnded()
83 }
84 if (state.phase != WhatsAppCallPhase.Idle) {
85 InAppCallRegistry.onMuted(state.muted)
86 InAppCallRegistry.onSpeaker(state.speaker)
87 }
88 }
89 }
90 }
91
92 private val controller = object : InAppCallController {
93 override fun answer() = WhatsAppCallManager.answer()
94
95 override fun reject() = WhatsAppCallManager.reject()
96
97 override fun hangup() = WhatsAppCallManager.hangup()
98
99 override fun setMuted(muted: Boolean) = WhatsAppCallManager.setMuted(muted)
100
101 override fun setSpeaker(on: Boolean) = WhatsAppCallManager.setSpeaker(on)
102 }
103}