Skip to content

Commit

Permalink
Do not handle click if finger is lifted outside view;
Browse files Browse the repository at this point in the history
Do not handle click if finger is drag inside the view;
  • Loading branch information
ivk1800 committed Jan 13, 2022
1 parent 15dc863 commit d409ec9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d409ec9

Please sign in to comment.