From a230f603c6ef8409c6aa240bafa406baacd481c4 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 5 Oct 2022 16:16:47 +0200 Subject: [PATCH] Improvements on use of Telecom Manager APIs to prevent dialog asking to terminate call when trying to add a new call to an existing conference --- .../org/linphone/telecom/NativeCallWrapper.kt | 10 +++++++ .../telecom/TelecomConnectionService.kt | 28 ++++++++++++++----- .../org/linphone/telecom/TelecomHelper.kt | 4 +-- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/linphone/telecom/NativeCallWrapper.kt b/app/src/main/java/org/linphone/telecom/NativeCallWrapper.kt index 2979327e6..a28ff57ca 100644 --- a/app/src/main/java/org/linphone/telecom/NativeCallWrapper.kt +++ b/app/src/main/java/org/linphone/telecom/NativeCallWrapper.kt @@ -19,6 +19,7 @@ */ package org.linphone.telecom +import android.annotation.TargetApi import android.graphics.drawable.Icon import android.os.Bundle import android.telecom.CallAudioState @@ -31,10 +32,15 @@ import org.linphone.core.Call import org.linphone.core.tools.Log import org.linphone.utils.AudioRouteUtils +@TargetApi(29) class NativeCallWrapper(var callId: String) : Connection() { init { + val properties = connectionProperties or PROPERTY_SELF_MANAGED + connectionProperties = properties + val capabilities = connectionCapabilities or CAPABILITY_MUTE or CAPABILITY_SUPPORT_HOLD or CAPABILITY_HOLD connectionCapabilities = capabilities + audioModeIsVoip = true statusHints = StatusHints( "", @@ -119,6 +125,10 @@ class NativeCallWrapper(var callId: String) : Connection() { coreContext.core.stopRinging() } + fun stateAsString(): String { + return intStateToString(state) + } + private fun getCall(): Call? { return coreContext.core.getCallByCallid(callId) } diff --git a/app/src/main/java/org/linphone/telecom/TelecomConnectionService.kt b/app/src/main/java/org/linphone/telecom/TelecomConnectionService.kt index 01717e429..9e779ddbb 100644 --- a/app/src/main/java/org/linphone/telecom/TelecomConnectionService.kt +++ b/app/src/main/java/org/linphone/telecom/TelecomConnectionService.kt @@ -19,6 +19,7 @@ */ package org.linphone.telecom +import android.annotation.TargetApi import android.content.ComponentName import android.content.Intent import android.net.Uri @@ -31,6 +32,7 @@ import org.linphone.core.Core import org.linphone.core.CoreListenerStub import org.linphone.core.tools.Log +@TargetApi(29) class TelecomConnectionService : ConnectionService() { private val listener: CoreListenerStub = object : CoreListenerStub() { override fun onCallStateChanged( @@ -51,7 +53,8 @@ class TelecomConnectionService : ConnectionService() { } Call.State.Error -> onCallError(call) Call.State.End, Call.State.Released -> onCallEnded(call) - Call.State.Connected -> onCallConnected(call) + Call.State.Paused, Call.State.Pausing, Call.State.PausedByRemote -> onCallPaused(call) + Call.State.Connected, Call.State.StreamsRunning -> onCallConnected(call) else -> {} } } @@ -87,7 +90,7 @@ class TelecomConnectionService : ConnectionService() { } override fun onCreateOutgoingConnection( - connectionManagerPhoneAccount: PhoneAccountHandle, + connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest ): Connection { if (coreContext.core.callsNb == 0) { @@ -149,7 +152,7 @@ class TelecomConnectionService : ConnectionService() { } override fun onCreateIncomingConnection( - connectionManagerPhoneAccount: PhoneAccountHandle, + connectionManagerPhoneAccount: PhoneAccountHandle?, request: ConnectionRequest ): Connection { if (coreContext.core.callsNb == 0) { @@ -215,6 +218,7 @@ class TelecomConnectionService : ConnectionService() { } TelecomHelper.get().connections.remove(connection) + Log.i("[Telecom Connection Service] Call [$callId] is in error, destroying connection currently in ${connection.stateAsString()}") connection.setDisconnected(DisconnectCause(DisconnectCause.ERROR)) connection.destroy() } @@ -229,11 +233,22 @@ class TelecomConnectionService : ConnectionService() { TelecomHelper.get().connections.remove(connection) val reason = call.reason - Log.i("[Telecom Connection Service] Call [$callId] ended with reason: $reason, destroying connection") + Log.i("[Telecom Connection Service] Call [$callId] ended with reason: $reason, destroying connection currently in ${connection.stateAsString()}") connection.setDisconnected(DisconnectCause(DisconnectCause.LOCAL)) connection.destroy() } + private fun onCallPaused(call: Call) { + val callId = call.callLog.callId + val connection = TelecomHelper.get().findConnectionForCallId(callId.orEmpty()) + if (connection == null) { + Log.e("[Telecom Connection Service] Failed to find connection for call id: $callId") + return + } + Log.i("[Telecom Connection Service] Setting connection as on hold, currently in ${connection.stateAsString()}") + connection.setOnHold() + } + private fun onCallConnected(call: Call) { val callId = call.callLog.callId val connection = TelecomHelper.get().findConnectionForCallId(callId.orEmpty()) @@ -242,8 +257,7 @@ class TelecomConnectionService : ConnectionService() { return } - if (connection.state != Connection.STATE_HOLDING) { - connection.setActive() - } + Log.i("[Telecom Connection Service] Setting connection as active, currently in ${connection.stateAsString()}") + connection.setActive() } } diff --git a/app/src/main/java/org/linphone/telecom/TelecomHelper.kt b/app/src/main/java/org/linphone/telecom/TelecomHelper.kt index aa2a6afaa..86783e83c 100644 --- a/app/src/main/java/org/linphone/telecom/TelecomHelper.kt +++ b/app/src/main/java/org/linphone/telecom/TelecomHelper.kt @@ -192,7 +192,7 @@ class TelecomHelper private constructor(context: Context) { } private fun onIncomingCall(call: Call) { - Log.i("[Telecom Helper] Incoming call received from ${call.remoteAddress.asStringUriOnly()}") + Log.i("[Telecom Helper] Incoming call received from ${call.remoteAddress.asStringUriOnly()}, using account handle ${account.accountHandle}") val extras = prepareBundle(call) telecomManager.addNewIncomingCall( @@ -206,7 +206,7 @@ class TelecomHelper private constructor(context: Context) { @SuppressLint("MissingPermission") private fun onOutgoingCall(call: Call) { - Log.i("[Telecom Helper] Outgoing call started to ${call.remoteAddress.asStringUriOnly()}") + Log.i("[Telecom Helper] Outgoing call started to ${call.remoteAddress.asStringUriOnly()}, using account handle ${account.accountHandle}") val extras = prepareBundle(call) telecomManager.placeCall(