MotionEvent.java revision 61b9649a50aefacc74449728e96f37f6f9d7a8b3
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 17package android.view; 18 19import android.graphics.Matrix; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.os.SystemClock; 23 24/** 25 * Object used to report movement (mouse, pen, finger, trackball) events. This 26 * class may hold either absolute or relative movements, depending on what 27 * it is being used for. 28 * <p> 29 * On pointing devices with source class {@link InputDevice#SOURCE_CLASS_POINTER} 30 * such as touch screens, the pointer coordinates specify absolute 31 * positions such as view X/Y coordinates. Each complete gesture is represented 32 * by a sequence of motion events with actions that describe pointer state transitions 33 * and movements. A gesture starts with a motion event with {@link #ACTION_DOWN} 34 * that provides the location of the first pointer down. As each additional 35 * pointer that goes down or up, the framework will generate a motion event with 36 * {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} accordingly. 37 * Pointer movements are described by motion events with {@link #ACTION_MOVE}. 38 * Finally, a gesture end either when the final pointer goes up as represented 39 * by a motion event with {@link #ACTION_UP} or when gesture is canceled 40 * with {@link #ACTION_CANCEL}. 41 * </p><p> 42 * On trackball devices with source class {@link InputDevice#SOURCE_CLASS_TRACKBALL}, 43 * the pointer coordinates specify relative movements as X/Y deltas. 44 * A trackball gesture consists of a sequence of movements described by motion 45 * events with {@link #ACTION_MOVE} interspersed with occasional {@link #ACTION_DOWN} 46 * or {@link #ACTION_UP} motion events when the trackball button is pressed or released. 47 * </p><p> 48 * On joystick devices with source class {@link InputDevice#SOURCE_CLASS_JOYSTICK}, 49 * the pointer coordinates specify the absolute position of the joystick axes. 50 * The joystick axis values are normalized to a range of -1.0 to 1.0 where 0.0 corresponds 51 * to the center position. More information about the set of available axes and the 52 * range of motion can be obtained using {@link InputDevice#getMotionRange}. 53 * </p><p> 54 * Motion events always report movements for all pointers at once. The number 55 * of pointers only ever changes by one as individual pointers go up and down, 56 * except when the gesture is canceled. 57 * </p><p> 58 * The order in which individual pointers appear within a motion event can change 59 * from one event to the next. Use the {@link #getPointerId(int)} method to obtain a 60 * pointer id to track pointers across motion events in a gesture. Then for 61 * successive motion events, use the {@link #findPointerIndex(int)} method to obtain 62 * the pointer index for a given pointer id in that motion event. 63 * </p><p> 64 * For efficiency, motion events with {@link #ACTION_MOVE} may batch together 65 * multiple movement samples within a single object. The most current 66 * pointer coordinates are available using {@link #getX(int)} and {@link #getY(int)}. 67 * Earlier coordinates within the batch are accessed using {@link #getHistoricalX(int, int)} 68 * and {@link #getHistoricalY(int, int)}. The coordinates are "historical" only 69 * insofar as they are older than the current coordinates in the batch; however, 70 * they are still distinct from any other coordinates reported in prior motion events. 71 * To process all coordinates in the batch in time order, first consume the historical 72 * coordinates then consume the current coordinates. 73 * </p><p> 74 * Example: Consuming all samples for all pointers in a motion event in time order. 75 * </p><p><pre><code> 76 * void printSamples(MotionEvent ev) { 77 * final int historySize = ev.getHistorySize(); 78 * final int pointerCount = ev.getPointerCount(); 79 * for (int h = 0; h < historySize; h++) { 80 * System.out.printf("At time %d:", ev.getHistoricalEventTime(h)); 81 * for (int p = 0; p < pointerCount; p++) { 82 * System.out.printf(" pointer %d: (%f,%f)", 83 * ev.getPointerId(p), ev.getHistoricalX(p, h), ev.getHistoricalY(p, h)); 84 * } 85 * } 86 * System.out.printf("At time %d:", ev.getEventTime()); 87 * for (int p = 0; p < pointerCount; p++) { 88 * System.out.printf(" pointer %d: (%f,%f)", 89 * ev.getPointerId(p), ev.getX(p), ev.getY(p)); 90 * } 91 * } 92 * </code></pre></p><p> 93 * In general, the framework cannot guarantee that the motion events it delivers 94 * to a view always constitute a complete motion sequences since some events may be dropped 95 * or modified by containing views before they are delivered. The view implementation 96 * should be prepared to handle {@link #ACTION_CANCEL} and should tolerate anomalous 97 * situations such as receiving a new {@link #ACTION_DOWN} without first having 98 * received an {@link #ACTION_UP} for the prior gesture. 99 * </p><p> 100 * Refer to {@link InputDevice} for more information about how different kinds of 101 * input devices and sources represent pointer coordinates. 102 * </p> 103 */ 104public final class MotionEvent extends InputEvent implements Parcelable { 105 private static final long MS_PER_NS = 1000000; 106 private static final boolean TRACK_RECYCLED_LOCATION = false; 107 108 /** 109 * Bit mask of the parts of the action code that are the action itself. 110 */ 111 public static final int ACTION_MASK = 0xff; 112 113 /** 114 * Constant for {@link #getAction}: A pressed gesture has started, the 115 * motion contains the initial starting location. 116 */ 117 public static final int ACTION_DOWN = 0; 118 119 /** 120 * Constant for {@link #getAction}: A pressed gesture has finished, the 121 * motion contains the final release location as well as any intermediate 122 * points since the last down or move event. 123 */ 124 public static final int ACTION_UP = 1; 125 126 /** 127 * Constant for {@link #getAction}: A change has happened during a 128 * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). 129 * The motion contains the most recent point, as well as any intermediate 130 * points since the last down or move event. 131 */ 132 public static final int ACTION_MOVE = 2; 133 134 /** 135 * Constant for {@link #getAction}: The current gesture has been aborted. 136 * You will not receive any more points in it. You should treat this as 137 * an up event, but not perform any action that you normally would. 138 */ 139 public static final int ACTION_CANCEL = 3; 140 141 /** 142 * Constant for {@link #getAction}: A movement has happened outside of the 143 * normal bounds of the UI element. This does not provide a full gesture, 144 * but only the initial location of the movement/touch. 145 */ 146 public static final int ACTION_OUTSIDE = 4; 147 148 /** 149 * A non-primary pointer has gone down. The bits in 150 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed. 151 */ 152 public static final int ACTION_POINTER_DOWN = 5; 153 154 /** 155 * A non-primary pointer has gone up. The bits in 156 * {@link #ACTION_POINTER_ID_MASK} indicate which pointer changed. 157 */ 158 public static final int ACTION_POINTER_UP = 6; 159 160 /** 161 * Bits in the action code that represent a pointer index, used with 162 * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Shifting 163 * down by {@link #ACTION_POINTER_INDEX_SHIFT} provides the actual pointer 164 * index where the data for the pointer going up or down can be found; you can 165 * get its identifier with {@link #getPointerId(int)} and the actual 166 * data with {@link #getX(int)} etc. 167 */ 168 public static final int ACTION_POINTER_INDEX_MASK = 0xff00; 169 170 /** 171 * Bit shift for the action bits holding the pointer index as 172 * defined by {@link #ACTION_POINTER_INDEX_MASK}. 173 */ 174 public static final int ACTION_POINTER_INDEX_SHIFT = 8; 175 176 /** 177 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 178 * data index associated with {@link #ACTION_POINTER_DOWN}. 179 */ 180 @Deprecated 181 public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000; 182 183 /** 184 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 185 * data index associated with {@link #ACTION_POINTER_DOWN}. 186 */ 187 @Deprecated 188 public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100; 189 190 /** 191 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 192 * data index associated with {@link #ACTION_POINTER_DOWN}. 193 */ 194 @Deprecated 195 public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200; 196 197 /** 198 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 199 * data index associated with {@link #ACTION_POINTER_UP}. 200 */ 201 @Deprecated 202 public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000; 203 204 /** 205 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 206 * data index associated with {@link #ACTION_POINTER_UP}. 207 */ 208 @Deprecated 209 public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100; 210 211 /** 212 * @deprecated Use {@link #ACTION_POINTER_INDEX_MASK} to retrieve the 213 * data index associated with {@link #ACTION_POINTER_UP}. 214 */ 215 @Deprecated 216 public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200; 217 218 /** 219 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_MASK} to match 220 * the actual data contained in these bits. 221 */ 222 @Deprecated 223 public static final int ACTION_POINTER_ID_MASK = 0xff00; 224 225 /** 226 * @deprecated Renamed to {@link #ACTION_POINTER_INDEX_SHIFT} to match 227 * the actual data contained in these bits. 228 */ 229 @Deprecated 230 public static final int ACTION_POINTER_ID_SHIFT = 8; 231 232 /** 233 * This flag indicates that the window that received this motion event is partly 234 * or wholly obscured by another visible window above it. This flag is set to true 235 * even if the event did not directly pass through the obscured area. 236 * A security sensitive application can check this flag to identify situations in which 237 * a malicious application may have covered up part of its content for the purpose 238 * of misleading the user or hijacking touches. An appropriate response might be 239 * to drop the suspect touches or to take additional precautions to confirm the user's 240 * actual intent. 241 */ 242 public static final int FLAG_WINDOW_IS_OBSCURED = 0x1; 243 244 /** 245 * Flag indicating the motion event intersected the top edge of the screen. 246 */ 247 public static final int EDGE_TOP = 0x00000001; 248 249 /** 250 * Flag indicating the motion event intersected the bottom edge of the screen. 251 */ 252 public static final int EDGE_BOTTOM = 0x00000002; 253 254 /** 255 * Flag indicating the motion event intersected the left edge of the screen. 256 */ 257 public static final int EDGE_LEFT = 0x00000004; 258 259 /** 260 * Flag indicating the motion event intersected the right edge of the screen. 261 */ 262 public static final int EDGE_RIGHT = 0x00000008; 263 264 /* 265 * Offset for the sample's X coordinate. 266 */ 267 static private final int SAMPLE_X = 0; 268 269 /* 270 * Offset for the sample's Y coordinate. 271 */ 272 static private final int SAMPLE_Y = 1; 273 274 /* 275 * Offset for the sample's pressure. 276 */ 277 static private final int SAMPLE_PRESSURE = 2; 278 279 /* 280 * Offset for the sample's size 281 */ 282 static private final int SAMPLE_SIZE = 3; 283 284 /* 285 * Offset for the sample's touch major axis length. 286 */ 287 static private final int SAMPLE_TOUCH_MAJOR = 4; 288 289 /* 290 * Offset for the sample's touch minor axis length. 291 */ 292 static private final int SAMPLE_TOUCH_MINOR = 5; 293 294 /* 295 * Offset for the sample's tool major axis length. 296 */ 297 static private final int SAMPLE_TOOL_MAJOR = 6; 298 299 /* 300 * Offset for the sample's tool minor axis length. 301 */ 302 static private final int SAMPLE_TOOL_MINOR = 7; 303 304 /* 305 * Offset for the sample's orientation. 306 */ 307 static private final int SAMPLE_ORIENTATION = 8; 308 309 /* 310 * Number of data items for each sample. 311 */ 312 static private final int NUM_SAMPLE_DATA = 9; 313 314 /* 315 * Minimum number of pointers for which to reserve space when allocating new 316 * motion events. This is explicitly not a bound on the maximum number of pointers. 317 */ 318 static private final int BASE_AVAIL_POINTERS = 5; 319 320 /* 321 * Minimum number of samples for which to reserve space when allocating new motion events. 322 */ 323 static private final int BASE_AVAIL_SAMPLES = 8; 324 325 private static final int MAX_RECYCLED = 10; 326 private static final Object gRecyclerLock = new Object(); 327 private static int gRecyclerUsed; 328 private static MotionEvent gRecyclerTop; 329 330 private long mDownTimeNano; 331 private int mAction; 332 private float mXOffset; 333 private float mYOffset; 334 private float mXPrecision; 335 private float mYPrecision; 336 private int mEdgeFlags; 337 private int mMetaState; 338 private int mFlags; 339 340 private int mNumPointers; 341 private int mNumSamples; 342 343 private int mLastDataSampleIndex; 344 private int mLastEventTimeNanoSampleIndex; 345 346 // Array of mNumPointers size of identifiers for each pointer of data. 347 private int[] mPointerIdentifiers; 348 349 // Array of (mNumSamples * mNumPointers * NUM_SAMPLE_DATA) size of event data. 350 // Samples are ordered from oldest to newest. 351 private float[] mDataSamples; 352 353 // Array of mNumSamples size of event time stamps in nanoseconds. 354 // Samples are ordered from oldest to newest. 355 private long[] mEventTimeNanoSamples; 356 357 private MotionEvent mNext; 358 private RuntimeException mRecycledLocation; 359 private boolean mRecycled; 360 361 private native void nativeTransform(Matrix matrix); 362 363 private MotionEvent(int pointerCount, int sampleCount) { 364 mPointerIdentifiers = new int[pointerCount]; 365 mDataSamples = new float[pointerCount * sampleCount * NUM_SAMPLE_DATA]; 366 mEventTimeNanoSamples = new long[sampleCount]; 367 } 368 369 static private MotionEvent obtain(int pointerCount, int sampleCount) { 370 final MotionEvent ev; 371 synchronized (gRecyclerLock) { 372 ev = gRecyclerTop; 373 if (ev == null) { 374 if (pointerCount < BASE_AVAIL_POINTERS) { 375 pointerCount = BASE_AVAIL_POINTERS; 376 } 377 if (sampleCount < BASE_AVAIL_SAMPLES) { 378 sampleCount = BASE_AVAIL_SAMPLES; 379 } 380 return new MotionEvent(pointerCount, sampleCount); 381 } 382 gRecyclerTop = ev.mNext; 383 gRecyclerUsed -= 1; 384 } 385 ev.mRecycledLocation = null; 386 ev.mRecycled = false; 387 ev.mNext = null; 388 389 if (ev.mPointerIdentifiers.length < pointerCount) { 390 ev.mPointerIdentifiers = new int[pointerCount]; 391 } 392 393 if (ev.mEventTimeNanoSamples.length < sampleCount) { 394 ev.mEventTimeNanoSamples = new long[sampleCount]; 395 } 396 397 final int neededDataSamplesLength = pointerCount * sampleCount * NUM_SAMPLE_DATA; 398 if (ev.mDataSamples.length < neededDataSamplesLength) { 399 ev.mDataSamples = new float[neededDataSamplesLength]; 400 } 401 402 return ev; 403 } 404 405 /** 406 * Create a new MotionEvent, filling in all of the basic values that 407 * define the motion. 408 * 409 * @param downTime The time (in ms) when the user originally pressed down to start 410 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 411 * @param eventTime The the time (in ms) when this specific event was generated. This 412 * must be obtained from {@link SystemClock#uptimeMillis()}. 413 * @param action The kind of action being performed -- one of either 414 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 415 * {@link #ACTION_CANCEL}. 416 * @param pointers The number of points that will be in this event. 417 * @param pointerIds An array of <em>pointers</em> values providing 418 * an identifier for each pointer. 419 * @param pointerCoords An array of <em>pointers</em> values providing 420 * a {@link PointerCoords} coordinate object for each pointer. 421 * @param metaState The state of any meta / modifier keys that were in effect when 422 * the event was generated. 423 * @param xPrecision The precision of the X coordinate being reported. 424 * @param yPrecision The precision of the Y coordinate being reported. 425 * @param deviceId The id for the device that this event came from. An id of 426 * zero indicates that the event didn't come from a physical device; other 427 * numbers are arbitrary and you shouldn't depend on the values. 428 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 429 * MotionEvent. 430 * @param source The source of this event. 431 * @param flags The motion event flags. 432 */ 433 static public MotionEvent obtain(long downTime, long eventTime, 434 int action, int pointers, int[] pointerIds, PointerCoords[] pointerCoords, 435 int metaState, float xPrecision, float yPrecision, int deviceId, 436 int edgeFlags, int source, int flags) { 437 MotionEvent ev = obtain(pointers, 1); 438 ev.mDeviceId = deviceId; 439 ev.mSource = source; 440 ev.mEdgeFlags = edgeFlags; 441 ev.mDownTimeNano = downTime * MS_PER_NS; 442 ev.mAction = action; 443 ev.mFlags = flags; 444 ev.mMetaState = metaState; 445 ev.mXOffset = 0; 446 ev.mYOffset = 0; 447 ev.mXPrecision = xPrecision; 448 ev.mYPrecision = yPrecision; 449 450 ev.mNumPointers = pointers; 451 ev.mNumSamples = 1; 452 453 ev.mLastDataSampleIndex = 0; 454 ev.mLastEventTimeNanoSampleIndex = 0; 455 456 System.arraycopy(pointerIds, 0, ev.mPointerIdentifiers, 0, pointers); 457 458 ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS; 459 460 ev.setPointerCoordsAtSampleIndex(0, pointerCoords); 461 462 return ev; 463 } 464 465 /** 466 * Create a new MotionEvent, filling in all of the basic values that 467 * define the motion. 468 * 469 * @param downTime The time (in ms) when the user originally pressed down to start 470 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 471 * @param eventTime The the time (in ms) when this specific event was generated. This 472 * must be obtained from {@link SystemClock#uptimeMillis()}. 473 * @param action The kind of action being performed -- one of either 474 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 475 * {@link #ACTION_CANCEL}. 476 * @param x The X coordinate of this event. 477 * @param y The Y coordinate of this event. 478 * @param pressure The current pressure of this event. The pressure generally 479 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 480 * values higher than 1 may be generated depending on the calibration of 481 * the input device. 482 * @param size A scaled value of the approximate size of the area being pressed when 483 * touched with the finger. The actual value in pixels corresponding to the finger 484 * touch is normalized with a device specific range of values 485 * and scaled to a value between 0 and 1. 486 * @param metaState The state of any meta / modifier keys that were in effect when 487 * the event was generated. 488 * @param xPrecision The precision of the X coordinate being reported. 489 * @param yPrecision The precision of the Y coordinate being reported. 490 * @param deviceId The id for the device that this event came from. An id of 491 * zero indicates that the event didn't come from a physical device; other 492 * numbers are arbitrary and you shouldn't depend on the values. 493 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 494 * MotionEvent. 495 */ 496 static public MotionEvent obtain(long downTime, long eventTime, int action, 497 float x, float y, float pressure, float size, int metaState, 498 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 499 MotionEvent ev = obtain(1, 1); 500 ev.mDeviceId = deviceId; 501 ev.mSource = InputDevice.SOURCE_UNKNOWN; 502 ev.mEdgeFlags = edgeFlags; 503 ev.mDownTimeNano = downTime * MS_PER_NS; 504 ev.mAction = action; 505 ev.mFlags = 0; 506 ev.mMetaState = metaState; 507 ev.mXOffset = 0; 508 ev.mYOffset = 0; 509 ev.mXPrecision = xPrecision; 510 ev.mYPrecision = yPrecision; 511 512 ev.mNumPointers = 1; 513 ev.mNumSamples = 1; 514 515 ev.mLastDataSampleIndex = 0; 516 ev.mLastEventTimeNanoSampleIndex = 0; 517 518 ev.mPointerIdentifiers[0] = 0; 519 520 ev.mEventTimeNanoSamples[0] = eventTime * MS_PER_NS; 521 522 ev.setPointerCoordsAtSampleIndex(0, x, y, pressure, size); 523 return ev; 524 } 525 526 /** 527 * Create a new MotionEvent, filling in all of the basic values that 528 * define the motion. 529 * 530 * @param downTime The time (in ms) when the user originally pressed down to start 531 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 532 * @param eventTime The the time (in ms) when this specific event was generated. This 533 * must be obtained from {@link SystemClock#uptimeMillis()}. 534 * @param action The kind of action being performed -- one of either 535 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 536 * {@link #ACTION_CANCEL}. 537 * @param pointers The number of pointers that are active in this event. 538 * @param x The X coordinate of this event. 539 * @param y The Y coordinate of this event. 540 * @param pressure The current pressure of this event. The pressure generally 541 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 542 * values higher than 1 may be generated depending on the calibration of 543 * the input device. 544 * @param size A scaled value of the approximate size of the area being pressed when 545 * touched with the finger. The actual value in pixels corresponding to the finger 546 * touch is normalized with a device specific range of values 547 * and scaled to a value between 0 and 1. 548 * @param metaState The state of any meta / modifier keys that were in effect when 549 * the event was generated. 550 * @param xPrecision The precision of the X coordinate being reported. 551 * @param yPrecision The precision of the Y coordinate being reported. 552 * @param deviceId The id for the device that this event came from. An id of 553 * zero indicates that the event didn't come from a physical device; other 554 * numbers are arbitrary and you shouldn't depend on the values. 555 * @param edgeFlags A bitfield indicating which edges, if any, were touched by this 556 * MotionEvent. 557 * 558 * @deprecated Use {@link #obtain(long, long, int, float, float, float, float, int, float, float, int, int)} 559 * instead. 560 */ 561 @Deprecated 562 static public MotionEvent obtain(long downTime, long eventTime, int action, 563 int pointers, float x, float y, float pressure, float size, int metaState, 564 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 565 return obtain(downTime, eventTime, action, x, y, pressure, size, 566 metaState, xPrecision, yPrecision, deviceId, edgeFlags); 567 } 568 569 /** 570 * Create a new MotionEvent, filling in a subset of the basic motion 571 * values. Those not specified here are: device id (always 0), pressure 572 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0). 573 * 574 * @param downTime The time (in ms) when the user originally pressed down to start 575 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 576 * @param eventTime The the time (in ms) when this specific event was generated. This 577 * must be obtained from {@link SystemClock#uptimeMillis()}. 578 * @param action The kind of action being performed -- one of either 579 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 580 * {@link #ACTION_CANCEL}. 581 * @param x The X coordinate of this event. 582 * @param y The Y coordinate of this event. 583 * @param metaState The state of any meta / modifier keys that were in effect when 584 * the event was generated. 585 */ 586 static public MotionEvent obtain(long downTime, long eventTime, int action, 587 float x, float y, int metaState) { 588 return obtain(downTime, eventTime, action, x, y, 1.0f, 1.0f, 589 metaState, 1.0f, 1.0f, 0, 0); 590 } 591 592 /** 593 * Create a new MotionEvent, copying from an existing one. 594 */ 595 static public MotionEvent obtain(MotionEvent o) { 596 MotionEvent ev = obtain(o.mNumPointers, o.mNumSamples); 597 ev.mDeviceId = o.mDeviceId; 598 ev.mSource = o.mSource; 599 ev.mEdgeFlags = o.mEdgeFlags; 600 ev.mDownTimeNano = o.mDownTimeNano; 601 ev.mAction = o.mAction; 602 ev.mFlags = o.mFlags; 603 ev.mMetaState = o.mMetaState; 604 ev.mXOffset = o.mXOffset; 605 ev.mYOffset = o.mYOffset; 606 ev.mXPrecision = o.mXPrecision; 607 ev.mYPrecision = o.mYPrecision; 608 int numPointers = ev.mNumPointers = o.mNumPointers; 609 int numSamples = ev.mNumSamples = o.mNumSamples; 610 611 ev.mLastDataSampleIndex = o.mLastDataSampleIndex; 612 ev.mLastEventTimeNanoSampleIndex = o.mLastEventTimeNanoSampleIndex; 613 614 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers); 615 616 System.arraycopy(o.mEventTimeNanoSamples, 0, ev.mEventTimeNanoSamples, 0, numSamples); 617 618 System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0, 619 numPointers * numSamples * NUM_SAMPLE_DATA); 620 return ev; 621 } 622 623 /** 624 * Create a new MotionEvent, copying from an existing one, but not including 625 * any historical point information. 626 */ 627 static public MotionEvent obtainNoHistory(MotionEvent o) { 628 MotionEvent ev = obtain(o.mNumPointers, 1); 629 ev.mDeviceId = o.mDeviceId; 630 ev.mSource = o.mSource; 631 ev.mEdgeFlags = o.mEdgeFlags; 632 ev.mDownTimeNano = o.mDownTimeNano; 633 ev.mAction = o.mAction; 634 o.mFlags = o.mFlags; 635 ev.mMetaState = o.mMetaState; 636 ev.mXOffset = o.mXOffset; 637 ev.mYOffset = o.mYOffset; 638 ev.mXPrecision = o.mXPrecision; 639 ev.mYPrecision = o.mYPrecision; 640 641 int numPointers = ev.mNumPointers = o.mNumPointers; 642 ev.mNumSamples = 1; 643 644 ev.mLastDataSampleIndex = 0; 645 ev.mLastEventTimeNanoSampleIndex = 0; 646 647 System.arraycopy(o.mPointerIdentifiers, 0, ev.mPointerIdentifiers, 0, numPointers); 648 649 ev.mEventTimeNanoSamples[0] = o.mEventTimeNanoSamples[o.mLastEventTimeNanoSampleIndex]; 650 651 System.arraycopy(o.mDataSamples, o.mLastDataSampleIndex, ev.mDataSamples, 0, 652 numPointers * NUM_SAMPLE_DATA); 653 return ev; 654 } 655 656 /** 657 * Recycle the MotionEvent, to be re-used by a later caller. After calling 658 * this function you must not ever touch the event again. 659 */ 660 public final void recycle() { 661 // Ensure recycle is only called once! 662 if (TRACK_RECYCLED_LOCATION) { 663 if (mRecycledLocation != null) { 664 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation); 665 } 666 mRecycledLocation = new RuntimeException("Last recycled here"); 667 //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation); 668 } else { 669 if (mRecycled) { 670 throw new RuntimeException(toString() + " recycled twice!"); 671 } 672 mRecycled = true; 673 } 674 675 synchronized (gRecyclerLock) { 676 if (gRecyclerUsed < MAX_RECYCLED) { 677 gRecyclerUsed++; 678 mNumSamples = 0; 679 mNext = gRecyclerTop; 680 gRecyclerTop = this; 681 } 682 } 683 } 684 685 /** 686 * Scales down the coordination of this event by the given scale. 687 * 688 * @hide 689 */ 690 public final void scale(float scale) { 691 mXOffset *= scale; 692 mYOffset *= scale; 693 mXPrecision *= scale; 694 mYPrecision *= scale; 695 696 float[] history = mDataSamples; 697 final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA; 698 for (int i = 0; i < length; i += NUM_SAMPLE_DATA) { 699 history[i + SAMPLE_X] *= scale; 700 history[i + SAMPLE_Y] *= scale; 701 // no need to scale pressure 702 history[i + SAMPLE_SIZE] *= scale; // TODO: square this? 703 history[i + SAMPLE_TOUCH_MAJOR] *= scale; 704 history[i + SAMPLE_TOUCH_MINOR] *= scale; 705 history[i + SAMPLE_TOOL_MAJOR] *= scale; 706 history[i + SAMPLE_TOOL_MINOR] *= scale; 707 } 708 } 709 710 /** 711 * Return the kind of action being performed -- one of either 712 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 713 * {@link #ACTION_CANCEL}. Consider using {@link #getActionMasked} 714 * and {@link #getActionIndex} to retrieve the separate masked action 715 * and pointer index. 716 */ 717 public final int getAction() { 718 return mAction; 719 } 720 721 /** 722 * Return the masked action being performed, without pointer index 723 * information. May be any of the actions: {@link #ACTION_DOWN}, 724 * {@link #ACTION_MOVE}, {@link #ACTION_UP}, {@link #ACTION_CANCEL}, 725 * {@link #ACTION_POINTER_DOWN}, or {@link #ACTION_POINTER_UP}. 726 * Use {@link #getActionIndex} to return the index associated with 727 * pointer actions. 728 */ 729 public final int getActionMasked() { 730 return mAction & ACTION_MASK; 731 } 732 733 /** 734 * For {@link #ACTION_POINTER_DOWN} or {@link #ACTION_POINTER_UP} 735 * as returned by {@link #getActionMasked}, this returns the associated 736 * pointer index. The index may be used with {@link #getPointerId(int)}, 737 * {@link #getX(int)}, {@link #getY(int)}, {@link #getPressure(int)}, 738 * and {@link #getSize(int)} to get information about the pointer that has 739 * gone down or up. 740 */ 741 public final int getActionIndex() { 742 return (mAction & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT; 743 } 744 745 /** 746 * Gets the motion event flags. 747 * 748 * @see #FLAG_WINDOW_IS_OBSCURED 749 */ 750 public final int getFlags() { 751 return mFlags; 752 } 753 754 /** 755 * Returns the time (in ms) when the user originally pressed down to start 756 * a stream of position events. 757 */ 758 public final long getDownTime() { 759 return mDownTimeNano / MS_PER_NS; 760 } 761 762 /** 763 * Returns the time (in ms) when this specific event was generated. 764 */ 765 public final long getEventTime() { 766 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] / MS_PER_NS; 767 } 768 769 /** 770 * Returns the time (in ns) when this specific event was generated. 771 * The value is in nanosecond precision but it may not have nanosecond accuracy. 772 * 773 * @hide 774 */ 775 public final long getEventTimeNano() { 776 return mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex]; 777 } 778 779 /** 780 * {@link #getX(int)} for the first pointer index (may be an 781 * arbitrary pointer identifier). 782 */ 783 public final float getX() { 784 return mDataSamples[mLastDataSampleIndex + SAMPLE_X] + mXOffset; 785 } 786 787 /** 788 * {@link #getY(int)} for the first pointer index (may be an 789 * arbitrary pointer identifier). 790 */ 791 public final float getY() { 792 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y] + mYOffset; 793 } 794 795 /** 796 * {@link #getPressure(int)} for the first pointer index (may be an 797 * arbitrary pointer identifier). 798 */ 799 public final float getPressure() { 800 return mDataSamples[mLastDataSampleIndex + SAMPLE_PRESSURE]; 801 } 802 803 /** 804 * {@link #getSize(int)} for the first pointer index (may be an 805 * arbitrary pointer identifier). 806 */ 807 public final float getSize() { 808 return mDataSamples[mLastDataSampleIndex + SAMPLE_SIZE]; 809 } 810 811 /** 812 * {@link #getTouchMajor(int)} for the first pointer index (may be an 813 * arbitrary pointer identifier). 814 */ 815 public final float getTouchMajor() { 816 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MAJOR]; 817 } 818 819 /** 820 * {@link #getTouchMinor(int)} for the first pointer index (may be an 821 * arbitrary pointer identifier). 822 */ 823 public final float getTouchMinor() { 824 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOUCH_MINOR]; 825 } 826 827 /** 828 * {@link #getToolMajor(int)} for the first pointer index (may be an 829 * arbitrary pointer identifier). 830 */ 831 public final float getToolMajor() { 832 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MAJOR]; 833 } 834 835 /** 836 * {@link #getToolMinor(int)} for the first pointer index (may be an 837 * arbitrary pointer identifier). 838 */ 839 public final float getToolMinor() { 840 return mDataSamples[mLastDataSampleIndex + SAMPLE_TOOL_MINOR]; 841 } 842 843 /** 844 * {@link #getOrientation(int)} for the first pointer index (may be an 845 * arbitrary pointer identifier). 846 */ 847 public final float getOrientation() { 848 return mDataSamples[mLastDataSampleIndex + SAMPLE_ORIENTATION]; 849 } 850 851 /** 852 * The number of pointers of data contained in this event. Always 853 * >= 1. 854 */ 855 public final int getPointerCount() { 856 return mNumPointers; 857 } 858 859 /** 860 * Return the pointer identifier associated with a particular pointer 861 * data index is this event. The identifier tells you the actual pointer 862 * number associated with the data, accounting for individual pointers 863 * going up and down since the start of the current gesture. 864 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 865 * (the first pointer that is down) to {@link #getPointerCount()}-1. 866 */ 867 public final int getPointerId(int pointerIndex) { 868 return mPointerIdentifiers[pointerIndex]; 869 } 870 871 /** 872 * Given a pointer identifier, find the index of its data in the event. 873 * 874 * @param pointerId The identifier of the pointer to be found. 875 * @return Returns either the index of the pointer (for use with 876 * {@link #getX(int)} et al.), or -1 if there is no data available for 877 * that pointer identifier. 878 */ 879 public final int findPointerIndex(int pointerId) { 880 int i = mNumPointers; 881 while (i > 0) { 882 i--; 883 if (mPointerIdentifiers[i] == pointerId) { 884 return i; 885 } 886 } 887 return -1; 888 } 889 890 /** 891 * Returns the X coordinate of this event for the given pointer 892 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 893 * identifier for this index). 894 * Whole numbers are pixels; the 895 * value may have a fraction for input devices that are sub-pixel precise. 896 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 897 * (the first pointer that is down) to {@link #getPointerCount()}-1. 898 */ 899 public final float getX(int pointerIndex) { 900 return mDataSamples[mLastDataSampleIndex 901 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset; 902 } 903 904 /** 905 * Returns the Y coordinate of this event for the given pointer 906 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 907 * identifier for this index). 908 * Whole numbers are pixels; the 909 * value may have a fraction for input devices that are sub-pixel precise. 910 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 911 * (the first pointer that is down) to {@link #getPointerCount()}-1. 912 */ 913 public final float getY(int pointerIndex) { 914 return mDataSamples[mLastDataSampleIndex 915 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset; 916 } 917 918 /** 919 * Returns the current pressure of this event for the given pointer 920 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 921 * identifier for this index). 922 * The pressure generally 923 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 924 * values higher than 1 may be generated depending on the calibration of 925 * the input device. 926 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 927 * (the first pointer that is down) to {@link #getPointerCount()}-1. 928 */ 929 public final float getPressure(int pointerIndex) { 930 return mDataSamples[mLastDataSampleIndex 931 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_PRESSURE]; 932 } 933 934 /** 935 * Returns a scaled value of the approximate size for the given pointer 936 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 937 * identifier for this index). 938 * This represents some approximation of the area of the screen being 939 * pressed; the actual value in pixels corresponding to the 940 * touch is normalized with the device specific range of values 941 * and scaled to a value between 0 and 1. The value of size can be used to 942 * determine fat touch events. 943 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 944 * (the first pointer that is down) to {@link #getPointerCount()}-1. 945 */ 946 public final float getSize(int pointerIndex) { 947 return mDataSamples[mLastDataSampleIndex 948 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_SIZE]; 949 } 950 951 /** 952 * Returns the length of the major axis of an ellipse that describes the touch 953 * area at the point of contact for the given pointer 954 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 955 * identifier for this index). 956 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 957 * (the first pointer that is down) to {@link #getPointerCount()}-1. 958 */ 959 public final float getTouchMajor(int pointerIndex) { 960 return mDataSamples[mLastDataSampleIndex 961 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR]; 962 } 963 964 /** 965 * Returns the length of the minor axis of an ellipse that describes the touch 966 * area at the point of contact for the given pointer 967 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 968 * identifier for this index). 969 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 970 * (the first pointer that is down) to {@link #getPointerCount()}-1. 971 */ 972 public final float getTouchMinor(int pointerIndex) { 973 return mDataSamples[mLastDataSampleIndex 974 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR]; 975 } 976 977 /** 978 * Returns the length of the major axis of an ellipse that describes the size of 979 * the approaching tool for the given pointer 980 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 981 * identifier for this index). 982 * The tool area represents the estimated size of the finger or pen that is 983 * touching the device independent of its actual touch area at the point of contact. 984 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 985 * (the first pointer that is down) to {@link #getPointerCount()}-1. 986 */ 987 public final float getToolMajor(int pointerIndex) { 988 return mDataSamples[mLastDataSampleIndex 989 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR]; 990 } 991 992 /** 993 * Returns the length of the minor axis of an ellipse that describes the size of 994 * the approaching tool for the given pointer 995 * <em>index</em> (use {@link #getPointerId(int)} to find the pointer 996 * identifier for this index). 997 * The tool area represents the estimated size of the finger or pen that is 998 * touching the device independent of its actual touch area at the point of contact. 999 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1000 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1001 */ 1002 public final float getToolMinor(int pointerIndex) { 1003 return mDataSamples[mLastDataSampleIndex 1004 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR]; 1005 } 1006 1007 /** 1008 * Returns the orientation of the touch area and tool area in radians clockwise from vertical 1009 * for the given pointer <em>index</em> (use {@link #getPointerId(int)} to find the pointer 1010 * identifier for this index). 1011 * An angle of 0 degrees indicates that the major axis of contact is oriented 1012 * upwards, is perfectly circular or is of unknown orientation. A positive angle 1013 * indicates that the major axis of contact is oriented to the right. A negative angle 1014 * indicates that the major axis of contact is oriented to the left. 1015 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians 1016 * (finger pointing fully right). 1017 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1018 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1019 */ 1020 public final float getOrientation(int pointerIndex) { 1021 return mDataSamples[mLastDataSampleIndex 1022 + pointerIndex * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION]; 1023 } 1024 1025 /** 1026 * Populates a {@link PointerCoords} object with pointer coordinate data for 1027 * the specified pointer index. 1028 * 1029 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1030 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1031 * @param outPointerCoords The pointer coordinate object to populate. 1032 */ 1033 public final void getPointerCoords(int pointerIndex, PointerCoords outPointerCoords) { 1034 final int sampleIndex = mLastDataSampleIndex + pointerIndex * NUM_SAMPLE_DATA; 1035 getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords); 1036 } 1037 1038 /** 1039 * Returns the state of any meta / modifier keys that were in effect when 1040 * the event was generated. This is the same values as those 1041 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}. 1042 * 1043 * @return an integer in which each bit set to 1 represents a pressed 1044 * meta key 1045 * 1046 * @see KeyEvent#getMetaState() 1047 */ 1048 public final int getMetaState() { 1049 return mMetaState; 1050 } 1051 1052 /** 1053 * Returns the original raw X coordinate of this event. For touch 1054 * events on the screen, this is the original location of the event 1055 * on the screen, before it had been adjusted for the containing window 1056 * and views. 1057 */ 1058 public final float getRawX() { 1059 return mDataSamples[mLastDataSampleIndex + SAMPLE_X]; 1060 } 1061 1062 /** 1063 * Returns the original raw Y coordinate of this event. For touch 1064 * events on the screen, this is the original location of the event 1065 * on the screen, before it had been adjusted for the containing window 1066 * and views. 1067 */ 1068 public final float getRawY() { 1069 return mDataSamples[mLastDataSampleIndex + SAMPLE_Y]; 1070 } 1071 1072 /** 1073 * Return the precision of the X coordinates being reported. You can 1074 * multiple this number with {@link #getX} to find the actual hardware 1075 * value of the X coordinate. 1076 * @return Returns the precision of X coordinates being reported. 1077 */ 1078 public final float getXPrecision() { 1079 return mXPrecision; 1080 } 1081 1082 /** 1083 * Return the precision of the Y coordinates being reported. You can 1084 * multiple this number with {@link #getY} to find the actual hardware 1085 * value of the Y coordinate. 1086 * @return Returns the precision of Y coordinates being reported. 1087 */ 1088 public final float getYPrecision() { 1089 return mYPrecision; 1090 } 1091 1092 /** 1093 * Returns the number of historical points in this event. These are 1094 * movements that have occurred between this event and the previous event. 1095 * This only applies to ACTION_MOVE events -- all other actions will have 1096 * a size of 0. 1097 * 1098 * @return Returns the number of historical points in the event. 1099 */ 1100 public final int getHistorySize() { 1101 return mLastEventTimeNanoSampleIndex; 1102 } 1103 1104 /** 1105 * Returns the time that a historical movement occurred between this event 1106 * and the previous event. Only applies to ACTION_MOVE events. 1107 * 1108 * @param pos Which historical value to return; must be less than 1109 * {@link #getHistorySize} 1110 * 1111 * @see #getHistorySize 1112 * @see #getEventTime 1113 */ 1114 public final long getHistoricalEventTime(int pos) { 1115 return mEventTimeNanoSamples[pos] / MS_PER_NS; 1116 } 1117 1118 /** 1119 * {@link #getHistoricalX(int)} for the first pointer index (may be an 1120 * arbitrary pointer identifier). 1121 */ 1122 public final float getHistoricalX(int pos) { 1123 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset; 1124 } 1125 1126 /** 1127 * {@link #getHistoricalY(int)} for the first pointer index (may be an 1128 * arbitrary pointer identifier). 1129 */ 1130 public final float getHistoricalY(int pos) { 1131 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset; 1132 } 1133 1134 /** 1135 * {@link #getHistoricalPressure(int)} for the first pointer index (may be an 1136 * arbitrary pointer identifier). 1137 */ 1138 public final float getHistoricalPressure(int pos) { 1139 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_PRESSURE]; 1140 } 1141 1142 /** 1143 * {@link #getHistoricalSize(int)} for the first pointer index (may be an 1144 * arbitrary pointer identifier). 1145 */ 1146 public final float getHistoricalSize(int pos) { 1147 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_SIZE]; 1148 } 1149 1150 /** 1151 * {@link #getHistoricalTouchMajor(int)} for the first pointer index (may be an 1152 * arbitrary pointer identifier). 1153 */ 1154 public final float getHistoricalTouchMajor(int pos) { 1155 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR]; 1156 } 1157 1158 /** 1159 * {@link #getHistoricalTouchMinor(int)} for the first pointer index (may be an 1160 * arbitrary pointer identifier). 1161 */ 1162 public final float getHistoricalTouchMinor(int pos) { 1163 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR]; 1164 } 1165 1166 /** 1167 * {@link #getHistoricalToolMajor(int)} for the first pointer index (may be an 1168 * arbitrary pointer identifier). 1169 */ 1170 public final float getHistoricalToolMajor(int pos) { 1171 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR]; 1172 } 1173 1174 /** 1175 * {@link #getHistoricalToolMinor(int)} for the first pointer index (may be an 1176 * arbitrary pointer identifier). 1177 */ 1178 public final float getHistoricalToolMinor(int pos) { 1179 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR]; 1180 } 1181 1182 /** 1183 * {@link #getHistoricalOrientation(int)} for the first pointer index (may be an 1184 * arbitrary pointer identifier). 1185 */ 1186 public final float getHistoricalOrientation(int pos) { 1187 return mDataSamples[pos * mNumPointers * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION]; 1188 } 1189 1190 /** 1191 * Returns a historical X coordinate, as per {@link #getX(int)}, that 1192 * occurred between this event and the previous event for the given pointer. 1193 * Only applies to ACTION_MOVE events. 1194 * 1195 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1196 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1197 * @param pos Which historical value to return; must be less than 1198 * {@link #getHistorySize} 1199 * 1200 * @see #getHistorySize 1201 * @see #getX 1202 */ 1203 public final float getHistoricalX(int pointerIndex, int pos) { 1204 return mDataSamples[(pos * mNumPointers + pointerIndex) 1205 * NUM_SAMPLE_DATA + SAMPLE_X] + mXOffset; 1206 } 1207 1208 /** 1209 * Returns a historical Y coordinate, as per {@link #getY(int)}, that 1210 * occurred between this event and the previous event for the given pointer. 1211 * Only applies to ACTION_MOVE events. 1212 * 1213 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1214 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1215 * @param pos Which historical value to return; must be less than 1216 * {@link #getHistorySize} 1217 * 1218 * @see #getHistorySize 1219 * @see #getY 1220 */ 1221 public final float getHistoricalY(int pointerIndex, int pos) { 1222 return mDataSamples[(pos * mNumPointers + pointerIndex) 1223 * NUM_SAMPLE_DATA + SAMPLE_Y] + mYOffset; 1224 } 1225 1226 /** 1227 * Returns a historical pressure coordinate, as per {@link #getPressure(int)}, 1228 * that occurred between this event and the previous event for the given 1229 * pointer. Only applies to ACTION_MOVE events. 1230 * 1231 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1232 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1233 * @param pos Which historical value to return; must be less than 1234 * {@link #getHistorySize} 1235 * 1236 * @see #getHistorySize 1237 * @see #getPressure 1238 */ 1239 public final float getHistoricalPressure(int pointerIndex, int pos) { 1240 return mDataSamples[(pos * mNumPointers + pointerIndex) 1241 * NUM_SAMPLE_DATA + SAMPLE_PRESSURE]; 1242 } 1243 1244 /** 1245 * Returns a historical size coordinate, as per {@link #getSize(int)}, that 1246 * occurred between this event and the previous event for the given pointer. 1247 * Only applies to ACTION_MOVE events. 1248 * 1249 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1250 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1251 * @param pos Which historical value to return; must be less than 1252 * {@link #getHistorySize} 1253 * 1254 * @see #getHistorySize 1255 * @see #getSize 1256 */ 1257 public final float getHistoricalSize(int pointerIndex, int pos) { 1258 return mDataSamples[(pos * mNumPointers + pointerIndex) 1259 * NUM_SAMPLE_DATA + SAMPLE_SIZE]; 1260 } 1261 1262 /** 1263 * Returns a historical touch major axis coordinate, as per {@link #getTouchMajor(int)}, that 1264 * occurred between this event and the previous event for the given pointer. 1265 * Only applies to ACTION_MOVE events. 1266 * 1267 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1268 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1269 * @param pos Which historical value to return; must be less than 1270 * {@link #getHistorySize} 1271 * 1272 * @see #getHistorySize 1273 * @see #getTouchMajor 1274 */ 1275 public final float getHistoricalTouchMajor(int pointerIndex, int pos) { 1276 return mDataSamples[(pos * mNumPointers + pointerIndex) 1277 * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MAJOR]; 1278 } 1279 1280 /** 1281 * Returns a historical touch minor axis coordinate, as per {@link #getTouchMinor(int)}, that 1282 * occurred between this event and the previous event for the given pointer. 1283 * Only applies to ACTION_MOVE events. 1284 * 1285 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1286 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1287 * @param pos Which historical value to return; must be less than 1288 * {@link #getHistorySize} 1289 * 1290 * @see #getHistorySize 1291 * @see #getTouchMinor 1292 */ 1293 public final float getHistoricalTouchMinor(int pointerIndex, int pos) { 1294 return mDataSamples[(pos * mNumPointers + pointerIndex) 1295 * NUM_SAMPLE_DATA + SAMPLE_TOUCH_MINOR]; 1296 } 1297 1298 /** 1299 * Returns a historical tool major axis coordinate, as per {@link #getToolMajor(int)}, that 1300 * occurred between this event and the previous event for the given pointer. 1301 * Only applies to ACTION_MOVE events. 1302 * 1303 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1304 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1305 * @param pos Which historical value to return; must be less than 1306 * {@link #getHistorySize} 1307 * 1308 * @see #getHistorySize 1309 * @see #getToolMajor 1310 */ 1311 public final float getHistoricalToolMajor(int pointerIndex, int pos) { 1312 return mDataSamples[(pos * mNumPointers + pointerIndex) 1313 * NUM_SAMPLE_DATA + SAMPLE_TOOL_MAJOR]; 1314 } 1315 1316 /** 1317 * Returns a historical tool minor axis coordinate, as per {@link #getToolMinor(int)}, that 1318 * occurred between this event and the previous event for the given pointer. 1319 * Only applies to ACTION_MOVE events. 1320 * 1321 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1322 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1323 * @param pos Which historical value to return; must be less than 1324 * {@link #getHistorySize} 1325 * 1326 * @see #getHistorySize 1327 * @see #getToolMinor 1328 */ 1329 public final float getHistoricalToolMinor(int pointerIndex, int pos) { 1330 return mDataSamples[(pos * mNumPointers + pointerIndex) 1331 * NUM_SAMPLE_DATA + SAMPLE_TOOL_MINOR]; 1332 } 1333 1334 /** 1335 * Returns a historical orientation coordinate, as per {@link #getOrientation(int)}, that 1336 * occurred between this event and the previous event for the given pointer. 1337 * Only applies to ACTION_MOVE events. 1338 * 1339 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1340 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1341 * @param pos Which historical value to return; must be less than 1342 * {@link #getHistorySize} 1343 * 1344 * @see #getHistorySize 1345 * @see #getOrientation 1346 */ 1347 public final float getHistoricalOrientation(int pointerIndex, int pos) { 1348 return mDataSamples[(pos * mNumPointers + pointerIndex) 1349 * NUM_SAMPLE_DATA + SAMPLE_ORIENTATION]; 1350 } 1351 1352 /** 1353 * Populates a {@link PointerCoords} object with historical pointer coordinate data, 1354 * as per {@link #getPointerCoords}, that occurred between this event and the previous 1355 * event for the given pointer. 1356 * Only applies to ACTION_MOVE events. 1357 * 1358 * @param pointerIndex Raw index of pointer to retrieve. Value may be from 0 1359 * (the first pointer that is down) to {@link #getPointerCount()}-1. 1360 * @param pos Which historical value to return; must be less than 1361 * {@link #getHistorySize} 1362 * @param outPointerCoords The pointer coordinate object to populate. 1363 * 1364 * @see #getHistorySize 1365 * @see #getPointerCoords 1366 */ 1367 public final void getHistoricalPointerCoords(int pointerIndex, int pos, 1368 PointerCoords outPointerCoords) { 1369 final int sampleIndex = (pos * mNumPointers + pointerIndex) * NUM_SAMPLE_DATA; 1370 getPointerCoordsAtSampleIndex(sampleIndex, outPointerCoords); 1371 } 1372 1373 /** 1374 * Returns a bitfield indicating which edges, if any, were touched by this 1375 * MotionEvent. For touch events, clients can use this to determine if the 1376 * user's finger was touching the edge of the display. 1377 * 1378 * @see #EDGE_LEFT 1379 * @see #EDGE_TOP 1380 * @see #EDGE_RIGHT 1381 * @see #EDGE_BOTTOM 1382 */ 1383 public final int getEdgeFlags() { 1384 return mEdgeFlags; 1385 } 1386 1387 1388 /** 1389 * Sets the bitfield indicating which edges, if any, were touched by this 1390 * MotionEvent. 1391 * 1392 * @see #getEdgeFlags() 1393 */ 1394 public final void setEdgeFlags(int flags) { 1395 mEdgeFlags = flags; 1396 } 1397 1398 /** 1399 * Sets this event's action. 1400 */ 1401 public final void setAction(int action) { 1402 mAction = action; 1403 } 1404 1405 /** 1406 * Adjust this event's location. 1407 * @param deltaX Amount to add to the current X coordinate of the event. 1408 * @param deltaY Amount to add to the current Y coordinate of the event. 1409 */ 1410 public final void offsetLocation(float deltaX, float deltaY) { 1411 mXOffset += deltaX; 1412 mYOffset += deltaY; 1413 } 1414 1415 /** 1416 * Set this event's location. Applies {@link #offsetLocation} with a 1417 * delta from the current location to the given new location. 1418 * 1419 * @param x New absolute X location. 1420 * @param y New absolute Y location. 1421 */ 1422 public final void setLocation(float x, float y) { 1423 final float[] dataSamples = mDataSamples; 1424 final int lastDataSampleIndex = mLastDataSampleIndex; 1425 mXOffset = x - dataSamples[lastDataSampleIndex + SAMPLE_X]; 1426 mYOffset = y - dataSamples[lastDataSampleIndex + SAMPLE_Y]; 1427 } 1428 1429 /** 1430 * Applies a transformation matrix to all of the points in the event. 1431 * 1432 * @param matrix The transformation matrix to apply. 1433 */ 1434 public final void transform(Matrix matrix) { 1435 if (matrix == null) { 1436 throw new IllegalArgumentException("matrix must not be null"); 1437 } 1438 1439 nativeTransform(matrix); 1440 } 1441 1442 private final void getPointerCoordsAtSampleIndex(int sampleIndex, 1443 PointerCoords outPointerCoords) { 1444 final float[] dataSamples = mDataSamples; 1445 outPointerCoords.x = dataSamples[sampleIndex + SAMPLE_X] + mXOffset; 1446 outPointerCoords.y = dataSamples[sampleIndex + SAMPLE_Y] + mYOffset; 1447 outPointerCoords.pressure = dataSamples[sampleIndex + SAMPLE_PRESSURE]; 1448 outPointerCoords.size = dataSamples[sampleIndex + SAMPLE_SIZE]; 1449 outPointerCoords.touchMajor = dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR]; 1450 outPointerCoords.touchMinor = dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR]; 1451 outPointerCoords.toolMajor = dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR]; 1452 outPointerCoords.toolMinor = dataSamples[sampleIndex + SAMPLE_TOOL_MINOR]; 1453 outPointerCoords.orientation = dataSamples[sampleIndex + SAMPLE_ORIENTATION]; 1454 } 1455 1456 private final void setPointerCoordsAtSampleIndex(int sampleIndex, 1457 PointerCoords[] pointerCoords) { 1458 final int numPointers = mNumPointers; 1459 for (int i = 0; i < numPointers; i++) { 1460 setPointerCoordsAtSampleIndex(sampleIndex, pointerCoords[i]); 1461 sampleIndex += NUM_SAMPLE_DATA; 1462 } 1463 } 1464 1465 private final void setPointerCoordsAtSampleIndex(int sampleIndex, 1466 PointerCoords pointerCoords) { 1467 final float[] dataSamples = mDataSamples; 1468 dataSamples[sampleIndex + SAMPLE_X] = pointerCoords.x - mXOffset; 1469 dataSamples[sampleIndex + SAMPLE_Y] = pointerCoords.y - mYOffset; 1470 dataSamples[sampleIndex + SAMPLE_PRESSURE] = pointerCoords.pressure; 1471 dataSamples[sampleIndex + SAMPLE_SIZE] = pointerCoords.size; 1472 dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pointerCoords.touchMajor; 1473 dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pointerCoords.touchMinor; 1474 dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = pointerCoords.toolMajor; 1475 dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = pointerCoords.toolMinor; 1476 dataSamples[sampleIndex + SAMPLE_ORIENTATION] = pointerCoords.orientation; 1477 } 1478 1479 private final void setPointerCoordsAtSampleIndex(int sampleIndex, 1480 float x, float y, float pressure, float size) { 1481 final float[] dataSamples = mDataSamples; 1482 dataSamples[sampleIndex + SAMPLE_X] = x - mXOffset; 1483 dataSamples[sampleIndex + SAMPLE_Y] = y - mYOffset; 1484 dataSamples[sampleIndex + SAMPLE_PRESSURE] = pressure; 1485 dataSamples[sampleIndex + SAMPLE_SIZE] = size; 1486 dataSamples[sampleIndex + SAMPLE_TOUCH_MAJOR] = pressure; 1487 dataSamples[sampleIndex + SAMPLE_TOUCH_MINOR] = pressure; 1488 dataSamples[sampleIndex + SAMPLE_TOOL_MAJOR] = size; 1489 dataSamples[sampleIndex + SAMPLE_TOOL_MINOR] = size; 1490 dataSamples[sampleIndex + SAMPLE_ORIENTATION] = 0; 1491 } 1492 1493 private final void incrementNumSamplesAndReserveStorage(int dataSampleStride) { 1494 if (mNumSamples == mEventTimeNanoSamples.length) { 1495 long[] newEventTimeNanoSamples = new long[mNumSamples + BASE_AVAIL_SAMPLES]; 1496 System.arraycopy(mEventTimeNanoSamples, 0, newEventTimeNanoSamples, 0, mNumSamples); 1497 mEventTimeNanoSamples = newEventTimeNanoSamples; 1498 } 1499 1500 int nextDataSampleIndex = mLastDataSampleIndex + dataSampleStride; 1501 if (nextDataSampleIndex + dataSampleStride > mDataSamples.length) { 1502 float[] newDataSamples = new float[nextDataSampleIndex 1503 + BASE_AVAIL_SAMPLES * dataSampleStride]; 1504 System.arraycopy(mDataSamples, 0, newDataSamples, 0, nextDataSampleIndex); 1505 mDataSamples = newDataSamples; 1506 } 1507 1508 mLastEventTimeNanoSampleIndex = mNumSamples; 1509 mLastDataSampleIndex = nextDataSampleIndex; 1510 mNumSamples += 1; 1511 } 1512 1513 /** 1514 * Add a new movement to the batch of movements in this event. The event's 1515 * current location, position and size is updated to the new values. 1516 * The current values in the event are added to a list of historical values. 1517 * 1518 * Only applies to {@link #ACTION_MOVE} events. 1519 * 1520 * @param eventTime The time stamp (in ms) for this data. 1521 * @param x The new X position. 1522 * @param y The new Y position. 1523 * @param pressure The new pressure. 1524 * @param size The new size. 1525 * @param metaState Meta key state. 1526 */ 1527 public final void addBatch(long eventTime, float x, float y, 1528 float pressure, float size, int metaState) { 1529 incrementNumSamplesAndReserveStorage(NUM_SAMPLE_DATA); 1530 1531 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS; 1532 setPointerCoordsAtSampleIndex(mLastDataSampleIndex, x, y, pressure, size); 1533 1534 mMetaState |= metaState; 1535 } 1536 1537 /** 1538 * Add a new movement to the batch of movements in this event. The event's 1539 * current location, position and size is updated to the new values. 1540 * The current values in the event are added to a list of historical values. 1541 * 1542 * Only applies to {@link #ACTION_MOVE} events. 1543 * 1544 * @param eventTime The time stamp (in ms) for this data. 1545 * @param pointerCoords The new pointer coordinates. 1546 * @param metaState Meta key state. 1547 */ 1548 public final void addBatch(long eventTime, PointerCoords[] pointerCoords, int metaState) { 1549 final int dataSampleStride = mNumPointers * NUM_SAMPLE_DATA; 1550 incrementNumSamplesAndReserveStorage(dataSampleStride); 1551 1552 mEventTimeNanoSamples[mLastEventTimeNanoSampleIndex] = eventTime * MS_PER_NS; 1553 setPointerCoordsAtSampleIndex(mLastDataSampleIndex, pointerCoords); 1554 1555 mMetaState |= metaState; 1556 } 1557 1558 @Override 1559 public String toString() { 1560 return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this)) 1561 + " pointerId=" + getPointerId(0) 1562 + " action=" + actionToString(mAction) 1563 + " x=" + getX() 1564 + " y=" + getY() 1565 + " pressure=" + getPressure() 1566 + " size=" + getSize() 1567 + " touchMajor=" + getTouchMajor() 1568 + " touchMinor=" + getTouchMinor() 1569 + " toolMajor=" + getToolMajor() 1570 + " toolMinor=" + getToolMinor() 1571 + " orientation=" + getOrientation() 1572 + " meta=" + KeyEvent.metaStateToString(mMetaState) 1573 + " pointerCount=" + getPointerCount() 1574 + " historySize=" + getHistorySize() 1575 + " flags=0x" + Integer.toHexString(mFlags) 1576 + " edgeFlags=0x" + Integer.toHexString(mEdgeFlags) 1577 + " device=" + mDeviceId 1578 + " source=0x" + Integer.toHexString(mSource) 1579 + (getPointerCount() > 1 ? 1580 " pointerId2=" + getPointerId(1) + " x2=" + getX(1) + " y2=" + getY(1) : "") 1581 + "}"; 1582 } 1583 1584 /** 1585 * Returns a string that represents the symbolic name of the specified action 1586 * such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or "35" (if unknown). 1587 * 1588 * @param action The action. 1589 * @return The symbolic name of the specified action. 1590 * @hide 1591 */ 1592 public static String actionToString(int action) { 1593 switch (action) { 1594 case ACTION_DOWN: 1595 return "ACTION_DOWN"; 1596 case ACTION_UP: 1597 return "ACTION_UP"; 1598 case ACTION_CANCEL: 1599 return "ACTION_CANCEL"; 1600 case ACTION_MOVE: 1601 return "ACTION_MOVE"; 1602 } 1603 int index = (action & ACTION_POINTER_INDEX_MASK) >> ACTION_POINTER_INDEX_SHIFT; 1604 switch (action & ACTION_MASK) { 1605 case ACTION_POINTER_DOWN: 1606 return "ACTION_POINTER_DOWN(" + index + ")"; 1607 case ACTION_POINTER_UP: 1608 return "ACTION_POINTER_UP(" + index + ")"; 1609 default: 1610 return Integer.toString(action); 1611 } 1612 } 1613 1614 public static final Parcelable.Creator<MotionEvent> CREATOR 1615 = new Parcelable.Creator<MotionEvent>() { 1616 public MotionEvent createFromParcel(Parcel in) { 1617 in.readInt(); // skip token, we already know this is a MotionEvent 1618 return MotionEvent.createFromParcelBody(in); 1619 } 1620 1621 public MotionEvent[] newArray(int size) { 1622 return new MotionEvent[size]; 1623 } 1624 }; 1625 1626 /** @hide */ 1627 public static MotionEvent createFromParcelBody(Parcel in) { 1628 final int NP = in.readInt(); 1629 final int NS = in.readInt(); 1630 final int NI = NP * NS * NUM_SAMPLE_DATA; 1631 1632 MotionEvent ev = obtain(NP, NS); 1633 ev.mNumPointers = NP; 1634 ev.mNumSamples = NS; 1635 1636 ev.readBaseFromParcel(in); 1637 1638 ev.mDownTimeNano = in.readLong(); 1639 ev.mAction = in.readInt(); 1640 ev.mXOffset = in.readFloat(); 1641 ev.mYOffset = in.readFloat(); 1642 ev.mXPrecision = in.readFloat(); 1643 ev.mYPrecision = in.readFloat(); 1644 ev.mEdgeFlags = in.readInt(); 1645 ev.mMetaState = in.readInt(); 1646 ev.mFlags = in.readInt(); 1647 1648 final int[] pointerIdentifiers = ev.mPointerIdentifiers; 1649 for (int i = 0; i < NP; i++) { 1650 pointerIdentifiers[i] = in.readInt(); 1651 } 1652 1653 final long[] eventTimeNanoSamples = ev.mEventTimeNanoSamples; 1654 for (int i = 0; i < NS; i++) { 1655 eventTimeNanoSamples[i] = in.readLong(); 1656 } 1657 1658 final float[] dataSamples = ev.mDataSamples; 1659 for (int i = 0; i < NI; i++) { 1660 dataSamples[i] = in.readFloat(); 1661 } 1662 1663 ev.mLastEventTimeNanoSampleIndex = NS - 1; 1664 ev.mLastDataSampleIndex = (NS - 1) * NP * NUM_SAMPLE_DATA; 1665 return ev; 1666 } 1667 1668 public void writeToParcel(Parcel out, int flags) { 1669 out.writeInt(PARCEL_TOKEN_MOTION_EVENT); 1670 1671 final int NP = mNumPointers; 1672 final int NS = mNumSamples; 1673 final int NI = NP * NS * NUM_SAMPLE_DATA; 1674 1675 out.writeInt(NP); 1676 out.writeInt(NS); 1677 1678 writeBaseToParcel(out); 1679 1680 out.writeLong(mDownTimeNano); 1681 out.writeInt(mAction); 1682 out.writeFloat(mXOffset); 1683 out.writeFloat(mYOffset); 1684 out.writeFloat(mXPrecision); 1685 out.writeFloat(mYPrecision); 1686 out.writeInt(mEdgeFlags); 1687 out.writeInt(mMetaState); 1688 out.writeInt(mFlags); 1689 1690 final int[] pointerIdentifiers = mPointerIdentifiers; 1691 for (int i = 0; i < NP; i++) { 1692 out.writeInt(pointerIdentifiers[i]); 1693 } 1694 1695 final long[] eventTimeNanoSamples = mEventTimeNanoSamples; 1696 for (int i = 0; i < NS; i++) { 1697 out.writeLong(eventTimeNanoSamples[i]); 1698 } 1699 1700 final float[] dataSamples = mDataSamples; 1701 for (int i = 0; i < NI; i++) { 1702 out.writeFloat(dataSamples[i]); 1703 } 1704 } 1705 1706 /** 1707 * Transfer object for pointer coordinates. 1708 * 1709 * Objects of this type can be used to manufacture new {@link MotionEvent} objects 1710 * and to query pointer coordinate information in bulk. 1711 * 1712 * Refer to {@link InputDevice} for information about how different kinds of 1713 * input devices and sources represent pointer coordinates. 1714 */ 1715 public static final class PointerCoords { 1716 /** 1717 * The X coordinate of the pointer movement. 1718 * The interpretation varies by input source and may represent the position of 1719 * the center of the contact area, a relative displacement in device-specific units 1720 * or something else. 1721 */ 1722 public float x; 1723 1724 /** 1725 * The Y coordinate of the pointer movement. 1726 * The interpretation varies by input source and may represent the position of 1727 * the center of the contact area, a relative displacement in device-specific units 1728 * or something else. 1729 */ 1730 public float y; 1731 1732 /** 1733 * A scaled value that describes the pressure applied to the pointer. 1734 * The pressure generally ranges from 0 (no pressure at all) to 1 (normal pressure), 1735 * however values higher than 1 may be generated depending on the calibration of 1736 * the input device. 1737 */ 1738 public float pressure; 1739 1740 /** 1741 * A scaled value of the approximate size of the pointer touch area. 1742 * This represents some approximation of the area of the screen being 1743 * pressed; the actual value in pixels corresponding to the 1744 * touch is normalized with the device specific range of values 1745 * and scaled to a value between 0 and 1. The value of size can be used to 1746 * determine fat touch events. 1747 */ 1748 public float size; 1749 1750 /** 1751 * The length of the major axis of an ellipse that describes the touch area at 1752 * the point of contact. 1753 */ 1754 public float touchMajor; 1755 1756 /** 1757 * The length of the minor axis of an ellipse that describes the touch area at 1758 * the point of contact. 1759 */ 1760 public float touchMinor; 1761 1762 /** 1763 * The length of the major axis of an ellipse that describes the size of 1764 * the approaching tool. 1765 * The tool area represents the estimated size of the finger or pen that is 1766 * touching the device independent of its actual touch area at the point of contact. 1767 */ 1768 public float toolMajor; 1769 1770 /** 1771 * The length of the minor axis of an ellipse that describes the size of 1772 * the approaching tool. 1773 * The tool area represents the estimated size of the finger or pen that is 1774 * touching the device independent of its actual touch area at the point of contact. 1775 */ 1776 public float toolMinor; 1777 1778 /** 1779 * The orientation of the touch area and tool area in radians clockwise from vertical. 1780 * An angle of 0 degrees indicates that the major axis of contact is oriented 1781 * upwards, is perfectly circular or is of unknown orientation. A positive angle 1782 * indicates that the major axis of contact is oriented to the right. A negative angle 1783 * indicates that the major axis of contact is oriented to the left. 1784 * The full range is from -PI/2 radians (finger pointing fully left) to PI/2 radians 1785 * (finger pointing fully right). 1786 */ 1787 public float orientation; 1788 1789 /* 1790 private static final float PI_4 = (float) (Math.PI / 4); 1791 1792 public float getTouchWidth() { 1793 return Math.abs(orientation) > PI_4 ? touchMajor : touchMinor; 1794 } 1795 1796 public float getTouchHeight() { 1797 return Math.abs(orientation) > PI_4 ? touchMinor : touchMajor; 1798 } 1799 1800 public float getToolWidth() { 1801 return Math.abs(orientation) > PI_4 ? toolMajor : toolMinor; 1802 } 1803 1804 public float getToolHeight() { 1805 return Math.abs(orientation) > PI_4 ? toolMinor : toolMajor; 1806 } 1807 */ 1808 } 1809} 1810