From d409ec902dce9611631e4a042394a3ef2a1e3b18 Mon Sep 17 00:00:00 2001 From: Ivan Date: Thu, 13 Jan 2022 22:19:42 +0400 Subject: [PATCH] Do not handle click if finger is lifted outside view; Do not handle click if finger is drag inside the view; --- .../android/exoplayer2/ui/PlayerView.java | 36 +++++++++++-------- .../exoplayer2/ui/StyledPlayerView.java | 35 ++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java index 25557af745f..c6fc1b5594c 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java @@ -31,6 +31,7 @@ import android.opengl.GLSurfaceView; import android.os.Looper; import android.util.AttributeSet; +import android.view.GestureDetector; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -45,6 +46,7 @@ import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.content.ContextCompat; +import androidx.core.view.GestureDetectorCompat; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.MediaMetadata; import com.google.android.exoplayer2.PlaybackException; @@ -280,6 +282,7 @@ public class PlayerView extends FrameLayout implements AdViewProvider { private static final int SURFACE_TYPE_VIDEO_DECODER_GL_SURFACE_VIEW = 4; private final ComponentListener componentListener; + private final GestureDetectorCompat gestureDetector; @Nullable private final AspectRatioFrameLayout contentFrame; @Nullable private final View shutterView; @Nullable private final View surfaceView; @@ -306,7 +309,6 @@ public class PlayerView extends FrameLayout implements AdViewProvider { private boolean controllerHideDuringAds; private boolean controllerHideOnTouch; private int textureViewRotation; - private boolean isTouching; private static final int PICTURE_TYPE_FRONT_COVER = 3; private static final int PICTURE_TYPE_NOT_SET = -1; @@ -323,6 +325,21 @@ public PlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAtt super(context, attrs, defStyleAttr); componentListener = new ComponentListener(); + gestureDetector = new GestureDetectorCompat( + getContext(), + new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + performClick(); + return true; + } + } + ); if (isInEditMode()) { contentFrame = null; @@ -1068,25 +1085,14 @@ public SubtitleView getSubtitleView() { return subtitleView; } + // performClick handled by GestureDetector + @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { if (!useController() || player == null) { return false; } - switch (event.getAction()) { - case MotionEvent.ACTION_DOWN: - isTouching = true; - return true; - case MotionEvent.ACTION_UP: - if (isTouching) { - isTouching = false; - performClick(); - return true; - } - return false; - default: - return false; - } + return gestureDetector.onTouchEvent(event); } @Override diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java index 4090e1f962f..411f626500c 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java @@ -32,6 +32,7 @@ import android.opengl.GLSurfaceView; import android.os.Looper; import android.util.AttributeSet; +import android.view.GestureDetector; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -46,6 +47,7 @@ import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.core.content.ContextCompat; +import androidx.core.view.GestureDetectorCompat; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.MediaMetadata; import com.google.android.exoplayer2.PlaybackException; @@ -281,6 +283,7 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider { private static final int SURFACE_TYPE_VIDEO_DECODER_GL_SURFACE_VIEW = 4; private final ComponentListener componentListener; + private final GestureDetectorCompat gestureDetector; @Nullable private final AspectRatioFrameLayout contentFrame; @Nullable private final View shutterView; @Nullable private final View surfaceView; @@ -307,7 +310,6 @@ public class StyledPlayerView extends FrameLayout implements AdViewProvider { private boolean controllerHideDuringAds; private boolean controllerHideOnTouch; private int textureViewRotation; - private boolean isTouching; private static final int PICTURE_TYPE_FRONT_COVER = 3; private static final int PICTURE_TYPE_NOT_SET = -1; @@ -324,6 +326,21 @@ public StyledPlayerView(Context context, @Nullable AttributeSet attrs, int defSt super(context, attrs, defStyleAttr); componentListener = new ComponentListener(); + gestureDetector = new GestureDetectorCompat( + getContext(), + new GestureDetector.SimpleOnGestureListener() { + @Override + public boolean onDown(MotionEvent e) { + return true; + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + performClick(); + return true; + } + } + ); if (isInEditMode()) { contentFrame = null; @@ -1105,24 +1122,14 @@ public SubtitleView getSubtitleView() { return subtitleView; } + // performClick handled by GestureDetector + @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event) { if (!useController() || player == null) { return false; } - switch (event.getAction()) { - case MotionEvent.ACTION_DOWN: - isTouching = true; - return true; - case MotionEvent.ACTION_UP: - if (isTouching) { - isTouching = false; - return performClick(); - } - return false; - default: - return false; - } + return gestureDetector.onTouchEvent(event); } @Override