Skip to content

Commit

Permalink
Fix volume gestures not working anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Jul 7, 2022
1 parent b20ffc4 commit 26099bb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.schabi.newpipe.R
import org.schabi.newpipe.ktx.AnimationType
import org.schabi.newpipe.ktx.animate
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.ui.MainPlayerUi
import kotlin.math.abs
Expand Down Expand Up @@ -64,22 +65,27 @@ class MainPlayerGestureListener(
}

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 (binding.volumeRelativeLayout.visibility != View.VISIBLE) {
val volumePercent: Float =
player.audioReactor.volume / player.audioReactor.maxVolume.toFloat()
binding.volumeProgressBar.progress = (volumePercent * MAX_GESTURE_LENGTH).toInt()
val volumePercent: Float = audioReactor.volume / audioReactor.maxVolume.toFloat()
bar.progress = (volumePercent * bar.max).toInt()
}

// Update progress bar
binding.volumeProgressBar.incrementProgressBy(distanceY.toInt())
val currentProgressPercent: Float =
binding.volumeProgressBar.progress.toFloat() / MAX_GESTURE_LENGTH
val currentVolume = (player.audioReactor.maxVolume * currentProgressPercent).toInt()
player.audioReactor.volume = currentVolume

// Update volume
val currentProgressPercent: Float = bar.progress / bar.max.toFloat()
val currentVolume = (audioReactor.maxVolume * currentProgressPercent).toInt()
audioReactor.volume = currentVolume
if (DEBUG) {
Log.d(TAG, "onScroll().volumeControl, currentVolume = $currentVolume")
}

// Update player center image
binding.volumeImageView.setImageDrawable(
AppCompatResources.getDrawable(
player.context,
Expand All @@ -92,6 +98,7 @@ class MainPlayerGestureListener(
)
)

// Make sure the correct layout is visible
if (binding.volumeRelativeLayout.visibility != View.VISIBLE) {
binding.volumeRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA)
}
Expand All @@ -105,9 +112,13 @@ class MainPlayerGestureListener(
val window = parent.window
val layoutParams = window.attributes
val bar: ProgressBar = binding.brightnessProgressBar

// Update progress bar
val oldBrightness = layoutParams.screenBrightness
bar.progress = (bar.max * max(0f, min(1f, oldBrightness))).toInt()
bar.incrementProgressBy(distanceY.toInt())

// Update brightness
val currentProgressPercent = bar.progress.toFloat() / bar.max
layoutParams.screenBrightness = currentProgressPercent
window.attributes = layoutParams
Expand All @@ -121,12 +132,20 @@ class MainPlayerGestureListener(
"currentBrightness = " + currentProgressPercent
)
}

// Update player center image
binding.brightnessImageView.setImageDrawable(
AppCompatResources.getDrawable(
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
}
)
)

// Make sure the correct layout is visible
if (binding.brightnessRelativeLayout.visibility != View.VISIBLE) {
binding.brightnessRelativeLayout.animate(true, 200, AnimationType.SCALE_AND_ALPHA)
}
Expand Down Expand Up @@ -210,7 +229,6 @@ class MainPlayerGestureListener(
private val TAG = MainPlayerGestureListener::class.java.simpleName
private val DEBUG = MainActivity.DEBUG
private const val MOVEMENT_THRESHOLD = 40
const val MAX_GESTURE_LENGTH = 0.75f

private fun getNavigationBarHeight(context: Context): Int {
val resId = context.resources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,13 @@ protected void setupSubtitleView(final float captionScale) {
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) {
if (l != ol || t != ot || r != or || b != ob) {
// Use smaller value to be consistent between screen orientations
// (and to make usage easier)
// Use a smaller value to be consistent across screen orientations, and to make usage
// 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 height = b - t;
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) {
Log.d(TAG, "maxGestureLength = " + maxGestureLength);
Expand Down

0 comments on commit 26099bb

Please sign in to comment.