2018-10-15 19:56:11 +02:00
|
|
|
/* Copyright 2017 Andrew Dawson
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.fragment
|
|
|
|
|
|
|
|
import android.animation.Animator
|
|
|
|
import android.animation.AnimatorListenerAdapter
|
|
|
|
import android.annotation.SuppressLint
|
2022-12-05 14:33:38 +01:00
|
|
|
import android.content.Context
|
2018-10-15 19:56:11 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
2021-09-17 21:55:54 +02:00
|
|
|
import android.text.method.ScrollingMovementMethod
|
2022-12-05 14:33:38 +01:00
|
|
|
import android.view.GestureDetector
|
2020-01-16 19:01:02 +01:00
|
|
|
import android.view.KeyEvent
|
2018-10-15 19:56:11 +02:00
|
|
|
import android.view.LayoutInflater
|
2022-12-05 14:33:38 +01:00
|
|
|
import android.view.MotionEvent
|
2018-10-15 19:56:11 +02:00
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.MediaController
|
2022-12-05 14:33:38 +01:00
|
|
|
import androidx.core.view.GestureDetectorCompat
|
2018-10-15 19:56:11 +02:00
|
|
|
import com.keylesspalace.tusky.ViewMediaActivity
|
2021-03-13 21:27:20 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.FragmentViewVideoBinding
|
2018-10-15 19:56:11 +02:00
|
|
|
import com.keylesspalace.tusky.entity.Attachment
|
|
|
|
import com.keylesspalace.tusky.util.hide
|
2018-11-01 14:52:22 +01:00
|
|
|
import com.keylesspalace.tusky.util.visible
|
2020-01-16 19:01:02 +01:00
|
|
|
import com.keylesspalace.tusky.view.ExposedPlayPauseVideoView
|
2022-12-05 14:33:38 +01:00
|
|
|
import kotlin.math.abs
|
2018-10-15 19:56:11 +02:00
|
|
|
|
|
|
|
class ViewVideoFragment : ViewMediaFragment() {
|
2022-12-05 14:33:38 +01:00
|
|
|
interface VideoActionsListener {
|
|
|
|
fun onDismiss()
|
|
|
|
}
|
2021-03-13 21:27:20 +01:00
|
|
|
|
|
|
|
private var _binding: FragmentViewVideoBinding? = null
|
|
|
|
private val binding get() = _binding!!
|
|
|
|
|
2022-12-05 14:33:38 +01:00
|
|
|
private lateinit var videoActionsListener: VideoActionsListener
|
2018-10-15 19:56:11 +02:00
|
|
|
private lateinit var toolbar: View
|
|
|
|
private val handler = Handler(Looper.getMainLooper())
|
|
|
|
private val hideToolbar = Runnable {
|
|
|
|
// Hoist toolbar hiding to activity so it can track state across different fragments
|
|
|
|
// This is explicitly stored as runnable so that we pass it to the handler later for cancellation
|
|
|
|
mediaActivity.onPhotoTap()
|
2020-01-16 19:01:02 +01:00
|
|
|
mediaController.hide()
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
private lateinit var mediaActivity: ViewMediaActivity
|
2021-06-28 21:13:24 +02:00
|
|
|
private lateinit var mediaController: MediaController
|
2020-01-16 19:01:02 +01:00
|
|
|
private var isAudio = false
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2023-02-20 19:58:37 +01:00
|
|
|
companion object {
|
|
|
|
private const val TOOLBAR_HIDE_DELAY_MS = 3000L
|
|
|
|
}
|
|
|
|
|
2022-12-05 14:33:38 +01:00
|
|
|
override fun onAttach(context: Context) {
|
|
|
|
super.onAttach(context)
|
|
|
|
videoActionsListener = context as VideoActionsListener
|
|
|
|
}
|
|
|
|
|
2022-12-30 11:22:39 +01:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2022-12-30 11:22:39 +01:00
|
|
|
if (_binding != null) {
|
2023-02-23 19:41:16 +01:00
|
|
|
if (mediaActivity.isToolbarVisible && !isAudio) {
|
|
|
|
hideToolbarAfterDelay(TOOLBAR_HIDE_DELAY_MS)
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.start()
|
2022-12-30 11:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
|
|
|
|
if (_binding != null) {
|
2018-10-15 19:56:11 +02:00
|
|
|
handler.removeCallbacks(hideToolbar)
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.pause()
|
2018-11-01 14:52:22 +01:00
|
|
|
mediaController.hide()
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
2020-08-01 21:48:51 +02:00
|
|
|
override fun setupMediaView(
|
2021-06-28 21:13:24 +02:00
|
|
|
url: String,
|
|
|
|
previewUrl: String?,
|
|
|
|
description: String?,
|
|
|
|
showingDescription: Boolean
|
2020-08-01 21:48:51 +02:00
|
|
|
) {
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.mediaDescription.text = description
|
|
|
|
binding.mediaDescription.visible(showingDescription)
|
2021-09-17 21:55:54 +02:00
|
|
|
binding.mediaDescription.movementMethod = ScrollingMovementMethod()
|
2019-10-01 18:24:09 +02:00
|
|
|
|
2023-01-27 21:52:29 +01:00
|
|
|
// Ensure the description is visible over the video
|
|
|
|
binding.mediaDescription.elevation = binding.videoView.elevation + 1
|
|
|
|
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.transitionName = url
|
|
|
|
binding.videoView.setVideoPath(url)
|
2020-01-16 19:01:02 +01:00
|
|
|
mediaController = object : MediaController(mediaActivity) {
|
|
|
|
override fun show(timeout: Int) {
|
|
|
|
// We're doing manual auto-close management.
|
|
|
|
// Also, take focus back from the pause button so we can use the back button.
|
|
|
|
super.show(0)
|
|
|
|
mediaController.requestFocus()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun dispatchKeyEvent(event: KeyEvent?): Boolean {
|
|
|
|
if (event?.keyCode == KeyEvent.KEYCODE_BACK) {
|
|
|
|
if (event.action == KeyEvent.ACTION_UP) {
|
|
|
|
hide()
|
|
|
|
activity?.supportFinishAfterTransition()
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return super.dispatchKeyEvent(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:27:20 +01:00
|
|
|
mediaController.setMediaPlayer(binding.videoView)
|
|
|
|
binding.videoView.setMediaController(mediaController)
|
|
|
|
binding.videoView.requestFocus()
|
2021-06-28 21:13:24 +02:00
|
|
|
binding.videoView.setPlayPauseListener(object : ExposedPlayPauseVideoView.PlayPauseListener {
|
2020-01-16 19:01:02 +01:00
|
|
|
override fun onPlay() {
|
2023-02-23 19:41:16 +01:00
|
|
|
if (!isAudio) {
|
2020-01-16 19:01:02 +01:00
|
|
|
hideToolbarAfterDelay(TOOLBAR_HIDE_DELAY_MS)
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 19:41:16 +01:00
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
if (!isAudio) {
|
|
|
|
handler.removeCallbacks(hideToolbar)
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 19:01:02 +01:00
|
|
|
})
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.setOnPreparedListener { mp ->
|
|
|
|
val containerWidth = binding.videoContainer.measuredWidth.toFloat()
|
|
|
|
val containerHeight = binding.videoContainer.measuredHeight.toFloat()
|
2019-10-01 18:24:09 +02:00
|
|
|
val videoWidth = mp.videoWidth.toFloat()
|
|
|
|
val videoHeight = mp.videoHeight.toFloat()
|
|
|
|
|
2021-09-17 21:55:54 +02:00
|
|
|
if (isAudio) {
|
|
|
|
binding.videoView.layoutParams.height = 1
|
|
|
|
binding.videoView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
|
} else if (containerWidth / containerHeight > videoWidth / videoHeight) {
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
|
binding.videoView.layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT
|
2019-10-01 18:24:09 +02:00
|
|
|
} else {
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
|
binding.videoView.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
|
2019-10-01 18:24:09 +02:00
|
|
|
}
|
|
|
|
|
2020-01-16 19:01:02 +01:00
|
|
|
// Wait until the media is loaded before accepting taps as we don't want toolbar to
|
|
|
|
// be hidden until then.
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.videoView.setOnTouchListener { _, _ ->
|
2020-01-16 19:01:02 +01:00
|
|
|
mediaActivity.onPhotoTap()
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2023-02-23 19:41:16 +01:00
|
|
|
// Audio doesn't cause the controller to show automatically
|
|
|
|
if (isAudio) {
|
|
|
|
mediaController.show()
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.progressBar.hide()
|
2018-10-15 19:56:11 +02:00
|
|
|
mp.isLooping = true
|
|
|
|
}
|
|
|
|
|
2020-04-06 11:46:38 +02:00
|
|
|
if (requireArguments().getBoolean(ARG_START_POSTPONED_TRANSITION)) {
|
2018-10-15 19:56:11 +02:00
|
|
|
mediaActivity.onBringUp()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun hideToolbarAfterDelay(delayMilliseconds: Long) {
|
|
|
|
handler.postDelayed(hideToolbar, delayMilliseconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
|
|
|
mediaActivity = activity as ViewMediaActivity
|
2021-03-13 21:27:20 +01:00
|
|
|
toolbar = mediaActivity.toolbar
|
|
|
|
_binding = FragmentViewVideoBinding.inflate(inflater, container, false)
|
|
|
|
return binding.root
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
2022-12-05 14:33:38 +01:00
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
2018-10-15 19:56:11 +02:00
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2022-11-09 19:32:54 +01:00
|
|
|
val attachment = arguments?.getParcelable<Attachment>(ARG_ATTACHMENT)
|
2023-02-20 19:58:37 +01:00
|
|
|
?: throw IllegalArgumentException("attachment has to be set")
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2022-08-07 19:36:09 +02:00
|
|
|
val url = attachment.url
|
2020-01-16 19:01:02 +01:00
|
|
|
isAudio = attachment.type == Attachment.Type.AUDIO
|
2022-12-05 14:33:38 +01:00
|
|
|
|
|
|
|
val gestureDetector = GestureDetectorCompat(
|
|
|
|
requireContext(),
|
|
|
|
object : GestureDetector.SimpleOnGestureListener() {
|
|
|
|
override fun onDown(event: MotionEvent): Boolean {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onFling(
|
|
|
|
e1: MotionEvent,
|
|
|
|
e2: MotionEvent,
|
|
|
|
velocityX: Float,
|
|
|
|
velocityY: Float
|
|
|
|
): Boolean {
|
|
|
|
if (abs(velocityY) > abs(velocityX)) {
|
|
|
|
videoActionsListener.onDismiss()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var lastY = 0f
|
|
|
|
binding.root.setOnTouchListener { _, event ->
|
|
|
|
if (event.action == MotionEvent.ACTION_DOWN) {
|
|
|
|
lastY = event.rawY
|
|
|
|
} else if (event.pointerCount == 1 && event.action == MotionEvent.ACTION_MOVE) {
|
|
|
|
val diff = event.rawY - lastY
|
|
|
|
if (binding.videoView.translationY != 0f || abs(diff) > 40) {
|
|
|
|
binding.videoView.translationY += diff
|
|
|
|
val scale = (-abs(binding.videoView.translationY) / 720 + 1).coerceAtLeast(0.5f)
|
|
|
|
binding.videoView.scaleY = scale
|
|
|
|
binding.videoView.scaleX = scale
|
|
|
|
lastY = event.rawY
|
|
|
|
return@setOnTouchListener true
|
|
|
|
}
|
|
|
|
} else if (event.action == MotionEvent.ACTION_UP || event.action == MotionEvent.ACTION_CANCEL) {
|
|
|
|
if (abs(binding.videoView.translationY) > 180) {
|
|
|
|
videoActionsListener.onDismiss()
|
|
|
|
} else {
|
|
|
|
binding.videoView.animate().translationY(0f).scaleX(1f).scaleY(1f).start()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gestureDetector.onTouchEvent(event)
|
|
|
|
}
|
|
|
|
|
2019-08-04 20:22:57 +02:00
|
|
|
finalizeViewSetup(url, attachment.previewUrl, attachment.description)
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onToolbarVisibilityChange(visible: Boolean) {
|
2021-03-13 21:27:20 +01:00
|
|
|
if (_binding == null || !userVisibleHint) {
|
2018-10-15 19:56:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
isDescriptionVisible = showingDescription && visible
|
|
|
|
val alpha = if (isDescriptionVisible) 1.0f else 0.0f
|
2020-01-16 19:01:02 +01:00
|
|
|
if (isDescriptionVisible) {
|
|
|
|
// If to be visible, need to make visible immediately and animate alpha
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.mediaDescription.alpha = 0.0f
|
|
|
|
binding.mediaDescription.visible(isDescriptionVisible)
|
2020-01-16 19:01:02 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 21:27:20 +01:00
|
|
|
binding.mediaDescription.animate().alpha(alpha)
|
2021-06-28 21:13:24 +02:00
|
|
|
.setListener(object : AnimatorListenerAdapter() {
|
|
|
|
override fun onAnimationEnd(animation: Animator) {
|
|
|
|
if (_binding != null) {
|
|
|
|
binding.mediaDescription.visible(isDescriptionVisible)
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
animation.removeListener(this)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.start()
|
2018-10-15 19:56:11 +02:00
|
|
|
|
2021-03-13 21:27:20 +01:00
|
|
|
if (visible && binding.videoView.isPlaying && !isAudio) {
|
2018-10-15 19:56:11 +02:00
|
|
|
hideToolbarAfterDelay(TOOLBAR_HIDE_DELAY_MS)
|
|
|
|
} else {
|
|
|
|
handler.removeCallbacks(hideToolbar)
|
|
|
|
}
|
|
|
|
}
|
2019-08-04 20:22:57 +02:00
|
|
|
|
|
|
|
override fun onTransitionEnd() {
|
|
|
|
}
|
2021-03-13 21:27:20 +01:00
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
_binding = null
|
|
|
|
}
|
2018-10-15 19:56:11 +02:00
|
|
|
}
|