From 6e75d41956eb2dc18763c967a471f8b913b05c34 Mon Sep 17 00:00:00 2001 From: Mauricio Colli Date: Tue, 4 Sep 2018 23:54:17 -0300 Subject: [PATCH] Use current volume as the start value in the volume gesture - Renamed some variables/classes to increase readability --- .../newpipe/player/MainVideoPlayer.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java index 41e7c305d..4e8398ff2 100644 --- a/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java +++ b/app/src/main/java/org/schabi/newpipe/player/MainVideoPlayer.java @@ -460,7 +460,7 @@ public final class MainVideoPlayer extends AppCompatActivity public void initListeners() { super.initListeners(); - MySimpleOnGestureListener listener = new MySimpleOnGestureListener(); + PlayerGestureListener listener = new PlayerGestureListener(); gestureDetector = new GestureDetector(context, listener); gestureDetector.setIsLongpressEnabled(false); getRootView().setOnTouchListener(listener); @@ -489,6 +489,8 @@ public final class MainVideoPlayer extends AppCompatActivity volumeProgressBar.setMax(maxGestureLength); brightnessProgressBar.setMax(maxGestureLength); + + setInitialGestureValues(); } }); } @@ -799,6 +801,13 @@ public final class MainVideoPlayer extends AppCompatActivity // Utils //////////////////////////////////////////////////////////////////////////*/ + private void setInitialGestureValues() { + if (getAudioReactor() != null) { + final float currentVolumeNormalized = (float) getAudioReactor().getVolume() / getAudioReactor().getMaxVolume(); + volumeProgressBar.setProgress((int) (volumeProgressBar.getMax() * currentVolumeNormalized)); + } + } + @Override public void showControlsThenHide() { if (queueVisible) return; @@ -939,7 +948,7 @@ public final class MainVideoPlayer extends AppCompatActivity } } - private class MySimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { + private class PlayerGestureListener extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { private boolean isMoving; @Override @@ -978,31 +987,30 @@ public final class MainVideoPlayer extends AppCompatActivity return super.onDown(e); } - private final boolean isPlayerGestureEnabled = PlayerHelper.isPlayerGestureEnabled(getApplicationContext()); + private static final int MOVEMENT_THRESHOLD = 40; + private final boolean isPlayerGestureEnabled = PlayerHelper.isPlayerGestureEnabled(getApplicationContext()); private final int maxVolume = playerImpl.getAudioReactor().getMaxVolume(); - private final int MOVEMENT_THRESHOLD = 40; - @Override - public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { + public boolean onScroll(MotionEvent initialEvent, MotionEvent movingEvent, float distanceX, float distanceY) { if (!isPlayerGestureEnabled) return false; //noinspection PointlessBooleanExpression if (DEBUG && false) Log.d(TAG, "MainVideoPlayer.onScroll = " + - ", e1.getRaw = [" + e1.getRawX() + ", " + e1.getRawY() + "]" + - ", e2.getRaw = [" + e2.getRawX() + ", " + e2.getRawY() + "]" + + ", e1.getRaw = [" + initialEvent.getRawX() + ", " + initialEvent.getRawY() + "]" + + ", e2.getRaw = [" + movingEvent.getRawX() + ", " + movingEvent.getRawY() + "]" + ", distanceXy = [" + distanceX + ", " + distanceY + "]"); - if (!isMoving && ( - Math.abs(e2.getY() - e1.getY()) <= MOVEMENT_THRESHOLD - || Math.abs(distanceX) > Math.abs(distanceY) - ) || playerImpl.getCurrentState() == BasePlayer.STATE_COMPLETED) + final boolean insideThreshold = Math.abs(movingEvent.getY() - initialEvent.getY()) <= MOVEMENT_THRESHOLD; + if (!isMoving && (insideThreshold || Math.abs(distanceX) > Math.abs(distanceY)) + || playerImpl.getCurrentState() == BasePlayer.STATE_COMPLETED) { return false; + } isMoving = true; - if (e1.getX() > playerImpl.getRootView().getWidth() / 2) { + if (initialEvent.getX() > playerImpl.getRootView().getWidth() / 2) { playerImpl.getVolumeProgressBar().incrementProgressBy((int) distanceY); float currentProgressPercent = (float) playerImpl.getVolumeProgressBar().getProgress() / playerImpl.getMaxGestureLength();