blob: db2cd505625299c4b5045ac7f882621b0dd3a744 [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
Jeff Brown20e987b2010-08-23 12:01:02 -070019import android.graphics.Matrix;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.os.Parcel;
21import android.os.Parcelable;
22import android.os.SystemClock;
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.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070028 * <p>
29 * On pointing devices such as touch screens, pointer coordinates specify absolute
30 * positions such as view X/Y coordinates. Each complete gesture is represented
31 * by a sequence of motion events with actions that describe pointer state transitions
32 * and movements. A gesture starts with a motion event with {@link #ACTION_DOWN}
33 * that provides the location of the first pointer down. As each additional
34 * pointer that goes down or up, the framework will generate a motion event with
35 * {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} accordingly.
36 * Pointer movements are described by motion events with {@link #ACTION_MOVE}.
37 * Finally, a gesture end either when the final pointer goes up as represented
38 * by a motion event with {@link #ACTION_UP} or when gesture is canceled
39 * with {@link #ACTION_CANCEL}.
40 * </p><p>
41 * On trackballs, the pointer coordinates specify relative movements as X/Y deltas.
42 * A trackball gesture consists of a sequence of movements described by motion
43 * events with {@link #ACTION_MOVE} interspersed with occasional {@link #ACTION_DOWN}
44 * or {@link #ACTION_UP} motion events when the trackball button is pressed or released.
45 * </p><p>
46 * Motion events always report movements for all pointers at once. The number
47 * of pointers only ever changes by one as individual pointers go up and down,
48 * except when the gesture is canceled.
49 * </p><p>
50 * The order in which individual pointers appear within a motion event can change
51 * from one event to the next. Use the {@link #getPointerId(int)} method to obtain a
52 * pointer id to track pointers across motion events in a gesture. Then for
53 * successive motion events, use the {@link #findPointerIndex(int)} method to obtain
54 * the pointer index for a given pointer id in that motion event.
55 * </p><p>
56 * For efficiency, motion events with {@link #ACTION_MOVE} may batch together
57 * multiple movement samples within a single object. The most current
58 * pointer coordinates are available using {@link #getX(int)} and {@link #getY(int)}.
59 * Earlier coordinates within the batch are accessed using {@link #getHistoricalX(int, int)}
60 * and {@link #getHistoricalY(int, int)}. The coordinates are "historical" only
61 * insofar as they are older than the current coordinates in the batch; however,
62 * they are still distinct from any other coordinates reported in prior motion events.
63 * To process all coordinates in the batch in time order, first consume the historical
64 * coordinates then consume the current coordinates.
65 * </p><p>
66 * Example: Consuming all samples for all pointers in a motion event in time order.
67 * </p><p><pre><code>
68 * void printSamples(MotionEvent ev) {
69 * final int historySize = ev.getHistorySize();
70 * final int pointerCount = ev.getPointerCount();
71 * for (int h = 0; h &lt; historySize; h++) {
72 * System.out.printf("At time %d:", ev.getHistoricalEventTime(h));
73 * for (int p = 0; p &lt; pointerCount; p++) {
74 * System.out.printf(" pointer %d: (%f,%f)",
75 * ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h));
76 * }
77 * }
78 * System.out.printf("At time %d:", ev.getEventTime());
79 * for (int p = 0; p &lt; pointerCount; p++) {
80 * System.out.printf(" pointer %d: (%f,%f)",
81 * ev.getPointerId(p), ev.getX(p), ev.getY(p));
82 * }
83 * }
84 * </code></pre></p><p>
Jeff Brownb6997262010-10-08 22:31:17 -070085 * In general, the framework cannot guarantee that the motion events it delivers
86 * to a view always constitute a complete motion sequences since some events may be dropped
87 * or modified by containing views before they are delivered. The view implementation
88 * should be prepared to handle {@link #ACTION_CANCEL} and should tolerate anomalous
89 * situations such as receiving a new {@link #ACTION_DOWN} without first having
90 * received an {@link #ACTION_UP} for the prior gesture.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070091 * </p><p>
92 * Refer to {@link InputDevice} for more information about how different kinds of
Jeff Brownc5ed5912010-07-14 18:48:53 -070093 * input devices and sources represent pointer coordinates.
Jeff Browndc1ab4b2010-09-14 18:03:38 -070094 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
Jeff Brownc5ed5912010-07-14 18:48:53 -070096public final class MotionEvent extends InputEvent implements Parcelable {
Jeff Brown5c225b12010-06-16 01:53:36 -070097 private static final long MS_PER_NS = 1000000;
Jeff Brown85a31762010-09-01 17:01:00 -070098 private static final boolean TRACK_RECYCLED_LOCATION = false;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700101 * Bit mask of the parts of the action code that are the action itself.
102 */
103 public static final int ACTION_MASK = 0xff;
104
105 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * Constant for {@link #getAction}: A pressed gesture has started, the
107 * motion contains the initial starting location.
108 */
109 public static final int ACTION_DOWN = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
112 * Constant for {@link #getAction}: A pressed gesture has finished, the
113 * motion contains the final release location as well as any intermediate
114 * points since the last down or move event.
115 */
116 public static final int ACTION_UP = 1;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 /**
119 * Constant for {@link #getAction}: A change has happened during a
120 * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
121 * The motion contains the most recent point, as well as any intermediate
122 * points since the last down or move event.
123 */
124 public static final int ACTION_MOVE = 2;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 /**
127 * Constant for {@link #getAction}: The current gesture has been aborted.
128 * You will not receive any more points in it. You should treat this as
129 * an up event, but not perform any action that you normally would.
130 */
131 public static final int ACTION_CANCEL = 3;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 /**
134 * Constant for {@link #getAction}: A movement has happened outside of the
135 * normal bounds of the UI element. This does not provide a full gesture,
136 * but only the initial location of the movement/touch.
137 */
138 public static final int ACTION_OUTSIDE = 4;
139
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700140 /**
141 * A non-primary pointer has gone down. The bits in
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700142 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700143 */
144 public static final int ACTION_POINTER_DOWN = 5;
145
146 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700147 * A non-primary pointer has gone up. The bits in
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700148 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700149 */
150 public static final int ACTION_POINTER_UP = 6;
151
152 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800153 * Bits in the action code that represent a pointer index, used with
154 * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Shifting
155 * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer
156 * index where the data for the pointer going up or down can be found; you can
157 * get its identifier with {@link #getPointerId(int)} and the actual
158 * data with {@link #getX(int)} etc.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700159 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800160 public static final int ACTION_POINTER_INDEX_MASK = 0xff00;
161
162 /**
163 * Bit shift for the action bits holding the pointer index as
164 * defined by {@link #ACTION_POINTER_INDEX_MASK}.
165 */
166 public static final int ACTION_POINTER_INDEX_SHIFT = 8;
167
168 /**
169 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
170 * data index associated with {@link #ACTION_POINTER_DOWN}.
171 */
172 @Deprecated
173 public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000;
174
175 /**
176 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
177 * data index associated with {@link #ACTION_POINTER_DOWN}.
178 */
179 @Deprecated
180 public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100;
181
182 /**
183 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
184 * data index associated with {@link #ACTION_POINTER_DOWN}.
185 */
186 @Deprecated
187 public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200;
188
189 /**
190 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
191 * data index associated with {@link #ACTION_POINTER_UP}.
192 */
193 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700194 public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000;
195
196 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800197 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
198 * data index associated with {@link #ACTION_POINTER_UP}.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700199 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800200 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700201 public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100;
202
203 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800204 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
205 * data index associated with {@link #ACTION_POINTER_UP}.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700206 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800207 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700208 public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200;
209
210 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800211 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match
212 * the actual data contained in these bits.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700213 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800214 @Deprecated
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700215 public static final int ACTION_POINTER_ID_MASK = 0xff00;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700216
217 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800218 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match
219 * the actual data contained in these bits.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700220 */
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800221 @Deprecated
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700222 public static final int ACTION_POINTER_ID_SHIFT = 8;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700223
Jeff Brown85a31762010-09-01 17:01:00 -0700224 /**
225 * This flag indicates that the window that received this motion event is partly
226 * or wholly obscured by another visible window above it. This flag is set to true
227 * even if the event did not directly pass through the obscured area.
228 * A security sensitive application can check this flag to identify situations in which
229 * a malicious application may have covered up part of its content for the purpose
230 * of misleading the user or hijacking touches. An appropriate response might be
231 * to drop the suspect touches or to take additional precautions to confirm the user's
232 * actual intent.
233 */
234 public static final int FLAG_WINDOW_IS_OBSCURED = 0x1;
Romain Guycafdea62009-06-12 10:51:36 -0700235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
237 * Flag indicating the motion event intersected the top edge of the screen.
238 */
239 public static final int EDGE_TOP = 0x00000001;
Romain Guycafdea62009-06-12 10:51:36 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 /**
242 * Flag indicating the motion event intersected the bottom edge of the screen.
243 */
244 public static final int EDGE_BOTTOM = 0x00000002;
Romain Guycafdea62009-06-12 10:51:36 -0700245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 /**
247 * Flag indicating the motion event intersected the left edge of the screen.
248 */
249 public static final int EDGE_LEFT = 0x00000004;
Romain Guycafdea62009-06-12 10:51:36 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 /**
252 * Flag indicating the motion event intersected the right edge of the screen.
253 */
254 public static final int EDGE_RIGHT = 0x00000008;
Romain Guycafdea62009-06-12 10:51:36 -0700255
Jeff Brown9e2ad362010-07-30 19:20:11 -0700256 /*
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700257 * Offset for the sample's X coordinate.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700258 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700259 static private final int SAMPLE_X = 0;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700260
Jeff Brown9e2ad362010-07-30 19:20:11 -0700261 /*
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700262 * Offset for the sample's Y coordinate.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700263 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700264 static private final int SAMPLE_Y = 1;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700265
Jeff Brown9e2ad362010-07-30 19:20:11 -0700266 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700267 * Offset for the sample's pressure.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700268 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700269 static private final int SAMPLE_PRESSURE = 2;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700270
Jeff Brown9e2ad362010-07-30 19:20:11 -0700271 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700272 * Offset for the sample's size
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700273 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700274 static private final int SAMPLE_SIZE = 3;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700275
Jeff Brown9e2ad362010-07-30 19:20:11 -0700276 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700277 * Offset for the sample's touch major axis length.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700278 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700279 static private final int SAMPLE_TOUCH_MAJOR = 4;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700280
Jeff Brown9e2ad362010-07-30 19:20:11 -0700281 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700282 * Offset for the sample's touch minor axis length.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700283 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700284 static private final int SAMPLE_TOUCH_MINOR = 5;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700285
Jeff Brown9e2ad362010-07-30 19:20:11 -0700286 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700287 * Offset for the sample's tool major axis length.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700288 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700289 static private final int SAMPLE_TOOL_MAJOR = 6;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700290
Jeff Brown9e2ad362010-07-30 19:20:11 -0700291 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700292 * Offset for the sample's tool minor axis length.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700293 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700294 static private final int SAMPLE_TOOL_MINOR = 7;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700295
Jeff Brown9e2ad362010-07-30 19:20:11 -0700296 /*
Jeff Brownc5ed5912010-07-14 18:48:53 -0700297 * Offset for the sample's orientation.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700298 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700299 static private final int SAMPLE_ORIENTATION = 8;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700300
Jeff Brown9e2ad362010-07-30 19:20:11 -0700301 /*
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700302 * Number of data items for each sample.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700303 */
Jeff Brown6d0fec22010-07-23 21:28:06 -0700304 static private final int NUM_SAMPLE_DATA = 9;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700305
Jeff Brown9e2ad362010-07-30 19:20:11 -0700306 /*
307 * Minimum number of pointers for which to reserve space when allocating new
308 * motion events. This is explicitly not a bound on the maximum number of pointers.
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700309 */
Jeff Brown9e2ad362010-07-30 19:20:11 -0700310 static private final int BASE_AVAIL_POINTERS = 5;
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700311
Jeff Brown9e2ad362010-07-30 19:20:11 -0700312 /*
313 * Minimum number of samples for which to reserve space when allocating new motion events.
314 */
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700315 static private final int BASE_AVAIL_SAMPLES = 8;
316
Jeff Brown1f245102010-11-18 20:53:46 -0800317 private static final int MAX_RECYCLED = 10;
318 private static final Object gRecyclerLock = new Object();
319 private static int gRecyclerUsed;
320 private static MotionEvent gRecyclerTop;
Romain Guycafdea62009-06-12 10:51:36 -0700321
Jeff Brown5c225b12010-06-16 01:53:36 -0700322 private long mDownTimeNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 private int mAction;
Jeff Brown5c225b12010-06-16 01:53:36 -0700324 private float mXOffset;
325 private float mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 private float mXPrecision;
327 private float mYPrecision;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 private int mEdgeFlags;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700329 private int mMetaState;
Jeff Brown85a31762010-09-01 17:01:00 -0700330 private int mFlags;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700331
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700332 private int mNumPointers;
333 private int mNumSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -0700334
335 private int mLastDataSampleIndex;
336 private int mLastEventTimeNanoSampleIndex;
337
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700338 // Array of mNumPointers size of identifiers for each pointer of data.
339 private int[] mPointerIdentifiers;
Jeff Brown5c225b12010-06-16 01:53:36 -0700340
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700341 // Array of (mNumSamples * mNumPointers * NUM_SAMPLE_DATA) size of event data.
Jeff Brown5c225b12010-06-16 01:53:36 -0700342 // Samples are ordered from oldest to newest.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700343 private float[] mDataSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -0700344
345 // Array of mNumSamples size of event time stamps in nanoseconds.
346 // Samples are ordered from oldest to newest.
347 private long[] mEventTimeNanoSamples;
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 private MotionEvent mNext;
350 private RuntimeException mRecycledLocation;
351 private boolean mRecycled;
352
Jeff Brown20e987b2010-08-23 12:01:02 -0700353 private native void nativeTransform(Matrix matrix);
354
Jeff Brown46b9ac02010-04-22 18:58:52 -0700355 private MotionEvent(int pointerCount, int sampleCount) {
356 mPointerIdentifiers = new int[pointerCount];
357 mDataSamples = new float[pointerCount * sampleCount * NUM_SAMPLE_DATA];
Jeff Brown5c225b12010-06-16 01:53:36 -0700358 mEventTimeNanoSamples = new long[sampleCount];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 }
Romain Guycafdea62009-06-12 10:51:36 -0700360
Jeff Brown46b9ac02010-04-22 18:58:52 -0700361 static private MotionEvent obtain(int pointerCount, int sampleCount) {
362 final MotionEvent ev;
363 synchronized (gRecyclerLock) {
Jeff Brown1f245102010-11-18 20:53:46 -0800364 ev = gRecyclerTop;
365 if (ev == null) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700366 if (pointerCount < BASE_AVAIL_POINTERS) {
367 pointerCount = BASE_AVAIL_POINTERS;
368 }
369 if (sampleCount < BASE_AVAIL_SAMPLES) {
370 sampleCount = BASE_AVAIL_SAMPLES;
371 }
372 return new MotionEvent(pointerCount, sampleCount);
373 }
Jeff Brown46b9ac02010-04-22 18:58:52 -0700374 gRecyclerTop = ev.mNext;
Jeff Brown5c225b12010-06-16 01:53:36 -0700375 gRecyclerUsed -= 1;
Jeff Brown46b9ac02010-04-22 18:58:52 -0700376 }
377 ev.mRecycledLocation = null;
378 ev.mRecycled = false;
379 ev.mNext = null;
380
381 if (ev.mPointerIdentifiers.length < pointerCount) {
382 ev.mPointerIdentifiers = new int[pointerCount];
383 }
384
Jeff Brown5c225b12010-06-16 01:53:36 -0700385 if (ev.mEventTimeNanoSamples.length < sampleCount) {
386 ev.mEventTimeNanoSamples = new long[sampleCount];
Jeff Brown46b9ac02010-04-22 18:58:52 -0700387 }
388
Jeff Brown46b9ac02010-04-22 18:58:52 -0700389 final int neededDataSamplesLength = pointerCount * sampleCount * NUM_SAMPLE_DATA;
Jeff Brown5c225b12010-06-16 01:53:36 -0700390 if (ev.mDataSamples.length < neededDataSamplesLength) {
Jeff Brown46b9ac02010-04-22 18:58:52 -0700391 ev.mDataSamples = new float[neededDataSamplesLength];
392 }
393
394 return ev;
395 }
Jeff Brown5c225b12010-06-16 01:53:36 -0700396
Michael Chan53071d62009-05-13 17:29:48 -0700397 /**
398 * Create a new MotionEvent, filling in all of the basic values that
399 * define the motion.
400 *
401 * @param downTime The time (in ms) when the user originally pressed down to start
402 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700403 * @param eventTime The the time (in ms) when this specific event was generated. This
Michael Chan53071d62009-05-13 17:29:48 -0700404 * must be obtained from {@link SystemClock#uptimeMillis()}.
Michael Chan53071d62009-05-13 17:29:48 -0700405 * @param action The kind of action being performed -- one of either
406 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
407 * {@link #ACTION_CANCEL}.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700408 * @param pointers The number of points that will be in this event.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700409 * @param pointerIds An array of <em>pointers</em> values providing
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700410 * an identifier for each pointer.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700411 * @param pointerCoords An array of <em>pointers</em> values providing
412 * a {@link PointerCoords} coordinate object for each pointer.
Michael Chan53071d62009-05-13 17:29:48 -0700413 * @param metaState The state of any meta / modifier keys that were in effect when
414 * the event was generated.
415 * @param xPrecision The precision of the X coordinate being reported.
416 * @param yPrecision The precision of the Y coordinate being reported.
417 * @param deviceId The id for the device that this event came from. An id of
418 * zero indicates that the event didn't come from a physical device; other
419 * numbers are arbitrary and you shouldn't depend on the values.
Jeff Brown85a31762010-09-01 17:01:00 -0700420 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
Michael Chan53071d62009-05-13 17:29:48 -0700421 * MotionEvent.
Jeff Brownc5ed5912010-07-14 18:48:53 -0700422 * @param source The source of this event.
Jeff Brown85a31762010-09-01 17:01:00 -0700423 * @param flags The motion event flags.
Michael Chan53071d62009-05-13 17:29:48 -0700424 */
Jeff Brownc5ed5912010-07-14 18:48:53 -0700425 static public MotionEvent obtain(long downTime, long eventTime,
426 int action, int pointers, int[] pointerIds, PointerCoords[] pointerCoords,
427 int metaState, float xPrecision, float yPrecision, int deviceId,
Jeff Brown85a31762010-09-01 17:01:00 -0700428 int edgeFlags, int source, int flags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700429 MotionEvent ev = obtain(pointers, 1);
Michael Chan53071d62009-05-13 17:29:48 -0700430 ev.mDeviceId = deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700431 ev.mSource = source;
Michael Chan53071d62009-05-13 17:29:48 -0700432 ev.mEdgeFlags = edgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700433 ev.mDownTimeNano = downTime * MS_PER_NS;
Michael Chan53071d62009-05-13 17:29:48 -0700434 ev.mAction = action;
Jeff Brown85a31762010-09-01 17:01:00 -0700435 ev.mFlags = flags;
Michael Chan53071d62009-05-13 17:29:48 -0700436 ev.mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700437 ev.mXOffset = 0;
438 ev.mYOffset = 0;
Michael Chan53071d62009-05-13 17:29:48 -0700439 ev.mXPrecision = xPrecision;
440 ev.mYPrecision = yPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700441
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700442 ev.mNumPointers = pointers;
443 ev.mNumSamples = 1;
444
Jeff Brown5c225b12010-06-16 01:53:36 -0700445 ev.mLastDataSampleIndex = 0;
446 ev.mLastEventTimeNanoSampleIndex = 0;
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700447
Jeff Brownc5ed5912010-07-14 18:48:53 -0700448 System.arraycopy(pointerIds, 0, ev.mPointerIdentifiers, 0, pointers);
Dianne Hackborn1e8dfc72009-08-06 12:43:01 -0700449
Jeff Brownc5ed5912010-07-14 18:48:53 -0700450 ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS;
Jeff Brown5c225b12010-06-16 01:53:36 -0700451
Jeff Brownc5ed5912010-07-14 18:48:53 -0700452 ev.setPointerCoordsAtSampleIndex(0, pointerCoords);
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700453
Michael Chan53071d62009-05-13 17:29:48 -0700454 return ev;
455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456
457 /**
458 * Create a new MotionEvent, filling in all of the basic values that
459 * define the motion.
Romain Guycafdea62009-06-12 10:51:36 -0700460 *
461 * @param downTime The time (in ms) when the user originally pressed down to start
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
Romain Guycafdea62009-06-12 10:51:36 -0700463 * @param eventTime The the time (in ms) when this specific event was generated. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 * must be obtained from {@link SystemClock#uptimeMillis()}.
465 * @param action The kind of action being performed -- one of either
466 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
467 * {@link #ACTION_CANCEL}.
468 * @param x The X coordinate of this event.
469 * @param y The Y coordinate of this event.
Romain Guycafdea62009-06-12 10:51:36 -0700470 * @param pressure The current pressure of this event. The pressure generally
471 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
472 * values higher than 1 may be generated depending on the calibration of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 * the input device.
474 * @param size A scaled value of the approximate size of the area being pressed when
Romain Guycafdea62009-06-12 10:51:36 -0700475 * touched with the finger. The actual value in pixels corresponding to the finger
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 * touch is normalized with a device specific range of values
477 * and scaled to a value between 0 and 1.
478 * @param metaState The state of any meta / modifier keys that were in effect when
479 * the event was generated.
480 * @param xPrecision The precision of the X coordinate being reported.
481 * @param yPrecision The precision of the Y coordinate being reported.
482 * @param deviceId The id for the device that this event came from. An id of
483 * zero indicates that the event didn't come from a physical device; other
484 * numbers are arbitrary and you shouldn't depend on the values.
Jeff Brown85a31762010-09-01 17:01:00 -0700485 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 * MotionEvent.
487 */
488 static public MotionEvent obtain(long downTime, long eventTime, int action,
489 float x, float y, float pressure, float size, int metaState,
490 float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700491 MotionEvent ev = obtain(1, 1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 ev.mDeviceId = deviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700493 ev.mSource = InputDevice.SOURCE_UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 ev.mEdgeFlags = edgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700495 ev.mDownTimeNano = downTime * MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 ev.mAction = action;
Jeff Brown85a31762010-09-01 17:01:00 -0700497 ev.mFlags = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 ev.mMetaState = metaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700499 ev.mXOffset = 0;
500 ev.mYOffset = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800501 ev.mXPrecision = xPrecision;
502 ev.mYPrecision = yPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700503
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700504 ev.mNumPointers = 1;
505 ev.mNumSamples = 1;
Jeff Brown5c225b12010-06-16 01:53:36 -0700506
507 ev.mLastDataSampleIndex = 0;
508 ev.mLastEventTimeNanoSampleIndex = 0;
509
510 ev.mPointerIdentifiers[0] = 0;
511
512 ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS;
513
Jeff Brownc5ed5912010-07-14 18:48:53 -0700514 ev.setPointerCoordsAtSampleIndex(0, x, y, pressure, size);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700515 return ev;
516 }
517
518 /**
519 * Create a new MotionEvent, filling in all of the basic values that
520 * define the motion.
521 *
522 * @param downTime The time (in ms) when the user originally pressed down to start
523 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
524 * @param eventTime The the time (in ms) when this specific event was generated. This
525 * must be obtained from {@link SystemClock#uptimeMillis()}.
526 * @param action The kind of action being performed -- one of either
527 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
528 * {@link #ACTION_CANCEL}.
529 * @param pointers The number of pointers that are active in this event.
530 * @param x The X coordinate of this event.
531 * @param y The Y coordinate of this event.
532 * @param pressure The current pressure of this event. The pressure generally
533 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
534 * values higher than 1 may be generated depending on the calibration of
535 * the input device.
536 * @param size A scaled value of the approximate size of the area being pressed when
537 * touched with the finger. The actual value in pixels corresponding to the finger
538 * touch is normalized with a device specific range of values
539 * and scaled to a value between 0 and 1.
540 * @param metaState The state of any meta / modifier keys that were in effect when
541 * the event was generated.
542 * @param xPrecision The precision of the X coordinate being reported.
543 * @param yPrecision The precision of the Y coordinate being reported.
544 * @param deviceId The id for the device that this event came from. An id of
545 * zero indicates that the event didn't come from a physical device; other
546 * numbers are arbitrary and you shouldn't depend on the values.
Jeff Brown85a31762010-09-01 17:01:00 -0700547 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700548 * MotionEvent.
Jeff Brown5c225b12010-06-16 01:53:36 -0700549 *
550 * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)}
551 * instead.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700552 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700553 @Deprecated
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700554 static public MotionEvent obtain(long downTime, long eventTime, int action,
555 int pointers, float x, float y, float pressure, float size, int metaState,
556 float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700557 return obtain(downTime, eventTime, action, x, y, pressure, size,
558 metaState, xPrecision, yPrecision, deviceId, edgeFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
Romain Guycafdea62009-06-12 10:51:36 -0700560
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 /**
562 * Create a new MotionEvent, filling in a subset of the basic motion
563 * values. Those not specified here are: device id (always 0), pressure
564 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
Romain Guycafdea62009-06-12 10:51:36 -0700565 *
566 * @param downTime The time (in ms) when the user originally pressed down to start
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}.
Romain Guycafdea62009-06-12 10:51:36 -0700568 * @param eventTime The the time (in ms) when this specific event was generated. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 * must be obtained from {@link SystemClock#uptimeMillis()}.
570 * @param action The kind of action being performed -- one of either
571 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
572 * {@link #ACTION_CANCEL}.
573 * @param x The X coordinate of this event.
574 * @param y The Y coordinate of this event.
575 * @param metaState The state of any meta / modifier keys that were in effect when
576 * the event was generated.
577 */
578 static public MotionEvent obtain(long downTime, long eventTime, int action,
579 float x, float y, int metaState) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700580 return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f,
581 metaState, 1.0f, 1.0f, 0, 0);
Mitsuru Oshima8169dae2009-04-28 18:12:09 -0700582 }
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 /**
585 * Create a new MotionEvent, copying from an existing one.
586 */
587 static public MotionEvent obtain(MotionEvent o) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700588 MotionEvent ev = obtain(o.mNumPointers, o.mNumSamples);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 ev.mDeviceId = o.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700590 ev.mSource = o.mSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 ev.mEdgeFlags = o.mEdgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700592 ev.mDownTimeNano = o.mDownTimeNano;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 ev.mAction = o.mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700594 ev.mFlags = o.mFlags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 ev.mMetaState = o.mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700596 ev.mXOffset = o.mXOffset;
597 ev.mYOffset = o.mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 ev.mXPrecision = o.mXPrecision;
599 ev.mYPrecision = o.mYPrecision;
Jeff Brown5c225b12010-06-16 01:53:36 -0700600 int numPointers = ev.mNumPointers = o.mNumPointers;
601 int numSamples = ev.mNumSamples = o.mNumSamples;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700602
Jeff Brown5c225b12010-06-16 01:53:36 -0700603 ev.mLastDataSampleIndex = o.mLastDataSampleIndex;
604 ev.mLastEventTimeNanoSampleIndex = o.mLastEventTimeNanoSampleIndex;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700605
Jeff Brown5c225b12010-06-16 01:53:36 -0700606 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700607
Jeff Brown5c225b12010-06-16 01:53:36 -0700608 System.arraycopy(o.mEventTimeNanoSamples, 0, ev.mEventTimeNanoSamples, 0, numSamples);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700609
Jeff Brown5c225b12010-06-16 01:53:36 -0700610 System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0,
611 numPointers * numSamples * NUM_SAMPLE_DATA);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 return ev;
613 }
Romain Guycafdea62009-06-12 10:51:36 -0700614
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 /**
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700616 * Create a new MotionEvent, copying from an existing one, but not including
617 * any historical point information.
618 */
619 static public MotionEvent obtainNoHistory(MotionEvent o) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700620 MotionEvent ev = obtain(o.mNumPointers, 1);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700621 ev.mDeviceId = o.mDeviceId;
Jeff Brownc5ed5912010-07-14 18:48:53 -0700622 ev.mSource = o.mSource;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700623 ev.mEdgeFlags = o.mEdgeFlags;
Jeff Brown5c225b12010-06-16 01:53:36 -0700624 ev.mDownTimeNano = o.mDownTimeNano;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700625 ev.mAction = o.mAction;
Jeff Brown85a31762010-09-01 17:01:00 -0700626 o.mFlags = o.mFlags;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700627 ev.mMetaState = o.mMetaState;
Jeff Brown5c225b12010-06-16 01:53:36 -0700628 ev.mXOffset = o.mXOffset;
629 ev.mYOffset = o.mYOffset;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700630 ev.mXPrecision = o.mXPrecision;
631 ev.mYPrecision = o.mYPrecision;
632
Jeff Brown5c225b12010-06-16 01:53:36 -0700633 int numPointers = ev.mNumPointers = o.mNumPointers;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700634 ev.mNumSamples = 1;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700635
Jeff Brown5c225b12010-06-16 01:53:36 -0700636 ev.mLastDataSampleIndex = 0;
637 ev.mLastEventTimeNanoSampleIndex = 0;
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700638
Jeff Brown5c225b12010-06-16 01:53:36 -0700639 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700640
Jeff Brown5c225b12010-06-16 01:53:36 -0700641 ev.mEventTimeNanoSamples[0] = o.mEventTimeNanoSamples[o.mLastEventTimeNanoSampleIndex];
642
643 System.arraycopy(o.mDataSamples, o.mLastDataSampleIndex, ev.mDataSamples, 0,
644 numPointers * NUM_SAMPLE_DATA);
Dianne Hackborn8df8b2b2009-08-17 15:15:18 -0700645 return ev;
646 }
647
648 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 * Recycle the MotionEvent, to be re-used by a later caller. After calling
650 * this function you must not ever touch the event again.
651 */
Jeff Brown5c225b12010-06-16 01:53:36 -0700652 public final void recycle() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 // Ensure recycle is only called once!
654 if (TRACK_RECYCLED_LOCATION) {
655 if (mRecycledLocation != null) {
656 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
657 }
658 mRecycledLocation = new RuntimeException("Last recycled here");
Jeff Brownd28f4be2010-06-02 15:35:46 -0700659 //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation);
660 } else {
661 if (mRecycled) {
662 throw new RuntimeException(toString() + " recycled twice!");
663 }
664 mRecycled = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 synchronized (gRecyclerLock) {
668 if (gRecyclerUsed < MAX_RECYCLED) {
669 gRecyclerUsed++;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700670 mNumSamples = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800671 mNext = gRecyclerTop;
672 gRecyclerTop = this;
673 }
674 }
675 }
Jeff Brown5c225b12010-06-16 01:53:36 -0700676
677 /**
678 * Scales down the coordination of this event by the given scale.
679 *
680 * @hide
681 */
682 public final void scale(float scale) {
683 mXOffset *= scale;
684 mYOffset *= scale;
685 mXPrecision *= scale;
686 mYPrecision *= scale;
687
688 float[] history = mDataSamples;
689 final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA;
690 for (int i = 0; i < length; i += NUM_SAMPLE_DATA) {
691 history[i + SAMPLE_X] *= scale;
692 history[i + SAMPLE_Y] *= scale;
693 // no need to scale pressure
694 history[i + SAMPLE_SIZE] *= scale; // TODO: square this?
Jeff Brownc5ed5912010-07-14 18:48:53 -0700695 history[i + SAMPLE_TOUCH_MAJOR] *= scale;
696 history[i + SAMPLE_TOUCH_MINOR] *= scale;
697 history[i + SAMPLE_TOOL_MAJOR] *= scale;
698 history[i + SAMPLE_TOOL_MINOR] *= scale;
Jeff Brown5c225b12010-06-16 01:53:36 -0700699 }
700 }
Romain Guycafdea62009-06-12 10:51:36 -0700701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800702 /**
703 * Return the kind of action being performed -- one of either
704 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800705 * {@link #ACTION_CANCEL}. Consider using {@link #getActionMasked}
706 * and {@link #getActionIndex} to retrieve the separate masked action
707 * and pointer index.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 */
709 public final int getAction() {
710 return mAction;
711 }
712
713 /**
Dianne Hackbornb125dc52010-02-12 15:52:09 -0800714 * Return the masked action being performed, without pointer index
715 * information. May be any of the actions: {@link #ACTION_DOWN},
716 * {@link #ACTION_MOVE}, {@link #ACTION_UP}, {@link #ACTION_CANCEL},
717 * {@link #ACTION_POINTER_DOWN}, or {@link #ACTION_POINTER_UP}.
718 * Use {@link #getActionIndex} to return the index associated with
719 * pointer actions.
720 */
721 public final int getActionMasked() {
722 return mAction & ACTION_MASK;
723 }
724
725 /**
726 * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP}
727 * as returned by {@link #getActionMasked}, this returns the associated
728 * pointer index. The index may be used with {@link #getPointerId(int)},
729 * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)},
730 * and {@link #getSize(int)} to get information about the pointer that has
731 * gone down or up.
732 */
733 public final int getActionIndex() {
734 return (mAction & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
735 }
736
737 /**
Jeff Brown85a31762010-09-01 17:01:00 -0700738 * Gets the motion event flags.
739 *
740 * @see #FLAG_WINDOW_IS_OBSCURED
741 */
742 public final int getFlags() {
743 return mFlags;
744 }
745
746 /**
Romain Guycafdea62009-06-12 10:51:36 -0700747 * Returns the time (in ms) when the user originally pressed down to start
748 * a stream of position events.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 */
750 public final long getDownTime() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700751 return mDownTimeNano / MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 }
753
754 /**
755 * Returns the time (in ms) when this specific event was generated.
756 */
757 public final long getEventTime() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700758 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] / MS_PER_NS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
761 /**
Michael Chan53071d62009-05-13 17:29:48 -0700762 * Returns the time (in ns) when this specific event was generated.
763 * The value is in nanosecond precision but it may not have nanosecond accuracy.
764 *
765 * @hide
766 */
767 public final long getEventTimeNano() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700768 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex];
Michael Chan53071d62009-05-13 17:29:48 -0700769 }
770
771 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700772 * {@link #getX(int)} for the first pointer index (may be an
773 * arbitrary pointer identifier).
774 */
775 public final float getX() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700776 return mDataSamples[mLastDataSampleIndex + SAMPLE_X] + mXOffset;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700777 }
778
779 /**
780 * {@link #getY(int)} for the first pointer index (may be an
781 * arbitrary pointer identifier).
782 */
783 public final float getY() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700784 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y] + mYOffset;
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700785 }
786
787 /**
788 * {@link #getPressure(int)} for the first pointer index (may be an
789 * arbitrary pointer identifier).
790 */
791 public final float getPressure() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700792 return mDataSamples[mLastDataSampleIndex + SAMPLE_PRESSURE];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700793 }
794
795 /**
796 * {@link #getSize(int)} for the first pointer index (may be an
797 * arbitrary pointer identifier).
798 */
799 public final float getSize() {
Jeff Brown5c225b12010-06-16 01:53:36 -0700800 return mDataSamples[mLastDataSampleIndex + SAMPLE_SIZE];
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700801 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700802
803 /**
804 * {@link #getTouchMajor(int)} for the first pointer index (may be an
805 * arbitrary pointer identifier).
806 */
807 public final float getTouchMajor() {
808 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MAJOR];
809 }
810
811 /**
812 * {@link #getTouchMinor(int)} for the first pointer index (may be an
813 * arbitrary pointer identifier).
814 */
815 public final float getTouchMinor() {
816 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MINOR];
817 }
818
819 /**
820 * {@link #getToolMajor(int)} for the first pointer index (may be an
821 * arbitrary pointer identifier).
822 */
823 public final float getToolMajor() {
824 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MAJOR];
825 }
826
827 /**
828 * {@link #getToolMinor(int)} for the first pointer index (may be an
829 * arbitrary pointer identifier).
830 */
831 public final float getToolMinor() {
832 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MINOR];
833 }
834
835 /**
836 * {@link #getOrientation(int)} for the first pointer index (may be an
837 * arbitrary pointer identifier).
838 */
839 public final float getOrientation() {
840 return mDataSamples[mLastDataSampleIndex + SAMPLE_ORIENTATION];
841 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700842
843 /**
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700844 * The number of pointers of data contained in this event. Always
845 * >= 1.
846 */
847 public final int getPointerCount() {
848 return mNumPointers;
849 }
850
851 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700852 * Return the pointer identifier associated with a particular pointer
853 * data index is this event. The identifier tells you the actual pointer
854 * number associated with the data, accounting for individual pointers
855 * going up and down since the start of the current gesture.
856 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
857 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 */
Dianne Hackbornd41ba662009-08-05 15:30:56 -0700859 public final int getPointerId(int pointerIndex) {
860 return mPointerIdentifiers[pointerIndex];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700864 * Given a pointer identifier, find the index of its data in the event.
865 *
866 * @param pointerId The identifier of the pointer to be found.
867 * @return Returns either the index of the pointer (for use with
Gilles Debunneb0d6ba12010-08-17 20:01:42 -0700868 * {@link #getX(int)} et al.), or -1 if there is no data available for
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700869 * that pointer identifier.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700871 public final int findPointerIndex(int pointerId) {
872 int i = mNumPointers;
873 while (i > 0) {
874 i--;
875 if (mPointerIdentifiers[i] == pointerId) {
876 return i;
877 }
878 }
879 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700883 * Returns the X coordinate of this event for the given pointer
884 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
885 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700886 * Whole numbers are pixels; the
887 * value may have a fraction for input devices that are sub-pixel precise.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700888 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
889 * (the first pointer that is down) to {@link #getPointerCount()}-1.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700890 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700891 public final float getX(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700892 return mDataSamples[mLastDataSampleIndex
893 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700894 }
895
896 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700897 * Returns the Y coordinate of this event for the given pointer
898 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
899 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700900 * Whole numbers are pixels; the
901 * value may have a fraction for input devices that are sub-pixel precise.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700902 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
903 * (the first pointer that is down) to {@link #getPointerCount()}-1.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700904 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700905 public final float getY(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700906 return mDataSamples[mLastDataSampleIndex
907 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700908 }
909
910 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700911 * Returns the current pressure of this event for the given pointer
912 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
913 * identifier for this index).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700914 * The pressure generally
Romain Guycafdea62009-06-12 10:51:36 -0700915 * ranges from 0 (no pressure at all) to 1 (normal pressure), however
916 * values higher than 1 may be generated depending on the calibration of
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 * the input device.
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700918 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
919 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700921 public final float getPressure(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700922 return mDataSamples[mLastDataSampleIndex
923 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 }
925
926 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700927 * Returns a scaled value of the approximate size for the given pointer
928 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
929 * identifier for this index).
930 * This represents some approximation of the area of the screen being
931 * pressed; the actual value in pixels corresponding to the
Dianne Hackborn9822d2b2009-07-20 17:33:15 -0700932 * touch is normalized with the device specific range of values
Romain Guycafdea62009-06-12 10:51:36 -0700933 * 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 -0800934 * determine fat touch events.
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 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -0700938 public final float getSize(int pointerIndex) {
Jeff Brown5c225b12010-06-16 01:53:36 -0700939 return mDataSamples[mLastDataSampleIndex
940 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800941 }
Jeff Brownc5ed5912010-07-14 18:48:53 -0700942
943 /**
944 * Returns the length of the major axis of an ellipse that describes the touch
945 * area at the point of contact for the given pointer
946 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
947 * identifier for this index).
948 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
949 * (the first pointer that is down) to {@link #getPointerCount()}-1.
950 */
951 public final float getTouchMajor(int pointerIndex) {
952 return mDataSamples[mLastDataSampleIndex
953 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
954 }
955
956 /**
957 * Returns the length of the minor axis of an ellipse that describes the touch
958 * area at the point of contact for the given pointer
959 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
960 * identifier for this index).
961 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
962 * (the first pointer that is down) to {@link #getPointerCount()}-1.
963 */
964 public final float getTouchMinor(int pointerIndex) {
965 return mDataSamples[mLastDataSampleIndex
966 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
967 }
968
969 /**
970 * Returns the length of the major axis of an ellipse that describes the size of
971 * the approaching tool for the given pointer
972 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
973 * identifier for this index).
974 * The tool area represents the estimated size of the finger or pen that is
975 * touching the device independent of its actual touch area at the point of contact.
976 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
977 * (the first pointer that is down) to {@link #getPointerCount()}-1.
978 */
979 public final float getToolMajor(int pointerIndex) {
980 return mDataSamples[mLastDataSampleIndex
981 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
982 }
983
984 /**
985 * Returns the length of the minor axis of an ellipse that describes the size of
986 * the approaching tool for the given pointer
987 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
988 * identifier for this index).
989 * The tool area represents the estimated size of the finger or pen that is
990 * touching the device independent of its actual touch area at the point of contact.
991 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
992 * (the first pointer that is down) to {@link #getPointerCount()}-1.
993 */
994 public final float getToolMinor(int pointerIndex) {
995 return mDataSamples[mLastDataSampleIndex
996 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
997 }
998
999 /**
1000 * Returns the orientation of the touch area and tool area in radians clockwise from vertical
1001 * for the given pointer <em>index</em> (use {@link #getPointerId(int)} to find the pointer
1002 * identifier for this index).
1003 * An angle of 0 degrees indicates that the major axis of contact is oriented
1004 * upwards, is perfectly circular or is of unknown orientation. A positive angle
1005 * indicates that the major axis of contact is oriented to the right. A negative angle
1006 * indicates that the major axis of contact is oriented to the left.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001007 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
Jeff Brownc5ed5912010-07-14 18:48:53 -07001008 * (finger pointing fully right).
1009 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1010 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1011 */
1012 public final float getOrientation(int pointerIndex) {
1013 return mDataSamples[mLastDataSampleIndex
1014 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
1015 }
1016
1017 /**
1018 * Populates a {@link PointerCoords} object with pointer coordinate data for
1019 * the specified pointer index.
1020 *
1021 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1022 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1023 * @param outPointerCoords The pointer coordinate object to populate.
1024 */
1025 public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) {
1026 final int sampleIndex = mLastDataSampleIndex + pointerIndex * NUM_SAMPLE_DATA;
1027 getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords);
1028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029
1030 /**
1031 * Returns the state of any meta / modifier keys that were in effect when
1032 * the event was generated. This is the same values as those
1033 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}.
1034 *
1035 * @return an integer in which each bit set to 1 represents a pressed
1036 * meta key
1037 *
1038 * @see KeyEvent#getMetaState()
1039 */
1040 public final int getMetaState() {
1041 return mMetaState;
1042 }
1043
1044 /**
1045 * Returns the original raw X coordinate of this event. For touch
1046 * events on the screen, this is the original location of the event
1047 * on the screen, before it had been adjusted for the containing window
1048 * and views.
1049 */
1050 public final float getRawX() {
Jeff Brown5c225b12010-06-16 01:53:36 -07001051 return mDataSamples[mLastDataSampleIndex + SAMPLE_X];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001052 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07001053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 /**
1055 * Returns the original raw Y coordinate of this event. For touch
1056 * events on the screen, this is the original location of the event
1057 * on the screen, before it had been adjusted for the containing window
1058 * and views.
1059 */
1060 public final float getRawY() {
Jeff Brown5c225b12010-06-16 01:53:36 -07001061 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 }
1063
1064 /**
1065 * Return the precision of the X coordinates being reported. You can
1066 * multiple this number with {@link #getX} to find the actual hardware
1067 * value of the X coordinate.
1068 * @return Returns the precision of X coordinates being reported.
1069 */
1070 public final float getXPrecision() {
1071 return mXPrecision;
1072 }
Romain Guycafdea62009-06-12 10:51:36 -07001073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Return the precision of the Y coordinates being reported. You can
1076 * multiple this number with {@link #getY} to find the actual hardware
1077 * value of the Y coordinate.
1078 * @return Returns the precision of Y coordinates being reported.
1079 */
1080 public final float getYPrecision() {
1081 return mYPrecision;
1082 }
Romain Guycafdea62009-06-12 10:51:36 -07001083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 /**
1085 * Returns the number of historical points in this event. These are
1086 * movements that have occurred between this event and the previous event.
1087 * This only applies to ACTION_MOVE events -- all other actions will have
1088 * a size of 0.
Romain Guycafdea62009-06-12 10:51:36 -07001089 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 * @return Returns the number of historical points in the event.
1091 */
1092 public final int getHistorySize() {
Jeff Brown5c225b12010-06-16 01:53:36 -07001093 return mLastEventTimeNanoSampleIndex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
Romain Guycafdea62009-06-12 10:51:36 -07001095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 /**
1097 * Returns the time that a historical movement occurred between this event
1098 * and the previous event. Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -07001099 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 * @param pos Which historical value to return; must be less than
1101 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -07001102 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 * @see #getHistorySize
1104 * @see #getEventTime
1105 */
1106 public final long getHistoricalEventTime(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001107 return mEventTimeNanoSamples[pos] / MS_PER_NS;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001108 }
1109
1110 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001111 * {@link #getHistoricalX(int)} for the first pointer index (may be an
1112 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001113 */
1114 public final float getHistoricalX(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001115 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001116 }
1117
1118 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001119 * {@link #getHistoricalY(int)} for the first pointer index (may be an
1120 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001121 */
1122 public final float getHistoricalY(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001123 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001124 }
1125
1126 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001127 * {@link #getHistoricalPressure(int)} for the first pointer index (may be an
1128 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001129 */
1130 public final float getHistoricalPressure(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001131 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001132 }
1133
1134 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001135 * {@link #getHistoricalSize(int)} for the first pointer index (may be an
1136 * arbitrary pointer identifier).
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001137 */
1138 public final float getHistoricalSize(int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001139 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 }
Romain Guycafdea62009-06-12 10:51:36 -07001141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001143 * {@link #getHistoricalTouchMajor(int)} for the first pointer index (may be an
1144 * arbitrary pointer identifier).
1145 */
1146 public final float getHistoricalTouchMajor(int pos) {
1147 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
1148 }
1149
1150 /**
1151 * {@link #getHistoricalTouchMinor(int)} for the first pointer index (may be an
1152 * arbitrary pointer identifier).
1153 */
1154 public final float getHistoricalTouchMinor(int pos) {
1155 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
1156 }
1157
1158 /**
1159 * {@link #getHistoricalToolMajor(int)} for the first pointer index (may be an
1160 * arbitrary pointer identifier).
1161 */
1162 public final float getHistoricalToolMajor(int pos) {
1163 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
1164 }
1165
1166 /**
1167 * {@link #getHistoricalToolMinor(int)} for the first pointer index (may be an
1168 * arbitrary pointer identifier).
1169 */
1170 public final float getHistoricalToolMinor(int pos) {
1171 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
1172 }
1173
1174 /**
1175 * {@link #getHistoricalOrientation(int)} for the first pointer index (may be an
1176 * arbitrary pointer identifier).
1177 */
1178 public final float getHistoricalOrientation(int pos) {
1179 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
1180 }
1181
1182 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001183 * Returns a historical X coordinate, as per {@link #getX(int)}, that
1184 * occurred between this event and the previous event for the given pointer.
1185 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -07001186 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001187 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1188 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 * @param pos Which historical value to return; must be less than
1190 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -07001191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 * @see #getHistorySize
1193 * @see #getX
1194 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001195 public final float getHistoricalX(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001196 return mDataSamples[(pos * mNumPointers + pointerIndex)
1197 * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 }
Romain Guycafdea62009-06-12 10:51:36 -07001199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001201 * Returns a historical Y coordinate, as per {@link #getY(int)}, that
1202 * occurred between this event and the previous event for the given pointer.
1203 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -07001204 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001205 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1206 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 * @param pos Which historical value to return; must be less than
1208 * {@link #getHistorySize}
Romain Guycafdea62009-06-12 10:51:36 -07001209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 * @see #getHistorySize
1211 * @see #getY
1212 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001213 public final float getHistoricalY(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001214 return mDataSamples[(pos * mNumPointers + pointerIndex)
1215 * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 }
Romain Guycafdea62009-06-12 10:51:36 -07001217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001218 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001219 * Returns a historical pressure coordinate, as per {@link #getPressure(int)},
1220 * that occurred between this event and the previous event for the given
1221 * pointer. Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -07001222 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001223 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1224 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 * @param pos Which historical value to return; must be less than
1226 * {@link #getHistorySize}
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001227 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 * @see #getHistorySize
1229 * @see #getPressure
1230 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001231 public final float getHistoricalPressure(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001232 return mDataSamples[(pos * mNumPointers + pointerIndex)
1233 * NUM_SAMPLE_DATA + SAMPLE_PRESSURE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 }
Romain Guycafdea62009-06-12 10:51:36 -07001235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001237 * Returns a historical size coordinate, as per {@link #getSize(int)}, that
1238 * occurred between this event and the previous event for the given pointer.
1239 * Only applies to ACTION_MOVE events.
Romain Guycafdea62009-06-12 10:51:36 -07001240 *
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001241 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1242 * (the first pointer that is down) to {@link #getPointerCount()}-1.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 * @param pos Which historical value to return; must be less than
1244 * {@link #getHistorySize}
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 * @see #getHistorySize
1247 * @see #getSize
1248 */
Dianne Hackborn0dd7cb42009-08-04 05:49:43 -07001249 public final float getHistoricalSize(int pointerIndex, int pos) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001250 return mDataSamples[(pos * mNumPointers + pointerIndex)
1251 * NUM_SAMPLE_DATA + SAMPLE_SIZE];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001255 * Returns a historical touch major axis coordinate, as per {@link #getTouchMajor(int)}, that
1256 * occurred between this event and the previous event for the given pointer.
1257 * Only applies to ACTION_MOVE events.
1258 *
1259 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1260 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1261 * @param pos Which historical value to return; must be less than
1262 * {@link #getHistorySize}
1263 *
1264 * @see #getHistorySize
1265 * @see #getTouchMajor
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001267 public final float getHistoricalTouchMajor(int pointerIndex, int pos) {
1268 return mDataSamples[(pos * mNumPointers + pointerIndex)
1269 * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
Romain Guycafdea62009-06-12 10:51:36 -07001271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001273 * Returns a historical touch minor axis coordinate, as per {@link #getTouchMinor(int)}, that
1274 * occurred between this event and the previous event for the given pointer.
1275 * Only applies to ACTION_MOVE events.
1276 *
1277 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1278 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1279 * @param pos Which historical value to return; must be less than
1280 * {@link #getHistorySize}
1281 *
1282 * @see #getHistorySize
1283 * @see #getTouchMinor
1284 */
1285 public final float getHistoricalTouchMinor(int pointerIndex, int pos) {
1286 return mDataSamples[(pos * mNumPointers + pointerIndex)
1287 * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR];
1288 }
1289
1290 /**
1291 * Returns a historical tool major axis coordinate, as per {@link #getToolMajor(int)}, that
1292 * occurred between this event and the previous event for the given pointer.
1293 * Only applies to ACTION_MOVE events.
1294 *
1295 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1296 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1297 * @param pos Which historical value to return; must be less than
1298 * {@link #getHistorySize}
1299 *
1300 * @see #getHistorySize
1301 * @see #getToolMajor
1302 */
1303 public final float getHistoricalToolMajor(int pointerIndex, int pos) {
1304 return mDataSamples[(pos * mNumPointers + pointerIndex)
1305 * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR];
1306 }
1307
1308 /**
1309 * Returns a historical tool minor axis coordinate, as per {@link #getToolMinor(int)}, that
1310 * occurred between this event and the previous event for the given pointer.
1311 * Only applies to ACTION_MOVE events.
1312 *
1313 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1314 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1315 * @param pos Which historical value to return; must be less than
1316 * {@link #getHistorySize}
1317 *
1318 * @see #getHistorySize
1319 * @see #getToolMinor
1320 */
1321 public final float getHistoricalToolMinor(int pointerIndex, int pos) {
1322 return mDataSamples[(pos * mNumPointers + pointerIndex)
1323 * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR];
1324 }
1325
1326 /**
1327 * Returns a historical orientation coordinate, as per {@link #getOrientation(int)}, that
1328 * occurred between this event and the previous event for the given pointer.
1329 * Only applies to ACTION_MOVE events.
1330 *
1331 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1332 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1333 * @param pos Which historical value to return; must be less than
1334 * {@link #getHistorySize}
1335 *
1336 * @see #getHistorySize
1337 * @see #getOrientation
1338 */
1339 public final float getHistoricalOrientation(int pointerIndex, int pos) {
1340 return mDataSamples[(pos * mNumPointers + pointerIndex)
1341 * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION];
1342 }
1343
1344 /**
1345 * Populates a {@link PointerCoords} object with historical pointer coordinate data,
1346 * as per {@link #getPointerCoords}, that occurred between this event and the previous
1347 * event for the given pointer.
1348 * Only applies to ACTION_MOVE events.
1349 *
1350 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0
1351 * (the first pointer that is down) to {@link #getPointerCount()}-1.
1352 * @param pos Which historical value to return; must be less than
1353 * {@link #getHistorySize}
1354 * @param outPointerCoords The pointer coordinate object to populate.
1355 *
1356 * @see #getHistorySize
1357 * @see #getPointerCoords
1358 */
1359 public final void getHistoricalPointerCoords(int pointerIndex, int pos,
1360 PointerCoords outPointerCoords) {
1361 final int sampleIndex = (pos * mNumPointers + pointerIndex) * NUM_SAMPLE_DATA;
1362 getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords);
1363 }
1364
1365 /**
Jeff Brown46b9ac02010-04-22 18:58:52 -07001366 * Returns a bitfield indicating which edges, if any, were touched by this
Romain Guycafdea62009-06-12 10:51:36 -07001367 * MotionEvent. For touch events, clients can use this to determine if the
1368 * user's finger was touching the edge of the display.
1369 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 * @see #EDGE_LEFT
1371 * @see #EDGE_TOP
1372 * @see #EDGE_RIGHT
1373 * @see #EDGE_BOTTOM
1374 */
1375 public final int getEdgeFlags() {
1376 return mEdgeFlags;
1377 }
Romain Guycafdea62009-06-12 10:51:36 -07001378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001379
1380 /**
Jeff Brown85a31762010-09-01 17:01:00 -07001381 * Sets the bitfield indicating which edges, if any, were touched by this
Romain Guycafdea62009-06-12 10:51:36 -07001382 * MotionEvent.
1383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001384 * @see #getEdgeFlags()
1385 */
1386 public final void setEdgeFlags(int flags) {
1387 mEdgeFlags = flags;
1388 }
1389
1390 /**
1391 * Sets this event's action.
1392 */
1393 public final void setAction(int action) {
1394 mAction = action;
1395 }
1396
1397 /**
1398 * Adjust this event's location.
1399 * @param deltaX Amount to add to the current X coordinate of the event.
1400 * @param deltaY Amount to add to the current Y coordinate of the event.
1401 */
1402 public final void offsetLocation(float deltaX, float deltaY) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001403 mXOffset += deltaX;
1404 mYOffset += deltaY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 }
Romain Guycafdea62009-06-12 10:51:36 -07001406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001407 /**
1408 * Set this event's location. Applies {@link #offsetLocation} with a
1409 * delta from the current location to the given new location.
Romain Guycafdea62009-06-12 10:51:36 -07001410 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001411 * @param x New absolute X location.
1412 * @param y New absolute Y location.
1413 */
1414 public final void setLocation(float x, float y) {
Jeff Brown2f6d9752010-08-19 16:44:02 -07001415 final float[] dataSamples = mDataSamples;
1416 final int lastDataSampleIndex = mLastDataSampleIndex;
1417 mXOffset = x - dataSamples[lastDataSampleIndex + SAMPLE_X];
1418 mYOffset = y - dataSamples[lastDataSampleIndex + SAMPLE_Y];
Jeff Brown5c225b12010-06-16 01:53:36 -07001419 }
1420
Jeff Brown20e987b2010-08-23 12:01:02 -07001421 /**
1422 * Applies a transformation matrix to all of the points in the event.
1423 *
1424 * @param matrix The transformation matrix to apply.
1425 */
1426 public final void transform(Matrix matrix) {
1427 if (matrix == null) {
1428 throw new IllegalArgumentException("matrix must not be null");
1429 }
1430
1431 nativeTransform(matrix);
1432 }
1433
Jeff Brownc5ed5912010-07-14 18:48:53 -07001434 private final void getPointerCoordsAtSampleIndex(int sampleIndex,
1435 PointerCoords outPointerCoords) {
Jeff Brown2f6d9752010-08-19 16:44:02 -07001436 final float[] dataSamples = mDataSamples;
1437 outPointerCoords.x = dataSamples[sampleIndex + SAMPLE_X] + mXOffset;
1438 outPointerCoords.y = dataSamples[sampleIndex + SAMPLE_Y] + mYOffset;
1439 outPointerCoords.pressure = dataSamples[sampleIndex + SAMPLE_PRESSURE];
1440 outPointerCoords.size = dataSamples[sampleIndex + SAMPLE_SIZE];
1441 outPointerCoords.touchMajor = dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR];
1442 outPointerCoords.touchMinor = dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR];
1443 outPointerCoords.toolMajor = dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR];
1444 outPointerCoords.toolMinor = dataSamples[sampleIndex + SAMPLE_TOOL_MINOR];
1445 outPointerCoords.orientation = dataSamples[sampleIndex + SAMPLE_ORIENTATION];
Jeff Brownc5ed5912010-07-14 18:48:53 -07001446 }
1447
1448 private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1449 PointerCoords[] pointerCoords) {
1450 final int numPointers = mNumPointers;
1451 for (int i = 0; i < numPointers; i++) {
1452 setPointerCoordsAtSampleIndex(sampleIndex, pointerCoords[i]);
1453 sampleIndex += NUM_SAMPLE_DATA;
1454 }
1455 }
1456
1457 private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1458 PointerCoords pointerCoords) {
Jeff Brown2f6d9752010-08-19 16:44:02 -07001459 final float[] dataSamples = mDataSamples;
1460 dataSamples[sampleIndex + SAMPLE_X] = pointerCoords.x - mXOffset;
1461 dataSamples[sampleIndex + SAMPLE_Y] = pointerCoords.y - mYOffset;
1462 dataSamples[sampleIndex + SAMPLE_PRESSURE] = pointerCoords.pressure;
1463 dataSamples[sampleIndex + SAMPLE_SIZE] = pointerCoords.size;
1464 dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pointerCoords.touchMajor;
1465 dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pointerCoords.touchMinor;
1466 dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = pointerCoords.toolMajor;
1467 dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = pointerCoords.toolMinor;
1468 dataSamples[sampleIndex + SAMPLE_ORIENTATION] = pointerCoords.orientation;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001469 }
1470
1471 private final void setPointerCoordsAtSampleIndex(int sampleIndex,
1472 float x, float y, float pressure, float size) {
Jeff Brown2f6d9752010-08-19 16:44:02 -07001473 final float[] dataSamples = mDataSamples;
1474 dataSamples[sampleIndex + SAMPLE_X] = x - mXOffset;
1475 dataSamples[sampleIndex + SAMPLE_Y] = y - mYOffset;
1476 dataSamples[sampleIndex + SAMPLE_PRESSURE] = pressure;
1477 dataSamples[sampleIndex + SAMPLE_SIZE] = size;
1478 dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pressure;
1479 dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pressure;
1480 dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = size;
1481 dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = size;
1482 dataSamples[sampleIndex + SAMPLE_ORIENTATION] = 0;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001483 }
1484
Jeff Brown5c225b12010-06-16 01:53:36 -07001485 private final void incrementNumSamplesAndReserveStorage(int dataSampleStride) {
1486 if (mNumSamples == mEventTimeNanoSamples.length) {
1487 long[] newEventTimeNanoSamples = new long[mNumSamples + BASE_AVAIL_SAMPLES];
1488 System.arraycopy(mEventTimeNanoSamples, 0, newEventTimeNanoSamples, 0, mNumSamples);
1489 mEventTimeNanoSamples = newEventTimeNanoSamples;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001490 }
Jeff Brown5c225b12010-06-16 01:53:36 -07001491
1492 int nextDataSampleIndex = mLastDataSampleIndex + dataSampleStride;
1493 if (nextDataSampleIndex + dataSampleStride > mDataSamples.length) {
1494 float[] newDataSamples = new float[nextDataSampleIndex
1495 + BASE_AVAIL_SAMPLES * dataSampleStride];
1496 System.arraycopy(mDataSamples, 0, newDataSamples, 0, nextDataSampleIndex);
1497 mDataSamples = newDataSamples;
1498 }
1499
1500 mLastEventTimeNanoSampleIndex = mNumSamples;
1501 mLastDataSampleIndex = nextDataSampleIndex;
1502 mNumSamples += 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 }
Romain Guycafdea62009-06-12 10:51:36 -07001504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 /**
1506 * Add a new movement to the batch of movements in this event. The event's
Jeff Brownc5ed5912010-07-14 18:48:53 -07001507 * current location, position and size is updated to the new values.
1508 * The current values in the event are added to a list of historical values.
1509 *
Jeff Browne33348b2010-07-15 23:54:05 -07001510 * Only applies to {@link #ACTION_MOVE} events.
Romain Guycafdea62009-06-12 10:51:36 -07001511 *
Jeff Brownc5ed5912010-07-14 18:48:53 -07001512 * @param eventTime The time stamp (in ms) for this data.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001513 * @param x The new X position.
1514 * @param y The new Y position.
1515 * @param pressure The new pressure.
1516 * @param size The new size.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001517 * @param metaState Meta key state.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 */
1519 public final void addBatch(long eventTime, float x, float y,
1520 float pressure, float size, int metaState) {
Jeff Brown5c225b12010-06-16 01:53:36 -07001521 incrementNumSamplesAndReserveStorage(NUM_SAMPLE_DATA);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001522
Jeff Brown5c225b12010-06-16 01:53:36 -07001523 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001524 setPointerCoordsAtSampleIndex(mLastDataSampleIndex, x, y, pressure, size);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001525
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001526 mMetaState |= metaState;
1527 }
Romain Guycafdea62009-06-12 10:51:36 -07001528
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001529 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -07001530 * Add a new movement to the batch of movements in this event. The event's
1531 * current location, position and size is updated to the new values.
1532 * The current values in the event are added to a list of historical values.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001533 *
Jeff Browne33348b2010-07-15 23:54:05 -07001534 * Only applies to {@link #ACTION_MOVE} events.
Jeff Brownc5ed5912010-07-14 18:48:53 -07001535 *
1536 * @param eventTime The time stamp (in ms) for this data.
1537 * @param pointerCoords The new pointer coordinates.
1538 * @param metaState Meta key state.
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001539 */
Jeff Brownc5ed5912010-07-14 18:48:53 -07001540 public final void addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState) {
1541 final int dataSampleStride = mNumPointers * NUM_SAMPLE_DATA;
Jeff Brown5c225b12010-06-16 01:53:36 -07001542 incrementNumSamplesAndReserveStorage(dataSampleStride);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001543
Jeff Brown5c225b12010-06-16 01:53:36 -07001544 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001545 setPointerCoordsAtSampleIndex(mLastDataSampleIndex, pointerCoords);
Jeff Brown5c225b12010-06-16 01:53:36 -07001546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 mMetaState |= metaState;
1548 }
Romain Guycafdea62009-06-12 10:51:36 -07001549
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 @Override
1551 public String toString() {
1552 return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this))
Huahui Wue838a422011-01-13 16:03:43 -08001553 + " pointerId=" + getPointerId(0)
Jeff Brown497a92c2010-09-12 17:55:08 -07001554 + " action=" + actionToString(mAction)
1555 + " x=" + getX()
1556 + " y=" + getY()
1557 + " pressure=" + getPressure()
1558 + " size=" + getSize()
1559 + " touchMajor=" + getTouchMajor()
1560 + " touchMinor=" + getTouchMinor()
1561 + " toolMajor=" + getToolMajor()
1562 + " toolMinor=" + getToolMinor()
1563 + " orientation=" + getOrientation()
1564 + " meta=" + KeyEvent.metaStateToString(mMetaState)
1565 + " pointerCount=" + getPointerCount()
1566 + " historySize=" + getHistorySize()
1567 + " flags=0x" + Integer.toHexString(mFlags)
1568 + " edgeFlags=0x" + Integer.toHexString(mEdgeFlags)
1569 + " device=" + mDeviceId
1570 + " source=0x" + Integer.toHexString(mSource)
Huahui Wue838a422011-01-13 16:03:43 -08001571 + (getPointerCount() > 1 ?
1572 " pointerId2=" + getPointerId(1) + " x2=" + getX(2) + " y2=" + getY(2) : "")
Jeff Brown497a92c2010-09-12 17:55:08 -07001573 + "}";
1574 }
1575
1576 /**
1577 * Returns a string that represents the symbolic name of the specified action
1578 * such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or "35" (if unknown).
1579 *
1580 * @param action The action.
1581 * @return The symbolic name of the specified action.
1582 * @hide
1583 */
1584 public static String actionToString(int action) {
1585 switch (action) {
1586 case ACTION_DOWN:
1587 return "ACTION_DOWN";
1588 case ACTION_UP:
1589 return "ACTION_UP";
1590 case ACTION_CANCEL:
1591 return "ACTION_CANCEL";
1592 case ACTION_MOVE:
1593 return "ACTION_MOVE";
1594 }
1595 int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
1596 switch (action & ACTION_MASK) {
1597 case ACTION_POINTER_DOWN:
1598 return "ACTION_POINTER_DOWN(" + index + ")";
1599 case ACTION_POINTER_UP:
1600 return "ACTION_POINTER_UP(" + index + ")";
1601 default:
1602 return Integer.toString(action);
1603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605
1606 public static final Parcelable.Creator<MotionEvent> CREATOR
1607 = new Parcelable.Creator<MotionEvent>() {
1608 public MotionEvent createFromParcel(Parcel in) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07001609 in.readInt(); // skip token, we already know this is a MotionEvent
1610 return MotionEvent.createFromParcelBody(in);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 }
1612
1613 public MotionEvent[] newArray(int size) {
1614 return new MotionEvent[size];
1615 }
1616 };
1617
Jeff Brown6ec402b2010-07-28 15:48:59 -07001618 /** @hide */
1619 public static MotionEvent createFromParcelBody(Parcel in) {
1620 final int NP = in.readInt();
1621 final int NS = in.readInt();
1622 final int NI = NP * NS * NUM_SAMPLE_DATA;
1623
1624 MotionEvent ev = obtain(NP, NS);
1625 ev.mNumPointers = NP;
1626 ev.mNumSamples = NS;
1627
1628 ev.readBaseFromParcel(in);
1629
1630 ev.mDownTimeNano = in.readLong();
1631 ev.mAction = in.readInt();
1632 ev.mXOffset = in.readFloat();
1633 ev.mYOffset = in.readFloat();
1634 ev.mXPrecision = in.readFloat();
1635 ev.mYPrecision = in.readFloat();
1636 ev.mEdgeFlags = in.readInt();
1637 ev.mMetaState = in.readInt();
Jeff Brown85a31762010-09-01 17:01:00 -07001638 ev.mFlags = in.readInt();
Jeff Brown6ec402b2010-07-28 15:48:59 -07001639
1640 final int[] pointerIdentifiers = ev.mPointerIdentifiers;
1641 for (int i = 0; i < NP; i++) {
1642 pointerIdentifiers[i] = in.readInt();
1643 }
1644
1645 final long[] eventTimeNanoSamples = ev.mEventTimeNanoSamples;
1646 for (int i = 0; i < NS; i++) {
1647 eventTimeNanoSamples[i] = in.readLong();
1648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649
Jeff Brown6ec402b2010-07-28 15:48:59 -07001650 final float[] dataSamples = ev.mDataSamples;
1651 for (int i = 0; i < NI; i++) {
1652 dataSamples[i] = in.readFloat();
1653 }
1654
1655 ev.mLastEventTimeNanoSampleIndex = NS - 1;
1656 ev.mLastDataSampleIndex = (NS - 1) * NP * NUM_SAMPLE_DATA;
1657 return ev;
1658 }
1659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 public void writeToParcel(Parcel out, int flags) {
Jeff Brown6ec402b2010-07-28 15:48:59 -07001661 out.writeInt(PARCEL_TOKEN_MOTION_EVENT);
1662
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001663 final int NP = mNumPointers;
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001664 final int NS = mNumSamples;
Jeff Brown5c225b12010-06-16 01:53:36 -07001665 final int NI = NP * NS * NUM_SAMPLE_DATA;
1666
1667 out.writeInt(NP);
Dianne Hackborn9822d2b2009-07-20 17:33:15 -07001668 out.writeInt(NS);
Jeff Brown5c225b12010-06-16 01:53:36 -07001669
Jeff Brown6ec402b2010-07-28 15:48:59 -07001670 writeBaseToParcel(out);
1671
Jeff Brown5c225b12010-06-16 01:53:36 -07001672 out.writeLong(mDownTimeNano);
1673 out.writeInt(mAction);
1674 out.writeFloat(mXOffset);
1675 out.writeFloat(mYOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 out.writeFloat(mXPrecision);
1677 out.writeFloat(mYPrecision);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 out.writeInt(mEdgeFlags);
Jeff Brown5c225b12010-06-16 01:53:36 -07001679 out.writeInt(mMetaState);
Jeff Brown85a31762010-09-01 17:01:00 -07001680 out.writeInt(mFlags);
Jeff Brown5c225b12010-06-16 01:53:36 -07001681
1682 final int[] pointerIdentifiers = mPointerIdentifiers;
1683 for (int i = 0; i < NP; i++) {
1684 out.writeInt(pointerIdentifiers[i]);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001685 }
Jeff Brown5c225b12010-06-16 01:53:36 -07001686
1687 final long[] eventTimeNanoSamples = mEventTimeNanoSamples;
1688 for (int i = 0; i < NS; i++) {
1689 out.writeLong(eventTimeNanoSamples[i]);
1690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691
Jeff Brown5c225b12010-06-16 01:53:36 -07001692 final float[] dataSamples = mDataSamples;
1693 for (int i = 0; i < NI; i++) {
1694 out.writeFloat(dataSamples[i]);
1695 }
1696 }
Jeff Brownc5ed5912010-07-14 18:48:53 -07001697
1698 /**
1699 * Transfer object for pointer coordinates.
1700 *
1701 * Objects of this type can be used to manufacture new {@link MotionEvent} objects
1702 * and to query pointer coordinate information in bulk.
1703 *
1704 * Refer to {@link InputDevice} for information about how different kinds of
1705 * input devices and sources represent pointer coordinates.
1706 */
1707 public static final class PointerCoords {
1708 /**
1709 * The X coordinate of the pointer movement.
1710 * The interpretation varies by input source and may represent the position of
1711 * the center of the contact area, a relative displacement in device-specific units
1712 * or something else.
1713 */
1714 public float x;
1715
1716 /**
1717 * The Y coordinate of the pointer movement.
1718 * The interpretation varies by input source and may represent the position of
1719 * the center of the contact area, a relative displacement in device-specific units
1720 * or something else.
1721 */
1722 public float y;
1723
1724 /**
1725 * A scaled value that describes the pressure applied to the pointer.
1726 * The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure),
1727 * however values higher than 1 may be generated depending on the calibration of
1728 * the input device.
1729 */
1730 public float pressure;
1731
1732 /**
1733 * A scaled value of the approximate size of the pointer touch area.
1734 * This represents some approximation of the area of the screen being
1735 * pressed; the actual value in pixels corresponding to the
1736 * touch is normalized with the device specific range of values
1737 * and scaled to a value between 0 and 1. The value of size can be used to
1738 * determine fat touch events.
1739 */
1740 public float size;
1741
1742 /**
1743 * The length of the major axis of an ellipse that describes the touch area at
1744 * the point of contact.
1745 */
1746 public float touchMajor;
1747
1748 /**
1749 * The length of the minor axis of an ellipse that describes the touch area at
1750 * the point of contact.
1751 */
1752 public float touchMinor;
1753
1754 /**
1755 * The length of the major axis of an ellipse that describes the size of
1756 * the approaching tool.
1757 * The tool area represents the estimated size of the finger or pen that is
1758 * touching the device independent of its actual touch area at the point of contact.
1759 */
1760 public float toolMajor;
1761
1762 /**
1763 * The length of the minor axis of an ellipse that describes the size of
1764 * the approaching tool.
1765 * The tool area represents the estimated size of the finger or pen that is
1766 * touching the device independent of its actual touch area at the point of contact.
1767 */
1768 public float toolMinor;
1769
1770 /**
1771 * The orientation of the touch area and tool area in radians clockwise from vertical.
1772 * An angle of 0 degrees indicates that the major axis of contact is oriented
1773 * upwards, is perfectly circular or is of unknown orientation. A positive angle
1774 * indicates that the major axis of contact is oriented to the right. A negative angle
1775 * indicates that the major axis of contact is oriented to the left.
Jeff Brown6d0fec22010-07-23 21:28:06 -07001776 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
Jeff Brownc5ed5912010-07-14 18:48:53 -07001777 * (finger pointing fully right).
1778 */
1779 public float orientation;
1780
1781 /*
Jeff Brown6d0fec22010-07-23 21:28:06 -07001782 private static final float PI_4 = (float) (Math.PI / 4);
Jeff Brownc5ed5912010-07-14 18:48:53 -07001783
1784 public float getTouchWidth() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001785 return Math.abs(orientation) > PI_4 ? touchMajor : touchMinor;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001786 }
1787
1788 public float getTouchHeight() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001789 return Math.abs(orientation) > PI_4 ? touchMinor : touchMajor;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001790 }
1791
1792 public float getToolWidth() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001793 return Math.abs(orientation) > PI_4 ? toolMajor : toolMinor;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001794 }
1795
1796 public float getToolHeight() {
Jeff Brown6d0fec22010-07-23 21:28:06 -07001797 return Math.abs(orientation) > PI_4 ? toolMinor : toolMajor;
Jeff Brownc5ed5912010-07-14 18:48:53 -07001798 }
1799 */
1800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801}