MotionEventCompat.java revision eedc67283a5a49dce86c625e54596dfdea9465a7
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.view.MotionEvent;
20
21/**
22 * Helper for accessing newer features in MotionEvent.
23 */
24public class MotionEventCompat {
25    /**
26     * Interface for the full API.
27     */
28    interface MotionEventVersionImpl {
29        public int findPointerIndex(MotionEvent event, int pointerId);
30        public int getPointerId(MotionEvent event, int pointerIndex);
31        public float getX(MotionEvent event, int pointerIndex);
32        public float getY(MotionEvent event, int pointerIndex);
33    }
34
35    /**
36     * Interface implementation that doesn't use anything about v4 APIs.
37     */
38    static class BaseMotionEventVersionImpl implements MotionEventVersionImpl {
39        @Override
40        public int findPointerIndex(MotionEvent event, int pointerId) {
41            return -1;
42        }
43        @Override
44        public int getPointerId(MotionEvent event, int pointerIndex) {
45            throw new IndexOutOfBoundsException("Pre-Eclair does not support pointers");
46        }
47        @Override
48        public float getX(MotionEvent event, int pointerIndex) {
49            throw new IndexOutOfBoundsException("Pre-Eclair does not support pointers");
50        }
51        @Override
52        public float getY(MotionEvent event, int pointerIndex) {
53            throw new IndexOutOfBoundsException("Pre-Eclair does not support pointers");
54        }
55    }
56
57    /**
58     * Interface implementation for devices with at least v11 APIs.
59     */
60    static class EclairMotionEventVersionImpl implements MotionEventVersionImpl {
61        @Override
62        public int findPointerIndex(MotionEvent event, int pointerId) {
63            return MotionEventCompatEclair.findPointerIndex(event, pointerId);
64        }
65        @Override
66        public int getPointerId(MotionEvent event, int pointerIndex) {
67            return MotionEventCompatEclair.getPointerId(event, pointerIndex);
68        }
69        @Override
70        public float getX(MotionEvent event, int pointerIndex) {
71            return MotionEventCompatEclair.getX(event, pointerIndex);
72        }
73        @Override
74        public float getY(MotionEvent event, int pointerIndex) {
75            return MotionEventCompatEclair.getY(event, pointerIndex);
76        }
77    }
78
79    /**
80     * Select the correct implementation to use for the current platform.
81     */
82    static final MotionEventVersionImpl IMPL;
83    static {
84        if (android.os.Build.VERSION.SDK_INT >= 5) {
85            IMPL = new EclairMotionEventVersionImpl();
86        } else {
87            IMPL = new BaseMotionEventVersionImpl();
88        }
89    }
90
91    // -------------------------------------------------------------------
92
93    /**
94     * Synonym for {@link MotionEvent#ACTION_MASK}.
95     */
96    public static final int ACTION_MASK = 0xff;
97
98    /**
99     * Synonym for {@link MotionEvent#ACTION_POINTER_DOWN}.
100     */
101    public static final int ACTION_POINTER_DOWN = 5;
102
103    /**
104     * Synonym for {@link MotionEvent#ACTION_POINTER_UP}.
105     */
106    public static final int ACTION_POINTER_UP = 6;
107
108    /**
109     * Synonym for {@link MotionEvent#ACTION_HOVER_MOVE}.
110     */
111    public static final int ACTION_HOVER_MOVE = 7;
112
113    /**
114     * Synonym for {@link MotionEvent#ACTION_SCROLL}.
115     */
116    public static final int ACTION_SCROLL = 8;
117
118    /**
119     * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_MASK}.
120     */
121    public static final int ACTION_POINTER_INDEX_MASK  = 0xff00;
122
123    /**
124     * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_SHIFT}.
125     */
126    public static final int ACTION_POINTER_INDEX_SHIFT = 8;
127
128    /**
129     * Call {@link MotionEvent#getAction}, returning only the {@link #ACTION_MASK}
130     * portion.
131     */
132    public static int getActionMasked(MotionEvent event) {
133        return event.getAction() & ACTION_MASK;
134    }
135
136    /**
137     * Call {@link MotionEvent#getAction}, returning only the pointer index
138     * portion
139     */
140    public static int getActionIndex(MotionEvent event) {
141        return (event.getAction() & ACTION_POINTER_INDEX_MASK)
142                >> ACTION_POINTER_INDEX_SHIFT;
143    }
144
145    /**
146     * Call {@link MotionEvent#findPointerIndex(int)}.
147     * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device,
148     * does nothing and returns -1.
149     */
150    public static int findPointerIndex(MotionEvent event, int pointerId) {
151        return IMPL.findPointerIndex(event, pointerId);
152    }
153
154    /**
155     * Call {@link MotionEvent#getPointerId(int)}.
156     * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device,
157     * {@link IndexOutOfBoundsException} is thrown.
158     */
159    public static int getPointerId(MotionEvent event, int pointerIndex) {
160        return IMPL.getPointerId(event, pointerIndex);
161    }
162
163    /**
164     * Call {@link MotionEvent#getX(int)}.
165     * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device,
166     * {@link IndexOutOfBoundsException} is thrown.
167     */
168    public static float getX(MotionEvent event, int pointerIndex) {
169        return IMPL.getX(event, pointerIndex);
170    }
171
172    /**
173     * Call {@link MotionEvent#getY(int)}.
174     * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device,
175     * {@link IndexOutOfBoundsException} is thrown.
176     */
177    public static float getY(MotionEvent event, int pointerIndex) {
178        return IMPL.getY(event, pointerIndex);
179    }
180}
181