Skip to content

Commit

Permalink
fix: ANR when recording an audio (WPB-9528) (#3066)
Browse files Browse the repository at this point in the history
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
2 people authored and github-actions[bot] committed Jun 24, 2024
1 parent 094c1aa commit 4d46e61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down Expand Up @@ -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()) {
Expand All @@ -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
)
)
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -277,6 +278,7 @@ class RecordAudioViewModelTest {
val globalDataStore = mockk<GlobalDataStore>()
val generateAudioFileWithEffects = mockk<GenerateAudioFileWithEffectsUseCase>()
val context = mockk<Context>()
val dispatchers = TestDispatcherProvider()

val viewModel by lazy {
RecordAudioViewModel(
Expand All @@ -287,7 +289,8 @@ class RecordAudioViewModelTest {
audioMediaRecorder = audioMediaRecorder,
getAssetSizeLimit = getAssetSizeLimit,
generateAudioFileWithEffects = generateAudioFileWithEffects,
globalDataStore = globalDataStore
globalDataStore = globalDataStore,
dispatchers = dispatchers
)
}

Expand Down

0 comments on commit 4d46e61

Please sign in to comment.