From 64f3808c9d481fd49696bc498153471e2731ef89 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 27 May 2021 15:05:04 +0200 Subject: [PATCH] Factorized code --- .../org/linphone/utils/AudioRouteUtils.kt | 104 +++++------------- 1 file changed, 27 insertions(+), 77 deletions(-) diff --git a/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt b/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt index 0e97565eb..047923c49 100644 --- a/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt +++ b/app/src/main/java/org/linphone/utils/AudioRouteUtils.kt @@ -26,105 +26,55 @@ import org.linphone.core.tools.Log class AudioRouteUtils { companion object { - fun routeAudioToEarpiece(call: Call? = null) { + private fun routeAudioTo(types : List, call: Call? = null) { + val listSize = types.size + val stringBuilder = StringBuilder() + var index = 0 + while (index < listSize) { + stringBuilder.append(types[index].name) + if (index < listSize - 1) { + stringBuilder.append("/") + } + index++ + } + val typesNames = stringBuilder.toString() + if (coreContext.core.callsNb == 0) { - Log.e("[Audio Route Helper] No call found, aborting earpiece audio route change") + Log.e("[Audio Route Helper] No call found, aborting [$typesNames] audio route change") return } val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0] val conference = coreContext.core.conference for (audioDevice in coreContext.core.audioDevices) { - if (audioDevice.type == AudioDevice.Type.Earpiece) { + if (types.contains(audioDevice.type) && audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { if (conference != null && conference.isIn) { - Log.i("[Audio Route Helper] Found earpiece audio device [${audioDevice.deviceName}], routing audio of conference to it") + Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing conference audio to it") conference.outputAudioDevice = audioDevice - return } else { - Log.i("[Audio Route Helper] Found earpiece audio device [${audioDevice.deviceName}], routing audio of call to it") + Log.i("[Audio Route Helper] Found [${audioDevice.type}] audio device [${audioDevice.deviceName}], routing call audio to it") currentCall.outputAudioDevice = audioDevice - return } - } + return + } } - Log.e("[Audio Route Helper] Couldn't find earpiece audio device") + Log.e("[Audio Route Helper] Couldn't find [$typesNames] audio device") + } + + fun routeAudioToEarpiece(call: Call? = null) { + routeAudioTo(arrayListOf(AudioDevice.Type.Earpiece), call) } fun routeAudioToSpeaker(call: Call? = null) { - if (coreContext.core.callsNb == 0) { - Log.e("[Audio Route Helper] No call found, aborting speaker audio route change") - return - } - - val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0] - val conference = coreContext.core.conference - - for (audioDevice in coreContext.core.audioDevices) { - if (audioDevice.type == AudioDevice.Type.Speaker) { - if (conference != null && conference.isIn) { - Log.i("[Audio Route Helper] Found speaker audio device [${audioDevice.deviceName}], routing audio of conference to it") - conference.outputAudioDevice = audioDevice - return - } else { - Log.i("[Audio Route Helper] Found speaker audio device [${audioDevice.deviceName}], routing audio of call to it") - currentCall.outputAudioDevice = audioDevice - return - } - } - } - Log.e("[Audio Route Helper] Couldn't find speaker audio device") + routeAudioTo(arrayListOf(AudioDevice.Type.Speaker), call) } fun routeAudioToBluetooth(call: Call? = null) { - if (coreContext.core.callsNb == 0) { - Log.e("[Audio Route Helper] No call found, aborting bluetooth audio route change") - return - } - val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0] - val conference = coreContext.core.conference - - for (audioDevice in coreContext.core.audioDevices) { - if (audioDevice.type == AudioDevice.Type.Bluetooth) { - if (audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { - if (conference != null && conference.isIn) { - Log.i("[Audio Route Helper] Found bluetooth audio device [${audioDevice.deviceName}], routing audio of conference to it") - conference.outputAudioDevice = audioDevice - return - } else { - Log.i("[Audio Route Helper] Found bluetooth audio device [${audioDevice.deviceName}], routing audio of call to it") - currentCall.outputAudioDevice = audioDevice - return - } - } - } - } - Log.e("[Audio Route Helper] Couldn't find bluetooth audio device") + routeAudioTo(arrayListOf(AudioDevice.Type.Bluetooth), call) } fun routeAudioToHeadset(call: Call? = null) { - if (coreContext.core.callsNb == 0) { - Log.e("[Audio Route Helper] No call found, aborting headset audio route change") - return - } - val currentCall = call ?: coreContext.core.currentCall ?: coreContext.core.calls[0] - val conference = coreContext.core.conference - - for (audioDevice in coreContext.core.audioDevices) { - if (audioDevice.type == AudioDevice.Type.Headphones || audioDevice.type == AudioDevice.Type.Headset) { - if (audioDevice.hasCapability(AudioDevice.Capabilities.CapabilityPlay)) { - if (conference != null && conference.isIn) { - Log.i("[Audio Route Helper] Found headset audio device [${audioDevice.deviceName}], routing audio of conference to it") - conference.outputAudioDevice = audioDevice - return - } else { - Log.i("[Audio Route Helper] Found headset audio device [${audioDevice.deviceName}], routing audio of call to it") - currentCall.outputAudioDevice = audioDevice - return - } - } - } - } - Log.e("[Audio Route Helper] Couldn't find headset audio device") + routeAudioTo(arrayListOf(AudioDevice.Type.Headphones, AudioDevice.Type.Headset), call) } fun isSpeakerAudioRouteCurrentlyUsed(call: Call? = null): Boolean {