Skip to content

Commit

Permalink
Fix FingerTracker (#827)
Browse files Browse the repository at this point in the history
Handle ACTION_CANCEL event and add assertions in debug builds.
  • Loading branch information
equeim authored and QuantumBadger committed Jun 16, 2024
1 parent fad5c4c commit da8c011
Showing 1 changed file with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.quantumbadger.redreader.views.imageview;

import android.view.MotionEvent;

import org.quantumbadger.redreader.BuildConfig;
import org.quantumbadger.redreader.common.MutableFloatPoint2D;

public class FingerTracker {
Expand All @@ -43,11 +45,15 @@ public FingerTracker(final FingerListener mListener) {
}

public void onTouchEvent(final MotionEvent event) {

switch(event.getActionMasked()) {
final int action = event.getActionMasked();
switch(action) {

case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
// ACTION_DOWN starts the gesture, and all fingers must be up at this point
if (action == MotionEvent.ACTION_DOWN) {
assertThatAllFingersAreInactive("before ACTION_DOWN");
}

for(final Finger f : mFingers) {
if(!f.mActive) {
Expand Down Expand Up @@ -83,8 +89,35 @@ public void onTouchEvent(final MotionEvent event) {
break;
}
}
// ACTION_UP ends the gesture, and all fingers must be up at this point
if (action == MotionEvent.ACTION_UP) {
assertThatAllFingersAreInactive("after ACTION_UP");
}

break;

case MotionEvent.ACTION_CANCEL:
// ACTION_CANCEL ends the gesture, process all fingers
for(final Finger f : mFingers) {
if(f.mActive) {
f.onUp(event);
mListener.onFingerUp(f);
}
}

break;
}
}

private void assertThatAllFingersAreInactive(final String when) {
if (BuildConfig.DEBUG) {
for (final Finger f : mFingers) {
if (f.mActive) {
throw new IllegalStateException(
"Finger for pointer id " + f.mAndroidId + " is active " + when
);
}
}
}
}

Expand Down

0 comments on commit da8c011

Please sign in to comment.