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

94 lines
3.7 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-21 05:36:45 +02:00
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
2021-06-01 06:56:58 +02:00
import android.util.AttributeSet
import android.view.LayoutInflater
2021-06-21 05:36:45 +02:00
import android.view.ViewOutlineProvider
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
2021-06-25 06:06:53 +02:00
import org.thoughtcrime.securesms.components.CornerMask
import org.thoughtcrime.securesms.conversation.v2.utilities.MessageBubbleUtilities
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-01 06:56:58 +02:00
class VoiceMessageView : LinearLayout {
2021-06-21 05:36:45 +02:00
private val snHandler = Handler(Looper.getMainLooper())
2021-06-25 06:06:53 +02:00
private val cornerMask by lazy { CornerMask(this) }
2021-06-21 05:36:45 +02:00
private var runnable: Runnable? = null
private var mockIsPlaying = false
private var mockProgress = 0L
set(value) { field = value; handleProgressChanged() }
private var mockDuration = 12000L
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",
TimeUnit.MILLISECONDS.toMinutes(mockDuration),
TimeUnit.MILLISECONDS.toSeconds(mockDuration))
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!!
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 05:36:45 +02:00
}
2021-06-21 03:09:30 +02:00
2021-06-21 05:36:45 +02:00
private fun handleProgressChanged() {
voiceMessageViewDurationTextView.text = String.format("%01d:%02d",
TimeUnit.MILLISECONDS.toMinutes(mockDuration - mockProgress),
TimeUnit.MILLISECONDS.toSeconds(mockDuration - mockProgress))
val layoutParams = progressView.layoutParams as RelativeLayout.LayoutParams
val fraction = mockProgress.toFloat() / mockDuration.toFloat()
layoutParams.width = (width.toFloat() * fraction).roundToInt()
progressView.layoutParams = layoutParams
2021-06-01 06:56:58 +02:00
}
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
fun recycle() {
// TODO: Implement
}
// endregion
2021-06-21 05:36:45 +02:00
// region Interaction
fun togglePlayback() {
2021-06-21 05:36:45 +02:00
mockIsPlaying = !mockIsPlaying
val iconID = if (mockIsPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
voiceMessagePlaybackImageView.setImageResource(iconID)
if (mockIsPlaying) {
updateProgress()
} else {
runnable?.let { snHandler.removeCallbacks(it) }
}
}
private fun updateProgress() {
mockProgress += 20L
val runnable = Runnable { updateProgress() }
this.runnable = runnable
snHandler.postDelayed(runnable, 20L)
}
// endregion
2021-06-01 06:56:58 +02:00
}