diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentEvents.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentEvents.kt index 997790a938..5b1f2ab90d 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentEvents.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentEvents.kt @@ -21,6 +21,10 @@ sealed class AttachmentEvents { } interface AttachmentEventListener { - fun onEvent(event: AttachmentEvents) } + +sealed class AttachmentCommands { + object PauseVideo : AttachmentCommands() + object StartVideo : AttachmentCommands() +} diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt index 2a83ab21c7..6f2436f261 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentViewerActivity.kt @@ -152,6 +152,7 @@ abstract class AttachmentViewerActivity : AppCompatActivity(), AttachmentEventLi super.onResume() attachmentsAdapter.onResume(currentPosition) } + override fun dispatchTouchEvent(ev: MotionEvent): Boolean { // The zoomable view is configured to disallow interception when image is zoomed @@ -302,6 +303,12 @@ abstract class AttachmentViewerActivity : AppCompatActivity(), AttachmentEventLi finish() } + public fun handle(commands: AttachmentCommands) { + (attachmentsAdapter.recyclerView?.findViewHolderForAdapterPosition(currentPosition) as? BaseViewHolder)?.let { + it.handleCommand(commands) + } + } + private fun hideSystemUI() { systemUiVisibility = false // Enables regular immersive mode. diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt index d1929f271e..b0cb5193e8 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/AttachmentsAdapter.kt @@ -31,6 +31,8 @@ abstract class BaseViewHolder constructor(itemView: View) : open fun entersBackground() {} open fun entersForeground() {} open fun onSelected(selected: Boolean) {} + + open fun handleCommand(commands: AttachmentCommands) {} } class AttachmentViewHolder constructor(itemView: View) : @@ -123,7 +125,6 @@ class AttachmentsAdapter() : RecyclerView.Adapter() { return false } - fun onPause(position: Int) { val holder = recyclerView?.findViewHolderForAdapterPosition(position) as? BaseViewHolder holder?.entersBackground() @@ -132,7 +133,6 @@ class AttachmentsAdapter() : RecyclerView.Adapter() { fun onResume(position: Int) { val holder = recyclerView?.findViewHolderForAdapterPosition(position) as? BaseViewHolder holder?.entersForeground() - } // override fun getItemCount(): Int { // return 8 diff --git a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt index ea5fed1acb..a2424dda57 100644 --- a/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt +++ b/attachment-viewer/src/main/java/im/vector/riotx/attachmentviewer/VideoViewHolder.kt @@ -26,7 +26,6 @@ import androidx.core.view.isVisible import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable -import kotlinx.coroutines.selects.select import java.io.File import java.lang.ref.WeakReference import java.util.concurrent.TimeUnit @@ -41,6 +40,7 @@ class VideoViewHolder constructor(itemView: View) : private var mVideoPath: String? = null private var progressDisposable: Disposable? = null private var progress: Int = 0 + private var wasPaused = false var eventListener: WeakReference? = null @@ -99,7 +99,6 @@ class VideoViewHolder constructor(itemView: View) : videoView.stopPlayback() videoView.pause() } - } override fun entersForeground() { @@ -111,11 +110,11 @@ class VideoViewHolder constructor(itemView: View) : if (videoView.isPlaying) { progress = videoView.currentPosition videoView.stopPlayback() - progressDisposable?.dispose() - progressDisposable = null } else { progress = 0 } + progressDisposable?.dispose() + progressDisposable = null } else { if (mVideoPath != null) { startPlaying() @@ -144,13 +143,30 @@ class VideoViewHolder constructor(itemView: View) : } videoView.setVideoPath(mVideoPath) - videoView.start() - if (progress > 0) { - videoView.seekTo(progress) + if (!wasPaused) { + videoView.start() + if (progress > 0) { + videoView.seekTo(progress) + } + } + } + + override fun handleCommand(commands: AttachmentCommands) { + if (!isSelected) return + when (commands) { + AttachmentCommands.StartVideo -> { + wasPaused = false + videoView.start() + } + AttachmentCommands.PauseVideo -> { + wasPaused = true + videoView.pause() + } } } override fun bind(attachmentInfo: AttachmentInfo) { progress = 0 + wasPaused = false } } diff --git a/vector/src/main/java/im/vector/riotx/features/media/AttachmentOverlayView.kt b/vector/src/main/java/im/vector/riotx/features/media/AttachmentOverlayView.kt index 05ebe17dea..a2657f7daf 100644 --- a/vector/src/main/java/im/vector/riotx/features/media/AttachmentOverlayView.kt +++ b/vector/src/main/java/im/vector/riotx/features/media/AttachmentOverlayView.kt @@ -35,6 +35,7 @@ class AttachmentOverlayView @JvmOverloads constructor( var onShareCallback: (() -> Unit)? = null var onBack: (() -> Unit)? = null + var onPlayPause: ((play: Boolean) -> Unit)? = null private val counterTextView: TextView private val infoTextView: TextView @@ -42,6 +43,8 @@ class AttachmentOverlayView @JvmOverloads constructor( private val overlayPlayPauseButton: ImageView private val overlaySeekBar: SeekBar + var isPlaying = false + val videoControlsGroup: Group init { @@ -61,6 +64,9 @@ class AttachmentOverlayView @JvmOverloads constructor( findViewById(R.id.overlayShareButton).setOnClickListener { onShareCallback?.invoke() } + findViewById(R.id.overlayPlayPauseButton).setOnClickListener { + onPlayPause?.invoke(!isPlaying) + } } fun updateWith(counter: String, senderInfo: String) { @@ -74,6 +80,7 @@ class AttachmentOverlayView @JvmOverloads constructor( overlayPlayPauseButton.setImageResource(if (!event.isPlaying) R.drawable.ic_play_arrow else R.drawable.ic_pause) val safeDuration = (if (event.duration == 0) 100 else event.duration).toFloat() val percent = ((event.progress / safeDuration) * 100f).toInt().coerceAtMost(100) + isPlaying = event.isPlaying overlaySeekBar.progress = percent } } diff --git a/vector/src/main/java/im/vector/riotx/features/media/RoomAttachmentProvider.kt b/vector/src/main/java/im/vector/riotx/features/media/RoomAttachmentProvider.kt index 09459b20d1..9f6080d95f 100644 --- a/vector/src/main/java/im/vector/riotx/features/media/RoomAttachmentProvider.kt +++ b/vector/src/main/java/im/vector/riotx/features/media/RoomAttachmentProvider.kt @@ -57,6 +57,7 @@ class RoomAttachmentProvider( interface InteractionListener { fun onDismissTapped() fun onShareTapped() + fun onPlayPause(play: Boolean) } var interactionListener: InteractionListener? = null @@ -197,6 +198,9 @@ class RoomAttachmentProvider( overlayView?.onShareCallback = { interactionListener?.onShareTapped() } + overlayView?.onPlayPause = { play -> + interactionListener?.onPlayPause(play) + } } val item = attachments[position] val dateString = item.root.localDateTime().let { diff --git a/vector/src/main/java/im/vector/riotx/features/media/VectorAttachmentViewerActivity.kt b/vector/src/main/java/im/vector/riotx/features/media/VectorAttachmentViewerActivity.kt index 44b536b2ae..2606f0bb76 100644 --- a/vector/src/main/java/im/vector/riotx/features/media/VectorAttachmentViewerActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/media/VectorAttachmentViewerActivity.kt @@ -39,6 +39,7 @@ import im.vector.matrix.android.api.session.room.model.message.getFileUrl import im.vector.matrix.android.api.session.room.timeline.TimelineEvent import im.vector.matrix.android.internal.crypto.attachments.toElementToDecrypt import im.vector.riotx.R +import im.vector.riotx.attachmentviewer.AttachmentCommands import im.vector.riotx.attachmentviewer.AttachmentViewerActivity import im.vector.riotx.core.di.ActiveSessionHolder import im.vector.riotx.core.di.DaggerScreenComponent @@ -242,6 +243,10 @@ class VectorAttachmentViewerActivity : AttachmentViewerActivity(), RoomAttachmen animateClose() } + override fun onPlayPause(play: Boolean) { + handle(if (play) AttachmentCommands.StartVideo else AttachmentCommands.PauseVideo) + } + override fun onShareTapped() { // Share eventList?.get(currentPosition)?.let { timelineEvent ->