MotionEvent.java revision 2f6d975b93bbc5b55c35a00d913ee38ad9b76401
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.os.SystemClock;
22
23/**
24 * Object used to report movement (mouse, pen, finger, trackball) events.  This
25 * class may hold either absolute or relative movements, depending on what
26 * it is being used for.
27 *
28 * Refer to {@link InputDevice} for information about how different kinds of
29 * input devices and sources represent pointer coordinates.
30 */
31public final class MotionEvent extends InputEvent implements Parcelable {
32    private static final long MS_PER_NS = 1000000;
33
34    /**
35     * Bit mask of the parts of the action code that are the action itself.
36     */
37    public static final int ACTION_MASK             = 0xff;
38
39    /**
40     * Constant for {@link #getAction}: A pressed gesture has started, the
41     * motion contains the initial starting location.
42     */
43    public static final int ACTION_DOWN             = 0;
44
45    /**
46     * Constant for {@link #getAction}: A pressed gesture has finished, the
47     * motion contains the final release location as well as any intermediate
48     * points since the last down or move event.
49     */
50    public static final int ACTION_UP               = 1;
51
52    /**
53     * Constant for {@link #getAction}: A change has happened during a
54     * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
55     * The motion contains the most recent point, as well as any intermediate
56     * points since the last down or move event.
57     */
58    public static final int ACTION_MOVE             = 2;
59
60    /**
61     * Constant for {@link #getAction}: The current gesture has been aborted.
62     * You will not receive any more points in it.  You should treat this as
63     * an up event, but not perform any action that you normally would.
64     */
65    public static final int ACTION_CANCEL           = 3;
66
67    /**
68     * Constant for {@link #getAction}: A movement has happened outside of the
69     * normal bounds of the UI element.  This does not provide a full gesture,
70     * but only the initial location of the movement/touch.
71     */
72    public static final int ACTION_OUTSIDE          = 4;
73
74    /**
75     * A non-primary pointer has gone down.  The bits in
76     * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
77     */
78    public static final int ACTION_POINTER_DOWN     = 5;
79
80    /**
81     * A non-primary pointer has gone up.  The bits in
82     * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
83     */
84    public static final int ACTION_POINTER_UP       = 6;
85
86    /**
87     * Bits in the action code that represent a pointer index, used with
88     * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}.  Shifting
89     * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer
90     * index where the data for the pointer going up or down can be found; you can
91     * get its identifier with {@link #getPointerId(int)} and the actual
92     * data with {@link #getX(int)} etc.
93     */
94    public static final int ACTION_POINTER_INDEX_MASK  = 0xff00;
95
96    /**
97     * Bit shift for the action bits holding the pointer index as
98     * defined by {@link #ACTION_POINTER_INDEX_MASK}.
99     */
100    public static final int ACTION_POINTER_INDEX_SHIFT = 8;
101
102    /**
103     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
104     * data index associated with {@link #ACTION_POINTER_DOWN}.
105     */
106    @Deprecated
107    public static final int ACTION_POINTER_1_DOWN   = ACTION_POINTER_DOWN | 0x0000;
108
109    /**
110     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
111     * data index associated with {@link #ACTION_POINTER_DOWN}.
112     */
113    @Deprecated
114    public static final int ACTION_POINTER_2_DOWN   = ACTION_POINTER_DOWN | 0x0100;
115
116    /**
117     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
118     * data index associated with {@link #ACTION_POINTER_DOWN}.
119     */
120    @Deprecated
121    public static final int ACTION_POINTER_3_DOWN   = ACTION_POINTER_DOWN | 0x0200;
122
123    /**
124     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
125     * data index associated with {@link #ACTION_POINTER_UP}.
126     */
127    @Deprecated
128    public static final int ACTION_POINTER_1_UP     = ACTION_POINTER_UP | 0x0000;
129
130    /**
131     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
132     * data index associated with {@link #ACTION_POINTER_UP}.
133     */
134    @Deprecated
135    public static final int ACTION_POINTER_2_UP     = ACTION_POINTER_UP | 0x0100;
136
137    /**
138     * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
139     * data index associated with {@link #ACTION_POINTER_UP}.
140     */
141    @Deprecated
142    public static final int ACTION_POINTER_3_UP     = ACTION_POINTER_UP | 0x0200;
143
144    /**
145     * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match
146     * the actual data contained in these bits.
147     */
148    @Deprecated
149    public static final int ACTION_POINTER_ID_MASK  = 0xff00;
150
151    /**
152     * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match
153     * the actual data contained in these bits.
154     */
155    @Deprecated
156    public static final int ACTION_POINTER_ID_SHIFT = 8;
157
158    private static final boolean TRACK_RECYCLED_LOCATION = false;
159
160    /**
161     * Flag indicating the motion event intersected the top edge of the screen.
162     */
163    public static final int EDGE_TOP = 0x00000001;
164
165    /**
166     * Flag indicating the motion event intersected the bottom edge of the screen.
167     */
168    public static final int EDGE_BOTTOM = 0x00000002;
169
170    /**
171     * Flag indicating the motion event intersected the left edge of the screen.
172     */
173    public static final int EDGE_LEFT = 0x00000004;
174
175    /**
176     * Flag indicating the motion event intersected the right edge of the screen.
177     */
178    public static final int EDGE_RIGHT = 0x00000008;
179
180    /*
181     * Offset for the sample's X coordinate.
182     */
183    static private final int SAMPLE_X = 0;
184
185    /*
186     * Offset for the sample's Y coordinate.
187     */
188    static private final int SAMPLE_Y = 1;
189
190    /*
191     * Offset for the sample's pressure.
192     */
193    static private final int SAMPLE_PRESSURE = 2;
194
195    /*
196     * Offset for the sample's size
197     */
198    static private final int SAMPLE_SIZE = 3;
199
200    /*
201     * Offset for the sample's touch major axis length.
202     */
203    static private final int SAMPLE_TOUCH_MAJOR = 4;
204
205    /*
206     * Offset for the sample's touch minor axis length.
207     */
208    static private final int SAMPLE_TOUCH_MINOR = 5;
209
210    /*
211     * Offset for the sample's tool major axis length.
212     */
213    static private final int SAMPLE_TOOL_MAJOR = 6;
214
215    /*
216     * Offset for the sample's tool minor axis length.
217     */
218    static private final int SAMPLE_TOOL_MINOR = 7;
219
220    /*
221     * Offset for the sample's orientation.
222     */
223    static private final int SAMPLE_ORIENTATION = 8;
224
225    /*
226     * Number of data items for each sample.
227     */
228    static private final int NUM_SAMPLE_DATA = 9;
229
230    /*
231     * Minimum number of pointers for which to reserve space when allocating new
232     * motion events.  This is explicitly not a bound on the maximum number of pointers.
233     */
234    static private final int BASE_AVAIL_POINTERS = 5;
235
236    /*
237     * Minimum number of samples for which to reserve space when allocating new motion events.
238     */
239    static private final int BASE_AVAIL_SAMPLES = 8;
240
241    static private final int MAX_RECYCLED = 10;
242    static private Object gRecyclerLock = new Object();
243    static private int gRecyclerUsed = 0;
244    static private MotionEvent gRecyclerTop = null;
245
246    private long mDownTimeNano;
247    private int mAction;
248    private float mXOffset;
249    private float mYOffset;
250    private float mXPrecision;
251    private float mYPrecision;
252    private int mEdgeFlags;
253    private int mMetaState;
254
255    private int mNumPointers;
256    private int mNumSamples;
257
258    private int mLastDataSampleIndex;
259    private int mLastEventTimeNanoSampleIndex;
260
261    // Array of mNumPointers size of identifiers for each pointer of data.
262    private int[] mPointerIdentifiers;
263
264    // Array of (mNumSamples * mNumPointers * NUM_SAMPLE_DATA) size of event data.
265    // Samples are ordered from oldest to newest.
266    private float[] mDataSamples;
267
268    // Array of mNumSamples size of event time stamps in nanoseconds.
269    // Samples are ordered from oldest to newest.
270    private long[] mEventTimeNanoSamples;
271
272    private MotionEvent mNext;
273    private RuntimeException mRecycledLocation;
274    private boolean mRecycled;
275
276    private MotionEvent(int pointerCount, int sampleCount) {
277        mPointerIdentifiers = new int[pointerCount];
278        mDataSamples = new float[pointerCount * sampleCount * NUM_SAMPLE_DATA];
279        mEventTimeNanoSamples = new long[sampleCount];
280    }
281
282    static private MotionEvent obtain(int pointerCount, int sampleCount) {
283        final MotionEvent ev;
284        synchronized (gRecyclerLock) {
285            if (gRecyclerTop == null) {
286                if (pointerCount < BASE_AVAIL_POINTERS) {
287                    pointerCount = BASE_AVAIL_POINTERS;
288                }
289                if (sampleCount < BASE_AVAIL_SAMPLES) {
290                    sampleCount = BASE_AVAIL_SAMPLES;
291                }
292                return new MotionEvent(pointerCount, sampleCount);
293            }
294            ev = gRecyclerTop;
295            gRecyclerTop = ev.mNext;
296            gRecyclerUsed -= 1;
297        }
298        ev.mRecycledLocation = null;
299        ev.mRecycled = false;
300        ev.mNext = null;
301
302        if (ev.mPointerIdentifiers.length < pointerCount) {
303            ev.mPointerIdentifiers = new int[pointerCount];
304        }
305
306        if (ev.mEventTimeNanoSamples.length < sampleCount) {
307            ev.mEventTimeNanoSamples = new long[sampleCount];
308        }
309
310        final int neededDataSamplesLength = pointerCount * sampleCount * NUM_SAMPLE_DATA;
311        if (ev.mDataSamples.length < neededDataSamplesLength) {
312            ev.mDataSamples = new float[neededDataSamplesLength];
313        }
314
315        return ev;
316    }
317
318    /**
319     * Create a new MotionEvent, filling in all of the basic values that
320     * define the motion.
321     *
322     * @param downTime The time (in ms) when the user originally pressed down to start
323     * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
324     * @param eventTime The the time (in ms) when this specific event was generated.  This
325     * must be obtained from {@link SystemClock#uptimeMillis()}.
326     * @param action The kind of action being performed -- one of either
327     * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
328     * {@link #ACTION_CANCEL}.
329     * @param pointers The number of points that will be in this event.
330     * @param pointerIds An array of <em>pointers</em> values providing
331     * an identifier for each pointer.
332     * @param pointerCoords An array of <em>pointers</em> values providing
333     * a {@link PointerCoords} coordinate object for each pointer.
334     * @param metaState The state of any meta / modifier keys that were in effect when
335     * the event was generated.
336     * @param xPrecision The precision of the X coordinate being reported.
337     * @param yPrecision The precision of the Y coordinate being reported.
338     * @param deviceId The id for the device that this event came from.  An id of
339     * zero indicates that the event didn't come from a physical device; other
340     * numbers are arbitrary and you shouldn't depend on the values.
341     * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
342     * MotionEvent.
343     * @param source The source of this event.
344     */
345    static public MotionEvent obtain(long downTime, long eventTime,
346            int action, int pointers, int[] pointerIds, PointerCoords[] pointerCoords,
347            int metaState, float xPrecision, float yPrecision, int deviceId,
348            int edgeFlags, int source) {
349        MotionEvent ev = obtain(pointers, 1);
350        ev.mDeviceId = deviceId;
351        ev.mSource = source;
352        ev.mEdgeFlags = edgeFlags;
353        ev.mDownTimeNano = downTime * MS_PER_NS;
354        ev.mAction = action;
355        ev.mMetaState = metaState;
356        ev.mXOffset = 0;
357        ev.mYOffset = 0;
358        ev.mXPrecision = xPrecision;
359        ev.mYPrecision = yPrecision;
360
361        ev.mNumPointers = pointers;
362        ev.mNumSamples = 1;
363
364        ev.mLastDataSampleIndex = 0;
365        ev.mLastEventTimeNanoSampleIndex = 0;
366
367        System.arraycopy(pointerIds, 0, ev.mPointerIdentifiers, 0, pointers);
368
369        ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS;
370
371        ev.setPointerCoordsAtSampleIndex(0, pointerCoords);
372
373        return ev;
374    }
375
376    /**
377     * Create a new MotionEvent, filling in all of the basic values that
378     * define the motion.
379     *
380     * @param downTime The time (in ms) when the user originally pressed down to start
381     * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
382     * @param eventTime  The the time (in ms) when this specific event was generated.  This
383     * must be obtained from {@link SystemClock#uptimeMillis()}.
384     * @param action The kind of action being performed -- one of either
385     * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
386     * {@link #ACTION_CANCEL}.
387     * @param x The X coordinate of this event.
388     * @param y The Y coordinate of this event.
389     * @param pressure The current pressure of this event.  The pressure generally
390     * ranges from 0 (no pressure at all) to 1 (normal pressure), however
391     * values higher than 1 may be generated depending on the calibration of
392     * the input device.
393     * @param size A scaled value of the approximate size of the area being pressed when
394     * touched with the finger. The actual value in pixels corresponding to the finger
395     * touch is normalized with a device specific range of values
396     * and scaled to a value between 0 and 1.
397     * @param metaState The state of any meta / modifier keys that were in effect when
398     * the event was generated.
399     * @param xPrecision The precision of the X coordinate being reported.
400     * @param yPrecision The precision of the Y coordinate being reported.
401     * @param deviceId The id for the device that this event came from.  An id of
402     * zero indicates that the event didn't come from a physical device; other
403     * numbers are arbitrary and you shouldn't depend on the values.
404     * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
405     * MotionEvent.
406     */
407    static public MotionEvent obtain(long downTime, long eventTime, int action,
408            float x, float y, float pressure, float size, int metaState,
409            float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
410        MotionEvent ev = obtain(1, 1);
411        ev.mDeviceId = deviceId;
412        ev.mSource = InputDevice.SOURCE_UNKNOWN;
413        ev.mEdgeFlags = edgeFlags;
414        ev.mDownTimeNano = downTime * MS_PER_NS;
415        ev.mAction = action;
416        ev.mMetaState = metaState;
417        ev.mXOffset = 0;
418        ev.mYOffset = 0;
419        ev.mXPrecision = xPrecision;
420        ev.mYPrecision = yPrecision;
421
422        ev.mNumPointers = 1;
423        ev.mNumSamples = 1;
424
425        ev.mLastDataSampleIndex = 0;
426        ev.mLastEventTimeNanoSampleIndex = 0;
427
428        ev.mPointerIdentifiers[0] = 0;
429
430        ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS;
431
432        ev.setPointerCoordsAtSampleIndex(0, x, y, pressure, size);
433        return ev;
434    }
435
436    /**
437     * Create a new MotionEvent, filling in all of the basic values that
438     * define the motion.
439     *
440     * @param downTime The time (in ms) when the user originally pressed down to start
441     * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
442     * @param eventTime  The the time (in ms) when this specific event was generated.  This
443     * must be obtained from {@link SystemClock#uptimeMillis()}.
444     * @param action The kind of action being performed -- one of either
445     * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
446     * {@link #ACTION_CANCEL}.
447     * @param pointers The number of pointers that are active in this event.
448     * @param x The X coordinate of this event.
449     * @param y The Y coordinate of this event.
450     * @param pressure The current pressure of this event.  The pressure generally
451     * ranges from 0 (no pressure at all) to 1 (normal pressure), however
452     * values higher than 1 may be generated depending on the calibration of
453     * the input device.
454     * @param size A scaled value of the approximate size of the area being pressed when
455     * touched with the finger. The actual value in pixels corresponding to the finger
456     * touch is normalized with a device specific range of values
457     * and scaled to a value between 0 and 1.
458     * @param metaState The state of any meta / modifier keys that were in effect when
459     * the event was generated.
460     * @param xPrecision The precision of the X coordinate being reported.
461     * @param yPrecision The precision of the Y coordinate being reported.
462     * @param deviceId The id for the device that this event came from.  An id of
463     * zero indicates that the event didn't come from a physical device; other
464     * numbers are arbitrary and you shouldn't depend on the values.
465     * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
466     * MotionEvent.
467     *
468     * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)}
469     * instead.
470     */
471    @Deprecated
472    static public MotionEvent obtain(long downTime, long eventTime, int action,
473            int pointers, float x, float y, float pressure, float size, int metaState,
474            float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
475        return obtain(downTime, eventTime, action, x, y, pressure, size,
476                metaState, xPrecision, yPrecision, deviceId, edgeFlags);
477    }
478
479    /**
480     * Create a new MotionEvent, filling in a subset of the basic motion
481     * values.  Those not specified here are: device id (always 0), pressure
482     * and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
483     *
484     * @param downTime The time (in ms) when the user originally pressed down to start
485     * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
486     * @param eventTime  The the time (in ms) when this specific event was generated.  This
487     * must be obtained from {@link SystemClock#uptimeMillis()}.
488     * @param action The kind of action being performed -- one of either
489     * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
490     * {@link #ACTION_CANCEL}.
491     * @param x The X coordinate of this event.
492     * @param y The Y coordinate of this event.
493     * @param metaState The state of any meta / modifier keys that were in effect when
494     * the event was generated.
495     */
496    static public MotionEvent obtain(long downTime, long eventTime, int action,
497            float x, float y, int metaState) {
498        return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f,
499                metaState, 1.0f, 1.0f, 0, 0);
500    }
501
502    /**
503     * Create a new MotionEvent, copying from an existing one.
504     */
505    static public MotionEvent obtain(MotionEvent o) {
506        MotionEvent ev = obtain(o.mNumPointers, o.mNumSamples);
507        ev.mDeviceId = o.mDeviceId;
508        ev.mSource = o.mSource;
509        ev.mEdgeFlags = o.mEdgeFlags;
510        ev.mDownTimeNano = o.mDownTimeNano;
511        ev.mAction = o.mAction;
512        ev.mMetaState = o.mMetaState;
513        ev.mXOffset = o.mXOffset;
514        ev.mYOffset = o.mYOffset;
515        ev.mXPrecision = o.mXPrecision;
516        ev.mYPrecision = o.mYPrecision;
517        int numPointers = ev.mNumPointers = o.mNumPointers;
518        int numSamples = ev.mNumSamples = o.mNumSamples;
519
520        ev.mLastDataSampleIndex = o.mLastDataSampleIndex;
521        ev.mLastEventTimeNanoSampleIndex = o.mLastEventTimeNanoSampleIndex;
522
523        System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
524
525        System.arraycopy(o.mEventTimeNanoSamples, 0, ev.mEventTimeNanoSamples, 0, numSamples);
526
527        System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0,
528                numPointers * numSamples * NUM_SAMPLE_DATA);
529        return ev;
530    }
531
532    /**
533     * Create a new MotionEvent, copying from an existing one, but not including
534     * any historical point information.
535     */
536    static public MotionEvent obtainNoHistory(MotionEvent o) {
537        MotionEvent ev = obtain(o.mNumPointers, 1);
538        ev.mDeviceId = o.mDeviceId;
539        ev.mSource = o.mSource;
540        ev.mEdgeFlags = o.mEdgeFlags;
541        ev.mDownTimeNano = o.mDownTimeNano;
542        ev.mAction = o.mAction;
543        ev.mMetaState = o.mMetaState;
544        ev.mXOffset = o.mXOffset;
545        ev.mYOffset = o.mYOffset;
546        ev.mXPrecision = o.mXPrecision;
547        ev.mYPrecision = o.mYPrecision;
548
549        int numPointers = ev.mNumPointers = o.mNumPointers;
550        ev.mNumSamples = 1;
551
552        ev.mLastDataSampleIndex = 0;
553        ev.mLastEventTimeNanoSampleIndex = 0;
554
555        System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
556
557        ev.mEventTimeNanoSamples[0] = o.mEventTimeNanoSamples[o.mLastEventTimeNanoSampleIndex];
558
559        System.arraycopy(o.mDataSamples, o.mLastDataSampleIndex, ev.mDataSamples, 0,
560                numPointers * NUM_SAMPLE_DATA);
561        return ev;
562    }
563
564    /**
565     * Recycle the MotionEvent, to be re-used by a later caller.  After calling
566     * this function you must not ever touch the event again.
567     */
568    public final void recycle() {
569        // Ensure recycle is only called once!
570        if (TRACK_RECYCLED_LOCATION) {
571            if (mRecycledLocation != null) {
572                throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
573            }
574            mRecycledLocation = new RuntimeException("Last recycled here");
575            //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation);
576        } else {
577            if (mRecycled) {
578                throw new RuntimeException(toString() + " recycled twice!");
579            }
580            mRecycled = true;
581        }
582
583        synchronized (gRecyclerLock) {
584            if (gRecyclerUsed < MAX_RECYCLED) {
585                gRecyclerUsed++;
586                mNumSamples = 0;
587                mNext = gRecyclerTop;
588                gRecyclerTop = this;
589            }
590        }
591    }
592
593    /**
594     * Scales down the coordination of this event by the given scale.
595     *
596     * @hide
597     */
598    public final void scale(float scale) {
599        mXOffset *= scale;
600        mYOffset *= scale;
601        mXPrecision *= scale;
602        mYPrecision *= scale;
603
604        float[] history = mDataSamples;
605        final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA;
606        for (int i = 0; i < length; i += NUM_SAMPLE_DATA) {
607            history[i + SAMPLE_X] *= scale;
608            history[i + SAMPLE_Y] *= scale;
609            // no need to scale pressure
610            history[i + SAMPLE_SIZE] *= scale;    // TODO: square this?
611            history[i + SAMPLE_TOUCH_MAJOR] *= scale;
612            history[i + SAMPLE_TOUCH_MINOR] *= scale;
613            history[i + SAMPLE_TOOL_MAJOR] *= scale;
614            history[i + SAMPLE_TOOL_MINOR] *= scale;
615        }
616    }
617
618    /**
619     * Return the kind of action being performed -- one of either
620     * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
621     * {@link #ACTION_CANCEL}.  Consider using {@link #getActionMasked}
622     * and {@link #getActionIndex} to retrieve the separate masked action
623     * and pointer index.
624     */
625    public final int getAction() {
626        return mAction;
627    }
628
629    /**
630     * Return the masked action being performed, without pointer index
631     * information.  May be any of the actions: {@link #ACTION_DOWN},
632     * {@link #ACTION_MOVE}, {@link #ACTION_UP}, {@link #ACTION_CANCEL},
633     * {@link #ACTION_POINTER_DOWN}, or {@link #ACTION_POINTER_UP}.
634     * Use {@link #getActionIndex} to return the index associated with
635     * pointer actions.
636     */
637    public final int getActionMasked() {
638        return mAction & ACTION_MASK;
639    }
640
641    /**
642     * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP}
643     * as returned by {@link #getActionMasked}, this returns the associated
644     * pointer index.  The index may be used with {@link #getPointerId(int)},
645     * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)},
646     * and {@link #getSize(int)} to get information about the pointer that has
647     * gone down or up.
648     */
649    public final int getActionIndex() {
650        return (mAction & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
651    }
652
653    /**
654     * Returns the time (in ms) when the user originally pressed down to start
655     * a stream of position events.
656     */
657    public final long getDownTime() {
658        return mDownTimeNano / MS_PER_NS;
659    }
660
661    /**
662     * Returns the time (in ms) when this specific event was generated.
663     */
664    public final long getEventTime() {
665        return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] / MS_PER_NS;
666    }
667
668    /**
669     * Returns the time (in ns) when this specific event was generated.
670     * The value is in nanosecond precision but it may not have nanosecond accuracy.
671     *
672     * @hide
673     */
674    public final long getEventTimeNano() {
675        return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex];
676    }
677
678    /**
679     * {@link #getX(int)} for the first pointer index (may be an
680     * arbitrary pointer identifier).
681     */
682    public final float getX() {
683        return mDataSamples[mLastDataSampleIndex + SAMPLE_X] + mXOffset;
684    }
685
686    /**
687     * {@link #getY(int)} for the first pointer index (may be an
688     * arbitrary pointer identifier).
689     */
690    public final float getY() {
691        return mDataSamples[mLastDataSampleIndex + SAMPLE_Y] + mYOffset;
692    }
693
694    /**
695     * {@link #getPressure(int)} for the first pointer index (may be an
696     * arbitrary pointer identifier).
697     */
698    public final float getPressure() {
699        return mDataSamples[mLastDataSampleIndex + SAMPLE_PRESSURE];
700    }
701
702    /**
703     * {@link #getSize(int)} for the first pointer index (may be an
704     * arbitrary pointer identifier).
705     */
706    public final float getSize() {
707        return mDataSamples[mLastDataSampleIndex + SAMPLE_SIZE];
708    }
709
710    /**
711     * {@link #getTouchMajor(int)} for the first pointer index (may be an
712     * arbitrary pointer identifier).
713     */
714    public final float getTouchMajor() {
715        return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MAJOR];
716    }
717
718    /**
719     * {@link #getTouchMinor(int)} for the first pointer index (may be an
720     * arbitrary pointer identifier).
721     */
722    public final float getTouchMinor() {
723        return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MINOR];
724    }
725
726    /**
727     * {@link #getToolMajor(int)} for the first pointer index (may be an
728     * arbitrary pointer identifier).
729     */
730    public final float getToolMajor() {
731        return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MAJOR];
732    }
733
734    /**
735     * {@link #getToolMinor(int)} for the first pointer index (may be an
736     * arbitrary pointer identifier).
737     */
738    public final float getToolMinor() {
739        return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MINOR];
740    }
741
742    /**
743     * {@link #getOrientation(int)} for the first pointer index (may be an
744     * arbitrary pointer identifier).
745     */
746    public final float getOrientation() {
747        return mDataSamples[mLastDataSampleIndex + SAMPLE_ORIENTATION];
748    }
749
750    /**
751     * The number of pointers of data contained in this event.  Always
752     * >= 1.
753     */
754    public final int getPointerCount() {
755        return mNumPointers;
756    }
757
758    /**
759     * Return the pointer identifier associated with a particular pointer
760     * data index is this event.  The identifier tells you the actual pointer
761     * number associated with the data, accounting for individual pointers
762     * going up and down since the start of the current gesture.
763     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
764     * (the first pointer that is down) to {@link #getPointerCount()}-1.
765     */
766    public final int getPointerId(int pointerIndex) {
767        return mPointerIdentifiers[pointerIndex];
768    }
769
770    /**
771     * Given a pointer identifier, find the index of its data in the event.
772     *
773     * @param pointerId The identifier of the pointer to be found.
774     * @return Returns either the index of the pointer (for use with
775     * {@link #getX(int)} et al.), or -1 if there is no data available for
776     * that pointer identifier.
777     */
778    public final int findPointerIndex(int pointerId) {
779        int i = mNumPointers;
780        while (i > 0) {
781            i--;
782            if (mPointerIdentifiers[i] == pointerId) {
783                return i;
784            }
785        }
786        return -1;
787    }
788
789    /**
790     * Returns the X coordinate of this event for the given pointer
791     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
792     * identifier for this index).
793     * Whole numbers are pixels; the
794     * value may have a fraction for input devices that are sub-pixel precise.
795     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
796     * (the first pointer that is down) to {@link #getPointerCount()}-1.
797     */
798    public final float getX(int pointerIndex) {
799        return mDataSamples[mLastDataSampleIndex
800                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
801    }
802
803    /**
804     * Returns the Y coordinate of this event for the given pointer
805     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
806     * identifier for this index).
807     * Whole numbers are pixels; the
808     * value may have a fraction for input devices that are sub-pixel precise.
809     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
810     * (the first pointer that is down) to {@link #getPointerCount()}-1.
811     */
812    public final float getY(int pointerIndex) {
813        return mDataSamples[mLastDataSampleIndex
814                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
815    }
816
817    /**
818     * Returns the current pressure of this event for the given pointer
819     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
820     * identifier for this index).
821     * The pressure generally
822     * ranges from 0 (no pressure at all) to 1 (normal pressure), however
823     * values higher than 1 may be generated depending on the calibration of
824     * the input device.
825     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
826     * (the first pointer that is down) to {@link #getPointerCount()}-1.
827     */
828    public final float getPressure(int pointerIndex) {
829        return mDataSamples[mLastDataSampleIndex
830                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
831    }
832
833    /**
834     * Returns a scaled value of the approximate size for the given pointer
835     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
836     * identifier for this index).
837     * This represents some approximation of the area of the screen being
838     * pressed; the actual value in pixels corresponding to the
839     * touch is normalized with the device specific range of values
840     * and scaled to a value between 0 and 1. The value of size can be used to
841     * determine fat touch events.
842     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
843     * (the first pointer that is down) to {@link #getPointerCount()}-1.
844     */
845    public final float getSize(int pointerIndex) {
846        return mDataSamples[mLastDataSampleIndex
847                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_SIZE];
848    }
849
850    /**
851     * Returns the length of the major axis of an ellipse that describes the touch
852     * area at the point of contact for the given pointer
853     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
854     * identifier for this index).
855     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
856     * (the first pointer that is down) to {@link #getPointerCount()}-1.
857     */
858    public final float getTouchMajor(int pointerIndex) {
859        return mDataSamples[mLastDataSampleIndex
860                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
861    }
862
863    /**
864     * Returns the length of the minor axis of an ellipse that describes the touch
865     * area at the point of contact for the given pointer
866     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
867     * identifier for this index).
868     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
869     * (the first pointer that is down) to {@link #getPointerCount()}-1.
870     */
871    public final float getTouchMinor(int pointerIndex) {
872        return mDataSamples[mLastDataSampleIndex
873                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
874    }
875
876    /**
877     * Returns the length of the major axis of an ellipse that describes the size of
878     * the approaching tool for the given pointer
879     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
880     * identifier for this index).
881     * The tool area represents the estimated size of the finger or pen that is
882     * touching the device independent of its actual touch area at the point of contact.
883     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
884     * (the first pointer that is down) to {@link #getPointerCount()}-1.
885     */
886    public final float getToolMajor(int pointerIndex) {
887        return mDataSamples[mLastDataSampleIndex
888                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
889    }
890
891    /**
892     * Returns the length of the minor axis of an ellipse that describes the size of
893     * the approaching tool for the given pointer
894     * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
895     * identifier for this index).
896     * The tool area represents the estimated size of the finger or pen that is
897     * touching the device independent of its actual touch area at the point of contact.
898     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
899     * (the first pointer that is down) to {@link #getPointerCount()}-1.
900     */
901    public final float getToolMinor(int pointerIndex) {
902        return mDataSamples[mLastDataSampleIndex
903                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
904    }
905
906    /**
907     * Returns the orientation of the touch area and tool area in radians clockwise from vertical
908     * for the given pointer <em>index</em> (use {@link #getPointerId(int)} to find the pointer
909     * identifier for this index).
910     * An angle of 0 degrees indicates that the major axis of contact is oriented
911     * upwards, is perfectly circular or is of unknown orientation.  A positive angle
912     * indicates that the major axis of contact is oriented to the right.  A negative angle
913     * indicates that the major axis of contact is oriented to the left.
914     * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
915     * (finger pointing fully right).
916     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
917     * (the first pointer that is down) to {@link #getPointerCount()}-1.
918     */
919    public final float getOrientation(int pointerIndex) {
920        return mDataSamples[mLastDataSampleIndex
921                            + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
922    }
923
924    /**
925     * Populates a {@link PointerCoords} object with pointer coordinate data for
926     * the specified pointer index.
927     *
928     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
929     * (the first pointer that is down) to {@link #getPointerCount()}-1.
930     * @param outPointerCoords The pointer coordinate object to populate.
931     */
932    public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) {
933        final int sampleIndex = mLastDataSampleIndex + pointerIndex * NUM_SAMPLE_DATA;
934        getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords);
935    }
936
937    /**
938     * Returns the state of any meta / modifier keys that were in effect when
939     * the event was generated.  This is the same values as those
940     * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}.
941     *
942     * @return an integer in which each bit set to 1 represents a pressed
943     *         meta key
944     *
945     * @see KeyEvent#getMetaState()
946     */
947    public final int getMetaState() {
948        return mMetaState;
949    }
950
951    /**
952     * Returns the original raw X coordinate of this event.  For touch
953     * events on the screen, this is the original location of the event
954     * on the screen, before it had been adjusted for the containing window
955     * and views.
956     */
957    public final float getRawX() {
958        return mDataSamples[mLastDataSampleIndex + SAMPLE_X];
959    }
960
961    /**
962     * Returns the original raw Y coordinate of this event.  For touch
963     * events on the screen, this is the original location of the event
964     * on the screen, before it had been adjusted for the containing window
965     * and views.
966     */
967    public final float getRawY() {
968        return mDataSamples[mLastDataSampleIndex + SAMPLE_Y];
969    }
970
971    /**
972     * Return the precision of the X coordinates being reported.  You can
973     * multiple this number with {@link #getX} to find the actual hardware
974     * value of the X coordinate.
975     * @return Returns the precision of X coordinates being reported.
976     */
977    public final float getXPrecision() {
978        return mXPrecision;
979    }
980
981    /**
982     * Return the precision of the Y coordinates being reported.  You can
983     * multiple this number with {@link #getY} to find the actual hardware
984     * value of the Y coordinate.
985     * @return Returns the precision of Y coordinates being reported.
986     */
987    public final float getYPrecision() {
988        return mYPrecision;
989    }
990
991    /**
992     * Returns the number of historical points in this event.  These are
993     * movements that have occurred between this event and the previous event.
994     * This only applies to ACTION_MOVE events -- all other actions will have
995     * a size of 0.
996     *
997     * @return Returns the number of historical points in the event.
998     */
999    public final int getHistorySize() {
1000        return mLastEventTimeNanoSampleIndex;
1001    }
1002
1003    /**
1004     * Returns the time that a historical movement occurred between this event
1005     * and the previous event.  Only applies to ACTION_MOVE events.
1006     *
1007     * @param pos Which historical value to return; must be less than
1008     * {@link #getHistorySize}
1009     *
1010     * @see #getHistorySize
1011     * @see #getEventTime
1012     */
1013    public final long getHistoricalEventTime(int pos) {
1014        return mEventTimeNanoSamples[pos] / MS_PER_NS;
1015    }
1016
1017    /**
1018     * {@link #getHistoricalX(int)} for the first pointer index (may be an
1019     * arbitrary pointer identifier).
1020     */
1021    public final float getHistoricalX(int pos) {
1022        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
1023    }
1024
1025    /**
1026     * {@link #getHistoricalY(int)} for the first pointer index (may be an
1027     * arbitrary pointer identifier).
1028     */
1029    public final float getHistoricalY(int pos) {
1030        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
1031    }
1032
1033    /**
1034     * {@link #getHistoricalPressure(int)} for the first pointer index (may be an
1035     * arbitrary pointer identifier).
1036     */
1037    public final float getHistoricalPressure(int pos) {
1038        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
1039    }
1040
1041    /**
1042     * {@link #getHistoricalSize(int)} for the first pointer index (may be an
1043     * arbitrary pointer identifier).
1044     */
1045    public final float getHistoricalSize(int pos) {
1046        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_SIZE];
1047    }
1048
1049    /**
1050     * {@link #getHistoricalTouchMajor(int)} for the first pointer index (may be an
1051     * arbitrary pointer identifier).
1052     */
1053    public final float getHistoricalTouchMajor(int pos) {
1054        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
1055    }
1056
1057    /**
1058     * {@link #getHistoricalTouchMinor(int)} for the first pointer index (may be an
1059     * arbitrary pointer identifier).
1060     */
1061    public final float getHistoricalTouchMinor(int pos) {
1062        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
1063    }
1064
1065    /**
1066     * {@link #getHistoricalToolMajor(int)} for the first pointer index (may be an
1067     * arbitrary pointer identifier).
1068     */
1069    public final float getHistoricalToolMajor(int pos) {
1070        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
1071    }
1072
1073    /**
1074     * {@link #getHistoricalToolMinor(int)} for the first pointer index (may be an
1075     * arbitrary pointer identifier).
1076     */
1077    public final float getHistoricalToolMinor(int pos) {
1078        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
1079    }
1080
1081    /**
1082     * {@link #getHistoricalOrientation(int)} for the first pointer index (may be an
1083     * arbitrary pointer identifier).
1084     */
1085    public final float getHistoricalOrientation(int pos) {
1086        return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
1087    }
1088
1089    /**
1090     * Returns a historical X coordinate, as per {@link #getX(int)}, that
1091     * occurred between this event and the previous event for the given pointer.
1092     * Only applies to ACTION_MOVE events.
1093     *
1094     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1095     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1096     * @param pos Which historical value to return; must be less than
1097     * {@link #getHistorySize}
1098     *
1099     * @see #getHistorySize
1100     * @see #getX
1101     */
1102    public final float getHistoricalX(int pointerIndex, int pos) {
1103        return mDataSamples[(pos * mNumPointers + pointerIndex)
1104                            * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
1105    }
1106
1107    /**
1108     * Returns a historical Y coordinate, as per {@link #getY(int)}, that
1109     * occurred between this event and the previous event for the given pointer.
1110     * Only applies to ACTION_MOVE events.
1111     *
1112     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1113     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1114     * @param pos Which historical value to return; must be less than
1115     * {@link #getHistorySize}
1116     *
1117     * @see #getHistorySize
1118     * @see #getY
1119     */
1120    public final float getHistoricalY(int pointerIndex, int pos) {
1121        return mDataSamples[(pos * mNumPointers + pointerIndex)
1122                            * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
1123    }
1124
1125    /**
1126     * Returns a historical pressure coordinate, as per {@link #getPressure(int)},
1127     * that occurred between this event and the previous event for the given
1128     * pointer.  Only applies to ACTION_MOVE events.
1129     *
1130     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1131     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1132     * @param pos Which historical value to return; must be less than
1133     * {@link #getHistorySize}
1134     *
1135     * @see #getHistorySize
1136     * @see #getPressure
1137     */
1138    public final float getHistoricalPressure(int pointerIndex, int pos) {
1139        return mDataSamples[(pos * mNumPointers + pointerIndex)
1140                            * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
1141    }
1142
1143    /**
1144     * Returns a historical size coordinate, as per {@link #getSize(int)}, that
1145     * occurred between this event and the previous event for the given pointer.
1146     * Only applies to ACTION_MOVE events.
1147     *
1148     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1149     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1150     * @param pos Which historical value to return; must be less than
1151     * {@link #getHistorySize}
1152     *
1153     * @see #getHistorySize
1154     * @see #getSize
1155     */
1156    public final float getHistoricalSize(int pointerIndex, int pos) {
1157        return mDataSamples[(pos * mNumPointers + pointerIndex)
1158                            * NUM_SAMPLE_DATA + SAMPLE_SIZE];
1159    }
1160
1161    /**
1162     * Returns a historical touch major axis coordinate, as per {@link #getTouchMajor(int)}, that
1163     * occurred between this event and the previous event for the given pointer.
1164     * Only applies to ACTION_MOVE events.
1165     *
1166     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1167     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1168     * @param pos Which historical value to return; must be less than
1169     * {@link #getHistorySize}
1170     *
1171     * @see #getHistorySize
1172     * @see #getTouchMajor
1173     */
1174    public final float getHistoricalTouchMajor(int pointerIndex, int pos) {
1175        return mDataSamples[(pos * mNumPointers + pointerIndex)
1176                            * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
1177    }
1178
1179    /**
1180     * Returns a historical touch minor axis coordinate, as per {@link #getTouchMinor(int)}, that
1181     * occurred between this event and the previous event for the given pointer.
1182     * Only applies to ACTION_MOVE events.
1183     *
1184     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1185     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1186     * @param pos Which historical value to return; must be less than
1187     * {@link #getHistorySize}
1188     *
1189     * @see #getHistorySize
1190     * @see #getTouchMinor
1191     */
1192    public final float getHistoricalTouchMinor(int pointerIndex, int pos) {
1193        return mDataSamples[(pos * mNumPointers + pointerIndex)
1194                            * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
1195    }
1196
1197    /**
1198     * Returns a historical tool major axis coordinate, as per {@link #getToolMajor(int)}, that
1199     * occurred between this event and the previous event for the given pointer.
1200     * Only applies to ACTION_MOVE events.
1201     *
1202     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1203     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1204     * @param pos Which historical value to return; must be less than
1205     * {@link #getHistorySize}
1206     *
1207     * @see #getHistorySize
1208     * @see #getToolMajor
1209     */
1210    public final float getHistoricalToolMajor(int pointerIndex, int pos) {
1211        return mDataSamples[(pos * mNumPointers + pointerIndex)
1212                            * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
1213    }
1214
1215    /**
1216     * Returns a historical tool minor axis coordinate, as per {@link #getToolMinor(int)}, that
1217     * occurred between this event and the previous event for the given pointer.
1218     * Only applies to ACTION_MOVE events.
1219     *
1220     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1221     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1222     * @param pos Which historical value to return; must be less than
1223     * {@link #getHistorySize}
1224     *
1225     * @see #getHistorySize
1226     * @see #getToolMinor
1227     */
1228    public final float getHistoricalToolMinor(int pointerIndex, int pos) {
1229        return mDataSamples[(pos * mNumPointers + pointerIndex)
1230                            * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
1231    }
1232
1233    /**
1234     * Returns a historical orientation coordinate, as per {@link #getOrientation(int)}, that
1235     * occurred between this event and the previous event for the given pointer.
1236     * Only applies to ACTION_MOVE events.
1237     *
1238     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1239     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1240     * @param pos Which historical value to return; must be less than
1241     * {@link #getHistorySize}
1242     *
1243     * @see #getHistorySize
1244     * @see #getOrientation
1245     */
1246    public final float getHistoricalOrientation(int pointerIndex, int pos) {
1247        return mDataSamples[(pos * mNumPointers + pointerIndex)
1248                            * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
1249    }
1250
1251    /**
1252     * Populates a {@link PointerCoords} object with historical pointer coordinate data,
1253     * as per {@link #getPointerCoords}, that occurred between this event and the previous
1254     * event for the given pointer.
1255     * Only applies to ACTION_MOVE events.
1256     *
1257     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
1258     * (the first pointer that is down) to {@link #getPointerCount()}-1.
1259     * @param pos Which historical value to return; must be less than
1260     * {@link #getHistorySize}
1261     * @param outPointerCoords The pointer coordinate object to populate.
1262     *
1263     * @see #getHistorySize
1264     * @see #getPointerCoords
1265     */
1266    public final void getHistoricalPointerCoords(int pointerIndex, int pos,
1267            PointerCoords outPointerCoords) {
1268        final int sampleIndex = (pos * mNumPointers + pointerIndex) * NUM_SAMPLE_DATA;
1269        getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords);
1270    }
1271
1272    /**
1273     * Returns a bitfield indicating which edges, if any, were touched by this
1274     * MotionEvent. For touch events, clients can use this to determine if the
1275     * user's finger was touching the edge of the display.
1276     *
1277     * @see #EDGE_LEFT
1278     * @see #EDGE_TOP
1279     * @see #EDGE_RIGHT
1280     * @see #EDGE_BOTTOM
1281     */
1282    public final int getEdgeFlags() {
1283        return mEdgeFlags;
1284    }
1285
1286
1287    /**
1288     * Sets the bitfield indicating which edges, if any, where touched by this
1289     * MotionEvent.
1290     *
1291     * @see #getEdgeFlags()
1292     */
1293    public final void setEdgeFlags(int flags) {
1294        mEdgeFlags = flags;
1295    }
1296
1297    /**
1298     * Sets this event's action.
1299     */
1300    public final void setAction(int action) {
1301        mAction = action;
1302    }
1303
1304    /**
1305     * Adjust this event's location.
1306     * @param deltaX Amount to add to the current X coordinate of the event.
1307     * @param deltaY Amount to add to the current Y coordinate of the event.
1308     */
1309    public final void offsetLocation(float deltaX, float deltaY) {
1310        mXOffset += deltaX;
1311        mYOffset += deltaY;
1312    }
1313
1314    /**
1315     * Set this event's location.  Applies {@link #offsetLocation} with a
1316     * delta from the current location to the given new location.
1317     *
1318     * @param x New absolute X location.
1319     * @param y New absolute Y location.
1320     */
1321    public final void setLocation(float x, float y) {
1322        final float[] dataSamples = mDataSamples;
1323        final int lastDataSampleIndex = mLastDataSampleIndex;
1324        mXOffset = x - dataSamples[lastDataSampleIndex + SAMPLE_X];
1325        mYOffset = y - dataSamples[lastDataSampleIndex + SAMPLE_Y];
1326    }
1327
1328    private final void getPointerCoordsAtSampleIndex(int sampleIndex,
1329            PointerCoords outPointerCoords) {
1330        final float[] dataSamples = mDataSamples;
1331        outPointerCoords.x = dataSamples[sampleIndex + SAMPLE_X] + mXOffset;
1332        outPointerCoords.y = dataSamples[sampleIndex + SAMPLE_Y] + mYOffset;
1333        outPointerCoords.pressure = dataSamples[sampleIndex + SAMPLE_PRESSURE];
1334        outPointerCoords.size = dataSamples[sampleIndex + SAMPLE_SIZE];
1335        outPointerCoords.touchMajor = dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR];
1336        outPointerCoords.touchMinor = dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR];
1337        outPointerCoords.toolMajor = dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR];
1338        outPointerCoords.toolMinor = dataSamples[sampleIndex + SAMPLE_TOOL_MINOR];
1339        outPointerCoords.orientation = dataSamples[sampleIndex + SAMPLE_ORIENTATION];
1340    }
1341
1342    private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1343            PointerCoords[] pointerCoords) {
1344        final int numPointers = mNumPointers;
1345        for (int i = 0; i < numPointers; i++) {
1346            setPointerCoordsAtSampleIndex(sampleIndex, pointerCoords[i]);
1347            sampleIndex += NUM_SAMPLE_DATA;
1348        }
1349    }
1350
1351    private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1352            PointerCoords pointerCoords) {
1353        final float[] dataSamples = mDataSamples;
1354        dataSamples[sampleIndex + SAMPLE_X] = pointerCoords.x - mXOffset;
1355        dataSamples[sampleIndex + SAMPLE_Y] = pointerCoords.y - mYOffset;
1356        dataSamples[sampleIndex + SAMPLE_PRESSURE] = pointerCoords.pressure;
1357        dataSamples[sampleIndex + SAMPLE_SIZE] = pointerCoords.size;
1358        dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pointerCoords.touchMajor;
1359        dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pointerCoords.touchMinor;
1360        dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = pointerCoords.toolMajor;
1361        dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = pointerCoords.toolMinor;
1362        dataSamples[sampleIndex + SAMPLE_ORIENTATION] = pointerCoords.orientation;
1363    }
1364
1365    private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1366            float x, float y, float pressure, float size) {
1367        final float[] dataSamples = mDataSamples;
1368        dataSamples[sampleIndex + SAMPLE_X] = x - mXOffset;
1369        dataSamples[sampleIndex + SAMPLE_Y] = y - mYOffset;
1370        dataSamples[sampleIndex + SAMPLE_PRESSURE] = pressure;
1371        dataSamples[sampleIndex + SAMPLE_SIZE] = size;
1372        dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pressure;
1373        dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pressure;
1374        dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = size;
1375        dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = size;
1376        dataSamples[sampleIndex + SAMPLE_ORIENTATION] = 0;
1377    }
1378
1379    private final void incrementNumSamplesAndReserveStorage(int dataSampleStride) {
1380        if (mNumSamples == mEventTimeNanoSamples.length) {
1381            long[] newEventTimeNanoSamples = new long[mNumSamples + BASE_AVAIL_SAMPLES];
1382            System.arraycopy(mEventTimeNanoSamples, 0, newEventTimeNanoSamples, 0, mNumSamples);
1383            mEventTimeNanoSamples = newEventTimeNanoSamples;
1384        }
1385
1386        int nextDataSampleIndex = mLastDataSampleIndex + dataSampleStride;
1387        if (nextDataSampleIndex + dataSampleStride > mDataSamples.length) {
1388            float[] newDataSamples = new float[nextDataSampleIndex
1389                                               + BASE_AVAIL_SAMPLES * dataSampleStride];
1390            System.arraycopy(mDataSamples, 0, newDataSamples, 0, nextDataSampleIndex);
1391            mDataSamples = newDataSamples;
1392        }
1393
1394        mLastEventTimeNanoSampleIndex = mNumSamples;
1395        mLastDataSampleIndex = nextDataSampleIndex;
1396        mNumSamples += 1;
1397    }
1398
1399    /**
1400     * Add a new movement to the batch of movements in this event.  The event's
1401     * current location, position and size is updated to the new values.
1402     * The current values in the event are added to a list of historical values.
1403     *
1404     * Only applies to {@link #ACTION_MOVE} events.
1405     *
1406     * @param eventTime The time stamp (in ms) for this data.
1407     * @param x The new X position.
1408     * @param y The new Y position.
1409     * @param pressure The new pressure.
1410     * @param size The new size.
1411     * @param metaState Meta key state.
1412     */
1413    public final void addBatch(long eventTime, float x, float y,
1414            float pressure, float size, int metaState) {
1415        incrementNumSamplesAndReserveStorage(NUM_SAMPLE_DATA);
1416
1417        mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
1418        setPointerCoordsAtSampleIndex(mLastDataSampleIndex, x, y, pressure, size);
1419
1420        mMetaState |= metaState;
1421    }
1422
1423    /**
1424     * Add a new movement to the batch of movements in this event.  The event's
1425     * current location, position and size is updated to the new values.
1426     * The current values in the event are added to a list of historical values.
1427     *
1428     * Only applies to {@link #ACTION_MOVE} events.
1429     *
1430     * @param eventTime The time stamp (in ms) for this data.
1431     * @param pointerCoords The new pointer coordinates.
1432     * @param metaState Meta key state.
1433     */
1434    public final void addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState) {
1435        final int dataSampleStride = mNumPointers * NUM_SAMPLE_DATA;
1436        incrementNumSamplesAndReserveStorage(dataSampleStride);
1437
1438        mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
1439        setPointerCoordsAtSampleIndex(mLastDataSampleIndex, pointerCoords);
1440
1441        mMetaState |= metaState;
1442    }
1443
1444    @Override
1445    public String toString() {
1446        return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this))
1447            + " action=" + mAction + " x=" + getX()
1448            + " y=" + getY() + " pressure=" + getPressure() + " size=" + getSize() + "}";
1449    }
1450
1451    public static final Parcelable.Creator<MotionEvent> CREATOR
1452            = new Parcelable.Creator<MotionEvent>() {
1453        public MotionEvent createFromParcel(Parcel in) {
1454            in.readInt(); // skip token, we already know this is a MotionEvent
1455            return MotionEvent.createFromParcelBody(in);
1456        }
1457
1458        public MotionEvent[] newArray(int size) {
1459            return new MotionEvent[size];
1460        }
1461    };
1462
1463    /** @hide */
1464    public static MotionEvent createFromParcelBody(Parcel in) {
1465        final int NP = in.readInt();
1466        final int NS = in.readInt();
1467        final int NI = NP * NS * NUM_SAMPLE_DATA;
1468
1469        MotionEvent ev = obtain(NP, NS);
1470        ev.mNumPointers = NP;
1471        ev.mNumSamples = NS;
1472
1473        ev.readBaseFromParcel(in);
1474
1475        ev.mDownTimeNano = in.readLong();
1476        ev.mAction = in.readInt();
1477        ev.mXOffset = in.readFloat();
1478        ev.mYOffset = in.readFloat();
1479        ev.mXPrecision = in.readFloat();
1480        ev.mYPrecision = in.readFloat();
1481        ev.mEdgeFlags = in.readInt();
1482        ev.mMetaState = in.readInt();
1483
1484        final int[] pointerIdentifiers = ev.mPointerIdentifiers;
1485        for (int i = 0; i < NP; i++) {
1486            pointerIdentifiers[i] = in.readInt();
1487        }
1488
1489        final long[] eventTimeNanoSamples = ev.mEventTimeNanoSamples;
1490        for (int i = 0; i < NS; i++) {
1491            eventTimeNanoSamples[i] = in.readLong();
1492        }
1493
1494        final float[] dataSamples = ev.mDataSamples;
1495        for (int i = 0; i < NI; i++) {
1496            dataSamples[i] = in.readFloat();
1497        }
1498
1499        ev.mLastEventTimeNanoSampleIndex = NS - 1;
1500        ev.mLastDataSampleIndex = (NS - 1) * NP * NUM_SAMPLE_DATA;
1501        return ev;
1502    }
1503
1504    public void writeToParcel(Parcel out, int flags) {
1505        out.writeInt(PARCEL_TOKEN_MOTION_EVENT);
1506
1507        final int NP = mNumPointers;
1508        final int NS = mNumSamples;
1509        final int NI = NP * NS * NUM_SAMPLE_DATA;
1510
1511        out.writeInt(NP);
1512        out.writeInt(NS);
1513
1514        writeBaseToParcel(out);
1515
1516        out.writeLong(mDownTimeNano);
1517        out.writeInt(mAction);
1518        out.writeFloat(mXOffset);
1519        out.writeFloat(mYOffset);
1520        out.writeFloat(mXPrecision);
1521        out.writeFloat(mYPrecision);
1522        out.writeInt(mEdgeFlags);
1523        out.writeInt(mMetaState);
1524
1525        final int[] pointerIdentifiers = mPointerIdentifiers;
1526        for (int i = 0; i < NP; i++) {
1527            out.writeInt(pointerIdentifiers[i]);
1528        }
1529
1530        final long[] eventTimeNanoSamples = mEventTimeNanoSamples;
1531        for (int i = 0; i < NS; i++) {
1532            out.writeLong(eventTimeNanoSamples[i]);
1533        }
1534
1535        final float[] dataSamples = mDataSamples;
1536        for (int i = 0; i < NI; i++) {
1537            out.writeFloat(dataSamples[i]);
1538        }
1539    }
1540
1541    /**
1542     * Transfer object for pointer coordinates.
1543     *
1544     * Objects of this type can be used to manufacture new {@link MotionEvent} objects
1545     * and to query pointer coordinate information in bulk.
1546     *
1547     * Refer to {@link InputDevice} for information about how different kinds of
1548     * input devices and sources represent pointer coordinates.
1549     */
1550    public static final class PointerCoords {
1551        /**
1552         * The X coordinate of the pointer movement.
1553         * The interpretation varies by input source and may represent the position of
1554         * the center of the contact area, a relative displacement in device-specific units
1555         * or something else.
1556         */
1557        public float x;
1558
1559        /**
1560         * The Y coordinate of the pointer movement.
1561         * The interpretation varies by input source and may represent the position of
1562         * the center of the contact area, a relative displacement in device-specific units
1563         * or something else.
1564         */
1565        public float y;
1566
1567        /**
1568         * A scaled value that describes the pressure applied to the pointer.
1569         * The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure),
1570         * however values higher than 1 may be generated depending on the calibration of
1571         * the input device.
1572         */
1573        public float pressure;
1574
1575        /**
1576         * A scaled value of the approximate size of the pointer touch area.
1577         * This represents some approximation of the area of the screen being
1578         * pressed; the actual value in pixels corresponding to the
1579         * touch is normalized with the device specific range of values
1580         * and scaled to a value between 0 and 1. The value of size can be used to
1581         * determine fat touch events.
1582         */
1583        public float size;
1584
1585        /**
1586         * The length of the major axis of an ellipse that describes the touch area at
1587         * the point of contact.
1588         */
1589        public float touchMajor;
1590
1591        /**
1592         * The length of the minor axis of an ellipse that describes the touch area at
1593         * the point of contact.
1594         */
1595        public float touchMinor;
1596
1597        /**
1598         * The length of the major axis of an ellipse that describes the size of
1599         * the approaching tool.
1600         * The tool area represents the estimated size of the finger or pen that is
1601         * touching the device independent of its actual touch area at the point of contact.
1602         */
1603        public float toolMajor;
1604
1605        /**
1606         * The length of the minor axis of an ellipse that describes the size of
1607         * the approaching tool.
1608         * The tool area represents the estimated size of the finger or pen that is
1609         * touching the device independent of its actual touch area at the point of contact.
1610         */
1611        public float toolMinor;
1612
1613        /**
1614         * The orientation of the touch area and tool area in radians clockwise from vertical.
1615         * An angle of 0 degrees indicates that the major axis of contact is oriented
1616         * upwards, is perfectly circular or is of unknown orientation.  A positive angle
1617         * indicates that the major axis of contact is oriented to the right.  A negative angle
1618         * indicates that the major axis of contact is oriented to the left.
1619         * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
1620         * (finger pointing fully right).
1621         */
1622        public float orientation;
1623
1624        /*
1625        private static final float PI_4 = (float) (Math.PI / 4);
1626
1627        public float getTouchWidth() {
1628            return Math.abs(orientation) > PI_4 ? touchMajor : touchMinor;
1629        }
1630
1631        public float getTouchHeight() {
1632            return Math.abs(orientation) > PI_4 ? touchMinor : touchMajor;
1633        }
1634
1635        public float getToolWidth() {
1636            return Math.abs(orientation) > PI_4 ? toolMajor : toolMinor;
1637        }
1638
1639        public float getToolHeight() {
1640            return Math.abs(orientation) > PI_4 ? toolMinor : toolMajor;
1641        }
1642        */
1643    }
1644}
1645