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