tauri-plugin-background-service 1.0.0

Background service lifecycle plugin for Tauri v2 — run long-lived tasks on Android, iOS, and desktop
Documentation
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package app.tauri.backgroundservice

import android.content.ComponentName
import android.content.Context
import android.content.pm.ServiceInfo
import android.media.AudioAttributes
import android.media.AudioFocusRequest
import android.media.AudioManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.telecom.CallAudioState
import android.telecom.Connection
import android.telecom.ConnectionRequest
import android.telecom.ConnectionService
import android.telecom.DisconnectCause
import android.telecom.PhoneAccount
import android.telecom.PhoneAccountHandle
import android.telecom.TelecomManager
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import java.util.concurrent.ConcurrentHashMap

/**
 * Self-managed Telecom [ConnectionService] (spec 08 C6, Step 15).
 *
 * Registering a self-managed `PhoneAccount` lets the OS route audio focus,
 * Bluetooth/IO device switching, and the system call sheet through the
 * platform call pipeline while the webview is closed — i.e. the system call UI
 * a user expects on Android. The actual call media (signaling + encoded frames)
 * is owned by the Rust/Iroh call stack; this class is the OS integration seam.
 *
 * `MANAGE_OWN_CALLS` is declared in the manifest. The account is registered as
 * `CAPABILITY_SELF_MANAGED` (API 26+), which makes the system defer its own
 * in-call UI to ours and lets us place/answer calls via `TelecomManager`.
 *
 * Physical on-device execution (placing a real call, BT routing verification)
 * is the runbook half (plan §Step 20); the in-repo class is the compilable,
 * manifest-registered seam.
 */
class BackgroundCallConnectionService : ConnectionService() {

    companion object {
        private const val TAG = "BackgroundCallConnectionService"
        const val PHONE_ACCOUNT_ID = "bg_self_managed_call"

        /**
         * Register the self-managed phone account (idempotent). Call once during
         * app/plugin startup, before any call is placed.
         */
        @JvmStatic
        fun registerPhoneAccount(context: Context) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
            val tm = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
            val handle = phoneAccountHandle(context)
            val account = PhoneAccount.builder(
                handle,
                context.applicationInfo.loadLabel(context.packageManager),
            )
                .setCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)
                .setHighlightColor(0)
                .setShortDescription("Background calls")
                .build()
            try {
                tm.registerPhoneAccount(account)
                Log.i(TAG, "Registered self-managed phone account")
            } catch (e: SecurityException) {
                Log.w(TAG, "Cannot register phone account (MANAGE_OWN_CALLS?): ${e.message}")
            }
        }

        fun phoneAccountHandle(context: Context): PhoneAccountHandle {
            val componentName = ComponentName(context, BackgroundCallConnectionService::class.java)
            return PhoneAccountHandle(componentName, PHONE_ACCOUNT_ID)
        }

        /**
         * Resolve the Rust call session key a Telecom [ConnectionRequest] carries
         * (M-NATIVE-1, Step 9). The [addNewIncomingCall] issuer (Step 11) puts the
         * `call_id` in the request extras under [IncomingCallNotifier.EXTRA_CALL_ID];
         * `onAnswer`/`onReject` route to the core for this id. Empty when absent
         * (the notification-broadcast route remains a parallel answer/decline
         * binding).
         *
         * `addNewIncomingCall(handle, extras)` nests the connection-service extras
         * under [TelecomManager.EXTRA_INCOMING_CALL_EXTRAS] on real Android, while
         * Robolectric copies the extras verbatim (top-level) — read both so the
         * binding holds in production AND under the host gate.
         */
        @VisibleForTesting
        @JvmStatic
        fun callIdFromRequest(request: ConnectionRequest): String {
            val extras = request.extras ?: return ""
            extras.getBundle(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS)
                ?.getString(IncomingCallNotifier.EXTRA_CALL_ID)
                ?.takeIf { it.isNotEmpty() }
                ?.let { return it }
            return extras.getString(IncomingCallNotifier.EXTRA_CALL_ID).orEmpty()
        }

        // ── M-NATIVE-3 (Step 11): DRIVE the registered self-managed account ──
        //
        // Step 9 wired every Connection callback + the full audio-focus lifecycle,
        // but nothing issued `addNewIncomingCall`/`placeCall`, so the OS never
        // created a `BackgroundCallConnection` and the focus path was dead. Step 11
        // issues them so the platform call pipeline (audio focus, MODE_IN_
        // COMMUNICATION, BT routing) engages for a real call.

        /**
         * Issue [TelecomManager.addNewIncomingCall] for an inbound offer so the OS
         * creates a [BackgroundCallConnection] through [onCreateIncomingConnection]. The
         * `call_id` rides the extras (top-level + nested under
         * [TelecomManager.EXTRA_INCOMING_CALL_EXTRAS]) so `onAnswer`/`onReject`
         * (Step 9) and the audio-focus lifecycle bind to it. Idempotent-safe,
         * API26+-guarded, `SecurityException`-safe (MANAGE_OWN_CALLS).
         */
        @JvmStatic
        fun addNewIncomingCall(context: Context, callId: String, isVideo: Boolean) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
            if (callId.isEmpty()) return
            val tm = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
            val handle = phoneAccountHandle(context)
            val csExtras = Bundle().apply {
                putString(IncomingCallNotifier.EXTRA_CALL_ID, callId)
                putBoolean(EXTRA_HAS_VIDEO, isVideo)
            }
            val extras = Bundle().apply {
                // Top-level (Robolectric copies verbatim into the ConnectionRequest).
                putString(IncomingCallNotifier.EXTRA_CALL_ID, callId)
                // Nested (the real-Android channel onCreateIncomingConnection reads).
                putBundle(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS, csExtras)
            }
            try {
                tm.addNewIncomingCall(handle, extras)
                Log.i(TAG, "addNewIncomingCall: callId=$callId, video=$isVideo")
            } catch (e: SecurityException) {
                Log.w(TAG, "Cannot add incoming call (MANAGE_OWN_CALLS?): ${e.message}")
            }
        }

        /**
         * Issue [TelecomManager.placeCall] for an outbound dial so the OS creates a
         * [BackgroundCallConnection] through [onCreateOutgoingConnection] (which activates
         * audio focus immediately). The address is a synthetic `bg-call:` URI (the real
         * peer is the Iroh node; Telecom only needs a non-null address for routing).
         *
         * NOTE: there is no native outbound-dial hook today — outbound calls are
         * core-initiated via `start_call` — so this companion is the symmetric
         * capability (host-unit-tested); wiring it to a live dial event is a
         * follow-on (DEC-058). The inbound [addNewIncomingCall] is the AC1-required
         * driven path.
         */
        @JvmStatic
        fun placeOutgoingCall(context: Context, callId: String) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
            if (callId.isEmpty()) return
            val tm = context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
            val handle = phoneAccountHandle(context)
            val csExtras = Bundle().apply {
                putString(IncomingCallNotifier.EXTRA_CALL_ID, callId)
            }
            val extras = Bundle().apply {
                putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, handle)
                putBundle(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, csExtras)
                putString(IncomingCallNotifier.EXTRA_CALL_ID, callId)
            }
            val address = Uri.fromParts("bg-call", callId, null)
            try {
                tm.placeCall(address, extras)
                Log.i(TAG, "placeCall: callId=$callId")
            } catch (e: SecurityException) {
                Log.w(TAG, "Cannot place self-managed call (MANAGE_OWN_CALLS?): ${e.message}")
            }
        }

        /**
         * Process-level registry of the live [BackgroundCallConnection] per `call_id`,
         * so the notification-answer path ([CallActionReceiver], the primary answer
         * surface) and the route command ([setCallAudioRoute]) can reach the OS
         * connection the OS created in [onCreateIncomingConnection]. Populated on
         * connection create, cleared on disconnect.
         */
        private val liveConnections = ConcurrentHashMap<String, BackgroundCallConnection>()

        @VisibleForTesting
        internal fun registerConnection(callId: String, connection: BackgroundCallConnection) {
            if (callId.isNotEmpty()) liveConnections[callId] = connection
        }

        @VisibleForTesting
        internal fun unregisterConnection(callId: String) {
            if (callId.isNotEmpty()) liveConnections.remove(callId)
        }

        @VisibleForTesting
        internal fun liveConnectionCount(): Int = liveConnections.size

        @VisibleForTesting
        internal fun clearLiveConnectionsForTest() = liveConnections.clear()

        /**
         * Drive the live Telecom connection for `call_id` to ACTIVE — bridges the
         * notification-answer path (the broadcast [CallActionReceiver] answer, our
         * primary answer surface) to the OS connection so its
         * `onStateChanged(STATE_ACTIVE)` engages audio focus + `MODE_IN_COMMUNICATION`.
         * No-op when no connection is live (e.g. Telecom drive unavailable / answered
         * before the OS created the connection).
         */
        @JvmStatic
        fun markCallActive(callId: String) {
            liveConnections[callId]?.setActive()
        }

        /**
         * Apply a device audio route to the live self-managed connection
         * (M-NATIVE-3 / CCF-11). `Connection.setAudioRoute` is the correct
         * self-managed routing API (no `MODIFY_AUDIO_SETTINGS`). The physical route
         * switch is device-verified; the pure route-string → [CallAudioState] route
         * mapping ([audioRouteFor]) is the host-testable seam. No-op for `system`
         * (let the platform decide) or when no connection is live.
         */
        @JvmStatic
        fun setCallAudioRoute(callId: String, route: String) {
            val connection = liveConnections[callId] ?: run {
                Log.w(TAG, "setCallAudioRoute: no live connection for callId=$callId")
                return
            }
            val routeInt = audioRouteFor(route) ?: run {
                Log.i(TAG, "setCallAudioRoute: route=$route is platform-managed (no override)")
                return
            }
            try {
                connection.setAudioRoute(routeInt)
                Log.i(TAG, "setCallAudioRoute: callId=$callId, route=$route")
            } catch (e: Exception) {
                Log.w(TAG, "setCallAudioRoute failed: ${e.message}")
            }
        }

        /**
         * Pure route-string → [CallAudioState] route mapping (the host-testable
         * seam of [setCallAudioRoute]). `system` (and any unknown value) → `null`
         * = let the platform manage the route (no override).
         */
        @VisibleForTesting
        @JvmStatic
        fun audioRouteFor(route: String): Int? = when (route) {
            "speaker" -> CallAudioState.ROUTE_SPEAKER
            "earpiece" -> CallAudioState.ROUTE_EARPIECE
            "bluetooth" -> CallAudioState.ROUTE_BLUETOOTH
            else -> null // "system" / unknown → platform-managed
        }

        /** Extras key for the offer's video flag carried into the connection. */
        const val EXTRA_HAS_VIDEO = "bg_service.has_video"

        @VisibleForTesting
        fun foregroundServiceType(): Int {
            // The phoneCall FGS type the service is promoted to while a call
            // is active (LifecycleService maps the string to this constant).
            return ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL
        }
    }

    @VisibleForTesting
    internal var connectionFactory: (PhoneAccountHandle, ConnectionRequest) -> Connection =
        { handle, request -> BackgroundCallConnection(this, handle, request) }

    override fun onCreateIncomingConnection(
        connectionManagerPhoneAccount: PhoneAccountHandle?,
        request: ConnectionRequest,
    ): Connection {
        Log.i(TAG, "onCreateIncomingConnection: ${request.address}")
        val handle = connectionManagerPhoneAccount
            ?: phoneAccountHandle(applicationContext)
        val connection = connectionFactory(handle, request)
        configureSelfManaged(connection)
        // Track the live connection so the notification-answer path (markCallActive)
        // and the route command (setCallAudioRoute) can reach it (Step 11).
        (connection as? BackgroundCallConnection)?.let { registerConnection(it.callId, it) }
        connection.setRinging()
        return connection
    }

    override fun onCreateOutgoingConnection(
        connectionManagerPhoneAccount: PhoneAccountHandle?,
        request: ConnectionRequest,
    ): Connection {
        Log.i(TAG, "onCreateOutgoingConnection: ${request.address}")
        val handle = connectionManagerPhoneAccount
            ?: phoneAccountHandle(applicationContext)
        val connection = connectionFactory(handle, request)
        configureSelfManaged(connection)
        (connection as? BackgroundCallConnection)?.let { registerConnection(it.callId, it) }
        connection.setDialing()
        // Outgoing calls activate audio focus immediately on Android.
        connection.setActive()
        return connection
    }

    private fun configureSelfManaged(connection: Connection) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            connection.setConnectionProperties(Connection.PROPERTY_SELF_MANAGED)
        }
        connection.setConnectionCapabilities(
            Connection.CAPABILITY_MUTE or Connection.CAPABILITY_SUPPORT_HOLD
        )
        // Route audio through the VoIP audio mode (speaker/BT handled by the
        // platform call audio router once the connection is active).
        connection.setAudioModeIsVoip(true)
    }

    /**
     * The self-managed connection for one call. Owns audio focus for the
     * `VOICE_COMMUNICATION` stream and releases it on disconnect.
     */
    @RequiresApi(Build.VERSION_CODES.M)
    class BackgroundCallConnection(
        private val context: Context,
        @Suppress("UNUSED_PARAMETER") handle: PhoneAccountHandle,
        request: ConnectionRequest,
    ) : Connection() {

        private val audioManager =
            context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
        private var focusRequest: AudioFocusRequest? = null

        /** The Rust call session key this Telecom connection drives (Step 9). */
        @VisibleForTesting
        internal val callId: String = callIdFromRequest(request)

        // M-NATIVE-1 (Step 9): the system call sheet's Answer/Reject drive the
        // Rust control plane via the same injectable seam as the notification
        // broadcast route. Bound to THIS connection's call_id. (Telecom STATE
        // driving — addNewIncomingCall, setActive on answer, audio-focus keep —
        // is Step 11; Step 9 only routes the action to the core.)
        override fun onAnswer() {
            super.onAnswer()
            if (callId.isNotEmpty()) CallActionDispatch.dispatcher.answerCall(context, callId)
            // Step 11: drive the connection ACTIVE on answer so onStateChanged
            // (STATE_ACTIVE) engages audio focus + MODE_IN_COMMUNICATION. (The
            // notification-answer path reaches the same setActive via markCallActive.)
            setActive()
        }

        override fun onReject() {
            super.onReject()
            if (callId.isNotEmpty()) CallActionDispatch.dispatcher.rejectCall(context, callId)
        }

        // android.telecom.Connection signals lifecycle via onStateChanged(int)
        // (there is no onActive/onRinging override). Request VOICE_COMMUNICATION
        // audio focus while the call is ACTIVE; release on DISCONNECTED.
        override fun onStateChanged(state: Int) {
            super.onStateChanged(state)
            when (state) {
                Connection.STATE_ACTIVE -> {
                    requestCallAudioFocus()
                    audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
                }
                Connection.STATE_DISCONNECTED -> {
                    releaseCallAudioFocus()
                    audioManager.mode = AudioManager.MODE_NORMAL
                    // Step 11: drop the live-connection registry entry on any
                    // disconnect path (local hang-up, abort, remote end).
                    unregisterConnection(callId)
                }
            }
        }

        override fun onDisconnect() {
            super.onDisconnect()
            // Local hang-up via the system call sheet → end_call (Step 9).
            if (callId.isNotEmpty()) CallActionDispatch.dispatcher.endCall(context, callId)
            setDisconnected(DisconnectCause(DisconnectCause.LOCAL))
            releaseCallAudioFocus()
            audioManager.mode = AudioManager.MODE_NORMAL
            destroy()
        }

        override fun onAbort() {
            super.onAbort()
            // Aborted before connect → reject_call (Step 9).
            if (callId.isNotEmpty()) CallActionDispatch.dispatcher.rejectCall(context, callId)
            setDisconnected(DisconnectCause(DisconnectCause.CANCELED))
            releaseCallAudioFocus()
            audioManager.mode = AudioManager.MODE_NORMAL
            destroy()
        }

        private fun requestCallAudioFocus() {
            val attrs = AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_VOICE_COMMUNICATION)
                .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                .build()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                val request = AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
                    .setAudioAttributes(attrs)
                    .setAcceptsDelayedFocusGain(false)
                    .build()
                focusRequest = request
                audioManager.requestAudioFocus(request)
            } else {
                @Suppress("DEPRECATION")
                audioManager.requestAudioFocus(null, AudioManager.STREAM_VOICE_CALL,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
            }
        }

        private fun releaseCallAudioFocus() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                focusRequest?.let { audioManager.abandonAudioFocusRequest(it) }
                focusRequest = null
            } else {
                @Suppress("DEPRECATION")
                audioManager.abandonAudioFocus(null)
            }
        }
    }
}