From 967fb0563dfde3b7b3bfbbb75ec5416b89714ead Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Mon, 16 Dec 2024 14:36:52 +0100 Subject: [PATCH] Prevent outgoing calls to be routed on speaker while ringing --- .../notifications/NotificationsManager.kt | 15 ++--------- .../telecom/TelecomCallControlCallback.kt | 10 +++++-- .../org/linphone/telecom/TelecomManager.kt | 15 ++++++++--- .../ui/call/viewmodel/CurrentCallViewModel.kt | 7 +---- .../java/org/linphone/utils/LinphoneUtils.kt | 26 +++++++++++++++++++ 5 files changed, 48 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt index 52d8c1b6a..c814b1eae 100644 --- a/app/src/main/java/org/linphone/notifications/NotificationsManager.kt +++ b/app/src/main/java/org/linphone/notifications/NotificationsManager.kt @@ -1038,13 +1038,8 @@ class NotificationsManager val answerIntent = getCallAnswerPendingIntent(notifiable) val remoteAddress = call.callLog.remoteAddress - val remoteContactAddress = call.remoteContactAddress - val conferenceInfo = if (remoteContactAddress != null) { - call.core.findConferenceInformationFromUri(remoteContactAddress) ?: call.callLog.conferenceInfo - } else { - call.callLog.conferenceInfo - } val conference = call.conference + val conferenceInfo = LinphoneUtils.getConferenceInfoIfAny(call) val isConference = conference != null || conferenceInfo != null val caller = if (isConference) { @@ -1066,13 +1061,7 @@ class NotificationsManager getPerson(contact, displayName) } - val isVideo = if (isConference) { - true - } else if (isIncoming) { - call.remoteParams?.isVideoEnabled == true && call.remoteParams?.videoDirection != MediaDirection.Inactive - } else { - call.currentParams.isVideoEnabled && call.currentParams.videoDirection != MediaDirection.Inactive - } + val isVideo = LinphoneUtils.isVideoEnabled(call) val smallIcon = if (isConference) { R.drawable.video_conference diff --git a/app/src/main/java/org/linphone/telecom/TelecomCallControlCallback.kt b/app/src/main/java/org/linphone/telecom/TelecomCallControlCallback.kt index cf1bd9801..d546ef536 100644 --- a/app/src/main/java/org/linphone/telecom/TelecomCallControlCallback.kt +++ b/app/src/main/java/org/linphone/telecom/TelecomCallControlCallback.kt @@ -58,8 +58,14 @@ class TelecomCallControlCallback( if (state == Call.State.Connected) { if (call.dir == Call.Dir.Incoming) { scope.launch { - Log.i("$TAG Answering call") - callControl.answer(CallAttributesCompat.CALL_TYPE_AUDIO_CALL) + val isVideo = LinphoneUtils.isVideoEnabled(call) + Log.i("$TAG Answering ${if (isVideo) "video" else "audio"} call") + val type = if (isVideo) { + CallAttributesCompat.Companion.CALL_TYPE_VIDEO_CALL + } else { + CallAttributesCompat.Companion.CALL_TYPE_AUDIO_CALL + } + callControl.answer(type) } } else { scope.launch { diff --git a/app/src/main/java/org/linphone/telecom/TelecomManager.kt b/app/src/main/java/org/linphone/telecom/TelecomManager.kt index adaf5cdfb..994eca22e 100644 --- a/app/src/main/java/org/linphone/telecom/TelecomManager.kt +++ b/app/src/main/java/org/linphone/telecom/TelecomManager.kt @@ -91,9 +91,6 @@ class TelecomManager Log.i("$TAG Call to [${call.remoteAddress.asStringUriOnly()}] created") val address = call.callLog.remoteAddress - val friend = coreContext.contactsManager.findContactByAddress(address) - val displayName = friend?.name ?: LinphoneUtils.getDisplayName(address) - val uri = Uri.parse(address.asStringUriOnly()) val direction = if (call.dir == Call.Dir.Outgoing) { @@ -102,9 +99,19 @@ class TelecomManager CallAttributesCompat.DIRECTION_INCOMING } - val type = CallAttributesCompat.CALL_TYPE_AUDIO_CALL or CallAttributesCompat.CALL_TYPE_VIDEO_CALL + val conferenceInfo = LinphoneUtils.getConferenceInfoIfAny(call) val capabilities = CallAttributesCompat.SUPPORTS_SET_INACTIVE or CallAttributesCompat.SUPPORTS_TRANSFER + val displayName = if (call.conference != null || conferenceInfo != null) { + conferenceInfo?.subject ?: call.conference?.subject ?: LinphoneUtils.getDisplayName(address) + } else { + val friend = coreContext.contactsManager.findContactByAddress(address) + 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, diff --git a/app/src/main/java/org/linphone/ui/call/viewmodel/CurrentCallViewModel.kt b/app/src/main/java/org/linphone/ui/call/viewmodel/CurrentCallViewModel.kt index d245c2d04..eb1b14524 100644 --- a/app/src/main/java/org/linphone/ui/call/viewmodel/CurrentCallViewModel.kt +++ b/app/src/main/java/org/linphone/ui/call/viewmodel/CurrentCallViewModel.kt @@ -1064,12 +1064,7 @@ class CurrentCallViewModel updateEncryption() } - val remoteContactAddress = call.remoteContactAddress - val conferenceInfo = if (remoteContactAddress != null) { - call.core.findConferenceInformationFromUri(remoteContactAddress) - } else { - call.callLog.conferenceInfo - } + val conferenceInfo = LinphoneUtils.getConferenceInfoIfAny(call) if (call.conference != null || conferenceInfo != null) { val subject = call.conference?.subject ?: conferenceInfo?.subject Log.i("$TAG Conference [$subject] found, going to conference fragment") diff --git a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt index c23347829..d0a211ec9 100644 --- a/app/src/main/java/org/linphone/utils/LinphoneUtils.kt +++ b/app/src/main/java/org/linphone/utils/LinphoneUtils.kt @@ -48,6 +48,7 @@ import org.linphone.core.ConferenceScheduler import org.linphone.core.Core import org.linphone.core.Factory import org.linphone.core.Friend +import org.linphone.core.MediaDirection import org.linphone.core.Reason import org.linphone.core.tools.Log import org.linphone.ui.main.contacts.model.ContactAvatarModel @@ -216,6 +217,31 @@ class LinphoneUtils { return core.defaultAccount?.params?.audioVideoConferenceFactoryAddress != null } + @WorkerThread + fun isVideoEnabled(call: Call): Boolean { + val conference = call.conference + val isConference = conference != null + + val isIncoming = isCallIncoming(call.state) + return if (isConference || getConferenceInfoIfAny(call) != null) { + true + } else if (isIncoming) { + call.remoteParams?.isVideoEnabled == true && call.remoteParams?.videoDirection != MediaDirection.Inactive + } else { + call.currentParams.isVideoEnabled && call.currentParams.videoDirection != MediaDirection.Inactive + } + } + + @WorkerThread + fun getConferenceInfoIfAny(call: Call): ConferenceInfo? { + val remoteContactAddress = call.remoteContactAddress + return if (remoteContactAddress != null) { + call.core.findConferenceInformationFromUri(remoteContactAddress) ?: call.callLog.conferenceInfo + } else { + call.callLog.conferenceInfo + } + } + @WorkerThread fun createConferenceScheduler(account: Account?): ConferenceScheduler { if (!account?.params?.ccmpServerUrl.isNullOrEmpty()) {