From aa22bc2dbeb01fbe3a9905adc8e87a859a3f3dca Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 7 Mar 2022 20:54:36 +0000 Subject: [PATCH] Fix PlayerView touch handling Overriding onTouchEvent was causing multiple issues, and appears to be unnecessary. Removing the override fixes: 1. StyledPlayerView accessibility issue where "hide player controls" actually toggled play/pause. 2. Delivery of events to a registered OnClickListener when useController is false. 3. Delivery of events to a registered OnLongClickListener in all configurations. 4. Incorrectly treating a sequence of touch events that exit the bounds of the view before ACTION_UP as a click, both for delivery to OnClickListener and for toggling the controls. Note: After this change, control visibility will not be toggled if the application developer explicitly sets the view to be non-clickable. I think that's probably working as intended though. It seems correct that a non-clickable view would not respond to clicks. Issue: google/ExoPlayer#8627 Issue: google/ExoPlayer#9605 Issue: google/ExoPlayer#9861 PiperOrigin-RevId: 433016626 --- .../java/androidx/media3/ui/PlayerView.java | 32 +++---------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java b/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java index be9ed95f61..269b697436 100644 --- a/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java +++ b/libraries/ui/src/main/java/androidx/media3/ui/PlayerView.java @@ -311,6 +311,7 @@ public class PlayerView extends FrameLayout implements AdViewProvider { LayoutInflater.from(context).inflate(playerLayoutId, this); setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); + setClickable(true); // Content frame. contentFrame = findViewById(R.id.exo_content_frame); @@ -1015,30 +1016,10 @@ public class PlayerView extends FrameLayout implements AdViewProvider { return subtitleView; } - @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; - } - } - @Override public boolean performClick() { - super.performClick(); - return toggleControllerVisibility(); + toggleControllerVisibility(); + return super.performClick(); } @Override @@ -1134,18 +1115,15 @@ public class PlayerView extends FrameLayout implements AdViewProvider { return false; } - private boolean toggleControllerVisibility() { + private void toggleControllerVisibility() { if (!useController() || player == null) { - return false; + return; } if (!controller.isFullyVisible()) { maybeShowController(true); - return true; } else if (controllerHideOnTouch) { controller.hide(); - return true; } - return false; } /** Shows the playback controls, but only if forced or shown indefinitely. */