1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.view;
18 
19 import static android.view.Display.DEFAULT_DISPLAY;
20 
21 import static java.lang.annotation.RetentionPolicy.SOURCE;
22 
23 import android.annotation.IntDef;
24 import android.annotation.TestApi;
25 import android.compat.annotation.UnsupportedAppUsage;
26 import android.graphics.Matrix;
27 import android.os.Build;
28 import android.os.Parcel;
29 import android.os.Parcelable;
30 import android.os.SystemClock;
31 import android.util.Log;
32 import android.util.SparseArray;
33 
34 import dalvik.annotation.optimization.CriticalNative;
35 import dalvik.annotation.optimization.FastNative;
36 
37 import java.lang.annotation.Retention;
38 import java.util.Objects;
39 
40 /**
41  * Object used to report movement (mouse, pen, finger, trackball) events.
42  * Motion events may hold either absolute or relative movements and other data,
43  * depending on the type of device.
44  *
45  * <h3>Overview</h3>
46  * <p>
47  * Motion events describe movements in terms of an action code and a set of axis values.
48  * The action code specifies the state change that occurred such as a pointer going
49  * down or up.  The axis values describe the position and other movement properties.
50  * </p><p>
51  * For example, when the user first touches the screen, the system delivers a touch
52  * event to the appropriate {@link View} with the action code {@link #ACTION_DOWN}
53  * and a set of axis values that include the X and Y coordinates of the touch and
54  * information about the pressure, size and orientation of the contact area.
55  * </p><p>
56  * Some devices can report multiple movement traces at the same time.  Multi-touch
57  * screens emit one movement trace for each finger.  The individual fingers or
58  * other objects that generate movement traces are referred to as <em>pointers</em>.
59  * Motion events contain information about all of the pointers that are currently active
60  * even if some of them have not moved since the last event was delivered.
61  * </p><p>
62  * The number of pointers only ever changes by one as individual pointers go up and down,
63  * except when the gesture is canceled.
64  * </p><p>
65  * Each pointer has a unique id that is assigned when it first goes down
66  * (indicated by {@link #ACTION_DOWN} or {@link #ACTION_POINTER_DOWN}).  A pointer id
67  * remains valid until the pointer eventually goes up (indicated by {@link #ACTION_UP}
68  * or {@link #ACTION_POINTER_UP}) or when the gesture is canceled (indicated by
69  * {@link #ACTION_CANCEL}).
70  * </p><p>
71  * The MotionEvent class provides many methods to query the position and other properties of
72  * pointers, such as {@link #getX(int)}, {@link #getY(int)}, {@link #getAxisValue},
73  * {@link #getPointerId(int)}, {@link #getToolType(int)}, and many others.  Most of these
74  * methods accept the pointer index as a parameter rather than the pointer id.
75  * The pointer index of each pointer in the event ranges from 0 to one less than the value
76  * returned by {@link #getPointerCount()}.
77  * </p><p>
78  * The order in which individual pointers appear within a motion event is undefined.
79  * Thus the pointer index of a pointer can change from one event to the next but
80  * the pointer id of a pointer is guaranteed to remain constant as long as the pointer
81  * remains active.  Use the {@link #getPointerId(int)} method to obtain the
82  * pointer id of a pointer to track it across all subsequent motion events in a gesture.
83  * Then for successive motion events, use the {@link #findPointerIndex(int)} method
84  * to obtain the pointer index for a given pointer id in that motion event.
85  * </p><p>
86  * Mouse and stylus buttons can be retrieved using {@link #getButtonState()}.  It is a
87  * good idea to check the button state while handling {@link #ACTION_DOWN} as part
88  * of a touch event.  The application may choose to perform some different action
89  * if the touch event starts due to a secondary button click, such as presenting a
90  * context menu.
91  * </p>
92  *
93  * <h3>Batching</h3>
94  * <p>
95  * For efficiency, motion events with {@link #ACTION_MOVE} may batch together
96  * multiple movement samples within a single object.  The most current
97  * pointer coordinates are available using {@link #getX(int)} and {@link #getY(int)}.
98  * Earlier coordinates within the batch are accessed using {@link #getHistoricalX(int, int)}
99  * and {@link #getHistoricalY(int, int)}.  The coordinates are "historical" only
100  * insofar as they are older than the current coordinates in the batch; however,
101  * they are still distinct from any other coordinates reported in prior motion events.
102  * To process all coordinates in the batch in time order, first consume the historical
103  * coordinates then consume the current coordinates.
104  * </p><p>
105  * Example: Consuming all samples for all pointers in a motion event in time order.
106  * </p><p><pre><code>
107  * void printSamples(MotionEvent ev) {
108  *     final int historySize = ev.getHistorySize();
109  *     final int pointerCount = ev.getPointerCount();
110  *     for (int h = 0; h &lt; historySize; h++) {
111  *         System.out.printf("At time %d:", ev.getHistoricalEventTime(h));
112  *         for (int p = 0; p &lt; pointerCount; p++) {
113  *             System.out.printf("  pointer %d: (%f,%f)",
114  *                 ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h));
115  *         }
116  *     }
117  *     System.out.printf("At time %d:", ev.getEventTime());
118  *     for (int p = 0; p &lt; pointerCount; p++) {
119  *         System.out.printf("  pointer %d: (%f,%f)",
120  *             ev.getPointerId(p), ev.getX(p), ev.getY(p));
121  *     }
122  * }
123  * </code></pre></p>
124  *
125  * <h3>Device Types</h3>
126  * <p>
127  * The interpretation of the contents of a MotionEvent varies significantly depending
128  * on the source class of the device.
129  * </p><p>
130  * On pointing devices with source class {@link InputDevice#SOURCE_CLASS_POINTER}
131  * such as touch screens, the pointer coordinates specify absolute
132  * positions such as view X/Y coordinates.  Each complete gesture is represented
133  * by a sequence of motion events with actions that describe pointer state transitions
134  * and movements.  A gesture starts with a motion event with {@link #ACTION_DOWN}
135  * that provides the location of the first pointer down.  As each additional
136  * pointer that goes down or up, the framework will generate a motion event with
137  * {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} accordingly.
138  * Pointer movements are described by motion events with {@link #ACTION_MOVE}.
139  * Finally, a gesture end either when the final pointer goes up as represented
140  * by a motion event with {@link #ACTION_UP} or when gesture is canceled
141  * with {@link #ACTION_CANCEL}.
142  * </p><p>
143  * Some pointing devices such as mice may support vertical and/or horizontal scrolling.
144  * A scroll event is reported as a generic motion event with {@link #ACTION_SCROLL} that
145  * includes the relative scroll offset in the {@link #AXIS_VSCROLL} and
146  * {@link #AXIS_HSCROLL} axes.  See {@link #getAxisValue(int)} for information
147  * about retrieving these additional axes.
148  * </p><p>
149  * On trackball devices with source class {@link InputDevice#SOURCE_CLASS_TRACKBALL},
150  * the pointer coordinates specify relative movements as X/Y deltas.
151  * A trackball gesture consists of a sequence of movements described by motion
152  * events with {@link #ACTION_MOVE} interspersed with occasional {@link #ACTION_DOWN}
153  * or {@link #ACTION_UP} motion events when the trackball button is pressed or released.
154  * </p><p>
155  * On joystick devices with source class {@link InputDevice#SOURCE_CLASS_JOYSTICK},
156  * the pointer coordinates specify the absolute position of the joystick axes.
157  * The joystick axis values are normalized to a range of -1.0 to 1.0 where 0.0 corresponds
158  * to the center position.  More information about the set of available axes and the
159  * range of motion can be obtained using {@link InputDevice#getMotionRange}.
160  * Some common joystick axes are {@link #AXIS_X}, {@link #AXIS_Y},
161  * {@link #AXIS_HAT_X}, {@link #AXIS_HAT_Y}, {@link #AXIS_Z} and {@link #AXIS_RZ}.
162  * </p><p>
163  * Refer to {@link InputDevice} for more information about how different kinds of
164  * input devices and sources represent pointer coordinates.
165  * </p>
166  *
167  * <h3>Consistency Guarantees</h3>
168  * <p>
169  * Motion events are always delivered to views as a consistent stream of events.
170  * What constitutes a consistent stream varies depending on the type of device.
171  * For touch events, consistency implies that pointers go down one at a time,
172  * move around as a group and then go up one at a time or are canceled.
173  * </p><p>
174  * While the framework tries to deliver consistent streams of motion events to
175  * views, it cannot guarantee it.  Some events may be dropped or modified by
176  * containing views in the application before they are delivered thereby making
177  * the stream of events inconsistent.  Views should always be prepared to
178  * handle {@link #ACTION_CANCEL} and should tolerate anomalous
179  * situations such as receiving a new {@link #ACTION_DOWN} without first having
180  * received an {@link #ACTION_UP} for the prior gesture.
181  * </p>
182  */
183 public final class MotionEvent extends InputEvent implements Parcelable {
184     private static final String TAG = "MotionEvent";
185     private static final long NS_PER_MS = 1000000;
186     private static final String LABEL_PREFIX = "AXIS_";
187 
188     private static final boolean DEBUG_CONCISE_TOSTRING = false;
189 
190     /**
191      * An invalid pointer id.
192      *
193      * This value (-1) can be used as a placeholder to indicate that a pointer id
194      * has not been assigned or is not available.  It cannot appear as
195      * a pointer id inside a {@link MotionEvent}.
196      */
197     public static final int INVALID_POINTER_ID = -1;
198 
199     /**
200      * Bit mask of the parts of the action code that are the action itself.
201      */
202     public static final int ACTION_MASK             = 0xff;
203 
204     /**
205      * Constant for {@link #getActionMasked}: A pressed gesture has started, the
206      * motion contains the initial starting location.
207      * <p>
208      * This is also a good time to check the button state to distinguish
209      * secondary and tertiary button clicks and handle them appropriately.
210      * Use {@link #getButtonState} to retrieve the button state.
211      * </p>
212      */
213     public static final int ACTION_DOWN             = 0;
214 
215     /**
216      * Constant for {@link #getActionMasked}: A pressed gesture has finished, the
217      * motion contains the final release location as well as any intermediate
218      * points since the last down or move event.
219      */
220     public static final int ACTION_UP               = 1;
221 
222     /**
223      * Constant for {@link #getActionMasked}: A change has happened during a
224      * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}).
225      * The motion contains the most recent point, as well as any intermediate
226      * points since the last down or move event.
227      */
228     public static final int ACTION_MOVE             = 2;
229 
230     /**
231      * Constant for {@link #getActionMasked}: The current gesture has been aborted.
232      * You will not receive any more points in it.  You should treat this as
233      * an up event, but not perform any action that you normally would.
234      */
235     public static final int ACTION_CANCEL           = 3;
236 
237     /**
238      * Constant for {@link #getActionMasked}: A movement has happened outside of the
239      * normal bounds of the UI element.  This does not provide a full gesture,
240      * but only the initial location of the movement/touch.
241      * <p>
242      * Note: Because the location of any event will be outside the
243      * bounds of the view hierarchy, it will not get dispatched to
244      * any children of a ViewGroup by default. Therefore,
245      * movements with ACTION_OUTSIDE should be handled in either the
246      * root {@link View} or in the appropriate {@link Window.Callback}
247      * (e.g. {@link android.app.Activity} or {@link android.app.Dialog}).
248      * </p>
249      */
250     public static final int ACTION_OUTSIDE          = 4;
251 
252     /**
253      * Constant for {@link #getActionMasked}: A non-primary pointer has gone down.
254      * <p>
255      * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.
256      * </p><p>
257      * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the
258      * unmasked action returned by {@link #getAction}.
259      * </p>
260      */
261     public static final int ACTION_POINTER_DOWN     = 5;
262 
263     /**
264      * Constant for {@link #getActionMasked}: A non-primary pointer has gone up.
265      * <p>
266      * Use {@link #getActionIndex} to retrieve the index of the pointer that changed.
267      * </p><p>
268      * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the
269      * unmasked action returned by {@link #getAction}.
270      * </p>
271      */
272     public static final int ACTION_POINTER_UP       = 6;
273 
274     /**
275      * Constant for {@link #getActionMasked}: A change happened but the pointer
276      * is not down (unlike {@link #ACTION_MOVE}).  The motion contains the most
277      * recent point, as well as any intermediate points since the last
278      * hover move event.
279      * <p>
280      * This action is always delivered to the window or view under the pointer.
281      * </p><p>
282      * This action is not a touch event so it is delivered to
283      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
284      * {@link View#onTouchEvent(MotionEvent)}.
285      * </p>
286      */
287     public static final int ACTION_HOVER_MOVE       = 7;
288 
289     /**
290      * Constant for {@link #getActionMasked}: The motion event contains relative
291      * vertical and/or horizontal scroll offsets.  Use {@link #getAxisValue(int)}
292      * to retrieve the information from {@link #AXIS_VSCROLL} and {@link #AXIS_HSCROLL}.
293      * The pointer may or may not be down when this event is dispatched.
294      * <p>
295      * This action is always delivered to the window or view under the pointer, which
296      * may not be the window or view currently touched.
297      * </p><p>
298      * This action is not a touch event so it is delivered to
299      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
300      * {@link View#onTouchEvent(MotionEvent)}.
301      * </p>
302      */
303     public static final int ACTION_SCROLL           = 8;
304 
305     /**
306      * Constant for {@link #getActionMasked}: The pointer is not down but has entered the
307      * boundaries of a window or view.
308      * <p>
309      * This action is always delivered to the window or view under the pointer.
310      * </p><p>
311      * This action is not a touch event so it is delivered to
312      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
313      * {@link View#onTouchEvent(MotionEvent)}.
314      * </p>
315      */
316     public static final int ACTION_HOVER_ENTER      = 9;
317 
318     /**
319      * Constant for {@link #getActionMasked}: The pointer is not down but has exited the
320      * boundaries of a window or view.
321      * <p>
322      * This action is always delivered to the window or view that was previously under the pointer.
323      * </p><p>
324      * This action is not a touch event so it is delivered to
325      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
326      * {@link View#onTouchEvent(MotionEvent)}.
327      * </p>
328      */
329     public static final int ACTION_HOVER_EXIT       = 10;
330 
331     /**
332      * Constant for {@link #getActionMasked}: A button has been pressed.
333      *
334      * <p>
335      * Use {@link #getActionButton()} to get which button was pressed.
336      * </p><p>
337      * This action is not a touch event so it is delivered to
338      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
339      * {@link View#onTouchEvent(MotionEvent)}.
340      * </p>
341      */
342     public static final int ACTION_BUTTON_PRESS   = 11;
343 
344     /**
345      * Constant for {@link #getActionMasked}: A button has been released.
346      *
347      * <p>
348      * Use {@link #getActionButton()} to get which button was released.
349      * </p><p>
350      * This action is not a touch event so it is delivered to
351      * {@link View#onGenericMotionEvent(MotionEvent)} rather than
352      * {@link View#onTouchEvent(MotionEvent)}.
353      * </p>
354      */
355     public static final int ACTION_BUTTON_RELEASE  = 12;
356 
357     /**
358      * Bits in the action code that represent a pointer index, used with
359      * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}.  Shifting
360      * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer
361      * index where the data for the pointer going up or down can be found; you can
362      * get its identifier with {@link #getPointerId(int)} and the actual
363      * data with {@link #getX(int)} etc.
364      *
365      * @see #getActionIndex
366      */
367     public static final int ACTION_POINTER_INDEX_MASK  = 0xff00;
368 
369     /**
370      * Bit shift for the action bits holding the pointer index as
371      * defined by {@link #ACTION_POINTER_INDEX_MASK}.
372      *
373      * @see #getActionIndex
374      */
375     public static final int ACTION_POINTER_INDEX_SHIFT = 8;
376 
377     /**
378      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
379      * data index associated with {@link #ACTION_POINTER_DOWN}.
380      */
381     @Deprecated
382     public static final int ACTION_POINTER_1_DOWN   = ACTION_POINTER_DOWN | 0x0000;
383 
384     /**
385      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
386      * data index associated with {@link #ACTION_POINTER_DOWN}.
387      */
388     @Deprecated
389     public static final int ACTION_POINTER_2_DOWN   = ACTION_POINTER_DOWN | 0x0100;
390 
391     /**
392      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
393      * data index associated with {@link #ACTION_POINTER_DOWN}.
394      */
395     @Deprecated
396     public static final int ACTION_POINTER_3_DOWN   = ACTION_POINTER_DOWN | 0x0200;
397 
398     /**
399      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
400      * data index associated with {@link #ACTION_POINTER_UP}.
401      */
402     @Deprecated
403     public static final int ACTION_POINTER_1_UP     = ACTION_POINTER_UP | 0x0000;
404 
405     /**
406      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
407      * data index associated with {@link #ACTION_POINTER_UP}.
408      */
409     @Deprecated
410     public static final int ACTION_POINTER_2_UP     = ACTION_POINTER_UP | 0x0100;
411 
412     /**
413      * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the
414      * data index associated with {@link #ACTION_POINTER_UP}.
415      */
416     @Deprecated
417     public static final int ACTION_POINTER_3_UP     = ACTION_POINTER_UP | 0x0200;
418 
419     /**
420      * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match
421      * the actual data contained in these bits.
422      */
423     @Deprecated
424     public static final int ACTION_POINTER_ID_MASK  = 0xff00;
425 
426     /**
427      * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match
428      * the actual data contained in these bits.
429      */
430     @Deprecated
431     public static final int ACTION_POINTER_ID_SHIFT = 8;
432 
433     /**
434      * This flag indicates that the window that received this motion event is partly
435      * or wholly obscured by another visible window above it. This flag is set to true
436      * if the event directly passed through the obscured area.
437      *
438      * A security sensitive application can check this flag to identify situations in which
439      * a malicious application may have covered up part of its content for the purpose
440      * of misleading the user or hijacking touches.  An appropriate response might be
441      * to drop the suspect touches or to take additional precautions to confirm the user's
442      * actual intent.
443      */
444     public static final int FLAG_WINDOW_IS_OBSCURED = 0x1;
445 
446     /**
447      * This flag indicates that the window that received this motion event is partly
448      * or wholly obscured by another visible window above it. This flag is set to true
449      * even if the event did not directly pass through the obscured area.
450      *
451      * A security sensitive application can check this flag to identify situations in which
452      * a malicious application may have covered up part of its content for the purpose
453      * of misleading the user or hijacking touches.  An appropriate response might be
454      * to drop the suspect touches or to take additional precautions to confirm the user's
455      * actual intent.
456      *
457      * Unlike FLAG_WINDOW_IS_OBSCURED, this is true even if the window that received this event is
458      * obstructed in areas other than the touched location.
459      */
460     public static final int FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2;
461 
462     /**
463      * This private flag is only set on {@link #ACTION_HOVER_MOVE} events and indicates that
464      * this event will be immediately followed by a {@link #ACTION_HOVER_EXIT}. It is used to
465      * prevent generating redundant {@link #ACTION_HOVER_ENTER} events.
466      * @hide
467      */
468     public static final int FLAG_HOVER_EXIT_PENDING = 0x4;
469 
470     /**
471      * This flag indicates that the event has been generated by a gesture generator. It
472      * provides a hint to the GestureDetector to not apply any touch slop.
473      *
474      * @hide
475      */
476     public static final int FLAG_IS_GENERATED_GESTURE = 0x8;
477 
478     /**
479      * Private flag that indicates when the system has detected that this motion event
480      * may be inconsistent with respect to the sequence of previously delivered motion events,
481      * such as when a pointer move event is sent but the pointer is not down.
482      *
483      * @hide
484      * @see #isTainted
485      * @see #setTainted
486      */
487     public static final int FLAG_TAINTED = 0x80000000;
488 
489     /**
490      * Private flag indicating that this event was synthesized by the system and should be delivered
491      * to the accessibility focused view first. When being dispatched such an event is not handled
492      * by predecessors of the accessibility focused view and after the event reaches that view the
493      * flag is cleared and normal event dispatch is performed. This ensures that the platform can
494      * click on any view that has accessibility focus which is semantically equivalent to asking the
495      * view to perform a click accessibility action but more generic as views not implementing click
496      * action correctly can still be activated.
497      *
498      * @hide
499      * @see #isTargetAccessibilityFocus()
500      * @see #setTargetAccessibilityFocus(boolean)
501      */
502     public static final int FLAG_TARGET_ACCESSIBILITY_FOCUS = 0x40000000;
503 
504     /**
505      * Flag indicating the motion event intersected the top edge of the screen.
506      */
507     public static final int EDGE_TOP = 0x00000001;
508 
509     /**
510      * Flag indicating the motion event intersected the bottom edge of the screen.
511      */
512     public static final int EDGE_BOTTOM = 0x00000002;
513 
514     /**
515      * Flag indicating the motion event intersected the left edge of the screen.
516      */
517     public static final int EDGE_LEFT = 0x00000004;
518 
519     /**
520      * Flag indicating the motion event intersected the right edge of the screen.
521      */
522     public static final int EDGE_RIGHT = 0x00000008;
523 
524     /**
525      * Axis constant: X axis of a motion event.
526      * <p>
527      * <ul>
528      * <li>For a touch screen, reports the absolute X screen position of the center of
529      * the touch contact area.  The units are display pixels.
530      * <li>For a touch pad, reports the absolute X surface position of the center of the touch
531      * contact area.  The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
532      * to query the effective range of values.
533      * <li>For a mouse, reports the absolute X screen position of the mouse pointer.
534      * The units are display pixels.
535      * <li>For a trackball, reports the relative horizontal displacement of the trackball.
536      * The value is normalized to a range from -1.0 (left) to 1.0 (right).
537      * <li>For a joystick, reports the absolute X position of the joystick.
538      * The value is normalized to a range from -1.0 (left) to 1.0 (right).
539      * </ul>
540      * </p>
541      *
542      * @see #getX(int)
543      * @see #getHistoricalX(int, int)
544      * @see MotionEvent.PointerCoords#x
545      * @see InputDevice#getMotionRange
546      */
547     public static final int AXIS_X = 0;
548 
549     /**
550      * Axis constant: Y axis of a motion event.
551      * <p>
552      * <ul>
553      * <li>For a touch screen, reports the absolute Y screen position of the center of
554      * the touch contact area.  The units are display pixels.
555      * <li>For a touch pad, reports the absolute Y surface position of the center of the touch
556      * contact area.  The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
557      * to query the effective range of values.
558      * <li>For a mouse, reports the absolute Y screen position of the mouse pointer.
559      * The units are display pixels.
560      * <li>For a trackball, reports the relative vertical displacement of the trackball.
561      * The value is normalized to a range from -1.0 (up) to 1.0 (down).
562      * <li>For a joystick, reports the absolute Y position of the joystick.
563      * The value is normalized to a range from -1.0 (up or far) to 1.0 (down or near).
564      * </ul>
565      * </p>
566      *
567      * @see #getY(int)
568      * @see #getHistoricalY(int, int)
569      * @see MotionEvent.PointerCoords#y
570      * @see InputDevice#getMotionRange
571      */
572     public static final int AXIS_Y = 1;
573 
574     /**
575      * Axis constant: Pressure axis of a motion event.
576      * <p>
577      * <ul>
578      * <li>For a touch screen or touch pad, reports the approximate pressure applied to the surface
579      * by a finger or other tool.  The value is normalized to a range from
580      * 0 (no pressure at all) to 1 (normal pressure), although values higher than 1
581      * may be generated depending on the calibration of the input device.
582      * <li>For a trackball, the value is set to 1 if the trackball button is pressed
583      * or 0 otherwise.
584      * <li>For a mouse, the value is set to 1 if the primary mouse button is pressed
585      * or 0 otherwise.
586      * </ul>
587      * </p>
588      *
589      * @see #getPressure(int)
590      * @see #getHistoricalPressure(int, int)
591      * @see MotionEvent.PointerCoords#pressure
592      * @see InputDevice#getMotionRange
593      */
594     public static final int AXIS_PRESSURE = 2;
595 
596     /**
597      * Axis constant: Size axis of a motion event.
598      * <p>
599      * <ul>
600      * <li>For a touch screen or touch pad, reports the approximate size of the contact area in
601      * relation to the maximum detectable size for the device.  The value is normalized
602      * to a range from 0 (smallest detectable size) to 1 (largest detectable size),
603      * although it is not a linear scale.  This value is of limited use.
604      * To obtain calibrated size information, use
605      * {@link #AXIS_TOUCH_MAJOR} or {@link #AXIS_TOOL_MAJOR}.
606      * </ul>
607      * </p>
608      *
609      * @see #getSize(int)
610      * @see #getHistoricalSize(int, int)
611      * @see MotionEvent.PointerCoords#size
612      * @see InputDevice#getMotionRange
613      */
614     public static final int AXIS_SIZE = 3;
615 
616     /**
617      * Axis constant: TouchMajor axis of a motion event.
618      * <p>
619      * <ul>
620      * <li>For a touch screen, reports the length of the major axis of an ellipse that
621      * represents the touch area at the point of contact.
622      * The units are display pixels.
623      * <li>For a touch pad, reports the length of the major axis of an ellipse that
624      * represents the touch area at the point of contact.
625      * The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
626      * to query the effective range of values.
627      * </ul>
628      * </p>
629      *
630      * @see #getTouchMajor(int)
631      * @see #getHistoricalTouchMajor(int, int)
632      * @see MotionEvent.PointerCoords#touchMajor
633      * @see InputDevice#getMotionRange
634      */
635     public static final int AXIS_TOUCH_MAJOR = 4;
636 
637     /**
638      * Axis constant: TouchMinor axis of a motion event.
639      * <p>
640      * <ul>
641      * <li>For a touch screen, reports the length of the minor axis of an ellipse that
642      * represents the touch area at the point of contact.
643      * The units are display pixels.
644      * <li>For a touch pad, reports the length of the minor axis of an ellipse that
645      * represents the touch area at the point of contact.
646      * The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
647      * to query the effective range of values.
648      * </ul>
649      * </p><p>
650      * When the touch is circular, the major and minor axis lengths will be equal to one another.
651      * </p>
652      *
653      * @see #getTouchMinor(int)
654      * @see #getHistoricalTouchMinor(int, int)
655      * @see MotionEvent.PointerCoords#touchMinor
656      * @see InputDevice#getMotionRange
657      */
658     public static final int AXIS_TOUCH_MINOR = 5;
659 
660     /**
661      * Axis constant: ToolMajor axis of a motion event.
662      * <p>
663      * <ul>
664      * <li>For a touch screen, reports the length of the major axis of an ellipse that
665      * represents the size of the approaching finger or tool used to make contact.
666      * <li>For a touch pad, reports the length of the major axis of an ellipse that
667      * represents the size of the approaching finger or tool used to make contact.
668      * The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
669      * to query the effective range of values.
670      * </ul>
671      * </p><p>
672      * When the touch is circular, the major and minor axis lengths will be equal to one another.
673      * </p><p>
674      * The tool size may be larger than the touch size since the tool may not be fully
675      * in contact with the touch sensor.
676      * </p>
677      *
678      * @see #getToolMajor(int)
679      * @see #getHistoricalToolMajor(int, int)
680      * @see MotionEvent.PointerCoords#toolMajor
681      * @see InputDevice#getMotionRange
682      */
683     public static final int AXIS_TOOL_MAJOR = 6;
684 
685     /**
686      * Axis constant: ToolMinor axis of a motion event.
687      * <p>
688      * <ul>
689      * <li>For a touch screen, reports the length of the minor axis of an ellipse that
690      * represents the size of the approaching finger or tool used to make contact.
691      * <li>For a touch pad, reports the length of the minor axis of an ellipse that
692      * represents the size of the approaching finger or tool used to make contact.
693      * The units are device-dependent; use {@link InputDevice#getMotionRange(int)}
694      * to query the effective range of values.
695      * </ul>
696      * </p><p>
697      * When the touch is circular, the major and minor axis lengths will be equal to one another.
698      * </p><p>
699      * The tool size may be larger than the touch size since the tool may not be fully
700      * in contact with the touch sensor.
701      * </p>
702      *
703      * @see #getToolMinor(int)
704      * @see #getHistoricalToolMinor(int, int)
705      * @see MotionEvent.PointerCoords#toolMinor
706      * @see InputDevice#getMotionRange
707      */
708     public static final int AXIS_TOOL_MINOR = 7;
709 
710     /**
711      * Axis constant: Orientation axis of a motion event.
712      * <p>
713      * <ul>
714      * <li>For a touch screen or touch pad, reports the orientation of the finger
715      * or tool in radians relative to the vertical plane of the device.
716      * An angle of 0 radians indicates that the major axis of contact is oriented
717      * upwards, is perfectly circular or is of unknown orientation.  A positive angle
718      * indicates that the major axis of contact is oriented to the right.  A negative angle
719      * indicates that the major axis of contact is oriented to the left.
720      * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
721      * (finger pointing fully right).
722      * <li>For a stylus, the orientation indicates the direction in which the stylus
723      * is pointing in relation to the vertical axis of the current orientation of the screen.
724      * The range is from -PI radians to PI radians, where 0 is pointing up,
725      * -PI/2 radians is pointing left, -PI or PI radians is pointing down, and PI/2 radians
726      * is pointing right.  See also {@link #AXIS_TILT}.
727      * </ul>
728      * </p>
729      *
730      * @see #getOrientation(int)
731      * @see #getHistoricalOrientation(int, int)
732      * @see MotionEvent.PointerCoords#orientation
733      * @see InputDevice#getMotionRange
734      */
735     public static final int AXIS_ORIENTATION = 8;
736 
737     /**
738      * Axis constant: Vertical Scroll axis of a motion event.
739      * <p>
740      * <ul>
741      * <li>For a mouse, reports the relative movement of the vertical scroll wheel.
742      * The value is normalized to a range from -1.0 (down) to 1.0 (up).
743      * </ul>
744      * </p><p>
745      * This axis should be used to scroll views vertically.
746      * </p>
747      *
748      * @see #getAxisValue(int, int)
749      * @see #getHistoricalAxisValue(int, int, int)
750      * @see MotionEvent.PointerCoords#getAxisValue(int)
751      * @see InputDevice#getMotionRange
752      */
753     public static final int AXIS_VSCROLL = 9;
754 
755     /**
756      * Axis constant: Horizontal Scroll axis of a motion event.
757      * <p>
758      * <ul>
759      * <li>For a mouse, reports the relative movement of the horizontal scroll wheel.
760      * The value is normalized to a range from -1.0 (left) to 1.0 (right).
761      * </ul>
762      * </p><p>
763      * This axis should be used to scroll views horizontally.
764      * </p>
765      *
766      * @see #getAxisValue(int, int)
767      * @see #getHistoricalAxisValue(int, int, int)
768      * @see MotionEvent.PointerCoords#getAxisValue(int)
769      * @see InputDevice#getMotionRange
770      */
771     public static final int AXIS_HSCROLL = 10;
772 
773     /**
774      * Axis constant: Z axis of a motion event.
775      * <p>
776      * <ul>
777      * <li>For a joystick, reports the absolute Z position of the joystick.
778      * The value is normalized to a range from -1.0 (high) to 1.0 (low).
779      * <em>On game pads with two analog joysticks, this axis is often reinterpreted
780      * to report the absolute X position of the second joystick instead.</em>
781      * </ul>
782      * </p>
783      *
784      * @see #getAxisValue(int, int)
785      * @see #getHistoricalAxisValue(int, int, int)
786      * @see MotionEvent.PointerCoords#getAxisValue(int)
787      * @see InputDevice#getMotionRange
788      */
789     public static final int AXIS_Z = 11;
790 
791     /**
792      * Axis constant: X Rotation axis of a motion event.
793      * <p>
794      * <ul>
795      * <li>For a joystick, reports the absolute rotation angle about the X axis.
796      * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise).
797      * </ul>
798      * </p>
799      *
800      * @see #getAxisValue(int, int)
801      * @see #getHistoricalAxisValue(int, int, int)
802      * @see MotionEvent.PointerCoords#getAxisValue(int)
803      * @see InputDevice#getMotionRange
804      */
805     public static final int AXIS_RX = 12;
806 
807     /**
808      * Axis constant: Y Rotation axis of a motion event.
809      * <p>
810      * <ul>
811      * <li>For a joystick, reports the absolute rotation angle about the Y axis.
812      * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise).
813      * </ul>
814      * </p>
815      *
816      * @see #getAxisValue(int, int)
817      * @see #getHistoricalAxisValue(int, int, int)
818      * @see MotionEvent.PointerCoords#getAxisValue(int)
819      * @see InputDevice#getMotionRange
820      */
821     public static final int AXIS_RY = 13;
822 
823     /**
824      * Axis constant: Z Rotation axis of a motion event.
825      * <p>
826      * <ul>
827      * <li>For a joystick, reports the absolute rotation angle about the Z axis.
828      * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise).
829      * <em>On game pads with two analog joysticks, this axis is often reinterpreted
830      * to report the absolute Y position of the second joystick instead.</em>
831      * </ul>
832      * </p>
833      *
834      * @see #getAxisValue(int, int)
835      * @see #getHistoricalAxisValue(int, int, int)
836      * @see MotionEvent.PointerCoords#getAxisValue(int)
837      * @see InputDevice#getMotionRange
838      */
839     public static final int AXIS_RZ = 14;
840 
841     /**
842      * Axis constant: Hat X axis of a motion event.
843      * <p>
844      * <ul>
845      * <li>For a joystick, reports the absolute X position of the directional hat control.
846      * The value is normalized to a range from -1.0 (left) to 1.0 (right).
847      * </ul>
848      * </p>
849      *
850      * @see #getAxisValue(int, int)
851      * @see #getHistoricalAxisValue(int, int, int)
852      * @see MotionEvent.PointerCoords#getAxisValue(int)
853      * @see InputDevice#getMotionRange
854      */
855     public static final int AXIS_HAT_X = 15;
856 
857     /**
858      * Axis constant: Hat Y axis of a motion event.
859      * <p>
860      * <ul>
861      * <li>For a joystick, reports the absolute Y position of the directional hat control.
862      * The value is normalized to a range from -1.0 (up) to 1.0 (down).
863      * </ul>
864      * </p>
865      *
866      * @see #getAxisValue(int, int)
867      * @see #getHistoricalAxisValue(int, int, int)
868      * @see MotionEvent.PointerCoords#getAxisValue(int)
869      * @see InputDevice#getMotionRange
870      */
871     public static final int AXIS_HAT_Y = 16;
872 
873     /**
874      * Axis constant: Left Trigger axis of a motion event.
875      * <p>
876      * <ul>
877      * <li>For a joystick, reports the absolute position of the left trigger control.
878      * The value is normalized to a range from 0.0 (released) to 1.0 (fully pressed).
879      * </ul>
880      * </p>
881      *
882      * @see #getAxisValue(int, int)
883      * @see #getHistoricalAxisValue(int, int, int)
884      * @see MotionEvent.PointerCoords#getAxisValue(int)
885      * @see InputDevice#getMotionRange
886      */
887     public static final int AXIS_LTRIGGER = 17;
888 
889     /**
890      * Axis constant: Right Trigger axis of a motion event.
891      * <p>
892      * <ul>
893      * <li>For a joystick, reports the absolute position of the right trigger control.
894      * The value is normalized to a range from 0.0 (released) to 1.0 (fully pressed).
895      * </ul>
896      * </p>
897      *
898      * @see #getAxisValue(int, int)
899      * @see #getHistoricalAxisValue(int, int, int)
900      * @see MotionEvent.PointerCoords#getAxisValue(int)
901      * @see InputDevice#getMotionRange
902      */
903     public static final int AXIS_RTRIGGER = 18;
904 
905     /**
906      * Axis constant: Throttle axis of a motion event.
907      * <p>
908      * <ul>
909      * <li>For a joystick, reports the absolute position of the throttle control.
910      * The value is normalized to a range from 0.0 (fully open) to 1.0 (fully closed).
911      * </ul>
912      * </p>
913      *
914      * @see #getAxisValue(int, int)
915      * @see #getHistoricalAxisValue(int, int, int)
916      * @see MotionEvent.PointerCoords#getAxisValue(int)
917      * @see InputDevice#getMotionRange
918      */
919     public static final int AXIS_THROTTLE = 19;
920 
921     /**
922      * Axis constant: Rudder axis of a motion event.
923      * <p>
924      * <ul>
925      * <li>For a joystick, reports the absolute position of the rudder control.
926      * The value is normalized to a range from -1.0 (turn left) to 1.0 (turn right).
927      * </ul>
928      * </p>
929      *
930      * @see #getAxisValue(int, int)
931      * @see #getHistoricalAxisValue(int, int, int)
932      * @see MotionEvent.PointerCoords#getAxisValue(int)
933      * @see InputDevice#getMotionRange
934      */
935     public static final int AXIS_RUDDER = 20;
936 
937     /**
938      * Axis constant: Wheel axis of a motion event.
939      * <p>
940      * <ul>
941      * <li>For a joystick, reports the absolute position of the steering wheel control.
942      * The value is normalized to a range from -1.0 (turn left) to 1.0 (turn right).
943      * </ul>
944      * </p>
945      *
946      * @see #getAxisValue(int, int)
947      * @see #getHistoricalAxisValue(int, int, int)
948      * @see MotionEvent.PointerCoords#getAxisValue(int)
949      * @see InputDevice#getMotionRange
950      */
951     public static final int AXIS_WHEEL = 21;
952 
953     /**
954      * Axis constant: Gas axis of a motion event.
955      * <p>
956      * <ul>
957      * <li>For a joystick, reports the absolute position of the gas (accelerator) control.
958      * The value is normalized to a range from 0.0 (no acceleration)
959      * to 1.0 (maximum acceleration).
960      * </ul>
961      * </p>
962      *
963      * @see #getAxisValue(int, int)
964      * @see #getHistoricalAxisValue(int, int, int)
965      * @see MotionEvent.PointerCoords#getAxisValue(int)
966      * @see InputDevice#getMotionRange
967      */
968     public static final int AXIS_GAS = 22;
969 
970     /**
971      * Axis constant: Brake axis of a motion event.
972      * <p>
973      * <ul>
974      * <li>For a joystick, reports the absolute position of the brake control.
975      * The value is normalized to a range from 0.0 (no braking) to 1.0 (maximum braking).
976      * </ul>
977      * </p>
978      *
979      * @see #getAxisValue(int, int)
980      * @see #getHistoricalAxisValue(int, int, int)
981      * @see MotionEvent.PointerCoords#getAxisValue(int)
982      * @see InputDevice#getMotionRange
983      */
984     public static final int AXIS_BRAKE = 23;
985 
986     /**
987      * Axis constant: Distance axis of a motion event.
988      * <p>
989      * <ul>
990      * <li>For a stylus, reports the distance of the stylus from the screen.
991      * A value of 0.0 indicates direct contact and larger values indicate increasing
992      * distance from the surface.
993      * </ul>
994      * </p>
995      *
996      * @see #getAxisValue(int, int)
997      * @see #getHistoricalAxisValue(int, int, int)
998      * @see MotionEvent.PointerCoords#getAxisValue(int)
999      * @see InputDevice#getMotionRange
1000      */
1001     public static final int AXIS_DISTANCE = 24;
1002 
1003     /**
1004      * Axis constant: Tilt axis of a motion event.
1005      * <p>
1006      * <ul>
1007      * <li>For a stylus, reports the tilt angle of the stylus in radians where
1008      * 0 radians indicates that the stylus is being held perpendicular to the
1009      * surface, and PI/2 radians indicates that the stylus is being held flat
1010      * against the surface.
1011      * </ul>
1012      * </p>
1013      *
1014      * @see #getAxisValue(int, int)
1015      * @see #getHistoricalAxisValue(int, int, int)
1016      * @see MotionEvent.PointerCoords#getAxisValue(int, int)
1017      * @see InputDevice#getMotionRange
1018      */
1019     public static final int AXIS_TILT = 25;
1020 
1021     /**
1022      * Axis constant: Generic scroll axis of a motion event.
1023      * <p>
1024      * <ul>
1025      * <li>Reports the relative movement of the generic scrolling device.
1026      * </ul>
1027      * </p><p>
1028      * This axis should be used for scroll events that are neither strictly vertical nor horizontal.
1029      * A good example would be the rotation of a rotary encoder input device.
1030      * </p>
1031      *
1032      * @see #getAxisValue(int, int)
1033      */
1034     public static final int AXIS_SCROLL = 26;
1035 
1036     /**
1037      * Axis constant: The movement of x position of a motion event.
1038      * <p>
1039      * <ul>
1040      * <li>For a mouse, reports a difference of x position between the previous position.
1041      * This is useful when pointer is captured, in that case the mouse pointer doesn't change
1042      * the location but this axis reports the difference which allows the app to see
1043      * how the mouse is moved.
1044      * </ul>
1045      * </p>
1046      *
1047      * @see #getAxisValue(int, int)
1048      * @see #getHistoricalAxisValue(int, int, int)
1049      * @see MotionEvent.PointerCoords#getAxisValue(int, int)
1050      * @see InputDevice#getMotionRange
1051      */
1052     public static final int AXIS_RELATIVE_X = 27;
1053 
1054     /**
1055      * Axis constant: The movement of y position of a motion event.
1056      * <p>
1057      * This is similar to {@link #AXIS_RELATIVE_X} but for y-axis.
1058      * </p>
1059      *
1060      * @see #getAxisValue(int, int)
1061      * @see #getHistoricalAxisValue(int, int, int)
1062      * @see MotionEvent.PointerCoords#getAxisValue(int, int)
1063      * @see InputDevice#getMotionRange
1064      */
1065     public static final int AXIS_RELATIVE_Y = 28;
1066 
1067     /**
1068      * Axis constant: Generic 1 axis of a motion event.
1069      * The interpretation of a generic axis is device-specific.
1070      *
1071      * @see #getAxisValue(int, int)
1072      * @see #getHistoricalAxisValue(int, int, int)
1073      * @see MotionEvent.PointerCoords#getAxisValue(int)
1074      * @see InputDevice#getMotionRange
1075      */
1076     public static final int AXIS_GENERIC_1 = 32;
1077 
1078     /**
1079      * Axis constant: Generic 2 axis of a motion event.
1080      * The interpretation of a generic axis is device-specific.
1081      *
1082      * @see #getAxisValue(int, int)
1083      * @see #getHistoricalAxisValue(int, int, int)
1084      * @see MotionEvent.PointerCoords#getAxisValue(int)
1085      * @see InputDevice#getMotionRange
1086      */
1087     public static final int AXIS_GENERIC_2 = 33;
1088 
1089     /**
1090      * Axis constant: Generic 3 axis of a motion event.
1091      * The interpretation of a generic axis is device-specific.
1092      *
1093      * @see #getAxisValue(int, int)
1094      * @see #getHistoricalAxisValue(int, int, int)
1095      * @see MotionEvent.PointerCoords#getAxisValue(int)
1096      * @see InputDevice#getMotionRange
1097      */
1098     public static final int AXIS_GENERIC_3 = 34;
1099 
1100     /**
1101      * Axis constant: Generic 4 axis of a motion event.
1102      * The interpretation of a generic axis is device-specific.
1103      *
1104      * @see #getAxisValue(int, int)
1105      * @see #getHistoricalAxisValue(int, int, int)
1106      * @see MotionEvent.PointerCoords#getAxisValue(int)
1107      * @see InputDevice#getMotionRange
1108      */
1109     public static final int AXIS_GENERIC_4 = 35;
1110 
1111     /**
1112      * Axis constant: Generic 5 axis of a motion event.
1113      * The interpretation of a generic axis is device-specific.
1114      *
1115      * @see #getAxisValue(int, int)
1116      * @see #getHistoricalAxisValue(int, int, int)
1117      * @see MotionEvent.PointerCoords#getAxisValue(int)
1118      * @see InputDevice#getMotionRange
1119      */
1120     public static final int AXIS_GENERIC_5 = 36;
1121 
1122     /**
1123      * Axis constant: Generic 6 axis of a motion event.
1124      * The interpretation of a generic axis is device-specific.
1125      *
1126      * @see #getAxisValue(int, int)
1127      * @see #getHistoricalAxisValue(int, int, int)
1128      * @see MotionEvent.PointerCoords#getAxisValue(int)
1129      * @see InputDevice#getMotionRange
1130      */
1131     public static final int AXIS_GENERIC_6 = 37;
1132 
1133     /**
1134      * Axis constant: Generic 7 axis of a motion event.
1135      * The interpretation of a generic axis is device-specific.
1136      *
1137      * @see #getAxisValue(int, int)
1138      * @see #getHistoricalAxisValue(int, int, int)
1139      * @see MotionEvent.PointerCoords#getAxisValue(int)
1140      * @see InputDevice#getMotionRange
1141      */
1142     public static final int AXIS_GENERIC_7 = 38;
1143 
1144     /**
1145      * Axis constant: Generic 8 axis of a motion event.
1146      * The interpretation of a generic axis is device-specific.
1147      *
1148      * @see #getAxisValue(int, int)
1149      * @see #getHistoricalAxisValue(int, int, int)
1150      * @see MotionEvent.PointerCoords#getAxisValue(int)
1151      * @see InputDevice#getMotionRange
1152      */
1153     public static final int AXIS_GENERIC_8 = 39;
1154 
1155     /**
1156      * Axis constant: Generic 9 axis of a motion event.
1157      * The interpretation of a generic axis is device-specific.
1158      *
1159      * @see #getAxisValue(int, int)
1160      * @see #getHistoricalAxisValue(int, int, int)
1161      * @see MotionEvent.PointerCoords#getAxisValue(int)
1162      * @see InputDevice#getMotionRange
1163      */
1164     public static final int AXIS_GENERIC_9 = 40;
1165 
1166     /**
1167      * Axis constant: Generic 10 axis of a motion event.
1168      * The interpretation of a generic axis is device-specific.
1169      *
1170      * @see #getAxisValue(int, int)
1171      * @see #getHistoricalAxisValue(int, int, int)
1172      * @see MotionEvent.PointerCoords#getAxisValue(int)
1173      * @see InputDevice#getMotionRange
1174      */
1175     public static final int AXIS_GENERIC_10 = 41;
1176 
1177     /**
1178      * Axis constant: Generic 11 axis of a motion event.
1179      * The interpretation of a generic axis is device-specific.
1180      *
1181      * @see #getAxisValue(int, int)
1182      * @see #getHistoricalAxisValue(int, int, int)
1183      * @see MotionEvent.PointerCoords#getAxisValue(int)
1184      * @see InputDevice#getMotionRange
1185      */
1186     public static final int AXIS_GENERIC_11 = 42;
1187 
1188     /**
1189      * Axis constant: Generic 12 axis of a motion event.
1190      * The interpretation of a generic axis is device-specific.
1191      *
1192      * @see #getAxisValue(int, int)
1193      * @see #getHistoricalAxisValue(int, int, int)
1194      * @see MotionEvent.PointerCoords#getAxisValue(int)
1195      * @see InputDevice#getMotionRange
1196      */
1197     public static final int AXIS_GENERIC_12 = 43;
1198 
1199     /**
1200      * Axis constant: Generic 13 axis of a motion event.
1201      * The interpretation of a generic axis is device-specific.
1202      *
1203      * @see #getAxisValue(int, int)
1204      * @see #getHistoricalAxisValue(int, int, int)
1205      * @see MotionEvent.PointerCoords#getAxisValue(int)
1206      * @see InputDevice#getMotionRange
1207      */
1208     public static final int AXIS_GENERIC_13 = 44;
1209 
1210     /**
1211      * Axis constant: Generic 14 axis of a motion event.
1212      * The interpretation of a generic axis is device-specific.
1213      *
1214      * @see #getAxisValue(int, int)
1215      * @see #getHistoricalAxisValue(int, int, int)
1216      * @see MotionEvent.PointerCoords#getAxisValue(int)
1217      * @see InputDevice#getMotionRange
1218      */
1219     public static final int AXIS_GENERIC_14 = 45;
1220 
1221     /**
1222      * Axis constant: Generic 15 axis of a motion event.
1223      * The interpretation of a generic axis is device-specific.
1224      *
1225      * @see #getAxisValue(int, int)
1226      * @see #getHistoricalAxisValue(int, int, int)
1227      * @see MotionEvent.PointerCoords#getAxisValue(int)
1228      * @see InputDevice#getMotionRange
1229      */
1230     public static final int AXIS_GENERIC_15 = 46;
1231 
1232     /**
1233      * Axis constant: Generic 16 axis of a motion event.
1234      * The interpretation of a generic axis is device-specific.
1235      *
1236      * @see #getAxisValue(int, int)
1237      * @see #getHistoricalAxisValue(int, int, int)
1238      * @see MotionEvent.PointerCoords#getAxisValue(int)
1239      * @see InputDevice#getMotionRange
1240      */
1241     public static final int AXIS_GENERIC_16 = 47;
1242 
1243     // NOTE: If you add a new axis here you must also add it to:
1244     //  native/include/android/input.h
1245     //  frameworks/base/include/ui/KeycodeLabels.h
1246 
1247     // Symbolic names of all axes.
1248     private static final SparseArray<String> AXIS_SYMBOLIC_NAMES = new SparseArray<String>();
1249     static {
1250         SparseArray<String> names = AXIS_SYMBOLIC_NAMES;
names.append(AXIS_X, "AXIS_X")1251         names.append(AXIS_X, "AXIS_X");
names.append(AXIS_Y, "AXIS_Y")1252         names.append(AXIS_Y, "AXIS_Y");
names.append(AXIS_PRESSURE, "AXIS_PRESSURE")1253         names.append(AXIS_PRESSURE, "AXIS_PRESSURE");
names.append(AXIS_SIZE, "AXIS_SIZE")1254         names.append(AXIS_SIZE, "AXIS_SIZE");
names.append(AXIS_TOUCH_MAJOR, "AXIS_TOUCH_MAJOR")1255         names.append(AXIS_TOUCH_MAJOR, "AXIS_TOUCH_MAJOR");
names.append(AXIS_TOUCH_MINOR, "AXIS_TOUCH_MINOR")1256         names.append(AXIS_TOUCH_MINOR, "AXIS_TOUCH_MINOR");
names.append(AXIS_TOOL_MAJOR, "AXIS_TOOL_MAJOR")1257         names.append(AXIS_TOOL_MAJOR, "AXIS_TOOL_MAJOR");
names.append(AXIS_TOOL_MINOR, "AXIS_TOOL_MINOR")1258         names.append(AXIS_TOOL_MINOR, "AXIS_TOOL_MINOR");
names.append(AXIS_ORIENTATION, "AXIS_ORIENTATION")1259         names.append(AXIS_ORIENTATION, "AXIS_ORIENTATION");
names.append(AXIS_VSCROLL, "AXIS_VSCROLL")1260         names.append(AXIS_VSCROLL, "AXIS_VSCROLL");
names.append(AXIS_HSCROLL, "AXIS_HSCROLL")1261         names.append(AXIS_HSCROLL, "AXIS_HSCROLL");
names.append(AXIS_Z, "AXIS_Z")1262         names.append(AXIS_Z, "AXIS_Z");
names.append(AXIS_RX, "AXIS_RX")1263         names.append(AXIS_RX, "AXIS_RX");
names.append(AXIS_RY, "AXIS_RY")1264         names.append(AXIS_RY, "AXIS_RY");
names.append(AXIS_RZ, "AXIS_RZ")1265         names.append(AXIS_RZ, "AXIS_RZ");
names.append(AXIS_HAT_X, "AXIS_HAT_X")1266         names.append(AXIS_HAT_X, "AXIS_HAT_X");
names.append(AXIS_HAT_Y, "AXIS_HAT_Y")1267         names.append(AXIS_HAT_Y, "AXIS_HAT_Y");
names.append(AXIS_LTRIGGER, "AXIS_LTRIGGER")1268         names.append(AXIS_LTRIGGER, "AXIS_LTRIGGER");
names.append(AXIS_RTRIGGER, "AXIS_RTRIGGER")1269         names.append(AXIS_RTRIGGER, "AXIS_RTRIGGER");
names.append(AXIS_THROTTLE, "AXIS_THROTTLE")1270         names.append(AXIS_THROTTLE, "AXIS_THROTTLE");
names.append(AXIS_RUDDER, "AXIS_RUDDER")1271         names.append(AXIS_RUDDER, "AXIS_RUDDER");
names.append(AXIS_WHEEL, "AXIS_WHEEL")1272         names.append(AXIS_WHEEL, "AXIS_WHEEL");
names.append(AXIS_GAS, "AXIS_GAS")1273         names.append(AXIS_GAS, "AXIS_GAS");
names.append(AXIS_BRAKE, "AXIS_BRAKE")1274         names.append(AXIS_BRAKE, "AXIS_BRAKE");
names.append(AXIS_DISTANCE, "AXIS_DISTANCE")1275         names.append(AXIS_DISTANCE, "AXIS_DISTANCE");
names.append(AXIS_TILT, "AXIS_TILT")1276         names.append(AXIS_TILT, "AXIS_TILT");
names.append(AXIS_SCROLL, "AXIS_SCROLL")1277         names.append(AXIS_SCROLL, "AXIS_SCROLL");
names.append(AXIS_RELATIVE_X, "AXIS_REALTIVE_X")1278         names.append(AXIS_RELATIVE_X, "AXIS_REALTIVE_X");
names.append(AXIS_RELATIVE_Y, "AXIS_REALTIVE_Y")1279         names.append(AXIS_RELATIVE_Y, "AXIS_REALTIVE_Y");
names.append(AXIS_GENERIC_1, "AXIS_GENERIC_1")1280         names.append(AXIS_GENERIC_1, "AXIS_GENERIC_1");
names.append(AXIS_GENERIC_2, "AXIS_GENERIC_2")1281         names.append(AXIS_GENERIC_2, "AXIS_GENERIC_2");
names.append(AXIS_GENERIC_3, "AXIS_GENERIC_3")1282         names.append(AXIS_GENERIC_3, "AXIS_GENERIC_3");
names.append(AXIS_GENERIC_4, "AXIS_GENERIC_4")1283         names.append(AXIS_GENERIC_4, "AXIS_GENERIC_4");
names.append(AXIS_GENERIC_5, "AXIS_GENERIC_5")1284         names.append(AXIS_GENERIC_5, "AXIS_GENERIC_5");
names.append(AXIS_GENERIC_6, "AXIS_GENERIC_6")1285         names.append(AXIS_GENERIC_6, "AXIS_GENERIC_6");
names.append(AXIS_GENERIC_7, "AXIS_GENERIC_7")1286         names.append(AXIS_GENERIC_7, "AXIS_GENERIC_7");
names.append(AXIS_GENERIC_8, "AXIS_GENERIC_8")1287         names.append(AXIS_GENERIC_8, "AXIS_GENERIC_8");
names.append(AXIS_GENERIC_9, "AXIS_GENERIC_9")1288         names.append(AXIS_GENERIC_9, "AXIS_GENERIC_9");
names.append(AXIS_GENERIC_10, "AXIS_GENERIC_10")1289         names.append(AXIS_GENERIC_10, "AXIS_GENERIC_10");
names.append(AXIS_GENERIC_11, "AXIS_GENERIC_11")1290         names.append(AXIS_GENERIC_11, "AXIS_GENERIC_11");
names.append(AXIS_GENERIC_12, "AXIS_GENERIC_12")1291         names.append(AXIS_GENERIC_12, "AXIS_GENERIC_12");
names.append(AXIS_GENERIC_13, "AXIS_GENERIC_13")1292         names.append(AXIS_GENERIC_13, "AXIS_GENERIC_13");
names.append(AXIS_GENERIC_14, "AXIS_GENERIC_14")1293         names.append(AXIS_GENERIC_14, "AXIS_GENERIC_14");
names.append(AXIS_GENERIC_15, "AXIS_GENERIC_15")1294         names.append(AXIS_GENERIC_15, "AXIS_GENERIC_15");
names.append(AXIS_GENERIC_16, "AXIS_GENERIC_16")1295         names.append(AXIS_GENERIC_16, "AXIS_GENERIC_16");
1296     }
1297 
1298     /**
1299      * Button constant: Primary button (left mouse button).
1300      *
1301      * This button constant is not set in response to simple touches with a finger
1302      * or stylus tip.  The user must actually push a button.
1303      *
1304      * @see #getButtonState
1305      */
1306     public static final int BUTTON_PRIMARY = 1 << 0;
1307 
1308     /**
1309      * Button constant: Secondary button (right mouse button).
1310      *
1311      * @see #getButtonState
1312      */
1313     public static final int BUTTON_SECONDARY = 1 << 1;
1314 
1315     /**
1316      * Button constant: Tertiary button (middle mouse button).
1317      *
1318      * @see #getButtonState
1319      */
1320     public static final int BUTTON_TERTIARY = 1 << 2;
1321 
1322     /**
1323      * Button constant: Back button pressed (mouse back button).
1324      * <p>
1325      * The system may send a {@link KeyEvent#KEYCODE_BACK} key press to the application
1326      * when this button is pressed.
1327      * </p>
1328      *
1329      * @see #getButtonState
1330      */
1331     public static final int BUTTON_BACK = 1 << 3;
1332 
1333     /**
1334      * Button constant: Forward button pressed (mouse forward button).
1335      * <p>
1336      * The system may send a {@link KeyEvent#KEYCODE_FORWARD} key press to the application
1337      * when this button is pressed.
1338      * </p>
1339      *
1340      * @see #getButtonState
1341      */
1342     public static final int BUTTON_FORWARD = 1 << 4;
1343 
1344     /**
1345      * Button constant: Primary stylus button pressed.
1346      *
1347      * @see #getButtonState
1348      */
1349     public static final int BUTTON_STYLUS_PRIMARY = 1 << 5;
1350 
1351     /**
1352      * Button constant: Secondary stylus button pressed.
1353      *
1354      * @see #getButtonState
1355      */
1356     public static final int BUTTON_STYLUS_SECONDARY = 1 << 6;
1357 
1358     // NOTE: If you add a new axis here you must also add it to:
1359     //  native/include/android/input.h
1360 
1361     // Symbolic names of all button states in bit order from least significant
1362     // to most significant.
1363     private static final String[] BUTTON_SYMBOLIC_NAMES = new String[] {
1364         "BUTTON_PRIMARY",
1365         "BUTTON_SECONDARY",
1366         "BUTTON_TERTIARY",
1367         "BUTTON_BACK",
1368         "BUTTON_FORWARD",
1369         "BUTTON_STYLUS_PRIMARY",
1370         "BUTTON_STYLUS_SECONDARY",
1371         "0x00000080",
1372         "0x00000100",
1373         "0x00000200",
1374         "0x00000400",
1375         "0x00000800",
1376         "0x00001000",
1377         "0x00002000",
1378         "0x00004000",
1379         "0x00008000",
1380         "0x00010000",
1381         "0x00020000",
1382         "0x00040000",
1383         "0x00080000",
1384         "0x00100000",
1385         "0x00200000",
1386         "0x00400000",
1387         "0x00800000",
1388         "0x01000000",
1389         "0x02000000",
1390         "0x04000000",
1391         "0x08000000",
1392         "0x10000000",
1393         "0x20000000",
1394         "0x40000000",
1395         "0x80000000",
1396     };
1397 
1398     /**
1399      * Classification constant: None.
1400      *
1401      * No additional information is available about the current motion event stream.
1402      *
1403      * @see #getClassification
1404      */
1405     public static final int CLASSIFICATION_NONE = 0;
1406 
1407     /**
1408      * Classification constant: Ambiguous gesture.
1409      *
1410      * The user's intent with respect to the current event stream is not yet determined.
1411      * Gestural actions, such as scrolling, should be inhibited until the classification resolves
1412      * to another value or the event stream ends.
1413      *
1414      * @see #getClassification
1415      */
1416     public static final int CLASSIFICATION_AMBIGUOUS_GESTURE = 1;
1417 
1418     /**
1419      * Classification constant: Deep press.
1420      *
1421      * The current event stream represents the user intentionally pressing harder on the screen.
1422      * This classification type should be used to accelerate the long press behaviour.
1423      *
1424      * @see #getClassification
1425      */
1426     public static final int CLASSIFICATION_DEEP_PRESS = 2;
1427 
1428     /** @hide */
1429     @Retention(SOURCE)
1430     @IntDef(prefix = { "CLASSIFICATION" }, value = {
1431             CLASSIFICATION_NONE, CLASSIFICATION_AMBIGUOUS_GESTURE, CLASSIFICATION_DEEP_PRESS})
1432     public @interface Classification {};
1433 
1434     /**
1435      * Tool type constant: Unknown tool type.
1436      * This constant is used when the tool type is not known or is not relevant,
1437      * such as for a trackball or other non-pointing device.
1438      *
1439      * @see #getToolType
1440      */
1441     public static final int TOOL_TYPE_UNKNOWN = 0;
1442 
1443     /**
1444      * Tool type constant: The tool is a finger.
1445      *
1446      * @see #getToolType
1447      */
1448     public static final int TOOL_TYPE_FINGER = 1;
1449 
1450     /**
1451      * Tool type constant: The tool is a stylus.
1452      *
1453      * @see #getToolType
1454      */
1455     public static final int TOOL_TYPE_STYLUS = 2;
1456 
1457     /**
1458      * Tool type constant: The tool is a mouse.
1459      *
1460      * @see #getToolType
1461      */
1462     public static final int TOOL_TYPE_MOUSE = 3;
1463 
1464     /**
1465      * Tool type constant: The tool is an eraser or a stylus being used in an inverted posture.
1466      *
1467      * @see #getToolType
1468      */
1469     public static final int TOOL_TYPE_ERASER = 4;
1470 
1471     // NOTE: If you add a new tool type here you must also add it to:
1472     //  native/include/android/input.h
1473 
1474     // Symbolic names of all tool types.
1475     private static final SparseArray<String> TOOL_TYPE_SYMBOLIC_NAMES = new SparseArray<String>();
1476     static {
1477         SparseArray<String> names = TOOL_TYPE_SYMBOLIC_NAMES;
names.append(TOOL_TYPE_UNKNOWN, "TOOL_TYPE_UNKNOWN")1478         names.append(TOOL_TYPE_UNKNOWN, "TOOL_TYPE_UNKNOWN");
names.append(TOOL_TYPE_FINGER, "TOOL_TYPE_FINGER")1479         names.append(TOOL_TYPE_FINGER, "TOOL_TYPE_FINGER");
names.append(TOOL_TYPE_STYLUS, "TOOL_TYPE_STYLUS")1480         names.append(TOOL_TYPE_STYLUS, "TOOL_TYPE_STYLUS");
names.append(TOOL_TYPE_MOUSE, "TOOL_TYPE_MOUSE")1481         names.append(TOOL_TYPE_MOUSE, "TOOL_TYPE_MOUSE");
names.append(TOOL_TYPE_ERASER, "TOOL_TYPE_ERASER")1482         names.append(TOOL_TYPE_ERASER, "TOOL_TYPE_ERASER");
1483     }
1484 
1485     // Private value for history pos that obtains the current sample.
1486     @UnsupportedAppUsage
1487     private static final int HISTORY_CURRENT = -0x80000000;
1488 
1489     // This is essentially the same as native AMOTION_EVENT_INVALID_CURSOR_POSITION as they're all
1490     // NaN and we use isnan() everywhere to check validity.
1491     private static final float INVALID_CURSOR_POSITION = Float.NaN;
1492 
1493     private static final int MAX_RECYCLED = 10;
1494     private static final Object gRecyclerLock = new Object();
1495     private static int gRecyclerUsed;
1496     private static MotionEvent gRecyclerTop;
1497 
1498     // Shared temporary objects used when translating coordinates supplied by
1499     // the caller into single element PointerCoords and pointer id arrays.
1500     private static final Object gSharedTempLock = new Object();
1501     private static PointerCoords[] gSharedTempPointerCoords;
1502     private static PointerProperties[] gSharedTempPointerProperties;
1503     private static int[] gSharedTempPointerIndexMap;
1504 
ensureSharedTempPointerCapacity(int desiredCapacity)1505     private static final void ensureSharedTempPointerCapacity(int desiredCapacity) {
1506         if (gSharedTempPointerCoords == null
1507                 || gSharedTempPointerCoords.length < desiredCapacity) {
1508             int capacity = gSharedTempPointerCoords != null ? gSharedTempPointerCoords.length : 8;
1509             while (capacity < desiredCapacity) {
1510                 capacity *= 2;
1511             }
1512             gSharedTempPointerCoords = PointerCoords.createArray(capacity);
1513             gSharedTempPointerProperties = PointerProperties.createArray(capacity);
1514             gSharedTempPointerIndexMap = new int[capacity];
1515         }
1516     }
1517 
1518     // Pointer to the native MotionEvent object that contains the actual data.
1519     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
1520     private long mNativePtr;
1521 
1522     private MotionEvent mNext;
1523 
nativeInitialize(long nativePtr, int deviceId, int source, int displayId, int action, int flags, int edgeFlags, int metaState, int buttonState, @Classification int classification, float xOffset, float yOffset, float xPrecision, float yPrecision, long downTimeNanos, long eventTimeNanos, int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords)1524     private static native long nativeInitialize(long nativePtr,
1525             int deviceId, int source, int displayId, int action, int flags, int edgeFlags,
1526             int metaState, int buttonState, @Classification int classification,
1527             float xOffset, float yOffset, float xPrecision, float yPrecision,
1528             long downTimeNanos, long eventTimeNanos,
1529             int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords);
nativeDispose(long nativePtr)1530     private static native void nativeDispose(long nativePtr);
nativeAddBatch(long nativePtr, long eventTimeNanos, PointerCoords[] pointerCoords, int metaState)1531     private static native void nativeAddBatch(long nativePtr, long eventTimeNanos,
1532             PointerCoords[] pointerCoords, int metaState);
nativeGetPointerCoords(long nativePtr, int pointerIndex, int historyPos, PointerCoords outPointerCoords)1533     private static native void nativeGetPointerCoords(long nativePtr,
1534             int pointerIndex, int historyPos, PointerCoords outPointerCoords);
nativeGetPointerProperties(long nativePtr, int pointerIndex, PointerProperties outPointerProperties)1535     private static native void nativeGetPointerProperties(long nativePtr,
1536             int pointerIndex, PointerProperties outPointerProperties);
1537 
nativeReadFromParcel(long nativePtr, Parcel parcel)1538     private static native long nativeReadFromParcel(long nativePtr, Parcel parcel);
nativeWriteToParcel(long nativePtr, Parcel parcel)1539     private static native void nativeWriteToParcel(long nativePtr, Parcel parcel);
1540 
nativeAxisToString(int axis)1541     private static native String nativeAxisToString(int axis);
nativeAxisFromString(String label)1542     private static native int nativeAxisFromString(String label);
1543 
1544     // -------------- @FastNative -------------------------
1545 
1546     @FastNative
nativeGetPointerId(long nativePtr, int pointerIndex)1547     private static native int nativeGetPointerId(long nativePtr, int pointerIndex);
1548     @FastNative
nativeGetToolType(long nativePtr, int pointerIndex)1549     private static native int nativeGetToolType(long nativePtr, int pointerIndex);
1550     @FastNative
nativeGetEventTimeNanos(long nativePtr, int historyPos)1551     private static native long nativeGetEventTimeNanos(long nativePtr, int historyPos);
1552     @FastNative
1553     @UnsupportedAppUsage
nativeGetRawAxisValue(long nativePtr, int axis, int pointerIndex, int historyPos)1554     private static native float nativeGetRawAxisValue(long nativePtr,
1555             int axis, int pointerIndex, int historyPos);
1556     @FastNative
nativeGetAxisValue(long nativePtr, int axis, int pointerIndex, int historyPos)1557     private static native float nativeGetAxisValue(long nativePtr,
1558             int axis, int pointerIndex, int historyPos);
1559     @FastNative
nativeTransform(long nativePtr, Matrix matrix)1560     private static native void nativeTransform(long nativePtr, Matrix matrix);
1561 
1562     // -------------- @CriticalNative ----------------------
1563 
1564     @CriticalNative
nativeCopy(long destNativePtr, long sourceNativePtr, boolean keepHistory)1565     private static native long nativeCopy(long destNativePtr, long sourceNativePtr,
1566             boolean keepHistory);
1567     @CriticalNative
nativeGetId(long nativePtr)1568     private static native int nativeGetId(long nativePtr);
1569     @CriticalNative
nativeGetDeviceId(long nativePtr)1570     private static native int nativeGetDeviceId(long nativePtr);
1571     @CriticalNative
nativeGetSource(long nativePtr)1572     private static native int nativeGetSource(long nativePtr);
1573     @CriticalNative
nativeSetSource(long nativePtr, int source)1574     private static native void nativeSetSource(long nativePtr, int source);
1575     @CriticalNative
nativeGetDisplayId(long nativePtr)1576     private static native int nativeGetDisplayId(long nativePtr);
1577     @CriticalNative
nativeSetDisplayId(long nativePtr, int displayId)1578     private static native void nativeSetDisplayId(long nativePtr, int displayId);
1579     @CriticalNative
nativeGetAction(long nativePtr)1580     private static native int nativeGetAction(long nativePtr);
1581     @CriticalNative
nativeSetAction(long nativePtr, int action)1582     private static native void nativeSetAction(long nativePtr, int action);
1583     @CriticalNative
nativeIsTouchEvent(long nativePtr)1584     private static native boolean nativeIsTouchEvent(long nativePtr);
1585     @CriticalNative
nativeGetFlags(long nativePtr)1586     private static native int nativeGetFlags(long nativePtr);
1587     @CriticalNative
nativeSetFlags(long nativePtr, int flags)1588     private static native void nativeSetFlags(long nativePtr, int flags);
1589     @CriticalNative
nativeGetEdgeFlags(long nativePtr)1590     private static native int nativeGetEdgeFlags(long nativePtr);
1591     @CriticalNative
nativeSetEdgeFlags(long nativePtr, int action)1592     private static native void nativeSetEdgeFlags(long nativePtr, int action);
1593     @CriticalNative
nativeGetMetaState(long nativePtr)1594     private static native int nativeGetMetaState(long nativePtr);
1595     @CriticalNative
nativeGetButtonState(long nativePtr)1596     private static native int nativeGetButtonState(long nativePtr);
1597     @CriticalNative
nativeSetButtonState(long nativePtr, int buttonState)1598     private static native void nativeSetButtonState(long nativePtr, int buttonState);
1599     @CriticalNative
nativeGetClassification(long nativePtr)1600     private static native int nativeGetClassification(long nativePtr);
1601     @CriticalNative
nativeGetActionButton(long nativePtr)1602     private static native int nativeGetActionButton(long nativePtr);
1603     @CriticalNative
nativeSetActionButton(long nativePtr, int actionButton)1604     private static native void nativeSetActionButton(long nativePtr, int actionButton);
1605     @CriticalNative
nativeOffsetLocation(long nativePtr, float deltaX, float deltaY)1606     private static native void nativeOffsetLocation(long nativePtr, float deltaX, float deltaY);
1607     @CriticalNative
nativeGetXOffset(long nativePtr)1608     private static native float nativeGetXOffset(long nativePtr);
1609     @CriticalNative
nativeGetYOffset(long nativePtr)1610     private static native float nativeGetYOffset(long nativePtr);
1611     @CriticalNative
nativeGetXPrecision(long nativePtr)1612     private static native float nativeGetXPrecision(long nativePtr);
1613     @CriticalNative
nativeGetYPrecision(long nativePtr)1614     private static native float nativeGetYPrecision(long nativePtr);
1615     @CriticalNative
nativeGetXCursorPosition(long nativePtr)1616     private static native float nativeGetXCursorPosition(long nativePtr);
1617     @CriticalNative
nativeGetYCursorPosition(long nativePtr)1618     private static native float nativeGetYCursorPosition(long nativePtr);
1619     @CriticalNative
nativeSetCursorPosition(long nativePtr, float x, float y)1620     private static native void nativeSetCursorPosition(long nativePtr, float x, float y);
1621     @CriticalNative
nativeGetDownTimeNanos(long nativePtr)1622     private static native long nativeGetDownTimeNanos(long nativePtr);
1623     @CriticalNative
nativeSetDownTimeNanos(long nativePtr, long downTime)1624     private static native void nativeSetDownTimeNanos(long nativePtr, long downTime);
1625 
1626     @CriticalNative
nativeGetPointerCount(long nativePtr)1627     private static native int nativeGetPointerCount(long nativePtr);
1628     @CriticalNative
nativeFindPointerIndex(long nativePtr, int pointerId)1629     private static native int nativeFindPointerIndex(long nativePtr, int pointerId);
1630 
1631     @CriticalNative
nativeGetHistorySize(long nativePtr)1632     private static native int nativeGetHistorySize(long nativePtr);
1633 
1634     @CriticalNative
nativeScale(long nativePtr, float scale)1635     private static native void nativeScale(long nativePtr, float scale);
1636 
MotionEvent()1637     private MotionEvent() {
1638     }
1639 
1640     @Override
finalize()1641     protected void finalize() throws Throwable {
1642         try {
1643             if (mNativePtr != 0) {
1644                 nativeDispose(mNativePtr);
1645                 mNativePtr = 0;
1646             }
1647         } finally {
1648             super.finalize();
1649         }
1650     }
1651 
1652     @UnsupportedAppUsage
obtain()1653     static private MotionEvent obtain() {
1654         final MotionEvent ev;
1655         synchronized (gRecyclerLock) {
1656             ev = gRecyclerTop;
1657             if (ev == null) {
1658                 return new MotionEvent();
1659             }
1660             gRecyclerTop = ev.mNext;
1661             gRecyclerUsed -= 1;
1662         }
1663         ev.mNext = null;
1664         ev.prepareForReuse();
1665         return ev;
1666     }
1667 
1668     /**
1669      * Create a new MotionEvent, filling in all of the basic values that
1670      * define the motion.
1671      *
1672      * @param downTime The time (in ms) when the user originally pressed down to start
1673      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1674      * @param eventTime The the time (in ms) when this specific event was generated.  This
1675      * must be obtained from {@link SystemClock#uptimeMillis()}.
1676      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1677      * @param pointerCount The number of pointers that will be in this event.
1678      * @param pointerProperties An array of <em>pointerCount</em> values providing
1679      * a {@link PointerProperties} property object for each pointer, which must
1680      * include the pointer identifier.
1681      * @param pointerCoords An array of <em>pointerCount</em> values providing
1682      * a {@link PointerCoords} coordinate object for each pointer.
1683      * @param metaState The state of any meta / modifier keys that were in effect when
1684      * the event was generated.
1685      * @param buttonState The state of buttons that are pressed.
1686      * @param xPrecision The precision of the X coordinate being reported.
1687      * @param yPrecision The precision of the Y coordinate being reported.
1688      * @param deviceId The id for the device that this event came from.  An id of
1689      * zero indicates that the event didn't come from a physical device; other
1690      * numbers are arbitrary and you shouldn't depend on the values.
1691      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1692      * MotionEvent.
1693      * @param source The source of this event.
1694      * @param displayId The display ID associated with this event.
1695      * @param flags The motion event flags.
1696      * @hide
1697      */
obtain(long downTime, long eventTime, int action, int pointerCount, PointerProperties[] pointerProperties, PointerCoords[] pointerCoords, int metaState, int buttonState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int displayId, int flags)1698     static public MotionEvent obtain(long downTime, long eventTime,
1699             int action, int pointerCount, PointerProperties[] pointerProperties,
1700             PointerCoords[] pointerCoords, int metaState, int buttonState,
1701             float xPrecision, float yPrecision, int deviceId,
1702             int edgeFlags, int source, int displayId, int flags) {
1703         MotionEvent ev = obtain();
1704         final boolean success = ev.initialize(deviceId, source, displayId, action, flags, edgeFlags,
1705                 metaState, buttonState, CLASSIFICATION_NONE, 0, 0, xPrecision, yPrecision,
1706                 downTime * NS_PER_MS, eventTime * NS_PER_MS,
1707                 pointerCount, pointerProperties, pointerCoords);
1708         if (!success) {
1709             Log.e(TAG, "Could not initialize MotionEvent");
1710             ev.recycle();
1711             return null;
1712         }
1713         return ev;
1714     }
1715 
1716     /**
1717      * Create a new MotionEvent, filling in all of the basic values that
1718      * define the motion.
1719      *
1720      * @param downTime The time (in ms) when the user originally pressed down to start
1721      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1722      * @param eventTime The the time (in ms) when this specific event was generated.  This
1723      * must be obtained from {@link SystemClock#uptimeMillis()}.
1724      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1725      * @param pointerCount The number of pointers that will be in this event.
1726      * @param pointerProperties An array of <em>pointerCount</em> values providing
1727      * a {@link PointerProperties} property object for each pointer, which must
1728      * include the pointer identifier.
1729      * @param pointerCoords An array of <em>pointerCount</em> values providing
1730      * a {@link PointerCoords} coordinate object for each pointer.
1731      * @param metaState The state of any meta / modifier keys that were in effect when
1732      * the event was generated.
1733      * @param buttonState The state of buttons that are pressed.
1734      * @param xPrecision The precision of the X coordinate being reported.
1735      * @param yPrecision The precision of the Y coordinate being reported.
1736      * @param deviceId The id for the device that this event came from.  An id of
1737      * zero indicates that the event didn't come from a physical device; other
1738      * numbers are arbitrary and you shouldn't depend on the values.
1739      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1740      * MotionEvent.
1741      * @param source The source of this event.
1742      * @param flags The motion event flags.
1743      */
obtain(long downTime, long eventTime, int action, int pointerCount, PointerProperties[] pointerProperties, PointerCoords[] pointerCoords, int metaState, int buttonState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags)1744     public static MotionEvent obtain(long downTime, long eventTime,
1745             int action, int pointerCount, PointerProperties[] pointerProperties,
1746             PointerCoords[] pointerCoords, int metaState, int buttonState,
1747             float xPrecision, float yPrecision, int deviceId,
1748             int edgeFlags, int source, int flags) {
1749         return obtain(downTime, eventTime, action, pointerCount, pointerProperties, pointerCoords,
1750                 metaState, buttonState, xPrecision, yPrecision, deviceId, edgeFlags, source,
1751                 DEFAULT_DISPLAY, flags);
1752     }
1753 
1754     /**
1755      * Create a new MotionEvent, filling in all of the basic values that
1756      * define the motion.
1757      *
1758      * @param downTime The time (in ms) when the user originally pressed down to start
1759      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1760      * @param eventTime The the time (in ms) when this specific event was generated.  This
1761      * must be obtained from {@link SystemClock#uptimeMillis()}.
1762      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1763      * @param pointerCount The number of pointers that will be in this event.
1764      * @param pointerIds An array of <em>pointerCount</em> values providing
1765      * an identifier for each pointer.
1766      * @param pointerCoords An array of <em>pointerCount</em> values providing
1767      * a {@link PointerCoords} coordinate object for each pointer.
1768      * @param metaState The state of any meta / modifier keys that were in effect when
1769      * the event was generated.
1770      * @param xPrecision The precision of the X coordinate being reported.
1771      * @param yPrecision The precision of the Y coordinate being reported.
1772      * @param deviceId The id for the device that this event came from.  An id of
1773      * zero indicates that the event didn't come from a physical device; other
1774      * numbers are arbitrary and you shouldn't depend on the values.
1775      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1776      * MotionEvent.
1777      * @param source The source of this event.
1778      * @param flags The motion event flags.
1779      *
1780      * @deprecated Use {@link #obtain(long, long, int, int, PointerProperties[], PointerCoords[], int, int, float, float, int, int, int, int)}
1781      * instead.
1782      */
1783     @Deprecated
obtain(long downTime, long eventTime, int action, int pointerCount, int[] pointerIds, PointerCoords[] pointerCoords, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags)1784     static public MotionEvent obtain(long downTime, long eventTime,
1785             int action, int pointerCount, int[] pointerIds, PointerCoords[] pointerCoords,
1786             int metaState, float xPrecision, float yPrecision, int deviceId,
1787             int edgeFlags, int source, int flags) {
1788         synchronized (gSharedTempLock) {
1789             ensureSharedTempPointerCapacity(pointerCount);
1790             final PointerProperties[] pp = gSharedTempPointerProperties;
1791             for (int i = 0; i < pointerCount; i++) {
1792                 pp[i].clear();
1793                 pp[i].id = pointerIds[i];
1794             }
1795             return obtain(downTime, eventTime, action, pointerCount, pp,
1796                     pointerCoords, metaState, 0, xPrecision, yPrecision, deviceId,
1797                     edgeFlags, source, flags);
1798         }
1799     }
1800 
1801     /**
1802      * Create a new MotionEvent, filling in all of the basic values that
1803      * define the motion.
1804      *
1805      * @param downTime The time (in ms) when the user originally pressed down to start
1806      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1807      * @param eventTime  The the time (in ms) when this specific event was generated.  This
1808      * must be obtained from {@link SystemClock#uptimeMillis()}.
1809      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1810      * @param x The X coordinate of this event.
1811      * @param y The Y coordinate of this event.
1812      * @param pressure The current pressure of this event.  The pressure generally
1813      * ranges from 0 (no pressure at all) to 1 (normal pressure), however
1814      * values higher than 1 may be generated depending on the calibration of
1815      * the input device.
1816      * @param size A scaled value of the approximate size of the area being pressed when
1817      * touched with the finger. The actual value in pixels corresponding to the finger
1818      * touch is normalized with a device specific range of values
1819      * and scaled to a value between 0 and 1.
1820      * @param metaState The state of any meta / modifier keys that were in effect when
1821      * the event was generated.
1822      * @param xPrecision The precision of the X coordinate being reported.
1823      * @param yPrecision The precision of the Y coordinate being reported.
1824      * @param deviceId The id for the device that this event came from.  An id of
1825      * zero indicates that the event didn't come from a physical device; other
1826      * numbers are arbitrary and you shouldn't depend on the values.
1827      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1828      * MotionEvent.
1829      */
obtain(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags)1830     static public MotionEvent obtain(long downTime, long eventTime, int action,
1831             float x, float y, float pressure, float size, int metaState,
1832             float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
1833         return obtain(downTime, eventTime, action, x, y, pressure, size, metaState,
1834                 xPrecision, yPrecision, deviceId, edgeFlags, InputDevice.SOURCE_UNKNOWN,
1835                 DEFAULT_DISPLAY);
1836     }
1837 
1838     /**
1839      * Create a new MotionEvent, filling in all of the basic values that
1840      * define the motion.
1841      *
1842      * @param downTime The time (in ms) when the user originally pressed down to start
1843      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1844      * @param eventTime  The the time (in ms) when this specific event was generated.  This
1845      * must be obtained from {@link SystemClock#uptimeMillis()}.
1846      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1847      * @param x The X coordinate of this event.
1848      * @param y The Y coordinate of this event.
1849      * @param pressure The current pressure of this event.  The pressure generally
1850      * ranges from 0 (no pressure at all) to 1 (normal pressure), however
1851      * values higher than 1 may be generated depending on the calibration of
1852      * the input device.
1853      * @param size A scaled value of the approximate size of the area being pressed when
1854      * touched with the finger. The actual value in pixels corresponding to the finger
1855      * touch is normalized with a device specific range of values
1856      * and scaled to a value between 0 and 1.
1857      * @param metaState The state of any meta / modifier keys that were in effect when
1858      * the event was generated.
1859      * @param xPrecision The precision of the X coordinate being reported.
1860      * @param yPrecision The precision of the Y coordinate being reported.
1861      * @param deviceId The id for the device that this event came from.  An id of
1862      * zero indicates that the event didn't come from a physical device; other
1863      * numbers are arbitrary and you shouldn't depend on the values.
1864      * @param source The source of this event.
1865      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1866      * MotionEvent.
1867      * @param displayId The display ID associated with this event.
1868      * @hide
1869      */
obtain(long downTime, long eventTime, int action, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int displayId)1870     public static MotionEvent obtain(long downTime, long eventTime, int action,
1871             float x, float y, float pressure, float size, int metaState,
1872             float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source,
1873             int displayId) {
1874         MotionEvent ev = obtain();
1875         synchronized (gSharedTempLock) {
1876             ensureSharedTempPointerCapacity(1);
1877             final PointerProperties[] pp = gSharedTempPointerProperties;
1878             pp[0].clear();
1879             pp[0].id = 0;
1880 
1881             final PointerCoords pc[] = gSharedTempPointerCoords;
1882             pc[0].clear();
1883             pc[0].x = x;
1884             pc[0].y = y;
1885             pc[0].pressure = pressure;
1886             pc[0].size = size;
1887 
1888             ev.initialize(deviceId, source, displayId,
1889                     action, 0, edgeFlags, metaState, 0 /*buttonState*/, CLASSIFICATION_NONE,
1890                     0, 0, xPrecision, yPrecision,
1891                     downTime * NS_PER_MS, eventTime * NS_PER_MS,
1892                     1, pp, pc);
1893             return ev;
1894         }
1895     }
1896 
1897     /**
1898      * Create a new MotionEvent, filling in all of the basic values that
1899      * define the motion.
1900      *
1901      * @param downTime The time (in ms) when the user originally pressed down to start
1902      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1903      * @param eventTime  The the time (in ms) when this specific event was generated.  This
1904      * must be obtained from {@link SystemClock#uptimeMillis()}.
1905      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1906      * @param pointerCount The number of pointers that are active in this event.
1907      * @param x The X coordinate of this event.
1908      * @param y The Y coordinate of this event.
1909      * @param pressure The current pressure of this event.  The pressure generally
1910      * ranges from 0 (no pressure at all) to 1 (normal pressure), however
1911      * values higher than 1 may be generated depending on the calibration of
1912      * the input device.
1913      * @param size A scaled value of the approximate size of the area being pressed when
1914      * touched with the finger. The actual value in pixels corresponding to the finger
1915      * touch is normalized with a device specific range of values
1916      * and scaled to a value between 0 and 1.
1917      * @param metaState The state of any meta / modifier keys that were in effect when
1918      * the event was generated.
1919      * @param xPrecision The precision of the X coordinate being reported.
1920      * @param yPrecision The precision of the Y coordinate being reported.
1921      * @param deviceId The id for the device that this event came from.  An id of
1922      * zero indicates that the event didn't come from a physical device; other
1923      * numbers are arbitrary and you shouldn't depend on the values.
1924      * @param edgeFlags A bitfield indicating which edges, if any, were touched by this
1925      * MotionEvent.
1926      *
1927      * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)}
1928      * instead.
1929      */
1930     @Deprecated
obtain(long downTime, long eventTime, int action, int pointerCount, float x, float y, float pressure, float size, int metaState, float xPrecision, float yPrecision, int deviceId, int edgeFlags)1931     static public MotionEvent obtain(long downTime, long eventTime, int action,
1932             int pointerCount, float x, float y, float pressure, float size, int metaState,
1933             float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
1934         return obtain(downTime, eventTime, action, x, y, pressure, size,
1935                 metaState, xPrecision, yPrecision, deviceId, edgeFlags);
1936     }
1937 
1938     /**
1939      * Create a new MotionEvent, filling in a subset of the basic motion
1940      * values.  Those not specified here are: device id (always 0), pressure
1941      * and size (always 1), x and y precision (always 1), and edgeFlags (always 0).
1942      *
1943      * @param downTime The time (in ms) when the user originally pressed down to start
1944      * a stream of position events.  This must be obtained from {@link SystemClock#uptimeMillis()}.
1945      * @param eventTime  The the time (in ms) when this specific event was generated.  This
1946      * must be obtained from {@link SystemClock#uptimeMillis()}.
1947      * @param action The kind of action being performed, such as {@link #ACTION_DOWN}.
1948      * @param x The X coordinate of this event.
1949      * @param y The Y coordinate of this event.
1950      * @param metaState The state of any meta / modifier keys that were in effect when
1951      * the event was generated.
1952      */
obtain(long downTime, long eventTime, int action, float x, float y, int metaState)1953     static public MotionEvent obtain(long downTime, long eventTime, int action,
1954             float x, float y, int metaState) {
1955         return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f,
1956                 metaState, 1.0f, 1.0f, 0, 0);
1957     }
1958 
1959     /**
1960      * Create a new MotionEvent, copying from an existing one.
1961      */
obtain(MotionEvent other)1962     static public MotionEvent obtain(MotionEvent other) {
1963         if (other == null) {
1964             throw new IllegalArgumentException("other motion event must not be null");
1965         }
1966 
1967         MotionEvent ev = obtain();
1968         ev.mNativePtr = nativeCopy(ev.mNativePtr, other.mNativePtr, true /*keepHistory*/);
1969         return ev;
1970     }
1971 
1972     /**
1973      * Create a new MotionEvent, copying from an existing one, but not including
1974      * any historical point information.
1975      */
obtainNoHistory(MotionEvent other)1976     static public MotionEvent obtainNoHistory(MotionEvent other) {
1977         if (other == null) {
1978             throw new IllegalArgumentException("other motion event must not be null");
1979         }
1980 
1981         MotionEvent ev = obtain();
1982         ev.mNativePtr = nativeCopy(ev.mNativePtr, other.mNativePtr, false /*keepHistory*/);
1983         return ev;
1984     }
1985 
initialize(int deviceId, int source, int displayId, int action, int flags, int edgeFlags, int metaState, int buttonState, @Classification int classification, float xOffset, float yOffset, float xPrecision, float yPrecision, long downTimeNanos, long eventTimeNanos, int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords)1986     private boolean initialize(int deviceId, int source, int displayId, int action, int flags,
1987             int edgeFlags, int metaState, int buttonState, @Classification int classification,
1988             float xOffset, float yOffset, float xPrecision, float yPrecision,
1989             long downTimeNanos, long eventTimeNanos,
1990             int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords) {
1991         mNativePtr = nativeInitialize(mNativePtr, deviceId, source, displayId, action, flags,
1992                 edgeFlags, metaState, buttonState, classification, xOffset, yOffset,
1993                 xPrecision, yPrecision, downTimeNanos, eventTimeNanos, pointerCount, pointerIds,
1994                 pointerCoords);
1995         if (mNativePtr == 0) {
1996             return false;
1997         }
1998         updateCursorPosition();
1999         return true;
2000     }
2001 
2002     /** @hide */
2003     @Override
2004     @UnsupportedAppUsage
copy()2005     public MotionEvent copy() {
2006         return obtain(this);
2007     }
2008 
2009     /**
2010      * Recycle the MotionEvent, to be re-used by a later caller.  After calling
2011      * this function you must not ever touch the event again.
2012      */
2013     @Override
recycle()2014     public final void recycle() {
2015         super.recycle();
2016 
2017         synchronized (gRecyclerLock) {
2018             if (gRecyclerUsed < MAX_RECYCLED) {
2019                 gRecyclerUsed++;
2020                 mNext = gRecyclerTop;
2021                 gRecyclerTop = this;
2022             }
2023         }
2024     }
2025 
2026     /**
2027      * Applies a scale factor to all points within this event.
2028      *
2029      * This method is used to adjust touch events to simulate different density
2030      * displays for compatibility mode.  The values returned by {@link #getRawX()},
2031      * {@link #getRawY()}, {@link #getXPrecision()} and {@link #getYPrecision()}
2032      * are also affected by the scale factor.
2033      *
2034      * @param scale The scale factor to apply.
2035      * @hide
2036      */
2037     @UnsupportedAppUsage
scale(float scale)2038     public final void scale(float scale) {
2039         if (scale != 1.0f) {
2040             nativeScale(mNativePtr, scale);
2041         }
2042     }
2043 
2044     /** @hide */
2045     @Override
getId()2046     public int getId() {
2047         return nativeGetId(mNativePtr);
2048     }
2049 
2050     /** {@inheritDoc} */
2051     @Override
getDeviceId()2052     public final int getDeviceId() {
2053         return nativeGetDeviceId(mNativePtr);
2054     }
2055 
2056     /** {@inheritDoc} */
2057     @Override
getSource()2058     public final int getSource() {
2059         return nativeGetSource(mNativePtr);
2060     }
2061 
2062     /** {@inheritDoc} */
2063     @Override
setSource(int source)2064     public final void setSource(int source) {
2065         if (source == getSource()) {
2066             return;
2067         }
2068         nativeSetSource(mNativePtr, source);
2069         updateCursorPosition();
2070     }
2071 
2072     /** @hide */
2073     @Override
getDisplayId()2074     public int getDisplayId() {
2075         return nativeGetDisplayId(mNativePtr);
2076     }
2077 
2078     /** @hide */
2079     @TestApi
2080     @Override
setDisplayId(int displayId)2081     public void setDisplayId(int displayId) {
2082         nativeSetDisplayId(mNativePtr, displayId);
2083     }
2084 
2085     /**
2086      * Return the kind of action being performed.
2087      * Consider using {@link #getActionMasked} and {@link #getActionIndex} to retrieve
2088      * the separate masked action and pointer index.
2089      * @return The action, such as {@link #ACTION_DOWN} or
2090      * the combination of {@link #ACTION_POINTER_DOWN} with a shifted pointer index.
2091      */
getAction()2092     public final int getAction() {
2093         return nativeGetAction(mNativePtr);
2094     }
2095 
2096     /**
2097      * Return the masked action being performed, without pointer index information.
2098      * Use {@link #getActionIndex} to return the index associated with pointer actions.
2099      * @return The action, such as {@link #ACTION_DOWN} or {@link #ACTION_POINTER_DOWN}.
2100      */
getActionMasked()2101     public final int getActionMasked() {
2102         return nativeGetAction(mNativePtr) & ACTION_MASK;
2103     }
2104 
2105     /**
2106      * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP}
2107      * as returned by {@link #getActionMasked}, this returns the associated
2108      * pointer index.
2109      * The index may be used with {@link #getPointerId(int)},
2110      * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)},
2111      * and {@link #getSize(int)} to get information about the pointer that has
2112      * gone down or up.
2113      * @return The index associated with the action.
2114      */
getActionIndex()2115     public final int getActionIndex() {
2116         return (nativeGetAction(mNativePtr) & ACTION_POINTER_INDEX_MASK)
2117                 >> ACTION_POINTER_INDEX_SHIFT;
2118     }
2119 
2120     /**
2121      * Returns true if this motion event is a touch event.
2122      * <p>
2123      * Specifically excludes pointer events with action {@link #ACTION_HOVER_MOVE},
2124      * {@link #ACTION_HOVER_ENTER}, {@link #ACTION_HOVER_EXIT}, or {@link #ACTION_SCROLL}
2125      * because they are not actually touch events (the pointer is not down).
2126      * </p>
2127      * @return True if this motion event is a touch event.
2128      * @hide
2129      */
isTouchEvent()2130     public final boolean isTouchEvent() {
2131         return nativeIsTouchEvent(mNativePtr);
2132     }
2133 
2134     /**
2135      * Gets the motion event flags.
2136      *
2137      * @see #FLAG_WINDOW_IS_OBSCURED
2138      */
getFlags()2139     public final int getFlags() {
2140         return nativeGetFlags(mNativePtr);
2141     }
2142 
2143     /** @hide */
2144     @Override
isTainted()2145     public final boolean isTainted() {
2146         final int flags = getFlags();
2147         return (flags & FLAG_TAINTED) != 0;
2148     }
2149 
2150     /** @hide */
2151     @Override
setTainted(boolean tainted)2152     public final void setTainted(boolean tainted) {
2153         final int flags = getFlags();
2154         nativeSetFlags(mNativePtr, tainted ? flags | FLAG_TAINTED : flags & ~FLAG_TAINTED);
2155     }
2156 
2157     /** @hide */
isTargetAccessibilityFocus()2158     public  boolean isTargetAccessibilityFocus() {
2159         final int flags = getFlags();
2160         return (flags & FLAG_TARGET_ACCESSIBILITY_FOCUS) != 0;
2161     }
2162 
2163     /** @hide */
setTargetAccessibilityFocus(boolean targetsFocus)2164     public void setTargetAccessibilityFocus(boolean targetsFocus) {
2165         final int flags = getFlags();
2166         nativeSetFlags(mNativePtr, targetsFocus
2167                 ? flags | FLAG_TARGET_ACCESSIBILITY_FOCUS
2168                 : flags & ~FLAG_TARGET_ACCESSIBILITY_FOCUS);
2169     }
2170 
2171     /** @hide */
isHoverExitPending()2172     public final boolean isHoverExitPending() {
2173         final int flags = getFlags();
2174         return (flags & FLAG_HOVER_EXIT_PENDING) != 0;
2175     }
2176 
2177     /** @hide */
setHoverExitPending(boolean hoverExitPending)2178     public void setHoverExitPending(boolean hoverExitPending) {
2179         final int flags = getFlags();
2180         nativeSetFlags(mNativePtr, hoverExitPending
2181                 ? flags | FLAG_HOVER_EXIT_PENDING
2182                 : flags & ~FLAG_HOVER_EXIT_PENDING);
2183     }
2184 
2185     /**
2186      * Returns the time (in ms) when the user originally pressed down to start
2187      * a stream of position events.
2188      */
getDownTime()2189     public final long getDownTime() {
2190         return nativeGetDownTimeNanos(mNativePtr) / NS_PER_MS;
2191     }
2192 
2193     /**
2194      * Sets the time (in ms) when the user originally pressed down to start
2195      * a stream of position events.
2196      *
2197      * @hide
2198      */
2199     @UnsupportedAppUsage
setDownTime(long downTime)2200     public final void setDownTime(long downTime) {
2201         nativeSetDownTimeNanos(mNativePtr, downTime * NS_PER_MS);
2202     }
2203 
2204     /**
2205      * Retrieve the time this event occurred,
2206      * in the {@link android.os.SystemClock#uptimeMillis} time base.
2207      *
2208      * @return Returns the time this event occurred,
2209      * in the {@link android.os.SystemClock#uptimeMillis} time base.
2210      */
2211     @Override
getEventTime()2212     public final long getEventTime() {
2213         return nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT) / NS_PER_MS;
2214     }
2215 
2216     /**
2217      * Retrieve the time this event occurred,
2218      * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2219      * nanosecond precision.
2220      * <p>
2221      * The value is in nanosecond precision but it may not have nanosecond accuracy.
2222      * </p>
2223      *
2224      * @return Returns the time this event occurred,
2225      * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2226      * nanosecond precision.
2227      *
2228      * @hide
2229      */
2230     @Override
2231     @UnsupportedAppUsage
getEventTimeNano()2232     public final long getEventTimeNano() {
2233         return nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT);
2234     }
2235 
2236     /**
2237      * {@link #getX(int)} for the first pointer index (may be an
2238      * arbitrary pointer identifier).
2239      *
2240      * @see #AXIS_X
2241      */
getX()2242     public final float getX() {
2243         return nativeGetAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
2244     }
2245 
2246     /**
2247      * {@link #getY(int)} for the first pointer index (may be an
2248      * arbitrary pointer identifier).
2249      *
2250      * @see #AXIS_Y
2251      */
getY()2252     public final float getY() {
2253         return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);
2254     }
2255 
2256     /**
2257      * {@link #getPressure(int)} for the first pointer index (may be an
2258      * arbitrary pointer identifier).
2259      *
2260      * @see #AXIS_PRESSURE
2261      */
getPressure()2262     public final float getPressure() {
2263         return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, 0, HISTORY_CURRENT);
2264     }
2265 
2266     /**
2267      * {@link #getSize(int)} for the first pointer index (may be an
2268      * arbitrary pointer identifier).
2269      *
2270      * @see #AXIS_SIZE
2271      */
getSize()2272     public final float getSize() {
2273         return nativeGetAxisValue(mNativePtr, AXIS_SIZE, 0, HISTORY_CURRENT);
2274     }
2275 
2276     /**
2277      * {@link #getTouchMajor(int)} for the first pointer index (may be an
2278      * arbitrary pointer identifier).
2279      *
2280      * @see #AXIS_TOUCH_MAJOR
2281      */
getTouchMajor()2282     public final float getTouchMajor() {
2283         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, 0, HISTORY_CURRENT);
2284     }
2285 
2286     /**
2287      * {@link #getTouchMinor(int)} for the first pointer index (may be an
2288      * arbitrary pointer identifier).
2289      *
2290      * @see #AXIS_TOUCH_MINOR
2291      */
getTouchMinor()2292     public final float getTouchMinor() {
2293         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, 0, HISTORY_CURRENT);
2294     }
2295 
2296     /**
2297      * {@link #getToolMajor(int)} for the first pointer index (may be an
2298      * arbitrary pointer identifier).
2299      *
2300      * @see #AXIS_TOOL_MAJOR
2301      */
getToolMajor()2302     public final float getToolMajor() {
2303         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, 0, HISTORY_CURRENT);
2304     }
2305 
2306     /**
2307      * {@link #getToolMinor(int)} for the first pointer index (may be an
2308      * arbitrary pointer identifier).
2309      *
2310      * @see #AXIS_TOOL_MINOR
2311      */
getToolMinor()2312     public final float getToolMinor() {
2313         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, 0, HISTORY_CURRENT);
2314     }
2315 
2316     /**
2317      * {@link #getOrientation(int)} for the first pointer index (may be an
2318      * arbitrary pointer identifier).
2319      *
2320      * @see #AXIS_ORIENTATION
2321      */
getOrientation()2322     public final float getOrientation() {
2323         return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, 0, HISTORY_CURRENT);
2324     }
2325 
2326     /**
2327      * {@link #getAxisValue(int)} for the first pointer index (may be an
2328      * arbitrary pointer identifier).
2329      *
2330      * @param axis The axis identifier for the axis value to retrieve.
2331      *
2332      * @see #AXIS_X
2333      * @see #AXIS_Y
2334      */
getAxisValue(int axis)2335     public final float getAxisValue(int axis) {
2336         return nativeGetAxisValue(mNativePtr, axis, 0, HISTORY_CURRENT);
2337     }
2338 
2339     /**
2340      * The number of pointers of data contained in this event.  Always
2341      * >= 1.
2342      */
getPointerCount()2343     public final int getPointerCount() {
2344         return nativeGetPointerCount(mNativePtr);
2345     }
2346 
2347     /**
2348      * Return the pointer identifier associated with a particular pointer
2349      * data index in this event.  The identifier tells you the actual pointer
2350      * number associated with the data, accounting for individual pointers
2351      * going up and down since the start of the current gesture.
2352      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2353      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2354      */
getPointerId(int pointerIndex)2355     public final int getPointerId(int pointerIndex) {
2356         return nativeGetPointerId(mNativePtr, pointerIndex);
2357     }
2358 
2359     /**
2360      * Gets the tool type of a pointer for the given pointer index.
2361      * The tool type indicates the type of tool used to make contact such
2362      * as a finger or stylus, if known.
2363      *
2364      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2365      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2366      * @return The tool type of the pointer.
2367      *
2368      * @see #TOOL_TYPE_UNKNOWN
2369      * @see #TOOL_TYPE_FINGER
2370      * @see #TOOL_TYPE_STYLUS
2371      * @see #TOOL_TYPE_MOUSE
2372      */
getToolType(int pointerIndex)2373     public final int getToolType(int pointerIndex) {
2374         return nativeGetToolType(mNativePtr, pointerIndex);
2375     }
2376 
2377     /**
2378      * Given a pointer identifier, find the index of its data in the event.
2379      *
2380      * @param pointerId The identifier of the pointer to be found.
2381      * @return Returns either the index of the pointer (for use with
2382      * {@link #getX(int)} et al.), or -1 if there is no data available for
2383      * that pointer identifier.
2384      */
findPointerIndex(int pointerId)2385     public final int findPointerIndex(int pointerId) {
2386         return nativeFindPointerIndex(mNativePtr, pointerId);
2387     }
2388 
2389     /**
2390      * Returns the X coordinate of this event for the given pointer
2391      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2392      * identifier for this index).
2393      * Whole numbers are pixels; the
2394      * value may have a fraction for input devices that are sub-pixel precise.
2395      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2396      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2397      *
2398      * @see #AXIS_X
2399      */
getX(int pointerIndex)2400     public final float getX(int pointerIndex) {
2401         return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT);
2402     }
2403 
2404     /**
2405      * Returns the Y coordinate of this event for the given pointer
2406      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2407      * identifier for this index).
2408      * Whole numbers are pixels; the
2409      * value may have a fraction for input devices that are sub-pixel precise.
2410      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2411      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2412      *
2413      * @see #AXIS_Y
2414      */
getY(int pointerIndex)2415     public final float getY(int pointerIndex) {
2416         return nativeGetAxisValue(mNativePtr, AXIS_Y, pointerIndex, HISTORY_CURRENT);
2417     }
2418 
2419     /**
2420      * Returns the current pressure of this event for the given pointer
2421      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2422      * identifier for this index).
2423      * The pressure generally
2424      * ranges from 0 (no pressure at all) to 1 (normal pressure), however
2425      * values higher than 1 may be generated depending on the calibration of
2426      * the input device.
2427      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2428      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2429      *
2430      * @see #AXIS_PRESSURE
2431      */
getPressure(int pointerIndex)2432     public final float getPressure(int pointerIndex) {
2433         return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, pointerIndex, HISTORY_CURRENT);
2434     }
2435 
2436     /**
2437      * Returns a scaled value of the approximate size for the given pointer
2438      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2439      * identifier for this index).
2440      * This represents some approximation of the area of the screen being
2441      * pressed; the actual value in pixels corresponding to the
2442      * touch is normalized with the device specific range of values
2443      * and scaled to a value between 0 and 1. The value of size can be used to
2444      * determine fat touch events.
2445      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2446      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2447      *
2448      * @see #AXIS_SIZE
2449      */
getSize(int pointerIndex)2450     public final float getSize(int pointerIndex) {
2451         return nativeGetAxisValue(mNativePtr, AXIS_SIZE, pointerIndex, HISTORY_CURRENT);
2452     }
2453 
2454     /**
2455      * Returns the length of the major axis of an ellipse that describes the touch
2456      * area at the point of contact for the given pointer
2457      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2458      * identifier for this index).
2459      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2460      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2461      *
2462      * @see #AXIS_TOUCH_MAJOR
2463      */
getTouchMajor(int pointerIndex)2464     public final float getTouchMajor(int pointerIndex) {
2465         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, pointerIndex, HISTORY_CURRENT);
2466     }
2467 
2468     /**
2469      * Returns the length of the minor axis of an ellipse that describes the touch
2470      * area at the point of contact for the given pointer
2471      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2472      * identifier for this index).
2473      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2474      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2475      *
2476      * @see #AXIS_TOUCH_MINOR
2477      */
getTouchMinor(int pointerIndex)2478     public final float getTouchMinor(int pointerIndex) {
2479         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, pointerIndex, HISTORY_CURRENT);
2480     }
2481 
2482     /**
2483      * Returns the length of the major axis of an ellipse that describes the size of
2484      * the approaching tool for the given pointer
2485      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2486      * identifier for this index).
2487      * The tool area represents the estimated size of the finger or pen that is
2488      * touching the device independent of its actual touch area at the point of contact.
2489      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2490      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2491      *
2492      * @see #AXIS_TOOL_MAJOR
2493      */
getToolMajor(int pointerIndex)2494     public final float getToolMajor(int pointerIndex) {
2495         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, pointerIndex, HISTORY_CURRENT);
2496     }
2497 
2498     /**
2499      * Returns the length of the minor axis of an ellipse that describes the size of
2500      * the approaching tool for the given pointer
2501      * <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2502      * identifier for this index).
2503      * The tool area represents the estimated size of the finger or pen that is
2504      * touching the device independent of its actual touch area at the point of contact.
2505      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2506      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2507      *
2508      * @see #AXIS_TOOL_MINOR
2509      */
getToolMinor(int pointerIndex)2510     public final float getToolMinor(int pointerIndex) {
2511         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, pointerIndex, HISTORY_CURRENT);
2512     }
2513 
2514     /**
2515      * Returns the orientation of the touch area and tool area in radians clockwise from vertical
2516      * for the given pointer <em>index</em> (use {@link #getPointerId(int)} to find the pointer
2517      * identifier for this index).
2518      * An angle of 0 radians indicates that the major axis of contact is oriented
2519      * upwards, is perfectly circular or is of unknown orientation.  A positive angle
2520      * indicates that the major axis of contact is oriented to the right.  A negative angle
2521      * indicates that the major axis of contact is oriented to the left.
2522      * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
2523      * (finger pointing fully right).
2524      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2525      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2526      *
2527      * @see #AXIS_ORIENTATION
2528      */
getOrientation(int pointerIndex)2529     public final float getOrientation(int pointerIndex) {
2530         return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, pointerIndex, HISTORY_CURRENT);
2531     }
2532 
2533     /**
2534      * Returns the value of the requested axis for the given pointer <em>index</em>
2535      * (use {@link #getPointerId(int)} to find the pointer identifier for this index).
2536      *
2537      * @param axis The axis identifier for the axis value to retrieve.
2538      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2539      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2540      * @return The value of the axis, or 0 if the axis is not available.
2541      *
2542      * @see #AXIS_X
2543      * @see #AXIS_Y
2544      */
getAxisValue(int axis, int pointerIndex)2545     public final float getAxisValue(int axis, int pointerIndex) {
2546         return nativeGetAxisValue(mNativePtr, axis, pointerIndex, HISTORY_CURRENT);
2547     }
2548 
2549     /**
2550      * Populates a {@link PointerCoords} object with pointer coordinate data for
2551      * the specified pointer index.
2552      *
2553      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2554      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2555      * @param outPointerCoords The pointer coordinate object to populate.
2556      *
2557      * @see PointerCoords
2558      */
getPointerCoords(int pointerIndex, PointerCoords outPointerCoords)2559     public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) {
2560         nativeGetPointerCoords(mNativePtr, pointerIndex, HISTORY_CURRENT, outPointerCoords);
2561     }
2562 
2563     /**
2564      * Populates a {@link PointerProperties} object with pointer properties for
2565      * the specified pointer index.
2566      *
2567      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2568      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2569      * @param outPointerProperties The pointer properties object to populate.
2570      *
2571      * @see PointerProperties
2572      */
getPointerProperties(int pointerIndex, PointerProperties outPointerProperties)2573     public final void getPointerProperties(int pointerIndex,
2574             PointerProperties outPointerProperties) {
2575         nativeGetPointerProperties(mNativePtr, pointerIndex, outPointerProperties);
2576     }
2577 
2578     /**
2579      * Returns the state of any meta / modifier keys that were in effect when
2580      * the event was generated.  This is the same values as those
2581      * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}.
2582      *
2583      * @return an integer in which each bit set to 1 represents a pressed
2584      *         meta key
2585      *
2586      * @see KeyEvent#getMetaState()
2587      */
getMetaState()2588     public final int getMetaState() {
2589         return nativeGetMetaState(mNativePtr);
2590     }
2591 
2592     /**
2593      * Gets the state of all buttons that are pressed such as a mouse or stylus button.
2594      *
2595      * @return The button state.
2596      *
2597      * @see #BUTTON_PRIMARY
2598      * @see #BUTTON_SECONDARY
2599      * @see #BUTTON_TERTIARY
2600      * @see #BUTTON_FORWARD
2601      * @see #BUTTON_BACK
2602      * @see #BUTTON_STYLUS_PRIMARY
2603      * @see #BUTTON_STYLUS_SECONDARY
2604      */
getButtonState()2605     public final int getButtonState() {
2606         return nativeGetButtonState(mNativePtr);
2607     }
2608 
2609     /**
2610      * Sets the bitfield indicating which buttons are pressed.
2611      *
2612      * @see #getButtonState()
2613      * @hide
2614      */
2615     @TestApi
setButtonState(int buttonState)2616     public final void setButtonState(int buttonState) {
2617         nativeSetButtonState(mNativePtr, buttonState);
2618     }
2619 
2620     /**
2621      * Returns the classification for the current gesture.
2622      * The classification may change as more events become available for the same gesture.
2623      *
2624      * @see #CLASSIFICATION_NONE
2625      * @see #CLASSIFICATION_AMBIGUOUS_GESTURE
2626      * @see #CLASSIFICATION_DEEP_PRESS
2627      */
getClassification()2628     public @Classification int getClassification() {
2629         return nativeGetClassification(mNativePtr);
2630     }
2631 
2632     /**
2633      * Gets which button has been modified during a press or release action.
2634      *
2635      * For actions other than {@link #ACTION_BUTTON_PRESS} and {@link #ACTION_BUTTON_RELEASE}
2636      * the returned value is undefined.
2637      *
2638      * @see #getButtonState()
2639      */
getActionButton()2640     public final int getActionButton() {
2641         return nativeGetActionButton(mNativePtr);
2642     }
2643 
2644     /**
2645      * Sets the action button for the event.
2646      *
2647      * @see #getActionButton()
2648      * @hide
2649      */
2650     @UnsupportedAppUsage
2651     @TestApi
setActionButton(int button)2652     public final void setActionButton(int button) {
2653         nativeSetActionButton(mNativePtr, button);
2654     }
2655 
2656     /**
2657      * Returns the original raw X coordinate of this event.  For touch
2658      * events on the screen, this is the original location of the event
2659      * on the screen, before it had been adjusted for the containing window
2660      * and views.
2661      *
2662      * @see #getX(int)
2663      * @see #AXIS_X
2664      */
getRawX()2665     public final float getRawX() {
2666         return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT);
2667     }
2668 
2669     /**
2670      * Returns the original raw Y coordinate of this event.  For touch
2671      * events on the screen, this is the original location of the event
2672      * on the screen, before it had been adjusted for the containing window
2673      * and views.
2674      *
2675      * @see #getY(int)
2676      * @see #AXIS_Y
2677      */
getRawY()2678     public final float getRawY() {
2679         return nativeGetRawAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT);
2680     }
2681 
2682     /**
2683      * Returns the original raw X coordinate of this event.  For touch
2684      * events on the screen, this is the original location of the event
2685      * on the screen, before it had been adjusted for the containing window
2686      * and views.
2687      *
2688      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2689      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2690      *
2691      * @see #getX(int)
2692      * @see #AXIS_X
2693      */
getRawX(int pointerIndex)2694     public float getRawX(int pointerIndex) {
2695         return nativeGetRawAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT);
2696     }
2697 
2698     /**
2699      * Returns the original raw Y coordinate of this event.  For touch
2700      * events on the screen, this is the original location of the event
2701      * on the screen, before it had been adjusted for the containing window
2702      * and views.
2703      *
2704      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2705      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2706      *
2707      * @see #getY(int)
2708      * @see #AXIS_Y
2709      */
getRawY(int pointerIndex)2710     public float getRawY(int pointerIndex) {
2711         return nativeGetRawAxisValue(mNativePtr, AXIS_Y, pointerIndex, HISTORY_CURRENT);
2712     }
2713 
2714     /**
2715      * Return the precision of the X coordinates being reported.  You can
2716      * multiply this number with {@link #getX} to find the actual hardware
2717      * value of the X coordinate.
2718      * @return Returns the precision of X coordinates being reported.
2719      *
2720      * @see #AXIS_X
2721      */
getXPrecision()2722     public final float getXPrecision() {
2723         return nativeGetXPrecision(mNativePtr);
2724     }
2725 
2726     /**
2727      * Return the precision of the Y coordinates being reported.  You can
2728      * multiply this number with {@link #getY} to find the actual hardware
2729      * value of the Y coordinate.
2730      * @return Returns the precision of Y coordinates being reported.
2731      *
2732      * @see #AXIS_Y
2733      */
getYPrecision()2734     public final float getYPrecision() {
2735         return nativeGetYPrecision(mNativePtr);
2736     }
2737 
2738     /**
2739      * Returns the x coordinate of mouse cursor position when this event is
2740      * reported. This value is only valid if {@link #getSource()} returns
2741      * {@link InputDevice#SOURCE_MOUSE}.
2742      *
2743      * @hide
2744      */
getXCursorPosition()2745     public float getXCursorPosition() {
2746         return nativeGetXCursorPosition(mNativePtr);
2747     }
2748 
2749     /**
2750      * Returns the y coordinate of mouse cursor position when this event is
2751      * reported. This value is only valid if {@link #getSource()} returns
2752      * {@link InputDevice#SOURCE_MOUSE}.
2753      *
2754      * @hide
2755      */
getYCursorPosition()2756     public float getYCursorPosition() {
2757         return nativeGetYCursorPosition(mNativePtr);
2758     }
2759 
2760     /**
2761      * Sets cursor position to given coordinates. The coordinate in parameters should be after
2762      * offsetting. In other words, the effect of this function is {@link #getXCursorPosition()} and
2763      * {@link #getYCursorPosition()} will return the same value passed in the parameters.
2764      *
2765      * @hide
2766      */
setCursorPosition(float x, float y)2767     private void setCursorPosition(float x, float y) {
2768         nativeSetCursorPosition(mNativePtr, x, y);
2769     }
2770 
2771     /**
2772      * Returns the number of historical points in this event.  These are
2773      * movements that have occurred between this event and the previous event.
2774      * This only applies to ACTION_MOVE events -- all other actions will have
2775      * a size of 0.
2776      *
2777      * @return Returns the number of historical points in the event.
2778      */
getHistorySize()2779     public final int getHistorySize() {
2780         return nativeGetHistorySize(mNativePtr);
2781     }
2782 
2783     /**
2784      * Returns the time that a historical movement occurred between this event
2785      * and the previous event, in the {@link android.os.SystemClock#uptimeMillis} time base.
2786      * <p>
2787      * This only applies to ACTION_MOVE events.
2788      * </p>
2789      *
2790      * @param pos Which historical value to return; must be less than
2791      * {@link #getHistorySize}
2792      * @return Returns the time that a historical movement occurred between this
2793      * event and the previous event,
2794      * in the {@link android.os.SystemClock#uptimeMillis} time base.
2795      *
2796      * @see #getHistorySize
2797      * @see #getEventTime
2798      */
getHistoricalEventTime(int pos)2799     public final long getHistoricalEventTime(int pos) {
2800         return nativeGetEventTimeNanos(mNativePtr, pos) / NS_PER_MS;
2801     }
2802 
2803     /**
2804      * Returns the time that a historical movement occurred between this event
2805      * and the previous event, in the {@link android.os.SystemClock#uptimeMillis} time base
2806      * but with nanosecond (instead of millisecond) precision.
2807      * <p>
2808      * This only applies to ACTION_MOVE events.
2809      * </p><p>
2810      * The value is in nanosecond precision but it may not have nanosecond accuracy.
2811      * </p>
2812      *
2813      * @param pos Which historical value to return; must be less than
2814      * {@link #getHistorySize}
2815      * @return Returns the time that a historical movement occurred between this
2816      * event and the previous event,
2817      * in the {@link android.os.SystemClock#uptimeMillis} time base but with
2818      * nanosecond (instead of millisecond) precision.
2819      *
2820      * @see #getHistorySize
2821      * @see #getEventTime
2822      *
2823      * @hide
2824      */
getHistoricalEventTimeNano(int pos)2825     public final long getHistoricalEventTimeNano(int pos) {
2826         return nativeGetEventTimeNanos(mNativePtr, pos);
2827     }
2828 
2829     /**
2830      * {@link #getHistoricalX(int, int)} for the first pointer index (may be an
2831      * arbitrary pointer identifier).
2832      *
2833      * @param pos Which historical value to return; must be less than
2834      * {@link #getHistorySize}
2835      *
2836      * @see #getHistorySize
2837      * @see #getX()
2838      * @see #AXIS_X
2839      */
getHistoricalX(int pos)2840     public final float getHistoricalX(int pos) {
2841         return nativeGetAxisValue(mNativePtr, AXIS_X, 0, pos);
2842     }
2843 
2844     /**
2845      * {@link #getHistoricalY(int, int)} for the first pointer index (may be an
2846      * arbitrary pointer identifier).
2847      *
2848      * @param pos Which historical value to return; must be less than
2849      * {@link #getHistorySize}
2850      *
2851      * @see #getHistorySize
2852      * @see #getY()
2853      * @see #AXIS_Y
2854      */
getHistoricalY(int pos)2855     public final float getHistoricalY(int pos) {
2856         return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, pos);
2857     }
2858 
2859     /**
2860      * {@link #getHistoricalPressure(int, int)} for the first pointer index (may be an
2861      * arbitrary pointer identifier).
2862      *
2863      * @param pos Which historical value to return; must be less than
2864      * {@link #getHistorySize}
2865      *
2866      * @see #getHistorySize
2867      * @see #getPressure()
2868      * @see #AXIS_PRESSURE
2869      */
getHistoricalPressure(int pos)2870     public final float getHistoricalPressure(int pos) {
2871         return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, 0, pos);
2872     }
2873 
2874     /**
2875      * {@link #getHistoricalSize(int, int)} for the first pointer index (may be an
2876      * arbitrary pointer identifier).
2877      *
2878      * @param pos Which historical value to return; must be less than
2879      * {@link #getHistorySize}
2880      *
2881      * @see #getHistorySize
2882      * @see #getSize()
2883      * @see #AXIS_SIZE
2884      */
getHistoricalSize(int pos)2885     public final float getHistoricalSize(int pos) {
2886         return nativeGetAxisValue(mNativePtr, AXIS_SIZE, 0, pos);
2887     }
2888 
2889     /**
2890      * {@link #getHistoricalTouchMajor(int, int)} for the first pointer index (may be an
2891      * arbitrary pointer identifier).
2892      *
2893      * @param pos Which historical value to return; must be less than
2894      * {@link #getHistorySize}
2895      *
2896      * @see #getHistorySize
2897      * @see #getTouchMajor()
2898      * @see #AXIS_TOUCH_MAJOR
2899      */
getHistoricalTouchMajor(int pos)2900     public final float getHistoricalTouchMajor(int pos) {
2901         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, 0, pos);
2902     }
2903 
2904     /**
2905      * {@link #getHistoricalTouchMinor(int, int)} for the first pointer index (may be an
2906      * arbitrary pointer identifier).
2907      *
2908      * @param pos Which historical value to return; must be less than
2909      * {@link #getHistorySize}
2910      *
2911      * @see #getHistorySize
2912      * @see #getTouchMinor()
2913      * @see #AXIS_TOUCH_MINOR
2914      */
getHistoricalTouchMinor(int pos)2915     public final float getHistoricalTouchMinor(int pos) {
2916         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, 0, pos);
2917     }
2918 
2919     /**
2920      * {@link #getHistoricalToolMajor(int, int)} for the first pointer index (may be an
2921      * arbitrary pointer identifier).
2922      *
2923      * @param pos Which historical value to return; must be less than
2924      * {@link #getHistorySize}
2925      *
2926      * @see #getHistorySize
2927      * @see #getToolMajor()
2928      * @see #AXIS_TOOL_MAJOR
2929      */
getHistoricalToolMajor(int pos)2930     public final float getHistoricalToolMajor(int pos) {
2931         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, 0, pos);
2932     }
2933 
2934     /**
2935      * {@link #getHistoricalToolMinor(int, int)} for the first pointer index (may be an
2936      * arbitrary pointer identifier).
2937      *
2938      * @param pos Which historical value to return; must be less than
2939      * {@link #getHistorySize}
2940      *
2941      * @see #getHistorySize
2942      * @see #getToolMinor()
2943      * @see #AXIS_TOOL_MINOR
2944      */
getHistoricalToolMinor(int pos)2945     public final float getHistoricalToolMinor(int pos) {
2946         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, 0, pos);
2947     }
2948 
2949     /**
2950      * {@link #getHistoricalOrientation(int, int)} for the first pointer index (may be an
2951      * arbitrary pointer identifier).
2952      *
2953      * @param pos Which historical value to return; must be less than
2954      * {@link #getHistorySize}
2955      *
2956      * @see #getHistorySize
2957      * @see #getOrientation()
2958      * @see #AXIS_ORIENTATION
2959      */
getHistoricalOrientation(int pos)2960     public final float getHistoricalOrientation(int pos) {
2961         return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, 0, pos);
2962     }
2963 
2964     /**
2965      * {@link #getHistoricalAxisValue(int, int, int)} for the first pointer index (may be an
2966      * arbitrary pointer identifier).
2967      *
2968      * @param axis The axis identifier for the axis value to retrieve.
2969      * @param pos Which historical value to return; must be less than
2970      * {@link #getHistorySize}
2971      *
2972      * @see #getHistorySize
2973      * @see #getAxisValue(int)
2974      * @see #AXIS_X
2975      * @see #AXIS_Y
2976      */
getHistoricalAxisValue(int axis, int pos)2977     public final float getHistoricalAxisValue(int axis, int pos) {
2978         return nativeGetAxisValue(mNativePtr, axis, 0, pos);
2979     }
2980 
2981     /**
2982      * Returns a historical X coordinate, as per {@link #getX(int)}, that
2983      * occurred between this event and the previous event for the given pointer.
2984      * Only applies to ACTION_MOVE events.
2985      *
2986      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
2987      * (the first pointer that is down) to {@link #getPointerCount()}-1.
2988      * @param pos Which historical value to return; must be less than
2989      * {@link #getHistorySize}
2990      *
2991      * @see #getHistorySize
2992      * @see #getX(int)
2993      * @see #AXIS_X
2994      */
getHistoricalX(int pointerIndex, int pos)2995     public final float getHistoricalX(int pointerIndex, int pos) {
2996         return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, pos);
2997     }
2998 
2999     /**
3000      * Returns a historical Y coordinate, as per {@link #getY(int)}, that
3001      * occurred between this event and the previous event for the given pointer.
3002      * Only applies to ACTION_MOVE events.
3003      *
3004      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3005      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3006      * @param pos Which historical value to return; must be less than
3007      * {@link #getHistorySize}
3008      *
3009      * @see #getHistorySize
3010      * @see #getY(int)
3011      * @see #AXIS_Y
3012      */
getHistoricalY(int pointerIndex, int pos)3013     public final float getHistoricalY(int pointerIndex, int pos) {
3014         return nativeGetAxisValue(mNativePtr, AXIS_Y, pointerIndex, pos);
3015     }
3016 
3017     /**
3018      * Returns a historical pressure coordinate, as per {@link #getPressure(int)},
3019      * that occurred between this event and the previous event for the given
3020      * pointer.  Only applies to ACTION_MOVE events.
3021      *
3022      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3023      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3024      * @param pos Which historical value to return; must be less than
3025      * {@link #getHistorySize}
3026      *
3027      * @see #getHistorySize
3028      * @see #getPressure(int)
3029      * @see #AXIS_PRESSURE
3030      */
getHistoricalPressure(int pointerIndex, int pos)3031     public final float getHistoricalPressure(int pointerIndex, int pos) {
3032         return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, pointerIndex, pos);
3033     }
3034 
3035     /**
3036      * Returns a historical size coordinate, as per {@link #getSize(int)}, that
3037      * occurred between this event and the previous event for the given pointer.
3038      * Only applies to ACTION_MOVE events.
3039      *
3040      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3041      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3042      * @param pos Which historical value to return; must be less than
3043      * {@link #getHistorySize}
3044      *
3045      * @see #getHistorySize
3046      * @see #getSize(int)
3047      * @see #AXIS_SIZE
3048      */
getHistoricalSize(int pointerIndex, int pos)3049     public final float getHistoricalSize(int pointerIndex, int pos) {
3050         return nativeGetAxisValue(mNativePtr, AXIS_SIZE, pointerIndex, pos);
3051     }
3052 
3053     /**
3054      * Returns a historical touch major axis coordinate, as per {@link #getTouchMajor(int)}, that
3055      * occurred between this event and the previous event for the given pointer.
3056      * Only applies to ACTION_MOVE events.
3057      *
3058      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3059      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3060      * @param pos Which historical value to return; must be less than
3061      * {@link #getHistorySize}
3062      *
3063      * @see #getHistorySize
3064      * @see #getTouchMajor(int)
3065      * @see #AXIS_TOUCH_MAJOR
3066      */
getHistoricalTouchMajor(int pointerIndex, int pos)3067     public final float getHistoricalTouchMajor(int pointerIndex, int pos) {
3068         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, pointerIndex, pos);
3069     }
3070 
3071     /**
3072      * Returns a historical touch minor axis coordinate, as per {@link #getTouchMinor(int)}, that
3073      * occurred between this event and the previous event for the given pointer.
3074      * Only applies to ACTION_MOVE events.
3075      *
3076      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3077      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3078      * @param pos Which historical value to return; must be less than
3079      * {@link #getHistorySize}
3080      *
3081      * @see #getHistorySize
3082      * @see #getTouchMinor(int)
3083      * @see #AXIS_TOUCH_MINOR
3084      */
getHistoricalTouchMinor(int pointerIndex, int pos)3085     public final float getHistoricalTouchMinor(int pointerIndex, int pos) {
3086         return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, pointerIndex, pos);
3087     }
3088 
3089     /**
3090      * Returns a historical tool major axis coordinate, as per {@link #getToolMajor(int)}, that
3091      * occurred between this event and the previous event for the given pointer.
3092      * Only applies to ACTION_MOVE events.
3093      *
3094      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3095      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3096      * @param pos Which historical value to return; must be less than
3097      * {@link #getHistorySize}
3098      *
3099      * @see #getHistorySize
3100      * @see #getToolMajor(int)
3101      * @see #AXIS_TOOL_MAJOR
3102      */
getHistoricalToolMajor(int pointerIndex, int pos)3103     public final float getHistoricalToolMajor(int pointerIndex, int pos) {
3104         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, pointerIndex, pos);
3105     }
3106 
3107     /**
3108      * Returns a historical tool minor axis coordinate, as per {@link #getToolMinor(int)}, that
3109      * occurred between this event and the previous event for the given pointer.
3110      * Only applies to ACTION_MOVE events.
3111      *
3112      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3113      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3114      * @param pos Which historical value to return; must be less than
3115      * {@link #getHistorySize}
3116      *
3117      * @see #getHistorySize
3118      * @see #getToolMinor(int)
3119      * @see #AXIS_TOOL_MINOR
3120      */
getHistoricalToolMinor(int pointerIndex, int pos)3121     public final float getHistoricalToolMinor(int pointerIndex, int pos) {
3122         return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, pointerIndex, pos);
3123     }
3124 
3125     /**
3126      * Returns a historical orientation coordinate, as per {@link #getOrientation(int)}, that
3127      * occurred between this event and the previous event for the given pointer.
3128      * Only applies to ACTION_MOVE events.
3129      *
3130      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3131      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3132      * @param pos Which historical value to return; must be less than
3133      * {@link #getHistorySize}
3134      *
3135      * @see #getHistorySize
3136      * @see #getOrientation(int)
3137      * @see #AXIS_ORIENTATION
3138      */
getHistoricalOrientation(int pointerIndex, int pos)3139     public final float getHistoricalOrientation(int pointerIndex, int pos) {
3140         return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, pointerIndex, pos);
3141     }
3142 
3143     /**
3144      * Returns the historical value of the requested axis, as per {@link #getAxisValue(int, int)},
3145      * occurred between this event and the previous event for the given pointer.
3146      * Only applies to ACTION_MOVE events.
3147      *
3148      * @param axis The axis identifier for the axis value to retrieve.
3149      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3150      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3151      * @param pos Which historical value to return; must be less than
3152      * {@link #getHistorySize}
3153      * @return The value of the axis, or 0 if the axis is not available.
3154      *
3155      * @see #AXIS_X
3156      * @see #AXIS_Y
3157      */
getHistoricalAxisValue(int axis, int pointerIndex, int pos)3158     public final float getHistoricalAxisValue(int axis, int pointerIndex, int pos) {
3159         return nativeGetAxisValue(mNativePtr, axis, pointerIndex, pos);
3160     }
3161 
3162     /**
3163      * Populates a {@link PointerCoords} object with historical pointer coordinate data,
3164      * as per {@link #getPointerCoords}, that occurred between this event and the previous
3165      * event for the given pointer.
3166      * Only applies to ACTION_MOVE events.
3167      *
3168      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
3169      * (the first pointer that is down) to {@link #getPointerCount()}-1.
3170      * @param pos Which historical value to return; must be less than
3171      * {@link #getHistorySize}
3172      * @param outPointerCoords The pointer coordinate object to populate.
3173      *
3174      * @see #getHistorySize
3175      * @see #getPointerCoords
3176      * @see PointerCoords
3177      */
getHistoricalPointerCoords(int pointerIndex, int pos, PointerCoords outPointerCoords)3178     public final void getHistoricalPointerCoords(int pointerIndex, int pos,
3179             PointerCoords outPointerCoords) {
3180         nativeGetPointerCoords(mNativePtr, pointerIndex, pos, outPointerCoords);
3181     }
3182 
3183     /**
3184      * Returns a bitfield indicating which edges, if any, were touched by this
3185      * MotionEvent. For touch events, clients can use this to determine if the
3186      * user's finger was touching the edge of the display.
3187      *
3188      * This property is only set for {@link #ACTION_DOWN} events.
3189      *
3190      * @see #EDGE_LEFT
3191      * @see #EDGE_TOP
3192      * @see #EDGE_RIGHT
3193      * @see #EDGE_BOTTOM
3194      */
getEdgeFlags()3195     public final int getEdgeFlags() {
3196         return nativeGetEdgeFlags(mNativePtr);
3197     }
3198 
3199     /**
3200      * Sets the bitfield indicating which edges, if any, were touched by this
3201      * MotionEvent.
3202      *
3203      * @see #getEdgeFlags()
3204      */
setEdgeFlags(int flags)3205     public final void setEdgeFlags(int flags) {
3206         nativeSetEdgeFlags(mNativePtr, flags);
3207     }
3208 
3209     /**
3210      * Sets this event's action.
3211      */
setAction(int action)3212     public final void setAction(int action) {
3213         nativeSetAction(mNativePtr, action);
3214     }
3215 
3216     /**
3217      * Adjust this event's location.
3218      * @param deltaX Amount to add to the current X coordinate of the event.
3219      * @param deltaY Amount to add to the current Y coordinate of the event.
3220      */
offsetLocation(float deltaX, float deltaY)3221     public final void offsetLocation(float deltaX, float deltaY) {
3222         if (deltaX != 0.0f || deltaY != 0.0f) {
3223             nativeOffsetLocation(mNativePtr, deltaX, deltaY);
3224         }
3225     }
3226 
3227     /**
3228      * Set this event's location.  Applies {@link #offsetLocation} with a
3229      * delta from the current location to the given new location.
3230      *
3231      * @param x New absolute X location.
3232      * @param y New absolute Y location.
3233      */
setLocation(float x, float y)3234     public final void setLocation(float x, float y) {
3235         float oldX = getX();
3236         float oldY = getY();
3237         offsetLocation(x - oldX, y - oldY);
3238     }
3239 
3240     /**
3241      * Applies a transformation matrix to all of the points in the event.
3242      *
3243      * @param matrix The transformation matrix to apply.
3244      */
transform(Matrix matrix)3245     public final void transform(Matrix matrix) {
3246         if (matrix == null) {
3247             throw new IllegalArgumentException("matrix must not be null");
3248         }
3249 
3250         nativeTransform(mNativePtr, matrix);
3251     }
3252 
3253     /**
3254      * Add a new movement to the batch of movements in this event.  The event's
3255      * current location, position and size is updated to the new values.
3256      * The current values in the event are added to a list of historical values.
3257      *
3258      * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events.
3259      *
3260      * @param eventTime The time stamp (in ms) for this data.
3261      * @param x The new X position.
3262      * @param y The new Y position.
3263      * @param pressure The new pressure.
3264      * @param size The new size.
3265      * @param metaState Meta key state.
3266      */
addBatch(long eventTime, float x, float y, float pressure, float size, int metaState)3267     public final void addBatch(long eventTime, float x, float y,
3268             float pressure, float size, int metaState) {
3269         synchronized (gSharedTempLock) {
3270             ensureSharedTempPointerCapacity(1);
3271             final PointerCoords[] pc = gSharedTempPointerCoords;
3272             pc[0].clear();
3273             pc[0].x = x;
3274             pc[0].y = y;
3275             pc[0].pressure = pressure;
3276             pc[0].size = size;
3277 
3278             nativeAddBatch(mNativePtr, eventTime * NS_PER_MS, pc, metaState);
3279         }
3280     }
3281 
3282     /**
3283      * Add a new movement to the batch of movements in this event.  The event's
3284      * current location, position and size is updated to the new values.
3285      * The current values in the event are added to a list of historical values.
3286      *
3287      * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events.
3288      *
3289      * @param eventTime The time stamp (in ms) for this data.
3290      * @param pointerCoords The new pointer coordinates.
3291      * @param metaState Meta key state.
3292      */
addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState)3293     public final void addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState) {
3294         nativeAddBatch(mNativePtr, eventTime * NS_PER_MS, pointerCoords, metaState);
3295     }
3296 
3297     /**
3298      * Adds all of the movement samples of the specified event to this one if
3299      * it is compatible.  To be compatible, the event must have the same device id,
3300      * source, display id, action, flags, classification, pointer count, pointer properties.
3301      *
3302      * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events.
3303      *
3304      * @param event The event whose movements samples should be added to this one
3305      * if possible.
3306      * @return True if batching was performed or false if batching was not possible.
3307      * @hide
3308      */
3309     @UnsupportedAppUsage
addBatch(MotionEvent event)3310     public final boolean addBatch(MotionEvent event) {
3311         final int action = nativeGetAction(mNativePtr);
3312         if (action != ACTION_MOVE && action != ACTION_HOVER_MOVE) {
3313             return false;
3314         }
3315         if (action != nativeGetAction(event.mNativePtr)) {
3316             return false;
3317         }
3318 
3319         if (nativeGetDeviceId(mNativePtr) != nativeGetDeviceId(event.mNativePtr)
3320                 || nativeGetSource(mNativePtr) != nativeGetSource(event.mNativePtr)
3321                 || nativeGetDisplayId(mNativePtr) != nativeGetDisplayId(event.mNativePtr)
3322                 || nativeGetFlags(mNativePtr) != nativeGetFlags(event.mNativePtr)
3323                 || nativeGetClassification(mNativePtr)
3324                         != nativeGetClassification(event.mNativePtr)) {
3325             return false;
3326         }
3327 
3328         final int pointerCount = nativeGetPointerCount(mNativePtr);
3329         if (pointerCount != nativeGetPointerCount(event.mNativePtr)) {
3330             return false;
3331         }
3332 
3333         synchronized (gSharedTempLock) {
3334             ensureSharedTempPointerCapacity(Math.max(pointerCount, 2));
3335             final PointerProperties[] pp = gSharedTempPointerProperties;
3336             final PointerCoords[] pc = gSharedTempPointerCoords;
3337 
3338             for (int i = 0; i < pointerCount; i++) {
3339                 nativeGetPointerProperties(mNativePtr, i, pp[0]);
3340                 nativeGetPointerProperties(event.mNativePtr, i, pp[1]);
3341                 if (!pp[0].equals(pp[1])) {
3342                     return false;
3343                 }
3344             }
3345 
3346             final int metaState = nativeGetMetaState(event.mNativePtr);
3347             final int historySize = nativeGetHistorySize(event.mNativePtr);
3348             for (int h = 0; h <= historySize; h++) {
3349                 final int historyPos = (h == historySize ? HISTORY_CURRENT : h);
3350 
3351                 for (int i = 0; i < pointerCount; i++) {
3352                     nativeGetPointerCoords(event.mNativePtr, i, historyPos, pc[i]);
3353                 }
3354 
3355                 final long eventTimeNanos = nativeGetEventTimeNanos(event.mNativePtr, historyPos);
3356                 nativeAddBatch(mNativePtr, eventTimeNanos, pc, metaState);
3357             }
3358         }
3359         return true;
3360     }
3361 
3362     /**
3363      * Returns true if all points in the motion event are completely within the specified bounds.
3364      * @hide
3365      */
isWithinBoundsNoHistory(float left, float top, float right, float bottom)3366     public final boolean isWithinBoundsNoHistory(float left, float top,
3367             float right, float bottom) {
3368         final int pointerCount = nativeGetPointerCount(mNativePtr);
3369         for (int i = 0; i < pointerCount; i++) {
3370             final float x = nativeGetAxisValue(mNativePtr, AXIS_X, i, HISTORY_CURRENT);
3371             final float y = nativeGetAxisValue(mNativePtr, AXIS_Y, i, HISTORY_CURRENT);
3372             if (x < left || x > right || y < top || y > bottom) {
3373                 return false;
3374             }
3375         }
3376         return true;
3377     }
3378 
clamp(float value, float low, float high)3379     private static final float clamp(float value, float low, float high) {
3380         if (value < low) {
3381             return low;
3382         } else if (value > high) {
3383             return high;
3384         }
3385         return value;
3386     }
3387 
3388     /**
3389      * Returns a new motion events whose points have been clamped to the specified bounds.
3390      * @hide
3391      */
clampNoHistory(float left, float top, float right, float bottom)3392     public final MotionEvent clampNoHistory(float left, float top, float right, float bottom) {
3393         MotionEvent ev = obtain();
3394         synchronized (gSharedTempLock) {
3395             final int pointerCount = nativeGetPointerCount(mNativePtr);
3396 
3397             ensureSharedTempPointerCapacity(pointerCount);
3398             final PointerProperties[] pp = gSharedTempPointerProperties;
3399             final PointerCoords[] pc = gSharedTempPointerCoords;
3400 
3401             for (int i = 0; i < pointerCount; i++) {
3402                 nativeGetPointerProperties(mNativePtr, i, pp[i]);
3403                 nativeGetPointerCoords(mNativePtr, i, HISTORY_CURRENT, pc[i]);
3404                 pc[i].x = clamp(pc[i].x, left, right);
3405                 pc[i].y = clamp(pc[i].y, top, bottom);
3406             }
3407             ev.initialize(nativeGetDeviceId(mNativePtr), nativeGetSource(mNativePtr),
3408                     nativeGetDisplayId(mNativePtr),
3409                     nativeGetAction(mNativePtr), nativeGetFlags(mNativePtr),
3410                     nativeGetEdgeFlags(mNativePtr), nativeGetMetaState(mNativePtr),
3411                     nativeGetButtonState(mNativePtr), nativeGetClassification(mNativePtr),
3412                     nativeGetXOffset(mNativePtr), nativeGetYOffset(mNativePtr),
3413                     nativeGetXPrecision(mNativePtr), nativeGetYPrecision(mNativePtr),
3414                     nativeGetDownTimeNanos(mNativePtr),
3415                     nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT),
3416                     pointerCount, pp, pc);
3417             return ev;
3418         }
3419     }
3420 
3421     /**
3422      * Gets an integer where each pointer id present in the event is marked as a bit.
3423      * @hide
3424      */
3425     @UnsupportedAppUsage
getPointerIdBits()3426     public final int getPointerIdBits() {
3427         int idBits = 0;
3428         final int pointerCount = nativeGetPointerCount(mNativePtr);
3429         for (int i = 0; i < pointerCount; i++) {
3430             idBits |= 1 << nativeGetPointerId(mNativePtr, i);
3431         }
3432         return idBits;
3433     }
3434 
3435     /**
3436      * Splits a motion event such that it includes only a subset of pointer ids.
3437      * @hide
3438      */
3439     @UnsupportedAppUsage
split(int idBits)3440     public final MotionEvent split(int idBits) {
3441         MotionEvent ev = obtain();
3442         synchronized (gSharedTempLock) {
3443             final int oldPointerCount = nativeGetPointerCount(mNativePtr);
3444             ensureSharedTempPointerCapacity(oldPointerCount);
3445             final PointerProperties[] pp = gSharedTempPointerProperties;
3446             final PointerCoords[] pc = gSharedTempPointerCoords;
3447             final int[] map = gSharedTempPointerIndexMap;
3448 
3449             final int oldAction = nativeGetAction(mNativePtr);
3450             final int oldActionMasked = oldAction & ACTION_MASK;
3451             final int oldActionPointerIndex = (oldAction & ACTION_POINTER_INDEX_MASK)
3452                     >> ACTION_POINTER_INDEX_SHIFT;
3453             int newActionPointerIndex = -1;
3454             int newPointerCount = 0;
3455             for (int i = 0; i < oldPointerCount; i++) {
3456                 nativeGetPointerProperties(mNativePtr, i, pp[newPointerCount]);
3457                 final int idBit = 1 << pp[newPointerCount].id;
3458                 if ((idBit & idBits) != 0) {
3459                     if (i == oldActionPointerIndex) {
3460                         newActionPointerIndex = newPointerCount;
3461                     }
3462                     map[newPointerCount] = i;
3463                     newPointerCount += 1;
3464                 }
3465             }
3466 
3467             if (newPointerCount == 0) {
3468                 throw new IllegalArgumentException("idBits did not match any ids in the event");
3469             }
3470 
3471             final int newAction;
3472             if (oldActionMasked == ACTION_POINTER_DOWN || oldActionMasked == ACTION_POINTER_UP) {
3473                 if (newActionPointerIndex < 0) {
3474                     // An unrelated pointer changed.
3475                     newAction = ACTION_MOVE;
3476                 } else if (newPointerCount == 1) {
3477                     // The first/last pointer went down/up.
3478                     newAction = oldActionMasked == ACTION_POINTER_DOWN
3479                             ? ACTION_DOWN : ACTION_UP;
3480                 } else {
3481                     // A secondary pointer went down/up.
3482                     newAction = oldActionMasked
3483                             | (newActionPointerIndex << ACTION_POINTER_INDEX_SHIFT);
3484                 }
3485             } else {
3486                 // Simple up/down/cancel/move or other motion action.
3487                 newAction = oldAction;
3488             }
3489 
3490             final int historySize = nativeGetHistorySize(mNativePtr);
3491             for (int h = 0; h <= historySize; h++) {
3492                 final int historyPos = h == historySize ? HISTORY_CURRENT : h;
3493 
3494                 for (int i = 0; i < newPointerCount; i++) {
3495                     nativeGetPointerCoords(mNativePtr, map[i], historyPos, pc[i]);
3496                 }
3497 
3498                 final long eventTimeNanos = nativeGetEventTimeNanos(mNativePtr, historyPos);
3499                 if (h == 0) {
3500                     ev.initialize(nativeGetDeviceId(mNativePtr), nativeGetSource(mNativePtr),
3501                             nativeGetDisplayId(mNativePtr),
3502                             newAction, nativeGetFlags(mNativePtr),
3503                             nativeGetEdgeFlags(mNativePtr), nativeGetMetaState(mNativePtr),
3504                             nativeGetButtonState(mNativePtr), nativeGetClassification(mNativePtr),
3505                             nativeGetXOffset(mNativePtr), nativeGetYOffset(mNativePtr),
3506                             nativeGetXPrecision(mNativePtr), nativeGetYPrecision(mNativePtr),
3507                             nativeGetDownTimeNanos(mNativePtr), eventTimeNanos,
3508                             newPointerCount, pp, pc);
3509                 } else {
3510                     nativeAddBatch(ev.mNativePtr, eventTimeNanos, pc, 0);
3511                 }
3512             }
3513             return ev;
3514         }
3515     }
3516 
3517     /**
3518      * Calculate new cursor position for events from mouse. This is used to split, clamp and inject
3519      * events.
3520      *
3521      * <p>If the source is mouse, it sets cursor position to the centroid of all pointers because
3522      * InputReader maps multiple fingers on a touchpad to locations around cursor position in screen
3523      * coordinates so that the mouse cursor is at the centroid of all pointers.
3524      *
3525      * <p>If the source is not mouse it sets cursor position to NaN.
3526      */
updateCursorPosition()3527     private void updateCursorPosition() {
3528         if (getSource() != InputDevice.SOURCE_MOUSE) {
3529             setCursorPosition(INVALID_CURSOR_POSITION, INVALID_CURSOR_POSITION);
3530             return;
3531         }
3532 
3533         float x = 0;
3534         float y = 0;
3535 
3536         final int pointerCount = getPointerCount();
3537         for (int i = 0; i < pointerCount; ++i) {
3538             x += getX(i);
3539             y += getY(i);
3540         }
3541 
3542         // If pointer count is 0, divisions below yield NaN, which is an acceptable result for this
3543         // corner case.
3544         x /= pointerCount;
3545         y /= pointerCount;
3546         setCursorPosition(x, y);
3547     }
3548 
3549     @Override
toString()3550     public String toString() {
3551         StringBuilder msg = new StringBuilder();
3552         msg.append("MotionEvent { action=").append(actionToString(getAction()));
3553         appendUnless("0", msg, ", actionButton=", buttonStateToString(getActionButton()));
3554 
3555         final int pointerCount = getPointerCount();
3556         for (int i = 0; i < pointerCount; i++) {
3557             appendUnless(i, msg, ", id[" + i + "]=", getPointerId(i));
3558             float x = getX(i);
3559             float y = getY(i);
3560             if (!DEBUG_CONCISE_TOSTRING || x != 0f || y != 0f) {
3561                 msg.append(", x[").append(i).append("]=").append(x);
3562                 msg.append(", y[").append(i).append("]=").append(y);
3563             }
3564             appendUnless(TOOL_TYPE_SYMBOLIC_NAMES.get(TOOL_TYPE_FINGER),
3565                     msg, ", toolType[" + i + "]=", toolTypeToString(getToolType(i)));
3566         }
3567 
3568         appendUnless("0", msg, ", buttonState=", MotionEvent.buttonStateToString(getButtonState()));
3569         appendUnless(classificationToString(CLASSIFICATION_NONE), msg, ", classification=",
3570                 classificationToString(getClassification()));
3571         appendUnless("0", msg, ", metaState=", KeyEvent.metaStateToString(getMetaState()));
3572         appendUnless("0", msg, ", flags=0x", Integer.toHexString(getFlags()));
3573         appendUnless("0", msg, ", edgeFlags=0x", Integer.toHexString(getEdgeFlags()));
3574         appendUnless(1, msg, ", pointerCount=", pointerCount);
3575         appendUnless(0, msg, ", historySize=", getHistorySize());
3576         msg.append(", eventTime=").append(getEventTime());
3577         if (!DEBUG_CONCISE_TOSTRING) {
3578             msg.append(", downTime=").append(getDownTime());
3579             msg.append(", deviceId=").append(getDeviceId());
3580             msg.append(", source=0x").append(Integer.toHexString(getSource()));
3581             msg.append(", displayId=").append(getDisplayId());
3582         }
3583         msg.append(" }");
3584         return msg.toString();
3585     }
3586 
appendUnless(T defValue, StringBuilder sb, String key, T value)3587     private static <T> void appendUnless(T defValue, StringBuilder sb, String key, T value) {
3588         if (DEBUG_CONCISE_TOSTRING && Objects.equals(defValue, value)) return;
3589         sb.append(key).append(value);
3590     }
3591 
3592     /**
3593      * Returns a string that represents the symbolic name of the specified unmasked action
3594      * such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent numeric constant
3595      * such as "35" if unknown.
3596      *
3597      * @param action The unmasked action.
3598      * @return The symbolic name of the specified action.
3599      * @see #getAction()
3600      */
actionToString(int action)3601     public static String actionToString(int action) {
3602         switch (action) {
3603             case ACTION_DOWN:
3604                 return "ACTION_DOWN";
3605             case ACTION_UP:
3606                 return "ACTION_UP";
3607             case ACTION_CANCEL:
3608                 return "ACTION_CANCEL";
3609             case ACTION_OUTSIDE:
3610                 return "ACTION_OUTSIDE";
3611             case ACTION_MOVE:
3612                 return "ACTION_MOVE";
3613             case ACTION_HOVER_MOVE:
3614                 return "ACTION_HOVER_MOVE";
3615             case ACTION_SCROLL:
3616                 return "ACTION_SCROLL";
3617             case ACTION_HOVER_ENTER:
3618                 return "ACTION_HOVER_ENTER";
3619             case ACTION_HOVER_EXIT:
3620                 return "ACTION_HOVER_EXIT";
3621             case ACTION_BUTTON_PRESS:
3622                 return "ACTION_BUTTON_PRESS";
3623             case ACTION_BUTTON_RELEASE:
3624                 return "ACTION_BUTTON_RELEASE";
3625         }
3626         int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT;
3627         switch (action & ACTION_MASK) {
3628             case ACTION_POINTER_DOWN:
3629                 return "ACTION_POINTER_DOWN(" + index + ")";
3630             case ACTION_POINTER_UP:
3631                 return "ACTION_POINTER_UP(" + index + ")";
3632             default:
3633                 return Integer.toString(action);
3634         }
3635     }
3636 
3637     /**
3638      * Returns a string that represents the symbolic name of the specified axis
3639      * such as "AXIS_X" or an equivalent numeric constant such as "42" if unknown.
3640      *
3641      * @param axis The axis.
3642      * @return The symbolic name of the specified axis.
3643      */
axisToString(int axis)3644     public static String axisToString(int axis) {
3645         String symbolicName = nativeAxisToString(axis);
3646         return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(axis);
3647     }
3648 
3649     /**
3650      * Gets an axis by its symbolic name such as "AXIS_X" or an
3651      * equivalent numeric constant such as "42".
3652      *
3653      * @param symbolicName The symbolic name of the axis.
3654      * @return The axis or -1 if not found.
3655      * @see KeyEvent#keyCodeToString(int)
3656      */
axisFromString(String symbolicName)3657     public static int axisFromString(String symbolicName) {
3658         if (symbolicName.startsWith(LABEL_PREFIX)) {
3659             symbolicName = symbolicName.substring(LABEL_PREFIX.length());
3660             int axis = nativeAxisFromString(symbolicName);
3661             if (axis >= 0) {
3662                 return axis;
3663             }
3664         }
3665         try {
3666             return Integer.parseInt(symbolicName, 10);
3667         } catch (NumberFormatException ex) {
3668             return -1;
3669         }
3670     }
3671 
3672     /**
3673      * Returns a string that represents the symbolic name of the specified combined
3674      * button state flags such as "0", "BUTTON_PRIMARY",
3675      * "BUTTON_PRIMARY|BUTTON_SECONDARY" or an equivalent numeric constant such as "0x10000000"
3676      * if unknown.
3677      *
3678      * @param buttonState The button state.
3679      * @return The symbolic name of the specified combined button state flags.
3680      * @hide
3681      */
buttonStateToString(int buttonState)3682     public static String buttonStateToString(int buttonState) {
3683         if (buttonState == 0) {
3684             return "0";
3685         }
3686         StringBuilder result = null;
3687         int i = 0;
3688         while (buttonState != 0) {
3689             final boolean isSet = (buttonState & 1) != 0;
3690             buttonState >>>= 1; // unsigned shift!
3691             if (isSet) {
3692                 final String name = BUTTON_SYMBOLIC_NAMES[i];
3693                 if (result == null) {
3694                     if (buttonState == 0) {
3695                         return name;
3696                     }
3697                     result = new StringBuilder(name);
3698                 } else {
3699                     result.append('|');
3700                     result.append(name);
3701                 }
3702             }
3703             i += 1;
3704         }
3705         return result.toString();
3706     }
3707 
3708     /**
3709      * Returns a string that represents the symbolic name of the specified classification.
3710      *
3711      * @param classification The classification type.
3712      * @return The symbolic name of this classification.
3713      * @hide
3714      */
classificationToString(@lassification int classification)3715     public static String classificationToString(@Classification int classification) {
3716         switch (classification) {
3717             case CLASSIFICATION_NONE:
3718                 return "NONE";
3719             case CLASSIFICATION_AMBIGUOUS_GESTURE:
3720                 return "AMBIGUOUS_GESTURE";
3721             case CLASSIFICATION_DEEP_PRESS:
3722                 return "DEEP_PRESS";
3723 
3724         }
3725         return "NONE";
3726     }
3727 
3728     /**
3729      * Returns a string that represents the symbolic name of the specified tool type
3730      * such as "TOOL_TYPE_FINGER" or an equivalent numeric constant such as "42" if unknown.
3731      *
3732      * @param toolType The tool type.
3733      * @return The symbolic name of the specified tool type.
3734      * @hide
3735      */
toolTypeToString(int toolType)3736     public static String toolTypeToString(int toolType) {
3737         String symbolicName = TOOL_TYPE_SYMBOLIC_NAMES.get(toolType);
3738         return symbolicName != null ? symbolicName : Integer.toString(toolType);
3739     }
3740 
3741     /**
3742      * Checks if a mouse or stylus button (or combination of buttons) is pressed.
3743      * @param button Button (or combination of buttons).
3744      * @return True if specified buttons are pressed.
3745      *
3746      * @see #BUTTON_PRIMARY
3747      * @see #BUTTON_SECONDARY
3748      * @see #BUTTON_TERTIARY
3749      * @see #BUTTON_FORWARD
3750      * @see #BUTTON_BACK
3751      * @see #BUTTON_STYLUS_PRIMARY
3752      * @see #BUTTON_STYLUS_SECONDARY
3753      */
isButtonPressed(int button)3754     public final boolean isButtonPressed(int button) {
3755         if (button == 0) {
3756             return false;
3757         }
3758         return (getButtonState() & button) == button;
3759     }
3760 
3761     public static final @android.annotation.NonNull Parcelable.Creator<MotionEvent> CREATOR
3762             = new Parcelable.Creator<MotionEvent>() {
3763         public MotionEvent createFromParcel(Parcel in) {
3764             in.readInt(); // skip token, we already know this is a MotionEvent
3765             return MotionEvent.createFromParcelBody(in);
3766         }
3767 
3768         public MotionEvent[] newArray(int size) {
3769             return new MotionEvent[size];
3770         }
3771     };
3772 
3773     /** @hide */
createFromParcelBody(Parcel in)3774     public static MotionEvent createFromParcelBody(Parcel in) {
3775         MotionEvent ev = obtain();
3776         ev.mNativePtr = nativeReadFromParcel(ev.mNativePtr, in);
3777         return ev;
3778     }
3779 
3780     /** @hide */
3781     @Override
cancel()3782     public final void cancel() {
3783         setAction(ACTION_CANCEL);
3784     }
3785 
writeToParcel(Parcel out, int flags)3786     public void writeToParcel(Parcel out, int flags) {
3787         out.writeInt(PARCEL_TOKEN_MOTION_EVENT);
3788         nativeWriteToParcel(mNativePtr, out);
3789     }
3790 
3791     /**
3792      * Transfer object for pointer coordinates.
3793      *
3794      * Objects of this type can be used to specify the pointer coordinates when
3795      * creating new {@link MotionEvent} objects and to query pointer coordinates
3796      * in bulk.
3797      *
3798      * Refer to {@link InputDevice} for information about how different kinds of
3799      * input devices and sources represent pointer coordinates.
3800      */
3801     public static final class PointerCoords {
3802         private static final int INITIAL_PACKED_AXIS_VALUES = 8;
3803         @UnsupportedAppUsage
3804         private long mPackedAxisBits;
3805         @UnsupportedAppUsage
3806         private float[] mPackedAxisValues;
3807 
3808         /**
3809          * Creates a pointer coords object with all axes initialized to zero.
3810          */
PointerCoords()3811         public PointerCoords() {
3812         }
3813 
3814         /**
3815          * Creates a pointer coords object as a copy of the
3816          * contents of another pointer coords object.
3817          *
3818          * @param other The pointer coords object to copy.
3819          */
PointerCoords(PointerCoords other)3820         public PointerCoords(PointerCoords other) {
3821             copyFrom(other);
3822         }
3823 
3824         /** @hide */
3825         @UnsupportedAppUsage
createArray(int size)3826         public static PointerCoords[] createArray(int size) {
3827             PointerCoords[] array = new PointerCoords[size];
3828             for (int i = 0; i < size; i++) {
3829                 array[i] = new PointerCoords();
3830             }
3831             return array;
3832         }
3833 
3834         /**
3835          * The X component of the pointer movement.
3836          *
3837          * @see MotionEvent#AXIS_X
3838          */
3839         public float x;
3840 
3841         /**
3842          * The Y component of the pointer movement.
3843          *
3844          * @see MotionEvent#AXIS_Y
3845          */
3846         public float y;
3847 
3848         /**
3849          * A normalized value that describes the pressure applied to the device
3850          * by a finger or other tool.
3851          * The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure),
3852          * although values higher than 1 may be generated depending on the calibration of
3853          * the input device.
3854          *
3855          * @see MotionEvent#AXIS_PRESSURE
3856          */
3857         public float pressure;
3858 
3859         /**
3860          * A normalized value that describes the approximate size of the pointer touch area
3861          * in relation to the maximum detectable size of the device.
3862          * It represents some approximation of the area of the screen being
3863          * pressed; the actual value in pixels corresponding to the
3864          * touch is normalized with the device specific range of values
3865          * and scaled to a value between 0 and 1. The value of size can be used to
3866          * determine fat touch events.
3867          *
3868          * @see MotionEvent#AXIS_SIZE
3869          */
3870         public float size;
3871 
3872         /**
3873          * The length of the major axis of an ellipse that describes the touch area at
3874          * the point of contact.
3875          * If the device is a touch screen, the length is reported in pixels, otherwise it is
3876          * reported in device-specific units.
3877          *
3878          * @see MotionEvent#AXIS_TOUCH_MAJOR
3879          */
3880         public float touchMajor;
3881 
3882         /**
3883          * The length of the minor axis of an ellipse that describes the touch area at
3884          * the point of contact.
3885          * If the device is a touch screen, the length is reported in pixels, otherwise it is
3886          * reported in device-specific units.
3887          *
3888          * @see MotionEvent#AXIS_TOUCH_MINOR
3889          */
3890         public float touchMinor;
3891 
3892         /**
3893          * The length of the major axis of an ellipse that describes the size of
3894          * the approaching tool.
3895          * The tool area represents the estimated size of the finger or pen that is
3896          * touching the device independent of its actual touch area at the point of contact.
3897          * If the device is a touch screen, the length is reported in pixels, otherwise it is
3898          * reported in device-specific units.
3899          *
3900          * @see MotionEvent#AXIS_TOOL_MAJOR
3901          */
3902         public float toolMajor;
3903 
3904         /**
3905          * The length of the minor axis of an ellipse that describes the size of
3906          * the approaching tool.
3907          * The tool area represents the estimated size of the finger or pen that is
3908          * touching the device independent of its actual touch area at the point of contact.
3909          * If the device is a touch screen, the length is reported in pixels, otherwise it is
3910          * reported in device-specific units.
3911          *
3912          * @see MotionEvent#AXIS_TOOL_MINOR
3913          */
3914         public float toolMinor;
3915 
3916         /**
3917          * The orientation of the touch area and tool area in radians clockwise from vertical.
3918          * An angle of 0 radians indicates that the major axis of contact is oriented
3919          * upwards, is perfectly circular or is of unknown orientation.  A positive angle
3920          * indicates that the major axis of contact is oriented to the right.  A negative angle
3921          * indicates that the major axis of contact is oriented to the left.
3922          * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians
3923          * (finger pointing fully right).
3924          *
3925          * @see MotionEvent#AXIS_ORIENTATION
3926          */
3927         public float orientation;
3928 
3929         /**
3930          * Clears the contents of this object.
3931          * Resets all axes to zero.
3932          */
clear()3933         public void clear() {
3934             mPackedAxisBits = 0;
3935 
3936             x = 0;
3937             y = 0;
3938             pressure = 0;
3939             size = 0;
3940             touchMajor = 0;
3941             touchMinor = 0;
3942             toolMajor = 0;
3943             toolMinor = 0;
3944             orientation = 0;
3945         }
3946 
3947         /**
3948          * Copies the contents of another pointer coords object.
3949          *
3950          * @param other The pointer coords object to copy.
3951          */
copyFrom(PointerCoords other)3952         public void copyFrom(PointerCoords other) {
3953             final long bits = other.mPackedAxisBits;
3954             mPackedAxisBits = bits;
3955             if (bits != 0) {
3956                 final float[] otherValues = other.mPackedAxisValues;
3957                 final int count = Long.bitCount(bits);
3958                 float[] values = mPackedAxisValues;
3959                 if (values == null || count > values.length) {
3960                     values = new float[otherValues.length];
3961                     mPackedAxisValues = values;
3962                 }
3963                 System.arraycopy(otherValues, 0, values, 0, count);
3964             }
3965 
3966             x = other.x;
3967             y = other.y;
3968             pressure = other.pressure;
3969             size = other.size;
3970             touchMajor = other.touchMajor;
3971             touchMinor = other.touchMinor;
3972             toolMajor = other.toolMajor;
3973             toolMinor = other.toolMinor;
3974             orientation = other.orientation;
3975         }
3976 
3977         /**
3978          * Gets the value associated with the specified axis.
3979          *
3980          * @param axis The axis identifier for the axis value to retrieve.
3981          * @return The value associated with the axis, or 0 if none.
3982          *
3983          * @see MotionEvent#AXIS_X
3984          * @see MotionEvent#AXIS_Y
3985          */
getAxisValue(int axis)3986         public float getAxisValue(int axis) {
3987             switch (axis) {
3988                 case AXIS_X:
3989                     return x;
3990                 case AXIS_Y:
3991                     return y;
3992                 case AXIS_PRESSURE:
3993                     return pressure;
3994                 case AXIS_SIZE:
3995                     return size;
3996                 case AXIS_TOUCH_MAJOR:
3997                     return touchMajor;
3998                 case AXIS_TOUCH_MINOR:
3999                     return touchMinor;
4000                 case AXIS_TOOL_MAJOR:
4001                     return toolMajor;
4002                 case AXIS_TOOL_MINOR:
4003                     return toolMinor;
4004                 case AXIS_ORIENTATION:
4005                     return orientation;
4006                 default: {
4007                     if (axis < 0 || axis > 63) {
4008                         throw new IllegalArgumentException("Axis out of range.");
4009                     }
4010                     final long bits = mPackedAxisBits;
4011                     final long axisBit = 0x8000000000000000L >>> axis;
4012                     if ((bits & axisBit) == 0) {
4013                         return 0;
4014                     }
4015                     final int index = Long.bitCount(bits & ~(0xFFFFFFFFFFFFFFFFL >>> axis));
4016                     return mPackedAxisValues[index];
4017                 }
4018             }
4019         }
4020 
4021         /**
4022          * Sets the value associated with the specified axis.
4023          *
4024          * @param axis The axis identifier for the axis value to assign.
4025          * @param value The value to set.
4026          *
4027          * @see MotionEvent#AXIS_X
4028          * @see MotionEvent#AXIS_Y
4029          */
setAxisValue(int axis, float value)4030         public void setAxisValue(int axis, float value) {
4031             switch (axis) {
4032                 case AXIS_X:
4033                     x = value;
4034                     break;
4035                 case AXIS_Y:
4036                     y = value;
4037                     break;
4038                 case AXIS_PRESSURE:
4039                     pressure = value;
4040                     break;
4041                 case AXIS_SIZE:
4042                     size = value;
4043                     break;
4044                 case AXIS_TOUCH_MAJOR:
4045                     touchMajor = value;
4046                     break;
4047                 case AXIS_TOUCH_MINOR:
4048                     touchMinor = value;
4049                     break;
4050                 case AXIS_TOOL_MAJOR:
4051                     toolMajor = value;
4052                     break;
4053                 case AXIS_TOOL_MINOR:
4054                     toolMinor = value;
4055                     break;
4056                 case AXIS_ORIENTATION:
4057                     orientation = value;
4058                     break;
4059                 default: {
4060                     if (axis < 0 || axis > 63) {
4061                         throw new IllegalArgumentException("Axis out of range.");
4062                     }
4063                     final long bits = mPackedAxisBits;
4064                     final long axisBit = 0x8000000000000000L >>> axis;
4065                     final int index = Long.bitCount(bits & ~(0xFFFFFFFFFFFFFFFFL >>> axis));
4066                     float[] values = mPackedAxisValues;
4067                     if ((bits & axisBit) == 0) {
4068                         if (values == null) {
4069                             values = new float[INITIAL_PACKED_AXIS_VALUES];
4070                             mPackedAxisValues = values;
4071                         } else {
4072                             final int count = Long.bitCount(bits);
4073                             if (count < values.length) {
4074                                 if (index != count) {
4075                                     System.arraycopy(values, index, values, index + 1,
4076                                             count - index);
4077                                 }
4078                             } else {
4079                                 float[] newValues = new float[count * 2];
4080                                 System.arraycopy(values, 0, newValues, 0, index);
4081                                 System.arraycopy(values, index, newValues, index + 1,
4082                                         count - index);
4083                                 values = newValues;
4084                                 mPackedAxisValues = values;
4085                             }
4086                         }
4087                         mPackedAxisBits = bits | axisBit;
4088                     }
4089                     values[index] = value;
4090                 }
4091             }
4092         }
4093     }
4094 
4095     /**
4096      * Transfer object for pointer properties.
4097      *
4098      * Objects of this type can be used to specify the pointer id and tool type
4099      * when creating new {@link MotionEvent} objects and to query pointer properties in bulk.
4100      */
4101     public static final class PointerProperties {
4102         /**
4103          * Creates a pointer properties object with an invalid pointer id.
4104          */
PointerProperties()4105         public PointerProperties() {
4106             clear();
4107         }
4108 
4109         /**
4110          * Creates a pointer properties object as a copy of the contents of
4111          * another pointer properties object.
4112          * @param other
4113          */
PointerProperties(PointerProperties other)4114         public PointerProperties(PointerProperties other) {
4115             copyFrom(other);
4116         }
4117 
4118         /** @hide */
4119         @UnsupportedAppUsage
createArray(int size)4120         public static PointerProperties[] createArray(int size) {
4121             PointerProperties[] array = new PointerProperties[size];
4122             for (int i = 0; i < size; i++) {
4123                 array[i] = new PointerProperties();
4124             }
4125             return array;
4126         }
4127 
4128         /**
4129          * The pointer id.
4130          * Initially set to {@link #INVALID_POINTER_ID} (-1).
4131          *
4132          * @see MotionEvent#getPointerId(int)
4133          */
4134         public int id;
4135 
4136         /**
4137          * The pointer tool type.
4138          * Initially set to 0.
4139          *
4140          * @see MotionEvent#getToolType(int)
4141          */
4142         public int toolType;
4143 
4144         /**
4145          * Resets the pointer properties to their initial values.
4146          */
clear()4147         public void clear() {
4148             id = INVALID_POINTER_ID;
4149             toolType = TOOL_TYPE_UNKNOWN;
4150         }
4151 
4152         /**
4153          * Copies the contents of another pointer properties object.
4154          *
4155          * @param other The pointer properties object to copy.
4156          */
copyFrom(PointerProperties other)4157         public void copyFrom(PointerProperties other) {
4158             id = other.id;
4159             toolType = other.toolType;
4160         }
4161 
4162         @Override
equals(Object other)4163         public boolean equals(Object other) {
4164             if (other instanceof PointerProperties) {
4165                 return equals((PointerProperties)other);
4166             }
4167             return false;
4168         }
4169 
equals(PointerProperties other)4170         private boolean equals(PointerProperties other) {
4171             return other != null && id == other.id && toolType == other.toolType;
4172         }
4173 
4174         @Override
hashCode()4175         public int hashCode() {
4176             return id | (toolType << 8);
4177         }
4178     }
4179 }
4180