MotionEventCompat.java revision 2196f53d080334b4e5bb5c25fba1e40578f3588b
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v4.view;
18
19import android.os.Build;
20import android.view.MotionEvent;
21
22/**
23 * Helper for accessing features in {@link MotionEvent} introduced
24 * after API level 4 in a backwards compatible fashion.
25 */
26public final class MotionEventCompat {
27    /**
28     * Interface for the full API.
29     */
30    interface MotionEventVersionImpl {
31        public int findPointerIndex(MotionEvent event, int pointerId);
32        public int getPointerId(MotionEvent event, int pointerIndex);
33        public float getX(MotionEvent event, int pointerIndex);
34        public float getY(MotionEvent event, int pointerIndex);
35        public int getPointerCount(MotionEvent event);
36        public int getSource(MotionEvent event);
37        float getAxisValue(MotionEvent event, int axis);
38        float getAxisValue(MotionEvent event, int axis, int pointerIndex);
39        int getButtonState(MotionEvent event);
40    }
41
42    /**
43     * Interface implementation that doesn't use anything about v4 APIs.
44     */
45    static class BaseMotionEventVersionImpl implements MotionEventVersionImpl {
46        @Override
47        public int findPointerIndex(MotionEvent event, int pointerId) {
48            if (pointerId == 0) {
49                // id 0 == index 0 and vice versa.
50                return 0;
51            }
52            return -1;
53        }
54        @Override
55        public int getPointerId(MotionEvent event, int pointerIndex) {
56            if (pointerIndex == 0) {
57                // index 0 == id 0 and vice versa.
58                return 0;
59            }
60            throw new IndexOutOfBoundsException("Pre-Eclair does not support multiple pointers");
61        }
62        @Override
63        public float getX(MotionEvent event, int pointerIndex) {
64            if (pointerIndex == 0) {
65                return event.getX();
66            }
67            throw new IndexOutOfBoundsException("Pre-Eclair does not support multiple pointers");
68        }
69        @Override
70        public float getY(MotionEvent event, int pointerIndex) {
71            if (pointerIndex == 0) {
72                return event.getY();
73            }
74            throw new IndexOutOfBoundsException("Pre-Eclair does not support multiple pointers");
75        }
76        @Override
77        public int getPointerCount(MotionEvent event) {
78            return 1;
79        }
80
81        @Override
82        public int getSource(MotionEvent event) {
83            return InputDeviceCompat.SOURCE_UNKNOWN;
84        }
85
86        @Override
87        public float getAxisValue(MotionEvent event, int axis) {
88            return 0;
89        }
90
91        @Override
92        public float getAxisValue(MotionEvent event, int axis, int pointerIndex) {
93            return 0;
94        }
95
96        @Override
97        public int getButtonState(MotionEvent event) {
98            return 0;
99        }
100    }
101
102    /**
103     * Interface implementation for devices with at least v5 APIs.
104     */
105    static class EclairMotionEventVersionImpl extends BaseMotionEventVersionImpl {
106        @Override
107        public int findPointerIndex(MotionEvent event, int pointerId) {
108            return MotionEventCompatEclair.findPointerIndex(event, pointerId);
109        }
110        @Override
111        public int getPointerId(MotionEvent event, int pointerIndex) {
112            return MotionEventCompatEclair.getPointerId(event, pointerIndex);
113        }
114        @Override
115        public float getX(MotionEvent event, int pointerIndex) {
116            return MotionEventCompatEclair.getX(event, pointerIndex);
117        }
118        @Override
119        public float getY(MotionEvent event, int pointerIndex) {
120            return MotionEventCompatEclair.getY(event, pointerIndex);
121        }
122        @Override
123        public int getPointerCount(MotionEvent event) {
124            return MotionEventCompatEclair.getPointerCount(event);
125        }
126    }
127
128    /**
129     * Interface implementation for devices with at least v8 APIs.
130     */
131    static class GingerbreadMotionEventVersionImpl extends EclairMotionEventVersionImpl {
132        @Override
133        public int getSource(MotionEvent event) {
134            return MotionEventCompatGingerbread.getSource(event);
135        }
136    }
137
138    /**
139     * Interface implementation for devices with at least v12 APIs.
140     */
141    static class HoneycombMr1MotionEventVersionImpl extends GingerbreadMotionEventVersionImpl {
142
143        @Override
144        public float getAxisValue(MotionEvent event, int axis) {
145            return MotionEventCompatHoneycombMr1.getAxisValue(event, axis);
146        }
147
148        @Override
149        public float getAxisValue(MotionEvent event, int axis, int pointerIndex) {
150            return MotionEventCompatHoneycombMr1.getAxisValue(event, axis, pointerIndex);
151        }
152    }
153
154
155    /**
156     * Interface implementation for devices with at least v14 APIs.
157     */
158    private static class ICSMotionEventVersionImpl extends GingerbreadMotionEventVersionImpl {
159        @Override
160        public int getButtonState(MotionEvent event) {
161            return MotionEventCompatICS.getButtonState(event);
162        }
163    }
164
165    /**
166     * Select the correct implementation to use for the current platform.
167     */
168    static final MotionEventVersionImpl IMPL;
169    static {
170        if (Build.VERSION.SDK_INT >= 14) {
171            IMPL = new ICSMotionEventVersionImpl();
172        } else if (Build.VERSION.SDK_INT >= 12) {
173            IMPL = new HoneycombMr1MotionEventVersionImpl();
174        } else if (Build.VERSION.SDK_INT >= 9) {
175            IMPL = new GingerbreadMotionEventVersionImpl();
176        } else if (Build.VERSION.SDK_INT >= 5) {
177            IMPL = new EclairMotionEventVersionImpl();
178        } else {
179            IMPL = new BaseMotionEventVersionImpl();
180        }
181    }
182
183    // -------------------------------------------------------------------
184
185    /**
186     * Synonym for {@link MotionEvent#ACTION_MASK}.
187     */
188    public static final int ACTION_MASK = 0xff;
189
190    /**
191     * Synonym for {@link MotionEvent#ACTION_POINTER_DOWN}.
192     */
193    public static final int ACTION_POINTER_DOWN = 5;
194
195    /**
196     * Synonym for {@link MotionEvent#ACTION_POINTER_UP}.
197     */
198    public static final int ACTION_POINTER_UP = 6;
199
200    /**
201     * Synonym for {@link MotionEvent#ACTION_HOVER_MOVE}.
202     */
203    public static final int ACTION_HOVER_MOVE = 7;
204
205    /**
206     * Synonym for {@link MotionEvent#ACTION_SCROLL}.
207     */
208    public static final int ACTION_SCROLL = 8;
209
210    /**
211     * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_MASK}.
212     */
213    public static final int ACTION_POINTER_INDEX_MASK  = 0xff00;
214
215    /**
216     * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_SHIFT}.
217     */
218    public static final int ACTION_POINTER_INDEX_SHIFT = 8;
219
220    /**
221     * Synonym for {@link MotionEvent#ACTION_HOVER_ENTER}.
222     */
223    public static final int ACTION_HOVER_ENTER = 9;
224
225    /**
226     * Synonym for {@link MotionEvent#ACTION_HOVER_EXIT}.
227     */
228    public static final int ACTION_HOVER_EXIT = 10;
229
230    /**
231     * Synonym for {@link MotionEvent#AXIS_X}.
232     */
233    public static final int AXIS_X = 0;
234
235    /**
236     * Synonym for {@link MotionEvent#AXIS_Y}.
237     */
238    public static final int AXIS_Y = 1;
239
240    /**
241     * Synonym for {@link MotionEvent#AXIS_PRESSURE}.
242     */
243    public static final int AXIS_PRESSURE = 2;
244
245    /**
246     * Synonym for {@link MotionEvent#AXIS_SIZE}.
247     */
248    public static final int AXIS_SIZE = 3;
249
250    /**
251     * Synonym for {@link MotionEvent#AXIS_TOUCH_MAJOR}.
252     */
253    public static final int AXIS_TOUCH_MAJOR = 4;
254
255    /**
256     * Synonym for {@link MotionEvent#AXIS_TOUCH_MINOR}.
257     */
258    public static final int AXIS_TOUCH_MINOR = 5;
259
260    /**
261     * Synonym for {@link MotionEvent#AXIS_TOOL_MAJOR}.
262     */
263    public static final int AXIS_TOOL_MAJOR = 6;
264
265    /**
266     * Synonym for {@link MotionEvent#AXIS_TOOL_MINOR}.
267     */
268    public static final int AXIS_TOOL_MINOR = 7;
269
270    /**
271     * Synonym for {@link MotionEvent#AXIS_ORIENTATION}.
272     */
273    public static final int AXIS_ORIENTATION = 8;
274
275    /**
276     * Synonym for {@link MotionEvent#AXIS_VSCROLL}.
277     */
278    public static final int AXIS_VSCROLL = 9;
279
280    /**
281     * Synonym for {@link MotionEvent#AXIS_HSCROLL}.
282     */
283    public static final int AXIS_HSCROLL = 10;
284
285    /**
286     * Synonym for {@link MotionEvent#AXIS_Z}.
287     */
288    public static final int AXIS_Z = 11;
289
290    /**
291     * Synonym for {@link MotionEvent#AXIS_RX}.
292     */
293    public static final int AXIS_RX = 12;
294
295    /**
296     * Synonym for {@link MotionEvent#AXIS_RY}.
297     */
298    public static final int AXIS_RY = 13;
299
300    /**
301     * Synonym for {@link MotionEvent#AXIS_RZ}.
302     */
303    public static final int AXIS_RZ = 14;
304
305    /**
306     * Synonym for {@link MotionEvent#AXIS_HAT_X}.
307     */
308    public static final int AXIS_HAT_X = 15;
309
310    /**
311     * Synonym for {@link MotionEvent#AXIS_HAT_Y}.
312     */
313    public static final int AXIS_HAT_Y = 16;
314
315    /**
316     * Synonym for {@link MotionEvent#AXIS_LTRIGGER}.
317     */
318    public static final int AXIS_LTRIGGER = 17;
319
320    /**
321     * Synonym for {@link MotionEvent#AXIS_RTRIGGER}.
322     */
323    public static final int AXIS_RTRIGGER = 18;
324
325    /**
326     * Synonym for {@link MotionEvent#AXIS_THROTTLE}.
327     */
328    public static final int AXIS_THROTTLE = 19;
329
330    /**
331     * Synonym for {@link MotionEvent#AXIS_RUDDER}.
332     */
333    public static final int AXIS_RUDDER = 20;
334
335    /**
336     * Synonym for {@link MotionEvent#AXIS_WHEEL}.
337     */
338    public static final int AXIS_WHEEL = 21;
339
340    /**
341     * Synonym for {@link MotionEvent#AXIS_GAS}.
342     */
343    public static final int AXIS_GAS = 22;
344
345    /**
346     * Synonym for {@link MotionEvent#AXIS_BRAKE}.
347     */
348    public static final int AXIS_BRAKE = 23;
349
350    /**
351     * Synonym for {@link MotionEvent#AXIS_DISTANCE}.
352     */
353    public static final int AXIS_DISTANCE = 24;
354
355    /**
356     * Synonym for {@link MotionEvent#AXIS_TILT}.
357     */
358    public static final int AXIS_TILT = 25;
359
360    /**
361     * Synonym for {@link MotionEvent#AXIS_GENERIC_1}.
362     */
363    public static final int AXIS_GENERIC_1 = 32;
364
365    /**
366     * Synonym for {@link MotionEvent#AXIS_GENERIC_2}.
367     */
368    public static final int AXIS_GENERIC_2 = 33;
369
370    /**
371     * Synonym for {@link MotionEvent#AXIS_GENERIC_3}.
372     */
373    public static final int AXIS_GENERIC_3 = 34;
374
375    /**
376     * Synonym for {@link MotionEvent#AXIS_GENERIC_4}.
377     */
378    public static final int AXIS_GENERIC_4 = 35;
379
380    /**
381     * Synonym for {@link MotionEvent#AXIS_GENERIC_5}.
382     */
383    public static final int AXIS_GENERIC_5 = 36;
384
385    /**
386     * Synonym for {@link MotionEvent#AXIS_GENERIC_6}.
387     */
388    public static final int AXIS_GENERIC_6 = 37;
389
390    /**
391     * Synonym for {@link MotionEvent#AXIS_GENERIC_7}.
392     */
393    public static final int AXIS_GENERIC_7 = 38;
394
395    /**
396     * Synonym for {@link MotionEvent#AXIS_GENERIC_8}.
397     */
398    public static final int AXIS_GENERIC_8 = 39;
399
400    /**
401     * Synonym for {@link MotionEvent#AXIS_GENERIC_9}.
402     */
403    public static final int AXIS_GENERIC_9 = 40;
404
405    /**
406     * Synonym for {@link MotionEvent#AXIS_GENERIC_10}.
407     */
408    public static final int AXIS_GENERIC_10 = 41;
409
410    /**
411     * Synonym for {@link MotionEvent#AXIS_GENERIC_11}.
412     */
413    public static final int AXIS_GENERIC_11 = 42;
414
415    /**
416     * Synonym for {@link MotionEvent#AXIS_GENERIC_12}.
417     */
418    public static final int AXIS_GENERIC_12 = 43;
419
420    /**
421     * Synonym for {@link MotionEvent#AXIS_GENERIC_13}.
422     */
423    public static final int AXIS_GENERIC_13 = 44;
424
425    /**
426     * Synonym for {@link MotionEvent#AXIS_GENERIC_14}.
427     */
428    public static final int AXIS_GENERIC_14 = 45;
429
430    /**
431     * Synonym for {@link MotionEvent#AXIS_GENERIC_15}.
432     */
433    public static final int AXIS_GENERIC_15 = 46;
434
435    /**
436     * Synonym for {@link MotionEvent#AXIS_GENERIC_16}.
437     */
438    public static final int AXIS_GENERIC_16 = 47;
439
440    /**
441     * Synonym for {@link MotionEvent#BUTTON_PRIMARY}.
442     */
443    public static final int BUTTON_PRIMARY = 1;
444
445    /**
446     * Call {@link MotionEvent#getAction}, returning only the {@link #ACTION_MASK}
447     * portion.
448     */
449    public static int getActionMasked(MotionEvent event) {
450        return event.getAction() & ACTION_MASK;
451    }
452
453    /**
454     * Call {@link MotionEvent#getAction}, returning only the pointer index
455     * portion
456     */
457    public static int getActionIndex(MotionEvent event) {
458        return (event.getAction() & ACTION_POINTER_INDEX_MASK)
459                >> ACTION_POINTER_INDEX_SHIFT;
460    }
461
462    /**
463     * Call {@link MotionEvent#findPointerIndex(int)}.
464     * If running on a pre-{@link android.os.Build.VERSION_CODES#ECLAIR} device,
465     * does nothing and returns -1.
466     */
467    public static int findPointerIndex(MotionEvent event, int pointerId) {
468        return IMPL.findPointerIndex(event, pointerId);
469    }
470
471    /**
472     * Call {@link MotionEvent#getPointerId(int)}.
473     * If running on a pre-{@link android.os.Build.VERSION_CODES#ECLAIR} device,
474     * {@link IndexOutOfBoundsException} is thrown.
475     */
476    public static int getPointerId(MotionEvent event, int pointerIndex) {
477        return IMPL.getPointerId(event, pointerIndex);
478    }
479
480    /**
481     * Call {@link MotionEvent#getX(int)}.
482     * If running on a pre-{@link android.os.Build.VERSION_CODES#ECLAIR} device,
483     * {@link IndexOutOfBoundsException} is thrown.
484     */
485    public static float getX(MotionEvent event, int pointerIndex) {
486        return IMPL.getX(event, pointerIndex);
487    }
488
489    /**
490     * Call {@link MotionEvent#getY(int)}.
491     * If running on a pre-{@link android.os.Build.VERSION_CODES#ECLAIR} device,
492     * {@link IndexOutOfBoundsException} is thrown.
493     */
494    public static float getY(MotionEvent event, int pointerIndex) {
495        return IMPL.getY(event, pointerIndex);
496    }
497
498    /**
499     * The number of pointers of data contained in this event.  Always
500     * >= 1.
501     */
502    public static int getPointerCount(MotionEvent event) {
503        return IMPL.getPointerCount(event);
504    }
505
506    /**
507     * Gets the source of the event.
508     *
509     * @return The event source or {@link InputDeviceCompat#SOURCE_UNKNOWN} if unknown.
510     */
511    public static int getSource(MotionEvent event) {
512        return IMPL.getSource(event);
513    }
514
515    /**
516     * Determines whether the event is from the given source.
517     * @param source The input source to check against.
518     * @return Whether the event is from the given source.
519     */
520    public static boolean isFromSource(MotionEvent event, int source) {
521        return (getSource(event) & source) == source;
522    }
523
524    /**
525     * Get axis value for the first pointer index (may be an
526     * arbitrary pointer identifier).
527     *
528     * @param axis The axis identifier for the axis value to retrieve.
529     *
530     * @see #AXIS_X
531     * @see #AXIS_Y
532     */
533    public static float getAxisValue(MotionEvent event, int axis) {
534        return IMPL.getAxisValue(event, axis);
535    }
536
537    /**
538     * Returns the value of the requested axis for the given pointer <em>index</em>
539     * (use {@link #getPointerId(MotionEvent, int)} to find the pointer identifier for this index).
540     *
541     * @param axis The axis identifier for the axis value to retrieve.
542     * @param pointerIndex Raw index of pointer to retrieve.  Value may be from 0
543     * (the first pointer that is down) to {@link #getPointerCount(MotionEvent)}-1.
544     * @return The value of the axis, or 0 if the axis is not available.
545     *
546     * @see #AXIS_X
547     * @see #AXIS_Y
548     */
549    public static float getAxisValue(MotionEvent event, int axis, int pointerIndex) {
550        return IMPL.getAxisValue(event, axis, pointerIndex);
551    }
552
553    /**
554     *
555     * @param event
556     * @return
557     */
558    public static int getButtonState(MotionEvent event) {
559        return IMPL.getButtonState(event);
560    }
561
562    private MotionEventCompat() {}
563}
564