Modern Apps git

← Modern-Apps.git

communicate: add call screen for SIM calls (#597)

Commit
03f90fcc48abe896f0213902b0f11ba62b851107
Author
vayun-mathur <[email protected]>
When
2026-10-18 18:32 UTC (1 month ago)
Parents
0d2d71b

Diff

communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.ktindex 7c3fff0719..33378a2e0d 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/call/InAppCall.kt@@ -19,6 +19,7 @@ data class CallCapabilities(     val video: Boolean = false,     val dtmf: Boolean = false,     val screenShare: Boolean = false,+    val hold: Boolean = false, ) {     companion object {         val AudioOnly = CallCapabilities()@@ -61,6 +62,8 @@ data class InAppCallState(     val remoteScreenSharing: Boolean = false,     val muted: Boolean = false,     val speaker: Boolean = false,+    /** Whether the carrier call is held (GSM hold / Telecom STATE_HOLDING). Only the SIM line sets this. */+    val held: Boolean = false,     /** When the call became [InAppCallPhase.Active], for the duration readout. 0 until then. */     val connectedAtMs: Long = 0L,     val endReason: String? = null,@@ -98,6 +101,13 @@ interface InAppCallDtmfController {     fun sendDtmf(digit: String) } +/**+ * Call hold, separate from [InAppCallController] because only the carrier line can act on it.+ */+interface InAppCallHoldController {+    fun setHeld(held: Boolean)+}+ /**  * Video capability, separate from [InAppCallController] because not every line has it and the shared screen  * should only offer the control when something can act on it.@@ -147,6 +157,10 @@ object InAppCallRegistry {     @Volatile     var dtmfController: InAppCallDtmfController? = null +    /** Set only by lines that can hold (the carrier line). */+    @Volatile+    var holdController: InAppCallHoldController? = null+     /** Set by the Telecom ConnectionService so state changes can be reflected into the system UI. */     @Volatile     var connection: InAppCallConnectionBridge? = null@@ -218,6 +232,11 @@ object InAppCallRegistry {         _state.value = _state.value.copy(speaker = on)     } +    fun onHeld(held: Boolean) {+        if (_state.value.phase == InAppCallPhase.Idle) return+        _state.value = _state.value.copy(held = held)+    }+     fun onRemoteVideo(enabled: Boolean) {         if (_state.value.phase == InAppCallPhase.Idle) return         Log.status(TAG, "remote video enabled=$enabled")@@ -277,6 +296,7 @@ object InAppCallRegistry {             controller = null             videoController = null             dtmfController = null+            holdController = null             connection = null         }     }@@ -310,4 +330,10 @@ object InAppCallRegistry {         controller?.setSpeaker(next)         onSpeaker(next)     }++    fun toggleHeld() {+        val next = !_state.value.held+        holdController?.setHeld(next)+        onHeld(next)+    } }
communicate/src/main/java/com/vayunmathur/communicate/telephony/CommunicateInCallService.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/telephony/CommunicateInCallService.kt b/communicate/src/main/java/com/vayunmathur/communicate/telephony/CommunicateInCallService.ktindex 4da6ef2fcb..7fc086a15a 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/telephony/CommunicateInCallService.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/telephony/CommunicateInCallService.kt@@ -6,6 +6,7 @@ import android.app.PendingIntent import android.content.Intent import android.os.Build import android.telecom.Call+import android.telecom.CallAudioState import android.telecom.InCallService import android.telecom.VideoProfile import com.vayunmathur.communicate.MainActivity@@ -14,13 +15,19 @@ import com.vayunmathur.communicate.data.CommunicateRepository import com.vayunmathur.communicate.data.findContactName import com.vayunmathur.library.util.ensureNotificationChannel -/** In-call bridge for carrier/SIM calls that lets Communicate expose notification controls. */+/**+ * In-call bridge for carrier/SIM calls.+ *+ * Owns the system call surface (notification controls) and mirrors the call into [SimCallBridge] so the+ * shared in-app call screen serves regular phone calls too — with the dial pad voicemail needs.+ */ class CommunicateInCallService : InCallService() {      private val callback = object : Call.Callback() {         override fun onStateChanged(call: Call, state: Int) {             currentCall = call             showOrClearNotification(call)+            SimCallBridge.onStateChanged(call, state)         }          override fun onDetailsChanged(call: Call, details: Call.Details) {@@ -29,18 +36,30 @@ class CommunicateInCallService : InCallService() {         }     } +    override fun onCallAudioStateChanged(audioState: CallAudioState) {+        super.onCallAudioStateChanged(audioState)+        SimCallBridge.onAudioStateChanged(audioState)+    }+     override fun onCallAdded(call: Call) {         super.onCallAdded(call)         currentCall?.unregisterCallback(callback)         currentCall = call         call.registerCallback(callback)         showOrClearNotification(call)+        val number = call.details.handle?.schemeSpecificPart.orEmpty()+        SimCallBridge.onCallAdded(this, call, number, callerLabel(number))+        // Ringing must surface the screen, not just a notification: bring the app forward so the+        // InAppCallOverlay shows the incoming call with answer / decline.+        if (call.details.state == Call.STATE_RINGING) bringCallScreenForward()     }      override fun onCallRemoved(call: Call) {+        val wasTracked = currentCall == call         call.unregisterCallback(callback)-        if (currentCall == call) currentCall = null+        if (wasTracked) currentCall = null         clearNotification()+        SimCallBridge.onCallRemoved(call)         super.onCallRemoved(call)     } @@ -55,7 +74,7 @@ class CommunicateInCallService : InCallService() {     }      private fun showOrClearNotification(call: Call) {-        when (call.state) {+        when (call.details.state) {             Call.STATE_RINGING -> showNotification(call, incoming = true)             Call.STATE_DIALING, Call.STATE_CONNECTING, Call.STATE_ACTIVE, Call.STATE_HOLDING -> showNotification(                 call,@@ -64,6 +83,15 @@ class CommunicateInCallService : InCallService() {         }     } +    /** Tapping the call notification opens the call screen rather than the message list. */+    private fun bringCallScreenForward() {+        startActivity(+            Intent(this, MainActivity::class.java).apply {+                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)+            },+        )+    }+     private fun showNotification(call: Call, incoming: Boolean) {         ensureNotificationChannel(             id = CHANNEL_ID,
communicate/src/main/java/com/vayunmathur/communicate/telephony/SimCallBridge.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/telephony/SimCallBridge.kt b/communicate/src/main/java/com/vayunmathur/communicate/telephony/SimCallBridge.ktnew file mode 100644index 0000000000..6b45fe511f--- /dev/null+++ b/communicate/src/main/java/com/vayunmathur/communicate/telephony/SimCallBridge.kt@@ -0,0 +1,150 @@+package com.vayunmathur.communicate.telephony++import android.telecom.Call+import android.telecom.CallAudioState+import android.telecom.VideoProfile+import com.vayunmathur.communicate.data.CommunicateLine+import com.vayunmathur.communicate.data.call.CallCapabilities+import com.vayunmathur.communicate.data.call.InAppCallController+import com.vayunmathur.communicate.data.call.InAppCallDtmfController+import com.vayunmathur.communicate.data.call.InAppCallHoldController+import com.vayunmathur.communicate.data.call.InAppCallPhase+import com.vayunmathur.communicate.data.call.InAppCallRegistry+import com.vayunmathur.library.log.Log++/**+ * Mirrors the carrier/SIM [Call] owned by [CommunicateInCallService] into the shared [InAppCallRegistry]+ * so regular phone calls get the same call screen as every other line — including the dial pad the issue+ * calls out (voicemail and phone menus need DTMF).+ *+ * An observer rather than a change inside the service: the service keeps owning the system call surface+ * and notifications, and this only projects the state it already publishes. Same pattern as+ * [com.vayunmathur.communicate.data.googlevoice.call.GoogleVoiceCallBridge].+ */+object SimCallBridge {+    private const val TAG = "SimCallBridge"++    /** The capabilities a regular phone call gets on the shared screen: keypad and hold. */+    val capabilities = CallCapabilities(dtmf = true, hold = true)++    /**+     * The carrier call the registry is mirroring. A second leg (call waiting) never replaces it: its+     * state changes are ignored and its removal ends nothing, so declining call-waiting cannot dismiss+     * the screen of the call still up.+     */+    @Volatile+    private var mirroredCall: Call? = null++    /**+     * Publish a carrier call that just appeared. [peerId]/[peerName] are the number and the resolved+     * contact name. Idempotent per call — the service calls it once from `onCallAdded`.+     */+    fun onCallAdded(service: CommunicateInCallService, call: Call, peerId: String, peerName: String) {+        val current = InAppCallRegistry.state.value+        if (current.phase != InAppCallPhase.Idle && current.phase != InAppCallPhase.Ended) {+            // The screen is already driving a live call: a second SIM leg (call waiting) shares nothing,+            // and a SIM call arriving during a VoIP call stays notification-only rather than clobbering+            // the other line's state.+            return+        }+        val telecomState = call.details.state+        val incoming = telecomState == Call.STATE_RINGING+        Log.status(TAG, "SIM call starting incoming=$incoming peer=$peerId")+        InAppCallRegistry.bind(CommunicateLine.Sim, simController(service, call), capabilities)+        InAppCallRegistry.dtmfController = simDtmf(call)+        InAppCallRegistry.holdController = simHold(call)+        InAppCallRegistry.onCallStarting(+            line = CommunicateLine.Sim,+            peerId = peerId,+            peerName = peerName.ifBlank { peerId },+            isVideo = false,+            incoming = incoming,+            capabilities = capabilities,+        )+        InAppCallRegistry.onPhase(mapPhase(telecomState))+        if (telecomState == Call.STATE_HOLDING) InAppCallRegistry.onHeld(true)+        mirroredCall = call+    }++    /**+     * Reflect a `Call` state change. Anything that is not the mirrored leg — call waiting, a second SIM+     * leg after a swap — is ignored.+     *+     * [telecomState] is the state the callback already reports, so the deprecated `Call.getState()`+     * is never read.+     */+    fun onStateChanged(call: Call, telecomState: Int) {+        if (call != mirroredCall) return+        if (InAppCallRegistry.state.value.line != CommunicateLine.Sim) return+        InAppCallRegistry.onPhase(mapPhase(telecomState))+        if (telecomState == Call.STATE_HOLDING || telecomState == Call.STATE_ACTIVE) {+            InAppCallRegistry.onHeld(telecomState == Call.STATE_HOLDING)+        }+        if (telecomState == Call.STATE_DISCONNECTED) {+            InAppCallRegistry.onHeld(false)+        }+    }++    /** Reflect the system mute / audio-route change onto the shared state. */+    fun onAudioStateChanged(audioState: CallAudioState) {+        if (InAppCallRegistry.state.value.line != CommunicateLine.Sim) return+        InAppCallRegistry.onMuted(audioState.isMuted)+        InAppCallRegistry.onSpeaker(audioState.route == CallAudioState.ROUTE_SPEAKER)+    }++    /** Ends the mirror when the mirrored carrier call goes away; other legs end nothing. */+    fun onCallRemoved(call: Call) {+        if (call != mirroredCall) return+        mirroredCall = null+        val current = InAppCallRegistry.state.value+        if (current.line == CommunicateLine.Sim && current.phase != InAppCallPhase.Idle) {+            InAppCallRegistry.onEnded(null)+        }+    }++    fun mapPhase(telecomState: Int): InAppCallPhase = when (telecomState) {+        Call.STATE_RINGING -> InAppCallPhase.Incoming+        Call.STATE_DIALING, Call.STATE_CONNECTING, Call.STATE_SELECT_PHONE_ACCOUNT -> InAppCallPhase.Outgoing+        Call.STATE_ACTIVE, Call.STATE_HOLDING -> InAppCallPhase.Active+        Call.STATE_DISCONNECTED, Call.STATE_DISCONNECTING -> InAppCallPhase.Ended+        // New / incoming-pending / audio-processing: the call exists but has no direction yet.+        else -> InAppCallPhase.Connecting+    }++    private fun simController(service: CommunicateInCallService, call: Call) = object : InAppCallController {+        override fun answer() {+            call.answer(VideoProfile.STATE_AUDIO_ONLY)+        }++        override fun reject() {+            call.reject(false, null)+        }++        override fun hangup() {+            call.disconnect()+        }++        override fun setMuted(muted: Boolean) {+            service.setMuted(muted)+        }++        override fun setSpeaker(on: Boolean) {+            @Suppress("DEPRECATION")+            service.setAudioRoute(+                if (on) CallAudioState.ROUTE_SPEAKER else CallAudioState.ROUTE_WIRED_OR_EARPIECE,+            )+        }+    }++    private fun simDtmf(call: Call) = object : InAppCallDtmfController {+        override fun sendDtmf(digit: String) {+            call.playDtmfTone(digit.firstOrNull() ?: return)+        }+    }++    private fun simHold(call: Call) = object : InAppCallHoldController {+        override fun setHeld(held: Boolean) {+            if (held) call.hold() else call.unhold()+        }+    }+}
communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallControls.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallControls.kt b/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallControls.ktindex b73720099b..b9f60661aa 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallControls.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/ui/call/InAppCallControls.kt@@ -33,6 +33,7 @@ import com.vayunmathur.library.ui.IconCallEnd import com.vayunmathur.library.ui.IconCameraOff import com.vayunmathur.library.ui.IconDialpad import com.vayunmathur.library.ui.IconFlipCamera+import com.vayunmathur.library.ui.IconPause import com.vayunmathur.library.ui.IconMic import com.vayunmathur.library.ui.IconMicOff import com.vayunmathur.library.ui.IconScreenShare@@ -182,6 +183,13 @@ internal fun OngoingControls(                     onClick = onToggleScreenShare,                 ) { tint -> IconScreenShare(tint = tint) }             }+            if (caps.hold) {+                CallToggle(+                    label = stringResource(R.string.call_hold),+                    active = state.held,+                    onClick = { InAppCallRegistry.toggleHeld() },+                ) { tint -> IconPause(tint = tint) }+            }             if (caps.dtmf) {                 CallToggle(                     label = stringResource(R.string.call_keypad),@@ -302,6 +310,8 @@ internal fun callStatusText(state: InAppCallState): String {         CommunicateLine.Signal -> stringResource(R.string.account_signal)         CommunicateLine.WhatsApp -> stringResource(R.string.account_whatsapp)         CommunicateLine.GoogleVoice -> stringResource(R.string.account_google_voice)+        CommunicateLine.Sim -> stringResource(R.string.account_sim)+        CommunicateLine.Rcs -> stringResource(R.string.account_rcs)         else -> ""     }     return when (state.phase) {@@ -312,8 +322,12 @@ internal fun callStatusText(state: InAppCallState): String {             "${stringResource(R.string.call_state_incoming_generic)} - $lineName"         }         InAppCallPhase.Connecting -> stringResource(R.string.call_state_connecting)-        // Once connected the duration carries the status, so name the line instead.-        InAppCallPhase.Active -> lineName+        // Once connected the duration carries the status, so name the line instead — unless held.+        InAppCallPhase.Active -> if (state.held) {+            stringResource(R.string.call_state_on_hold)+        } else {+            lineName+        }         InAppCallPhase.Ended -> state.endReason?.takeIf { it.isNotBlank() }             ?: stringResource(R.string.call_state_ended)         InAppCallPhase.Idle -> ""
communicate/src/main/res/values/strings.xml
diff --git a/communicate/src/main/res/values/strings.xml b/communicate/src/main/res/values/strings.xmlindex 5b0fd1416f..de1fcace4a 100644--- a/communicate/src/main/res/values/strings.xml+++ b/communicate/src/main/res/values/strings.xml@@ -91,6 +91,8 @@     <string name="call_answer">Answer</string>     <string name="call_decline">Decline</string>     <string name="call_keypad">Keypad</string>+    <string name="call_hold">Hold</string>+    <string name="call_state_on_hold">On hold</string>     <string name="call_state_dialing">Dialing…</string>     <string name="call_state_ringing">Ringing…</string>     <string name="call_state_active">Connected</string>
communicate/src/test/java/com/vayunmathur/communicate/telephony/SimCallBridgeTest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/telephony/SimCallBridgeTest.kt b/communicate/src/test/java/com/vayunmathur/communicate/telephony/SimCallBridgeTest.ktnew file mode 100644index 0000000000..cf53f11912--- /dev/null+++ b/communicate/src/test/java/com/vayunmathur/communicate/telephony/SimCallBridgeTest.kt@@ -0,0 +1,47 @@+package com.vayunmathur.communicate.telephony++import android.telecom.Call+import com.vayunmathur.communicate.data.call.InAppCallPhase+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++class SimCallBridgeTest {+    @Test+    fun `ringing maps to incoming`() {+        assertEquals(InAppCallPhase.Incoming, SimCallBridge.mapPhase(Call.STATE_RINGING))+    }++    @Test+    fun `dialing connecting and account selection map to outgoing`() {+        assertEquals(InAppCallPhase.Outgoing, SimCallBridge.mapPhase(Call.STATE_DIALING))+        assertEquals(InAppCallPhase.Outgoing, SimCallBridge.mapPhase(Call.STATE_CONNECTING))+        assertEquals(InAppCallPhase.Outgoing, SimCallBridge.mapPhase(Call.STATE_SELECT_PHONE_ACCOUNT))+    }++    @Test+    fun `active and holding map to active`() {+        assertEquals(InAppCallPhase.Active, SimCallBridge.mapPhase(Call.STATE_ACTIVE))+        assertEquals(InAppCallPhase.Active, SimCallBridge.mapPhase(Call.STATE_HOLDING))+    }++    @Test+    fun `disconnecting and disconnected map to ended`() {+        assertEquals(InAppCallPhase.Ended, SimCallBridge.mapPhase(Call.STATE_DISCONNECTING))+        assertEquals(InAppCallPhase.Ended, SimCallBridge.mapPhase(Call.STATE_DISCONNECTED))+    }++    @Test+    fun `unknown states map to connecting rather than idle`() {+        assertEquals(InAppCallPhase.Connecting, SimCallBridge.mapPhase(Call.STATE_NEW))+    }++    @Test+    fun `sim capabilities offer keypad and hold but no video`() {+        val caps = SimCallBridge.capabilities+        assertTrue(caps.dtmf)+        assertTrue(caps.hold)+        assertTrue(!caps.video)+        assertTrue(!caps.screenShare)+    }+}