summaryrefslogtreecommitdiffstats
path: root/core/java
diff options
context:
space:
mode:
authorAdam Powell <adamp@google.com>2010-02-01 15:03:17 -0800
committerAdam Powell <adamp@google.com>2010-02-02 17:52:29 -0800
commit216bccf804db9c972b317620a27de6a8adf7fbfe (patch)
tree229296e8cd22d3d8c4efbd55300d5b7c8c77339f /core/java
parent8c18d89f282d88ba80444f53771778ecf066874a (diff)
downloadframeworks_base-216bccf804db9c972b317620a27de6a8adf7fbfe.zip
frameworks_base-216bccf804db9c972b317620a27de6a8adf7fbfe.tar.gz
frameworks_base-216bccf804db9c972b317620a27de6a8adf7fbfe.tar.bz2
Updated GestureDetector to ignore multitouch if requested by the app
or if targetSdkVersion >= Froyo. Made ScaleGestureDetector public. current.xml API updates for gestures.
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/os/Build.java2
-rw-r--r--core/java/android/view/GestureDetector.java83
-rw-r--r--core/java/android/view/ScaleGestureDetector.java7
3 files changed, 71 insertions, 21 deletions
diff --git a/core/java/android/os/Build.java b/core/java/android/os/Build.java
index fcd8f38..4c46b1d 100644
--- a/core/java/android/os/Build.java
+++ b/core/java/android/os/Build.java
@@ -178,6 +178,8 @@ public class Build {
* January 2010: Android 2.1
*/
public static final int ECLAIR_MR1 = 7;
+
+ public static final int FROYO = 8;
}
/** The type of build, like "user" or "eng". */
diff --git a/core/java/android/view/GestureDetector.java b/core/java/android/view/GestureDetector.java
index 1e558be..3c79200 100644
--- a/core/java/android/view/GestureDetector.java
+++ b/core/java/android/view/GestureDetector.java
@@ -16,9 +16,10 @@
package android.view;
+import android.content.Context;
+import android.os.Build;
import android.os.Handler;
import android.os.Message;
-import android.content.Context;
/**
* Detects various gestures and events using the supplied {@link MotionEvent}s.
@@ -231,6 +232,13 @@ public class GestureDetector {
private float mLastMotionX;
private boolean mIsLongpressEnabled;
+
+ /**
+ * True if we are at a target API level of >= Froyo or the developer can
+ * explicitly set it. If true, input events with > 1 pointer will be ignored
+ * so we can work side by side with multitouch gesture detectors.
+ */
+ private boolean mIgnoreMultitouch;
/**
* Determines speed during touch scrolling
@@ -336,6 +344,26 @@ public class GestureDetector {
* @throws NullPointerException if {@code listener} is null.
*/
public GestureDetector(Context context, OnGestureListener listener, Handler handler) {
+ this(context, listener, handler, context != null &&
+ context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.FROYO);
+ }
+
+ /**
+ * Creates a GestureDetector with the supplied listener.
+ * You may only use this constructor from a UI thread (this is the usual situation).
+ * @see android.os.Handler#Handler()
+ *
+ * @param context the application's context
+ * @param listener the listener invoked for all the callbacks, this must
+ * not be null.
+ * @param handler the handler to use
+ * @param ignoreMultitouch whether events involving more than one pointer should
+ * be ignored.
+ *
+ * @throws NullPointerException if {@code listener} is null.
+ */
+ public GestureDetector(Context context, OnGestureListener listener, Handler handler,
+ boolean ignoreMultitouch) {
if (handler != null) {
mHandler = new GestureHandler(handler);
} else {
@@ -345,14 +373,15 @@ public class GestureDetector {
if (listener instanceof OnDoubleTapListener) {
setOnDoubleTapListener((OnDoubleTapListener) listener);
}
- init(context);
+ init(context, ignoreMultitouch);
}
- private void init(Context context) {
+ private void init(Context context, boolean ignoreMultitouch) {
if (mListener == null) {
throw new NullPointerException("OnGestureListener must not be null");
}
mIsLongpressEnabled = true;
+ mIgnoreMultitouch = ignoreMultitouch;
// Fallback to support pre-donuts releases
int touchSlop, doubleTapSlop;
@@ -425,7 +454,26 @@ public class GestureDetector {
boolean handled = false;
- switch (action) {
+ switch (action & MotionEvent.ACTION_MASK) {
+ case MotionEvent.ACTION_POINTER_DOWN:
+ if (mIgnoreMultitouch) {
+ // Multitouch event - abort.
+ cancel();
+ }
+ break;
+
+ case MotionEvent.ACTION_POINTER_UP:
+ // Ending a multitouch gesture and going back to 1 finger
+ if (mIgnoreMultitouch && ev.getPointerCount() == 2) {
+ int id = (((action & MotionEvent.ACTION_POINTER_ID_MASK)
+ >> MotionEvent.ACTION_POINTER_ID_SHIFT) == 0) ? 1 : 0;
+ mLastMotionX = ev.getX(id);
+ mLastMotionY = ev.getY(id);
+ mVelocityTracker.recycle();
+ mVelocityTracker = VelocityTracker.obtain();
+ }
+ break;
+
case MotionEvent.ACTION_DOWN:
if (mDoubleTapListener != null) {
boolean hadTapMessage = mHandler.hasMessages(TAP);
@@ -462,7 +510,7 @@ public class GestureDetector {
break;
case MotionEvent.ACTION_MOVE:
- if (mInLongPress) {
+ if (mInLongPress || (mIgnoreMultitouch && ev.getPointerCount() > 1)) {
break;
}
final float scrollX = mLastMotionX - x;
@@ -525,21 +573,24 @@ public class GestureDetector {
mHandler.removeMessages(LONG_PRESS);
break;
case MotionEvent.ACTION_CANCEL:
- mHandler.removeMessages(SHOW_PRESS);
- mHandler.removeMessages(LONG_PRESS);
- mHandler.removeMessages(TAP);
- mVelocityTracker.recycle();
- mVelocityTracker = null;
- mIsDoubleTapping = false;
- mStillDown = false;
- if (mInLongPress) {
- mInLongPress = false;
- break;
- }
+ cancel();
}
return handled;
}
+ private void cancel() {
+ mHandler.removeMessages(SHOW_PRESS);
+ mHandler.removeMessages(LONG_PRESS);
+ mHandler.removeMessages(TAP);
+ mVelocityTracker.recycle();
+ mVelocityTracker = null;
+ mIsDoubleTapping = false;
+ mStillDown = false;
+ if (mInLongPress) {
+ mInLongPress = false;
+ }
+ }
+
private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent firstUp,
MotionEvent secondDown) {
if (!mAlwaysInBiggerTapRegion) {
diff --git a/core/java/android/view/ScaleGestureDetector.java b/core/java/android/view/ScaleGestureDetector.java
index f991df7..94ce640 100644
--- a/core/java/android/view/ScaleGestureDetector.java
+++ b/core/java/android/view/ScaleGestureDetector.java
@@ -18,7 +18,6 @@ package android.view;
import android.content.Context;
import android.util.DisplayMetrics;
-import android.util.Log;
/**
* Detects transformation gestures involving more than one pointer ("multitouch")
@@ -34,7 +33,6 @@ import android.util.Log;
* {@link #onTouchEvent(MotionEvent)}. The methods defined in your
* callback will be executed when the events occur.
* </ul>
- * @hide Pending API approval
*/
public class ScaleGestureDetector {
/**
@@ -82,8 +80,7 @@ public class ScaleGestureDetector {
/**
* Responds to the end of a scale gesture. Reported by existing
- * pointers going up. If the end of a gesture would result in a fling,
- * {@link onTransformFling()} is called instead.
+ * pointers going up.
*
* Once a scale has ended, {@link ScaleGestureDetector#getFocusX()}
* and {@link ScaleGestureDetector#getFocusY()} will return the location
@@ -103,7 +100,7 @@ public class ScaleGestureDetector {
* {@link OnScaleGestureListener#onScaleBegin(ScaleGestureDetector)} return
* {@code true}.
*/
- public class SimpleOnScaleGestureListener implements OnScaleGestureListener {
+ public static class SimpleOnScaleGestureListener implements OnScaleGestureListener {
public boolean onScale(ScaleGestureDetector detector) {
return true;