basic double tap implementation

This commit is contained in:
Bnyro 2022-07-08 17:26:45 +02:00
parent dbea7652ee
commit 61721fd2df
5 changed files with 95 additions and 81 deletions

View File

@ -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)
}
}

View File

@ -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<ChapterSegment>) {
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

View File

@ -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()
}
}

View File

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".activities.Player">
<com.github.libretube.views.CustomExoPlayerView
android:id="@+id/fullscreen_player"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black" />
</RelativeLayout>

View File

@ -1,26 +1,7 @@
<?xml version="1.0" encoding="utf-8"?><!-- Copyright 2020 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 0dp dimensions are used to prevent this view from influencing the size of
the parent view if it uses "wrap_content". It is expanded to occupy the
entirety of the parent in code, after the parent's size has been
determined. See: https://github.com/google/ExoPlayer/issues/8726.
-->
<View
android:id="@id/exo_controls_background"
android:layout_width="0dp"
@ -231,42 +212,47 @@
android:gravity="center"
android:padding="@dimen/exo_styled_controls_padding">
<LinearLayout
android:layout_width="0dp"
<FrameLayout
android:id="@+id/rewindFL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
android:layout_weight="1">
<Button
android:id="@id/exo_rew_with_amount"
style="@style/ExoStyledControls.Button.Center.RewWithAmount"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
<ImageButton
android:id="@+id/rewindBTN"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical"
android:background="?android:selectableItemBackground"
android:src="@drawable/ic_three_dots"
android:clickable="false"
app:tint="@android:color/white" />
</LinearLayout>
</FrameLayout>
<ImageButton
android:id="@id/exo_play_pause"
style="@style/ExoStyledControls.Button.Center.PlayPause"
app:tint="@android:color/white" />
<LinearLayout
android:layout_width="0dp"
<FrameLayout
android:id="@+id/forwardFL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
android:layout_weight="1">
<Button
android:id="@id/exo_ffwd_with_amount"
style="@style/ExoStyledControls.Button.Center.FfwdWithAmount"
android:layout_gravity="center"
android:layout_marginEnd="20dp"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
<ImageButton
android:id="@+id/forwardBTN"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_gravity="center_vertical|end"
android:background="?android:selectableItemBackground"
android:src="@drawable/ic_three_dots"
android:clickable="false"
app:tint="@android:color/white" />
</FrameLayout>
</LinearLayout>
</LinearLayout>