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