From ecd6a4ad11f0c353a4e4b5ad3d2aa5f491aa41d4 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Tue, 21 Apr 2026 18:38:51 +0300 Subject: [PATCH] Replaced util function isPSTNCallActive with isAudioMode --- .../main/kotlin/com/tutpro/baresip/Call.kt | 3 +- .../main/kotlin/com/tutpro/baresip/Utils.kt | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/Call.kt b/app/src/main/kotlin/com/tutpro/baresip/Call.kt index 0ba1f01b..330cb546 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Call.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Call.kt @@ -174,8 +174,7 @@ class Call(val callp: Long, val ua: UserAgent, val peerUri: String, val dir: Str // Check if there exist SIP calls that are not onhold or held if (BaresipService.calls.any { !it.onhold && !it.held }) 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 + return Utils.isAudioMode(ctx, AudioManager.MODE_IN_CALL) } } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt index a92f2f25..48ccaf0c 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Utils.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Utils.kt @@ -26,6 +26,8 @@ import android.os.Environment import android.provider.DocumentsContract import android.provider.MediaStore import android.provider.OpenableColumns +import android.telecom.TelecomManager +import android.telecom.Call import android.text.format.DateUtils import androidx.activity.result.ActivityResultLauncher import androidx.annotation.RequiresApi @@ -849,10 +851,32 @@ object Utils { Configuration.UI_MODE_NIGHT_YES } + @Suppress("unused") fun isPSTNCallActive(ctx: Context): Boolean { - // MODE_IN_CALL indicates a PSTN call is active + val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as? TelecomManager ?: return false + try { + val getCallsMethod = TelecomManager::class.java.getMethod("getCalls") + val calls = getCallsMethod.invoke(tm) as? List<*> + if (calls != null) { + for (c in calls) { + if (c == null) continue + val call = c as? Call ?: continue + val state = call.javaClass.getMethod("getState").invoke(call) as Int + if (state == Call.STATE_ACTIVE || + state == Call.STATE_DIALING || + state == Call.STATE_CONNECTING) + return true + } + } + } catch (_: Exception) { + // treat as no active PSTN call + } + return false + } + + fun isAudioMode(ctx: Context, mode: Int): Boolean { val am = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager - return am.mode == AudioManager.MODE_IN_CALL + return am.mode == mode } fun relativeTime(ctx: Context, time: GregorianCalendar): String {