From bdf9402685d419091159ba707aabc8010357c897 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 23 Nov 2021 17:45:10 +0000 Subject: [PATCH 1/4] only deleting voice files if they were successfully uploading - fixes flaky networks losing the voice file and being unable to retry --- .../internal/session/content/UploadContentWorker.kt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt index b657d950bda..86e0630fcf5 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt @@ -279,6 +279,11 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter Timber.e(failure, "## Failed to update file cache") } + // Delete the temporary voice message file + if (params.attachment.type == ContentAttachmentData.Type.AUDIO && params.attachment.mimeType == MimeTypes.Ogg) { + context.contentResolver.delete(params.attachment.queryUri, null, null) + } + val uploadThumbnailResult = dealWithThumbnail(params) handleSuccess(params, @@ -299,11 +304,6 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter filesToDelete.forEach { tryOrNull { it.delete() } } - - // Delete the temporary voice message file - if (params.attachment.type == ContentAttachmentData.Type.AUDIO && params.attachment.mimeType == MimeTypes.Ogg) { - context.contentResolver.delete(params.attachment.queryUri, null, null) - } } } From 251c54ecfe40d5193366a0af1aa67e9307b723c6 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 23 Nov 2021 17:49:44 +0000 Subject: [PATCH 2/4] 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 adcd6a3008e..5d351e843fc 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 1a91c00e11d..5d8bce1ea3b 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 } +} From 56cfee572fe287713b4af9a6251a2ab71416c37f Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 23 Nov 2021 17:52:21 +0000 Subject: [PATCH 3/4] fixing message edits thinking there's an active voice recording --- .../home/room/detail/composer/MessageComposerViewState.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewState.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewState.kt index def1c8d2fb1..75bd94586c8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewState.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewState.kt @@ -58,10 +58,7 @@ data class MessageComposerViewState( VoiceMessageRecorderView.RecordingUiState.Started -> true } - val isVoiceMessageIdle = when (voiceRecordingUiState) { - VoiceMessageRecorderView.RecordingUiState.None, VoiceMessageRecorderView.RecordingUiState.Cancelled -> false - else -> true - } + val isVoiceMessageIdle = !isVoiceRecording val isComposerVisible = canSendMessage && !isVoiceRecording val isVoiceMessageRecorderVisible = canSendMessage && !isSendButtonVisible From 72bc442fcaeaa3f1ad289114eaa801d08d368888 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Tue, 23 Nov 2021 18:12:49 +0000 Subject: [PATCH 4/4] adding changelog entry --- changelog.d/3833.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/3833.bugfix diff --git a/changelog.d/3833.bugfix b/changelog.d/3833.bugfix new file mode 100644 index 00000000000..7d25fb2aad8 --- /dev/null +++ b/changelog.d/3833.bugfix @@ -0,0 +1 @@ +Fixing queued voice message failing to send or retry \ No newline at end of file