Improved unused isPSTNCallActive util function

This commit is contained in:
Juha Heinanen
2026-04-22 09:58:17 +03:00
parent df4e6bbe0c
commit 27b99381d2
@@ -27,7 +27,7 @@ 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.TelecomManager
import android.telecom.Call import android.telecom.PhoneAccountHandle
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
@@ -854,22 +854,26 @@ object Utils {
@Suppress("unused") @Suppress("unused")
fun isPSTNCallActive(ctx: Context): Boolean { fun isPSTNCallActive(ctx: Context): Boolean {
val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as? TelecomManager ?: return false val tm = ctx.getSystemService(Context.TELECOM_SERVICE) as? TelecomManager ?: return false
val baresipHandle = BaresipService.getPhoneAccountHandle(ctx)
try { try {
val getCallsMethod = TelecomManager::class.java.getMethod("getCalls") val getCallsMethod = TelecomManager::class.java.getMethod("getCalls")
val calls = getCallsMethod.invoke(tm) as? List<*> val calls = getCallsMethod.invoke(tm) as? List<*>
if (calls != null) { if (calls != null) {
for (c in calls) { for (c in calls) {
if (c == null) continue if (c == null) continue
val call = c as? Call ?: continue val state = c.javaClass.getMethod("getState").invoke(c) as Int
val state = call.javaClass.getMethod("getState").invoke(call) as Int if (state == 1 || state == 2 || state == 3 || state == 4) { // 1=DIALING, 2=RINGING, 3=HOLDING, 4=ACTIVE
if (state == Call.STATE_ACTIVE || val details = c.javaClass.getMethod("getDetails").invoke(c)
state == Call.STATE_DIALING || val accountHandle = details?.javaClass?.getMethod("getAccountHandle")?.invoke(details) as? PhoneAccountHandle
state == Call.STATE_CONNECTING) if (accountHandle != baresipHandle) {
Log.d(TAG, "External call detected from account: $accountHandle")
return true return true
} }
} }
} catch (_: Exception) { }
// treat as no active PSTN call }
} catch (e: Exception) {
Log.e(TAG, "isPSTNCallActive reflection error: $e")
} }
return false return false
} }