From 251c54ecfe40d5193366a0af1aa67e9307b723c6 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 23 Nov 2021 17:49:44 +0000 Subject: [PATCH] creating unique files for each voice recording - stops the recording flow from deleting/overwriting pending voice messages --- .../room/detail/composer/VoiceMessageHelper.kt | 5 ++--- .../app/features/voice/AbstractVoiceRecorder.kt | 14 +++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageHelper.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageHelper.kt index adcd6a3008..5d351e843f 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageHelper.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/VoiceMessageHelper.kt @@ -76,9 +76,8 @@ class VoiceMessageHelper @Inject constructor( } try { voiceMessageFile?.let { - val outputFileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", it) - return outputFileUri - ?.toMultiPickerAudioType(context) + val outputFileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", it, "Voice message.${it.extension}") + return outputFileUri.toMultiPickerAudioType(context) ?.apply { waveform = if (amplitudeList.size < 50) { amplitudeList diff --git a/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt b/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt index 1a91c00e11..5d8bce1ea3 100644 --- a/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt +++ b/vector/src/main/java/im/vector/app/features/voice/AbstractVoiceRecorder.kt @@ -18,9 +18,12 @@ package im.vector.app.features.voice import android.content.Context import android.media.MediaRecorder +import android.net.Uri import android.os.Build +import org.matrix.android.sdk.api.session.content.ContentAttachmentData import java.io.File import java.io.FileOutputStream +import java.util.UUID abstract class AbstractVoiceRecorder( private val context: Context, @@ -59,7 +62,7 @@ abstract class AbstractVoiceRecorder( override fun startRecord() { init() - outputFile = File(outputDirectory, "Voice message.$filenameExt") + outputFile = File(outputDirectory, "${UUID.randomUUID()}$filenameExt") val mr = mediaRecorder ?: return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -100,3 +103,12 @@ abstract class AbstractVoiceRecorder( return convertFile(outputFile) } } + +@Suppress("UNUSED") // preemptively added for https://github.com/vector-im/element-android/pull/4527 +private fun ContentAttachmentData.findVoiceFile(baseDirectory: File): File { + return File(baseDirectory, queryUri.takePathAfter(baseDirectory.name)) +} + +private fun Uri.takePathAfter(after: String): String { + return pathSegments.takeLastWhile { it != after }.joinToString(separator = "/") { it } +}