blob: ae8c21dd666bb2fc9da283ef29d2e68d502bd057 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070022import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24/**
25 * Object used to report movement (mouse, pen, finger, trackball) events. This
26 * class may hold either absolute or relative movements, depending on what
27 * it is being used for.
28 */
29public final class MotionEvent implements Parcelable {
Jeff Brown5c225b12010-06-16 01:53:36 -070030 private static final long MS_PER_NS = 1000000;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070031 static final boolean DEBUG_POINTERS = false;
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070034 * Bit mask of the parts of the action code that are the action itself.
35 */
36 public static final int ACTION_MASK = 0xff;
37
38 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 * Constant for {@link #getAction}: A pressed gesture has started, the
40 * motion contains the initial starting location.
41 */
42 public static final int ACTION_DOWN = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 /**
45 * Constant for {@link #getAction}: A pressed gesture has finished, the
46 * motion contains the final release location as well as any intermediate
47 * points since the last down or move event.
48 */
49 public static final int ACTION_UP = 1;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 /**
52 * Constant for {@link #getAction}: A change has happened during a
53 * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
54 * The motion contains the most recent point, as well as any intermediate
55 * points since the last down or move event.
56 */
57 public static final int ACTION_MOVE = 2;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 /**
60 * Constant for {@link #getAction}: The current gesture has been aborted.
61 * You will not receive any more points in it. You should treat this as
62 * an up event, but not perform any action that you normally would.
63 */
64 public static final int ACTION_CANCEL = 3;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 /**
67 * Constant for {@link #getAction}: A movement has happened outside of the
68 * normal bounds of the UI element. This does not provide a full gesture,
69 * but only the initial location of the movement/touch.
70 */
71 public static final int ACTION_OUTSIDE = 4;
72
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070073 /**
74 * A non-primary pointer has gone down. The bits in
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070075 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070076 */
77 public static final int ACTION_POINTER_DOWN = 5;
78
79 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070080 * A non-primary pointer has gone up. The bits in
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070081 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070082 */
83 public static final int ACTION_POINTER_UP = 6;
84
85 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -080086 * Bits in the action code that represent a pointer index, used with
87 * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Shifting
88 * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer
89 * index where the data for the pointer going up or down can be found; you can
90 * get its identifier with {@link #getPointerId(int)} and the actual
91 * data with {@link #getX(int)} etc.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -070092 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -080093 public static final int ACTION_POINTER_INDEX_MASK = 0xff00;
94
95 /**
96 * Bit shift for the action bits holding the pointer index as
97 * defined by {@link #ACTION_POINTER_INDEX_MASK}.
98 */
99 public static final int ACTION_POINTER_INDEX_SHIFT = 8;
100
101 /**
102 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
103 * data index associated with {@link #ACTION_POINTER_DOWN}.
104 */
105 @Deprecated
106 public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000;
107
108 /**
109 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
110 * data index associated with {@link #ACTION_POINTER_DOWN}.
111 */
112 @Deprecated
113 public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100;
114
115 /**
116 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
117 * data index associated with {@link #ACTION_POINTER_DOWN}.
118 */
119 @Deprecated
120 public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200;
121
122 /**
123 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
124 * data index associated with {@link #ACTION_POINTER_UP}.
125 */
126 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700127 public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000;
128
129 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800130 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
131 * data index associated with {@link #ACTION_POINTER_UP}.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700132 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800133 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700134 public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100;
135
136 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800137 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
138 * data index associated with {@link #ACTION_POINTER_UP}.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700139 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800140 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700141 public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200;
142
143 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800144 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match
145 * the actual data contained in these bits.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700146 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800147 @Deprecated
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700148 public static final int ACTION_POINTER_ID_MASK = 0xff00;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700149
150 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800151 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match
152 * the actual data contained in these bits.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700153 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800154 @Deprecated
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700155 public static final int ACTION_POINTER_ID_SHIFT = 8;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 private static final boolean TRACK_RECYCLED_LOCATION = false;
Romain Guycafdea62009-06-12 10:51:36 -0700158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 /**
160 * Flag indicating the motion event intersected the top edge of the screen.
161 */
162 public static final int EDGE_TOP = 0x00000001;
Romain Guycafdea62009-06-12 10:51:36 -0700163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 /**
165 * Flag indicating the motion event intersected the bottom edge of the screen.
166 */
167 public static final int EDGE_BOTTOM = 0x00000002;
Romain Guycafdea62009-06-12 10:51:36 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 /**
170 * Flag indicating the motion event intersected the left edge of the screen.
171 */
172 public static final int EDGE_LEFT = 0x00000004;
Romain Guycafdea62009-06-12 10:51:36 -0700173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 /**
175 * Flag indicating the motion event intersected the right edge of the screen.
176 */
177 public static final int EDGE_RIGHT = 0x00000008;
Romain Guycafdea62009-06-12 10:51:36 -0700178
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700179 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700180 * Offset for the sample's X coordinate.
181 * @hide
182 */
183 static public final int SAMPLE_X = 0;
184
185 /**
186 * Offset for the sample's Y coordinate.
187 * @hide
188 */
189 static public final int SAMPLE_Y = 1;
190
191 /**
192 * Offset for the sample's X coordinate.
193 * @hide
194 */
195 static public final int SAMPLE_PRESSURE = 2;
196
197 /**
198 * Offset for the sample's X coordinate.
199 * @hide
200 */
201 static public final int SAMPLE_SIZE = 3;
202
203 /**
204 * Number of data items for each sample.
205 * @hide
206 */
207 static public final int NUM_SAMPLE_DATA = 4;
208
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700209 /**
210 * Number of possible pointers.
211 * @hide
212 */
213 static public final int BASE_AVAIL_POINTERS = 5;
214
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700215 static private final int BASE_AVAIL_SAMPLES = 8;
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 static private final int MAX_RECYCLED = 10;
218 static private Object gRecyclerLock = new Object();
219 static private int gRecyclerUsed = 0;
220 static private MotionEvent gRecyclerTop = null;
Romain Guycafdea62009-06-12 10:51:36 -0700221
Jeff Brown5c225b12010-06-16 01:53:36 -0700222 private long mDownTimeNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 private int mAction;
Jeff Brown5c225b12010-06-16 01:53:36 -0700224 private float mXOffset;
225 private float mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 private float mXPrecision;
227 private float mYPrecision;
228 private int mDeviceId;
229 private int mEdgeFlags;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700230 private int mMetaState;
231
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700232 private int mNumPointers;
233 private int mNumSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -0700234
235 private int mLastDataSampleIndex;
236 private int mLastEventTimeNanoSampleIndex;
237
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700238 // Array of mNumPointers size of identifiers for each pointer of data.
239 private int[] mPointerIdentifiers;
Jeff Brown5c225b12010-06-16 01:53:36 -0700240
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700241 // Array of (mNumSamples * mNumPointers * NUM_SAMPLE_DATA) size of event data.
Jeff Brown5c225b12010-06-16 01:53:36 -0700242 // Samples are ordered from oldest to newest.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700243 private float[] mDataSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -0700244
245 // Array of mNumSamples size of event time stamps in nanoseconds.
246 // Samples are ordered from oldest to newest.
247 private long[] mEventTimeNanoSamples;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 private MotionEvent mNext;
250 private RuntimeException mRecycledLocation;
251 private boolean mRecycled;
252
Jeff Brown46b9ac02010-04-22 18:58:52 -0700253 private MotionEvent(int pointerCount, int sampleCount) {
254 mPointerIdentifiers = new int[pointerCount];
255 mDataSamples = new float[pointerCount * sampleCount * NUM_SAMPLE_DATA];
Jeff Brown5c225b12010-06-16 01:53:36 -0700256 mEventTimeNanoSamples = new long[sampleCount];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 }
Romain Guycafdea62009-06-12 10:51:36 -0700258
Jeff Brown46b9ac02010-04-22 18:58:52 -0700259 static private MotionEvent obtain(int pointerCount, int sampleCount) {
260 final MotionEvent ev;
261 synchronized (gRecyclerLock) {
262 if (gRecyclerTop == null) {
263 if (pointerCount < BASE_AVAIL_POINTERS) {
264 pointerCount = BASE_AVAIL_POINTERS;
265 }
266 if (sampleCount < BASE_AVAIL_SAMPLES) {
267 sampleCount = BASE_AVAIL_SAMPLES;
268 }
269 return new MotionEvent(pointerCount, sampleCount);
270 }
271 ev = gRecyclerTop;
272 gRecyclerTop = ev.mNext;
Jeff Brown5c225b12010-06-16 01:53:36 -0700273 gRecyclerUsed -= 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700274 }
275 ev.mRecycledLocation = null;
276 ev.mRecycled = false;
277 ev.mNext = null;
278
279 if (ev.mPointerIdentifiers.length < pointerCount) {
280 ev.mPointerIdentifiers = new int[pointerCount];
281 }
282
Jeff Brown5c225b12010-06-16 01:53:36 -0700283 if (ev.mEventTimeNanoSamples.length < sampleCount) {
284 ev.mEventTimeNanoSamples = new long[sampleCount];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700285 }
286
Jeff Brown46b9ac02010-04-22 18:58:52 -0700287 final int neededDataSamplesLength = pointerCount * sampleCount * NUM_SAMPLE_DATA;
Jeff Brown5c225b12010-06-16 01:53:36 -0700288 if (ev.mDataSamples.length < neededDataSamplesLength) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700289 ev.mDataSamples = new float[neededDataSamplesLength];
290 }
291
292 return ev;
293 }
Jeff Brown5c225b12010-06-16 01:53:36 -0700294
Michael Chan53071d62009-05-13 17:29:48 -0700295 /**
296 * Create a new MotionEvent, filling in all of the basic values that
297 * define the motion.
298 *
299 * @param downTime The time (in ms) when the user originally pressed down to start
300 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
301 * @param eventTime The the time (in ms) when this specific event was generated. This
302 * must be obtained from {@link SystemClock#uptimeMillis()}.
303 * @param eventTimeNano The the time (in ns) when this specific event was generated. This
304 * must be obtained from {@link System#nanoTime()}.
305 * @param action The kind of action being performed -- one of either
306 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
307 * {@link #ACTION_CANCEL}.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700308 * @param pointers The number of points that will be in this event.
309 * @param inPointerIds An array of <em>pointers</em> values providing
310 * an identifier for each pointer.
311 * @param inData An array of <em>pointers*NUM_SAMPLE_DATA</em> of initial
312 * data samples for the event.
Michael Chan53071d62009-05-13 17:29:48 -0700313 * @param metaState The state of any meta / modifier keys that were in effect when
314 * the event was generated.
315 * @param xPrecision The precision of the X coordinate being reported.
316 * @param yPrecision The precision of the Y coordinate being reported.
317 * @param deviceId The id for the device that this event came from. An id of
318 * zero indicates that the event didn't come from a physical device; other
319 * numbers are arbitrary and you shouldn't depend on the values.
320 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
321 * MotionEvent.
322 *
323 * @hide
324 */
325 static public MotionEvent obtainNano(long downTime, long eventTime, long eventTimeNano,
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700326 int action, int pointers, int[] inPointerIds, float[] inData, int metaState,
Michael Chan53071d62009-05-13 17:29:48 -0700327 float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700328 MotionEvent ev = obtain(pointers, 1);
Michael Chan53071d62009-05-13 17:29:48 -0700329 ev.mDeviceId = deviceId;
330 ev.mEdgeFlags = edgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700331 ev.mDownTimeNano = downTime * MS_PER_NS;
Michael Chan53071d62009-05-13 17:29:48 -0700332 ev.mAction = action;
Michael Chan53071d62009-05-13 17:29:48 -0700333 ev.mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700334 ev.mXOffset = 0;
335 ev.mYOffset = 0;
Michael Chan53071d62009-05-13 17:29:48 -0700336 ev.mXPrecision = xPrecision;
337 ev.mYPrecision = yPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700338
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700339 ev.mNumPointers = pointers;
340 ev.mNumSamples = 1;
341
Jeff Brown5c225b12010-06-16 01:53:36 -0700342 ev.mLastDataSampleIndex = 0;
343 ev.mLastEventTimeNanoSampleIndex = 0;
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700344
Jeff Brown5c225b12010-06-16 01:53:36 -0700345 System.arraycopy(inPointerIds, 0, ev.mPointerIdentifiers, 0, pointers);
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700346
Jeff Brown5c225b12010-06-16 01:53:36 -0700347 ev.mEventTimeNanoSamples[0] = eventTimeNano;
348
349 System.arraycopy(inData, 0, ev.mDataSamples, 0, pointers * NUM_SAMPLE_DATA);
Michael Chan53071d62009-05-13 17:29:48 -0700350
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700351 if (DEBUG_POINTERS) {
352 StringBuilder sb = new StringBuilder(128);
353 sb.append("New:");
Jeff Brown5c225b12010-06-16 01:53:36 -0700354 for (int i = 0; i < pointers; i++) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700355 sb.append(" #");
Jeff Brown5c225b12010-06-16 01:53:36 -0700356 sb.append(ev.getPointerId(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700357 sb.append("(");
Jeff Brown5c225b12010-06-16 01:53:36 -0700358 sb.append(ev.getX(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700359 sb.append(",");
Jeff Brown5c225b12010-06-16 01:53:36 -0700360 sb.append(ev.getY(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700361 sb.append(")");
362 }
363 Log.v("MotionEvent", sb.toString());
364 }
365
Michael Chan53071d62009-05-13 17:29:48 -0700366 return ev;
367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
369 /**
370 * Create a new MotionEvent, filling in all of the basic values that
371 * define the motion.
Romain Guycafdea62009-06-12 10:51:36 -0700372 *
373 * @param downTime The time (in ms) when the user originally pressed down to start
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
Romain Guycafdea62009-06-12 10:51:36 -0700375 * @param eventTime The the time (in ms) when this specific event was generated. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * must be obtained from {@link SystemClock#uptimeMillis()}.
377 * @param action The kind of action being performed -- one of either
378 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
379 * {@link #ACTION_CANCEL}.
380 * @param x The X coordinate of this event.
381 * @param y The Y coordinate of this event.
Romain Guycafdea62009-06-12 10:51:36 -0700382 * @param pressure The current pressure of this event. The pressure generally
383 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
384 * values higher than 1 may be generated depending on the calibration of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * the input device.
386 * @param size A scaled value of the approximate size of the area being pressed when
Romain Guycafdea62009-06-12 10:51:36 -0700387 * touched with the finger. The actual value in pixels corresponding to the finger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * touch is normalized with a device specific range of values
389 * and scaled to a value between 0 and 1.
390 * @param metaState The state of any meta / modifier keys that were in effect when
391 * the event was generated.
392 * @param xPrecision The precision of the X coordinate being reported.
393 * @param yPrecision The precision of the Y coordinate being reported.
394 * @param deviceId The id for the device that this event came from. An id of
395 * zero indicates that the event didn't come from a physical device; other
396 * numbers are arbitrary and you shouldn't depend on the values.
397 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
398 * MotionEvent.
399 */
400 static public MotionEvent obtain(long downTime, long eventTime, int action,
401 float x, float y, float pressure, float size, int metaState,
402 float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700403 MotionEvent ev = obtain(1, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 ev.mDeviceId = deviceId;
405 ev.mEdgeFlags = edgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700406 ev.mDownTimeNano = downTime * MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 ev.mAction = action;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408 ev.mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700409 ev.mXOffset = 0;
410 ev.mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 ev.mXPrecision = xPrecision;
412 ev.mYPrecision = yPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700413
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700414 ev.mNumPointers = 1;
415 ev.mNumSamples = 1;
Jeff Brown5c225b12010-06-16 01:53:36 -0700416
417 ev.mLastDataSampleIndex = 0;
418 ev.mLastEventTimeNanoSampleIndex = 0;
419
420 ev.mPointerIdentifiers[0] = 0;
421
422 ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS;
423
424 float[] dataSamples = ev.mDataSamples;
425 dataSamples[SAMPLE_X] = x;
426 dataSamples[SAMPLE_Y] = y;
427 dataSamples[SAMPLE_PRESSURE] = pressure;
428 dataSamples[SAMPLE_SIZE] = size;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700429 return ev;
430 }
431
432 /**
433 * Create a new MotionEvent, filling in all of the basic values that
434 * define the motion.
435 *
436 * @param downTime The time (in ms) when the user originally pressed down to start
437 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
438 * @param eventTime The the time (in ms) when this specific event was generated. This
439 * must be obtained from {@link SystemClock#uptimeMillis()}.
440 * @param action The kind of action being performed -- one of either
441 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
442 * {@link #ACTION_CANCEL}.
443 * @param pointers The number of pointers that are active in this event.
444 * @param x The X coordinate of this event.
445 * @param y The Y coordinate of this event.
446 * @param pressure The current pressure of this event. The pressure generally
447 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
448 * values higher than 1 may be generated depending on the calibration of
449 * the input device.
450 * @param size A scaled value of the approximate size of the area being pressed when
451 * touched with the finger. The actual value in pixels corresponding to the finger
452 * touch is normalized with a device specific range of values
453 * and scaled to a value between 0 and 1.
454 * @param metaState The state of any meta / modifier keys that were in effect when
455 * the event was generated.
456 * @param xPrecision The precision of the X coordinate being reported.
457 * @param yPrecision The precision of the Y coordinate being reported.
458 * @param deviceId The id for the device that this event came from. An id of
459 * zero indicates that the event didn't come from a physical device; other
460 * numbers are arbitrary and you shouldn't depend on the values.
461 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this
462 * MotionEvent.
Jeff Brown5c225b12010-06-16 01:53:36 -0700463 *
464 * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)}
465 * instead.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700466 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700467 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700468 static public MotionEvent obtain(long downTime, long eventTime, int action,
469 int pointers, float x, float y, float pressure, float size, int metaState,
470 float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700471 return obtain(downTime, eventTime, action, x, y, pressure, size,
472 metaState, xPrecision, yPrecision, deviceId, edgeFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 }
Romain Guycafdea62009-06-12 10:51:36 -0700474
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 /**
476 * Create a new MotionEvent, filling in a subset of the basic motion
477 * values. Those not specified here are: device id (always 0), pressure
478 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
Romain Guycafdea62009-06-12 10:51:36 -0700479 *
480 * @param downTime The time (in ms) when the user originally pressed down to start
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
Romain Guycafdea62009-06-12 10:51:36 -0700482 * @param eventTime The the time (in ms) when this specific event was generated. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 * must be obtained from {@link SystemClock#uptimeMillis()}.
484 * @param action The kind of action being performed -- one of either
485 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
486 * {@link #ACTION_CANCEL}.
487 * @param x The X coordinate of this event.
488 * @param y The Y coordinate of this event.
489 * @param metaState The state of any meta / modifier keys that were in effect when
490 * the event was generated.
491 */
492 static public MotionEvent obtain(long downTime, long eventTime, int action,
493 float x, float y, int metaState) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700494 return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f,
495 metaState, 1.0f, 1.0f, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700496 }
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
499 * Create a new MotionEvent, copying from an existing one.
500 */
501 static public MotionEvent obtain(MotionEvent o) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700502 MotionEvent ev = obtain(o.mNumPointers, o.mNumSamples);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 ev.mDeviceId = o.mDeviceId;
504 ev.mEdgeFlags = o.mEdgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700505 ev.mDownTimeNano = o.mDownTimeNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 ev.mAction = o.mAction;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 ev.mMetaState = o.mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700508 ev.mXOffset = o.mXOffset;
509 ev.mYOffset = o.mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800510 ev.mXPrecision = o.mXPrecision;
511 ev.mYPrecision = o.mYPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700512 int numPointers = ev.mNumPointers = o.mNumPointers;
513 int numSamples = ev.mNumSamples = o.mNumSamples;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700514
Jeff Brown5c225b12010-06-16 01:53:36 -0700515 ev.mLastDataSampleIndex = o.mLastDataSampleIndex;
516 ev.mLastEventTimeNanoSampleIndex = o.mLastEventTimeNanoSampleIndex;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700517
Jeff Brown5c225b12010-06-16 01:53:36 -0700518 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700519
Jeff Brown5c225b12010-06-16 01:53:36 -0700520 System.arraycopy(o.mEventTimeNanoSamples, 0, ev.mEventTimeNanoSamples, 0, numSamples);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700521
Jeff Brown5c225b12010-06-16 01:53:36 -0700522 System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0,
523 numPointers * numSamples * NUM_SAMPLE_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 return ev;
525 }
Romain Guycafdea62009-06-12 10:51:36 -0700526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700528 * Create a new MotionEvent, copying from an existing one, but not including
529 * any historical point information.
530 */
531 static public MotionEvent obtainNoHistory(MotionEvent o) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700532 MotionEvent ev = obtain(o.mNumPointers, 1);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700533 ev.mDeviceId = o.mDeviceId;
534 ev.mEdgeFlags = o.mEdgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700535 ev.mDownTimeNano = o.mDownTimeNano;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700536 ev.mAction = o.mAction;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700537 ev.mMetaState = o.mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700538 ev.mXOffset = o.mXOffset;
539 ev.mYOffset = o.mYOffset;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700540 ev.mXPrecision = o.mXPrecision;
541 ev.mYPrecision = o.mYPrecision;
542
Jeff Brown5c225b12010-06-16 01:53:36 -0700543 int numPointers = ev.mNumPointers = o.mNumPointers;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700544 ev.mNumSamples = 1;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700545
Jeff Brown5c225b12010-06-16 01:53:36 -0700546 ev.mLastDataSampleIndex = 0;
547 ev.mLastEventTimeNanoSampleIndex = 0;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700548
Jeff Brown5c225b12010-06-16 01:53:36 -0700549 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700550
Jeff Brown5c225b12010-06-16 01:53:36 -0700551 ev.mEventTimeNanoSamples[0] = o.mEventTimeNanoSamples[o.mLastEventTimeNanoSampleIndex];
552
553 System.arraycopy(o.mDataSamples, o.mLastDataSampleIndex, ev.mDataSamples, 0,
554 numPointers * NUM_SAMPLE_DATA);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700555 return ev;
556 }
557
558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 * Recycle the MotionEvent, to be re-used by a later caller. After calling
560 * this function you must not ever touch the event again.
561 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700562 public final void recycle() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 // Ensure recycle is only called once!
564 if (TRACK_RECYCLED_LOCATION) {
565 if (mRecycledLocation != null) {
566 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
567 }
568 mRecycledLocation = new RuntimeException("Last recycled here");
Jeff Brownd28f4be2010-06-02 15:35:46 -0700569 //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation);
570 } else {
571 if (mRecycled) {
572 throw new RuntimeException(toString() + " recycled twice!");
573 }
574 mRecycled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 synchronized (gRecyclerLock) {
578 if (gRecyclerUsed < MAX_RECYCLED) {
579 gRecyclerUsed++;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700580 mNumSamples = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 mNext = gRecyclerTop;
582 gRecyclerTop = this;
583 }
584 }
585 }
Jeff Brown5c225b12010-06-16 01:53:36 -0700586
587 /**
588 * Scales down the coordination of this event by the given scale.
589 *
590 * @hide
591 */
592 public final void scale(float scale) {
593 mXOffset *= scale;
594 mYOffset *= scale;
595 mXPrecision *= scale;
596 mYPrecision *= scale;
597
598 float[] history = mDataSamples;
599 final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA;
600 for (int i = 0; i < length; i += NUM_SAMPLE_DATA) {
601 history[i + SAMPLE_X] *= scale;
602 history[i + SAMPLE_Y] *= scale;
603 // no need to scale pressure
604 history[i + SAMPLE_SIZE] *= scale; // TODO: square this?
605 }
606 }
Romain Guycafdea62009-06-12 10:51:36 -0700607
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 /**
609 * Return the kind of action being performed -- one of either
610 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800611 * {@link #ACTION_CANCEL}. Consider using {@link #getActionMasked}
612 * and {@link #getActionIndex} to retrieve the separate masked action
613 * and pointer index.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 */
615 public final int getAction() {
616 return mAction;
617 }
618
619 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800620 * Return the masked action being performed, without pointer index
621 * information. May be any of the actions: {@link #ACTION_DOWN},
622 * {@link #ACTION_MOVE}, {@link #ACTION_UP}, {@link #ACTION_CANCEL},
623 * {@link #ACTION_POINTER_DOWN}, or {@link #ACTION_POINTER_UP}.
624 * Use {@link #getActionIndex} to return the index associated with
625 * pointer actions.
626 */
627 public final int getActionMasked() {
628 return mAction & ACTION_MASK;
629 }
630
631 /**
632 * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP}
633 * as returned by {@link #getActionMasked}, this returns the associated
634 * pointer index. The index may be used with {@link #getPointerId(int)},
635 * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)},
636 * and {@link #getSize(int)} to get information about the pointer that has
637 * gone down or up.
638 */
639 public final int getActionIndex() {
640 return (mAction & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
641 }
642
643 /**
Romain Guycafdea62009-06-12 10:51:36 -0700644 * Returns the time (in ms) when the user originally pressed down to start
645 * a stream of position events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800646 */
647 public final long getDownTime() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700648 return mDownTimeNano / MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 }
650
651 /**
652 * Returns the time (in ms) when this specific event was generated.
653 */
654 public final long getEventTime() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700655 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] / MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 }
657
658 /**
Michael Chan53071d62009-05-13 17:29:48 -0700659 * Returns the time (in ns) when this specific event was generated.
660 * The value is in nanosecond precision but it may not have nanosecond accuracy.
661 *
662 * @hide
663 */
664 public final long getEventTimeNano() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700665 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex];
Michael Chan53071d62009-05-13 17:29:48 -0700666 }
667
668 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700669 * {@link #getX(int)} for the first pointer index (may be an
670 * arbitrary pointer identifier).
671 */
672 public final float getX() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700673 return mDataSamples[mLastDataSampleIndex + SAMPLE_X] + mXOffset;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700674 }
675
676 /**
677 * {@link #getY(int)} for the first pointer index (may be an
678 * arbitrary pointer identifier).
679 */
680 public final float getY() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700681 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y] + mYOffset;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700682 }
683
684 /**
685 * {@link #getPressure(int)} for the first pointer index (may be an
686 * arbitrary pointer identifier).
687 */
688 public final float getPressure() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700689 return mDataSamples[mLastDataSampleIndex + SAMPLE_PRESSURE];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700690 }
691
692 /**
693 * {@link #getSize(int)} for the first pointer index (may be an
694 * arbitrary pointer identifier).
695 */
696 public final float getSize() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700697 return mDataSamples[mLastDataSampleIndex + SAMPLE_SIZE];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700698 }
699
700 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700701 * The number of pointers of data contained in this event. Always
702 * >= 1.
703 */
704 public final int getPointerCount() {
705 return mNumPointers;
706 }
707
708 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700709 * Return the pointer identifier associated with a particular pointer
710 * data index is this event. The identifier tells you the actual pointer
711 * number associated with the data, accounting for individual pointers
712 * going up and down since the start of the current gesture.
713 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
714 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 */
Dianne Hackbornd41ba662009-08-05 15:30:56 -0700716 public final int getPointerId(int pointerIndex) {
717 return mPointerIdentifiers[pointerIndex];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700721 * Given a pointer identifier, find the index of its data in the event.
722 *
723 * @param pointerId The identifier of the pointer to be found.
724 * @return Returns either the index of the pointer (for use with
725 * {@link #getX(int) et al.), or -1 if there is no data available for
726 * that pointer identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700728 public final int findPointerIndex(int pointerId) {
729 int i = mNumPointers;
730 while (i > 0) {
731 i--;
732 if (mPointerIdentifiers[i] == pointerId) {
733 return i;
734 }
735 }
736 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700740 * Returns the X coordinate of this event for the given pointer
741 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
742 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700743 * Whole numbers are pixels; the
744 * value may have a fraction for input devices that are sub-pixel precise.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700745 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
746 * (the first pointer that is down) to {@link #getPointerCount()}-1.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700747 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700748 public final float getX(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700749 return mDataSamples[mLastDataSampleIndex
750 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700751 }
752
753 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700754 * Returns the Y coordinate of this event for the given pointer
755 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
756 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700757 * Whole numbers are pixels; the
758 * value may have a fraction for input devices that are sub-pixel precise.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700759 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
760 * (the first pointer that is down) to {@link #getPointerCount()}-1.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700761 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700762 public final float getY(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700763 return mDataSamples[mLastDataSampleIndex
764 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700765 }
766
767 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700768 * Returns the current pressure of this event for the given pointer
769 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
770 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700771 * The pressure generally
Romain Guycafdea62009-06-12 10:51:36 -0700772 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
773 * values higher than 1 may be generated depending on the calibration of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 * the input device.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700775 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
776 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700778 public final float getPressure(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700779 return mDataSamples[mLastDataSampleIndex
780 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800781 }
782
783 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700784 * Returns a scaled value of the approximate size for the given pointer
785 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
786 * identifier for this index).
787 * This represents some approximation of the area of the screen being
788 * pressed; the actual value in pixels corresponding to the
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700789 * touch is normalized with the device specific range of values
Romain Guycafdea62009-06-12 10:51:36 -0700790 * and scaled to a value between 0 and 1. The value of size can be used to
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 * determine fat touch events.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700792 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
793 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700795 public final float getSize(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700796 return mDataSamples[mLastDataSampleIndex
797 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 }
799
800 /**
801 * Returns the state of any meta / modifier keys that were in effect when
802 * the event was generated. This is the same values as those
803 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}.
804 *
805 * @return an integer in which each bit set to 1 represents a pressed
806 * meta key
807 *
808 * @see KeyEvent#getMetaState()
809 */
810 public final int getMetaState() {
811 return mMetaState;
812 }
813
814 /**
815 * Returns the original raw X coordinate of this event. For touch
816 * events on the screen, this is the original location of the event
817 * on the screen, before it had been adjusted for the containing window
818 * and views.
819 */
820 public final float getRawX() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700821 return mDataSamples[mLastDataSampleIndex + SAMPLE_X];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823
824 /**
825 * Returns the original raw Y coordinate of this event. For touch
826 * events on the screen, this is the original location of the event
827 * on the screen, before it had been adjusted for the containing window
828 * and views.
829 */
830 public final float getRawY() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700831 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
833
834 /**
835 * Return the precision of the X coordinates being reported. You can
836 * multiple this number with {@link #getX} to find the actual hardware
837 * value of the X coordinate.
838 * @return Returns the precision of X coordinates being reported.
839 */
840 public final float getXPrecision() {
841 return mXPrecision;
842 }
Romain Guycafdea62009-06-12 10:51:36 -0700843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 /**
845 * Return the precision of the Y coordinates being reported. You can
846 * multiple this number with {@link #getY} to find the actual hardware
847 * value of the Y coordinate.
848 * @return Returns the precision of Y coordinates being reported.
849 */
850 public final float getYPrecision() {
851 return mYPrecision;
852 }
Romain Guycafdea62009-06-12 10:51:36 -0700853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 /**
855 * Returns the number of historical points in this event. These are
856 * movements that have occurred between this event and the previous event.
857 * This only applies to ACTION_MOVE events -- all other actions will have
858 * a size of 0.
Romain Guycafdea62009-06-12 10:51:36 -0700859 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 * @return Returns the number of historical points in the event.
861 */
862 public final int getHistorySize() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700863 return mLastEventTimeNanoSampleIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 }
Romain Guycafdea62009-06-12 10:51:36 -0700865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 /**
867 * Returns the time that a historical movement occurred between this event
868 * and the previous event. Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -0700869 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 * @param pos Which historical value to return; must be less than
871 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -0700872 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 * @see #getHistorySize
874 * @see #getEventTime
875 */
876 public final long getHistoricalEventTime(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700877 return mEventTimeNanoSamples[pos] / MS_PER_NS;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700878 }
879
880 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700881 * {@link #getHistoricalX(int)} for the first pointer index (may be an
882 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700883 */
884 public final float getHistoricalX(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700885 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700886 }
887
888 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700889 * {@link #getHistoricalY(int)} for the first pointer index (may be an
890 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700891 */
892 public final float getHistoricalY(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700893 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700894 }
895
896 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700897 * {@link #getHistoricalPressure(int)} for the first pointer index (may be an
898 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700899 */
900 public final float getHistoricalPressure(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700901 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700902 }
903
904 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700905 * {@link #getHistoricalSize(int)} for the first pointer index (may be an
906 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700907 */
908 public final float getHistoricalSize(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700909 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 }
Romain Guycafdea62009-06-12 10:51:36 -0700911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700913 * Returns a historical X coordinate, as per {@link #getX(int)}, that
914 * occurred between this event and the previous event for the given pointer.
915 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -0700916 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700917 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
918 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 * @param pos Which historical value to return; must be less than
920 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -0700921 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 * @see #getHistorySize
923 * @see #getX
924 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700925 public final float getHistoricalX(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700926 return mDataSamples[(pos * mNumPointers + pointerIndex)
927 * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
Romain Guycafdea62009-06-12 10:51:36 -0700929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700931 * Returns a historical Y coordinate, as per {@link #getY(int)}, that
932 * occurred between this event and the previous event for the given pointer.
933 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -0700934 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700935 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
936 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 * @param pos Which historical value to return; must be less than
938 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -0700939 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 * @see #getHistorySize
941 * @see #getY
942 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700943 public final float getHistoricalY(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700944 return mDataSamples[(pos * mNumPointers + pointerIndex)
945 * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800946 }
Romain Guycafdea62009-06-12 10:51:36 -0700947
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700949 * Returns a historical pressure coordinate, as per {@link #getPressure(int)},
950 * that occurred between this event and the previous event for the given
951 * pointer. Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -0700952 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700953 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
954 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 * @param pos Which historical value to return; must be less than
956 * {@link #getHistorySize}
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700957 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 * @see #getHistorySize
959 * @see #getPressure
960 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700961 public final float getHistoricalPressure(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700962 return mDataSamples[(pos * mNumPointers + pointerIndex)
963 * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 }
Romain Guycafdea62009-06-12 10:51:36 -0700965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700967 * Returns a historical size coordinate, as per {@link #getSize(int)}, that
968 * occurred between this event and the previous event for the given pointer.
969 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -0700970 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700971 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
972 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 * @param pos Which historical value to return; must be less than
974 * {@link #getHistorySize}
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700975 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 * @see #getHistorySize
977 * @see #getSize
978 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700979 public final float getHistoricalSize(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700980 return mDataSamples[(pos * mNumPointers + pointerIndex)
981 * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
Romain Guycafdea62009-06-12 10:51:36 -0700983
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800984 /**
985 * Return the id for the device that this event came from. An id of
986 * zero indicates that the event didn't come from a physical device; other
987 * numbers are arbitrary and you shouldn't depend on the values.
988 */
989 public final int getDeviceId() {
990 return mDeviceId;
991 }
Romain Guycafdea62009-06-12 10:51:36 -0700992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 /**
Jeff Brown46b9ac02010-04-22 18:58:52 -0700994 * Returns a bitfield indicating which edges, if any, were touched by this
Romain Guycafdea62009-06-12 10:51:36 -0700995 * MotionEvent. For touch events, clients can use this to determine if the
996 * user's finger was touching the edge of the display.
997 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 * @see #EDGE_LEFT
999 * @see #EDGE_TOP
1000 * @see #EDGE_RIGHT
1001 * @see #EDGE_BOTTOM
1002 */
1003 public final int getEdgeFlags() {
1004 return mEdgeFlags;
1005 }
Romain Guycafdea62009-06-12 10:51:36 -07001006
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007
1008 /**
1009 * Sets the bitfield indicating which edges, if any, where touched by this
Romain Guycafdea62009-06-12 10:51:36 -07001010 * MotionEvent.
1011 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 * @see #getEdgeFlags()
1013 */
1014 public final void setEdgeFlags(int flags) {
1015 mEdgeFlags = flags;
1016 }
1017
1018 /**
1019 * Sets this event's action.
1020 */
1021 public final void setAction(int action) {
1022 mAction = action;
1023 }
1024
1025 /**
1026 * Adjust this event's location.
1027 * @param deltaX Amount to add to the current X coordinate of the event.
1028 * @param deltaY Amount to add to the current Y coordinate of the event.
1029 */
1030 public final void offsetLocation(float deltaX, float deltaY) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001031 mXOffset += deltaX;
1032 mYOffset += deltaY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 }
Romain Guycafdea62009-06-12 10:51:36 -07001034
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
1036 * Set this event's location. Applies {@link #offsetLocation} with a
1037 * delta from the current location to the given new location.
Romain Guycafdea62009-06-12 10:51:36 -07001038 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 * @param x New absolute X location.
1040 * @param y New absolute Y location.
1041 */
1042 public final void setLocation(float x, float y) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001043 mXOffset = x - mDataSamples[mLastDataSampleIndex + SAMPLE_X];
1044 mYOffset = y - mDataSamples[mLastDataSampleIndex + SAMPLE_Y];
1045 }
1046
1047 private final void incrementNumSamplesAndReserveStorage(int dataSampleStride) {
1048 if (mNumSamples == mEventTimeNanoSamples.length) {
1049 long[] newEventTimeNanoSamples = new long[mNumSamples + BASE_AVAIL_SAMPLES];
1050 System.arraycopy(mEventTimeNanoSamples, 0, newEventTimeNanoSamples, 0, mNumSamples);
1051 mEventTimeNanoSamples = newEventTimeNanoSamples;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
Jeff Brown5c225b12010-06-16 01:53:36 -07001053
1054 int nextDataSampleIndex = mLastDataSampleIndex + dataSampleStride;
1055 if (nextDataSampleIndex + dataSampleStride > mDataSamples.length) {
1056 float[] newDataSamples = new float[nextDataSampleIndex
1057 + BASE_AVAIL_SAMPLES * dataSampleStride];
1058 System.arraycopy(mDataSamples, 0, newDataSamples, 0, nextDataSampleIndex);
1059 mDataSamples = newDataSamples;
1060 }
1061
1062 mLastEventTimeNanoSampleIndex = mNumSamples;
1063 mLastDataSampleIndex = nextDataSampleIndex;
1064 mNumSamples += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
Romain Guycafdea62009-06-12 10:51:36 -07001066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001067 /**
1068 * Add a new movement to the batch of movements in this event. The event's
1069 * current location, position and size is updated to the new values. In
1070 * the future, the current values in the event will be added to a list of
1071 * historic values.
Romain Guycafdea62009-06-12 10:51:36 -07001072 *
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001073 * @param eventTime The time stamp for this data.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 * @param x The new X position.
1075 * @param y The new Y position.
1076 * @param pressure The new pressure.
1077 * @param size The new size.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001078 * @param metaState Meta key state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 */
1080 public final void addBatch(long eventTime, float x, float y,
1081 float pressure, float size, int metaState) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001082 incrementNumSamplesAndReserveStorage(NUM_SAMPLE_DATA);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001083
Jeff Brown5c225b12010-06-16 01:53:36 -07001084 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001085
Jeff Brown5c225b12010-06-16 01:53:36 -07001086 float[] dataSamples = mDataSamples;
1087 dataSamples[mLastDataSampleIndex + SAMPLE_X] = x - mXOffset;
1088 dataSamples[mLastDataSampleIndex + SAMPLE_Y] = y - mYOffset;
1089 dataSamples[mLastDataSampleIndex + SAMPLE_PRESSURE] = pressure;
1090 dataSamples[mLastDataSampleIndex + SAMPLE_SIZE] = size;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001091
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001092 mMetaState |= metaState;
1093 }
Romain Guycafdea62009-06-12 10:51:36 -07001094
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001095 /**
1096 * Add a new movement to the batch of movements in this event. The
1097 * input data must contain (NUM_SAMPLE_DATA * {@link #getPointerCount()})
1098 * samples of data.
1099 *
1100 * @param eventTime The time stamp for this data.
1101 * @param inData The actual data.
1102 * @param metaState Meta key state.
1103 *
1104 * @hide
1105 */
1106 public final void addBatch(long eventTime, float[] inData, int metaState) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001107 final int numPointers = mNumPointers;
1108 final int dataSampleStride = numPointers * NUM_SAMPLE_DATA;
1109 incrementNumSamplesAndReserveStorage(dataSampleStride);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001110
Jeff Brown5c225b12010-06-16 01:53:36 -07001111 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001112
Jeff Brown5c225b12010-06-16 01:53:36 -07001113 float[] dataSamples = mDataSamples;
1114 System.arraycopy(inData, 0, dataSamples, mLastDataSampleIndex, dataSampleStride);
Romain Guycafdea62009-06-12 10:51:36 -07001115
Jeff Brown5c225b12010-06-16 01:53:36 -07001116 if (mXOffset != 0 || mYOffset != 0) {
1117 int index = mLastEventTimeNanoSampleIndex;
1118 for (int i = 0; i < numPointers; i++) {
1119 dataSamples[index + SAMPLE_X] -= mXOffset;
1120 dataSamples[index + SAMPLE_Y] -= mYOffset;
1121 index += NUM_SAMPLE_DATA;
1122 }
1123 }
1124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 mMetaState |= metaState;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001126
1127 if (DEBUG_POINTERS) {
1128 StringBuilder sb = new StringBuilder(128);
1129 sb.append("Add:");
Jeff Brown5c225b12010-06-16 01:53:36 -07001130 for (int i = 0; i < mNumPointers; i++) {
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001131 sb.append(" #");
Jeff Brown5c225b12010-06-16 01:53:36 -07001132 sb.append(getPointerId(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001133 sb.append("(");
Jeff Brown5c225b12010-06-16 01:53:36 -07001134 sb.append(getX(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001135 sb.append(",");
Jeff Brown5c225b12010-06-16 01:53:36 -07001136 sb.append(getY(i));
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001137 sb.append(")");
1138 }
1139 Log.v("MotionEvent", sb.toString());
1140 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 }
Romain Guycafdea62009-06-12 10:51:36 -07001142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 @Override
1144 public String toString() {
1145 return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this))
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001146 + " action=" + mAction + " x=" + getX()
1147 + " y=" + getY() + " pressure=" + getPressure() + " size=" + getSize() + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001148 }
1149
1150 public static final Parcelable.Creator<MotionEvent> CREATOR
1151 = new Parcelable.Creator<MotionEvent>() {
1152 public MotionEvent createFromParcel(Parcel in) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001153 final int NP = in.readInt();
1154 final int NS = in.readInt();
1155 final int NI = NP * NS * NUM_SAMPLE_DATA;
1156
1157 MotionEvent ev = obtain(NP, NS);
1158 ev.mNumPointers = NP;
1159 ev.mNumSamples = NS;
1160
1161 ev.mDownTimeNano = in.readLong();
1162 ev.mAction = in.readInt();
1163 ev.mXOffset = in.readFloat();
1164 ev.mYOffset = in.readFloat();
1165 ev.mXPrecision = in.readFloat();
1166 ev.mYPrecision = in.readFloat();
1167 ev.mDeviceId = in.readInt();
1168 ev.mEdgeFlags = in.readInt();
1169 ev.mMetaState = in.readInt();
1170
1171 final int[] pointerIdentifiers = ev.mPointerIdentifiers;
1172 for (int i = 0; i < NP; i++) {
1173 pointerIdentifiers[i] = in.readInt();
1174 }
1175
1176 final long[] eventTimeNanoSamples = ev.mEventTimeNanoSamples;
1177 for (int i = 0; i < NS; i++) {
1178 eventTimeNanoSamples[i] = in.readLong();
1179 }
1180
1181 final float[] dataSamples = ev.mDataSamples;
1182 for (int i = 0; i < NI; i++) {
1183 dataSamples[i] = in.readFloat();
1184 }
1185
1186 ev.mLastEventTimeNanoSampleIndex = NS - 1;
1187 ev.mLastDataSampleIndex = (NS - 1) * NP * NUM_SAMPLE_DATA;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 return ev;
1189 }
1190
1191 public MotionEvent[] newArray(int size) {
1192 return new MotionEvent[size];
1193 }
1194 };
1195
1196 public int describeContents() {
1197 return 0;
1198 }
1199
1200 public void writeToParcel(Parcel out, int flags) {
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001201 final int NP = mNumPointers;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001202 final int NS = mNumSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -07001203 final int NI = NP * NS * NUM_SAMPLE_DATA;
1204
1205 out.writeInt(NP);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001206 out.writeInt(NS);
Jeff Brown5c225b12010-06-16 01:53:36 -07001207
1208 out.writeLong(mDownTimeNano);
1209 out.writeInt(mAction);
1210 out.writeFloat(mXOffset);
1211 out.writeFloat(mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 out.writeFloat(mXPrecision);
1213 out.writeFloat(mYPrecision);
1214 out.writeInt(mDeviceId);
1215 out.writeInt(mEdgeFlags);
Jeff Brown5c225b12010-06-16 01:53:36 -07001216 out.writeInt(mMetaState);
1217
1218 final int[] pointerIdentifiers = mPointerIdentifiers;
1219 for (int i = 0; i < NP; i++) {
1220 out.writeInt(pointerIdentifiers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 }
Jeff Brown5c225b12010-06-16 01:53:36 -07001222
1223 final long[] eventTimeNanoSamples = mEventTimeNanoSamples;
1224 for (int i = 0; i < NS; i++) {
1225 out.writeLong(eventTimeNanoSamples[i]);
1226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227
Jeff Brown5c225b12010-06-16 01:53:36 -07001228 final float[] dataSamples = mDataSamples;
1229 for (int i = 0; i < NI; i++) {
1230 out.writeFloat(dataSamples[i]);
1231 }
1232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233}