From b0b485c933a5a4c5c5a1fc04f78dc67fc94e0150 Mon Sep 17 00:00:00 2001 From: Oussama Hassine Date: Mon, 24 Jun 2024 11:08:28 +0200 Subject: [PATCH] fix: ANR when recording an audio (WPB-9528) (#3066) Co-authored-by: Yamil Medina --- .../recordaudio/RecordAudioViewModel.kt | 50 ++++++++++--------- .../recordaudio/RecordAudioViewModelTest.kt | 5 +- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt b/app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt index 5c482edfd83..244490f61bc 100644 --- a/app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt +++ b/app/src/main/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModel.kt @@ -33,6 +33,7 @@ import com.wire.android.ui.home.conversations.model.UriAsset import com.wire.android.util.AUDIO_MIME_TYPE import com.wire.android.util.CurrentScreen import com.wire.android.util.CurrentScreenManager +import com.wire.android.util.dispatchers.DispatcherProvider import com.wire.android.util.getAudioLengthInMs import com.wire.android.util.ui.UIText import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase @@ -59,7 +60,8 @@ class RecordAudioViewModel @Inject constructor( private val generateAudioFileWithEffects: GenerateAudioFileWithEffectsUseCase, private val currentScreenManager: CurrentScreenManager, private val audioMediaRecorder: AudioMediaRecorder, - private val globalDataStore: GlobalDataStore + private val globalDataStore: GlobalDataStore, + private val dispatchers: DispatcherProvider ) : ViewModel() { var state: RecordAudioState by mutableStateOf(RecordAudioState()) @@ -152,7 +154,7 @@ class RecordAudioViewModel @Inject constructor( infoMessage.emit(RecordAudioInfoMessageType.UnableToRecordAudioCall.uiText) } } else { - viewModelScope.launch { + viewModelScope.launch(dispatchers.default()) { val assetSizeLimit = getAssetSizeLimit(false) audioMediaRecorder.setUp(assetSizeLimit) if (audioMediaRecorder.startRecording()) { @@ -169,31 +171,33 @@ class RecordAudioViewModel @Inject constructor( } fun stopRecording() { - if (state.buttonState == RecordAudioButtonState.RECORDING) { - audioMediaRecorder.stop() - } - audioMediaRecorder.release() + viewModelScope.launch(dispatchers.default()) { + if (state.buttonState == RecordAudioButtonState.RECORDING) { + audioMediaRecorder.stop() + } + audioMediaRecorder.release() - if (state.originalOutputFile != null && state.effectsOutputFile != null) { - generateAudioFileWithEffects( - context = context, - originalFilePath = state.originalOutputFile!!.path, - effectsFilePath = state.effectsOutputFile!!.path - ) + if (state.originalOutputFile != null && state.effectsOutputFile != null) { + generateAudioFileWithEffects( + context = context, + originalFilePath = state.originalOutputFile!!.path, + effectsFilePath = state.effectsOutputFile!!.path + ) - state = state.copy( - buttonState = RecordAudioButtonState.READY_TO_SEND, - audioState = AudioState.DEFAULT.copy( - totalTimeInMs = AudioState.TotalTimeInMs.Known( - getPlayableAudioFile()?.let { - getAudioLengthInMs( - dataPath = it.path.toPath(), - mimeType = AUDIO_MIME_TYPE - ).toInt() - } ?: 0 + state = state.copy( + buttonState = RecordAudioButtonState.READY_TO_SEND, + audioState = AudioState.DEFAULT.copy( + totalTimeInMs = AudioState.TotalTimeInMs.Known( + getPlayableAudioFile()?.let { + getAudioLengthInMs( + dataPath = it.path.toPath(), + mimeType = AUDIO_MIME_TYPE + ).toInt() + } ?: 0 + ) ) ) - ) + } } } diff --git a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModelTest.kt b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModelTest.kt index 5b3ed567499..ba1bf91ab05 100644 --- a/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModelTest.kt +++ b/app/src/test/kotlin/com/wire/android/ui/home/messagecomposer/recordaudio/RecordAudioViewModelTest.kt @@ -20,6 +20,7 @@ package com.wire.android.ui.home.messagecomposer.recordaudio import android.content.Context import app.cash.turbine.test import com.wire.android.config.CoroutineTestExtension +import com.wire.android.config.TestDispatcherProvider import com.wire.android.datastore.GlobalDataStore import com.wire.android.framework.FakeKaliumFileSystem import com.wire.android.media.audiomessage.AudioState @@ -277,6 +278,7 @@ class RecordAudioViewModelTest { val globalDataStore = mockk() val generateAudioFileWithEffects = mockk() val context = mockk() + val dispatchers = TestDispatcherProvider() val viewModel by lazy { RecordAudioViewModel( @@ -287,7 +289,8 @@ class RecordAudioViewModelTest { audioMediaRecorder = audioMediaRecorder, getAssetSizeLimit = getAssetSizeLimit, generateAudioFileWithEffects = generateAudioFileWithEffects, - globalDataStore = globalDataStore + globalDataStore = globalDataStore, + dispatchers = dispatchers ) }