From b125dc5599468a09d82751cd76152071ae485afb Mon Sep 17 00:00:00 2001 From: Dianne Hackborn Date: Fri, 12 Feb 2010 15:52:09 -0800 Subject: [PATCH] Fix MotionEvent pointer API. Ooops. The API said that the pointer down and up actions contained the pointer id, but it is actually the index. Actually it makes much more sense for it to be the index, and those ACTION_POINTER_1_DOWN etc. constants were stupid. --- api/current.xml | 58 +++++++-- .../common/ui/PointerLocationView.java | 22 ++-- core/java/android/view/GestureDetector.java | 8 +- core/java/android/view/MotionEvent.java | 112 ++++++++++++------ .../java/com/android/server/InputDevice.java | 4 +- 5 files changed, 149 insertions(+), 55 deletions(-) diff --git a/api/current.xml b/api/current.xml index be1473131549d..9cdd2f03f788a 100644 --- a/api/current.xml +++ b/api/current.xml @@ -168648,6 +168648,28 @@ visibility="public" > + + + + @@ -169279,7 +169301,7 @@ value="6" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > @@ -169290,7 +169312,7 @@ value="261" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > @@ -169301,7 +169323,7 @@ value="262" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > @@ -169312,7 +169334,7 @@ value="517" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > @@ -169323,7 +169345,7 @@ value="518" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > @@ -169345,11 +169367,33 @@ value="65280" static="true" final="true" - deprecated="not deprecated" + deprecated="deprecated" visibility="public" > + + + +> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int index = (action&MotionEvent.ACTION_POINTER_INDEX_MASK) + >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; + final int id = event.getPointerId(index); while (NP <= id) { PointerState ps = new PointerState(); ps.mVelocity = VelocityTracker.obtain(); @@ -260,13 +261,14 @@ public class PointerLocationView extends View { } for (int i=0; i> MotionEvent.ACTION_POINTER_ID_SHIFT) == 0) ? 1 : 0; - mLastMotionX = ev.getX(id); - mLastMotionY = ev.getY(id); + int index = (((action & MotionEvent.ACTION_POINTER_INDEX_MASK) + >> MotionEvent.ACTION_POINTER_INDEX_SHIFT) == 0) ? 1 : 0; + mLastMotionX = ev.getX(index); + mLastMotionY = ev.getY(index); mVelocityTracker.recycle(); mVelocityTracker = VelocityTracker.obtain(); } diff --git a/core/java/android/view/MotionEvent.java b/core/java/android/view/MotionEvent.java index ca907af108dc2..d648e96693c78 100644 --- a/core/java/android/view/MotionEvent.java +++ b/core/java/android/view/MotionEvent.java @@ -75,24 +75,6 @@ public final class MotionEvent implements Parcelable { */ public static final int ACTION_POINTER_DOWN = 5; - /** - * Synonym for {@link #ACTION_POINTER_DOWN} with - * {@link #ACTION_POINTER_ID_MASK} of 0: the primary pointer has gone done. - */ - public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000; - - /** - * Synonym for {@link #ACTION_POINTER_DOWN} with - * {@link #ACTION_POINTER_ID_MASK} of 1: the secondary pointer has gone done. - */ - public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100; - - /** - * Synonym for {@link #ACTION_POINTER_DOWN} with - * {@link #ACTION_POINTER_ID_MASK} of 2: the tertiary pointer has gone done. - */ - public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200; - /** * A non-primary pointer has gone up. The bits in * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed. @@ -100,37 +82,75 @@ public final class MotionEvent implements Parcelable { public static final int ACTION_POINTER_UP = 6; /** - * Synonym for {@link #ACTION_POINTER_UP} with - * {@link #ACTION_POINTER_ID_MASK} of 0: the primary pointer has gone up. + * Bits in the action code that represent a pointer index, used with + * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Shifting + * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer + * index where the data for the pointer going up or down can be found; you can + * get its identifier with {@link #getPointerId(int)} and the actual + * data with {@link #getX(int)} etc. */ + public static final int ACTION_POINTER_INDEX_MASK = 0xff00; + + /** + * Bit shift for the action bits holding the pointer index as + * defined by {@link #ACTION_POINTER_INDEX_MASK}. + */ + public static final int ACTION_POINTER_INDEX_SHIFT = 8; + + /** + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_DOWN}. + */ + @Deprecated + public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000; + + /** + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_DOWN}. + */ + @Deprecated + public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100; + + /** + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_DOWN}. + */ + @Deprecated + public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200; + + /** + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_UP}. + */ + @Deprecated public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000; /** - * Synonym for {@link #ACTION_POINTER_UP} with - * {@link #ACTION_POINTER_ID_MASK} of 1: the secondary pointer has gone up. + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_UP}. */ + @Deprecated public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100; /** - * Synonym for {@link #ACTION_POINTER_UP} with - * {@link #ACTION_POINTER_ID_MASK} of 2: the tertiary pointer has gone up. + * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the + * data index associated with {@link #ACTION_POINTER_UP}. */ + @Deprecated public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200; /** - * Bits in the action code that represent a pointer ID, used with - * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Pointer IDs - * start at 0, with 0 being the primary (first) pointer in the motion. Note - * that this not not an index into the array of pointer values, - * which is compacted to only contain pointers that are down; the pointer - * ID for a particular index can be found with {@link #findPointerIndex}. + * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match + * the actual data contained in these bits. */ + @Deprecated public static final int ACTION_POINTER_ID_MASK = 0xff00; /** - * Bit shift for the action bits holding the pointer identifier as - * defined by {@link #ACTION_POINTER_ID_MASK}. + * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match + * the actual data contained in these bits. */ + @Deprecated public static final int ACTION_POINTER_ID_SHIFT = 8; private static final boolean TRACK_RECYCLED_LOCATION = false; @@ -618,12 +638,38 @@ public final class MotionEvent implements Parcelable { /** * Return the kind of action being performed -- one of either * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or - * {@link #ACTION_CANCEL}. + * {@link #ACTION_CANCEL}. Consider using {@link #getActionMasked} + * and {@link #getActionIndex} to retrieve the separate masked action + * and pointer index. */ public final int getAction() { return mAction; } + /** + * Return the masked action being performed, without pointer index + * information. May be any of the actions: {@link #ACTION_DOWN}, + * {@link #ACTION_MOVE}, {@link #ACTION_UP}, {@link #ACTION_CANCEL}, + * {@link #ACTION_POINTER_DOWN}, or {@link #ACTION_POINTER_UP}. + * Use {@link #getActionIndex} to return the index associated with + * pointer actions. + */ + public final int getActionMasked() { + return mAction & ACTION_MASK; + } + + /** + * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} + * as returned by {@link #getActionMasked}, this returns the associated + * pointer index. The index may be used with {@link #getPointerId(int)}, + * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)}, + * and {@link #getSize(int)} to get information about the pointer that has + * gone down or up. + */ + public final int getActionIndex() { + return (mAction & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT; + } + /** * Returns the time (in ms) when the user originally pressed down to start * a stream of position events. diff --git a/services/java/com/android/server/InputDevice.java b/services/java/com/android/server/InputDevice.java index 6f207e0ff8b93..d3bb6dc4a9d06 100644 --- a/services/java/com/android/server/InputDevice.java +++ b/services/java/com/android/server/InputDevice.java @@ -570,14 +570,14 @@ public class InputDevice { mDownTime = curTime; } else { action = MotionEvent.ACTION_POINTER_DOWN - | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT); + | (upOrDownPointer << MotionEvent.ACTION_POINTER_INDEX_SHIFT); } } else { if (numPointers == 1) { action = MotionEvent.ACTION_UP; } else { action = MotionEvent.ACTION_POINTER_UP - | (upOrDownPointer << MotionEvent.ACTION_POINTER_ID_SHIFT); + | (upOrDownPointer << MotionEvent.ACTION_POINTER_INDEX_SHIFT); } } currentMove = null;