From c17eb5e404bf41b24cd6293a65beb390bafee819 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 28 Jun 2021 14:51:49 +1000 Subject: [PATCH] WIP --- .../securesms/audio/AudioSlidePlayer.java | 15 ++++- .../conversation/v2/ConversationActivityV2.kt | 1 - .../conversation/v2/dialogs/BlockedDialog.kt | 1 - .../v2/messages/ControlMessageView.kt | 2 +- .../conversation/v2/messages/DocumentView.kt | 4 -- .../v2/messages/LinkPreviewView.kt | 4 -- .../v2/messages/VisibleMessageContentView.kt | 6 -- .../v2/messages/VisibleMessageView.kt | 6 -- .../v2/messages/VoiceMessageView.kt | 66 ++++++++++--------- .../securesms/mms/AudioSlide.java | 1 - 10 files changed, 48 insertions(+), 58 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java index a7622306ba..6158fe7f32 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java +++ b/app/src/main/java/org/thoughtcrime/securesms/audio/AudioSlidePlayer.java @@ -105,7 +105,7 @@ public class AudioSlidePlayer implements SensorEventListener { private void play(final double progress, boolean earpiece) throws IOException { if (this.mediaPlayer != null) return; - LoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE).createDefaultLoadControl(); + LoadControl loadControl = new DefaultLoadControl.Builder().setBufferDurationsMs(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE).createDefaultLoadControl(); this.mediaPlayer = ExoPlayerFactory.newSimpleInstance(context, new DefaultRenderersFactory(context), new DefaultTrackSelector(), loadControl); this.audioAttachmentServer = new AttachmentServer(context, slide.asAttachment()); this.startTime = System.currentTimeMillis(); @@ -267,8 +267,17 @@ public class AudioSlidePlayer implements SensorEventListener { return slide; } + public Long getDuration() { + if (mediaPlayer == null) { return 0L; } + return mediaPlayer.getDuration(); + } - private Pair getProgress() { + public Double getProgress() { + if (mediaPlayer == null) { return 0.0; } + return (double) mediaPlayer.getCurrentPosition() / (double) mediaPlayer.getDuration(); + } + + private Pair getProgressTuple() { if (mediaPlayer == null || mediaPlayer.getCurrentPosition() <= 0 || mediaPlayer.getDuration() <= 0) { return new Pair<>(0D, 0); } else { @@ -383,7 +392,7 @@ public class AudioSlidePlayer implements SensorEventListener { return; } - Pair progress = player.getProgress(); + Pair progress = player.getProgressTuple(); player.notifyOnProgress(progress.first, progress.second); sendEmptyMessageDelayed(0, 50); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 3a5f36d555..fc44d99324 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -72,7 +72,6 @@ import org.thoughtcrime.securesms.mediasend.Media import org.thoughtcrime.securesms.mediasend.MediaSendActivity import org.thoughtcrime.securesms.mms.* import org.thoughtcrime.securesms.notifications.MarkReadReceiver -import org.thoughtcrime.securesms.providers.BlobProvider import org.thoughtcrime.securesms.util.DateUtils import org.thoughtcrime.securesms.util.MediaUtil import java.util.* diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt index 3748add489..3013ab8901 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/dialogs/BlockedDialog.kt @@ -13,7 +13,6 @@ import org.session.libsession.messaging.contacts.Contact import org.session.libsession.utilities.recipients.Recipient import org.thoughtcrime.securesms.conversation.v2.utilities.BaseDialog import org.thoughtcrime.securesms.database.DatabaseFactory -import org.thoughtcrime.securesms.database.RecipientDatabase /** Shown upon sending a message to a user that's blocked. */ class BlockedDialog(private val recipient: Recipient) : BaseDialog() { diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt index ab6c11dcb5..b4f3810e16 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/ControlMessageView.kt @@ -37,7 +37,7 @@ class ControlMessageView : LinearLayout { } fun recycle() { - // TODO: Implement + } // endregion } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/DocumentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/DocumentView.kt index 4a9c189962..c9daca6c7a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/DocumentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/DocumentView.kt @@ -30,9 +30,5 @@ class DocumentView : LinearLayout { documentTitleTextView.setTextColor(textColor) documentViewIconImageView.imageTintList = ColorStateList.valueOf(textColor) } - - fun recycle() { - // TODO: Implement - } // endregion } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt index 00a98d1d33..aae48a622e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/LinkPreviewView.kt @@ -64,9 +64,5 @@ class LinkPreviewView : LinearLayout { super.dispatchDraw(canvas) cornerMask.mask(canvas) } - - fun recycle() { - // TODO: Implement - } // endregion } \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt index e242d6e6b5..9427f9e38b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageContentView.kt @@ -1,12 +1,9 @@ package org.thoughtcrime.securesms.conversation.v2.messages import android.content.Context -import android.content.res.ColorStateList -import android.graphics.Color import android.graphics.drawable.Drawable import android.text.util.Linkify import android.util.AttributeSet -import android.util.Log import android.util.TypedValue import android.view.LayoutInflater import android.widget.LinearLayout @@ -19,8 +16,6 @@ import androidx.core.graphics.BlendModeCompat import androidx.core.text.toSpannable import kotlinx.android.synthetic.main.view_visible_message_content.view.* import network.loki.messenger.R -import org.session.libsession.messaging.utilities.UpdateMessageData -import org.session.libsession.messaging.utilities.UpdateMessageData.Companion.fromJSON import org.session.libsession.utilities.ThemeUtil import org.session.libsession.utilities.ViewUtil import org.session.libsession.utilities.recipients.Recipient @@ -28,7 +23,6 @@ import org.thoughtcrime.securesms.components.emoji.EmojiTextView import org.thoughtcrime.securesms.database.model.MessageRecord import org.thoughtcrime.securesms.database.model.MmsMessageRecord import org.thoughtcrime.securesms.loki.utilities.* -import org.thoughtcrime.securesms.loki.utilities.MentionUtilities.highlightMentions import org.thoughtcrime.securesms.mms.GlideRequests import kotlin.math.roundToInt diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt index 3d665c5cbd..e3d7482b64 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VisibleMessageView.kt @@ -4,19 +4,14 @@ import android.content.Context import android.content.res.Resources import android.graphics.Canvas import android.graphics.Rect -import android.graphics.Region import android.graphics.drawable.ColorDrawable import android.os.Build import android.os.Handler import android.os.Looper import android.util.AttributeSet -import android.util.Log import android.view.* import android.widget.LinearLayout -import androidx.annotation.ColorRes -import androidx.annotation.DrawableRes import androidx.core.content.ContextCompat -import androidx.core.graphics.withClip import androidx.core.view.isVisible import kotlinx.android.synthetic.main.view_conversation.view.* import kotlinx.android.synthetic.main.view_visible_message.view.* @@ -27,7 +22,6 @@ import org.session.libsession.messaging.open_groups.OpenGroupAPIV2 import org.session.libsession.utilities.ViewUtil import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.model.MessageRecord -import org.thoughtcrime.securesms.loki.utilities.disableClipping import org.thoughtcrime.securesms.loki.utilities.getColorWithID import org.thoughtcrime.securesms.loki.utilities.toDp import org.thoughtcrime.securesms.loki.utilities.toPx diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt index 111f251805..9d2620ecd0 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/messages/VoiceMessageView.kt @@ -13,20 +13,22 @@ import android.widget.RelativeLayout import androidx.core.view.isVisible import kotlinx.android.synthetic.main.view_voice_message.view.* 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.conversation.v2.utilities.MessageBubbleUtilities +import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.model.MmsMessageRecord +import org.thoughtcrime.securesms.mms.AudioSlide import java.util.concurrent.TimeUnit import kotlin.math.roundToInt +import kotlin.math.roundToLong -class VoiceMessageView : LinearLayout { - private val snHandler = Handler(Looper.getMainLooper()) +class VoiceMessageView : LinearLayout, AudioSlidePlayer.Listener { private val cornerMask by lazy { CornerMask(this) } - private var runnable: Runnable? = null - private var mockIsPlaying = false - private var mockProgress = 0L - set(value) { field = value; handleProgressChanged() } - private var mockDuration = 12000L + private var isPlaying = false + private var progress = 0.0 + private var player: AudioSlidePlayer? = null // region Lifecycle constructor(context: Context) : super(context) { initialize() } @@ -36,14 +38,22 @@ class VoiceMessageView : LinearLayout { private fun initialize() { LayoutInflater.from(context).inflate(R.layout.view_voice_message, this) voiceMessageViewDurationTextView.text = String.format("%01d:%02d", - TimeUnit.MILLISECONDS.toMinutes(mockDuration), - TimeUnit.MILLISECONDS.toSeconds(mockDuration)) + TimeUnit.MILLISECONDS.toMinutes(0), + TimeUnit.MILLISECONDS.toSeconds(0)) } // endregion // region Updating fun bind(message: MmsMessageRecord, isStartOfMessageCluster: Boolean, isEndOfMessageCluster: Boolean) { 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 val cornerRadii = MessageBubbleUtilities.calculateRadii(context, isStartOfMessageCluster, isEndOfMessageCluster, message.isOutgoing) cornerMask.setTopLeftRadius(cornerRadii[0]) @@ -52,43 +62,37 @@ class VoiceMessageView : LinearLayout { 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", - TimeUnit.MILLISECONDS.toMinutes(mockDuration - mockProgress), - TimeUnit.MILLISECONDS.toSeconds(mockDuration - mockProgress)) + TimeUnit.MILLISECONDS.toMinutes(duration - (progress * duration.toDouble()).roundToLong()), + TimeUnit.MILLISECONDS.toSeconds(duration - (progress * duration.toDouble()).roundToLong())) val layoutParams = progressView.layoutParams as RelativeLayout.LayoutParams - val fraction = mockProgress.toFloat() / mockDuration.toFloat() - layoutParams.width = (width.toFloat() * fraction).roundToInt() + layoutParams.width = (width.toFloat() * progress.toFloat()).roundToInt() progressView.layoutParams = layoutParams + this.progress = progress } + override fun onPlayerStop(player: AudioSlidePlayer) { } + override fun dispatchDraw(canvas: Canvas) { super.dispatchDraw(canvas) cornerMask.mask(canvas) } - - fun recycle() { - // TODO: Implement - } // endregion // region Interaction fun togglePlayback() { - mockIsPlaying = !mockIsPlaying - val iconID = if (mockIsPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play + val player = this.player ?: return + isPlaying = !isPlaying + val iconID = if (isPlaying) R.drawable.exo_icon_pause else R.drawable.exo_icon_play voiceMessagePlaybackImageView.setImageResource(iconID) - if (mockIsPlaying) { - updateProgress() + if (isPlaying) { + player.play(player.progress) } 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 -} \ No newline at end of file +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/mms/AudioSlide.java b/app/src/main/java/org/thoughtcrime/securesms/mms/AudioSlide.java index 9a1885ab90..3d45e6a6e7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/mms/AudioSlide.java +++ b/app/src/main/java/org/thoughtcrime/securesms/mms/AudioSlide.java @@ -31,7 +31,6 @@ import org.session.libsession.utilities.MediaTypes; import org.thoughtcrime.securesms.database.AttachmentDatabase; import org.thoughtcrime.securesms.util.ResUtil; - public class AudioSlide extends Slide { public AudioSlide(Context context, Uri uri, long dataSize, boolean voiceNote) {