Fixed bug in checking if call already exists

This commit is contained in:
Juha Heinanen
2026-04-02 19:04:08 +03:00
parent eb52a1a8d3
commit 91f28f03df
5 changed files with 27 additions and 17 deletions

View File

@ -801,7 +801,7 @@ class BaresipService: Service() {
"incoming call" -> { "incoming call" -> {
val peerUri = ev[1] val peerUri = ev[1]
val bevent = ev[2].toLong() val bevent = ev[2].toLong()
val toastMsg = if (Call.isAnyCallActive(applicationContext)) val toastMsg = if (Utils.isAnyCallActive(applicationContext))
String.format(getString(R.string.call_auto_rejected), String.format(getString(R.string.call_auto_rejected),
Utils.friendlyUri(this, peerUri, ua.account)) Utils.friendlyUri(this, peerUri, ua.account))
else if (ua.account.blockUnknown && Contact.contactName(peerUri) == peerUri) else if (ua.account.blockUnknown && Contact.contactName(peerUri) == peerUri)
@ -1541,8 +1541,7 @@ class BaresipService: Service() {
@SuppressLint("WakelockTimeout") @SuppressLint("WakelockTimeout")
private fun updatePartialWakeLock() { private fun updatePartialWakeLock() {
val isAnyUaActive = uasStatus.value.values.any { it != R.drawable.circle_white } val isAnyUaActive = uasStatus.value.values.any { it != R.drawable.circle_white }
val isAnyCallActive = calls.isNotEmpty() val needsToStayAwake = isAnyUaActive || calls.isNotEmpty()
val needsToStayAwake = isAnyUaActive || isAnyCallActive
try { try {
if (needsToStayAwake) { if (needsToStayAwake) {
if (!partialWakeLock.isHeld) { if (!partialWakeLock.isHeld) {

View File

@ -1,7 +1,5 @@
package com.tutpro.baresip package com.tutpro.baresip
import android.content.Context
import android.media.AudioManager
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -144,13 +142,5 @@ class Call(val callp: Long, val ua: UserAgent, val peerUri: String, val dir: Str
fun inCall(): Boolean { fun inCall(): Boolean {
return BaresipService.calls.isNotEmpty() 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
}
} }
} }

View File

@ -206,13 +206,23 @@ class ConnectionService : ConnectionService() {
override fun onHold() { override fun onHold() {
Log.d(TAG, "Telecom Connection onHold $callp") Log.d(TAG, "Telecom Connection onHold $callp")
Api.call_hold(callp, true) val call = Call.ofCallp(callp)
if (call == null || !call.conferenceCall) {
Api.call_hold(callp, true)
} else {
Log.d(TAG, "Call $callp is in conference, skipping baresip hold")
}
setOnHold() setOnHold()
} }
override fun onUnhold() { override fun onUnhold() {
Log.d(TAG, "Telecom Connection onUnhold $callp") Log.d(TAG, "Telecom Connection onUnhold $callp")
Api.call_hold(callp, false) val call = Call.ofCallp(callp)
if (call == null || !call.conferenceCall) {
Api.call_hold(callp, false)
} else {
Log.d(TAG, "Call $callp is in conference, skipping baresip resume")
}
setActive() setActive()
} }

View File

@ -2061,7 +2061,7 @@ private fun makeCall(ctx: Context, viewModel: ViewModel, uriText: String, confer
alertMessage.value = String.format(ctx.getString(R.string.invalid_sip_or_tel_uri), uri) alertMessage.value = String.format(ctx.getString(R.string.invalid_sip_or_tel_uri), uri)
showAlert.value = true showAlert.value = true
} }
else if (Call.isAnyCallActive(ctx)) else if (Utils.isPSTNCallActive(ctx) || Call.calls().any { it.ua.account.aor != ua.account.aor } )
Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show() Toast.makeText(ctx, R.string.call_already_active, Toast.LENGTH_SHORT).show()
else { else {
val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as android.telecom.TelecomManager val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as android.telecom.TelecomManager

View File

@ -41,6 +41,7 @@ import androidx.core.text.isDigitsOnly
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner
import androidx.navigation.NavController import androidx.navigation.NavController
import com.tutpro.baresip.Call.Companion.inCall
import java.io.BufferedInputStream import java.io.BufferedInputStream
import java.io.BufferedOutputStream import java.io.BufferedOutputStream
import java.io.File import java.io.File
@ -841,6 +842,16 @@ object Utils {
Configuration.UI_MODE_NIGHT_YES Configuration.UI_MODE_NIGHT_YES
} }
fun isAnyCallActive(ctx: Context): Boolean {
return inCall() || isPSTNCallActive(ctx)
}
fun isPSTNCallActive(ctx: Context): Boolean {
// 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
}
fun relativeTime(ctx: Context, time: GregorianCalendar): String { fun relativeTime(ctx: Context, time: GregorianCalendar): String {
return if (DateUtils.isToday(time.timeInMillis)) { return if (DateUtils.isToday(time.timeInMillis)) {
val fmt = DateFormat.getTimeInstance(DateFormat.SHORT) val fmt = DateFormat.getTimeInstance(DateFormat.SHORT)
@ -887,7 +898,7 @@ object Utils {
Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE") Log.d(TAG, "Setting com device to TYPE_BUILTIN_EARPIECE")
if (!am.setCommunicationDevice(speakerDevice)) if (!am.setCommunicationDevice(speakerDevice))
Log.e(TAG, "Could not set com device") Log.e(TAG, "Could not set com device")
if (Call.inCall() && am.mode == AudioManager.MODE_NORMAL) { if (inCall() && am.mode == AudioManager.MODE_NORMAL) {
Log.d(TAG, "Setting mode to communication") Log.d(TAG, "Setting mode to communication")
am.mode = AudioManager.MODE_IN_COMMUNICATION am.mode = AudioManager.MODE_IN_COMMUNICATION
} }