Skip to content

Commit

Permalink
fix: crash when checking audio file size limit (WPB-5961) (#2757) (#2852
Browse files Browse the repository at this point in the history
)

Co-authored-by: Oussama Hassine <oussama.has100@gmail.com>
Co-authored-by: Michał Saleniuk <30429749+saleniuk@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 4, 2024
1 parent 23d925e commit ac7786b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 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,22 +76,23 @@ class AudioMediaRecorder @Inject constructor(
mediaRecorder?.setMaxFileSize(assetLimitInMegabyte)
mediaRecorder?.setOutputFile(outputFile)

observeAudioFileSize()
observeAudioFileSize(assetLimitInMegabyte)
}
}

fun startRecording() {
try {
fun startRecording(): Boolean = try {
mediaRecorder?.prepare()
mediaRecorder?.start()
true
} catch (e: IllegalStateException) {
e.printStackTrace()
appLogger.e("[RecordAudio] startRecording: IllegalStateException - ${e.message}")
false
} catch (e: IOException) {
e.printStackTrace()
appLogger.e("[RecordAudio] startRecording: IOException - ${e.message}")
false
}
}

fun stop() {
mediaRecorder?.stop()
Expand All @@ -112,7 +102,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 @@ -24,9 +24,16 @@ import com.wire.android.util.ui.UIText
sealed class RecordAudioInfoMessageType(override val uiText: UIText) : SnackBarMessage {

// Unable to Record Audio due to being in a call
object UnableToRecordAudioCall : RecordAudioInfoMessageType(
data object UnableToRecordAudioCall : RecordAudioInfoMessageType(
UIText.StringResource(
R.string.record_audio_unable_due_to_ongoing_call
)
)

// Unable to Record Audio due to error
data object UnableToRecordAudioError : RecordAudioInfoMessageType(
UIText.StringResource(
R.string.record_audio_unable_due_to_error
)
)
}
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,17 +132,18 @@ class RecordAudioViewModel @Inject constructor(
infoMessage.emit(RecordAudioInfoMessageType.UnableToRecordAudioCall.uiText)
}
} else {
audioMediaRecorder.setUp()

state = state.copy(
outputFile = audioMediaRecorder.outputFile
)

audioMediaRecorder.startRecording()

state = state.copy(
buttonState = RecordAudioButtonState.RECORDING
)
viewModelScope.launch {
val assetSizeLimit = getAssetSizeLimit(false)
audioMediaRecorder.setUp(assetSizeLimit)
if (audioMediaRecorder.startRecording()) {
state = state.copy(
outputFile = audioMediaRecorder.outputFile,
buttonState = RecordAudioButtonState.RECORDING
)
} else {
infoMessage.emit(RecordAudioInfoMessageType.UnableToRecordAudioError.uiText)
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@
<string name="record_audio_max_file_size_reached_title">Recording Stopped</string>
<string name="record_audio_max_file_size_reached_text">File size for audio messages is limited to %1$d MB.</string>
<string name="record_audio_unable_due_to_ongoing_call">You can’t record an audio message during a call.</string>
<string name="record_audio_unable_due_to_error">Something went wrong while trying to record audio message. Please try again.</string>
<string name="call_permission_dialog_title">App permission</string>
<string name="call_permission_dialog_description">To make a call, allow Wire to access your microphone in your device settings.</string>
<string name="label_not_now">Not Now</string>
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 @@ -216,19 +224,55 @@ class RecordAudioViewModelTest {
}
}

@Test
fun `given start recording succeeded, when recording audio, then recording screen is shown`() =
runTest {
// given
val (_, viewModel) = Arrangement()
.withStartRecordingSuccessful()
.arrange()

viewModel.getInfoMessage().test {
// when
viewModel.startRecording()
// then
assertEquals(RecordAudioButtonState.RECORDING, viewModel.getButtonState())
expectNoEvents()
}
}

@Test
fun `given start recording failed, when recording audio, then info message is shown`() =
runTest {
// given
val (_, viewModel) = Arrangement()
.withStartRecordingFailed()
.arrange()

viewModel.getInfoMessage().test {
// when
viewModel.startRecording()
// then
assertEquals(RecordAudioButtonState.ENABLED, viewModel.getButtonState())
assertEquals(RecordAudioInfoMessageType.UnableToRecordAudioError.uiText, awaitItem())
}
}

private class Arrangement {

val recordAudioMessagePlayer = mockk<RecordAudioMessagePlayer>()
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,8 +281,9 @@ class RecordAudioViewModelTest {

val fakeKaliumFileSystem = FakeKaliumFileSystem()

every { audioMediaRecorder.setUp() } returns Unit
every { audioMediaRecorder.startRecording() } returns Unit
coEvery { getAssetSizeLimit.invoke(false) } returns ASSET_SIZE_LIMIT
every { audioMediaRecorder.setUp(ASSET_SIZE_LIMIT) } returns Unit
every { audioMediaRecorder.startRecording() } returns true
every { audioMediaRecorder.stop() } returns Unit
every { audioMediaRecorder.release() } returns Unit
every { audioMediaRecorder.outputFile } returns fakeKaliumFileSystem
Expand Down Expand Up @@ -273,9 +318,13 @@ class RecordAudioViewModelTest {
)
}

fun withStartRecordingSuccessful() = apply { every { audioMediaRecorder.startRecording() } returns true }
fun withStartRecordingFailed() = apply { every { audioMediaRecorder.startRecording() } returns false }

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 ac7786b

Please sign in to comment.