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