From 6ccf6f596e7e3e1d2eca736cfdfd470dfb464eb9 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Thu, 2 Apr 2026 09:30:18 +0300 Subject: [PATCH] Improved checking if there already is an active call --- app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt | 8 +++----- app/src/main/kotlin/com/tutpro/baresip/Call.kt | 9 +++++++++ app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt | 4 ++-- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 175c5618..0a645e31 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -863,17 +863,15 @@ class BaresipService: Service() { "incoming call" -> { val peerUri = ev[1] val bevent = ev[2].toLong() - val blockUnknown = ua.account.blockUnknown && Contact.contactName(peerUri) == peerUri - val toastMsg = if (Call.inCall()) + val toastMsg = if (Call.isAnyCallActive(applicationContext)) String.format(getString(R.string.call_auto_rejected), Utils.friendlyUri(this, peerUri, ua.account)) - else if (blockUnknown) + else if (ua.account.blockUnknown && Contact.contactName(peerUri) == peerUri) String.format(getString(R.string.call_blocked), Utils.friendlyUri(this, peerUri, ua.account)) else if (!Utils.checkPermissions(this, arrayOf(RECORD_AUDIO))) getString(R.string.no_calls) else if (!requestAudioFocus(applicationContext)) - // request fails if there is an active telephony call getString(R.string.audio_focus_denied) else "" @@ -882,7 +880,7 @@ class BaresipService: Service() { Api.sip_treply(callp, 486, "Busy Here") Api.bevent_stop(bevent) toast(toastMsg) - if (blockUnknown) { + if (toastMsg.contains(getString(R.string.call_blocked))) { if (ua.account.callHistory) Blocked( ua.account.aor, diff --git a/app/src/main/kotlin/com/tutpro/baresip/Call.kt b/app/src/main/kotlin/com/tutpro/baresip/Call.kt index de38f6d6..3dff670e 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Call.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Call.kt @@ -1,5 +1,7 @@ package com.tutpro.baresip +import android.content.Context +import android.media.AudioManager import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf @@ -143,5 +145,12 @@ class Call(val callp: Long, val ua: UserAgent, val peerUri: String, val dir: Str return BaresipService.calls.isNotEmpty() } + fun isAnyCallActive(ctx: Context): Boolean { + // Check SIP calls managed by baresip + if (inCall()) return true + // MODE_IN_CALL indicates a PSTN call is active + val am = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager + return am.mode == AudioManager.MODE_IN_CALL + } } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt b/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt index 542bb7bd..5a5a5afd 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainScreen.kt @@ -2037,10 +2037,10 @@ private fun makeCall(ctx: Context, viewModel: ViewModel, uriText: String, confer alertMessage.value = String.format(ctx.getString(R.string.invalid_sip_or_tel_uri), uri) showAlert.value = true } + else if (Call.isAnyCallActive(ctx)) + Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show() else if (!BaresipService.requestAudioFocus(ctx)) Toast.makeText(ctx, R.string.audio_focus_denied, Toast.LENGTH_SHORT).show() - else if (Call.calls().any { it.ua.account.aor != ua.account.aor }) - Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show() else { viewModel.dialerState.callButtonsEnabled.value = false if (Build.VERSION.SDK_INT < 31) {