cpp-140: Long click eraser is triggered when several buttons are clicked

It turned out that onLongPress method call might be postponed and thus ACTION_UP
happens before eraser.start is called. Now with new property this case
should be handled

Fixes #140
This commit is contained in:
serso 2015-02-17 12:15:14 +01:00
parent 6b9614b402
commit bced8f138f

View File

@ -1,6 +1,5 @@
package org.solovyev.android.calculator.view; package org.solovyev.android.calculator.view;
import android.text.TextUtils;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
@ -39,7 +38,9 @@ public final class LongClickEraser implements View.OnTouchListener {
this.view = view; this.view = view;
this.gestureDetector = new GestureDetector(view.getContext(), new GestureDetector.SimpleOnGestureListener() { this.gestureDetector = new GestureDetector(view.getContext(), new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) { public void onLongPress(MotionEvent e) {
eraser.start(); if (eraser.isTracking()) {
eraser.start();
}
} }
}); });
} }
@ -49,9 +50,10 @@ public final class LongClickEraser implements View.OnTouchListener {
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
eraser.stop(); eraser.stopTracking();
break; break;
default: default:
eraser.startTracking();
gestureDetector.onTouchEvent(event); gestureDetector.onTouchEvent(event);
break; break;
} }
@ -63,6 +65,7 @@ public final class LongClickEraser implements View.OnTouchListener {
private long delay; private long delay;
private boolean wasCalculatingOnFly; private boolean wasCalculatingOnFly;
private boolean erasing; private boolean erasing;
private boolean tracking = true;
@Override @Override
public void run() { public void run() {
@ -100,5 +103,18 @@ public final class LongClickEraser implements View.OnTouchListener {
calculator.setCalculateOnFly(true); calculator.setCalculateOnFly(true);
} }
} }
public void stopTracking() {
stop();
tracking = false;
}
public boolean isTracking() {
return tracking;
}
public void startTracking() {
tracking = true;
}
} }
} }