From 1df867f345aac2cad700e594324fae9c7984e647 Mon Sep 17 00:00:00 2001 From: Onuray Sahin Date: Wed, 8 Sep 2021 13:20:56 +0300 Subject: [PATCH] Fix rendering voice message if the waveform data is corrupted. --- changelog.d/3983.bugfix | 1 + .../sdk/api/session/content/ContentAttachmentData.kt | 2 +- .../session/room/model/message/AudioWaveformInfo.kt | 2 +- .../internal/session/room/send/WaveFormSanitizer.kt | 8 ++++---- .../detail/timeline/factory/MessageItemFactory.kt | 12 +++++++----- 5 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 changelog.d/3983.bugfix diff --git a/changelog.d/3983.bugfix b/changelog.d/3983.bugfix new file mode 100644 index 0000000000..4b3fdc454d --- /dev/null +++ b/changelog.d/3983.bugfix @@ -0,0 +1 @@ +Voice Message - Cannot render voice message if the waveform data is corrupted \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt index 7ee26de8db..38c3dd860c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/content/ContentAttachmentData.kt @@ -36,7 +36,7 @@ data class ContentAttachmentData( val queryUri: Uri, val mimeType: String?, val type: Type, - val waveform: List? = null + val waveform: List? = null ) : Parcelable { @JsonClass(generateAdapter = false) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/AudioWaveformInfo.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/AudioWaveformInfo.kt index d576f1057a..aa05eb7189 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/AudioWaveformInfo.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/AudioWaveformInfo.kt @@ -32,5 +32,5 @@ data class AudioWaveformInfo( * List of integers between zero and 1024, inclusive. */ @Json(name = "waveform") - val waveform: List? = null + val waveform: List? = null ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/WaveFormSanitizer.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/WaveFormSanitizer.kt index 78a03f3775..b0474dd6ff 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/WaveFormSanitizer.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/WaveFormSanitizer.kt @@ -33,7 +33,7 @@ internal class WaveFormSanitizer @Inject constructor() { * The array should have no less than 30 elements and no more than 120. * List of integers between zero and 1024, inclusive. */ - fun sanitize(waveForm: List?): List? { + fun sanitize(waveForm: List?): List? { if (waveForm.isNullOrEmpty()) { return null } @@ -46,7 +46,7 @@ internal class WaveFormSanitizer @Inject constructor() { val repeatTimes = ceil(MIN_NUMBER_OF_VALUES / waveForm.size.toDouble()).toInt() waveForm.map { value -> repeat(repeatTimes) { - sizeInRangeList.add(value) + sizeInRangeList.add(value ?: 0) } } } @@ -54,12 +54,12 @@ internal class WaveFormSanitizer @Inject constructor() { val keepOneOf = ceil(waveForm.size.toDouble() / MAX_NUMBER_OF_VALUES).toInt() waveForm.mapIndexed { idx, value -> if (idx % keepOneOf == 0) { - sizeInRangeList.add(value) + sizeInRangeList.add(value ?: 0) } } } else -> { - sizeInRangeList.addAll(waveForm) + sizeInRangeList.addAll(waveForm.filterNotNull()) } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt index e67fa7cca0..287cd014e9 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt @@ -622,11 +622,13 @@ class MessageItemFactory @Inject constructor( .highlighted(highlight) } - private fun List?.toFft(): List? { - return this?.map { - // Value comes from AudioRecordView.maxReportableAmp, and 1024 is the max value in the Matrix spec - it * 22760 / 1024 - } + private fun List?.toFft(): List? { + return this + ?.filterNotNull() + ?.map { + // Value comes from AudioRecordView.maxReportableAmp, and 1024 is the max value in the Matrix spec + it * 22760 / 1024 + } } companion object {