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