diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastConstants.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastConstants.kt index d445dfdfbe..7b937f740e 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastConstants.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastConstants.kt @@ -16,11 +16,16 @@ package im.vector.app.features.voicebroadcast +import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent + object VoiceBroadcastConstants { /** Voice Broadcast State Event. */ const val STATE_ROOM_VOICE_BROADCAST_INFO = "io.element.voice_broadcast_info" + /** Custom key passed to the [MessageAudioContent] with Voice Broadcast information. */ + const val VOICE_BROADCAST_CHUNK_KEY = "io.element.voice_broadcast_chunk" + /** Default voice broadcast chunk duration, in seconds. */ - const val DEFAULT_CHUNK_LENGTH_IN_SECONDS = 120 + const val DEFAULT_CHUNK_LENGTH_IN_SECONDS = 10 } diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastExtensions.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastExtensions.kt index d016703968..fe449c679e 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastExtensions.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastExtensions.kt @@ -16,14 +16,18 @@ package im.vector.app.features.voicebroadcast -import org.matrix.android.sdk.api.session.events.model.RelationType +import im.vector.app.features.voicebroadcast.model.VoiceBroadcastChunk +import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.getRelationContent +import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.room.model.message.MessageAudioEvent -fun MessageAudioEvent?.isVoiceBroadcast() = this?.getVoiceBroadcastEventId() != null +fun MessageAudioEvent?.isVoiceBroadcast() = this?.root?.getClearContent()?.get(VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY) != null +fun MessageAudioEvent.getVoiceBroadcastEventId(): String? = if (isVoiceBroadcast()) root.getRelationContent()?.eventId else null -fun MessageAudioEvent.getVoiceBroadcastEventId(): String? = - // TODO Improve this condition by checking the referenced event type - root.takeIf { content.voiceMessageIndicator != null } - ?.getRelationContent()?.takeIf { it.type == RelationType.REFERENCE } - ?.eventId +fun MessageAudioEvent.getVoiceBroadcastChunk(): VoiceBroadcastChunk? { + @Suppress("UNCHECKED_CAST") + return (root.getClearContent()?.get(VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY) as? Content).toModel() +} + +val MessageAudioEvent.sequence: Int? get() = getVoiceBroadcastChunk()?.sequence diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastPlayer.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastPlayer.kt index 41ed1ff84d..dfd50ea5cb 100644 --- a/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastPlayer.kt +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/VoiceBroadcastPlayer.kt @@ -85,7 +85,7 @@ class VoiceBroadcastPlayer @Inject constructor( private fun updatePlaylist(room: Room, eventId: String) { val timelineEvents = room.timelineService().getTimelineEventsRelatedTo(RelationType.REFERENCE, eventId) val audioEvents = timelineEvents.mapNotNull { it.root.asMessageAudioEvent() } - playlist = audioEvents.sortedBy { it.root.originServerTs } + playlist = audioEvents.sortedBy { it.getVoiceBroadcastChunk()?.sequence?.toLong() ?: it.root.originServerTs } } private fun startPlayback() { diff --git a/vector/src/main/java/im/vector/app/features/voicebroadcast/model/VoiceBroadcastChunk.kt b/vector/src/main/java/im/vector/app/features/voicebroadcast/model/VoiceBroadcastChunk.kt new file mode 100644 index 0000000000..e0f6e6e7b1 --- /dev/null +++ b/vector/src/main/java/im/vector/app/features/voicebroadcast/model/VoiceBroadcastChunk.kt @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.voicebroadcast.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class VoiceBroadcastChunk( + @Json(name = "sequence") val sequence: Int? = null +) 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 780150d5e7..ba1799c520 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 @@ -23,6 +23,7 @@ import im.vector.app.features.attachments.toContentAttachmentData import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent +import im.vector.app.features.voicebroadcast.model.VoiceBroadcastChunk import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent import im.vector.lib.multipicker.utils.toMultiPickerAudioType @@ -98,7 +99,10 @@ class StartVoiceBroadcastUseCase @Inject constructor( attachment = audioType.toContentAttachmentData(isVoiceMessage = true), compressBeforeSending = false, roomIds = emptySet(), - relatesTo = RelationDefaultContent(RelationType.REFERENCE, referenceEventId) + relatesTo = RelationDefaultContent(RelationType.REFERENCE, referenceEventId), + additionalContent = mapOf( + VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY to VoiceBroadcastChunk(sequence = sequence).toContent() + ) ) } }