Replaced util function isPSTNCallActive with isAudioMode

This commit is contained in:
Juha Heinanen
2026-04-21 18:38:51 +03:00
parent e8b9a54e0d
commit ecd6a4ad11
2 changed files with 27 additions and 4 deletions

View File

@ -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 // Check if there exist SIP calls that are not onhold or held
if (BaresipService.calls.any { !it.onhold && !it.held }) return true if (BaresipService.calls.any { !it.onhold && !it.held }) return true
// MODE_IN_CALL indicates a PSTN call is active // MODE_IN_CALL indicates a PSTN call is active
val am = ctx.getSystemService(Context.AUDIO_SERVICE) as AudioManager return Utils.isAudioMode(ctx, AudioManager.MODE_IN_CALL)
return am.mode == AudioManager.MODE_IN_CALL
} }
} }
} }

View File

@ -26,6 +26,8 @@ import android.os.Environment
import android.provider.DocumentsContract import android.provider.DocumentsContract
import android.provider.MediaStore import android.provider.MediaStore
import android.provider.OpenableColumns import android.provider.OpenableColumns
import android.telecom.TelecomManager
import android.telecom.Call
import android.text.format.DateUtils import android.text.format.DateUtils
import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultLauncher
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
@ -849,10 +851,32 @@ object Utils {
Configuration.UI_MODE_NIGHT_YES Configuration.UI_MODE_NIGHT_YES
} }
@Suppress("unused")
fun isPSTNCallActive(ctx: Context): Boolean { 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 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 { fun relativeTime(ctx: Context, time: GregorianCalendar): String {