Skip to content

Commit

Permalink
fix: crash when checking audio file size limit (WPB-5961) (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine authored Mar 1, 2024
1 parent 84d3737 commit c8e59af
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import com.wire.android.appLogger
import com.wire.android.util.audioFileDateTime
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.asset.KaliumFileSystem
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
import com.wire.kalium.util.DateTimeUtil
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.CoroutineScope
Expand All @@ -36,14 +35,12 @@ import kotlinx.coroutines.launch
import java.io.File
import java.io.IOException
import javax.inject.Inject
import kotlin.properties.Delegates

@ViewModelScoped
class AudioMediaRecorder @Inject constructor(
private val context: Context,
private val kaliumFileSystem: KaliumFileSystem,
private val dispatcherProvider: DispatcherProvider,
private val getAssetSizeLimit: GetAssetSizeLimitUseCase
private val dispatcherProvider: DispatcherProvider
) {

private val scope by lazy {
Expand All @@ -52,21 +49,13 @@ class AudioMediaRecorder @Inject constructor(

private var mediaRecorder: MediaRecorder? = null

private var assetLimitInMegabyte by Delegates.notNull<Long>()

var outputFile: File? = null

private val _maxFileSizeReached = MutableSharedFlow<RecordAudioDialogState>()
fun getMaxFileSizeReached(): Flow<RecordAudioDialogState> =
_maxFileSizeReached.asSharedFlow()

init {
scope.launch {
assetLimitInMegabyte = getAssetSizeLimit(isImage = false)
}
}

fun setUp() {
fun setUp(assetLimitInMegabyte: Long) {
if (mediaRecorder == null) {
mediaRecorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
MediaRecorder(context)
Expand All @@ -87,7 +76,7 @@ class AudioMediaRecorder @Inject constructor(
mediaRecorder?.setMaxFileSize(assetLimitInMegabyte)
mediaRecorder?.setOutputFile(outputFile)

observeAudioFileSize()
observeAudioFileSize(assetLimitInMegabyte)
}
}

Expand All @@ -112,7 +101,7 @@ class AudioMediaRecorder @Inject constructor(
mediaRecorder?.release()
}

private fun observeAudioFileSize() {
private fun observeAudioFileSize(assetLimitInMegabyte: Long) {
mediaRecorder?.setOnInfoListener { _, what, _ ->
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) {
scope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.wire.android.util.CurrentScreen
import com.wire.android.util.CurrentScreenManager
import com.wire.android.util.getAudioLengthInMs
import com.wire.android.util.ui.UIText
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
Expand All @@ -47,6 +48,7 @@ import kotlin.io.path.deleteIfExists
class RecordAudioViewModel @Inject constructor(
private val recordAudioMessagePlayer: RecordAudioMessagePlayer,
private val observeEstablishedCalls: ObserveEstablishedCallsUseCase,
private val getAssetSizeLimit: GetAssetSizeLimitUseCase,
private val currentScreenManager: CurrentScreenManager,
private val audioMediaRecorder: AudioMediaRecorder
) : ViewModel() {
Expand Down Expand Up @@ -130,7 +132,10 @@ class RecordAudioViewModel @Inject constructor(
infoMessage.emit(RecordAudioInfoMessageType.UnableToRecordAudioCall.uiText)
}
} else {
audioMediaRecorder.setUp()
viewModelScope.launch {
val assetSizeLimit = getAssetSizeLimit(false)
audioMediaRecorder.setUp(assetSizeLimit)
}

state = state.copy(
outputFile = audioMediaRecorder.outputFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ import com.wire.android.config.CoroutineTestExtension
import com.wire.android.framework.FakeKaliumFileSystem
import com.wire.android.media.audiomessage.AudioState
import com.wire.android.media.audiomessage.RecordAudioMessagePlayer
import com.wire.android.ui.home.messagecomposer.recordaudio.RecordAudioViewModelTest.Arrangement.Companion.ASSET_SIZE_LIMIT
import com.wire.android.util.CurrentScreen
import com.wire.android.util.CurrentScreenManager
import com.wire.kalium.logic.data.call.Call
import com.wire.kalium.logic.data.call.CallStatus
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCase
import com.wire.kalium.logic.feature.asset.GetAssetSizeLimitUseCaseImpl
import com.wire.kalium.logic.feature.call.usecase.ObserveEstablishedCallsUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -69,7 +73,7 @@ class RecordAudioViewModelTest {
fun `given user is not in a call, when start recording audio, then recording screen is shown`() =
runTest {
// given
val (_, viewModel) = Arrangement()
val (arrangement, viewModel) = Arrangement()
.arrange()

// when
Expand All @@ -80,6 +84,10 @@ class RecordAudioViewModelTest {
RecordAudioButtonState.RECORDING,
viewModel.getButtonState()
)
coVerify(exactly = 1) { arrangement.getAssetSizeLimit(false) }
verify(exactly = 1) { arrangement.audioMediaRecorder.setUp(ASSET_SIZE_LIMIT) }
verify(exactly = 1) { arrangement.audioMediaRecorder.setUp(ASSET_SIZE_LIMIT) }
verify(exactly = 1) { arrangement.audioMediaRecorder.startRecording() }
}

@Test
Expand Down Expand Up @@ -222,13 +230,15 @@ class RecordAudioViewModelTest {
val audioMediaRecorder = mockk<AudioMediaRecorder>()
val observeEstablishedCalls = mockk<ObserveEstablishedCallsUseCase>()
val currentScreenManager = mockk<CurrentScreenManager>()
val getAssetSizeLimit = mockk<GetAssetSizeLimitUseCase>()

val viewModel by lazy {
RecordAudioViewModel(
recordAudioMessagePlayer = recordAudioMessagePlayer,
observeEstablishedCalls = observeEstablishedCalls,
currentScreenManager = currentScreenManager,
audioMediaRecorder = audioMediaRecorder
audioMediaRecorder = audioMediaRecorder,
getAssetSizeLimit = getAssetSizeLimit,
)
}

Expand All @@ -237,7 +247,8 @@ class RecordAudioViewModelTest {

val fakeKaliumFileSystem = FakeKaliumFileSystem()

every { audioMediaRecorder.setUp() } returns Unit
coEvery { getAssetSizeLimit.invoke(false) } returns ASSET_SIZE_LIMIT
every { audioMediaRecorder.setUp(ASSET_SIZE_LIMIT) } returns Unit
every { audioMediaRecorder.startRecording() } returns Unit
every { audioMediaRecorder.stop() } returns Unit
every { audioMediaRecorder.release() } returns Unit
Expand Down Expand Up @@ -276,6 +287,7 @@ class RecordAudioViewModelTest {
fun arrange() = this to viewModel

companion object {
const val ASSET_SIZE_LIMIT = 5L
val DUMMY_CALL = Call(
conversationId = ConversationId(
value = "conversationId",
Expand Down

0 comments on commit c8e59af

Please sign in to comment.