|
|
@ -13,20 +13,22 @@ import android.widget.RelativeLayout
|
|
|
|
import androidx.core.view.isVisible
|
|
|
|
import androidx.core.view.isVisible
|
|
|
|
import kotlinx.android.synthetic.main.view_voice_message.view.*
|
|
|
|
import kotlinx.android.synthetic.main.view_voice_message.view.*
|
|
|
|
import network.loki.messenger.R
|
|
|
|
import network.loki.messenger.R
|
|
|
|
|
|
|
|
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.audio.AudioSlidePlayer
|
|
|
|
import org.thoughtcrime.securesms.components.CornerMask
|
|
|
|
import org.thoughtcrime.securesms.components.CornerMask
|
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.MessageBubbleUtilities
|
|
|
|
import org.thoughtcrime.securesms.conversation.v2.utilities.MessageBubbleUtilities
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
|
|
|
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
|
|
|
|
import org.thoughtcrime.securesms.database.model.MmsMessageRecord
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.mms.AudioSlide
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import kotlin.math.roundToInt
|
|
|
|
import kotlin.math.roundToInt
|
|
|
|
|
|
|
|
import kotlin.math.roundToLong
|
|
|
|
|
|
|
|
|
|
|
|
class VoiceMessageView : LinearLayout {
|
|
|
|
class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
|
|
|
|
private val snHandler = Handler(Looper.getMainLooper())
|
|
|
|
|
|
|
|
private val cornerMask by lazy { CornerMask(this) }
|
|
|
|
private val cornerMask by lazy { CornerMask(this) }
|
|
|
|
private var runnable: Runnable? = null
|
|
|
|
private var isPlaying = false
|
|
|
|
private var mockIsPlaying = false
|
|
|
|
private var progress = 0.0
|
|
|
|
private var mockProgress = 0L
|
|
|
|
private var player: AudioSlidePlayer? = null
|
|
|
|
set(value) { field = value; handleProgressChanged() }
|
|
|
|
|
|
|
|
private var mockDuration = 12000L
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// region Lifecycle
|
|
|
|
// region Lifecycle
|
|
|
|
constructor(context: Context) : super(context) { initialize() }
|
|
|
|
constructor(context: Context) : super(context) { initialize() }
|
|
|
@ -36,14 +38,22 @@ class VoiceMessageView : LinearLayout {
|
|
|
|
private fun initialize() {
|
|
|
|
private fun initialize() {
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_voice_message, this)
|
|
|
|
LayoutInflater.from(context).inflate(R.layout.view_voice_message, this)
|
|
|
|
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
|
|
|
|
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(mockDuration),
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(0),
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(mockDuration))
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// endregion
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
|
|
// region Updating
|
|
|
|
// region Updating
|
|
|
|
fun bind(message: MmsMessageRecord, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) {
|
|
|
|
fun bind(message: MmsMessageRecord, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) {
|
|
|
|
val audio = message.slideDeck.audioSlide!!
|
|
|
|
val audio = message.slideDeck.audioSlide!!
|
|
|
|
|
|
|
|
val player = AudioSlidePlayer.createFor(context, audio, this)
|
|
|
|
|
|
|
|
this.player = player
|
|
|
|
|
|
|
|
player.play(0.0)
|
|
|
|
|
|
|
|
val duration = player.duration
|
|
|
|
|
|
|
|
player.stop()
|
|
|
|
|
|
|
|
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
|
|
|
|
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(duration),
|
|
|
|
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(duration))
|
|
|
|
voiceMessageViewLoader.isVisible = audio.isPendingDownload
|
|
|
|
voiceMessageViewLoader.isVisible = audio.isPendingDownload
|
|
|
|
val cornerRadii = MessageBubbleUtilities.calculateRadii(context, isStartOfMessageCluster, isEndOfMessageCluster, message.isOutgoing)
|
|
|
|
val cornerRadii = MessageBubbleUtilities.calculateRadii(context, isStartOfMessageCluster, isEndOfMessageCluster, message.isOutgoing)
|
|
|
|
cornerMask.setTopLeftRadius(cornerRadii[0])
|
|
|
|
cornerMask.setTopLeftRadius(cornerRadii[0])
|
|
|
@ -52,43 +62,37 @@ class VoiceMessageView : LinearLayout {
|
|
|
|
cornerMask.setBottomLeftRadius(cornerRadii[3])
|
|
|
|
cornerMask.setBottomLeftRadius(cornerRadii[3])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun handleProgressChanged() {
|
|
|
|
override fun onPlayerStart(player: AudioSlidePlayer) { }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onPlayerProgress(player: AudioSlidePlayer, progress: Double, duration: Long) {
|
|
|
|
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
|
|
|
|
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(mockDuration - mockProgress),
|
|
|
|
TimeUnit.MILLISECONDS.toMinutes(duration - (progress * duration.toDouble()).roundToLong()),
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(mockDuration - mockProgress))
|
|
|
|
TimeUnit.MILLISECONDS.toSeconds(duration - (progress * duration.toDouble()).roundToLong()))
|
|
|
|
val layoutParams = progressView.layoutParams as RelativeLayout.LayoutParams
|
|
|
|
val layoutParams = progressView.layoutParams as RelativeLayout.LayoutParams
|
|
|
|
val fraction = mockProgress.toFloat() / mockDuration.toFloat()
|
|
|
|
layoutParams.width = (width.toFloat() * progress.toFloat()).roundToInt()
|
|
|
|
layoutParams.width = (width.toFloat() * fraction).roundToInt()
|
|
|
|
|
|
|
|
progressView.layoutParams = layoutParams
|
|
|
|
progressView.layoutParams = layoutParams
|
|
|
|
|
|
|
|
this.progress = progress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onPlayerStop(player: AudioSlidePlayer) { }
|
|
|
|
|
|
|
|
|
|
|
|
override fun dispatchDraw(canvas: Canvas) {
|
|
|
|
override fun dispatchDraw(canvas: Canvas) {
|
|
|
|
super.dispatchDraw(canvas)
|
|
|
|
super.dispatchDraw(canvas)
|
|
|
|
cornerMask.mask(canvas)
|
|
|
|
cornerMask.mask(canvas)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun recycle() {
|
|
|
|
|
|
|
|
// TODO: Implement
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
// endregion
|
|
|
|
|
|
|
|
|
|
|
|
// region Interaction
|
|
|
|
// region Interaction
|
|
|
|
fun togglePlayback() {
|
|
|
|
fun togglePlayback() {
|
|
|
|
mockIsPlaying = !mockIsPlaying
|
|
|
|
val player = this.player ?: return
|
|
|
|
val iconID = if (mockIsPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
|
|
|
|
isPlaying = !isPlaying
|
|
|
|
|
|
|
|
val iconID = if (isPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
|
|
|
|
voiceMessagePlaybackImageView.setImageResource(iconID)
|
|
|
|
voiceMessagePlaybackImageView.setImageResource(iconID)
|
|
|
|
if (mockIsPlaying) {
|
|
|
|
if (isPlaying) {
|
|
|
|
updateProgress()
|
|
|
|
player.play(player.progress)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
runnable?.let { snHandler.removeCallbacks(it) }
|
|
|
|
player.stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun updateProgress() {
|
|
|
|
|
|
|
|
mockProgress += 20L
|
|
|
|
|
|
|
|
val runnable = Runnable { updateProgress() }
|
|
|
|
|
|
|
|
this.runnable = runnable
|
|
|
|
|
|
|
|
snHandler.postDelayed(runnable, 20L)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// endregion
|
|
|
|
// endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|