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

257 lines
10 KiB
Java
Raw Normal View History

package org.schabi.newpipe.player.event;
import static org.schabi.newpipe.ktx.AnimationType.ALPHA;
import static org.schabi.newpipe.ktx.AnimationType.SCALE_AND_ALPHA;
import static org.schabi.newpipe.ktx.ViewUtils.animate;
import static org.schabi.newpipe.player.Player.DEFAULT_CONTROLS_DURATION;
import static org.schabi.newpipe.player.Player.DEFAULT_CONTROLS_HIDE_TIME;
import static org.schabi.newpipe.player.Player.STATE_PLAYING;
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.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
2020-10-21 16:40:22 +02:00
2021-01-08 18:35:33 +01:00
import org.schabi.newpipe.MainActivity;
import org.schabi.newpipe.R;
import org.schabi.newpipe.player.MainPlayer;
2021-01-08 18:35:33 +01:00
import org.schabi.newpipe.player.Player;
import org.schabi.newpipe.player.helper.PlayerHelper;
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 {
2021-01-08 18:35:33 +01:00
private static final String TAG = PlayerGestureListener.class.getSimpleName();
private static final boolean DEBUG = MainActivity.DEBUG;
private final int maxVolume;
2021-01-08 18:35:33 +01:00
public PlayerGestureListener(final Player player, final MainPlayer service) {
super(player, service);
maxVolume = player.getAudioReactor().getMaxVolume();
}
@Override
public void onDoubleTap(@NonNull final MotionEvent event,
@NonNull 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 = ["
2021-01-08 18:35:33 +01:00
+ player.getPlayerType() + "], portion = [" + portion + "]");
2020-07-14 19:21:32 +02:00
}
2021-01-08 18:35:33 +01:00
if (player.isSomePopupMenuVisible()) {
player.hideControls(0, 0);
2020-07-14 19:21:32 +02:00
}
if (portion == DisplayPortion.LEFT || portion == DisplayPortion.RIGHT) {
startMultiDoubleTap(event);
} else if (portion == DisplayPortion.MIDDLE) {
2021-01-08 18:35:33 +01:00
player.playPause();
2020-07-14 19:21:32 +02:00
}
}
2020-07-14 19:21:32 +02:00
@Override
public void onSingleTap(@NonNull final MainPlayer.PlayerType playerType) {
2020-07-14 19:21:32 +02:00
if (DEBUG) {
2021-01-08 18:35:33 +01:00
Log.d(TAG, "onSingleTap called with playerType = [" + player.getPlayerType() + "]");
2020-07-14 19:21:32 +02:00
}
if (player.isControlsVisible()) {
player.hideControls(150, 0);
return;
}
// -- Controls are not visible --
// When player is completed show controls and don't hide them later
if (player.getCurrentState() == Player.STATE_COMPLETED) {
player.showControls(0);
} else {
player.showControlsThenHide();
2020-07-14 19:21:32 +02:00
}
}
@Override
public void onScroll(@NonNull final MainPlayer.PlayerType playerType,
@NonNull final DisplayPortion portion,
@NonNull final MotionEvent initialEvent,
@NonNull final MotionEvent movingEvent,
2020-10-21 16:40:22 +02:00
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 = ["
2021-01-08 18:35:33 +01:00
+ player.getPlayerType() + "], portion = [" + portion + "]");
2020-07-14 19:21:32 +02:00
}
2020-10-21 16:40:22 +02:00
if (playerType == MainPlayer.PlayerType.VIDEO) {
// -- Brightness and Volume control --
final boolean isBrightnessGestureEnabled =
PlayerHelper.isBrightnessGestureEnabled(service);
final boolean isVolumeGestureEnabled = PlayerHelper.isVolumeGestureEnabled(service);
if (isBrightnessGestureEnabled && isVolumeGestureEnabled) {
if (portion == DisplayPortion.LEFT_HALF) {
onScrollMainBrightness(distanceX, distanceY);
} else /* DisplayPortion.RIGHT_HALF */ {
onScrollMainVolume(distanceX, distanceY);
}
} else if (isBrightnessGestureEnabled) {
onScrollMainBrightness(distanceX, distanceY);
} else if (isVolumeGestureEnabled) {
onScrollMainVolume(distanceX, distanceY);
2020-10-21 16:40:22 +02:00
}
2020-10-21 16:40:22 +02:00
} else /* MainPlayer.PlayerType.POPUP */ {
// -- Determine if the ClosingOverlayView (red X) has to be shown or hidden --
2021-01-08 18:35:33 +01:00
final View closingOverlayView = player.getClosingOverlayView();
final boolean showClosingOverlayView = player.isInsideClosingRadius(movingEvent);
// Check if an view is in expected state and if not animate it into the correct state
final int expectedVisibility = showClosingOverlayView ? View.VISIBLE : View.GONE;
if (closingOverlayView.getVisibility() != expectedVisibility) {
animate(closingOverlayView, showClosingOverlayView, 200);
}
}
}
2020-10-21 16:40:22 +02:00
private void onScrollMainVolume(final float distanceX, final float distanceY) {
// If we just started sliding, change the progress bar to match the system volume
if (player.getVolumeRelativeLayout().getVisibility() != View.VISIBLE) {
final float volumePercent = player
.getAudioReactor().getVolume() / (float) maxVolume;
player.getVolumeProgressBar().setProgress(
(int) (volumePercent * player.getMaxGestureLength()));
}
2021-01-08 18:35:33 +01:00
player.getVolumeProgressBar().incrementProgressBy((int) distanceY);
final float currentProgressPercent = (float) player
.getVolumeProgressBar().getProgress() / player.getMaxGestureLength();
final int currentVolume = (int) (maxVolume * currentProgressPercent);
2021-01-08 18:35:33 +01:00
player.getAudioReactor().setVolume(currentVolume);
if (DEBUG) {
Log.d(TAG, "onScroll().volumeControl, currentVolume = " + currentVolume);
}
2021-01-08 18:35:33 +01:00
player.getVolumeImageView().setImageDrawable(
AppCompatResources.getDrawable(service, currentProgressPercent <= 0
2021-03-27 15:45:49 +01:00
? R.drawable.ic_volume_off
: currentProgressPercent < 0.25 ? R.drawable.ic_volume_mute
: currentProgressPercent < 0.75 ? R.drawable.ic_volume_down
: R.drawable.ic_volume_up)
);
2021-01-08 18:35:33 +01:00
if (player.getVolumeRelativeLayout().getVisibility() != View.VISIBLE) {
animate(player.getVolumeRelativeLayout(), true, 200, SCALE_AND_ALPHA);
}
2021-01-08 18:35:33 +01:00
if (player.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) {
player.getBrightnessRelativeLayout().setVisibility(View.GONE);
2020-10-21 16:40:22 +02:00
}
}
private void onScrollMainBrightness(final float distanceX, final float distanceY) {
2021-01-08 18:35:33 +01:00
final Activity parent = player.getParentActivity();
if (parent == null) {
return;
}
final Window window = parent.getWindow();
final WindowManager.LayoutParams layoutParams = window.getAttributes();
2021-01-08 18:35:33 +01:00
final ProgressBar bar = player.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);
if (DEBUG) {
Log.d(TAG, "onScroll().brightnessControl, "
+ "currentBrightness = " + currentProgressPercent);
}
2021-01-08 18:35:33 +01:00
player.getBrightnessImageView().setImageDrawable(
AppCompatResources.getDrawable(service,
currentProgressPercent < 0.25
2021-03-27 15:45:49 +01:00
? R.drawable.ic_brightness_low
: currentProgressPercent < 0.75
2021-03-27 15:45:49 +01:00
? R.drawable.ic_brightness_medium
: R.drawable.ic_brightness_high)
);
2021-01-08 18:35:33 +01:00
if (player.getBrightnessRelativeLayout().getVisibility() != View.VISIBLE) {
animate(player.getBrightnessRelativeLayout(), true, 200, SCALE_AND_ALPHA);
}
2021-01-08 18:35:33 +01:00
if (player.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) {
player.getVolumeRelativeLayout().setVisibility(View.GONE);
}
}
2020-10-21 16:40:22 +02:00
@Override
public void onScrollEnd(@NonNull final MainPlayer.PlayerType playerType,
@NonNull 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 = ["
2021-01-08 18:35:33 +01:00
+ player.getPlayerType() + "]");
2020-07-14 19:21:32 +02:00
}
2021-11-05 18:07:21 +01:00
if (player.isControlsVisible() && player.getCurrentState() == STATE_PLAYING) {
player.hideControls(DEFAULT_CONTROLS_DURATION, DEFAULT_CONTROLS_HIDE_TIME);
}
2020-10-21 16:40:22 +02:00
if (playerType == MainPlayer.PlayerType.VIDEO) {
2021-01-08 18:35:33 +01:00
if (player.getVolumeRelativeLayout().getVisibility() == View.VISIBLE) {
animate(player.getVolumeRelativeLayout(), false, 200, SCALE_AND_ALPHA,
200);
2020-07-14 19:21:32 +02:00
}
2021-01-08 18:35:33 +01:00
if (player.getBrightnessRelativeLayout().getVisibility() == View.VISIBLE) {
animate(player.getBrightnessRelativeLayout(), false, 200, SCALE_AND_ALPHA,
200);
2020-07-14 19:21:32 +02:00
}
2021-11-05 18:06:10 +01:00
} else /* Popup-Player */ {
2021-01-08 18:35:33 +01:00
if (player.isInsideClosingRadius(event)) {
player.closePopup();
2021-01-19 09:33:47 +01:00
} else if (!player.isPopupClosing()) {
animate(player.getCloseOverlayButton(), false, 200);
animate(player.getClosingOverlayView(), false, 200);
}
}
}
2020-10-21 16:40:22 +02:00
@Override
public void onPopupResizingStart() {
if (DEBUG) {
Log.d(TAG, "onPopupResizingStart called");
}
2021-01-08 18:35:33 +01:00
player.getLoadingPanel().setVisibility(View.GONE);
2021-01-08 18:35:33 +01:00
player.hideControls(0, 0);
animate(player.getFastSeekOverlay(), false, 0);
animate(player.getCurrentDisplaySeek(), false, 0, ALPHA, 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
}
}
}