From 61721fd2df96055b65830cdeeb96ea19f9404e9d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 8 Jul 2022 17:26:45 +0200 Subject: [PATCH 1/5] basic double tap implementation --- .../com/github/libretube/activities/Player.kt | 15 ---- .../libretube/fragments/PlayerFragment.kt | 34 +++++++-- .../libretube/views/DoubleClickListener.kt | 37 ++++++++++ app/src/main/res/layout/activity_player.xml | 16 ---- .../layout/exo_styled_player_control_view.xml | 74 ++++++++----------- 5 files changed, 95 insertions(+), 81 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/activities/Player.kt create mode 100644 app/src/main/java/com/github/libretube/views/DoubleClickListener.kt delete mode 100644 app/src/main/res/layout/activity_player.xml diff --git a/app/src/main/java/com/github/libretube/activities/Player.kt b/app/src/main/java/com/github/libretube/activities/Player.kt deleted file mode 100644 index aa0a3cbec..000000000 --- a/app/src/main/java/com/github/libretube/activities/Player.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.libretube.activities - -import android.app.Activity -import android.os.Bundle -import com.github.libretube.R -import com.google.android.material.color.DynamicColors - -class Player : Activity() { - - override fun onCreate(savedInstanceState: Bundle?) { - DynamicColors.applyToActivityIfAvailable(this) - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_player) - } -} diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index dccc5a21c..5fef9054c 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -54,6 +54,7 @@ import com.github.libretube.util.CronetHelper import com.github.libretube.util.DescriptionAdapter import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.formatShort +import com.github.libretube.views.DoubleClickListener import com.google.android.exoplayer2.C import com.google.android.exoplayer2.DefaultLoadControl import com.google.android.exoplayer2.ExoPlayer @@ -319,10 +320,6 @@ class PlayerFragment : Fragment() { isFullScreen = !isFullScreen // scale the exo player center controls - playerBinding.exoFfwdWithAmount.scaleX = scaleFactor - playerBinding.exoFfwdWithAmount.scaleY = scaleFactor - playerBinding.exoRewWithAmount.scaleX = scaleFactor - playerBinding.exoRewWithAmount.scaleY = scaleFactor playerBinding.exoPlayPause.scaleX = scaleFactor playerBinding.exoPlayPause.scaleY = scaleFactor } @@ -691,6 +688,8 @@ class PlayerFragment : Fragment() { playerBinding.exoTitle.text = response.title + enableDoubleTapToSeek() + // init the chapters recyclerview if (response.chapters != null) initializeChapters(response.chapters) @@ -844,6 +843,31 @@ class PlayerFragment : Fragment() { } } + private fun enableDoubleTapToSeek() { + val seekIncrement = + PreferenceHelper.getString(requireContext(), "seek_increment", "5")?.toLong()!! * 1000 + + playerBinding.rewindFL.setOnClickListener( + DoubleClickListener( + callback = object : DoubleClickListener.Callback { + override fun doubleClicked() { + exoPlayer.seekTo(exoPlayer.currentPosition - seekIncrement) + } + } + ) + ) + + playerBinding.forwardFL.setOnClickListener( + DoubleClickListener( + callback = object : DoubleClickListener.Callback { + override fun doubleClicked() { + exoPlayer.seekTo(exoPlayer.currentPosition + seekIncrement) + } + } + ) + ) + } + private fun initializeChapters(chapters: List) { if (chapters.isNotEmpty()) { binding.chaptersRecView.layoutManager = @@ -1064,8 +1088,6 @@ class PlayerFragment : Fragment() { val visibility = if (isLocked) View.VISIBLE else View.GONE playerBinding.exoTopBarRight.visibility = visibility playerBinding.exoPlayPause.visibility = visibility - playerBinding.exoFfwdWithAmount.visibility = visibility - playerBinding.exoRewWithAmount.visibility = visibility playerBinding.exoBottomBar.visibility = visibility playerBinding.closeImageButton.visibility = visibility playerBinding.exoTitle.visibility = visibility diff --git a/app/src/main/java/com/github/libretube/views/DoubleClickListener.kt b/app/src/main/java/com/github/libretube/views/DoubleClickListener.kt new file mode 100644 index 000000000..9ef4a5e51 --- /dev/null +++ b/app/src/main/java/com/github/libretube/views/DoubleClickListener.kt @@ -0,0 +1,37 @@ +package com.github.libretube.views + +import android.view.View + +class DoubleClickListener(private val doubleClickTimeLimitMills: Long = 500, private val callback: Callback) : View.OnClickListener { + private var lastClicked: Long = -1L + + override fun onClick(v: View?) { + lastClicked = when { + lastClicked == -1L -> { + System.currentTimeMillis() + } + isDoubleClicked() -> { + callback.doubleClicked() + -1L + } + else -> { + System.currentTimeMillis() + } + } + } + + private fun getTimeDiff(from: Long, to: Long): Long { + return to - from + } + + private fun isDoubleClicked(): Boolean { + return getTimeDiff( + lastClicked, + System.currentTimeMillis() + ) <= doubleClickTimeLimitMills + } + + interface Callback { + fun doubleClicked() + } +} diff --git a/app/src/main/res/layout/activity_player.xml b/app/src/main/res/layout/activity_player.xml deleted file mode 100644 index 8075249cf..000000000 --- a/app/src/main/res/layout/activity_player.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/exo_styled_player_control_view.xml b/app/src/main/res/layout/exo_styled_player_control_view.xml index ddb5a8a18..8a1f8c1bc 100644 --- a/app/src/main/res/layout/exo_styled_player_control_view.xml +++ b/app/src/main/res/layout/exo_styled_player_control_view.xml @@ -1,26 +1,7 @@ - + - - - + android:layout_weight="1"> -