From 2e1f89025ff9348a2b7f06aea896f35620093485 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sat, 12 Mar 2022 21:05:59 +0100 Subject: [PATCH] Android: Only use getActionIndex for ACTION_POINTER_DOWN/ACTION_POINTER_UP According to the documentation, getActionIndex should only be used with ACTION_POINTER_DOWN and ACTION_POINTER_UP. We've had a few crashes reported in the Play Console regarding invalid pointer indices for getY, and I'm hoping this will help with that. --- .../dolphinemu/overlay/InputOverlay.java | 7 ++++-- .../overlay/InputOverlayDrawableButton.java | 15 +++++-------- .../overlay/InputOverlayDrawableDpad.java | 15 +++++-------- .../overlay/InputOverlayDrawableJoystick.java | 22 +++++++++---------- .../overlay/InputOverlayPointer.java | 7 ++++-- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlay.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlay.java index c627603900..a614f21a64 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlay.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlay.java @@ -198,14 +198,17 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener return onTouchWhileEditing(event); } - int pointerIndex = event.getActionIndex(); + int action = event.getActionMasked(); + boolean firstPointer = action != MotionEvent.ACTION_POINTER_DOWN && + action != MotionEvent.ACTION_POINTER_UP; + int pointerIndex = firstPointer ? 0 : event.getActionIndex(); // Tracks if any button/joystick is pressed down boolean pressed = false; for (InputOverlayDrawableButton button : overlayButtons) { // Determine the button state to apply based on the MotionEvent action flag. - switch (event.getAction() & MotionEvent.ACTION_MASK) + switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java index 94c2633c5d..83e97cc31a 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableButton.java @@ -71,22 +71,19 @@ public final class InputOverlayDrawableButton public void onConfigureTouch(MotionEvent event) { - int pointerIndex = event.getActionIndex(); - int fingerPositionX = (int) event.getX(pointerIndex); - int fingerPositionY = (int) event.getY(pointerIndex); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: - mControlPositionX += fingerPositionX - mPreviousTouchX; - mControlPositionY += fingerPositionY - mPreviousTouchY; + mControlPositionX += (int) event.getX() - mPreviousTouchX; + mControlPositionY += (int) event.getY() - mPreviousTouchY; setBounds(mControlPositionX, mControlPositionY, getWidth() + mControlPositionX, getHeight() + mControlPositionY); - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java index ddc7ffd503..755ed7e2ff 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableDpad.java @@ -149,22 +149,19 @@ public final class InputOverlayDrawableDpad public void onConfigureTouch(MotionEvent event) { - int pointerIndex = event.getActionIndex(); - int fingerPositionX = (int) event.getX(pointerIndex); - int fingerPositionY = (int) event.getY(pointerIndex); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: - mControlPositionX += fingerPositionX - mPreviousTouchX; - mControlPositionY += fingerPositionY - mPreviousTouchY; + mControlPositionX += (int) event.getX() - mPreviousTouchX; + mControlPositionY += (int) event.getY() - mPreviousTouchY; setBounds(mControlPositionX, mControlPositionY, getWidth() + mControlPositionX, getHeight() + mControlPositionY); - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableJoystick.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableJoystick.java index b4b933f991..0e4e1f33f5 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableJoystick.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayDrawableJoystick.java @@ -95,10 +95,13 @@ public final class InputOverlayDrawableJoystick public boolean TrackEvent(MotionEvent event) { boolean reCenter = BooleanSetting.MAIN_JOYSTICK_REL_CENTER.getBooleanGlobal(); - int pointerIndex = event.getActionIndex(); + int action = event.getActionMasked(); + boolean firstPointer = action != MotionEvent.ACTION_POINTER_DOWN && + action != MotionEvent.ACTION_POINTER_UP; + int pointerIndex = firstPointer ? 0 : event.getActionIndex(); boolean pressed = false; - switch (event.getAction() & MotionEvent.ACTION_MASK) + switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: @@ -163,18 +166,15 @@ public final class InputOverlayDrawableJoystick public void onConfigureTouch(MotionEvent event) { - int pointerIndex = event.getActionIndex(); - int fingerPositionX = (int) event.getX(pointerIndex); - int fingerPositionY = (int) event.getY(pointerIndex); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: - int deltaX = fingerPositionX - mPreviousTouchX; - int deltaY = fingerPositionY - mPreviousTouchY; + int deltaX = (int) event.getX() - mPreviousTouchX; + int deltaY = (int) event.getY() - mPreviousTouchY; mControlPositionX += deltaX; mControlPositionY += deltaY; setBounds(new Rect(mControlPositionX, mControlPositionY, @@ -187,8 +187,8 @@ public final class InputOverlayDrawableJoystick setOrigBounds(new Rect(new Rect(mControlPositionX, mControlPositionY, mOuterBitmap.getIntrinsicWidth() + mControlPositionX, mOuterBitmap.getIntrinsicHeight() + mControlPositionY))); - mPreviousTouchX = fingerPositionX; - mPreviousTouchY = fingerPositionY; + mPreviousTouchX = (int) event.getX(); + mPreviousTouchY = (int) event.getY(); break; } } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayPointer.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayPointer.java index c592dbfcd0..67f8d23419 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayPointer.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/overlay/InputOverlayPointer.java @@ -82,9 +82,12 @@ public class InputOverlayPointer public void onTouch(MotionEvent event) { - int pointerIndex = event.getActionIndex(); + int action = event.getActionMasked(); + boolean firstPointer = action != MotionEvent.ACTION_POINTER_DOWN && + action != MotionEvent.ACTION_POINTER_UP; + int pointerIndex = firstPointer ? 0 : event.getActionIndex(); - switch (event.getAction() & MotionEvent.ACTION_MASK) + switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: