1 /*
2  * Copyright 2018 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 androidx.core.view;
18 
19 import android.view.MotionEvent;
20 
21 import org.jspecify.annotations.NonNull;
22 
23 /**
24  * Helper for accessing features in {@link MotionEvent}.
25  */
26 public final class MotionEventCompat {
27     /**
28      * Synonym for {@link MotionEvent#ACTION_MASK}.
29      *
30      * @deprecated Use {@link MotionEvent#ACTION_MASK} directly.
31      */
32     @Deprecated
33     public static final int ACTION_MASK = 0xff;
34 
35     /**
36      * Synonym for {@link MotionEvent#ACTION_POINTER_DOWN}.
37      *
38      * @deprecated Use {@link MotionEvent#ACTION_POINTER_DOWN} directly.
39      */
40     @Deprecated
41     public static final int ACTION_POINTER_DOWN = 5;
42 
43     /**
44      * Synonym for {@link MotionEvent#ACTION_POINTER_UP}.
45      *
46      * @deprecated Use {@link MotionEvent#ACTION_POINTER_UP} directly.
47      */
48     @Deprecated
49     public static final int ACTION_POINTER_UP = 6;
50 
51     /**
52      * Synonym for {@link MotionEvent#ACTION_HOVER_MOVE}.
53      *
54      * @deprecated Use {@link MotionEvent#ACTION_HOVER_MOVE} directly.
55      */
56     @Deprecated
57     public static final int ACTION_HOVER_MOVE = 7;
58 
59     /**
60      * Synonym for {@link MotionEvent#ACTION_SCROLL}.
61      *
62      * @deprecated Use {@link MotionEvent#ACTION_SCROLL} directly.
63      */
64     @Deprecated
65     public static final int ACTION_SCROLL = 8;
66 
67     /**
68      * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_MASK}.
69      *
70      * @deprecated Use {@link MotionEvent#ACTION_POINTER_INDEX_MASK} directly.
71      */
72     @Deprecated
73     public static final int ACTION_POINTER_INDEX_MASK  = 0xff00;
74 
75     /**
76      * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_SHIFT}.
77      *
78      * @deprecated Use {@link MotionEvent#ACTION_POINTER_INDEX_SHIFT} directly.
79      */
80     @Deprecated
81     public static final int ACTION_POINTER_INDEX_SHIFT = 8;
82 
83     /**
84      * Synonym for {@link MotionEvent#ACTION_HOVER_ENTER}.
85      *
86      * @deprecated Use {@link MotionEvent#ACTION_HOVER_ENTER} directly.
87      */
88     @Deprecated
89     public static final int ACTION_HOVER_ENTER = 9;
90 
91     /**
92      * Synonym for {@link MotionEvent#ACTION_HOVER_EXIT}.
93      *
94      * @deprecated Use {@link MotionEvent#ACTION_HOVER_EXIT} directly.
95      */
96     @Deprecated
97     public static final int ACTION_HOVER_EXIT = 10;
98 
99     /**
100      * Synonym for {@link MotionEvent#AXIS_X}.
101      *
102      * @deprecated Use {@link MotionEvent#AXIS_X} directly.
103      */
104     @Deprecated
105     public static final int AXIS_X = 0;
106 
107     /**
108      * Synonym for {@link MotionEvent#AXIS_Y}.
109      *
110      * @deprecated Use {@link MotionEvent#AXIS_Y} directly.
111      */
112     @Deprecated
113     public static final int AXIS_Y = 1;
114 
115     /**
116      * Synonym for {@link MotionEvent#AXIS_PRESSURE}.
117      *
118      * @deprecated Use {@link MotionEvent#AXIS_PRESSURE} directly.
119      */
120     @Deprecated
121     public static final int AXIS_PRESSURE = 2;
122 
123     /**
124      * Synonym for {@link MotionEvent#AXIS_SIZE}.
125      *
126      * @deprecated Use {@link MotionEvent#AXIS_SIZE} directly.
127      */
128     @Deprecated
129     public static final int AXIS_SIZE = 3;
130 
131     /**
132      * Synonym for {@link MotionEvent#AXIS_TOUCH_MAJOR}.
133      *
134      * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MAJOR} directly.
135      */
136     @Deprecated
137     public static final int AXIS_TOUCH_MAJOR = 4;
138 
139     /**
140      * Synonym for {@link MotionEvent#AXIS_TOUCH_MINOR}.
141      *
142      * @deprecated Use {@link MotionEvent#AXIS_TOUCH_MINOR} directly.
143      */
144     @Deprecated
145     public static final int AXIS_TOUCH_MINOR = 5;
146 
147     /**
148      * Synonym for {@link MotionEvent#AXIS_TOOL_MAJOR}.
149      *
150      * @deprecated Use {@link MotionEvent#AXIS_TOOL_MAJOR} directly.
151      */
152     @Deprecated
153     public static final int AXIS_TOOL_MAJOR = 6;
154 
155     /**
156      * Synonym for {@link MotionEvent#AXIS_TOOL_MINOR}.
157      *
158      * @deprecated Use {@link MotionEvent#AXIS_TOOL_MINOR} directly.
159      */
160     @Deprecated
161     public static final int AXIS_TOOL_MINOR = 7;
162 
163     /**
164      * Synonym for {@link MotionEvent#AXIS_ORIENTATION}.
165      *
166      * @deprecated Use {@link MotionEvent#AXIS_ORIENTATION} directly.
167      */
168     @Deprecated
169     public static final int AXIS_ORIENTATION = 8;
170 
171     /**
172      * Synonym for {@link MotionEvent#AXIS_VSCROLL}.
173      *
174      * @deprecated Use {@link MotionEvent#AXIS_VSCROLL} directly.
175      */
176     @Deprecated
177     public static final int AXIS_VSCROLL = 9;
178 
179     /**
180      * Synonym for {@link MotionEvent#AXIS_HSCROLL}.
181      *
182      * @deprecated Use {@link MotionEvent#AXIS_HSCROLL} directly.
183      */
184     @Deprecated
185     public static final int AXIS_HSCROLL = 10;
186 
187     /**
188      * Synonym for {@link MotionEvent#AXIS_Z}.
189      *
190      * @deprecated Use {@link MotionEvent#AXIS_Z} directly.
191      */
192     @Deprecated
193     public static final int AXIS_Z = 11;
194 
195     /**
196      * Synonym for {@link MotionEvent#AXIS_RX}.
197      *
198      * @deprecated Use {@link MotionEvent#AXIS_RX} directly.
199      */
200     @Deprecated
201     public static final int AXIS_RX = 12;
202 
203     /**
204      * Synonym for {@link MotionEvent#AXIS_RY}.
205      *
206      * @deprecated Use {@link MotionEvent#AXIS_RY} directly.
207      */
208     @Deprecated
209     public static final int AXIS_RY = 13;
210 
211     /**
212      * Synonym for {@link MotionEvent#AXIS_RZ}.
213      *
214      * @deprecated Use {@link MotionEvent#AXIS_RZ} directly.
215      */
216     @Deprecated
217     public static final int AXIS_RZ = 14;
218 
219     /**
220      * Synonym for {@link MotionEvent#AXIS_HAT_X}.
221      *
222      * @deprecated Use {@link MotionEvent#AXIS_HAT_X} directly.
223      */
224     @Deprecated
225     public static final int AXIS_HAT_X = 15;
226 
227     /**
228      * Synonym for {@link MotionEvent#AXIS_HAT_Y}.
229      *
230      * @deprecated Use {@link MotionEvent#AXIS_HAT_Y} directly.
231      */
232     @Deprecated
233     public static final int AXIS_HAT_Y = 16;
234 
235     /**
236      * Synonym for {@link MotionEvent#AXIS_LTRIGGER}.
237      *
238      * @deprecated Use {@link MotionEvent#AXIS_LTRIGGER} directly.
239      */
240     @Deprecated
241     public static final int AXIS_LTRIGGER = 17;
242 
243     /**
244      * Synonym for {@link MotionEvent#AXIS_RTRIGGER}.
245      *
246      * @deprecated Use {@link MotionEvent#AXIS_RTRIGGER} directly.
247      */
248     @Deprecated
249     public static final int AXIS_RTRIGGER = 18;
250 
251     /**
252      * Synonym for {@link MotionEvent#AXIS_THROTTLE}.
253      *
254      * @deprecated Use {@link MotionEvent#AXIS_THROTTLE} directly.
255      */
256     @Deprecated
257     public static final int AXIS_THROTTLE = 19;
258 
259     /**
260      * Synonym for {@link MotionEvent#AXIS_RUDDER}.
261      *
262      * @deprecated Use {@link MotionEvent#AXIS_RUDDER} directly.
263      */
264     @Deprecated
265     public static final int AXIS_RUDDER = 20;
266 
267     /**
268      * Synonym for {@link MotionEvent#AXIS_WHEEL}.
269      *
270      * @deprecated Use {@link MotionEvent#AXIS_WHEEL} directly.
271      */
272     @Deprecated
273     public static final int AXIS_WHEEL = 21;
274 
275     /**
276      * Synonym for {@link MotionEvent#AXIS_GAS}.
277      *
278      * @deprecated Use {@link MotionEvent#AXIS_GAS} directly.
279      */
280     @Deprecated
281     public static final int AXIS_GAS = 22;
282 
283     /**
284      * Synonym for {@link MotionEvent#AXIS_BRAKE}.
285      *
286      * @deprecated Use {@link MotionEvent#AXIS_BRAKE} directly.
287      */
288     @Deprecated
289     public static final int AXIS_BRAKE = 23;
290 
291     /**
292      * Synonym for {@link MotionEvent#AXIS_DISTANCE}.
293      *
294      * @deprecated Use {@link MotionEvent#AXIS_DISTANCE} directly.
295      */
296     @Deprecated
297     public static final int AXIS_DISTANCE = 24;
298 
299     /**
300      * Synonym for {@link MotionEvent#AXIS_TILT}.
301      *
302      * @deprecated Use {@link MotionEvent#AXIS_TILT} directly.
303      */
304     @Deprecated
305     public static final int AXIS_TILT = 25;
306 
307     /**
308      * Synonym for {@link MotionEvent#AXIS_SCROLL}.
309      */
310     public static final int AXIS_SCROLL = 26;
311 
312     /**
313      * Synonym for {@link MotionEvent#AXIS_RELATIVE_X}.
314      */
315     public static final int AXIS_RELATIVE_X = 27;
316 
317     /**
318      * Synonym for {@link MotionEvent#AXIS_RELATIVE_Y}.
319      */
320     public static final int AXIS_RELATIVE_Y = 28;
321 
322     /**
323      * Synonym for {@link MotionEvent#AXIS_GENERIC_1}.
324      *
325      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_1} directly.
326      */
327     @Deprecated
328     public static final int AXIS_GENERIC_1 = 32;
329 
330     /**
331      * Synonym for {@link MotionEvent#AXIS_GENERIC_2}.
332      *
333      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_2} directly.
334      */
335     @Deprecated
336     public static final int AXIS_GENERIC_2 = 33;
337 
338     /**
339      * Synonym for {@link MotionEvent#AXIS_GENERIC_3}.
340      *
341      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_3} directly.
342      */
343     @Deprecated
344     public static final int AXIS_GENERIC_3 = 34;
345 
346     /**
347      * Synonym for {@link MotionEvent#AXIS_GENERIC_4}.
348      *
349      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_4} directly.
350      */
351     @Deprecated
352     public static final int AXIS_GENERIC_4 = 35;
353 
354     /**
355      * Synonym for {@link MotionEvent#AXIS_GENERIC_5}.
356      *
357      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_5} directly.
358      */
359     @Deprecated
360     public static final int AXIS_GENERIC_5 = 36;
361 
362     /**
363      * Synonym for {@link MotionEvent#AXIS_GENERIC_6}.
364      *
365      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_6} directly.
366      */
367     @Deprecated
368     public static final int AXIS_GENERIC_6 = 37;
369 
370     /**
371      * Synonym for {@link MotionEvent#AXIS_GENERIC_7}.
372      *
373      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_7} directly.
374      */
375     @Deprecated
376     public static final int AXIS_GENERIC_7 = 38;
377 
378     /**
379      * Synonym for {@link MotionEvent#AXIS_GENERIC_8}.
380      *
381      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_8} directly.
382      */
383     @Deprecated
384     public static final int AXIS_GENERIC_8 = 39;
385 
386     /**
387      * Synonym for {@link MotionEvent#AXIS_GENERIC_9}.
388      *
389      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_9} directly.
390      */
391     @Deprecated
392     public static final int AXIS_GENERIC_9 = 40;
393 
394     /**
395      * Synonym for {@link MotionEvent#AXIS_GENERIC_10}.
396      *
397      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_10} directly.
398      */
399     @Deprecated
400     public static final int AXIS_GENERIC_10 = 41;
401 
402     /**
403      * Synonym for {@link MotionEvent#AXIS_GENERIC_11}.
404      *
405      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_11} directly.
406      */
407     @Deprecated
408     public static final int AXIS_GENERIC_11 = 42;
409 
410     /**
411      * Synonym for {@link MotionEvent#AXIS_GENERIC_12}.
412      *
413      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_12} directly.
414      */
415     @Deprecated
416     public static final int AXIS_GENERIC_12 = 43;
417 
418     /**
419      * Synonym for {@link MotionEvent#AXIS_GENERIC_13}.
420      *
421      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_13} directly.
422      */
423     @Deprecated
424     public static final int AXIS_GENERIC_13 = 44;
425 
426     /**
427      * Synonym for {@link MotionEvent#AXIS_GENERIC_14}.
428      *
429      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_14} directly.
430      */
431     @Deprecated
432     public static final int AXIS_GENERIC_14 = 45;
433 
434     /**
435      * Synonym for {@link MotionEvent#AXIS_GENERIC_15}.
436      *
437      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_15} directly.
438      */
439     @Deprecated
440     public static final int AXIS_GENERIC_15 = 46;
441 
442     /**
443      * Synonym for {@link MotionEvent#AXIS_GENERIC_16}.
444      *
445      * @deprecated Use {@link MotionEvent#AXIS_GENERIC_16} directly.
446      */
447     @Deprecated
448     public static final int AXIS_GENERIC_16 = 47;
449 
450     /**
451      * Synonym for {@link MotionEvent#BUTTON_PRIMARY}.
452      *
453      * @deprecated Use {@link MotionEvent#BUTTON_PRIMARY} directly.
454      */
455     @Deprecated
456     public static final int BUTTON_PRIMARY = 1;
457 
458     /**
459      * Call {@link MotionEvent#getAction}, returning only the {@link #ACTION_MASK}
460      * portion.
461      *
462      * @deprecated Call {@link MotionEvent#getAction()} directly. This method will be
463      * removed in a future release.
464      */
465     @androidx.annotation.ReplaceWith(expression = "event.getActionMasked()")
466     @Deprecated
getActionMasked(MotionEvent event)467     public static int getActionMasked(MotionEvent event) {
468         return event.getActionMasked();
469     }
470 
471     /**
472      * Call {@link MotionEvent#getAction}, returning only the pointer index
473      * portion.
474      *
475      * @deprecated Call {@link MotionEvent#getActionIndex()} directly. This method will be
476      * removed in a future release.
477      */
478     @androidx.annotation.ReplaceWith(expression = "event.getActionIndex()")
479     @Deprecated
getActionIndex(MotionEvent event)480     public static int getActionIndex(MotionEvent event) {
481         return event.getActionIndex();
482     }
483 
484     /**
485      * Call {@link MotionEvent#findPointerIndex(int)}.
486      *
487      * @deprecated Call {@link MotionEvent#findPointerIndex(int)} directly. This method will be
488      * removed in a future release.
489      */
490     @androidx.annotation.ReplaceWith(expression = "event.findPointerIndex(pointerId)")
491     @Deprecated
findPointerIndex(MotionEvent event, int pointerId)492     public static int findPointerIndex(MotionEvent event, int pointerId) {
493         return event.findPointerIndex(pointerId);
494     }
495 
496     /**
497      * Call {@link MotionEvent#getPointerId(int)}.
498      *
499      * @deprecated Call {@link MotionEvent#getPointerId(int)} directly. This method will be
500      * removed in a future release.
501      */
502     @androidx.annotation.ReplaceWith(expression = "event.getPointerId(pointerIndex)")
503     @Deprecated
getPointerId(MotionEvent event, int pointerIndex)504     public static int getPointerId(MotionEvent event, int pointerIndex) {
505         return event.getPointerId(pointerIndex);
506     }
507 
508     /**
509      * Call {@link MotionEvent#getX(int)}.
510      *
511      * @deprecated Call {@link MotionEvent#getX()} directly. This method will be
512      * removed in a future release.
513      */
514     @androidx.annotation.ReplaceWith(expression = "event.getX(pointerIndex)")
515     @Deprecated
getX(MotionEvent event, int pointerIndex)516     public static float getX(MotionEvent event, int pointerIndex) {
517         return event.getX(pointerIndex);
518     }
519 
520     /**
521      * Call {@link MotionEvent#getY(int)}.
522      *
523      * @deprecated Call {@link MotionEvent#getY()} directly. This method will be
524      * removed in a future release.
525      */
526     @androidx.annotation.ReplaceWith(expression = "event.getY(pointerIndex)")
527     @Deprecated
getY(MotionEvent event, int pointerIndex)528     public static float getY(MotionEvent event, int pointerIndex) {
529         return event.getY(pointerIndex);
530     }
531 
532     /**
533      * The number of pointers of data contained in this event.  Always
534      *
535      * @deprecated Call {@link MotionEvent#getPointerCount()} directly. This method will be
536      * removed in a future release.
537      */
538     @androidx.annotation.ReplaceWith(expression = "event.getPointerCount()")
539     @Deprecated
getPointerCount(MotionEvent event)540     public static int getPointerCount(MotionEvent event) {
541         return event.getPointerCount();
542     }
543 
544     /**
545      * Gets the source of the event.
546      *
547      * @return The event source or {@link InputDeviceCompat#SOURCE_UNKNOWN} if unknown.
548      * @deprecated Call {@link MotionEvent#getSource()} directly. This method will be
549      * removed in a future release.
550      */
551     @androidx.annotation.ReplaceWith(expression = "event.getSource()")
552     @Deprecated
getSource(MotionEvent event)553     public static int getSource(MotionEvent event) {
554         return event.getSource();
555     }
556 
557     /**
558      * Determines whether the event is from the given source.
559      * @param event motion event for which to check the source.
560      * @param source The input source to check against.
561      * @return Whether the event is from the given source.
562      */
isFromSource(@onNull MotionEvent event, int source)563     public static boolean isFromSource(@NonNull MotionEvent event, int source) {
564         return (event.getSource() & source) == source;
565     }
566 
567     /**
568      * Get axis value for the first pointer index (may be an
569      * arbitrary pointer identifier).
570      *
571      * @param event motion event for which to get the value.
572      * @param axis The axis identifier for the axis value to retrieve.
573      *
574      * @see #AXIS_X
575      * @see #AXIS_Y
576      *
577      * @deprecated Call {@link MotionEvent#getAxisValue(int)} directly. This method will be
578      * removed in a future release.
579      */
580     @androidx.annotation.ReplaceWith(expression = "event.getAxisValue(axis)")
581     @Deprecated
getAxisValue(MotionEvent event, int axis)582     public static float getAxisValue(MotionEvent event, int axis) {
583         return event.getAxisValue(axis);
584     }
585 
586     /**
587      * Returns the value of the requested axis for the given pointer <em>index</em>
588      * (use {@link #getPointerId(MotionEvent, int)} to find the pointer identifier for this index).
589      *
590      * @param event motion event for which to get the value.
591      * @param axis The axis identifier for the axis value to retrieve.
592      * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
593      * (the first pointer that is down) to {@link #getPointerCount(MotionEvent)}-1.
594      * @return The value of the axis, or 0 if the axis is not available.
595      *
596      * @see #AXIS_X
597      * @see #AXIS_Y
598      *
599      * @deprecated Call {@link MotionEvent#getAxisValue(int, int)} directly. This method will be
600      * removed in a future release.
601      */
602     @androidx.annotation.ReplaceWith(expression = "event.getAxisValue(axis, pointerIndex)")
603     @Deprecated
getAxisValue(MotionEvent event, int axis, int pointerIndex)604     public static float getAxisValue(MotionEvent event, int axis, int pointerIndex) {
605         return event.getAxisValue(axis, pointerIndex);
606     }
607 
608     /**
609      * @deprecated Call {@link MotionEvent#getButtonState()} directly. This method will be
610      * removed in a future release.
611      */
612     @androidx.annotation.ReplaceWith(expression = "event.getButtonState()")
613     @Deprecated
getButtonState(MotionEvent event)614     public static int getButtonState(MotionEvent event) {
615         return event.getButtonState();
616     }
617 
MotionEventCompat()618     private MotionEventCompat() {}
619 }
620