session-android/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt

119 lines
4.9 KiB
Kotlin
Raw Normal View History

2021-06-01 06:56:58 +02:00
package org.thoughtcrime.securesms.conversation.v2.messages
import android.content.Context
2021-06-25 06:06:53 +02:00
import android.graphics.Canvas
2021-06-01 06:56:58 +02:00
import android.util.AttributeSet
2021-06-28 07:41:23 +02:00
import android.util.Log
import android.view.*
2021-06-01 06:56:58 +02:00
import android.widget.LinearLayout
2021-06-21 05:36:45 +02:00
import android.widget.RelativeLayout
import androidx.core.view.isVisible
2021-06-01 06:56:58 +02:00
import kotlinx.android.synthetic.main.view_voice_message.view.*
import network.loki.messenger.R
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
2021-06-28 06:51:49 +02:00
import org.thoughtcrime.securesms.audio.AudioSlidePlayer
2021-06-25 06:06:53 +02:00
import org.thoughtcrime.securesms.components.CornerMask
import org.thoughtcrime.securesms.conversation.v2.utilities.MessageBubbleUtilities
import org.thoughtcrime.securesms.database.DatabaseFactory
2021-06-21 05:36:45 +02:00
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt
2021-06-28 06:51:49 +02:00
import kotlin.math.roundToLong
2021-06-01 06:56:58 +02:00
2021-06-28 06:51:49 +02:00
class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
2021-06-25 06:06:53 +02:00
private val cornerMask by lazy { CornerMask(this) }
2021-06-28 06:51:49 +02:00
private var isPlaying = false
private var progress = 0.0
2021-06-28 07:06:51 +02:00
private var duration = 0L
2021-06-28 06:51:49 +02:00
private var player: AudioSlidePlayer? = null
2021-06-28 07:06:51 +02:00
private var isPreparing = false
2021-06-01 06:56:58 +02:00
// region Lifecycle
2021-06-21 03:09:30 +02:00
constructor(context: Context) : super(context) { initialize() }
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) { initialize() }
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { initialize() }
2021-06-01 06:56:58 +02:00
2021-06-21 03:09:30 +02:00
private fun initialize() {
2021-06-02 02:12:49 +02:00
LayoutInflater.from(context).inflate(R.layout.view_voice_message, this)
2021-06-21 05:36:45 +02:00
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
2021-06-28 06:51:49 +02:00
TimeUnit.MILLISECONDS.toMinutes(0),
TimeUnit.MILLISECONDS.toSeconds(0))
2021-06-01 06:56:58 +02:00
}
// endregion
// region Updating
2021-06-25 06:06:53 +02:00
fun bind(message: MmsMessageRecord, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) {
2021-06-21 05:36:45 +02:00
val audio = message.slideDeck.audioSlide!!
2021-06-28 06:51:49 +02:00
val player = AudioSlidePlayer.createFor(context, audio, this)
this.player = player
2021-06-21 05:36:45 +02:00
voiceMessageViewLoader.isVisible = audio.isPendingDownload
2021-06-25 06:06:53 +02:00
val cornerRadii = MessageBubbleUtilities.calculateRadii(context, isStartOfMessageCluster, isEndOfMessageCluster, message.isOutgoing)
cornerMask.setTopLeftRadius(cornerRadii[0])
cornerMask.setTopRightRadius(cornerRadii[1])
cornerMask.setBottomRightRadius(cornerRadii[2])
cornerMask.setBottomLeftRadius(cornerRadii[3])
2021-06-21 03:09:30 +02:00
// only process audio if downloaded
if (audio.isPendingDownload || audio.isInProgress) return
(audio.asAttachment() as? DatabaseAttachment)?.let { attachment ->
DatabaseFactory.getAttachmentDatabase(context).getAttachmentAudioExtras(attachment.attachmentId)?.let { audioExtras ->
if (audioExtras.durationMs > 0) {
voiceMessageViewDurationTextView.visibility = View.VISIBLE
voiceMessageViewDurationTextView.text = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(audioExtras.durationMs),
TimeUnit.MILLISECONDS.toSeconds(audioExtras.durationMs))
}
}
}
2021-06-28 07:06:51 +02:00
}
2021-06-28 06:51:49 +02:00
override fun onPlayerStart(player: AudioSlidePlayer) {}
2021-06-28 07:06:51 +02:00
override fun onPlayerProgress(player: AudioSlidePlayer, progress: Double, unused: Long) {
if (progress == 1.0) {
togglePlayback()
handleProgressChanged(0.0)
} else {
handleProgressChanged(progress)
}
}
private fun handleProgressChanged(progress: Double) {
this.progress = progress
2021-06-21 05:36:45 +02:00
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
2021-06-28 06:51:49 +02:00
TimeUnit.MILLISECONDS.toMinutes(duration - (progress * duration.toDouble()).roundToLong()),
TimeUnit.MILLISECONDS.toSeconds(duration - (progress * duration.toDouble()).roundToLong()))
2021-06-21 05:36:45 +02:00
val layoutParams = progressView.layoutParams as RelativeLayout.LayoutParams
2021-06-28 06:51:49 +02:00
layoutParams.width = (width.toFloat() * progress.toFloat()).roundToInt()
2021-06-21 05:36:45 +02:00
progressView.layoutParams = layoutParams
2021-06-01 06:56:58 +02:00
}
2021-06-28 06:51:49 +02:00
override fun onPlayerStop(player: AudioSlidePlayer) { }
2021-06-25 06:06:53 +02:00
override fun dispatchDraw(canvas: Canvas) {
super.dispatchDraw(canvas)
cornerMask.mask(canvas)
}
2021-06-01 06:56:58 +02:00
// endregion
2021-06-21 05:36:45 +02:00
// region Interaction
fun togglePlayback() {
2021-06-28 06:51:49 +02:00
val player = this.player ?: return
isPlaying = !isPlaying
val iconID = if (isPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
2021-06-21 05:36:45 +02:00
voiceMessagePlaybackImageView.setImageResource(iconID)
2021-06-28 06:51:49 +02:00
if (isPlaying) {
2021-06-28 07:06:51 +02:00
player.play(progress)
2021-06-21 05:36:45 +02:00
} else {
2021-06-28 06:51:49 +02:00
player.stop()
2021-06-21 05:36:45 +02:00
}
}
2021-06-28 07:41:23 +02:00
fun handleDoubleTap() {
2021-06-28 07:49:29 +02:00
val player = this.player ?: return
player.playbackSpeed = if (player.playbackSpeed == 1.0f) 1.5f else 1.0f
2021-06-28 07:41:23 +02:00
}
2021-06-21 05:36:45 +02:00
// endregion
2021-06-28 06:51:49 +02:00
}