From 347566c311f9e215b48baa20ab3c10f53525f809 Mon Sep 17 00:00:00 2001 From: vkay94 Date: Wed, 21 Oct 2020 16:41:14 +0200 Subject: [PATCH] Player gestures: Add multi-double-tap logic --- .../player/event/BasePlayerGestureListener.kt | 69 +++++++++++++++++++ .../newpipe/player/event/DoubleTapListener.kt | 7 ++ .../player/event/PlayerGestureListener.java | 2 + 3 files changed, 78 insertions(+) create mode 100644 app/src/main/java/org/schabi/newpipe/player/event/DoubleTapListener.kt diff --git a/app/src/main/java/org/schabi/newpipe/player/event/BasePlayerGestureListener.kt b/app/src/main/java/org/schabi/newpipe/player/event/BasePlayerGestureListener.kt index 8ce140443..dcc1bb128 100644 --- a/app/src/main/java/org/schabi/newpipe/player/event/BasePlayerGestureListener.kt +++ b/app/src/main/java/org/schabi/newpipe/player/event/BasePlayerGestureListener.kt @@ -1,6 +1,7 @@ package org.schabi.newpipe.player.event import android.content.Context +import android.os.Handler import android.util.Log import android.view.GestureDetector import android.view.MotionEvent @@ -203,6 +204,11 @@ abstract class BasePlayerGestureListener( if (DEBUG) Log.d(TAG, "onDown called with e = [$e]") + if (isDoubleTapping && isDoubleTapEnabled) { + doubleTapControls?.onDoubleTapProgressDown(getDisplayPortion(e)) + return true + } + return if (playerImpl.popupPlayerSelected()) onDownInPopup(e) else @@ -233,6 +239,9 @@ abstract class BasePlayerGestureListener( if (DEBUG) Log.d(TAG, "onSingleTapConfirmed() called with: e = [$e]") + if (isDoubleTapping) + return true + if (playerImpl.popupPlayerSelected()) { if (playerImpl.player == null) return false @@ -374,6 +383,65 @@ abstract class BasePlayerGestureListener( return true } + // /////////////////////////////////////////////////////////////////// + // Multi double tapping + // /////////////////////////////////////////////////////////////////// + + var doubleTapControls: DoubleTapListener? = null + private set + + val isDoubleTapEnabled: Boolean + get() = doubleTapDelay > 0 + + var isDoubleTapping = false + private set + + fun doubleTapControls(listener: DoubleTapListener) = apply { + doubleTapControls = listener + } + + private var doubleTapDelay = DOUBLE_TAP_DELAY + private val doubleTapHandler: Handler = Handler() + private val doubleTapRunnable = Runnable { + if (DEBUG) + Log.d(TAG, "doubleTapRunnable called") + + isDoubleTapping = false + doubleTapControls?.onDoubleTapFinished() + } + + fun startMultiDoubleTap(e: MotionEvent) { + if (!isDoubleTapping) { + if (DEBUG) + Log.d(TAG, "startMultiDoubleTap called with e = [$e]") + + keepInDoubleTapMode() + doubleTapControls?.onDoubleTapStarted(getDisplayPortion(e)) + } + } + + fun keepInDoubleTapMode() { + if (DEBUG) + Log.d(TAG, "keepInDoubleTapMode called") + + isDoubleTapping = true + doubleTapHandler.removeCallbacks(doubleTapRunnable) + doubleTapHandler.postDelayed(doubleTapRunnable, doubleTapDelay) + } + + fun endMultiDoubleTap() { + if (DEBUG) + Log.d(TAG, "endMultiDoubleTap called") + + isDoubleTapping = false + doubleTapHandler.removeCallbacks(doubleTapRunnable) + doubleTapControls?.onDoubleTapFinished() + } + + fun enableMultiDoubleTap(enable: Boolean) = apply { + doubleTapDelay = if (enable) DOUBLE_TAP_DELAY else 0 + } + // /////////////////////////////////////////////////////////////////// // Utils // /////////////////////////////////////////////////////////////////// @@ -429,6 +497,7 @@ abstract class BasePlayerGestureListener( private const val TAG = "BasePlayerGestListener" private val DEBUG = BasePlayer.DEBUG + private const val DOUBLE_TAP_DELAY = 550L private const val MOVEMENT_THRESHOLD = 40 } } diff --git a/app/src/main/java/org/schabi/newpipe/player/event/DoubleTapListener.kt b/app/src/main/java/org/schabi/newpipe/player/event/DoubleTapListener.kt new file mode 100644 index 000000000..84cfb9b8d --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/event/DoubleTapListener.kt @@ -0,0 +1,7 @@ +package org.schabi.newpipe.player.event + +interface DoubleTapListener { + fun onDoubleTapStarted(portion: DisplayPortion) {} + fun onDoubleTapProgressDown(portion: DisplayPortion) {} + fun onDoubleTapFinished() {} +} diff --git a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java index 003b9ff9d..a23d5d3ff 100644 --- a/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java +++ b/app/src/main/java/org/schabi/newpipe/player/event/PlayerGestureListener.java @@ -62,6 +62,8 @@ public class PlayerGestureListener if (portion == DisplayPortion.LEFT) { playerImpl.onFastRewind(); + } else if (portion == DisplayPortion.MIDDLE) { + playerImpl.onPlayPause(); } else if (portion == DisplayPortion.RIGHT) { playerImpl.onFastForward(); }