1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 16:40:32 +00:00

Fix volume gestures not working anymore

This commit is contained in:
Stypox 2022-07-07 11:09:07 +02:00
parent 4979f84e41
commit 1cf746f721
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 39 additions and 23 deletions

View File

@ -8,11 +8,13 @@ import android.view.View.OnTouchListener
import android.widget.ProgressBar import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.content.res.AppCompatResources
import androidx.core.view.isVisible
import org.schabi.newpipe.MainActivity import org.schabi.newpipe.MainActivity
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.ktx.AnimationType import org.schabi.newpipe.ktx.AnimationType
import org.schabi.newpipe.ktx.animate import org.schabi.newpipe.ktx.animate
import org.schabi.newpipe.player.Player import org.schabi.newpipe.player.Player
import org.schabi.newpipe.player.helper.AudioReactor
import org.schabi.newpipe.player.helper.PlayerHelper import org.schabi.newpipe.player.helper.PlayerHelper
import org.schabi.newpipe.player.ui.MainPlayerUi import org.schabi.newpipe.player.ui.MainPlayerUi
import kotlin.math.abs import kotlin.math.abs
@ -64,22 +66,27 @@ class MainPlayerGestureListener(
} }
private fun onScrollVolume(distanceY: Float) { private fun onScrollVolume(distanceY: Float) {
val bar: ProgressBar = binding.volumeProgressBar
val audioReactor: AudioReactor = player.audioReactor
// If we just started sliding, change the progress bar to match the system volume // If we just started sliding, change the progress bar to match the system volume
if (binding.volumeRelativeLayout.visibility != View.VISIBLE) { if (!binding.volumeRelativeLayout.isVisible) {
val volumePercent: Float = val volumePercent: Float = audioReactor.volume / audioReactor.maxVolume.toFloat()
player.audioReactor.volume / player.audioReactor.maxVolume.toFloat() bar.progress = (volumePercent * bar.max).toInt()
binding.volumeProgressBar.progress = (volumePercent * MAX_GESTURE_LENGTH).toInt()
} }
// Update progress bar
binding.volumeProgressBar.incrementProgressBy(distanceY.toInt()) binding.volumeProgressBar.incrementProgressBy(distanceY.toInt())
val currentProgressPercent: Float =
binding.volumeProgressBar.progress.toFloat() / MAX_GESTURE_LENGTH // Update volume
val currentVolume = (player.audioReactor.maxVolume * currentProgressPercent).toInt() val currentProgressPercent: Float = bar.progress / bar.max.toFloat()
player.audioReactor.volume = currentVolume val currentVolume = (audioReactor.maxVolume * currentProgressPercent).toInt()
audioReactor.volume = currentVolume
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onScroll().volumeControl, currentVolume = $currentVolume") Log.d(TAG, "onScroll().volumeControl, currentVolume = $currentVolume")
} }
// Update player center image
binding.volumeImageView.setImageDrawable( binding.volumeImageView.setImageDrawable(
AppCompatResources.getDrawable( AppCompatResources.getDrawable(
player.context, player.context,
@ -92,12 +99,11 @@ class MainPlayerGestureListener(
) )
) )
if (binding.volumeRelativeLayout.visibility != View.VISIBLE) { // Make sure the correct layout is visible
if (!binding.volumeRelativeLayout.isVisible) {
binding.volumeRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA) binding.volumeRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA)
} }
if (binding.brightnessRelativeLayout.visibility == View.VISIBLE) { binding.brightnessRelativeLayout.isVisible = false
binding.volumeRelativeLayout.visibility = View.GONE
}
} }
private fun onScrollBrightness(distanceY: Float) { private fun onScrollBrightness(distanceY: Float) {
@ -105,9 +111,13 @@ class MainPlayerGestureListener(
val window = parent.window val window = parent.window
val layoutParams = window.attributes val layoutParams = window.attributes
val bar: ProgressBar = binding.brightnessProgressBar val bar: ProgressBar = binding.brightnessProgressBar
// Update progress bar
val oldBrightness = layoutParams.screenBrightness val oldBrightness = layoutParams.screenBrightness
bar.progress = (bar.max * max(0f, min(1f, oldBrightness))).toInt() bar.progress = (bar.max * max(0f, min(1f, oldBrightness))).toInt()
bar.incrementProgressBy(distanceY.toInt()) bar.incrementProgressBy(distanceY.toInt())
// Update brightness
val currentProgressPercent = bar.progress.toFloat() / bar.max val currentProgressPercent = bar.progress.toFloat() / bar.max
layoutParams.screenBrightness = currentProgressPercent layoutParams.screenBrightness = currentProgressPercent
window.attributes = layoutParams window.attributes = layoutParams
@ -121,26 +131,32 @@ class MainPlayerGestureListener(
"currentBrightness = " + currentProgressPercent "currentBrightness = " + currentProgressPercent
) )
} }
// Update player center image
binding.brightnessImageView.setImageDrawable( binding.brightnessImageView.setImageDrawable(
AppCompatResources.getDrawable( AppCompatResources.getDrawable(
player.context, player.context,
if (currentProgressPercent < 0.25) R.drawable.ic_brightness_low else if (currentProgressPercent < 0.75) R.drawable.ic_brightness_medium else R.drawable.ic_brightness_high when {
currentProgressPercent < 0.25 -> R.drawable.ic_brightness_low
currentProgressPercent < 0.75 -> R.drawable.ic_brightness_medium
else -> R.drawable.ic_brightness_high
}
) )
) )
if (binding.brightnessRelativeLayout.visibility != View.VISIBLE) {
// Make sure the correct layout is visible
if (!binding.brightnessRelativeLayout.isVisible) {
binding.brightnessRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA) binding.brightnessRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA)
} }
if (binding.volumeRelativeLayout.visibility == View.VISIBLE) { binding.volumeRelativeLayout.isVisible = false
binding.volumeRelativeLayout.visibility = View.GONE
}
} }
override fun onScrollEnd(event: MotionEvent) { override fun onScrollEnd(event: MotionEvent) {
super.onScrollEnd(event) super.onScrollEnd(event)
if (binding.volumeRelativeLayout.visibility == View.VISIBLE) { if (binding.volumeRelativeLayout.isVisible) {
binding.volumeRelativeLayout.animate(false, 200, AnimationType.SCALE_AND_ALPHA, 200) binding.volumeRelativeLayout.animate(false, 200, AnimationType.SCALE_AND_ALPHA, 200)
} }
if (binding.brightnessRelativeLayout.visibility == View.VISIBLE) { if (binding.brightnessRelativeLayout.isVisible) {
binding.brightnessRelativeLayout.animate(false, 200, AnimationType.SCALE_AND_ALPHA, 200) binding.brightnessRelativeLayout.animate(false, 200, AnimationType.SCALE_AND_ALPHA, 200)
} }
} }
@ -210,7 +226,6 @@ class MainPlayerGestureListener(
private val TAG = MainPlayerGestureListener::class.java.simpleName private val TAG = MainPlayerGestureListener::class.java.simpleName
private val DEBUG = MainActivity.DEBUG private val DEBUG = MainActivity.DEBUG
private const val MOVEMENT_THRESHOLD = 40 private const val MOVEMENT_THRESHOLD = 40
const val MAX_GESTURE_LENGTH = 0.75f
private fun getNavigationBarHeight(context: Context): Int { private fun getNavigationBarHeight(context: Context): Int {
val resId = context.resources val resId = context.resources

View File

@ -520,12 +520,13 @@ public final class MainPlayerUi extends VideoPlayerUi implements View.OnLayoutCh
public void onLayoutChange(final View view, final int l, final int t, final int r, final int b, public void onLayoutChange(final View view, final int l, final int t, final int r, final int b,
final int ol, final int ot, final int or, final int ob) { final int ol, final int ot, final int or, final int ob) {
if (l != ol || t != ot || r != or || b != ob) { if (l != ol || t != ot || r != or || b != ob) {
// Use smaller value to be consistent between screen orientations // Use a smaller value to be consistent across screen orientations, and to make usage
// (and to make usage easier) // easier. Multiply by 3/4 to ensure the user does not need to move the finger up to the
// screen border, in order to reach the maximum volume/brightness.
final int width = r - l; final int width = r - l;
final int height = b - t; final int height = b - t;
final int min = Math.min(width, height); final int min = Math.min(width, height);
final int maxGestureLength = (int) (min * MainPlayerGestureListener.MAX_GESTURE_LENGTH); final int maxGestureLength = (int) (min * 0.75);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "maxGestureLength = " + maxGestureLength); Log.d(TAG, "maxGestureLength = " + maxGestureLength);