Prevent call in state IDLE to be added to TelecomManager, wait for IncomingReceived or OutgoingProgress

This commit is contained in:
Sylvain Berfini 2025-02-24 12:16:05 +01:00
parent 7e2a4c124d
commit 296a324ba3

View file

@ -23,6 +23,7 @@ import android.content.Context
import android.net.Uri
import androidx.annotation.WorkerThread
import androidx.core.telecom.CallAttributesCompat
import androidx.core.telecom.CallException
import androidx.core.telecom.CallsManager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -51,8 +52,15 @@ class TelecomManager
private val coreListener = object : CoreListenerStub() {
@WorkerThread
override fun onCallCreated(core: Core, call: Call) {
onCallCreated(call)
override fun onCallStateChanged(
core: Core,
call: Call,
state: Call.State?,
message: String
) {
if (state == Call.State.IncomingReceived || state == Call.State.OutgoingProgress) {
onCallCreated(call)
}
}
@WorkerThread
@ -88,7 +96,7 @@ class TelecomManager
@WorkerThread
fun onCallCreated(call: Call) {
Log.i("$TAG Call to [${call.remoteAddress.asStringUriOnly()}] created")
Log.i("$TAG Call to [${call.remoteAddress.asStringUriOnly()}] created in state [${call.state}]")
val address = call.callLog.remoteAddress
val uri = Uri.parse(address.asStringUriOnly())
@ -99,9 +107,9 @@ class TelecomManager
CallAttributesCompat.DIRECTION_INCOMING
}
val conferenceInfo = LinphoneUtils.getConferenceInfoIfAny(call)
val capabilities = CallAttributesCompat.SUPPORTS_SET_INACTIVE or CallAttributesCompat.SUPPORTS_TRANSFER
val conferenceInfo = LinphoneUtils.getConferenceInfoIfAny(call)
val displayName = if (call.conference != null || conferenceInfo != null) {
conferenceInfo?.subject ?: call.conference?.subject ?: LinphoneUtils.getDisplayName(address)
} else {
@ -109,20 +117,24 @@ class TelecomManager
friend?.name ?: LinphoneUtils.getDisplayName(address)
}
// When call is created, it is ringing (incoming or outgoing, do not set video)
val type = CallAttributesCompat.Companion.CALL_TYPE_AUDIO_CALL
val callAttributes = CallAttributesCompat(
displayName,
uri,
direction,
type,
capabilities
)
Log.i("$TAG Adding call to Telecom's CallsManager with attributes [$callAttributes]")
val isVideo = LinphoneUtils.isVideoEnabled(call)
val type = if (isVideo) {
CallAttributesCompat.Companion.CALL_TYPE_VIDEO_CALL
} else {
CallAttributesCompat.Companion.CALL_TYPE_AUDIO_CALL
}
scope.launch {
try {
val callAttributes = CallAttributesCompat(
displayName,
uri,
direction,
type,
capabilities
)
Log.i("$TAG Adding call to Telecom's CallsManager with attributes [$callAttributes]")
callsManager.addCall(
callAttributes,
{ callType -> // onAnswer
@ -160,6 +172,11 @@ class TelecomManager
) {
val callbacks = TelecomCallControlCallback(call, this, scope)
// We must first call setCallback on callControlScope before using it
callbacks.onCallControlCallbackSet()
currentlyFollowedCalls += 1
Log.i("$TAG Call added to Telecom's CallsManager")
coreContext.postOnCoreThread {
val callId = call.callLog.callId.orEmpty()
if (callId.isNotEmpty()) {
@ -167,13 +184,8 @@ class TelecomManager
map[callId] = callbacks
}
}
// We must first call setCallback on callControlScope before using it
callbacks.onCallControlCallbackSet()
currentlyFollowedCalls += 1
Log.i("$TAG Call added to Telecom's CallsManager")
}
} catch (e: Exception) {
} catch (e: CallException) {
Log.e("$TAG Failed to add call to Telecom's CallsManager: $e")
}
}