MotionEventCompat.java revision 17d15d92db2288bd27b8710c68e5bc1b9b5945f0
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 float getAxisValue(MotionEvent event, int axis); 32 float getAxisValue(MotionEvent event, int axis, int pointerIndex); 33 int getButtonState(MotionEvent event); 34 } 35 36 /** 37 * Interface implementation that doesn't use anything about v4 APIs. 38 */ 39 static class BaseMotionEventVersionImpl implements MotionEventVersionImpl { 40 @Override 41 public float getAxisValue(MotionEvent event, int axis) { 42 return 0; 43 } 44 45 @Override 46 public float getAxisValue(MotionEvent event, int axis, int pointerIndex) { 47 return 0; 48 } 49 50 @Override 51 public int getButtonState(MotionEvent event) { 52 return 0; 53 } 54 } 55 56 /** 57 * Interface implementation for devices with at least v12 APIs. 58 */ 59 static class HoneycombMr1MotionEventVersionImpl extends BaseMotionEventVersionImpl { 60 61 @Override 62 public float getAxisValue(MotionEvent event, int axis) { 63 return MotionEventCompatHoneycombMr1.getAxisValue(event, axis); 64 } 65 66 @Override 67 public float getAxisValue(MotionEvent event, int axis, int pointerIndex) { 68 return MotionEventCompatHoneycombMr1.getAxisValue(event, axis, pointerIndex); 69 } 70 } 71 72 73 /** 74 * Interface implementation for devices with at least v14 APIs. 75 */ 76 private static class ICSMotionEventVersionImpl extends HoneycombMr1MotionEventVersionImpl { 77 @Override 78 public int getButtonState(MotionEvent event) { 79 return MotionEventCompatICS.getButtonState(event); 80 } 81 } 82 83 /** 84 * Select the correct implementation to use for the current platform. 85 */ 86 static final MotionEventVersionImpl IMPL; 87 static { 88 if (Build.VERSION.SDK_INT >= 14) { 89 IMPL = new ICSMotionEventVersionImpl(); 90 } else if (Build.VERSION.SDK_INT >= 12) { 91 IMPL = new HoneycombMr1MotionEventVersionImpl(); 92 } else { 93 IMPL = new BaseMotionEventVersionImpl(); 94 } 95 } 96 97 // ------------------------------------------------------------------- 98 99 /** 100 * Synonym for {@link MotionEvent#ACTION_MASK}. 101 */ 102 public static final int ACTION_MASK = 0xff; 103 104 /** 105 * Synonym for {@link MotionEvent#ACTION_POINTER_DOWN}. 106 */ 107 public static final int ACTION_POINTER_DOWN = 5; 108 109 /** 110 * Synonym for {@link MotionEvent#ACTION_POINTER_UP}. 111 */ 112 public static final int ACTION_POINTER_UP = 6; 113 114 /** 115 * Synonym for {@link MotionEvent#ACTION_HOVER_MOVE}. 116 */ 117 public static final int ACTION_HOVER_MOVE = 7; 118 119 /** 120 * Synonym for {@link MotionEvent#ACTION_SCROLL}. 121 */ 122 public static final int ACTION_SCROLL = 8; 123 124 /** 125 * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_MASK}. 126 */ 127 public static final int ACTION_POINTER_INDEX_MASK = 0xff00; 128 129 /** 130 * Synonym for {@link MotionEvent#ACTION_POINTER_INDEX_SHIFT}. 131 */ 132 public static final int ACTION_POINTER_INDEX_SHIFT = 8; 133 134 /** 135 * Synonym for {@link MotionEvent#ACTION_HOVER_ENTER}. 136 */ 137 public static final int ACTION_HOVER_ENTER = 9; 138 139 /** 140 * Synonym for {@link MotionEvent#ACTION_HOVER_EXIT}. 141 */ 142 public static final int ACTION_HOVER_EXIT = 10; 143 144 /** 145 * Synonym for {@link MotionEvent#AXIS_X}. 146 */ 147 public static final int AXIS_X = 0; 148 149 /** 150 * Synonym for {@link MotionEvent#AXIS_Y}. 151 */ 152 public static final int AXIS_Y = 1; 153 154 /** 155 * Synonym for {@link MotionEvent#AXIS_PRESSURE}. 156 */ 157 public static final int AXIS_PRESSURE = 2; 158 159 /** 160 * Synonym for {@link MotionEvent#AXIS_SIZE}. 161 */ 162 public static final int AXIS_SIZE = 3; 163 164 /** 165 * Synonym for {@link MotionEvent#AXIS_TOUCH_MAJOR}. 166 */ 167 public static final int AXIS_TOUCH_MAJOR = 4; 168 169 /** 170 * Synonym for {@link MotionEvent#AXIS_TOUCH_MINOR}. 171 */ 172 public static final int AXIS_TOUCH_MINOR = 5; 173 174 /** 175 * Synonym for {@link MotionEvent#AXIS_TOOL_MAJOR}. 176 */ 177 public static final int AXIS_TOOL_MAJOR = 6; 178 179 /** 180 * Synonym for {@link MotionEvent#AXIS_TOOL_MINOR}. 181 */ 182 public static final int AXIS_TOOL_MINOR = 7; 183 184 /** 185 * Synonym for {@link MotionEvent#AXIS_ORIENTATION}. 186 */ 187 public static final int AXIS_ORIENTATION = 8; 188 189 /** 190 * Synonym for {@link MotionEvent#AXIS_VSCROLL}. 191 */ 192 public static final int AXIS_VSCROLL = 9; 193 194 /** 195 * Synonym for {@link MotionEvent#AXIS_HSCROLL}. 196 */ 197 public static final int AXIS_HSCROLL = 10; 198 199 /** 200 * Synonym for {@link MotionEvent#AXIS_Z}. 201 */ 202 public static final int AXIS_Z = 11; 203 204 /** 205 * Synonym for {@link MotionEvent#AXIS_RX}. 206 */ 207 public static final int AXIS_RX = 12; 208 209 /** 210 * Synonym for {@link MotionEvent#AXIS_RY}. 211 */ 212 public static final int AXIS_RY = 13; 213 214 /** 215 * Synonym for {@link MotionEvent#AXIS_RZ}. 216 */ 217 public static final int AXIS_RZ = 14; 218 219 /** 220 * Synonym for {@link MotionEvent#AXIS_HAT_X}. 221 */ 222 public static final int AXIS_HAT_X = 15; 223 224 /** 225 * Synonym for {@link MotionEvent#AXIS_HAT_Y}. 226 */ 227 public static final int AXIS_HAT_Y = 16; 228 229 /** 230 * Synonym for {@link MotionEvent#AXIS_LTRIGGER}. 231 */ 232 public static final int AXIS_LTRIGGER = 17; 233 234 /** 235 * Synonym for {@link MotionEvent#AXIS_RTRIGGER}. 236 */ 237 public static final int AXIS_RTRIGGER = 18; 238 239 /** 240 * Synonym for {@link MotionEvent#AXIS_THROTTLE}. 241 */ 242 public static final int AXIS_THROTTLE = 19; 243 244 /** 245 * Synonym for {@link MotionEvent#AXIS_RUDDER}. 246 */ 247 public static final int AXIS_RUDDER = 20; 248 249 /** 250 * Synonym for {@link MotionEvent#AXIS_WHEEL}. 251 */ 252 public static final int AXIS_WHEEL = 21; 253 254 /** 255 * Synonym for {@link MotionEvent#AXIS_GAS}. 256 */ 257 public static final int AXIS_GAS = 22; 258 259 /** 260 * Synonym for {@link MotionEvent#AXIS_BRAKE}. 261 */ 262 public static final int AXIS_BRAKE = 23; 263 264 /** 265 * Synonym for {@link MotionEvent#AXIS_DISTANCE}. 266 */ 267 public static final int AXIS_DISTANCE = 24; 268 269 /** 270 * Synonym for {@link MotionEvent#AXIS_TILT}. 271 */ 272 public static final int AXIS_TILT = 25; 273 274 /** 275 * Synonym for {@link MotionEvent#AXIS_RELATIVE_X}. 276 */ 277 public static final int AXIS_RELATIVE_X = 27; 278 279 /** 280 * Synonym for {@link MotionEvent#AXIS_RELATIVE_Y}. 281 */ 282 public static final int AXIS_RELATIVE_Y = 28; 283 284 /** 285 * Synonym for {@link MotionEvent#AXIS_GENERIC_1}. 286 */ 287 public static final int AXIS_GENERIC_1 = 32; 288 289 /** 290 * Synonym for {@link MotionEvent#AXIS_GENERIC_2}. 291 */ 292 public static final int AXIS_GENERIC_2 = 33; 293 294 /** 295 * Synonym for {@link MotionEvent#AXIS_GENERIC_3}. 296 */ 297 public static final int AXIS_GENERIC_3 = 34; 298 299 /** 300 * Synonym for {@link MotionEvent#AXIS_GENERIC_4}. 301 */ 302 public static final int AXIS_GENERIC_4 = 35; 303 304 /** 305 * Synonym for {@link MotionEvent#AXIS_GENERIC_5}. 306 */ 307 public static final int AXIS_GENERIC_5 = 36; 308 309 /** 310 * Synonym for {@link MotionEvent#AXIS_GENERIC_6}. 311 */ 312 public static final int AXIS_GENERIC_6 = 37; 313 314 /** 315 * Synonym for {@link MotionEvent#AXIS_GENERIC_7}. 316 */ 317 public static final int AXIS_GENERIC_7 = 38; 318 319 /** 320 * Synonym for {@link MotionEvent#AXIS_GENERIC_8}. 321 */ 322 public static final int AXIS_GENERIC_8 = 39; 323 324 /** 325 * Synonym for {@link MotionEvent#AXIS_GENERIC_9}. 326 */ 327 public static final int AXIS_GENERIC_9 = 40; 328 329 /** 330 * Synonym for {@link MotionEvent#AXIS_GENERIC_10}. 331 */ 332 public static final int AXIS_GENERIC_10 = 41; 333 334 /** 335 * Synonym for {@link MotionEvent#AXIS_GENERIC_11}. 336 */ 337 public static final int AXIS_GENERIC_11 = 42; 338 339 /** 340 * Synonym for {@link MotionEvent#AXIS_GENERIC_12}. 341 */ 342 public static final int AXIS_GENERIC_12 = 43; 343 344 /** 345 * Synonym for {@link MotionEvent#AXIS_GENERIC_13}. 346 */ 347 public static final int AXIS_GENERIC_13 = 44; 348 349 /** 350 * Synonym for {@link MotionEvent#AXIS_GENERIC_14}. 351 */ 352 public static final int AXIS_GENERIC_14 = 45; 353 354 /** 355 * Synonym for {@link MotionEvent#AXIS_GENERIC_15}. 356 */ 357 public static final int AXIS_GENERIC_15 = 46; 358 359 /** 360 * Synonym for {@link MotionEvent#AXIS_GENERIC_16}. 361 */ 362 public static final int AXIS_GENERIC_16 = 47; 363 364 /** 365 * Synonym for {@link MotionEvent#BUTTON_PRIMARY}. 366 */ 367 public static final int BUTTON_PRIMARY = 1; 368 369 /** 370 * Call {@link MotionEvent#getAction}, returning only the {@link #ACTION_MASK} 371 * portion. 372 */ 373 public static int getActionMasked(MotionEvent event) { 374 return event.getAction() & ACTION_MASK; 375 } 376 377 /** 378 * Call {@link MotionEvent#getAction}, returning only the pointer index 379 * portion 380 */ 381 public static int getActionIndex(MotionEvent event) { 382 return (event.getAction() & ACTION_POINTER_INDEX_MASK) 383 >> ACTION_POINTER_INDEX_SHIFT; 384 } 385 386 /** 387 * Call {@link MotionEvent#findPointerIndex(int)}. 388 * 389 * @deprecated Call {@link MotionEvent#findPointerIndex(int)} directly. This method will be 390 * removed in a future release. 391 */ 392 @Deprecated 393 public static int findPointerIndex(MotionEvent event, int pointerId) { 394 return event.findPointerIndex(pointerId); 395 } 396 397 /** 398 * Call {@link MotionEvent#getPointerId(int)}. 399 * 400 * @deprecated Call {@link MotionEvent#getPointerId(int)} directly. This method will be 401 * removed in a future release. 402 */ 403 @Deprecated 404 public static int getPointerId(MotionEvent event, int pointerIndex) { 405 return event.getPointerId(pointerIndex); 406 } 407 408 /** 409 * Call {@link MotionEvent#getX(int)}. 410 * 411 * @deprecated Call {@link MotionEvent#getX()} directly. This method will be 412 * removed in a future release. 413 */ 414 @Deprecated 415 public static float getX(MotionEvent event, int pointerIndex) { 416 return event.getX(pointerIndex); 417 } 418 419 /** 420 * Call {@link MotionEvent#getY(int)}. 421 * 422 * @deprecated Call {@link MotionEvent#getY()} directly. This method will be 423 * removed in a future release. 424 */ 425 @Deprecated 426 public static float getY(MotionEvent event, int pointerIndex) { 427 return event.getY(pointerIndex); 428 } 429 430 /** 431 * The number of pointers of data contained in this event. Always 432 * 433 * @deprecated Call {@link MotionEvent#getPointerCount()} directly. This method will be 434 * removed in a future release. 435 */ 436 @Deprecated 437 public static int getPointerCount(MotionEvent event) { 438 return event.getPointerCount(); 439 } 440 441 /** 442 * Gets the source of the event. 443 * 444 * @return The event source or {@link InputDeviceCompat#SOURCE_UNKNOWN} if unknown. 445 * @deprecated Call {@link MotionEvent#getSource()} directly. This method will be 446 * removed in a future release. 447 */ 448 @Deprecated 449 public static int getSource(MotionEvent event) { 450 return event.getSource(); 451 } 452 453 /** 454 * Determines whether the event is from the given source. 455 * @param source The input source to check against. 456 * @return Whether the event is from the given source. 457 */ 458 public static boolean isFromSource(MotionEvent event, int source) { 459 return (event.getSource() & source) == source; 460 } 461 462 /** 463 * Get axis value for the first pointer index (may be an 464 * arbitrary pointer identifier). 465 * 466 * @param axis The axis identifier for the axis value to retrieve. 467 * 468 * @see #AXIS_X 469 * @see #AXIS_Y 470 */ 471 public static float getAxisValue(MotionEvent event, int axis) { 472 return IMPL.getAxisValue(event, axis); 473 } 474 475 /** 476 * Returns the value of the requested axis for the given pointer <em>index</em> 477 * (use {@link #getPointerId(MotionEvent, int)} to find the pointer identifier for this index). 478 * 479 * @param axis The axis identifier for the axis value to retrieve. 480 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 481 * (the first pointer that is down) to {@link #getPointerCount(MotionEvent)}-1. 482 * @return The value of the axis, or 0 if the axis is not available. 483 * 484 * @see #AXIS_X 485 * @see #AXIS_Y 486 */ 487 public static float getAxisValue(MotionEvent event, int axis, int pointerIndex) { 488 return IMPL.getAxisValue(event, axis, pointerIndex); 489 } 490 491 /** 492 * 493 * @param event 494 * @return 495 */ 496 public static int getButtonState(MotionEvent event) { 497 return IMPL.getButtonState(event); 498 } 499 500 private MotionEventCompat() {} 501} 502