NewPipe-app-android/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java

274 lines
11 KiB
Java
Raw Normal View History

package org.schabi.newpipe.player.event;
import android.app.Activity;
import android.util.Log;
2020-07-14 19:21:32 +02:00
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
2020-10-21 16:40:22 +02:00
import androidx.appcompat.content.res.AppCompatResources;
2020-10-21 16:40:22 +02:00
import org.jetbrains.annotations.NotNull;
import org.schabi.newpipe.R;
import org.schabi.newpipe.player.BasePlayer;
import org.schabi.newpipe.player.MainPlayer;
import org.schabi.newpipe.player.VideoPlayerImpl;
import org.schabi.newpipe.player.helper.PlayerHelper;
2020-07-14 19:21:32 +02:00
import static org.schabi.newpipe.player.BasePlayer.STATE_PLAYING;
import static org.schabi.newpipe.player.VideoPlayer.DEFAULT_CONTROLS_DURATION;
import static org.schabi.newpipe.player.VideoPlayer.DEFAULT_CONTROLS_HIDE_TIME;
import static org.schabi.newpipe.util.AnimationUtils.Type.SCALE_AND_ALPHA;
import static org.schabi.newpipe.util.AnimationUtils.animateView;
2020-10-21 16:40:22 +02:00
/**
* GestureListener for the player
*
* While {@link BasePlayerGestureListener} contains the logic behind the single gestures
* this class focuses on the visual aspect like hiding and showing the controls or changing
* volume/brightness during scrolling for specific events.
*/
2020-07-14 19:21:32 +02:00
public class PlayerGestureListener
2020-10-21 16:40:22 +02:00
extends BasePlayerGestureListener
2020-07-14 19:21:32 +02:00
implements View.OnTouchListener {
private static final String TAG = ".PlayerGestureListener";
private static final boolean DEBUG = BasePlayer.DEBUG;
private final boolean isVolumeGestureEnabled;
private final boolean isBrightnessGestureEnabled;
private final int maxVolume;
public PlayerGestureListener(final VideoPlayerImpl playerImpl, final MainPlayer service) {
2020-10-21 16:40:22 +02:00
super(playerImpl, service);
isVolumeGestureEnabled = PlayerHelper.isVolumeGestureEnabled(service);
isBrightnessGestureEnabled = PlayerHelper.isBrightnessGestureEnabled(service);
maxVolume = playerImpl.getAudioReactor().getMaxVolume();
}
@Override
2020-10-21 16:40:22 +02:00
public void onDoubleTap(@NotNull final MotionEvent event,
@NotNull final DisplayPortion portion) {
2020-07-14 19:21:32 +02:00
if (DEBUG) {
2020-10-21 16:40:22 +02:00
Log.d(TAG, "onDoubleTap called with playerType = ["
+ playerImpl.getPlayerType() + "], portion = ["
+ portion + "]");
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerImpl.isSomePopupMenuVisible()) {
playerImpl.hideControls(0, 0);
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (portion == DisplayPortion.LEFT) {
playerImpl.onFastRewind();
} else if (portion == DisplayPortion.MIDDLE) {
playerImpl.onPlayPause();
2020-10-21 16:40:22 +02:00
} else if (portion == DisplayPortion.RIGHT) {
playerImpl.onFastForward();
2020-07-14 19:21:32 +02:00
}
}
2020-07-14 19:21:32 +02:00
@Override
2020-10-21 16:40:22 +02:00
public void onSingleTap(@NotNull final MainPlayer.PlayerType playerType) {
2020-07-14 19:21:32 +02:00
if (DEBUG) {
2020-10-21 16:40:22 +02:00
Log.d(TAG, "onSingleTap called with playerType = ["
+ playerImpl.getPlayerType() + "]");
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerType == MainPlayer.PlayerType.POPUP) {
2020-10-21 16:40:22 +02:00
if (playerImpl.isControlsVisible()) {
playerImpl.hideControls(100, 100);
} else {
playerImpl.getPlayPauseButton().requestFocus();
playerImpl.showControlsThenHide();
}
2020-10-21 16:40:22 +02:00
} else /* playerType == MainPlayer.PlayerType.VIDEO */ {
2020-10-21 16:40:22 +02:00
if (playerImpl.isControlsVisible()) {
playerImpl.hideControls(150, 0);
} else {
if (playerImpl.getCurrentState() == BasePlayer.STATE_COMPLETED) {
playerImpl.showControls(0);
} else {
playerImpl.showControlsThenHide();
}
}
2020-07-14 19:21:32 +02:00
}
}
@Override
2020-10-21 16:40:22 +02:00
public void onScroll(@NotNull final MainPlayer.PlayerType playerType,
@NotNull final DisplayPortion portion,
@NotNull final MotionEvent initialEvent,
@NotNull final MotionEvent movingEvent,
final float distanceX, final float distanceY) {
2020-07-14 19:21:32 +02:00
if (DEBUG) {
2020-10-21 16:40:22 +02:00
Log.d(TAG, "onScroll called with playerType = ["
+ playerImpl.getPlayerType() + "], portion = ["
+ portion + "]");
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerType == MainPlayer.PlayerType.VIDEO) {
if (portion == DisplayPortion.LEFT_HALF) {
onScrollMainVolume(distanceX, distanceY);
2020-10-21 16:40:22 +02:00
} else /* DisplayPortion.RIGHT_HALF */ {
onScrollMainBrightness(distanceX, distanceY);
}
2020-10-21 16:40:22 +02:00
} else /* MainPlayer.PlayerType.POPUP */ {
final View closingOverlayView = playerImpl.getClosingOverlayView();
if (playerImpl.isInsideClosingRadius(movingEvent)) {
if (closingOverlayView.getVisibility() == View.GONE) {
animateView(closingOverlayView, true, 250);
}
} else {
2020-10-21 16:40:22 +02:00
if (closingOverlayView.getVisibility() == View.VISIBLE) {
animateView(closingOverlayView, false, 0);
}
}
}
}
2020-10-21 16:40:22 +02:00
private void onScrollMainVolume(final float distanceX, final float distanceY) {
if (isVolumeGestureEnabled) {
playerImpl.getVolumeProgressBar().incrementProgressBy((int) distanceY);
2020-07-14 19:21:32 +02:00
final float currentProgressPercent = (float) playerImpl
.getVolumeProgressBar().getProgress() / playerImpl.getMaxGestureLength();
final int currentVolume = (int) (maxVolume * currentProgressPercent);
playerImpl.getAudioReactor().setVolume(currentVolume);
2020-07-14 19:21:32 +02:00
if (DEBUG) {
Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
}
2020-06-27 05:25:50 +02:00
playerImpl.getVolumeImageView().setImageDrawable(
2020-07-14 19:21:32 +02:00
AppCompatResources.getDrawable(service, currentProgressPercent <= 0
? R.drawable.ic_volume_off_white_24dp
2020-07-13 03:17:21 +02:00
: currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute_white_24dp
: currentProgressPercent < 0.75 ? R.drawable.ic_volume_down_white_24dp
: R.drawable.ic_volume_up_white_24dp)
);
if (playerImpl.getVolumeRelativeLayout().getVisibility() != View.VISIBLE) {
animateView(playerImpl.getVolumeRelativeLayout(), SCALE_AND_ALPHA, true, 200);
}
if (playerImpl.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) {
playerImpl.getBrightnessRelativeLayout().setVisibility(View.GONE);
}
2020-10-21 16:40:22 +02:00
}
}
private void onScrollMainBrightness(final float distanceX, final float distanceY) {
if (isBrightnessGestureEnabled) {
final Activity parent = playerImpl.getParentActivity();
2020-07-14 19:21:32 +02:00
if (parent == null) {
2020-10-21 16:40:22 +02:00
return;
2020-07-14 19:21:32 +02:00
}
final Window window = parent.getWindow();
final WindowManager.LayoutParams layoutParams = window.getAttributes();
final ProgressBar bar = playerImpl.getBrightnessProgressBar();
final float oldBrightness = layoutParams.screenBrightness;
bar.setProgress((int) (bar.getMax() * Math.max(0, Math.min(1, oldBrightness))));
bar.incrementProgressBy((int) distanceY);
final float currentProgressPercent = (float) bar.getProgress() / bar.getMax();
layoutParams.screenBrightness = currentProgressPercent;
window.setAttributes(layoutParams);
// Save current brightness level
PlayerHelper.setScreenBrightness(parent, currentProgressPercent);
2020-07-14 19:21:32 +02:00
if (DEBUG) {
Log.d(TAG, "onScroll().brightnessControl, "
+ "currentBrightness = " + currentProgressPercent);
}
playerImpl.getBrightnessImageView().setImageDrawable(
2020-06-27 05:25:50 +02:00
AppCompatResources.getDrawable(service,
2020-07-14 19:21:32 +02:00
currentProgressPercent < 0.25
? R.drawable.ic_brightness_low_white_24dp
: currentProgressPercent < 0.75
? R.drawable.ic_brightness_medium_white_24dp
2020-07-13 03:17:21 +02:00
: R.drawable.ic_brightness_high_white_24dp)
);
if (playerImpl.getBrightnessRelativeLayout().getVisibility() != View.VISIBLE) {
animateView(playerImpl.getBrightnessRelativeLayout(), SCALE_AND_ALPHA, true, 200);
}
if (playerImpl.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) {
playerImpl.getVolumeRelativeLayout().setVisibility(View.GONE);
}
}
}
2020-10-21 16:40:22 +02:00
@Override
public void onScrollEnd(@NotNull final MainPlayer.PlayerType playerType,
@NotNull final MotionEvent event) {
2020-07-14 19:21:32 +02:00
if (DEBUG) {
2020-10-21 16:40:22 +02:00
Log.d(TAG, "onScrollEnd called with playerType = ["
+ playerImpl.getPlayerType() + "]");
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerType == MainPlayer.PlayerType.VIDEO) {
if (DEBUG) {
Log.d(TAG, "onScrollEnd() called");
}
2020-10-21 16:40:22 +02:00
if (playerImpl.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) {
animateView(playerImpl.getVolumeRelativeLayout(), SCALE_AND_ALPHA,
false, 200, 200);
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerImpl.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) {
animateView(playerImpl.getBrightnessRelativeLayout(), SCALE_AND_ALPHA,
false, 200, 200);
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerImpl.isControlsVisible() && playerImpl.getCurrentState() == STATE_PLAYING) {
playerImpl.hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
} else {
if (playerImpl == null) {
return;
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerImpl.isControlsVisible() && playerImpl.getCurrentState() == STATE_PLAYING) {
playerImpl.hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
}
2020-10-21 16:40:22 +02:00
if (playerImpl.isInsideClosingRadius(event)) {
playerImpl.closePopup();
} else {
animateView(playerImpl.getClosingOverlayView(), false, 0);
2020-10-21 16:40:22 +02:00
if (!playerImpl.isPopupClosing) {
animateView(playerImpl.getCloseOverlayButton(), false, 200);
}
}
}
}
2020-10-21 16:40:22 +02:00
@Override
public void onPopupResizingStart() {
if (DEBUG) {
Log.d(TAG, "onPopupResizingStart called");
}
2020-10-21 16:40:22 +02:00
playerImpl.showAndAnimateControl(-1, true);
playerImpl.getLoadingPanel().setVisibility(View.GONE);
2020-10-21 16:40:22 +02:00
playerImpl.hideControls(0, 0);
animateView(playerImpl.getCurrentDisplaySeek(), false, 0, 0);
animateView(playerImpl.getResizingIndicator(), true, 200, 0);
}
2020-10-21 16:40:22 +02:00
@Override
public void onPopupResizingEnd() {
if (DEBUG) {
Log.d(TAG, "onPopupResizingEnd called");
2020-07-13 03:17:21 +02:00
}
2020-10-21 16:40:22 +02:00
animateView(playerImpl.getResizingIndicator(), false, 100, 0);
2020-07-13 03:17:21 +02:00
}
}