fix: avoid crash in voice message view for pending downloads, display the icon on player stop when player starting from another view. Use thread id in setAttachmentAudioExtras

This commit is contained in:
Harris 2021-07-07 15:18:12 +10:00
parent 683b5243bd
commit ce490f5f90
2 changed files with 24 additions and 8 deletions

View File

@ -107,7 +107,7 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper)
attachmentId = attachmentId, attachmentId = attachmentId,
visualSamples = byteArrayOf(), visualSamples = byteArrayOf(),
durationMs = durationMs durationMs = durationMs
)) ), threadId)
} }
override fun isOutgoingMessage(timestamp: Long): Boolean { override fun isOutgoingMessage(timestamp: Long): Boolean {

View File

@ -4,7 +4,8 @@ import android.content.Context
import android.graphics.Canvas import android.graphics.Canvas
import android.util.AttributeSet import android.util.AttributeSet
import android.util.Log import android.util.Log
import android.view.* import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.RelativeLayout import android.widget.RelativeLayout
import androidx.core.view.isVisible import androidx.core.view.isVisible
@ -23,6 +24,10 @@ import kotlin.math.roundToLong
class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener { class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
private val cornerMask by lazy { CornerMask(this) } private val cornerMask by lazy { CornerMask(this) }
private var isPlaying = false private var isPlaying = false
set(value) {
field = value
renderIcon()
}
private var progress = 0.0 private var progress = 0.0
private var duration = 0L private var duration = 0L
private var player: AudioSlidePlayer? = null private var player: AudioSlidePlayer? = null
@ -44,8 +49,6 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
// 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
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])
@ -54,7 +57,13 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
cornerMask.setBottomLeftRadius(cornerRadii[3]) cornerMask.setBottomLeftRadius(cornerRadii[3])
// only process audio if downloaded // only process audio if downloaded
if (audio.isPendingDownload || audio.isInProgress) return if (audio.isPendingDownload || audio.isInProgress) {
this.player = null
return
}
val player = AudioSlidePlayer.createFor(context, audio, this)
this.player = player
(audio.asAttachment() as? DatabaseAttachment)?.let { attachment -> (audio.asAttachment() as? DatabaseAttachment)?.let { attachment ->
DatabaseFactory.getAttachmentDatabase(context).getAttachmentAudioExtras(attachment.attachmentId)?.let { audioExtras -> DatabaseFactory.getAttachmentDatabase(context).getAttachmentAudioExtras(attachment.attachmentId)?.let { audioExtras ->
@ -90,20 +99,27 @@ class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener {
progressView.layoutParams = layoutParams progressView.layoutParams = layoutParams
} }
override fun onPlayerStop(player: AudioSlidePlayer) { } override fun onPlayerStop(player: AudioSlidePlayer) {
Log.d("Loki", "Player stopped")
isPlaying = false
}
override fun dispatchDraw(canvas: Canvas) { override fun dispatchDraw(canvas: Canvas) {
super.dispatchDraw(canvas) super.dispatchDraw(canvas)
cornerMask.mask(canvas) cornerMask.mask(canvas)
} }
private fun renderIcon() {
val iconID = if (isPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
voiceMessagePlaybackImageView.setImageResource(iconID)
}
// endregion // endregion
// region Interaction // region Interaction
fun togglePlayback() { fun togglePlayback() {
val player = this.player ?: return val player = this.player ?: return
isPlaying = !isPlaying isPlaying = !isPlaying
val iconID = if (isPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play
voiceMessagePlaybackImageView.setImageResource(iconID)
if (isPlaying) { if (isPlaying) {
player.play(progress) player.play(progress)
} else { } else {