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 android.annotation.TestApi; 20 import android.graphics.Matrix; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.os.SystemClock; 24 import android.util.SparseArray; 25 26 import dalvik.annotation.optimization.CriticalNative; 27 import dalvik.annotation.optimization.FastNative; 28 29 /** 30 * Object used to report movement (mouse, pen, finger, trackball) events. 31 * Motion events may hold either absolute or relative movements and other data, 32 * depending on the type of device. 33 * 34 * <h3>Overview</h3> 35 * <p> 36 * Motion events describe movements in terms of an action code and a set of axis values. 37 * The action code specifies the state change that occurred such as a pointer going 38 * down or up. The axis values describe the position and other movement properties. 39 * </p><p> 40 * For example, when the user first touches the screen, the system delivers a touch 41 * event to the appropriate {@link View} with the action code {@link #ACTION_DOWN} 42 * and a set of axis values that include the X and Y coordinates of the touch and 43 * information about the pressure, size and orientation of the contact area. 44 * </p><p> 45 * Some devices can report multiple movement traces at the same time. Multi-touch 46 * screens emit one movement trace for each finger. The individual fingers or 47 * other objects that generate movement traces are referred to as <em>pointers</em>. 48 * Motion events contain information about all of the pointers that are currently active 49 * even if some of them have not moved since the last event was delivered. 50 * </p><p> 51 * The number of pointers only ever changes by one as individual pointers go up and down, 52 * except when the gesture is canceled. 53 * </p><p> 54 * Each pointer has a unique id that is assigned when it first goes down 55 * (indicated by {@link #ACTION_DOWN} or {@link #ACTION_POINTER_DOWN}). A pointer id 56 * remains valid until the pointer eventually goes up (indicated by {@link #ACTION_UP} 57 * or {@link #ACTION_POINTER_UP}) or when the gesture is canceled (indicated by 58 * {@link #ACTION_CANCEL}). 59 * </p><p> 60 * The MotionEvent class provides many methods to query the position and other properties of 61 * pointers, such as {@link #getX(int)}, {@link #getY(int)}, {@link #getAxisValue}, 62 * {@link #getPointerId(int)}, {@link #getToolType(int)}, and many others. Most of these 63 * methods accept the pointer index as a parameter rather than the pointer id. 64 * The pointer index of each pointer in the event ranges from 0 to one less than the value 65 * returned by {@link #getPointerCount()}. 66 * </p><p> 67 * The order in which individual pointers appear within a motion event is undefined. 68 * Thus the pointer index of a pointer can change from one event to the next but 69 * the pointer id of a pointer is guaranteed to remain constant as long as the pointer 70 * remains active. Use the {@link #getPointerId(int)} method to obtain the 71 * pointer id of a pointer to track it across all subsequent motion events in a gesture. 72 * Then for successive motion events, use the {@link #findPointerIndex(int)} method 73 * to obtain the pointer index for a given pointer id in that motion event. 74 * </p><p> 75 * Mouse and stylus buttons can be retrieved using {@link #getButtonState()}. It is a 76 * good idea to check the button state while handling {@link #ACTION_DOWN} as part 77 * of a touch event. The application may choose to perform some different action 78 * if the touch event starts due to a secondary button click, such as presenting a 79 * context menu. 80 * </p> 81 * 82 * <h3>Batching</h3> 83 * <p> 84 * For efficiency, motion events with {@link #ACTION_MOVE} may batch together 85 * multiple movement samples within a single object. The most current 86 * pointer coordinates are available using {@link #getX(int)} and {@link #getY(int)}. 87 * Earlier coordinates within the batch are accessed using {@link #getHistoricalX(int, int)} 88 * and {@link #getHistoricalY(int, int)}. The coordinates are "historical" only 89 * insofar as they are older than the current coordinates in the batch; however, 90 * they are still distinct from any other coordinates reported in prior motion events. 91 * To process all coordinates in the batch in time order, first consume the historical 92 * coordinates then consume the current coordinates. 93 * </p><p> 94 * Example: Consuming all samples for all pointers in a motion event in time order. 95 * </p><p><pre><code> 96 * void printSamples(MotionEvent ev) { 97 * final int historySize = ev.getHistorySize(); 98 * final int pointerCount = ev.getPointerCount(); 99 * for (int h = 0; h < historySize; h++) { 100 * System.out.printf("At time %d:", ev.getHistoricalEventTime(h)); 101 * for (int p = 0; p < pointerCount; p++) { 102 * System.out.printf(" pointer %d: (%f,%f)", 103 * ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h)); 104 * } 105 * } 106 * System.out.printf("At time %d:", ev.getEventTime()); 107 * for (int p = 0; p < pointerCount; p++) { 108 * System.out.printf(" pointer %d: (%f,%f)", 109 * ev.getPointerId(p), ev.getX(p), ev.getY(p)); 110 * } 111 * } 112 * </code></pre></p> 113 * 114 * <h3>Device Types</h3> 115 * <p> 116 * The interpretation of the contents of a MotionEvent varies significantly depending 117 * on the source class of the device. 118 * </p><p> 119 * On pointing devices with source class {@link InputDevice#SOURCE_CLASS_POINTER} 120 * such as touch screens, the pointer coordinates specify absolute 121 * positions such as view X/Y coordinates. Each complete gesture is represented 122 * by a sequence of motion events with actions that describe pointer state transitions 123 * and movements. A gesture starts with a motion event with {@link #ACTION_DOWN} 124 * that provides the location of the first pointer down. As each additional 125 * pointer that goes down or up, the framework will generate a motion event with 126 * {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} accordingly. 127 * Pointer movements are described by motion events with {@link #ACTION_MOVE}. 128 * Finally, a gesture end either when the final pointer goes up as represented 129 * by a motion event with {@link #ACTION_UP} or when gesture is canceled 130 * with {@link #ACTION_CANCEL}. 131 * </p><p> 132 * Some pointing devices such as mice may support vertical and/or horizontal scrolling. 133 * A scroll event is reported as a generic motion event with {@link #ACTION_SCROLL} that 134 * includes the relative scroll offset in the {@link #AXIS_VSCROLL} and 135 * {@link #AXIS_HSCROLL} axes. See {@link #getAxisValue(int)} for information 136 * about retrieving these additional axes. 137 * </p><p> 138 * On trackball devices with source class {@link InputDevice#SOURCE_CLASS_TRACKBALL}, 139 * the pointer coordinates specify relative movements as X/Y deltas. 140 * A trackball gesture consists of a sequence of movements described by motion 141 * events with {@link #ACTION_MOVE} interspersed with occasional {@link #ACTION_DOWN} 142 * or {@link #ACTION_UP} motion events when the trackball button is pressed or released. 143 * </p><p> 144 * On joystick devices with source class {@link InputDevice#SOURCE_CLASS_JOYSTICK}, 145 * the pointer coordinates specify the absolute position of the joystick axes. 146 * The joystick axis values are normalized to a range of -1.0 to 1.0 where 0.0 corresponds 147 * to the center position. More information about the set of available axes and the 148 * range of motion can be obtained using {@link InputDevice#getMotionRange}. 149 * Some common joystick axes are {@link #AXIS_X}, {@link #AXIS_Y}, 150 * {@link #AXIS_HAT_X}, {@link #AXIS_HAT_Y}, {@link #AXIS_Z} and {@link #AXIS_RZ}. 151 * </p><p> 152 * Refer to {@link InputDevice} for more information about how different kinds of 153 * input devices and sources represent pointer coordinates. 154 * </p> 155 * 156 * <h3>Consistency Guarantees</h3> 157 * <p> 158 * Motion events are always delivered to views as a consistent stream of events. 159 * What constitutes a consistent stream varies depending on the type of device. 160 * For touch events, consistency implies that pointers go down one at a time, 161 * move around as a group and then go up one at a time or are canceled. 162 * </p><p> 163 * While the framework tries to deliver consistent streams of motion events to 164 * views, it cannot guarantee it. Some events may be dropped or modified by 165 * containing views in the application before they are delivered thereby making 166 * the stream of events inconsistent. Views should always be prepared to 167 * handle {@link #ACTION_CANCEL} and should tolerate anomalous 168 * situations such as receiving a new {@link #ACTION_DOWN} without first having 169 * received an {@link #ACTION_UP} for the prior gesture. 170 * </p> 171 */ 172 public final class MotionEvent extends InputEvent implements Parcelable { 173 private static final long NS_PER_MS = 1000000; 174 private static final String LABEL_PREFIX = "AXIS_"; 175 176 /** 177 * An invalid pointer id. 178 * 179 * This value (-1) can be used as a placeholder to indicate that a pointer id 180 * has not been assigned or is not available. It cannot appear as 181 * a pointer id inside a {@link MotionEvent}. 182 */ 183 public static final int INVALID_POINTER_ID = -1; 184 185 /** 186 * Bit mask of the parts of the action code that are the action itself. 187 */ 188 public static final int ACTION_MASK = 0xff; 189 190 /** 191 * Constant for {@link #getActionMasked}: A pressed gesture has started, the 192 * motion contains the initial starting location. 193 * <p> 194 * This is also a good time to check the button state to distinguish 195 * secondary and tertiary button clicks and handle them appropriately. 196 * Use {@link #getButtonState} to retrieve the button state. 197 * </p> 198 */ 199 public static final int ACTION_DOWN = 0; 200 201 /** 202 * Constant for {@link #getActionMasked}: A pressed gesture has finished, the 203 * motion contains the final release location as well as any intermediate 204 * points since the last down or move event. 205 */ 206 public static final int ACTION_UP = 1; 207 208 /** 209 * Constant for {@link #getActionMasked}: A change has happened during a 210 * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). 211 * The motion contains the most recent point, as well as any intermediate 212 * points since the last down or move event. 213 */ 214 public static final int ACTION_MOVE = 2; 215 216 /** 217 * Constant for {@link #getActionMasked}: The current gesture has been aborted. 218 * You will not receive any more points in it. You should treat this as 219 * an up event, but not perform any action that you normally would. 220 */ 221 public static final int ACTION_CANCEL = 3; 222 223 /** 224 * Constant for {@link #getActionMasked}: A movement has happened outside of the 225 * normal bounds of the UI element. This does not provide a full gesture, 226 * but only the initial location of the movement/touch. 227 * <p> 228 * Note: Because the location of any event will be outside the 229 * bounds of the view hierarchy, it will not get dispatched to 230 * any children of a ViewGroup by default. Therefore, 231 * movements with ACTION_OUTSIDE should be handled in either the 232 * root {@link View} or in the appropriate {@link Window.Callback} 233 * (e.g. {@link android.app.Activity} or {@link android.app.Dialog}). 234 * </p> 235 */ 236 public static final int ACTION_OUTSIDE = 4; 237 238 /** 239 * Constant for {@link #getActionMasked}: A non-primary pointer has gone down. 240 * <p> 241 * Use {@link #getActionIndex} to retrieve the index of the pointer that changed. 242 * </p><p> 243 * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the 244 * unmasked action returned by {@link #getAction}. 245 * </p> 246 */ 247 public static final int ACTION_POINTER_DOWN = 5; 248 249 /** 250 * Constant for {@link #getActionMasked}: A non-primary pointer has gone up. 251 * <p> 252 * Use {@link #getActionIndex} to retrieve the index of the pointer that changed. 253 * </p><p> 254 * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the 255 * unmasked action returned by {@link #getAction}. 256 * </p> 257 */ 258 public static final int ACTION_POINTER_UP = 6; 259 260 /** 261 * Constant for {@link #getActionMasked}: A change happened but the pointer 262 * is not down (unlike {@link #ACTION_MOVE}). The motion contains the most 263 * recent point, as well as any intermediate points since the last 264 * hover move event. 265 * <p> 266 * This action is always delivered to the window or view under the pointer. 267 * </p><p> 268 * This action is not a touch event so it is delivered to 269 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 270 * {@link View#onTouchEvent(MotionEvent)}. 271 * </p> 272 */ 273 public static final int ACTION_HOVER_MOVE = 7; 274 275 /** 276 * Constant for {@link #getActionMasked}: The motion event contains relative 277 * vertical and/or horizontal scroll offsets. Use {@link #getAxisValue(int)} 278 * to retrieve the information from {@link #AXIS_VSCROLL} and {@link #AXIS_HSCROLL}. 279 * The pointer may or may not be down when this event is dispatched. 280 * <p> 281 * This action is always delivered to the window or view under the pointer, which 282 * may not be the window or view currently touched. 283 * </p><p> 284 * This action is not a touch event so it is delivered to 285 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 286 * {@link View#onTouchEvent(MotionEvent)}. 287 * </p> 288 */ 289 public static final int ACTION_SCROLL = 8; 290 291 /** 292 * Constant for {@link #getActionMasked}: The pointer is not down but has entered the 293 * boundaries of a window or view. 294 * <p> 295 * This action is always delivered to the window or view under the pointer. 296 * </p><p> 297 * This action is not a touch event so it is delivered to 298 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 299 * {@link View#onTouchEvent(MotionEvent)}. 300 * </p> 301 */ 302 public static final int ACTION_HOVER_ENTER = 9; 303 304 /** 305 * Constant for {@link #getActionMasked}: The pointer is not down but has exited the 306 * boundaries of a window or view. 307 * <p> 308 * This action is always delivered to the window or view that was previously under the pointer. 309 * </p><p> 310 * This action is not a touch event so it is delivered to 311 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 312 * {@link View#onTouchEvent(MotionEvent)}. 313 * </p> 314 */ 315 public static final int ACTION_HOVER_EXIT = 10; 316 317 /** 318 * Constant for {@link #getActionMasked}: A button has been pressed. 319 * 320 * <p> 321 * Use {@link #getActionButton()} to get which button was pressed. 322 * </p><p> 323 * This action is not a touch event so it is delivered to 324 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 325 * {@link View#onTouchEvent(MotionEvent)}. 326 * </p> 327 */ 328 public static final int ACTION_BUTTON_PRESS = 11; 329 330 /** 331 * Constant for {@link #getActionMasked}: A button has been released. 332 * 333 * <p> 334 * Use {@link #getActionButton()} to get which button was released. 335 * </p><p> 336 * This action is not a touch event so it is delivered to 337 * {@link View#onGenericMotionEvent(MotionEvent)} rather than 338 * {@link View#onTouchEvent(MotionEvent)}. 339 * </p> 340 */ 341 public static final int ACTION_BUTTON_RELEASE = 12; 342 343 /** 344 * Bits in the action code that represent a pointer index, used with 345 * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Shifting 346 * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer 347 * index where the data for the pointer going up or down can be found; you can 348 * get its identifier with {@link #getPointerId(int)} and the actual 349 * data with {@link #getX(int)} etc. 350 * 351 * @see #getActionIndex 352 */ 353 public static final int ACTION_POINTER_INDEX_MASK = 0xff00; 354 355 /** 356 * Bit shift for the action bits holding the pointer index as 357 * defined by {@link #ACTION_POINTER_INDEX_MASK}. 358 * 359 * @see #getActionIndex 360 */ 361 public static final int ACTION_POINTER_INDEX_SHIFT = 8; 362 363 /** 364 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 365 * data index associated with {@link #ACTION_POINTER_DOWN}. 366 */ 367 @Deprecated 368 public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000; 369 370 /** 371 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 372 * data index associated with {@link #ACTION_POINTER_DOWN}. 373 */ 374 @Deprecated 375 public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100; 376 377 /** 378 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 379 * data index associated with {@link #ACTION_POINTER_DOWN}. 380 */ 381 @Deprecated 382 public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200; 383 384 /** 385 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 386 * data index associated with {@link #ACTION_POINTER_UP}. 387 */ 388 @Deprecated 389 public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000; 390 391 /** 392 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 393 * data index associated with {@link #ACTION_POINTER_UP}. 394 */ 395 @Deprecated 396 public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100; 397 398 /** 399 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 400 * data index associated with {@link #ACTION_POINTER_UP}. 401 */ 402 @Deprecated 403 public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200; 404 405 /** 406 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match 407 * the actual data contained in these bits. 408 */ 409 @Deprecated 410 public static final int ACTION_POINTER_ID_MASK = 0xff00; 411 412 /** 413 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match 414 * the actual data contained in these bits. 415 */ 416 @Deprecated 417 public static final int ACTION_POINTER_ID_SHIFT = 8; 418 419 /** 420 * This flag indicates that the window that received this motion event is partly 421 * or wholly obscured by another visible window above it. This flag is set to true 422 * even if the event did not directly pass through the obscured area. 423 * A security sensitive application can check this flag to identify situations in which 424 * a malicious application may have covered up part of its content for the purpose 425 * of misleading the user or hijacking touches. An appropriate response might be 426 * to drop the suspect touches or to take additional precautions to confirm the user's 427 * actual intent. 428 */ 429 public static final int FLAG_WINDOW_IS_OBSCURED = 0x1; 430 431 /** 432 * This flag indicates that the window that received this motion event is partly 433 * or wholly obscured by another visible window above it. This flag is set to true 434 * even if the event did not directly pass through the obscured area. 435 * A security sensitive application can check this flag to identify situations in which 436 * a malicious application may have covered up part of its content for the purpose 437 * of misleading the user or hijacking touches. An appropriate response might be 438 * to drop the suspect touches or to take additional precautions to confirm the user's 439 * actual intent. 440 * 441 * Unlike FLAG_WINDOW_IS_OBSCURED, this is actually true. 442 * @hide 443 */ 444 public static final int FLAG_WINDOW_IS_PARTIALLY_OBSCURED = 0x2; 445 446 /** 447 * This private flag is only set on {@link #ACTION_HOVER_MOVE} events and indicates that 448 * this event will be immediately followed by a {@link #ACTION_HOVER_EXIT}. It is used to 449 * prevent generating redundant {@link #ACTION_HOVER_ENTER} events. 450 * @hide 451 */ 452 public static final int FLAG_HOVER_EXIT_PENDING = 0x4; 453 454 /** 455 * This flag indicates that the event has been generated by a gesture generator. It 456 * provides a hint to the GestureDector to not apply any touch slop. 457 * 458 * @hide 459 */ 460 public static final int FLAG_IS_GENERATED_GESTURE = 0x8; 461 462 /** 463 * Private flag that indicates when the system has detected that this motion event 464 * may be inconsistent with respect to the sequence of previously delivered motion events, 465 * such as when a pointer move event is sent but the pointer is not down. 466 * 467 * @hide 468 * @see #isTainted 469 * @see #setTainted 470 */ 471 public static final int FLAG_TAINTED = 0x80000000; 472 473 /** 474 * Private flag indicating that this event was synthesized by the system and 475 * should be delivered to the accessibility focused view first. When being 476 * dispatched such an event is not handled by predecessors of the accessibility 477 * focused view and after the event reaches that view the flag is cleared and 478 * normal event dispatch is performed. This ensures that the platform can click 479 * on any view that has accessibility focus which is semantically equivalent to 480 * asking the view to perform a click accessibility action but more generic as 481 * views not implementing click action correctly can still be activated. 482 * 483 * @hide 484 * @see #isTargetAccessibilityFocus() 485 * @see #setTargetAccessibilityFocus(boolean) 486 */ 487 public static final int FLAG_TARGET_ACCESSIBILITY_FOCUS = 0x40000000; 488 489 490 /** 491 * Flag indicating the motion event intersected the top edge of the screen. 492 */ 493 public static final int EDGE_TOP = 0x00000001; 494 495 /** 496 * Flag indicating the motion event intersected the bottom edge of the screen. 497 */ 498 public static final int EDGE_BOTTOM = 0x00000002; 499 500 /** 501 * Flag indicating the motion event intersected the left edge of the screen. 502 */ 503 public static final int EDGE_LEFT = 0x00000004; 504 505 /** 506 * Flag indicating the motion event intersected the right edge of the screen. 507 */ 508 public static final int EDGE_RIGHT = 0x00000008; 509 510 /** 511 * Axis constant: X axis of a motion event. 512 * <p> 513 * <ul> 514 * <li>For a touch screen, reports the absolute X screen position of the center of 515 * the touch contact area. The units are display pixels. 516 * <li>For a touch pad, reports the absolute X surface position of the center of the touch 517 * contact area. The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 518 * to query the effective range of values. 519 * <li>For a mouse, reports the absolute X screen position of the mouse pointer. 520 * The units are display pixels. 521 * <li>For a trackball, reports the relative horizontal displacement of the trackball. 522 * The value is normalized to a range from -1.0 (left) to 1.0 (right). 523 * <li>For a joystick, reports the absolute X position of the joystick. 524 * The value is normalized to a range from -1.0 (left) to 1.0 (right). 525 * </ul> 526 * </p> 527 * 528 * @see #getX(int) 529 * @see #getHistoricalX(int, int) 530 * @see MotionEvent.PointerCoords#x 531 * @see InputDevice#getMotionRange 532 */ 533 public static final int AXIS_X = 0; 534 535 /** 536 * Axis constant: Y axis of a motion event. 537 * <p> 538 * <ul> 539 * <li>For a touch screen, reports the absolute Y screen position of the center of 540 * the touch contact area. The units are display pixels. 541 * <li>For a touch pad, reports the absolute Y surface position of the center of the touch 542 * contact area. The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 543 * to query the effective range of values. 544 * <li>For a mouse, reports the absolute Y screen position of the mouse pointer. 545 * The units are display pixels. 546 * <li>For a trackball, reports the relative vertical displacement of the trackball. 547 * The value is normalized to a range from -1.0 (up) to 1.0 (down). 548 * <li>For a joystick, reports the absolute Y position of the joystick. 549 * The value is normalized to a range from -1.0 (up or far) to 1.0 (down or near). 550 * </ul> 551 * </p> 552 * 553 * @see #getY(int) 554 * @see #getHistoricalY(int, int) 555 * @see MotionEvent.PointerCoords#y 556 * @see InputDevice#getMotionRange 557 */ 558 public static final int AXIS_Y = 1; 559 560 /** 561 * Axis constant: Pressure axis of a motion event. 562 * <p> 563 * <ul> 564 * <li>For a touch screen or touch pad, reports the approximate pressure applied to the surface 565 * by a finger or other tool. The value is normalized to a range from 566 * 0 (no pressure at all) to 1 (normal pressure), although values higher than 1 567 * may be generated depending on the calibration of the input device. 568 * <li>For a trackball, the value is set to 1 if the trackball button is pressed 569 * or 0 otherwise. 570 * <li>For a mouse, the value is set to 1 if the primary mouse button is pressed 571 * or 0 otherwise. 572 * </ul> 573 * </p> 574 * 575 * @see #getPressure(int) 576 * @see #getHistoricalPressure(int, int) 577 * @see MotionEvent.PointerCoords#pressure 578 * @see InputDevice#getMotionRange 579 */ 580 public static final int AXIS_PRESSURE = 2; 581 582 /** 583 * Axis constant: Size axis of a motion event. 584 * <p> 585 * <ul> 586 * <li>For a touch screen or touch pad, reports the approximate size of the contact area in 587 * relation to the maximum detectable size for the device. The value is normalized 588 * to a range from 0 (smallest detectable size) to 1 (largest detectable size), 589 * although it is not a linear scale. This value is of limited use. 590 * To obtain calibrated size information, use 591 * {@link #AXIS_TOUCH_MAJOR} or {@link #AXIS_TOOL_MAJOR}. 592 * </ul> 593 * </p> 594 * 595 * @see #getSize(int) 596 * @see #getHistoricalSize(int, int) 597 * @see MotionEvent.PointerCoords#size 598 * @see InputDevice#getMotionRange 599 */ 600 public static final int AXIS_SIZE = 3; 601 602 /** 603 * Axis constant: TouchMajor axis of a motion event. 604 * <p> 605 * <ul> 606 * <li>For a touch screen, reports the length of the major axis of an ellipse that 607 * represents the touch area at the point of contact. 608 * The units are display pixels. 609 * <li>For a touch pad, reports the length of the major axis of an ellipse that 610 * represents the touch area at the point of contact. 611 * The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 612 * to query the effective range of values. 613 * </ul> 614 * </p> 615 * 616 * @see #getTouchMajor(int) 617 * @see #getHistoricalTouchMajor(int, int) 618 * @see MotionEvent.PointerCoords#touchMajor 619 * @see InputDevice#getMotionRange 620 */ 621 public static final int AXIS_TOUCH_MAJOR = 4; 622 623 /** 624 * Axis constant: TouchMinor axis of a motion event. 625 * <p> 626 * <ul> 627 * <li>For a touch screen, reports the length of the minor axis of an ellipse that 628 * represents the touch area at the point of contact. 629 * The units are display pixels. 630 * <li>For a touch pad, reports the length of the minor axis of an ellipse that 631 * represents the touch area at the point of contact. 632 * The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 633 * to query the effective range of values. 634 * </ul> 635 * </p><p> 636 * When the touch is circular, the major and minor axis lengths will be equal to one another. 637 * </p> 638 * 639 * @see #getTouchMinor(int) 640 * @see #getHistoricalTouchMinor(int, int) 641 * @see MotionEvent.PointerCoords#touchMinor 642 * @see InputDevice#getMotionRange 643 */ 644 public static final int AXIS_TOUCH_MINOR = 5; 645 646 /** 647 * Axis constant: ToolMajor axis of a motion event. 648 * <p> 649 * <ul> 650 * <li>For a touch screen, reports the length of the major axis of an ellipse that 651 * represents the size of the approaching finger or tool used to make contact. 652 * <li>For a touch pad, reports the length of the major axis of an ellipse that 653 * represents the size of the approaching finger or tool used to make contact. 654 * The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 655 * to query the effective range of values. 656 * </ul> 657 * </p><p> 658 * When the touch is circular, the major and minor axis lengths will be equal to one another. 659 * </p><p> 660 * The tool size may be larger than the touch size since the tool may not be fully 661 * in contact with the touch sensor. 662 * </p> 663 * 664 * @see #getToolMajor(int) 665 * @see #getHistoricalToolMajor(int, int) 666 * @see MotionEvent.PointerCoords#toolMajor 667 * @see InputDevice#getMotionRange 668 */ 669 public static final int AXIS_TOOL_MAJOR = 6; 670 671 /** 672 * Axis constant: ToolMinor axis of a motion event. 673 * <p> 674 * <ul> 675 * <li>For a touch screen, reports the length of the minor axis of an ellipse that 676 * represents the size of the approaching finger or tool used to make contact. 677 * <li>For a touch pad, reports the length of the minor axis of an ellipse that 678 * represents the size of the approaching finger or tool used to make contact. 679 * The units are device-dependent; use {@link InputDevice#getMotionRange(int)} 680 * to query the effective range of values. 681 * </ul> 682 * </p><p> 683 * When the touch is circular, the major and minor axis lengths will be equal to one another. 684 * </p><p> 685 * The tool size may be larger than the touch size since the tool may not be fully 686 * in contact with the touch sensor. 687 * </p> 688 * 689 * @see #getToolMinor(int) 690 * @see #getHistoricalToolMinor(int, int) 691 * @see MotionEvent.PointerCoords#toolMinor 692 * @see InputDevice#getMotionRange 693 */ 694 public static final int AXIS_TOOL_MINOR = 7; 695 696 /** 697 * Axis constant: Orientation axis of a motion event. 698 * <p> 699 * <ul> 700 * <li>For a touch screen or touch pad, reports the orientation of the finger 701 * or tool in radians relative to the vertical plane of the device. 702 * An angle of 0 radians indicates that the major axis of contact is oriented 703 * upwards, is perfectly circular or is of unknown orientation. A positive angle 704 * indicates that the major axis of contact is oriented to the right. A negative angle 705 * indicates that the major axis of contact is oriented to the left. 706 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians 707 * (finger pointing fully right). 708 * <li>For a stylus, the orientation indicates the direction in which the stylus 709 * is pointing in relation to the vertical axis of the current orientation of the screen. 710 * The range is from -PI radians to PI radians, where 0 is pointing up, 711 * -PI/2 radians is pointing left, -PI or PI radians is pointing down, and PI/2 radians 712 * is pointing right. See also {@link #AXIS_TILT}. 713 * </ul> 714 * </p> 715 * 716 * @see #getOrientation(int) 717 * @see #getHistoricalOrientation(int, int) 718 * @see MotionEvent.PointerCoords#orientation 719 * @see InputDevice#getMotionRange 720 */ 721 public static final int AXIS_ORIENTATION = 8; 722 723 /** 724 * Axis constant: Vertical Scroll axis of a motion event. 725 * <p> 726 * <ul> 727 * <li>For a mouse, reports the relative movement of the vertical scroll wheel. 728 * The value is normalized to a range from -1.0 (down) to 1.0 (up). 729 * </ul> 730 * </p><p> 731 * This axis should be used to scroll views vertically. 732 * </p> 733 * 734 * @see #getAxisValue(int, int) 735 * @see #getHistoricalAxisValue(int, int, int) 736 * @see MotionEvent.PointerCoords#getAxisValue(int) 737 * @see InputDevice#getMotionRange 738 */ 739 public static final int AXIS_VSCROLL = 9; 740 741 /** 742 * Axis constant: Horizontal Scroll axis of a motion event. 743 * <p> 744 * <ul> 745 * <li>For a mouse, reports the relative movement of the horizontal scroll wheel. 746 * The value is normalized to a range from -1.0 (left) to 1.0 (right). 747 * </ul> 748 * </p><p> 749 * This axis should be used to scroll views horizontally. 750 * </p> 751 * 752 * @see #getAxisValue(int, int) 753 * @see #getHistoricalAxisValue(int, int, int) 754 * @see MotionEvent.PointerCoords#getAxisValue(int) 755 * @see InputDevice#getMotionRange 756 */ 757 public static final int AXIS_HSCROLL = 10; 758 759 /** 760 * Axis constant: Z axis of a motion event. 761 * <p> 762 * <ul> 763 * <li>For a joystick, reports the absolute Z position of the joystick. 764 * The value is normalized to a range from -1.0 (high) to 1.0 (low). 765 * <em>On game pads with two analog joysticks, this axis is often reinterpreted 766 * to report the absolute X position of the second joystick instead.</em> 767 * </ul> 768 * </p> 769 * 770 * @see #getAxisValue(int, int) 771 * @see #getHistoricalAxisValue(int, int, int) 772 * @see MotionEvent.PointerCoords#getAxisValue(int) 773 * @see InputDevice#getMotionRange 774 */ 775 public static final int AXIS_Z = 11; 776 777 /** 778 * Axis constant: X Rotation axis of a motion event. 779 * <p> 780 * <ul> 781 * <li>For a joystick, reports the absolute rotation angle about the X axis. 782 * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise). 783 * </ul> 784 * </p> 785 * 786 * @see #getAxisValue(int, int) 787 * @see #getHistoricalAxisValue(int, int, int) 788 * @see MotionEvent.PointerCoords#getAxisValue(int) 789 * @see InputDevice#getMotionRange 790 */ 791 public static final int AXIS_RX = 12; 792 793 /** 794 * Axis constant: Y Rotation axis of a motion event. 795 * <p> 796 * <ul> 797 * <li>For a joystick, reports the absolute rotation angle about the Y axis. 798 * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise). 799 * </ul> 800 * </p> 801 * 802 * @see #getAxisValue(int, int) 803 * @see #getHistoricalAxisValue(int, int, int) 804 * @see MotionEvent.PointerCoords#getAxisValue(int) 805 * @see InputDevice#getMotionRange 806 */ 807 public static final int AXIS_RY = 13; 808 809 /** 810 * Axis constant: Z Rotation axis of a motion event. 811 * <p> 812 * <ul> 813 * <li>For a joystick, reports the absolute rotation angle about the Z axis. 814 * The value is normalized to a range from -1.0 (counter-clockwise) to 1.0 (clockwise). 815 * <em>On game pads with two analog joysticks, this axis is often reinterpreted 816 * to report the absolute Y position of the second joystick instead.</em> 817 * </ul> 818 * </p> 819 * 820 * @see #getAxisValue(int, int) 821 * @see #getHistoricalAxisValue(int, int, int) 822 * @see MotionEvent.PointerCoords#getAxisValue(int) 823 * @see InputDevice#getMotionRange 824 */ 825 public static final int AXIS_RZ = 14; 826 827 /** 828 * Axis constant: Hat X axis of a motion event. 829 * <p> 830 * <ul> 831 * <li>For a joystick, reports the absolute X position of the directional hat control. 832 * The value is normalized to a range from -1.0 (left) to 1.0 (right). 833 * </ul> 834 * </p> 835 * 836 * @see #getAxisValue(int, int) 837 * @see #getHistoricalAxisValue(int, int, int) 838 * @see MotionEvent.PointerCoords#getAxisValue(int) 839 * @see InputDevice#getMotionRange 840 */ 841 public static final int AXIS_HAT_X = 15; 842 843 /** 844 * Axis constant: Hat Y axis of a motion event. 845 * <p> 846 * <ul> 847 * <li>For a joystick, reports the absolute Y position of the directional hat control. 848 * The value is normalized to a range from -1.0 (up) to 1.0 (down). 849 * </ul> 850 * </p> 851 * 852 * @see #getAxisValue(int, int) 853 * @see #getHistoricalAxisValue(int, int, int) 854 * @see MotionEvent.PointerCoords#getAxisValue(int) 855 * @see InputDevice#getMotionRange 856 */ 857 public static final int AXIS_HAT_Y = 16; 858 859 /** 860 * Axis constant: Left Trigger axis of a motion event. 861 * <p> 862 * <ul> 863 * <li>For a joystick, reports the absolute position of the left trigger control. 864 * The value is normalized to a range from 0.0 (released) to 1.0 (fully pressed). 865 * </ul> 866 * </p> 867 * 868 * @see #getAxisValue(int, int) 869 * @see #getHistoricalAxisValue(int, int, int) 870 * @see MotionEvent.PointerCoords#getAxisValue(int) 871 * @see InputDevice#getMotionRange 872 */ 873 public static final int AXIS_LTRIGGER = 17; 874 875 /** 876 * Axis constant: Right Trigger axis of a motion event. 877 * <p> 878 * <ul> 879 * <li>For a joystick, reports the absolute position of the right trigger control. 880 * The value is normalized to a range from 0.0 (released) to 1.0 (fully pressed). 881 * </ul> 882 * </p> 883 * 884 * @see #getAxisValue(int, int) 885 * @see #getHistoricalAxisValue(int, int, int) 886 * @see MotionEvent.PointerCoords#getAxisValue(int) 887 * @see InputDevice#getMotionRange 888 */ 889 public static final int AXIS_RTRIGGER = 18; 890 891 /** 892 * Axis constant: Throttle axis of a motion event. 893 * <p> 894 * <ul> 895 * <li>For a joystick, reports the absolute position of the throttle control. 896 * The value is normalized to a range from 0.0 (fully open) to 1.0 (fully closed). 897 * </ul> 898 * </p> 899 * 900 * @see #getAxisValue(int, int) 901 * @see #getHistoricalAxisValue(int, int, int) 902 * @see MotionEvent.PointerCoords#getAxisValue(int) 903 * @see InputDevice#getMotionRange 904 */ 905 public static final int AXIS_THROTTLE = 19; 906 907 /** 908 * Axis constant: Rudder axis of a motion event. 909 * <p> 910 * <ul> 911 * <li>For a joystick, reports the absolute position of the rudder control. 912 * The value is normalized to a range from -1.0 (turn left) to 1.0 (turn right). 913 * </ul> 914 * </p> 915 * 916 * @see #getAxisValue(int, int) 917 * @see #getHistoricalAxisValue(int, int, int) 918 * @see MotionEvent.PointerCoords#getAxisValue(int) 919 * @see InputDevice#getMotionRange 920 */ 921 public static final int AXIS_RUDDER = 20; 922 923 /** 924 * Axis constant: Wheel axis of a motion event. 925 * <p> 926 * <ul> 927 * <li>For a joystick, reports the absolute position of the steering wheel control. 928 * The value is normalized to a range from -1.0 (turn left) to 1.0 (turn right). 929 * </ul> 930 * </p> 931 * 932 * @see #getAxisValue(int, int) 933 * @see #getHistoricalAxisValue(int, int, int) 934 * @see MotionEvent.PointerCoords#getAxisValue(int) 935 * @see InputDevice#getMotionRange 936 */ 937 public static final int AXIS_WHEEL = 21; 938 939 /** 940 * Axis constant: Gas axis of a motion event. 941 * <p> 942 * <ul> 943 * <li>For a joystick, reports the absolute position of the gas (accelerator) control. 944 * The value is normalized to a range from 0.0 (no acceleration) 945 * to 1.0 (maximum acceleration). 946 * </ul> 947 * </p> 948 * 949 * @see #getAxisValue(int, int) 950 * @see #getHistoricalAxisValue(int, int, int) 951 * @see MotionEvent.PointerCoords#getAxisValue(int) 952 * @see InputDevice#getMotionRange 953 */ 954 public static final int AXIS_GAS = 22; 955 956 /** 957 * Axis constant: Brake axis of a motion event. 958 * <p> 959 * <ul> 960 * <li>For a joystick, reports the absolute position of the brake control. 961 * The value is normalized to a range from 0.0 (no braking) to 1.0 (maximum braking). 962 * </ul> 963 * </p> 964 * 965 * @see #getAxisValue(int, int) 966 * @see #getHistoricalAxisValue(int, int, int) 967 * @see MotionEvent.PointerCoords#getAxisValue(int) 968 * @see InputDevice#getMotionRange 969 */ 970 public static final int AXIS_BRAKE = 23; 971 972 /** 973 * Axis constant: Distance axis of a motion event. 974 * <p> 975 * <ul> 976 * <li>For a stylus, reports the distance of the stylus from the screen. 977 * A value of 0.0 indicates direct contact and larger values indicate increasing 978 * distance from the surface. 979 * </ul> 980 * </p> 981 * 982 * @see #getAxisValue(int, int) 983 * @see #getHistoricalAxisValue(int, int, int) 984 * @see MotionEvent.PointerCoords#getAxisValue(int) 985 * @see InputDevice#getMotionRange 986 */ 987 public static final int AXIS_DISTANCE = 24; 988 989 /** 990 * Axis constant: Tilt axis of a motion event. 991 * <p> 992 * <ul> 993 * <li>For a stylus, reports the tilt angle of the stylus in radians where 994 * 0 radians indicates that the stylus is being held perpendicular to the 995 * surface, and PI/2 radians indicates that the stylus is being held flat 996 * against the surface. 997 * </ul> 998 * </p> 999 * 1000 * @see #getAxisValue(int, int) 1001 * @see #getHistoricalAxisValue(int, int, int) 1002 * @see MotionEvent.PointerCoords#getAxisValue(int, int) 1003 * @see InputDevice#getMotionRange 1004 */ 1005 public static final int AXIS_TILT = 25; 1006 1007 /** 1008 * Axis constant: Generic scroll axis of a motion event. 1009 * <p> 1010 * <ul> 1011 * <li>Reports the relative movement of the generic scrolling device. 1012 * </ul> 1013 * </p><p> 1014 * This axis should be used for scroll events that are neither strictly vertical nor horizontal. 1015 * A good example would be the rotation of a rotary encoder input device. 1016 * </p> 1017 * 1018 * @see #getAxisValue(int, int) 1019 */ 1020 public static final int AXIS_SCROLL = 26; 1021 1022 /** 1023 * Axis constant: The movement of x position of a motion event. 1024 * <p> 1025 * <ul> 1026 * <li>For a mouse, reports a difference of x position between the previous position. 1027 * This is useful when pointer is captured, in that case the mouse pointer doesn't change 1028 * the location but this axis reports the difference which allows the app to see 1029 * how the mouse is moved. 1030 * </ul> 1031 * </p> 1032 * 1033 * @see #getAxisValue(int, int) 1034 * @see #getHistoricalAxisValue(int, int, int) 1035 * @see MotionEvent.PointerCoords#getAxisValue(int, int) 1036 * @see InputDevice#getMotionRange 1037 */ 1038 public static final int AXIS_RELATIVE_X = 27; 1039 1040 /** 1041 * Axis constant: The movement of y position of a motion event. 1042 * <p> 1043 * This is similar to {@link #AXIS_RELATIVE_X} but for y-axis. 1044 * </p> 1045 * 1046 * @see #getAxisValue(int, int) 1047 * @see #getHistoricalAxisValue(int, int, int) 1048 * @see MotionEvent.PointerCoords#getAxisValue(int, int) 1049 * @see InputDevice#getMotionRange 1050 */ 1051 public static final int AXIS_RELATIVE_Y = 28; 1052 1053 /** 1054 * Axis constant: Generic 1 axis of a motion event. 1055 * The interpretation of a generic axis is device-specific. 1056 * 1057 * @see #getAxisValue(int, int) 1058 * @see #getHistoricalAxisValue(int, int, int) 1059 * @see MotionEvent.PointerCoords#getAxisValue(int) 1060 * @see InputDevice#getMotionRange 1061 */ 1062 public static final int AXIS_GENERIC_1 = 32; 1063 1064 /** 1065 * Axis constant: Generic 2 axis of a motion event. 1066 * The interpretation of a generic axis is device-specific. 1067 * 1068 * @see #getAxisValue(int, int) 1069 * @see #getHistoricalAxisValue(int, int, int) 1070 * @see MotionEvent.PointerCoords#getAxisValue(int) 1071 * @see InputDevice#getMotionRange 1072 */ 1073 public static final int AXIS_GENERIC_2 = 33; 1074 1075 /** 1076 * Axis constant: Generic 3 axis of a motion event. 1077 * The interpretation of a generic axis is device-specific. 1078 * 1079 * @see #getAxisValue(int, int) 1080 * @see #getHistoricalAxisValue(int, int, int) 1081 * @see MotionEvent.PointerCoords#getAxisValue(int) 1082 * @see InputDevice#getMotionRange 1083 */ 1084 public static final int AXIS_GENERIC_3 = 34; 1085 1086 /** 1087 * Axis constant: Generic 4 axis of a motion event. 1088 * The interpretation of a generic axis is device-specific. 1089 * 1090 * @see #getAxisValue(int, int) 1091 * @see #getHistoricalAxisValue(int, int, int) 1092 * @see MotionEvent.PointerCoords#getAxisValue(int) 1093 * @see InputDevice#getMotionRange 1094 */ 1095 public static final int AXIS_GENERIC_4 = 35; 1096 1097 /** 1098 * Axis constant: Generic 5 axis of a motion event. 1099 * The interpretation of a generic axis is device-specific. 1100 * 1101 * @see #getAxisValue(int, int) 1102 * @see #getHistoricalAxisValue(int, int, int) 1103 * @see MotionEvent.PointerCoords#getAxisValue(int) 1104 * @see InputDevice#getMotionRange 1105 */ 1106 public static final int AXIS_GENERIC_5 = 36; 1107 1108 /** 1109 * Axis constant: Generic 6 axis of a motion event. 1110 * The interpretation of a generic axis is device-specific. 1111 * 1112 * @see #getAxisValue(int, int) 1113 * @see #getHistoricalAxisValue(int, int, int) 1114 * @see MotionEvent.PointerCoords#getAxisValue(int) 1115 * @see InputDevice#getMotionRange 1116 */ 1117 public static final int AXIS_GENERIC_6 = 37; 1118 1119 /** 1120 * Axis constant: Generic 7 axis of a motion event. 1121 * The interpretation of a generic axis is device-specific. 1122 * 1123 * @see #getAxisValue(int, int) 1124 * @see #getHistoricalAxisValue(int, int, int) 1125 * @see MotionEvent.PointerCoords#getAxisValue(int) 1126 * @see InputDevice#getMotionRange 1127 */ 1128 public static final int AXIS_GENERIC_7 = 38; 1129 1130 /** 1131 * Axis constant: Generic 8 axis of a motion event. 1132 * The interpretation of a generic axis is device-specific. 1133 * 1134 * @see #getAxisValue(int, int) 1135 * @see #getHistoricalAxisValue(int, int, int) 1136 * @see MotionEvent.PointerCoords#getAxisValue(int) 1137 * @see InputDevice#getMotionRange 1138 */ 1139 public static final int AXIS_GENERIC_8 = 39; 1140 1141 /** 1142 * Axis constant: Generic 9 axis of a motion event. 1143 * The interpretation of a generic axis is device-specific. 1144 * 1145 * @see #getAxisValue(int, int) 1146 * @see #getHistoricalAxisValue(int, int, int) 1147 * @see MotionEvent.PointerCoords#getAxisValue(int) 1148 * @see InputDevice#getMotionRange 1149 */ 1150 public static final int AXIS_GENERIC_9 = 40; 1151 1152 /** 1153 * Axis constant: Generic 10 axis of a motion event. 1154 * The interpretation of a generic axis is device-specific. 1155 * 1156 * @see #getAxisValue(int, int) 1157 * @see #getHistoricalAxisValue(int, int, int) 1158 * @see MotionEvent.PointerCoords#getAxisValue(int) 1159 * @see InputDevice#getMotionRange 1160 */ 1161 public static final int AXIS_GENERIC_10 = 41; 1162 1163 /** 1164 * Axis constant: Generic 11 axis of a motion event. 1165 * The interpretation of a generic axis is device-specific. 1166 * 1167 * @see #getAxisValue(int, int) 1168 * @see #getHistoricalAxisValue(int, int, int) 1169 * @see MotionEvent.PointerCoords#getAxisValue(int) 1170 * @see InputDevice#getMotionRange 1171 */ 1172 public static final int AXIS_GENERIC_11 = 42; 1173 1174 /** 1175 * Axis constant: Generic 12 axis of a motion event. 1176 * The interpretation of a generic axis is device-specific. 1177 * 1178 * @see #getAxisValue(int, int) 1179 * @see #getHistoricalAxisValue(int, int, int) 1180 * @see MotionEvent.PointerCoords#getAxisValue(int) 1181 * @see InputDevice#getMotionRange 1182 */ 1183 public static final int AXIS_GENERIC_12 = 43; 1184 1185 /** 1186 * Axis constant: Generic 13 axis of a motion event. 1187 * The interpretation of a generic axis is device-specific. 1188 * 1189 * @see #getAxisValue(int, int) 1190 * @see #getHistoricalAxisValue(int, int, int) 1191 * @see MotionEvent.PointerCoords#getAxisValue(int) 1192 * @see InputDevice#getMotionRange 1193 */ 1194 public static final int AXIS_GENERIC_13 = 44; 1195 1196 /** 1197 * Axis constant: Generic 14 axis of a motion event. 1198 * The interpretation of a generic axis is device-specific. 1199 * 1200 * @see #getAxisValue(int, int) 1201 * @see #getHistoricalAxisValue(int, int, int) 1202 * @see MotionEvent.PointerCoords#getAxisValue(int) 1203 * @see InputDevice#getMotionRange 1204 */ 1205 public static final int AXIS_GENERIC_14 = 45; 1206 1207 /** 1208 * Axis constant: Generic 15 axis of a motion event. 1209 * The interpretation of a generic axis is device-specific. 1210 * 1211 * @see #getAxisValue(int, int) 1212 * @see #getHistoricalAxisValue(int, int, int) 1213 * @see MotionEvent.PointerCoords#getAxisValue(int) 1214 * @see InputDevice#getMotionRange 1215 */ 1216 public static final int AXIS_GENERIC_15 = 46; 1217 1218 /** 1219 * Axis constant: Generic 16 axis of a motion event. 1220 * The interpretation of a generic axis is device-specific. 1221 * 1222 * @see #getAxisValue(int, int) 1223 * @see #getHistoricalAxisValue(int, int, int) 1224 * @see MotionEvent.PointerCoords#getAxisValue(int) 1225 * @see InputDevice#getMotionRange 1226 */ 1227 public static final int AXIS_GENERIC_16 = 47; 1228 1229 // NOTE: If you add a new axis here you must also add it to: 1230 // native/include/android/input.h 1231 // frameworks/base/include/ui/KeycodeLabels.h 1232 1233 // Symbolic names of all axes. 1234 private static final SparseArray<String> AXIS_SYMBOLIC_NAMES = new SparseArray<String>(); 1235 static { 1236 SparseArray<String> names = AXIS_SYMBOLIC_NAMES; names.append(AXIS_X, "AXIS_X")1237 names.append(AXIS_X, "AXIS_X"); names.append(AXIS_Y, "AXIS_Y")1238 names.append(AXIS_Y, "AXIS_Y"); names.append(AXIS_PRESSURE, "AXIS_PRESSURE")1239 names.append(AXIS_PRESSURE, "AXIS_PRESSURE"); names.append(AXIS_SIZE, "AXIS_SIZE")1240 names.append(AXIS_SIZE, "AXIS_SIZE"); names.append(AXIS_TOUCH_MAJOR, "AXIS_TOUCH_MAJOR")1241 names.append(AXIS_TOUCH_MAJOR, "AXIS_TOUCH_MAJOR"); names.append(AXIS_TOUCH_MINOR, "AXIS_TOUCH_MINOR")1242 names.append(AXIS_TOUCH_MINOR, "AXIS_TOUCH_MINOR"); names.append(AXIS_TOOL_MAJOR, "AXIS_TOOL_MAJOR")1243 names.append(AXIS_TOOL_MAJOR, "AXIS_TOOL_MAJOR"); names.append(AXIS_TOOL_MINOR, "AXIS_TOOL_MINOR")1244 names.append(AXIS_TOOL_MINOR, "AXIS_TOOL_MINOR"); names.append(AXIS_ORIENTATION, "AXIS_ORIENTATION")1245 names.append(AXIS_ORIENTATION, "AXIS_ORIENTATION"); names.append(AXIS_VSCROLL, "AXIS_VSCROLL")1246 names.append(AXIS_VSCROLL, "AXIS_VSCROLL"); names.append(AXIS_HSCROLL, "AXIS_HSCROLL")1247 names.append(AXIS_HSCROLL, "AXIS_HSCROLL"); names.append(AXIS_Z, "AXIS_Z")1248 names.append(AXIS_Z, "AXIS_Z"); names.append(AXIS_RX, "AXIS_RX")1249 names.append(AXIS_RX, "AXIS_RX"); names.append(AXIS_RY, "AXIS_RY")1250 names.append(AXIS_RY, "AXIS_RY"); names.append(AXIS_RZ, "AXIS_RZ")1251 names.append(AXIS_RZ, "AXIS_RZ"); names.append(AXIS_HAT_X, "AXIS_HAT_X")1252 names.append(AXIS_HAT_X, "AXIS_HAT_X"); names.append(AXIS_HAT_Y, "AXIS_HAT_Y")1253 names.append(AXIS_HAT_Y, "AXIS_HAT_Y"); names.append(AXIS_LTRIGGER, "AXIS_LTRIGGER")1254 names.append(AXIS_LTRIGGER, "AXIS_LTRIGGER"); names.append(AXIS_RTRIGGER, "AXIS_RTRIGGER")1255 names.append(AXIS_RTRIGGER, "AXIS_RTRIGGER"); names.append(AXIS_THROTTLE, "AXIS_THROTTLE")1256 names.append(AXIS_THROTTLE, "AXIS_THROTTLE"); names.append(AXIS_RUDDER, "AXIS_RUDDER")1257 names.append(AXIS_RUDDER, "AXIS_RUDDER"); names.append(AXIS_WHEEL, "AXIS_WHEEL")1258 names.append(AXIS_WHEEL, "AXIS_WHEEL"); names.append(AXIS_GAS, "AXIS_GAS")1259 names.append(AXIS_GAS, "AXIS_GAS"); names.append(AXIS_BRAKE, "AXIS_BRAKE")1260 names.append(AXIS_BRAKE, "AXIS_BRAKE"); names.append(AXIS_DISTANCE, "AXIS_DISTANCE")1261 names.append(AXIS_DISTANCE, "AXIS_DISTANCE"); names.append(AXIS_TILT, "AXIS_TILT")1262 names.append(AXIS_TILT, "AXIS_TILT"); names.append(AXIS_SCROLL, "AXIS_SCROLL")1263 names.append(AXIS_SCROLL, "AXIS_SCROLL"); names.append(AXIS_RELATIVE_X, "AXIS_REALTIVE_X")1264 names.append(AXIS_RELATIVE_X, "AXIS_REALTIVE_X"); names.append(AXIS_RELATIVE_Y, "AXIS_REALTIVE_Y")1265 names.append(AXIS_RELATIVE_Y, "AXIS_REALTIVE_Y"); names.append(AXIS_GENERIC_1, "AXIS_GENERIC_1")1266 names.append(AXIS_GENERIC_1, "AXIS_GENERIC_1"); names.append(AXIS_GENERIC_2, "AXIS_GENERIC_2")1267 names.append(AXIS_GENERIC_2, "AXIS_GENERIC_2"); names.append(AXIS_GENERIC_3, "AXIS_GENERIC_3")1268 names.append(AXIS_GENERIC_3, "AXIS_GENERIC_3"); names.append(AXIS_GENERIC_4, "AXIS_GENERIC_4")1269 names.append(AXIS_GENERIC_4, "AXIS_GENERIC_4"); names.append(AXIS_GENERIC_5, "AXIS_GENERIC_5")1270 names.append(AXIS_GENERIC_5, "AXIS_GENERIC_5"); names.append(AXIS_GENERIC_6, "AXIS_GENERIC_6")1271 names.append(AXIS_GENERIC_6, "AXIS_GENERIC_6"); names.append(AXIS_GENERIC_7, "AXIS_GENERIC_7")1272 names.append(AXIS_GENERIC_7, "AXIS_GENERIC_7"); names.append(AXIS_GENERIC_8, "AXIS_GENERIC_8")1273 names.append(AXIS_GENERIC_8, "AXIS_GENERIC_8"); names.append(AXIS_GENERIC_9, "AXIS_GENERIC_9")1274 names.append(AXIS_GENERIC_9, "AXIS_GENERIC_9"); names.append(AXIS_GENERIC_10, "AXIS_GENERIC_10")1275 names.append(AXIS_GENERIC_10, "AXIS_GENERIC_10"); names.append(AXIS_GENERIC_11, "AXIS_GENERIC_11")1276 names.append(AXIS_GENERIC_11, "AXIS_GENERIC_11"); names.append(AXIS_GENERIC_12, "AXIS_GENERIC_12")1277 names.append(AXIS_GENERIC_12, "AXIS_GENERIC_12"); names.append(AXIS_GENERIC_13, "AXIS_GENERIC_13")1278 names.append(AXIS_GENERIC_13, "AXIS_GENERIC_13"); names.append(AXIS_GENERIC_14, "AXIS_GENERIC_14")1279 names.append(AXIS_GENERIC_14, "AXIS_GENERIC_14"); names.append(AXIS_GENERIC_15, "AXIS_GENERIC_15")1280 names.append(AXIS_GENERIC_15, "AXIS_GENERIC_15"); names.append(AXIS_GENERIC_16, "AXIS_GENERIC_16")1281 names.append(AXIS_GENERIC_16, "AXIS_GENERIC_16"); 1282 } 1283 1284 /** 1285 * Button constant: Primary button (left mouse button). 1286 * 1287 * This button constant is not set in response to simple touches with a finger 1288 * or stylus tip. The user must actually push a button. 1289 * 1290 * @see #getButtonState 1291 */ 1292 public static final int BUTTON_PRIMARY = 1 << 0; 1293 1294 /** 1295 * Button constant: Secondary button (right mouse button). 1296 * 1297 * @see #getButtonState 1298 */ 1299 public static final int BUTTON_SECONDARY = 1 << 1; 1300 1301 /** 1302 * Button constant: Tertiary button (middle mouse button). 1303 * 1304 * @see #getButtonState 1305 */ 1306 public static final int BUTTON_TERTIARY = 1 << 2; 1307 1308 /** 1309 * Button constant: Back button pressed (mouse back button). 1310 * <p> 1311 * The system may send a {@link KeyEvent#KEYCODE_BACK} key press to the application 1312 * when this button is pressed. 1313 * </p> 1314 * 1315 * @see #getButtonState 1316 */ 1317 public static final int BUTTON_BACK = 1 << 3; 1318 1319 /** 1320 * Button constant: Forward button pressed (mouse forward button). 1321 * <p> 1322 * The system may send a {@link KeyEvent#KEYCODE_FORWARD} key press to the application 1323 * when this button is pressed. 1324 * </p> 1325 * 1326 * @see #getButtonState 1327 */ 1328 public static final int BUTTON_FORWARD = 1 << 4; 1329 1330 /** 1331 * Button constant: Primary stylus button pressed. 1332 * 1333 * @see #getButtonState 1334 */ 1335 public static final int BUTTON_STYLUS_PRIMARY = 1 << 5; 1336 1337 /** 1338 * Button constant: Secondary stylus button pressed. 1339 * 1340 * @see #getButtonState 1341 */ 1342 public static final int BUTTON_STYLUS_SECONDARY = 1 << 6; 1343 1344 // NOTE: If you add a new axis here you must also add it to: 1345 // native/include/android/input.h 1346 1347 // Symbolic names of all button states in bit order from least significant 1348 // to most significant. 1349 private static final String[] BUTTON_SYMBOLIC_NAMES = new String[] { 1350 "BUTTON_PRIMARY", 1351 "BUTTON_SECONDARY", 1352 "BUTTON_TERTIARY", 1353 "BUTTON_BACK", 1354 "BUTTON_FORWARD", 1355 "BUTTON_STYLUS_PRIMARY", 1356 "BUTTON_STYLUS_SECONDARY", 1357 "0x00000080", 1358 "0x00000100", 1359 "0x00000200", 1360 "0x00000400", 1361 "0x00000800", 1362 "0x00001000", 1363 "0x00002000", 1364 "0x00004000", 1365 "0x00008000", 1366 "0x00010000", 1367 "0x00020000", 1368 "0x00040000", 1369 "0x00080000", 1370 "0x00100000", 1371 "0x00200000", 1372 "0x00400000", 1373 "0x00800000", 1374 "0x01000000", 1375 "0x02000000", 1376 "0x04000000", 1377 "0x08000000", 1378 "0x10000000", 1379 "0x20000000", 1380 "0x40000000", 1381 "0x80000000", 1382 }; 1383 1384 /** 1385 * Tool type constant: Unknown tool type. 1386 * This constant is used when the tool type is not known or is not relevant, 1387 * such as for a trackball or other non-pointing device. 1388 * 1389 * @see #getToolType 1390 */ 1391 public static final int TOOL_TYPE_UNKNOWN = 0; 1392 1393 /** 1394 * Tool type constant: The tool is a finger. 1395 * 1396 * @see #getToolType 1397 */ 1398 public static final int TOOL_TYPE_FINGER = 1; 1399 1400 /** 1401 * Tool type constant: The tool is a stylus. 1402 * 1403 * @see #getToolType 1404 */ 1405 public static final int TOOL_TYPE_STYLUS = 2; 1406 1407 /** 1408 * Tool type constant: The tool is a mouse or trackpad. 1409 * 1410 * @see #getToolType 1411 */ 1412 public static final int TOOL_TYPE_MOUSE = 3; 1413 1414 /** 1415 * Tool type constant: The tool is an eraser or a stylus being used in an inverted posture. 1416 * 1417 * @see #getToolType 1418 */ 1419 public static final int TOOL_TYPE_ERASER = 4; 1420 1421 // NOTE: If you add a new tool type here you must also add it to: 1422 // native/include/android/input.h 1423 1424 // Symbolic names of all tool types. 1425 private static final SparseArray<String> TOOL_TYPE_SYMBOLIC_NAMES = new SparseArray<String>(); 1426 static { 1427 SparseArray<String> names = TOOL_TYPE_SYMBOLIC_NAMES; names.append(TOOL_TYPE_UNKNOWN, "TOOL_TYPE_UNKNOWN")1428 names.append(TOOL_TYPE_UNKNOWN, "TOOL_TYPE_UNKNOWN"); names.append(TOOL_TYPE_FINGER, "TOOL_TYPE_FINGER")1429 names.append(TOOL_TYPE_FINGER, "TOOL_TYPE_FINGER"); names.append(TOOL_TYPE_STYLUS, "TOOL_TYPE_STYLUS")1430 names.append(TOOL_TYPE_STYLUS, "TOOL_TYPE_STYLUS"); names.append(TOOL_TYPE_MOUSE, "TOOL_TYPE_MOUSE")1431 names.append(TOOL_TYPE_MOUSE, "TOOL_TYPE_MOUSE"); names.append(TOOL_TYPE_ERASER, "TOOL_TYPE_ERASER")1432 names.append(TOOL_TYPE_ERASER, "TOOL_TYPE_ERASER"); 1433 } 1434 1435 // Private value for history pos that obtains the current sample. 1436 private static final int HISTORY_CURRENT = -0x80000000; 1437 1438 private static final int MAX_RECYCLED = 10; 1439 private static final Object gRecyclerLock = new Object(); 1440 private static int gRecyclerUsed; 1441 private static MotionEvent gRecyclerTop; 1442 1443 // Shared temporary objects used when translating coordinates supplied by 1444 // the caller into single element PointerCoords and pointer id arrays. 1445 private static final Object gSharedTempLock = new Object(); 1446 private static PointerCoords[] gSharedTempPointerCoords; 1447 private static PointerProperties[] gSharedTempPointerProperties; 1448 private static int[] gSharedTempPointerIndexMap; 1449 ensureSharedTempPointerCapacity(int desiredCapacity)1450 private static final void ensureSharedTempPointerCapacity(int desiredCapacity) { 1451 if (gSharedTempPointerCoords == null 1452 || gSharedTempPointerCoords.length < desiredCapacity) { 1453 int capacity = gSharedTempPointerCoords != null ? gSharedTempPointerCoords.length : 8; 1454 while (capacity < desiredCapacity) { 1455 capacity *= 2; 1456 } 1457 gSharedTempPointerCoords = PointerCoords.createArray(capacity); 1458 gSharedTempPointerProperties = PointerProperties.createArray(capacity); 1459 gSharedTempPointerIndexMap = new int[capacity]; 1460 } 1461 } 1462 1463 // Pointer to the native MotionEvent object that contains the actual data. 1464 private long mNativePtr; 1465 1466 private MotionEvent mNext; 1467 nativeInitialize(long nativePtr, int deviceId, int source, int action, int flags, int edgeFlags, int metaState, int buttonState, float xOffset, float yOffset, float xPrecision, float yPrecision, long downTimeNanos, long eventTimeNanos, int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords)1468 private static native long nativeInitialize(long nativePtr, 1469 int deviceId, int source, int action, int flags, int edgeFlags, 1470 int metaState, int buttonState, 1471 float xOffset, float yOffset, float xPrecision, float yPrecision, 1472 long downTimeNanos, long eventTimeNanos, 1473 int pointerCount, PointerProperties[] pointerIds, PointerCoords[] pointerCoords); nativeDispose(long nativePtr)1474 private static native void nativeDispose(long nativePtr); nativeAddBatch(long nativePtr, long eventTimeNanos, PointerCoords[] pointerCoords, int metaState)1475 private static native void nativeAddBatch(long nativePtr, long eventTimeNanos, 1476 PointerCoords[] pointerCoords, int metaState); nativeGetPointerCoords(long nativePtr, int pointerIndex, int historyPos, PointerCoords outPointerCoords)1477 private static native void nativeGetPointerCoords(long nativePtr, 1478 int pointerIndex, int historyPos, PointerCoords outPointerCoords); nativeGetPointerProperties(long nativePtr, int pointerIndex, PointerProperties outPointerProperties)1479 private static native void nativeGetPointerProperties(long nativePtr, 1480 int pointerIndex, PointerProperties outPointerProperties); 1481 nativeReadFromParcel(long nativePtr, Parcel parcel)1482 private static native long nativeReadFromParcel(long nativePtr, Parcel parcel); nativeWriteToParcel(long nativePtr, Parcel parcel)1483 private static native void nativeWriteToParcel(long nativePtr, Parcel parcel); 1484 nativeAxisToString(int axis)1485 private static native String nativeAxisToString(int axis); nativeAxisFromString(String label)1486 private static native int nativeAxisFromString(String label); 1487 1488 // -------------- @FastNative ------------------------- 1489 1490 @FastNative nativeGetPointerId(long nativePtr, int pointerIndex)1491 private static native int nativeGetPointerId(long nativePtr, int pointerIndex); 1492 @FastNative nativeGetToolType(long nativePtr, int pointerIndex)1493 private static native int nativeGetToolType(long nativePtr, int pointerIndex); 1494 @FastNative nativeGetEventTimeNanos(long nativePtr, int historyPos)1495 private static native long nativeGetEventTimeNanos(long nativePtr, int historyPos); 1496 @FastNative nativeGetRawAxisValue(long nativePtr, int axis, int pointerIndex, int historyPos)1497 private static native float nativeGetRawAxisValue(long nativePtr, 1498 int axis, int pointerIndex, int historyPos); 1499 @FastNative nativeGetAxisValue(long nativePtr, int axis, int pointerIndex, int historyPos)1500 private static native float nativeGetAxisValue(long nativePtr, 1501 int axis, int pointerIndex, int historyPos); 1502 1503 // -------------- @CriticalNative ---------------------- 1504 1505 @CriticalNative nativeCopy(long destNativePtr, long sourceNativePtr, boolean keepHistory)1506 private static native long nativeCopy(long destNativePtr, long sourceNativePtr, 1507 boolean keepHistory); 1508 @CriticalNative nativeGetDeviceId(long nativePtr)1509 private static native int nativeGetDeviceId(long nativePtr); 1510 @CriticalNative nativeGetSource(long nativePtr)1511 private static native int nativeGetSource(long nativePtr); 1512 @CriticalNative nativeSetSource(long nativePtr, int source)1513 private static native int nativeSetSource(long nativePtr, int source); 1514 @CriticalNative nativeGetAction(long nativePtr)1515 private static native int nativeGetAction(long nativePtr); 1516 @CriticalNative nativeSetAction(long nativePtr, int action)1517 private static native void nativeSetAction(long nativePtr, int action); 1518 @CriticalNative nativeIsTouchEvent(long nativePtr)1519 private static native boolean nativeIsTouchEvent(long nativePtr); 1520 @CriticalNative nativeGetFlags(long nativePtr)1521 private static native int nativeGetFlags(long nativePtr); 1522 @CriticalNative nativeSetFlags(long nativePtr, int flags)1523 private static native void nativeSetFlags(long nativePtr, int flags); 1524 @CriticalNative nativeGetEdgeFlags(long nativePtr)1525 private static native int nativeGetEdgeFlags(long nativePtr); 1526 @CriticalNative nativeSetEdgeFlags(long nativePtr, int action)1527 private static native void nativeSetEdgeFlags(long nativePtr, int action); 1528 @CriticalNative nativeGetMetaState(long nativePtr)1529 private static native int nativeGetMetaState(long nativePtr); 1530 @CriticalNative nativeGetButtonState(long nativePtr)1531 private static native int nativeGetButtonState(long nativePtr); 1532 @CriticalNative nativeSetButtonState(long nativePtr, int buttonState)1533 private static native void nativeSetButtonState(long nativePtr, int buttonState); 1534 @CriticalNative nativeGetActionButton(long nativePtr)1535 private static native int nativeGetActionButton(long nativePtr); 1536 @CriticalNative nativeSetActionButton(long nativePtr, int actionButton)1537 private static native void nativeSetActionButton(long nativePtr, int actionButton); 1538 @CriticalNative nativeOffsetLocation(long nativePtr, float deltaX, float deltaY)1539 private static native void nativeOffsetLocation(long nativePtr, float deltaX, float deltaY); 1540 @CriticalNative nativeGetXOffset(long nativePtr)1541 private static native float nativeGetXOffset(long nativePtr); 1542 @CriticalNative nativeGetYOffset(long nativePtr)1543 private static native float nativeGetYOffset(long nativePtr); 1544 @CriticalNative nativeGetXPrecision(long nativePtr)1545 private static native float nativeGetXPrecision(long nativePtr); 1546 @CriticalNative nativeGetYPrecision(long nativePtr)1547 private static native float nativeGetYPrecision(long nativePtr); 1548 @CriticalNative nativeGetDownTimeNanos(long nativePtr)1549 private static native long nativeGetDownTimeNanos(long nativePtr); 1550 @CriticalNative nativeSetDownTimeNanos(long nativePtr, long downTime)1551 private static native void nativeSetDownTimeNanos(long nativePtr, long downTime); 1552 1553 @CriticalNative nativeGetPointerCount(long nativePtr)1554 private static native int nativeGetPointerCount(long nativePtr); 1555 @CriticalNative nativeFindPointerIndex(long nativePtr, int pointerId)1556 private static native int nativeFindPointerIndex(long nativePtr, int pointerId); 1557 1558 @CriticalNative nativeGetHistorySize(long nativePtr)1559 private static native int nativeGetHistorySize(long nativePtr); 1560 1561 @CriticalNative nativeScale(long nativePtr, float scale)1562 private static native void nativeScale(long nativePtr, float scale); 1563 @CriticalNative nativeTransform(long nativePtr, long matrix)1564 private static native void nativeTransform(long nativePtr, long matrix); 1565 MotionEvent()1566 private MotionEvent() { 1567 } 1568 1569 @Override finalize()1570 protected void finalize() throws Throwable { 1571 try { 1572 if (mNativePtr != 0) { 1573 nativeDispose(mNativePtr); 1574 mNativePtr = 0; 1575 } 1576 } finally { 1577 super.finalize(); 1578 } 1579 } 1580 obtain()1581 static private MotionEvent obtain() { 1582 final MotionEvent ev; 1583 synchronized (gRecyclerLock) { 1584 ev = gRecyclerTop; 1585 if (ev == null) { 1586 return new MotionEvent(); 1587 } 1588 gRecyclerTop = ev.mNext; 1589 gRecyclerUsed -= 1; 1590 } 1591 ev.mNext = null; 1592 ev.prepareForReuse(); 1593 return ev; 1594 } 1595 1596 /** 1597 * Create a new MotionEvent, filling in all of the basic values that 1598 * define the motion. 1599 * 1600 * @param downTime The time (in ms) when the user originally pressed down to start 1601 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 1602 * @param eventTime The the time (in ms) when this specific event was generated. This 1603 * must be obtained from {@link SystemClock#uptimeMillis()}. 1604 * @param action The kind of action being performed, such as {@link #ACTION_DOWN}. 1605 * @param pointerCount The number of pointers that will be in this event. 1606 * @param pointerProperties An array of <em>pointerCount</em> values providing 1607 * a {@link PointerProperties} property object for each pointer, which must 1608 * include the pointer identifier. 1609 * @param pointerCoords An array of <em>pointerCount</em> values providing 1610 * a {@link PointerCoords} coordinate object for each pointer. 1611 * @param metaState The state of any meta / modifier keys that were in effect when 1612 * the event was generated. 1613 * @param buttonState The state of buttons that are pressed. 1614 * @param xPrecision The precision of the X coordinate being reported. 1615 * @param yPrecision The precision of the Y coordinate being reported. 1616 * @param deviceId The id for the device that this event came from. An id of 1617 * zero indicates that the event didn't come from a physical device; other 1618 * numbers are arbitrary and you shouldn't depend on the values. 1619 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 1620 * MotionEvent. 1621 * @param source The source of this event. 1622 * @param flags The motion event flags. 1623 */ 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)1624 static public MotionEvent obtain(long downTime, long eventTime, 1625 int action, int pointerCount, PointerProperties[] pointerProperties, 1626 PointerCoords[] pointerCoords, int metaState, int buttonState, 1627 float xPrecision, float yPrecision, int deviceId, 1628 int edgeFlags, int source, int flags) { 1629 MotionEvent ev = obtain(); 1630 ev.mNativePtr = nativeInitialize(ev.mNativePtr, 1631 deviceId, source, action, flags, edgeFlags, metaState, buttonState, 1632 0, 0, xPrecision, yPrecision, 1633 downTime * NS_PER_MS, eventTime * NS_PER_MS, 1634 pointerCount, pointerProperties, pointerCoords); 1635 return ev; 1636 } 1637 1638 /** 1639 * Create a new MotionEvent, filling in all of the basic values that 1640 * define the motion. 1641 * 1642 * @param downTime The time (in ms) when the user originally pressed down to start 1643 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 1644 * @param eventTime The the time (in ms) when this specific event was generated. This 1645 * must be obtained from {@link SystemClock#uptimeMillis()}. 1646 * @param action The kind of action being performed, such as {@link #ACTION_DOWN}. 1647 * @param pointerCount The number of pointers that will be in this event. 1648 * @param pointerIds An array of <em>pointerCount</em> values providing 1649 * an identifier for each pointer. 1650 * @param pointerCoords An array of <em>pointerCount</em> values providing 1651 * a {@link PointerCoords} coordinate object for each pointer. 1652 * @param metaState The state of any meta / modifier keys that were in effect when 1653 * the event was generated. 1654 * @param xPrecision The precision of the X coordinate being reported. 1655 * @param yPrecision The precision of the Y coordinate being reported. 1656 * @param deviceId The id for the device that this event came from. An id of 1657 * zero indicates that the event didn't come from a physical device; other 1658 * numbers are arbitrary and you shouldn't depend on the values. 1659 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 1660 * MotionEvent. 1661 * @param source The source of this event. 1662 * @param flags The motion event flags. 1663 * 1664 * @deprecated Use {@link #obtain(long, long, int, int, PointerProperties[], PointerCoords[], int, int, float, float, int, int, int, int)} 1665 * instead. 1666 */ 1667 @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)1668 static public MotionEvent obtain(long downTime, long eventTime, 1669 int action, int pointerCount, int[] pointerIds, PointerCoords[] pointerCoords, 1670 int metaState, float xPrecision, float yPrecision, int deviceId, 1671 int edgeFlags, int source, int flags) { 1672 synchronized (gSharedTempLock) { 1673 ensureSharedTempPointerCapacity(pointerCount); 1674 final PointerProperties[] pp = gSharedTempPointerProperties; 1675 for (int i = 0; i < pointerCount; i++) { 1676 pp[i].clear(); 1677 pp[i].id = pointerIds[i]; 1678 } 1679 return obtain(downTime, eventTime, action, pointerCount, pp, 1680 pointerCoords, metaState, 0, xPrecision, yPrecision, deviceId, 1681 edgeFlags, source, flags); 1682 } 1683 } 1684 1685 /** 1686 * Create a new MotionEvent, filling in all of the basic values that 1687 * define the motion. 1688 * 1689 * @param downTime The time (in ms) when the user originally pressed down to start 1690 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 1691 * @param eventTime The the time (in ms) when this specific event was generated. This 1692 * must be obtained from {@link SystemClock#uptimeMillis()}. 1693 * @param action The kind of action being performed, such as {@link #ACTION_DOWN}. 1694 * @param x The X coordinate of this event. 1695 * @param y The Y coordinate of this event. 1696 * @param pressure The current pressure of this event. The pressure generally 1697 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 1698 * values higher than 1 may be generated depending on the calibration of 1699 * the input device. 1700 * @param size A scaled value of the approximate size of the area being pressed when 1701 * touched with the finger. The actual value in pixels corresponding to the finger 1702 * touch is normalized with a device specific range of values 1703 * and scaled to a value between 0 and 1. 1704 * @param metaState The state of any meta / modifier keys that were in effect when 1705 * the event was generated. 1706 * @param xPrecision The precision of the X coordinate being reported. 1707 * @param yPrecision The precision of the Y coordinate being reported. 1708 * @param deviceId The id for the device that this event came from. An id of 1709 * zero indicates that the event didn't come from a physical device; other 1710 * numbers are arbitrary and you shouldn't depend on the values. 1711 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 1712 * MotionEvent. 1713 */ 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)1714 static public MotionEvent obtain(long downTime, long eventTime, int action, 1715 float x, float y, float pressure, float size, int metaState, 1716 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 1717 MotionEvent ev = obtain(); 1718 synchronized (gSharedTempLock) { 1719 ensureSharedTempPointerCapacity(1); 1720 final PointerProperties[] pp = gSharedTempPointerProperties; 1721 pp[0].clear(); 1722 pp[0].id = 0; 1723 1724 final PointerCoords pc[] = gSharedTempPointerCoords; 1725 pc[0].clear(); 1726 pc[0].x = x; 1727 pc[0].y = y; 1728 pc[0].pressure = pressure; 1729 pc[0].size = size; 1730 1731 ev.mNativePtr = nativeInitialize(ev.mNativePtr, 1732 deviceId, InputDevice.SOURCE_UNKNOWN, action, 0, edgeFlags, metaState, 0, 1733 0, 0, xPrecision, yPrecision, 1734 downTime * NS_PER_MS, eventTime * NS_PER_MS, 1735 1, pp, pc); 1736 return ev; 1737 } 1738 } 1739 1740 /** 1741 * Create a new MotionEvent, filling in all of the basic values that 1742 * define the motion. 1743 * 1744 * @param downTime The time (in ms) when the user originally pressed down to start 1745 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 1746 * @param eventTime The the time (in ms) when this specific event was generated. This 1747 * must be obtained from {@link SystemClock#uptimeMillis()}. 1748 * @param action The kind of action being performed, such as {@link #ACTION_DOWN}. 1749 * @param pointerCount The number of pointers that are active in this event. 1750 * @param x The X coordinate of this event. 1751 * @param y The Y coordinate of this event. 1752 * @param pressure The current pressure of this event. The pressure generally 1753 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 1754 * values higher than 1 may be generated depending on the calibration of 1755 * the input device. 1756 * @param size A scaled value of the approximate size of the area being pressed when 1757 * touched with the finger. The actual value in pixels corresponding to the finger 1758 * touch is normalized with a device specific range of values 1759 * and scaled to a value between 0 and 1. 1760 * @param metaState The state of any meta / modifier keys that were in effect when 1761 * the event was generated. 1762 * @param xPrecision The precision of the X coordinate being reported. 1763 * @param yPrecision The precision of the Y coordinate being reported. 1764 * @param deviceId The id for the device that this event came from. An id of 1765 * zero indicates that the event didn't come from a physical device; other 1766 * numbers are arbitrary and you shouldn't depend on the values. 1767 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 1768 * MotionEvent. 1769 * 1770 * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)} 1771 * instead. 1772 */ 1773 @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)1774 static public MotionEvent obtain(long downTime, long eventTime, int action, 1775 int pointerCount, float x, float y, float pressure, float size, int metaState, 1776 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 1777 return obtain(downTime, eventTime, action, x, y, pressure, size, 1778 metaState, xPrecision, yPrecision, deviceId, edgeFlags); 1779 } 1780 1781 /** 1782 * Create a new MotionEvent, filling in a subset of the basic motion 1783 * values. Those not specified here are: device id (always 0), pressure 1784 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0). 1785 * 1786 * @param downTime The time (in ms) when the user originally pressed down to start 1787 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 1788 * @param eventTime The the time (in ms) when this specific event was generated. This 1789 * must be obtained from {@link SystemClock#uptimeMillis()}. 1790 * @param action The kind of action being performed, such as {@link #ACTION_DOWN}. 1791 * @param x The X coordinate of this event. 1792 * @param y The Y coordinate of this event. 1793 * @param metaState The state of any meta / modifier keys that were in effect when 1794 * the event was generated. 1795 */ obtain(long downTime, long eventTime, int action, float x, float y, int metaState)1796 static public MotionEvent obtain(long downTime, long eventTime, int action, 1797 float x, float y, int metaState) { 1798 return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f, 1799 metaState, 1.0f, 1.0f, 0, 0); 1800 } 1801 1802 /** 1803 * Create a new MotionEvent, copying from an existing one. 1804 */ obtain(MotionEvent other)1805 static public MotionEvent obtain(MotionEvent other) { 1806 if (other == null) { 1807 throw new IllegalArgumentException("other motion event must not be null"); 1808 } 1809 1810 MotionEvent ev = obtain(); 1811 ev.mNativePtr = nativeCopy(ev.mNativePtr, other.mNativePtr, true /*keepHistory*/); 1812 return ev; 1813 } 1814 1815 /** 1816 * Create a new MotionEvent, copying from an existing one, but not including 1817 * any historical point information. 1818 */ obtainNoHistory(MotionEvent other)1819 static public MotionEvent obtainNoHistory(MotionEvent other) { 1820 if (other == null) { 1821 throw new IllegalArgumentException("other motion event must not be null"); 1822 } 1823 1824 MotionEvent ev = obtain(); 1825 ev.mNativePtr = nativeCopy(ev.mNativePtr, other.mNativePtr, false /*keepHistory*/); 1826 return ev; 1827 } 1828 1829 /** @hide */ 1830 @Override copy()1831 public MotionEvent copy() { 1832 return obtain(this); 1833 } 1834 1835 /** 1836 * Recycle the MotionEvent, to be re-used by a later caller. After calling 1837 * this function you must not ever touch the event again. 1838 */ 1839 @Override recycle()1840 public final void recycle() { 1841 super.recycle(); 1842 1843 synchronized (gRecyclerLock) { 1844 if (gRecyclerUsed < MAX_RECYCLED) { 1845 gRecyclerUsed++; 1846 mNext = gRecyclerTop; 1847 gRecyclerTop = this; 1848 } 1849 } 1850 } 1851 1852 /** 1853 * Applies a scale factor to all points within this event. 1854 * 1855 * This method is used to adjust touch events to simulate different density 1856 * displays for compatibility mode. The values returned by {@link #getRawX()}, 1857 * {@link #getRawY()}, {@link #getXPrecision()} and {@link #getYPrecision()} 1858 * are also affected by the scale factor. 1859 * 1860 * @param scale The scale factor to apply. 1861 * @hide 1862 */ scale(float scale)1863 public final void scale(float scale) { 1864 if (scale != 1.0f) { 1865 nativeScale(mNativePtr, scale); 1866 } 1867 } 1868 1869 /** {@inheritDoc} */ 1870 @Override getDeviceId()1871 public final int getDeviceId() { 1872 return nativeGetDeviceId(mNativePtr); 1873 } 1874 1875 /** {@inheritDoc} */ 1876 @Override getSource()1877 public final int getSource() { 1878 return nativeGetSource(mNativePtr); 1879 } 1880 1881 /** {@inheritDoc} */ 1882 @Override setSource(int source)1883 public final void setSource(int source) { 1884 nativeSetSource(mNativePtr, source); 1885 } 1886 1887 /** 1888 * Return the kind of action being performed. 1889 * Consider using {@link #getActionMasked} and {@link #getActionIndex} to retrieve 1890 * the separate masked action and pointer index. 1891 * @return The action, such as {@link #ACTION_DOWN} or 1892 * the combination of {@link #ACTION_POINTER_DOWN} with a shifted pointer index. 1893 */ getAction()1894 public final int getAction() { 1895 return nativeGetAction(mNativePtr); 1896 } 1897 1898 /** 1899 * Return the masked action being performed, without pointer index information. 1900 * Use {@link #getActionIndex} to return the index associated with pointer actions. 1901 * @return The action, such as {@link #ACTION_DOWN} or {@link #ACTION_POINTER_DOWN}. 1902 */ getActionMasked()1903 public final int getActionMasked() { 1904 return nativeGetAction(mNativePtr) & ACTION_MASK; 1905 } 1906 1907 /** 1908 * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} 1909 * as returned by {@link #getActionMasked}, this returns the associated 1910 * pointer index. 1911 * The index may be used with {@link #getPointerId(int)}, 1912 * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)}, 1913 * and {@link #getSize(int)} to get information about the pointer that has 1914 * gone down or up. 1915 * @return The index associated with the action. 1916 */ getActionIndex()1917 public final int getActionIndex() { 1918 return (nativeGetAction(mNativePtr) & ACTION_POINTER_INDEX_MASK) 1919 >> ACTION_POINTER_INDEX_SHIFT; 1920 } 1921 1922 /** 1923 * Returns true if this motion event is a touch event. 1924 * <p> 1925 * Specifically excludes pointer events with action {@link #ACTION_HOVER_MOVE}, 1926 * {@link #ACTION_HOVER_ENTER}, {@link #ACTION_HOVER_EXIT}, or {@link #ACTION_SCROLL} 1927 * because they are not actually touch events (the pointer is not down). 1928 * </p> 1929 * @return True if this motion event is a touch event. 1930 * @hide 1931 */ isTouchEvent()1932 public final boolean isTouchEvent() { 1933 return nativeIsTouchEvent(mNativePtr); 1934 } 1935 1936 /** 1937 * Gets the motion event flags. 1938 * 1939 * @see #FLAG_WINDOW_IS_OBSCURED 1940 */ getFlags()1941 public final int getFlags() { 1942 return nativeGetFlags(mNativePtr); 1943 } 1944 1945 /** @hide */ 1946 @Override isTainted()1947 public final boolean isTainted() { 1948 final int flags = getFlags(); 1949 return (flags & FLAG_TAINTED) != 0; 1950 } 1951 1952 /** @hide */ 1953 @Override setTainted(boolean tainted)1954 public final void setTainted(boolean tainted) { 1955 final int flags = getFlags(); 1956 nativeSetFlags(mNativePtr, tainted ? flags | FLAG_TAINTED : flags & ~FLAG_TAINTED); 1957 } 1958 1959 /** @hide */ isTargetAccessibilityFocus()1960 public final boolean isTargetAccessibilityFocus() { 1961 final int flags = getFlags(); 1962 return (flags & FLAG_TARGET_ACCESSIBILITY_FOCUS) != 0; 1963 } 1964 1965 /** @hide */ setTargetAccessibilityFocus(boolean targetsFocus)1966 public final void setTargetAccessibilityFocus(boolean targetsFocus) { 1967 final int flags = getFlags(); 1968 nativeSetFlags(mNativePtr, targetsFocus 1969 ? flags | FLAG_TARGET_ACCESSIBILITY_FOCUS 1970 : flags & ~FLAG_TARGET_ACCESSIBILITY_FOCUS); 1971 } 1972 1973 /** @hide */ isHoverExitPending()1974 public final boolean isHoverExitPending() { 1975 final int flags = getFlags(); 1976 return (flags & FLAG_HOVER_EXIT_PENDING) != 0; 1977 } 1978 1979 /** @hide */ setHoverExitPending(boolean hoverExitPending)1980 public void setHoverExitPending(boolean hoverExitPending) { 1981 final int flags = getFlags(); 1982 nativeSetFlags(mNativePtr, hoverExitPending 1983 ? flags | FLAG_HOVER_EXIT_PENDING 1984 : flags & ~FLAG_HOVER_EXIT_PENDING); 1985 } 1986 1987 /** 1988 * Returns the time (in ms) when the user originally pressed down to start 1989 * a stream of position events. 1990 */ getDownTime()1991 public final long getDownTime() { 1992 return nativeGetDownTimeNanos(mNativePtr) / NS_PER_MS; 1993 } 1994 1995 /** 1996 * Sets the time (in ms) when the user originally pressed down to start 1997 * a stream of position events. 1998 * 1999 * @hide 2000 */ setDownTime(long downTime)2001 public final void setDownTime(long downTime) { 2002 nativeSetDownTimeNanos(mNativePtr, downTime * NS_PER_MS); 2003 } 2004 2005 /** 2006 * Retrieve the time this event occurred, 2007 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2008 * 2009 * @return Returns the time this event occurred, 2010 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2011 */ 2012 @Override getEventTime()2013 public final long getEventTime() { 2014 return nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT) / NS_PER_MS; 2015 } 2016 2017 /** 2018 * Retrieve the time this event occurred, 2019 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2020 * nanosecond precision. 2021 * <p> 2022 * The value is in nanosecond precision but it may not have nanosecond accuracy. 2023 * </p> 2024 * 2025 * @return Returns the time this event occurred, 2026 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2027 * nanosecond precision. 2028 * 2029 * @hide 2030 */ 2031 @Override getEventTimeNano()2032 public final long getEventTimeNano() { 2033 return nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT); 2034 } 2035 2036 /** 2037 * {@link #getX(int)} for the first pointer index (may be an 2038 * arbitrary pointer identifier). 2039 * 2040 * @see #AXIS_X 2041 */ getX()2042 public final float getX() { 2043 return nativeGetAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT); 2044 } 2045 2046 /** 2047 * {@link #getY(int)} for the first pointer index (may be an 2048 * arbitrary pointer identifier). 2049 * 2050 * @see #AXIS_Y 2051 */ getY()2052 public final float getY() { 2053 return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT); 2054 } 2055 2056 /** 2057 * {@link #getPressure(int)} for the first pointer index (may be an 2058 * arbitrary pointer identifier). 2059 * 2060 * @see #AXIS_PRESSURE 2061 */ getPressure()2062 public final float getPressure() { 2063 return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, 0, HISTORY_CURRENT); 2064 } 2065 2066 /** 2067 * {@link #getSize(int)} for the first pointer index (may be an 2068 * arbitrary pointer identifier). 2069 * 2070 * @see #AXIS_SIZE 2071 */ getSize()2072 public final float getSize() { 2073 return nativeGetAxisValue(mNativePtr, AXIS_SIZE, 0, HISTORY_CURRENT); 2074 } 2075 2076 /** 2077 * {@link #getTouchMajor(int)} for the first pointer index (may be an 2078 * arbitrary pointer identifier). 2079 * 2080 * @see #AXIS_TOUCH_MAJOR 2081 */ getTouchMajor()2082 public final float getTouchMajor() { 2083 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, 0, HISTORY_CURRENT); 2084 } 2085 2086 /** 2087 * {@link #getTouchMinor(int)} for the first pointer index (may be an 2088 * arbitrary pointer identifier). 2089 * 2090 * @see #AXIS_TOUCH_MINOR 2091 */ getTouchMinor()2092 public final float getTouchMinor() { 2093 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, 0, HISTORY_CURRENT); 2094 } 2095 2096 /** 2097 * {@link #getToolMajor(int)} for the first pointer index (may be an 2098 * arbitrary pointer identifier). 2099 * 2100 * @see #AXIS_TOOL_MAJOR 2101 */ getToolMajor()2102 public final float getToolMajor() { 2103 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, 0, HISTORY_CURRENT); 2104 } 2105 2106 /** 2107 * {@link #getToolMinor(int)} for the first pointer index (may be an 2108 * arbitrary pointer identifier). 2109 * 2110 * @see #AXIS_TOOL_MINOR 2111 */ getToolMinor()2112 public final float getToolMinor() { 2113 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, 0, HISTORY_CURRENT); 2114 } 2115 2116 /** 2117 * {@link #getOrientation(int)} for the first pointer index (may be an 2118 * arbitrary pointer identifier). 2119 * 2120 * @see #AXIS_ORIENTATION 2121 */ getOrientation()2122 public final float getOrientation() { 2123 return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, 0, HISTORY_CURRENT); 2124 } 2125 2126 /** 2127 * {@link #getAxisValue(int)} for the first pointer index (may be an 2128 * arbitrary pointer identifier). 2129 * 2130 * @param axis The axis identifier for the axis value to retrieve. 2131 * 2132 * @see #AXIS_X 2133 * @see #AXIS_Y 2134 */ getAxisValue(int axis)2135 public final float getAxisValue(int axis) { 2136 return nativeGetAxisValue(mNativePtr, axis, 0, HISTORY_CURRENT); 2137 } 2138 2139 /** 2140 * The number of pointers of data contained in this event. Always 2141 * >= 1. 2142 */ getPointerCount()2143 public final int getPointerCount() { 2144 return nativeGetPointerCount(mNativePtr); 2145 } 2146 2147 /** 2148 * Return the pointer identifier associated with a particular pointer 2149 * data index in this event. The identifier tells you the actual pointer 2150 * number associated with the data, accounting for individual pointers 2151 * going up and down since the start of the current gesture. 2152 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2153 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2154 */ getPointerId(int pointerIndex)2155 public final int getPointerId(int pointerIndex) { 2156 return nativeGetPointerId(mNativePtr, pointerIndex); 2157 } 2158 2159 /** 2160 * Gets the tool type of a pointer for the given pointer index. 2161 * The tool type indicates the type of tool used to make contact such 2162 * as a finger or stylus, if known. 2163 * 2164 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2165 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2166 * @return The tool type of the pointer. 2167 * 2168 * @see #TOOL_TYPE_UNKNOWN 2169 * @see #TOOL_TYPE_FINGER 2170 * @see #TOOL_TYPE_STYLUS 2171 * @see #TOOL_TYPE_MOUSE 2172 */ getToolType(int pointerIndex)2173 public final int getToolType(int pointerIndex) { 2174 return nativeGetToolType(mNativePtr, pointerIndex); 2175 } 2176 2177 /** 2178 * Given a pointer identifier, find the index of its data in the event. 2179 * 2180 * @param pointerId The identifier of the pointer to be found. 2181 * @return Returns either the index of the pointer (for use with 2182 * {@link #getX(int)} et al.), or -1 if there is no data available for 2183 * that pointer identifier. 2184 */ findPointerIndex(int pointerId)2185 public final int findPointerIndex(int pointerId) { 2186 return nativeFindPointerIndex(mNativePtr, pointerId); 2187 } 2188 2189 /** 2190 * Returns the X coordinate of this event for the given pointer 2191 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2192 * identifier for this index). 2193 * Whole numbers are pixels; the 2194 * value may have a fraction for input devices that are sub-pixel precise. 2195 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2196 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2197 * 2198 * @see #AXIS_X 2199 */ getX(int pointerIndex)2200 public final float getX(int pointerIndex) { 2201 return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, HISTORY_CURRENT); 2202 } 2203 2204 /** 2205 * Returns the Y coordinate of this event for the given pointer 2206 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2207 * identifier for this index). 2208 * Whole numbers are pixels; the 2209 * value may have a fraction for input devices that are sub-pixel precise. 2210 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2211 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2212 * 2213 * @see #AXIS_Y 2214 */ getY(int pointerIndex)2215 public final float getY(int pointerIndex) { 2216 return nativeGetAxisValue(mNativePtr, AXIS_Y, pointerIndex, HISTORY_CURRENT); 2217 } 2218 2219 /** 2220 * Returns the current pressure of this event for the given pointer 2221 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2222 * identifier for this index). 2223 * The pressure generally 2224 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 2225 * values higher than 1 may be generated depending on the calibration of 2226 * the input device. 2227 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2228 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2229 * 2230 * @see #AXIS_PRESSURE 2231 */ getPressure(int pointerIndex)2232 public final float getPressure(int pointerIndex) { 2233 return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, pointerIndex, HISTORY_CURRENT); 2234 } 2235 2236 /** 2237 * Returns a scaled value of the approximate size for the given pointer 2238 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2239 * identifier for this index). 2240 * This represents some approximation of the area of the screen being 2241 * pressed; the actual value in pixels corresponding to the 2242 * touch is normalized with the device specific range of values 2243 * and scaled to a value between 0 and 1. The value of size can be used to 2244 * determine fat touch events. 2245 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2246 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2247 * 2248 * @see #AXIS_SIZE 2249 */ getSize(int pointerIndex)2250 public final float getSize(int pointerIndex) { 2251 return nativeGetAxisValue(mNativePtr, AXIS_SIZE, pointerIndex, HISTORY_CURRENT); 2252 } 2253 2254 /** 2255 * Returns the length of the major axis of an ellipse that describes the touch 2256 * area at the point of contact for the given pointer 2257 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2258 * identifier for this index). 2259 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2260 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2261 * 2262 * @see #AXIS_TOUCH_MAJOR 2263 */ getTouchMajor(int pointerIndex)2264 public final float getTouchMajor(int pointerIndex) { 2265 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, pointerIndex, HISTORY_CURRENT); 2266 } 2267 2268 /** 2269 * Returns the length of the minor axis of an ellipse that describes the touch 2270 * area at the point of contact for the given pointer 2271 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2272 * identifier for this index). 2273 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2274 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2275 * 2276 * @see #AXIS_TOUCH_MINOR 2277 */ getTouchMinor(int pointerIndex)2278 public final float getTouchMinor(int pointerIndex) { 2279 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, pointerIndex, HISTORY_CURRENT); 2280 } 2281 2282 /** 2283 * Returns the length of the major axis of an ellipse that describes the size of 2284 * the approaching tool for the given pointer 2285 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2286 * identifier for this index). 2287 * The tool area represents the estimated size of the finger or pen that is 2288 * touching the device independent of its actual touch area at the point of contact. 2289 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2290 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2291 * 2292 * @see #AXIS_TOOL_MAJOR 2293 */ getToolMajor(int pointerIndex)2294 public final float getToolMajor(int pointerIndex) { 2295 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, pointerIndex, HISTORY_CURRENT); 2296 } 2297 2298 /** 2299 * Returns the length of the minor axis of an ellipse that describes the size of 2300 * the approaching tool for the given pointer 2301 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2302 * identifier for this index). 2303 * The tool area represents the estimated size of the finger or pen that is 2304 * touching the device independent of its actual touch area at the point of contact. 2305 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2306 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2307 * 2308 * @see #AXIS_TOOL_MINOR 2309 */ getToolMinor(int pointerIndex)2310 public final float getToolMinor(int pointerIndex) { 2311 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, pointerIndex, HISTORY_CURRENT); 2312 } 2313 2314 /** 2315 * Returns the orientation of the touch area and tool area in radians clockwise from vertical 2316 * for the given pointer <em>index</em> (use {@link #getPointerId(int)} to find the pointer 2317 * identifier for this index). 2318 * An angle of 0 radians indicates that the major axis of contact is oriented 2319 * upwards, is perfectly circular or is of unknown orientation. A positive angle 2320 * indicates that the major axis of contact is oriented to the right. A negative angle 2321 * indicates that the major axis of contact is oriented to the left. 2322 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians 2323 * (finger pointing fully right). 2324 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2325 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2326 * 2327 * @see #AXIS_ORIENTATION 2328 */ getOrientation(int pointerIndex)2329 public final float getOrientation(int pointerIndex) { 2330 return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, pointerIndex, HISTORY_CURRENT); 2331 } 2332 2333 /** 2334 * Returns the value of the requested axis for the given pointer <em>index</em> 2335 * (use {@link #getPointerId(int)} to find the pointer identifier for this index). 2336 * 2337 * @param axis The axis identifier for the axis value to retrieve. 2338 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2339 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2340 * @return The value of the axis, or 0 if the axis is not available. 2341 * 2342 * @see #AXIS_X 2343 * @see #AXIS_Y 2344 */ getAxisValue(int axis, int pointerIndex)2345 public final float getAxisValue(int axis, int pointerIndex) { 2346 return nativeGetAxisValue(mNativePtr, axis, pointerIndex, HISTORY_CURRENT); 2347 } 2348 2349 /** 2350 * Populates a {@link PointerCoords} object with pointer coordinate data for 2351 * the specified pointer index. 2352 * 2353 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2354 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2355 * @param outPointerCoords The pointer coordinate object to populate. 2356 * 2357 * @see PointerCoords 2358 */ getPointerCoords(int pointerIndex, PointerCoords outPointerCoords)2359 public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) { 2360 nativeGetPointerCoords(mNativePtr, pointerIndex, HISTORY_CURRENT, outPointerCoords); 2361 } 2362 2363 /** 2364 * Populates a {@link PointerProperties} object with pointer properties for 2365 * the specified pointer index. 2366 * 2367 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2368 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2369 * @param outPointerProperties The pointer properties object to populate. 2370 * 2371 * @see PointerProperties 2372 */ getPointerProperties(int pointerIndex, PointerProperties outPointerProperties)2373 public final void getPointerProperties(int pointerIndex, 2374 PointerProperties outPointerProperties) { 2375 nativeGetPointerProperties(mNativePtr, pointerIndex, outPointerProperties); 2376 } 2377 2378 /** 2379 * Returns the state of any meta / modifier keys that were in effect when 2380 * the event was generated. This is the same values as those 2381 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}. 2382 * 2383 * @return an integer in which each bit set to 1 represents a pressed 2384 * meta key 2385 * 2386 * @see KeyEvent#getMetaState() 2387 */ getMetaState()2388 public final int getMetaState() { 2389 return nativeGetMetaState(mNativePtr); 2390 } 2391 2392 /** 2393 * Gets the state of all buttons that are pressed such as a mouse or stylus button. 2394 * 2395 * @return The button state. 2396 * 2397 * @see #BUTTON_PRIMARY 2398 * @see #BUTTON_SECONDARY 2399 * @see #BUTTON_TERTIARY 2400 * @see #BUTTON_FORWARD 2401 * @see #BUTTON_BACK 2402 * @see #BUTTON_STYLUS_PRIMARY 2403 * @see #BUTTON_STYLUS_SECONDARY 2404 */ getButtonState()2405 public final int getButtonState() { 2406 return nativeGetButtonState(mNativePtr); 2407 } 2408 2409 /** 2410 * Sets the bitfield indicating which buttons are pressed. 2411 * 2412 * @see #getButtonState() 2413 * @hide 2414 */ 2415 @TestApi setButtonState(int buttonState)2416 public final void setButtonState(int buttonState) { 2417 nativeSetButtonState(mNativePtr, buttonState); 2418 } 2419 2420 /** 2421 * Gets which button has been modified during a press or release action. 2422 * 2423 * For actions other than {@link #ACTION_BUTTON_PRESS} and {@link #ACTION_BUTTON_RELEASE} 2424 * the returned value is undefined. 2425 * 2426 * @see #getButtonState() 2427 */ getActionButton()2428 public final int getActionButton() { 2429 return nativeGetActionButton(mNativePtr); 2430 } 2431 2432 /** 2433 * Sets the action button for the event. 2434 * 2435 * @see #getActionButton() 2436 * @hide 2437 */ 2438 @TestApi setActionButton(int button)2439 public final void setActionButton(int button) { 2440 nativeSetActionButton(mNativePtr, button); 2441 } 2442 2443 /** 2444 * Returns the original raw X coordinate of this event. For touch 2445 * events on the screen, this is the original location of the event 2446 * on the screen, before it had been adjusted for the containing window 2447 * and views. 2448 * 2449 * @see #getX(int) 2450 * @see #AXIS_X 2451 */ getRawX()2452 public final float getRawX() { 2453 return nativeGetRawAxisValue(mNativePtr, AXIS_X, 0, HISTORY_CURRENT); 2454 } 2455 2456 /** 2457 * Returns the original raw Y coordinate of this event. For touch 2458 * events on the screen, this is the original location of the event 2459 * on the screen, before it had been adjusted for the containing window 2460 * and views. 2461 * 2462 * @see #getY(int) 2463 * @see #AXIS_Y 2464 */ getRawY()2465 public final float getRawY() { 2466 return nativeGetRawAxisValue(mNativePtr, AXIS_Y, 0, HISTORY_CURRENT); 2467 } 2468 2469 /** 2470 * Return the precision of the X coordinates being reported. You can 2471 * multiply this number with {@link #getX} to find the actual hardware 2472 * value of the X coordinate. 2473 * @return Returns the precision of X coordinates being reported. 2474 * 2475 * @see #AXIS_X 2476 */ getXPrecision()2477 public final float getXPrecision() { 2478 return nativeGetXPrecision(mNativePtr); 2479 } 2480 2481 /** 2482 * Return the precision of the Y coordinates being reported. You can 2483 * multiply this number with {@link #getY} to find the actual hardware 2484 * value of the Y coordinate. 2485 * @return Returns the precision of Y coordinates being reported. 2486 * 2487 * @see #AXIS_Y 2488 */ getYPrecision()2489 public final float getYPrecision() { 2490 return nativeGetYPrecision(mNativePtr); 2491 } 2492 2493 /** 2494 * Returns the number of historical points in this event. These are 2495 * movements that have occurred between this event and the previous event. 2496 * This only applies to ACTION_MOVE events -- all other actions will have 2497 * a size of 0. 2498 * 2499 * @return Returns the number of historical points in the event. 2500 */ getHistorySize()2501 public final int getHistorySize() { 2502 return nativeGetHistorySize(mNativePtr); 2503 } 2504 2505 /** 2506 * Returns the time that a historical movement occurred between this event 2507 * and the previous event, in the {@link android.os.SystemClock#uptimeMillis} time base. 2508 * <p> 2509 * This only applies to ACTION_MOVE events. 2510 * </p> 2511 * 2512 * @param pos Which historical value to return; must be less than 2513 * {@link #getHistorySize} 2514 * @return Returns the time that a historical movement occurred between this 2515 * event and the previous event, 2516 * in the {@link android.os.SystemClock#uptimeMillis} time base. 2517 * 2518 * @see #getHistorySize 2519 * @see #getEventTime 2520 */ getHistoricalEventTime(int pos)2521 public final long getHistoricalEventTime(int pos) { 2522 return nativeGetEventTimeNanos(mNativePtr, pos) / NS_PER_MS; 2523 } 2524 2525 /** 2526 * Returns the time that a historical movement occurred between this event 2527 * and the previous event, in the {@link android.os.SystemClock#uptimeMillis} time base 2528 * but with nanosecond (instead of millisecond) precision. 2529 * <p> 2530 * This only applies to ACTION_MOVE events. 2531 * </p><p> 2532 * The value is in nanosecond precision but it may not have nanosecond accuracy. 2533 * </p> 2534 * 2535 * @param pos Which historical value to return; must be less than 2536 * {@link #getHistorySize} 2537 * @return Returns the time that a historical movement occurred between this 2538 * event and the previous event, 2539 * in the {@link android.os.SystemClock#uptimeMillis} time base but with 2540 * nanosecond (instead of millisecond) precision. 2541 * 2542 * @see #getHistorySize 2543 * @see #getEventTime 2544 * 2545 * @hide 2546 */ getHistoricalEventTimeNano(int pos)2547 public final long getHistoricalEventTimeNano(int pos) { 2548 return nativeGetEventTimeNanos(mNativePtr, pos); 2549 } 2550 2551 /** 2552 * {@link #getHistoricalX(int, int)} for the first pointer index (may be an 2553 * arbitrary pointer identifier). 2554 * 2555 * @param pos Which historical value to return; must be less than 2556 * {@link #getHistorySize} 2557 * 2558 * @see #getHistorySize 2559 * @see #getX() 2560 * @see #AXIS_X 2561 */ getHistoricalX(int pos)2562 public final float getHistoricalX(int pos) { 2563 return nativeGetAxisValue(mNativePtr, AXIS_X, 0, pos); 2564 } 2565 2566 /** 2567 * {@link #getHistoricalY(int, int)} for the first pointer index (may be an 2568 * arbitrary pointer identifier). 2569 * 2570 * @param pos Which historical value to return; must be less than 2571 * {@link #getHistorySize} 2572 * 2573 * @see #getHistorySize 2574 * @see #getY() 2575 * @see #AXIS_Y 2576 */ getHistoricalY(int pos)2577 public final float getHistoricalY(int pos) { 2578 return nativeGetAxisValue(mNativePtr, AXIS_Y, 0, pos); 2579 } 2580 2581 /** 2582 * {@link #getHistoricalPressure(int, int)} for the first pointer index (may be an 2583 * arbitrary pointer identifier). 2584 * 2585 * @param pos Which historical value to return; must be less than 2586 * {@link #getHistorySize} 2587 * 2588 * @see #getHistorySize 2589 * @see #getPressure() 2590 * @see #AXIS_PRESSURE 2591 */ getHistoricalPressure(int pos)2592 public final float getHistoricalPressure(int pos) { 2593 return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, 0, pos); 2594 } 2595 2596 /** 2597 * {@link #getHistoricalSize(int, int)} for the first pointer index (may be an 2598 * arbitrary pointer identifier). 2599 * 2600 * @param pos Which historical value to return; must be less than 2601 * {@link #getHistorySize} 2602 * 2603 * @see #getHistorySize 2604 * @see #getSize() 2605 * @see #AXIS_SIZE 2606 */ getHistoricalSize(int pos)2607 public final float getHistoricalSize(int pos) { 2608 return nativeGetAxisValue(mNativePtr, AXIS_SIZE, 0, pos); 2609 } 2610 2611 /** 2612 * {@link #getHistoricalTouchMajor(int, int)} for the first pointer index (may be an 2613 * arbitrary pointer identifier). 2614 * 2615 * @param pos Which historical value to return; must be less than 2616 * {@link #getHistorySize} 2617 * 2618 * @see #getHistorySize 2619 * @see #getTouchMajor() 2620 * @see #AXIS_TOUCH_MAJOR 2621 */ getHistoricalTouchMajor(int pos)2622 public final float getHistoricalTouchMajor(int pos) { 2623 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, 0, pos); 2624 } 2625 2626 /** 2627 * {@link #getHistoricalTouchMinor(int, int)} for the first pointer index (may be an 2628 * arbitrary pointer identifier). 2629 * 2630 * @param pos Which historical value to return; must be less than 2631 * {@link #getHistorySize} 2632 * 2633 * @see #getHistorySize 2634 * @see #getTouchMinor() 2635 * @see #AXIS_TOUCH_MINOR 2636 */ getHistoricalTouchMinor(int pos)2637 public final float getHistoricalTouchMinor(int pos) { 2638 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, 0, pos); 2639 } 2640 2641 /** 2642 * {@link #getHistoricalToolMajor(int, int)} for the first pointer index (may be an 2643 * arbitrary pointer identifier). 2644 * 2645 * @param pos Which historical value to return; must be less than 2646 * {@link #getHistorySize} 2647 * 2648 * @see #getHistorySize 2649 * @see #getToolMajor() 2650 * @see #AXIS_TOOL_MAJOR 2651 */ getHistoricalToolMajor(int pos)2652 public final float getHistoricalToolMajor(int pos) { 2653 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, 0, pos); 2654 } 2655 2656 /** 2657 * {@link #getHistoricalToolMinor(int, int)} for the first pointer index (may be an 2658 * arbitrary pointer identifier). 2659 * 2660 * @param pos Which historical value to return; must be less than 2661 * {@link #getHistorySize} 2662 * 2663 * @see #getHistorySize 2664 * @see #getToolMinor() 2665 * @see #AXIS_TOOL_MINOR 2666 */ getHistoricalToolMinor(int pos)2667 public final float getHistoricalToolMinor(int pos) { 2668 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, 0, pos); 2669 } 2670 2671 /** 2672 * {@link #getHistoricalOrientation(int, int)} for the first pointer index (may be an 2673 * arbitrary pointer identifier). 2674 * 2675 * @param pos Which historical value to return; must be less than 2676 * {@link #getHistorySize} 2677 * 2678 * @see #getHistorySize 2679 * @see #getOrientation() 2680 * @see #AXIS_ORIENTATION 2681 */ getHistoricalOrientation(int pos)2682 public final float getHistoricalOrientation(int pos) { 2683 return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, 0, pos); 2684 } 2685 2686 /** 2687 * {@link #getHistoricalAxisValue(int, int, int)} for the first pointer index (may be an 2688 * arbitrary pointer identifier). 2689 * 2690 * @param axis The axis identifier for the axis value to retrieve. 2691 * @param pos Which historical value to return; must be less than 2692 * {@link #getHistorySize} 2693 * 2694 * @see #getHistorySize 2695 * @see #getAxisValue(int) 2696 * @see #AXIS_X 2697 * @see #AXIS_Y 2698 */ getHistoricalAxisValue(int axis, int pos)2699 public final float getHistoricalAxisValue(int axis, int pos) { 2700 return nativeGetAxisValue(mNativePtr, axis, 0, pos); 2701 } 2702 2703 /** 2704 * Returns a historical X coordinate, as per {@link #getX(int)}, that 2705 * occurred between this event and the previous event for the given pointer. 2706 * Only applies to ACTION_MOVE events. 2707 * 2708 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2709 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2710 * @param pos Which historical value to return; must be less than 2711 * {@link #getHistorySize} 2712 * 2713 * @see #getHistorySize 2714 * @see #getX(int) 2715 * @see #AXIS_X 2716 */ getHistoricalX(int pointerIndex, int pos)2717 public final float getHistoricalX(int pointerIndex, int pos) { 2718 return nativeGetAxisValue(mNativePtr, AXIS_X, pointerIndex, pos); 2719 } 2720 2721 /** 2722 * Returns a historical Y coordinate, as per {@link #getY(int)}, that 2723 * occurred between this event and the previous event for the given pointer. 2724 * Only applies to ACTION_MOVE events. 2725 * 2726 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2727 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2728 * @param pos Which historical value to return; must be less than 2729 * {@link #getHistorySize} 2730 * 2731 * @see #getHistorySize 2732 * @see #getY(int) 2733 * @see #AXIS_Y 2734 */ getHistoricalY(int pointerIndex, int pos)2735 public final float getHistoricalY(int pointerIndex, int pos) { 2736 return nativeGetAxisValue(mNativePtr, AXIS_Y, pointerIndex, pos); 2737 } 2738 2739 /** 2740 * Returns a historical pressure coordinate, as per {@link #getPressure(int)}, 2741 * that occurred between this event and the previous event for the given 2742 * pointer. Only applies to ACTION_MOVE events. 2743 * 2744 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2745 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2746 * @param pos Which historical value to return; must be less than 2747 * {@link #getHistorySize} 2748 * 2749 * @see #getHistorySize 2750 * @see #getPressure(int) 2751 * @see #AXIS_PRESSURE 2752 */ getHistoricalPressure(int pointerIndex, int pos)2753 public final float getHistoricalPressure(int pointerIndex, int pos) { 2754 return nativeGetAxisValue(mNativePtr, AXIS_PRESSURE, pointerIndex, pos); 2755 } 2756 2757 /** 2758 * Returns a historical size coordinate, as per {@link #getSize(int)}, that 2759 * occurred between this event and the previous event for the given pointer. 2760 * Only applies to ACTION_MOVE events. 2761 * 2762 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2763 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2764 * @param pos Which historical value to return; must be less than 2765 * {@link #getHistorySize} 2766 * 2767 * @see #getHistorySize 2768 * @see #getSize(int) 2769 * @see #AXIS_SIZE 2770 */ getHistoricalSize(int pointerIndex, int pos)2771 public final float getHistoricalSize(int pointerIndex, int pos) { 2772 return nativeGetAxisValue(mNativePtr, AXIS_SIZE, pointerIndex, pos); 2773 } 2774 2775 /** 2776 * Returns a historical touch major axis coordinate, as per {@link #getTouchMajor(int)}, that 2777 * occurred between this event and the previous event for the given pointer. 2778 * Only applies to ACTION_MOVE events. 2779 * 2780 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2781 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2782 * @param pos Which historical value to return; must be less than 2783 * {@link #getHistorySize} 2784 * 2785 * @see #getHistorySize 2786 * @see #getTouchMajor(int) 2787 * @see #AXIS_TOUCH_MAJOR 2788 */ getHistoricalTouchMajor(int pointerIndex, int pos)2789 public final float getHistoricalTouchMajor(int pointerIndex, int pos) { 2790 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MAJOR, pointerIndex, pos); 2791 } 2792 2793 /** 2794 * Returns a historical touch minor axis coordinate, as per {@link #getTouchMinor(int)}, that 2795 * occurred between this event and the previous event for the given pointer. 2796 * Only applies to ACTION_MOVE events. 2797 * 2798 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2799 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2800 * @param pos Which historical value to return; must be less than 2801 * {@link #getHistorySize} 2802 * 2803 * @see #getHistorySize 2804 * @see #getTouchMinor(int) 2805 * @see #AXIS_TOUCH_MINOR 2806 */ getHistoricalTouchMinor(int pointerIndex, int pos)2807 public final float getHistoricalTouchMinor(int pointerIndex, int pos) { 2808 return nativeGetAxisValue(mNativePtr, AXIS_TOUCH_MINOR, pointerIndex, pos); 2809 } 2810 2811 /** 2812 * Returns a historical tool major axis coordinate, as per {@link #getToolMajor(int)}, that 2813 * occurred between this event and the previous event for the given pointer. 2814 * Only applies to ACTION_MOVE events. 2815 * 2816 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2817 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2818 * @param pos Which historical value to return; must be less than 2819 * {@link #getHistorySize} 2820 * 2821 * @see #getHistorySize 2822 * @see #getToolMajor(int) 2823 * @see #AXIS_TOOL_MAJOR 2824 */ getHistoricalToolMajor(int pointerIndex, int pos)2825 public final float getHistoricalToolMajor(int pointerIndex, int pos) { 2826 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MAJOR, pointerIndex, pos); 2827 } 2828 2829 /** 2830 * Returns a historical tool minor axis coordinate, as per {@link #getToolMinor(int)}, that 2831 * occurred between this event and the previous event for the given pointer. 2832 * Only applies to ACTION_MOVE events. 2833 * 2834 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2835 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2836 * @param pos Which historical value to return; must be less than 2837 * {@link #getHistorySize} 2838 * 2839 * @see #getHistorySize 2840 * @see #getToolMinor(int) 2841 * @see #AXIS_TOOL_MINOR 2842 */ getHistoricalToolMinor(int pointerIndex, int pos)2843 public final float getHistoricalToolMinor(int pointerIndex, int pos) { 2844 return nativeGetAxisValue(mNativePtr, AXIS_TOOL_MINOR, pointerIndex, pos); 2845 } 2846 2847 /** 2848 * Returns a historical orientation coordinate, as per {@link #getOrientation(int)}, that 2849 * occurred between this event and the previous event for the given pointer. 2850 * Only applies to ACTION_MOVE events. 2851 * 2852 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2853 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2854 * @param pos Which historical value to return; must be less than 2855 * {@link #getHistorySize} 2856 * 2857 * @see #getHistorySize 2858 * @see #getOrientation(int) 2859 * @see #AXIS_ORIENTATION 2860 */ getHistoricalOrientation(int pointerIndex, int pos)2861 public final float getHistoricalOrientation(int pointerIndex, int pos) { 2862 return nativeGetAxisValue(mNativePtr, AXIS_ORIENTATION, pointerIndex, pos); 2863 } 2864 2865 /** 2866 * Returns the historical value of the requested axis, as per {@link #getAxisValue(int, int)}, 2867 * occurred between this event and the previous event for the given pointer. 2868 * Only applies to ACTION_MOVE events. 2869 * 2870 * @param axis The axis identifier for the axis value to retrieve. 2871 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2872 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2873 * @param pos Which historical value to return; must be less than 2874 * {@link #getHistorySize} 2875 * @return The value of the axis, or 0 if the axis is not available. 2876 * 2877 * @see #AXIS_X 2878 * @see #AXIS_Y 2879 */ getHistoricalAxisValue(int axis, int pointerIndex, int pos)2880 public final float getHistoricalAxisValue(int axis, int pointerIndex, int pos) { 2881 return nativeGetAxisValue(mNativePtr, axis, pointerIndex, pos); 2882 } 2883 2884 /** 2885 * Populates a {@link PointerCoords} object with historical pointer coordinate data, 2886 * as per {@link #getPointerCoords}, that occurred between this event and the previous 2887 * event for the given pointer. 2888 * Only applies to ACTION_MOVE events. 2889 * 2890 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 2891 * (the first pointer that is down) to {@link #getPointerCount()}-1. 2892 * @param pos Which historical value to return; must be less than 2893 * {@link #getHistorySize} 2894 * @param outPointerCoords The pointer coordinate object to populate. 2895 * 2896 * @see #getHistorySize 2897 * @see #getPointerCoords 2898 * @see PointerCoords 2899 */ getHistoricalPointerCoords(int pointerIndex, int pos, PointerCoords outPointerCoords)2900 public final void getHistoricalPointerCoords(int pointerIndex, int pos, 2901 PointerCoords outPointerCoords) { 2902 nativeGetPointerCoords(mNativePtr, pointerIndex, pos, outPointerCoords); 2903 } 2904 2905 /** 2906 * Returns a bitfield indicating which edges, if any, were touched by this 2907 * MotionEvent. For touch events, clients can use this to determine if the 2908 * user's finger was touching the edge of the display. 2909 * 2910 * This property is only set for {@link #ACTION_DOWN} events. 2911 * 2912 * @see #EDGE_LEFT 2913 * @see #EDGE_TOP 2914 * @see #EDGE_RIGHT 2915 * @see #EDGE_BOTTOM 2916 */ getEdgeFlags()2917 public final int getEdgeFlags() { 2918 return nativeGetEdgeFlags(mNativePtr); 2919 } 2920 2921 /** 2922 * Sets the bitfield indicating which edges, if any, were touched by this 2923 * MotionEvent. 2924 * 2925 * @see #getEdgeFlags() 2926 */ setEdgeFlags(int flags)2927 public final void setEdgeFlags(int flags) { 2928 nativeSetEdgeFlags(mNativePtr, flags); 2929 } 2930 2931 /** 2932 * Sets this event's action. 2933 */ setAction(int action)2934 public final void setAction(int action) { 2935 nativeSetAction(mNativePtr, action); 2936 } 2937 2938 /** 2939 * Adjust this event's location. 2940 * @param deltaX Amount to add to the current X coordinate of the event. 2941 * @param deltaY Amount to add to the current Y coordinate of the event. 2942 */ offsetLocation(float deltaX, float deltaY)2943 public final void offsetLocation(float deltaX, float deltaY) { 2944 if (deltaX != 0.0f || deltaY != 0.0f) { 2945 nativeOffsetLocation(mNativePtr, deltaX, deltaY); 2946 } 2947 } 2948 2949 /** 2950 * Set this event's location. Applies {@link #offsetLocation} with a 2951 * delta from the current location to the given new location. 2952 * 2953 * @param x New absolute X location. 2954 * @param y New absolute Y location. 2955 */ setLocation(float x, float y)2956 public final void setLocation(float x, float y) { 2957 float oldX = getX(); 2958 float oldY = getY(); 2959 offsetLocation(x - oldX, y - oldY); 2960 } 2961 2962 /** 2963 * Applies a transformation matrix to all of the points in the event. 2964 * 2965 * @param matrix The transformation matrix to apply. 2966 */ transform(Matrix matrix)2967 public final void transform(Matrix matrix) { 2968 if (matrix == null) { 2969 throw new IllegalArgumentException("matrix must not be null"); 2970 } 2971 2972 nativeTransform(mNativePtr, matrix.native_instance); 2973 } 2974 2975 /** 2976 * Add a new movement to the batch of movements in this event. The event's 2977 * current location, position and size is updated to the new values. 2978 * The current values in the event are added to a list of historical values. 2979 * 2980 * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events. 2981 * 2982 * @param eventTime The time stamp (in ms) for this data. 2983 * @param x The new X position. 2984 * @param y The new Y position. 2985 * @param pressure The new pressure. 2986 * @param size The new size. 2987 * @param metaState Meta key state. 2988 */ addBatch(long eventTime, float x, float y, float pressure, float size, int metaState)2989 public final void addBatch(long eventTime, float x, float y, 2990 float pressure, float size, int metaState) { 2991 synchronized (gSharedTempLock) { 2992 ensureSharedTempPointerCapacity(1); 2993 final PointerCoords[] pc = gSharedTempPointerCoords; 2994 pc[0].clear(); 2995 pc[0].x = x; 2996 pc[0].y = y; 2997 pc[0].pressure = pressure; 2998 pc[0].size = size; 2999 3000 nativeAddBatch(mNativePtr, eventTime * NS_PER_MS, pc, metaState); 3001 } 3002 } 3003 3004 /** 3005 * Add a new movement to the batch of movements in this event. The event's 3006 * current location, position and size is updated to the new values. 3007 * The current values in the event are added to a list of historical values. 3008 * 3009 * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events. 3010 * 3011 * @param eventTime The time stamp (in ms) for this data. 3012 * @param pointerCoords The new pointer coordinates. 3013 * @param metaState Meta key state. 3014 */ addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState)3015 public final void addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState) { 3016 nativeAddBatch(mNativePtr, eventTime * NS_PER_MS, pointerCoords, metaState); 3017 } 3018 3019 /** 3020 * Adds all of the movement samples of the specified event to this one if 3021 * it is compatible. To be compatible, the event must have the same device id, 3022 * source, action, flags, pointer count, pointer properties. 3023 * 3024 * Only applies to {@link #ACTION_MOVE} or {@link #ACTION_HOVER_MOVE} events. 3025 * 3026 * @param event The event whose movements samples should be added to this one 3027 * if possible. 3028 * @return True if batching was performed or false if batching was not possible. 3029 * @hide 3030 */ addBatch(MotionEvent event)3031 public final boolean addBatch(MotionEvent event) { 3032 final int action = nativeGetAction(mNativePtr); 3033 if (action != ACTION_MOVE && action != ACTION_HOVER_MOVE) { 3034 return false; 3035 } 3036 if (action != nativeGetAction(event.mNativePtr)) { 3037 return false; 3038 } 3039 3040 if (nativeGetDeviceId(mNativePtr) != nativeGetDeviceId(event.mNativePtr) 3041 || nativeGetSource(mNativePtr) != nativeGetSource(event.mNativePtr) 3042 || nativeGetFlags(mNativePtr) != nativeGetFlags(event.mNativePtr)) { 3043 return false; 3044 } 3045 3046 final int pointerCount = nativeGetPointerCount(mNativePtr); 3047 if (pointerCount != nativeGetPointerCount(event.mNativePtr)) { 3048 return false; 3049 } 3050 3051 synchronized (gSharedTempLock) { 3052 ensureSharedTempPointerCapacity(Math.max(pointerCount, 2)); 3053 final PointerProperties[] pp = gSharedTempPointerProperties; 3054 final PointerCoords[] pc = gSharedTempPointerCoords; 3055 3056 for (int i = 0; i < pointerCount; i++) { 3057 nativeGetPointerProperties(mNativePtr, i, pp[0]); 3058 nativeGetPointerProperties(event.mNativePtr, i, pp[1]); 3059 if (!pp[0].equals(pp[1])) { 3060 return false; 3061 } 3062 } 3063 3064 final int metaState = nativeGetMetaState(event.mNativePtr); 3065 final int historySize = nativeGetHistorySize(event.mNativePtr); 3066 for (int h = 0; h <= historySize; h++) { 3067 final int historyPos = (h == historySize ? HISTORY_CURRENT : h); 3068 3069 for (int i = 0; i < pointerCount; i++) { 3070 nativeGetPointerCoords(event.mNativePtr, i, historyPos, pc[i]); 3071 } 3072 3073 final long eventTimeNanos = nativeGetEventTimeNanos(event.mNativePtr, historyPos); 3074 nativeAddBatch(mNativePtr, eventTimeNanos, pc, metaState); 3075 } 3076 } 3077 return true; 3078 } 3079 3080 /** 3081 * Returns true if all points in the motion event are completely within the specified bounds. 3082 * @hide 3083 */ isWithinBoundsNoHistory(float left, float top, float right, float bottom)3084 public final boolean isWithinBoundsNoHistory(float left, float top, 3085 float right, float bottom) { 3086 final int pointerCount = nativeGetPointerCount(mNativePtr); 3087 for (int i = 0; i < pointerCount; i++) { 3088 final float x = nativeGetAxisValue(mNativePtr, AXIS_X, i, HISTORY_CURRENT); 3089 final float y = nativeGetAxisValue(mNativePtr, AXIS_Y, i, HISTORY_CURRENT); 3090 if (x < left || x > right || y < top || y > bottom) { 3091 return false; 3092 } 3093 } 3094 return true; 3095 } 3096 clamp(float value, float low, float high)3097 private static final float clamp(float value, float low, float high) { 3098 if (value < low) { 3099 return low; 3100 } else if (value > high) { 3101 return high; 3102 } 3103 return value; 3104 } 3105 3106 /** 3107 * Returns a new motion events whose points have been clamped to the specified bounds. 3108 * @hide 3109 */ clampNoHistory(float left, float top, float right, float bottom)3110 public final MotionEvent clampNoHistory(float left, float top, float right, float bottom) { 3111 MotionEvent ev = obtain(); 3112 synchronized (gSharedTempLock) { 3113 final int pointerCount = nativeGetPointerCount(mNativePtr); 3114 3115 ensureSharedTempPointerCapacity(pointerCount); 3116 final PointerProperties[] pp = gSharedTempPointerProperties; 3117 final PointerCoords[] pc = gSharedTempPointerCoords; 3118 3119 for (int i = 0; i < pointerCount; i++) { 3120 nativeGetPointerProperties(mNativePtr, i, pp[i]); 3121 nativeGetPointerCoords(mNativePtr, i, HISTORY_CURRENT, pc[i]); 3122 pc[i].x = clamp(pc[i].x, left, right); 3123 pc[i].y = clamp(pc[i].y, top, bottom); 3124 } 3125 ev.mNativePtr = nativeInitialize(ev.mNativePtr, 3126 nativeGetDeviceId(mNativePtr), nativeGetSource(mNativePtr), 3127 nativeGetAction(mNativePtr), nativeGetFlags(mNativePtr), 3128 nativeGetEdgeFlags(mNativePtr), nativeGetMetaState(mNativePtr), 3129 nativeGetButtonState(mNativePtr), 3130 nativeGetXOffset(mNativePtr), nativeGetYOffset(mNativePtr), 3131 nativeGetXPrecision(mNativePtr), nativeGetYPrecision(mNativePtr), 3132 nativeGetDownTimeNanos(mNativePtr), 3133 nativeGetEventTimeNanos(mNativePtr, HISTORY_CURRENT), 3134 pointerCount, pp, pc); 3135 return ev; 3136 } 3137 } 3138 3139 /** 3140 * Gets an integer where each pointer id present in the event is marked as a bit. 3141 * @hide 3142 */ getPointerIdBits()3143 public final int getPointerIdBits() { 3144 int idBits = 0; 3145 final int pointerCount = nativeGetPointerCount(mNativePtr); 3146 for (int i = 0; i < pointerCount; i++) { 3147 idBits |= 1 << nativeGetPointerId(mNativePtr, i); 3148 } 3149 return idBits; 3150 } 3151 3152 /** 3153 * Splits a motion event such that it includes only a subset of pointer ids. 3154 * @hide 3155 */ split(int idBits)3156 public final MotionEvent split(int idBits) { 3157 MotionEvent ev = obtain(); 3158 synchronized (gSharedTempLock) { 3159 final int oldPointerCount = nativeGetPointerCount(mNativePtr); 3160 ensureSharedTempPointerCapacity(oldPointerCount); 3161 final PointerProperties[] pp = gSharedTempPointerProperties; 3162 final PointerCoords[] pc = gSharedTempPointerCoords; 3163 final int[] map = gSharedTempPointerIndexMap; 3164 3165 final int oldAction = nativeGetAction(mNativePtr); 3166 final int oldActionMasked = oldAction & ACTION_MASK; 3167 final int oldActionPointerIndex = (oldAction & ACTION_POINTER_INDEX_MASK) 3168 >> ACTION_POINTER_INDEX_SHIFT; 3169 int newActionPointerIndex = -1; 3170 int newPointerCount = 0; 3171 int newIdBits = 0; 3172 for (int i = 0; i < oldPointerCount; i++) { 3173 nativeGetPointerProperties(mNativePtr, i, pp[newPointerCount]); 3174 final int idBit = 1 << pp[newPointerCount].id; 3175 if ((idBit & idBits) != 0) { 3176 if (i == oldActionPointerIndex) { 3177 newActionPointerIndex = newPointerCount; 3178 } 3179 map[newPointerCount] = i; 3180 newPointerCount += 1; 3181 newIdBits |= idBit; 3182 } 3183 } 3184 3185 if (newPointerCount == 0) { 3186 throw new IllegalArgumentException("idBits did not match any ids in the event"); 3187 } 3188 3189 final int newAction; 3190 if (oldActionMasked == ACTION_POINTER_DOWN || oldActionMasked == ACTION_POINTER_UP) { 3191 if (newActionPointerIndex < 0) { 3192 // An unrelated pointer changed. 3193 newAction = ACTION_MOVE; 3194 } else if (newPointerCount == 1) { 3195 // The first/last pointer went down/up. 3196 newAction = oldActionMasked == ACTION_POINTER_DOWN 3197 ? ACTION_DOWN : ACTION_UP; 3198 } else { 3199 // A secondary pointer went down/up. 3200 newAction = oldActionMasked 3201 | (newActionPointerIndex << ACTION_POINTER_INDEX_SHIFT); 3202 } 3203 } else { 3204 // Simple up/down/cancel/move or other motion action. 3205 newAction = oldAction; 3206 } 3207 3208 final int historySize = nativeGetHistorySize(mNativePtr); 3209 for (int h = 0; h <= historySize; h++) { 3210 final int historyPos = h == historySize ? HISTORY_CURRENT : h; 3211 3212 for (int i = 0; i < newPointerCount; i++) { 3213 nativeGetPointerCoords(mNativePtr, map[i], historyPos, pc[i]); 3214 } 3215 3216 final long eventTimeNanos = nativeGetEventTimeNanos(mNativePtr, historyPos); 3217 if (h == 0) { 3218 ev.mNativePtr = nativeInitialize(ev.mNativePtr, 3219 nativeGetDeviceId(mNativePtr), nativeGetSource(mNativePtr), 3220 newAction, nativeGetFlags(mNativePtr), 3221 nativeGetEdgeFlags(mNativePtr), nativeGetMetaState(mNativePtr), 3222 nativeGetButtonState(mNativePtr), 3223 nativeGetXOffset(mNativePtr), nativeGetYOffset(mNativePtr), 3224 nativeGetXPrecision(mNativePtr), nativeGetYPrecision(mNativePtr), 3225 nativeGetDownTimeNanos(mNativePtr), eventTimeNanos, 3226 newPointerCount, pp, pc); 3227 } else { 3228 nativeAddBatch(ev.mNativePtr, eventTimeNanos, pc, 0); 3229 } 3230 } 3231 return ev; 3232 } 3233 } 3234 3235 @Override toString()3236 public String toString() { 3237 StringBuilder msg = new StringBuilder(); 3238 msg.append("MotionEvent { action=").append(actionToString(getAction())); 3239 msg.append(", actionButton=").append(buttonStateToString(getActionButton())); 3240 3241 final int pointerCount = getPointerCount(); 3242 for (int i = 0; i < pointerCount; i++) { 3243 msg.append(", id[").append(i).append("]=").append(getPointerId(i)); 3244 msg.append(", x[").append(i).append("]=").append(getX(i)); 3245 msg.append(", y[").append(i).append("]=").append(getY(i)); 3246 msg.append(", toolType[").append(i).append("]=").append( 3247 toolTypeToString(getToolType(i))); 3248 } 3249 3250 msg.append(", buttonState=").append(MotionEvent.buttonStateToString(getButtonState())); 3251 msg.append(", metaState=").append(KeyEvent.metaStateToString(getMetaState())); 3252 msg.append(", flags=0x").append(Integer.toHexString(getFlags())); 3253 msg.append(", edgeFlags=0x").append(Integer.toHexString(getEdgeFlags())); 3254 msg.append(", pointerCount=").append(pointerCount); 3255 msg.append(", historySize=").append(getHistorySize()); 3256 msg.append(", eventTime=").append(getEventTime()); 3257 msg.append(", downTime=").append(getDownTime()); 3258 msg.append(", deviceId=").append(getDeviceId()); 3259 msg.append(", source=0x").append(Integer.toHexString(getSource())); 3260 msg.append(" }"); 3261 return msg.toString(); 3262 } 3263 3264 /** 3265 * Returns a string that represents the symbolic name of the specified unmasked action 3266 * such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent numeric constant 3267 * such as "35" if unknown. 3268 * 3269 * @param action The unmasked action. 3270 * @return The symbolic name of the specified action. 3271 * @see #getAction() 3272 */ actionToString(int action)3273 public static String actionToString(int action) { 3274 switch (action) { 3275 case ACTION_DOWN: 3276 return "ACTION_DOWN"; 3277 case ACTION_UP: 3278 return "ACTION_UP"; 3279 case ACTION_CANCEL: 3280 return "ACTION_CANCEL"; 3281 case ACTION_OUTSIDE: 3282 return "ACTION_OUTSIDE"; 3283 case ACTION_MOVE: 3284 return "ACTION_MOVE"; 3285 case ACTION_HOVER_MOVE: 3286 return "ACTION_HOVER_MOVE"; 3287 case ACTION_SCROLL: 3288 return "ACTION_SCROLL"; 3289 case ACTION_HOVER_ENTER: 3290 return "ACTION_HOVER_ENTER"; 3291 case ACTION_HOVER_EXIT: 3292 return "ACTION_HOVER_EXIT"; 3293 case ACTION_BUTTON_PRESS: 3294 return "ACTION_BUTTON_PRESS"; 3295 case ACTION_BUTTON_RELEASE: 3296 return "ACTION_BUTTON_RELEASE"; 3297 } 3298 int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT; 3299 switch (action & ACTION_MASK) { 3300 case ACTION_POINTER_DOWN: 3301 return "ACTION_POINTER_DOWN(" + index + ")"; 3302 case ACTION_POINTER_UP: 3303 return "ACTION_POINTER_UP(" + index + ")"; 3304 default: 3305 return Integer.toString(action); 3306 } 3307 } 3308 3309 /** 3310 * Returns a string that represents the symbolic name of the specified axis 3311 * such as "AXIS_X" or an equivalent numeric constant such as "42" if unknown. 3312 * 3313 * @param axis The axis. 3314 * @return The symbolic name of the specified axis. 3315 */ axisToString(int axis)3316 public static String axisToString(int axis) { 3317 String symbolicName = nativeAxisToString(axis); 3318 return symbolicName != null ? LABEL_PREFIX + symbolicName : Integer.toString(axis); 3319 } 3320 3321 /** 3322 * Gets an axis by its symbolic name such as "AXIS_X" or an 3323 * equivalent numeric constant such as "42". 3324 * 3325 * @param symbolicName The symbolic name of the axis. 3326 * @return The axis or -1 if not found. 3327 * @see KeyEvent#keyCodeToString(int) 3328 */ axisFromString(String symbolicName)3329 public static int axisFromString(String symbolicName) { 3330 if (symbolicName.startsWith(LABEL_PREFIX)) { 3331 symbolicName = symbolicName.substring(LABEL_PREFIX.length()); 3332 int axis = nativeAxisFromString(symbolicName); 3333 if (axis >= 0) { 3334 return axis; 3335 } 3336 } 3337 try { 3338 return Integer.parseInt(symbolicName, 10); 3339 } catch (NumberFormatException ex) { 3340 return -1; 3341 } 3342 } 3343 3344 /** 3345 * Returns a string that represents the symbolic name of the specified combined 3346 * button state flags such as "0", "BUTTON_PRIMARY", 3347 * "BUTTON_PRIMARY|BUTTON_SECONDARY" or an equivalent numeric constant such as "0x10000000" 3348 * if unknown. 3349 * 3350 * @param buttonState The button state. 3351 * @return The symbolic name of the specified combined button state flags. 3352 * @hide 3353 */ buttonStateToString(int buttonState)3354 public static String buttonStateToString(int buttonState) { 3355 if (buttonState == 0) { 3356 return "0"; 3357 } 3358 StringBuilder result = null; 3359 int i = 0; 3360 while (buttonState != 0) { 3361 final boolean isSet = (buttonState & 1) != 0; 3362 buttonState >>>= 1; // unsigned shift! 3363 if (isSet) { 3364 final String name = BUTTON_SYMBOLIC_NAMES[i]; 3365 if (result == null) { 3366 if (buttonState == 0) { 3367 return name; 3368 } 3369 result = new StringBuilder(name); 3370 } else { 3371 result.append('|'); 3372 result.append(name); 3373 } 3374 } 3375 i += 1; 3376 } 3377 return result.toString(); 3378 } 3379 3380 /** 3381 * Returns a string that represents the symbolic name of the specified tool type 3382 * such as "TOOL_TYPE_FINGER" or an equivalent numeric constant such as "42" if unknown. 3383 * 3384 * @param toolType The tool type. 3385 * @return The symbolic name of the specified tool type. 3386 * @hide 3387 */ toolTypeToString(int toolType)3388 public static String toolTypeToString(int toolType) { 3389 String symbolicName = TOOL_TYPE_SYMBOLIC_NAMES.get(toolType); 3390 return symbolicName != null ? symbolicName : Integer.toString(toolType); 3391 } 3392 3393 /** 3394 * Checks if a mouse or stylus button (or combination of buttons) is pressed. 3395 * @param button Button (or combination of buttons). 3396 * @return True if specified buttons are pressed. 3397 * 3398 * @see #BUTTON_PRIMARY 3399 * @see #BUTTON_SECONDARY 3400 * @see #BUTTON_TERTIARY 3401 * @see #BUTTON_FORWARD 3402 * @see #BUTTON_BACK 3403 * @see #BUTTON_STYLUS_PRIMARY 3404 * @see #BUTTON_STYLUS_SECONDARY 3405 */ isButtonPressed(int button)3406 public final boolean isButtonPressed(int button) { 3407 if (button == 0) { 3408 return false; 3409 } 3410 return (getButtonState() & button) == button; 3411 } 3412 3413 public static final Parcelable.Creator<MotionEvent> CREATOR 3414 = new Parcelable.Creator<MotionEvent>() { 3415 public MotionEvent createFromParcel(Parcel in) { 3416 in.readInt(); // skip token, we already know this is a MotionEvent 3417 return MotionEvent.createFromParcelBody(in); 3418 } 3419 3420 public MotionEvent[] newArray(int size) { 3421 return new MotionEvent[size]; 3422 } 3423 }; 3424 3425 /** @hide */ createFromParcelBody(Parcel in)3426 public static MotionEvent createFromParcelBody(Parcel in) { 3427 MotionEvent ev = obtain(); 3428 ev.mNativePtr = nativeReadFromParcel(ev.mNativePtr, in); 3429 return ev; 3430 } 3431 3432 /** @hide */ 3433 @Override cancel()3434 public final void cancel() { 3435 setAction(ACTION_CANCEL); 3436 } 3437 writeToParcel(Parcel out, int flags)3438 public void writeToParcel(Parcel out, int flags) { 3439 out.writeInt(PARCEL_TOKEN_MOTION_EVENT); 3440 nativeWriteToParcel(mNativePtr, out); 3441 } 3442 3443 /** 3444 * Transfer object for pointer coordinates. 3445 * 3446 * Objects of this type can be used to specify the pointer coordinates when 3447 * creating new {@link MotionEvent} objects and to query pointer coordinates 3448 * in bulk. 3449 * 3450 * Refer to {@link InputDevice} for information about how different kinds of 3451 * input devices and sources represent pointer coordinates. 3452 */ 3453 public static final class PointerCoords { 3454 private static final int INITIAL_PACKED_AXIS_VALUES = 8; 3455 private long mPackedAxisBits; 3456 private float[] mPackedAxisValues; 3457 3458 /** 3459 * Creates a pointer coords object with all axes initialized to zero. 3460 */ PointerCoords()3461 public PointerCoords() { 3462 } 3463 3464 /** 3465 * Creates a pointer coords object as a copy of the 3466 * contents of another pointer coords object. 3467 * 3468 * @param other The pointer coords object to copy. 3469 */ PointerCoords(PointerCoords other)3470 public PointerCoords(PointerCoords other) { 3471 copyFrom(other); 3472 } 3473 3474 /** @hide */ createArray(int size)3475 public static PointerCoords[] createArray(int size) { 3476 PointerCoords[] array = new PointerCoords[size]; 3477 for (int i = 0; i < size; i++) { 3478 array[i] = new PointerCoords(); 3479 } 3480 return array; 3481 } 3482 3483 /** 3484 * The X component of the pointer movement. 3485 * 3486 * @see MotionEvent#AXIS_X 3487 */ 3488 public float x; 3489 3490 /** 3491 * The Y component of the pointer movement. 3492 * 3493 * @see MotionEvent#AXIS_Y 3494 */ 3495 public float y; 3496 3497 /** 3498 * A normalized value that describes the pressure applied to the device 3499 * by a finger or other tool. 3500 * The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure), 3501 * although values higher than 1 may be generated depending on the calibration of 3502 * the input device. 3503 * 3504 * @see MotionEvent#AXIS_PRESSURE 3505 */ 3506 public float pressure; 3507 3508 /** 3509 * A normalized value that describes the approximate size of the pointer touch area 3510 * in relation to the maximum detectable size of the device. 3511 * It represents some approximation of the area of the screen being 3512 * pressed; the actual value in pixels corresponding to the 3513 * touch is normalized with the device specific range of values 3514 * and scaled to a value between 0 and 1. The value of size can be used to 3515 * determine fat touch events. 3516 * 3517 * @see MotionEvent#AXIS_SIZE 3518 */ 3519 public float size; 3520 3521 /** 3522 * The length of the major axis of an ellipse that describes the touch area at 3523 * the point of contact. 3524 * If the device is a touch screen, the length is reported in pixels, otherwise it is 3525 * reported in device-specific units. 3526 * 3527 * @see MotionEvent#AXIS_TOUCH_MAJOR 3528 */ 3529 public float touchMajor; 3530 3531 /** 3532 * The length of the minor axis of an ellipse that describes the touch area at 3533 * the point of contact. 3534 * If the device is a touch screen, the length is reported in pixels, otherwise it is 3535 * reported in device-specific units. 3536 * 3537 * @see MotionEvent#AXIS_TOUCH_MINOR 3538 */ 3539 public float touchMinor; 3540 3541 /** 3542 * The length of the major axis of an ellipse that describes the size of 3543 * the approaching tool. 3544 * The tool area represents the estimated size of the finger or pen that is 3545 * touching the device independent of its actual touch area at the point of contact. 3546 * If the device is a touch screen, the length is reported in pixels, otherwise it is 3547 * reported in device-specific units. 3548 * 3549 * @see MotionEvent#AXIS_TOOL_MAJOR 3550 */ 3551 public float toolMajor; 3552 3553 /** 3554 * The length of the minor axis of an ellipse that describes the size of 3555 * the approaching tool. 3556 * The tool area represents the estimated size of the finger or pen that is 3557 * touching the device independent of its actual touch area at the point of contact. 3558 * If the device is a touch screen, the length is reported in pixels, otherwise it is 3559 * reported in device-specific units. 3560 * 3561 * @see MotionEvent#AXIS_TOOL_MINOR 3562 */ 3563 public float toolMinor; 3564 3565 /** 3566 * The orientation of the touch area and tool area in radians clockwise from vertical. 3567 * An angle of 0 radians indicates that the major axis of contact is oriented 3568 * upwards, is perfectly circular or is of unknown orientation. A positive angle 3569 * indicates that the major axis of contact is oriented to the right. A negative angle 3570 * indicates that the major axis of contact is oriented to the left. 3571 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians 3572 * (finger pointing fully right). 3573 * 3574 * @see MotionEvent#AXIS_ORIENTATION 3575 */ 3576 public float orientation; 3577 3578 /** 3579 * Clears the contents of this object. 3580 * Resets all axes to zero. 3581 */ clear()3582 public void clear() { 3583 mPackedAxisBits = 0; 3584 3585 x = 0; 3586 y = 0; 3587 pressure = 0; 3588 size = 0; 3589 touchMajor = 0; 3590 touchMinor = 0; 3591 toolMajor = 0; 3592 toolMinor = 0; 3593 orientation = 0; 3594 } 3595 3596 /** 3597 * Copies the contents of another pointer coords object. 3598 * 3599 * @param other The pointer coords object to copy. 3600 */ copyFrom(PointerCoords other)3601 public void copyFrom(PointerCoords other) { 3602 final long bits = other.mPackedAxisBits; 3603 mPackedAxisBits = bits; 3604 if (bits != 0) { 3605 final float[] otherValues = other.mPackedAxisValues; 3606 final int count = Long.bitCount(bits); 3607 float[] values = mPackedAxisValues; 3608 if (values == null || count > values.length) { 3609 values = new float[otherValues.length]; 3610 mPackedAxisValues = values; 3611 } 3612 System.arraycopy(otherValues, 0, values, 0, count); 3613 } 3614 3615 x = other.x; 3616 y = other.y; 3617 pressure = other.pressure; 3618 size = other.size; 3619 touchMajor = other.touchMajor; 3620 touchMinor = other.touchMinor; 3621 toolMajor = other.toolMajor; 3622 toolMinor = other.toolMinor; 3623 orientation = other.orientation; 3624 } 3625 3626 /** 3627 * Gets the value associated with the specified axis. 3628 * 3629 * @param axis The axis identifier for the axis value to retrieve. 3630 * @return The value associated with the axis, or 0 if none. 3631 * 3632 * @see MotionEvent#AXIS_X 3633 * @see MotionEvent#AXIS_Y 3634 */ getAxisValue(int axis)3635 public float getAxisValue(int axis) { 3636 switch (axis) { 3637 case AXIS_X: 3638 return x; 3639 case AXIS_Y: 3640 return y; 3641 case AXIS_PRESSURE: 3642 return pressure; 3643 case AXIS_SIZE: 3644 return size; 3645 case AXIS_TOUCH_MAJOR: 3646 return touchMajor; 3647 case AXIS_TOUCH_MINOR: 3648 return touchMinor; 3649 case AXIS_TOOL_MAJOR: 3650 return toolMajor; 3651 case AXIS_TOOL_MINOR: 3652 return toolMinor; 3653 case AXIS_ORIENTATION: 3654 return orientation; 3655 default: { 3656 if (axis < 0 || axis > 63) { 3657 throw new IllegalArgumentException("Axis out of range."); 3658 } 3659 final long bits = mPackedAxisBits; 3660 final long axisBit = 0x8000000000000000L >>> axis; 3661 if ((bits & axisBit) == 0) { 3662 return 0; 3663 } 3664 final int index = Long.bitCount(bits & ~(0xFFFFFFFFFFFFFFFFL >>> axis)); 3665 return mPackedAxisValues[index]; 3666 } 3667 } 3668 } 3669 3670 /** 3671 * Sets the value associated with the specified axis. 3672 * 3673 * @param axis The axis identifier for the axis value to assign. 3674 * @param value The value to set. 3675 * 3676 * @see MotionEvent#AXIS_X 3677 * @see MotionEvent#AXIS_Y 3678 */ setAxisValue(int axis, float value)3679 public void setAxisValue(int axis, float value) { 3680 switch (axis) { 3681 case AXIS_X: 3682 x = value; 3683 break; 3684 case AXIS_Y: 3685 y = value; 3686 break; 3687 case AXIS_PRESSURE: 3688 pressure = value; 3689 break; 3690 case AXIS_SIZE: 3691 size = value; 3692 break; 3693 case AXIS_TOUCH_MAJOR: 3694 touchMajor = value; 3695 break; 3696 case AXIS_TOUCH_MINOR: 3697 touchMinor = value; 3698 break; 3699 case AXIS_TOOL_MAJOR: 3700 toolMajor = value; 3701 break; 3702 case AXIS_TOOL_MINOR: 3703 toolMinor = value; 3704 break; 3705 case AXIS_ORIENTATION: 3706 orientation = value; 3707 break; 3708 default: { 3709 if (axis < 0 || axis > 63) { 3710 throw new IllegalArgumentException("Axis out of range."); 3711 } 3712 final long bits = mPackedAxisBits; 3713 final long axisBit = 0x8000000000000000L >>> axis; 3714 final int index = Long.bitCount(bits & ~(0xFFFFFFFFFFFFFFFFL >>> axis)); 3715 float[] values = mPackedAxisValues; 3716 if ((bits & axisBit) == 0) { 3717 if (values == null) { 3718 values = new float[INITIAL_PACKED_AXIS_VALUES]; 3719 mPackedAxisValues = values; 3720 } else { 3721 final int count = Long.bitCount(bits); 3722 if (count < values.length) { 3723 if (index != count) { 3724 System.arraycopy(values, index, values, index + 1, 3725 count - index); 3726 } 3727 } else { 3728 float[] newValues = new float[count * 2]; 3729 System.arraycopy(values, 0, newValues, 0, index); 3730 System.arraycopy(values, index, newValues, index + 1, 3731 count - index); 3732 values = newValues; 3733 mPackedAxisValues = values; 3734 } 3735 } 3736 mPackedAxisBits = bits | axisBit; 3737 } 3738 values[index] = value; 3739 } 3740 } 3741 } 3742 } 3743 3744 /** 3745 * Transfer object for pointer properties. 3746 * 3747 * Objects of this type can be used to specify the pointer id and tool type 3748 * when creating new {@link MotionEvent} objects and to query pointer properties in bulk. 3749 */ 3750 public static final class PointerProperties { 3751 /** 3752 * Creates a pointer properties object with an invalid pointer id. 3753 */ PointerProperties()3754 public PointerProperties() { 3755 clear(); 3756 } 3757 3758 /** 3759 * Creates a pointer properties object as a copy of the contents of 3760 * another pointer properties object. 3761 * @param other 3762 */ PointerProperties(PointerProperties other)3763 public PointerProperties(PointerProperties other) { 3764 copyFrom(other); 3765 } 3766 3767 /** @hide */ createArray(int size)3768 public static PointerProperties[] createArray(int size) { 3769 PointerProperties[] array = new PointerProperties[size]; 3770 for (int i = 0; i < size; i++) { 3771 array[i] = new PointerProperties(); 3772 } 3773 return array; 3774 } 3775 3776 /** 3777 * The pointer id. 3778 * Initially set to {@link #INVALID_POINTER_ID} (-1). 3779 * 3780 * @see MotionEvent#getPointerId(int) 3781 */ 3782 public int id; 3783 3784 /** 3785 * The pointer tool type. 3786 * Initially set to 0. 3787 * 3788 * @see MotionEvent#getToolType(int) 3789 */ 3790 public int toolType; 3791 3792 /** 3793 * Resets the pointer properties to their initial values. 3794 */ clear()3795 public void clear() { 3796 id = INVALID_POINTER_ID; 3797 toolType = TOOL_TYPE_UNKNOWN; 3798 } 3799 3800 /** 3801 * Copies the contents of another pointer properties object. 3802 * 3803 * @param other The pointer properties object to copy. 3804 */ copyFrom(PointerProperties other)3805 public void copyFrom(PointerProperties other) { 3806 id = other.id; 3807 toolType = other.toolType; 3808 } 3809 3810 @Override equals(Object other)3811 public boolean equals(Object other) { 3812 if (other instanceof PointerProperties) { 3813 return equals((PointerProperties)other); 3814 } 3815 return false; 3816 } 3817 equals(PointerProperties other)3818 private boolean equals(PointerProperties other) { 3819 return other != null && id == other.id && toolType == other.toolType; 3820 } 3821 3822 @Override hashCode()3823 public int hashCode() { 3824 return id | (toolType << 8); 3825 } 3826 } 3827 } 3828