From 07f3c49a8835a1ef19017083dba37ba016776432 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:30:39 +0200 Subject: [PATCH] feat: Return empty list on error for fetchDeviceMicrophones --- .../alibi/ui/utils/available-microphones.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/app/myzel394/alibi/ui/utils/available-microphones.kt b/app/src/main/java/app/myzel394/alibi/ui/utils/available-microphones.kt index 23991b5c7..8020ecb74 100644 --- a/app/src/main/java/app/myzel394/alibi/ui/utils/available-microphones.kt +++ b/app/src/main/java/app/myzel394/alibi/ui/utils/available-microphones.kt @@ -4,6 +4,7 @@ import android.content.Context import android.media.AudioDeviceInfo import android.media.AudioManager import android.os.Build +import android.util.Log val ALLOWED_MICROPHONE_TYPES = setOf( @@ -52,13 +53,19 @@ data class MicrophoneInfo( } fun fetchDeviceMicrophones(context: Context): List { - val audioManager = context.getSystemService(Context.AUDIO_SERVICE)!! as AudioManager - return (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { - audioManager.availableCommunicationDevices.map(::fromDeviceInfo) - } else { - audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).map(::fromDeviceInfo) - }).filter { - ALLOWED_MICROPHONE_TYPES.contains(it.deviceInfo.type) && it.deviceInfo.isSink + return try { + val audioManager = context.getSystemService(Context.AUDIO_SERVICE)!! as AudioManager + (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + audioManager.availableCommunicationDevices.map(::fromDeviceInfo) + } else { + audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS).map(::fromDeviceInfo) + }).filter { + ALLOWED_MICROPHONE_TYPES.contains(it.deviceInfo.type) && it.deviceInfo.isSink + } + } catch (error: Exception) { + Log.getStackTraceString(error) + + emptyList() } } }