From 3ad245db8bbc5e9c5254909bc073ac0515e82d4e Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 13 Oct 2022 23:45:49 +0200 Subject: [PATCH] Trigger VoiceBroadcast recording actions --- .../usecase/PauseVoiceBroadcastUseCase.kt | 6 +-- .../usecase/ResumeVoiceBroadcastUseCase.kt | 6 +-- .../usecase/StartVoiceBroadcastUseCase.kt | 46 ++++++++++++++++--- .../usecase/StopVoiceBroadcastUseCase.kt | 9 ++-- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/PauseVoiceBroadcastUseCase.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/PauseVoiceBroadcastUseCase.kt index 6c779b7a50..1ba3f7aaa9 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/PauseVoiceBroadcastUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/PauseVoiceBroadcastUseCase.kt @@ -16,8 +16,8 @@ package im.vector.app.features.voicebroadcast.usecase -import im.vector.app.features.home.room.detail.composer.AudioMessageHelper import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO +import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent @@ -32,7 +32,7 @@ import javax.inject.Inject class PauseVoiceBroadcastUseCase @Inject constructor( private val session: Session, - private val audioMessageHelper: AudioMessageHelper, + private val voiceBroadcastRecorder: VoiceBroadcastRecorder?, ) { suspend fun execute(roomId: String): Result = runCatching { @@ -66,6 +66,6 @@ class PauseVoiceBroadcastUseCase @Inject constructor( } private fun pauseRecording() { - audioMessageHelper.pauseRecording() + voiceBroadcastRecorder?.pauseRecord() } } diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/ResumeVoiceBroadcastUseCase.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/ResumeVoiceBroadcastUseCase.kt index fbbbc32612..69d3fe99a9 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/ResumeVoiceBroadcastUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/ResumeVoiceBroadcastUseCase.kt @@ -16,8 +16,8 @@ package im.vector.app.features.voicebroadcast.usecase -import im.vector.app.features.home.room.detail.composer.AudioMessageHelper import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO +import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent @@ -32,7 +32,7 @@ import javax.inject.Inject class ResumeVoiceBroadcastUseCase @Inject constructor( private val session: Session, - private val audioMessageHelper: AudioMessageHelper, + private val voiceBroadcastRecorder: VoiceBroadcastRecorder?, ) { suspend fun execute(roomId: String): Result = runCatching { @@ -71,6 +71,6 @@ class ResumeVoiceBroadcastUseCase @Inject constructor( } private fun resumeRecording() { - audioMessageHelper.resumeRecording() + voiceBroadcastRecorder?.resumeRecord() } } diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StartVoiceBroadcastUseCase.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StartVoiceBroadcastUseCase.kt index 03dbbed5e4..f2d1df0ea4 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StartVoiceBroadcastUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StartVoiceBroadcastUseCase.kt @@ -16,22 +16,33 @@ package im.vector.app.features.voicebroadcast.usecase -import im.vector.app.features.home.room.detail.composer.AudioMessageHelper +import android.content.Context +import android.os.Build +import androidx.core.content.FileProvider +import im.vector.app.core.resources.BuildMeta +import im.vector.app.features.attachments.toContentAttachmentData import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO +import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent +import im.vector.lib.multipicker.utils.toMultiPickerAudioType import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.session.Session +import org.matrix.android.sdk.api.session.events.model.RelationType import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.getRoom import org.matrix.android.sdk.api.session.room.Room +import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent import timber.log.Timber +import java.io.File import javax.inject.Inject class StartVoiceBroadcastUseCase @Inject constructor( private val session: Session, - private val audioMessageHelper: AudioMessageHelper, + private val voiceBroadcastRecorder: VoiceBroadcastRecorder?, + private val context: Context, + private val buildMeta: BuildMeta, ) { suspend fun execute(roomId: String): Result = runCatching { @@ -55,7 +66,7 @@ class StartVoiceBroadcastUseCase @Inject constructor( private suspend fun startVoiceBroadcast(room: Room) { Timber.d("## StartVoiceBroadcastUseCase: Send new voice broadcast info state event") - room.stateService().sendStateEvent( + val eventId = room.stateService().sendStateEvent( eventType = STATE_ROOM_VOICE_BROADCAST_INFO, stateKey = session.myUserId, body = MessageVoiceBroadcastInfoContent( @@ -64,10 +75,33 @@ class StartVoiceBroadcastUseCase @Inject constructor( ).toContent() ) - startRecording(room) + startRecording(room, eventId) } - private fun startRecording(room: Room) { - audioMessageHelper.startRecording(room.roomId) + private fun startRecording(room: Room, eventId: String) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + voiceBroadcastRecorder?.listener = VoiceBroadcastRecorder.Listener { file -> + sendVoiceFile(room, file, eventId) + } + voiceBroadcastRecorder?.startRecord(room.roomId) + } + } + + private fun sendVoiceFile(room: Room, voiceMessageFile: File, referenceEventId: String) { + val outputFileUri = FileProvider.getUriForFile( + context, + buildMeta.applicationId + ".fileProvider", + voiceMessageFile, + "Voice message.${voiceMessageFile.extension}" + ) + val audioType = outputFileUri.toMultiPickerAudioType(context) ?: return + if (audioType.duration > 1000) { + room.sendService().sendMedia( + attachment = audioType.toContentAttachmentData(isVoiceMessage = true), + compressBeforeSending = false, + roomIds = emptySet(), + relatesTo = RelationDefaultContent(RelationType.REFERENCE, referenceEventId) + ) + } } } diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StopVoiceBroadcastUseCase.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StopVoiceBroadcastUseCase.kt index ed868fced8..7f352fc591 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StopVoiceBroadcastUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/usecase/StopVoiceBroadcastUseCase.kt @@ -16,8 +16,9 @@ package im.vector.app.features.voicebroadcast.usecase -import im.vector.app.features.home.room.detail.composer.AudioMessageHelper +import android.os.Build import im.vector.app.features.voicebroadcast.STATE_ROOM_VOICE_BROADCAST_INFO +import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent @@ -32,7 +33,7 @@ import javax.inject.Inject class StopVoiceBroadcastUseCase @Inject constructor( private val session: Session, - private val audioMessageHelper: AudioMessageHelper, + private val voiceBroadcastRecorder: VoiceBroadcastRecorder?, ) { suspend fun execute(roomId: String): Result = runCatching { @@ -67,6 +68,8 @@ class StopVoiceBroadcastUseCase @Inject constructor( } private fun stopRecording() { - audioMessageHelper.stopRecording() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + voiceBroadcastRecorder?.stopRecord() + } } }