MotionEvent.java revision dac57e4ec94591207eeb660adbe53245dd78e713
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.os.Parcel; 20import android.os.Parcelable; 21import android.os.SystemClock; 22 23/** 24 * Object used to report movement (mouse, pen, finger, trackball) events. This 25 * class may hold either absolute or relative movements, depending on what 26 * it is being used for. 27 */ 28public final class MotionEvent implements Parcelable { 29 /** 30 * Bit mask of the parts of the action code that are the action itself. 31 */ 32 public static final int ACTION_MASK = 0xff; 33 34 /** 35 * Constant for {@link #getAction}: A pressed gesture has started, the 36 * motion contains the initial starting location. 37 */ 38 public static final int ACTION_DOWN = 0; 39 40 /** 41 * Constant for {@link #getAction}: A pressed gesture has finished, the 42 * motion contains the final release location as well as any intermediate 43 * points since the last down or move event. 44 */ 45 public static final int ACTION_UP = 1; 46 47 /** 48 * Constant for {@link #getAction}: A change has happened during a 49 * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). 50 * The motion contains the most recent point, as well as any intermediate 51 * points since the last down or move event. 52 */ 53 public static final int ACTION_MOVE = 2; 54 55 /** 56 * Constant for {@link #getAction}: The current gesture has been aborted. 57 * You will not receive any more points in it. You should treat this as 58 * an up event, but not perform any action that you normally would. 59 */ 60 public static final int ACTION_CANCEL = 3; 61 62 /** 63 * Constant for {@link #getAction}: A movement has happened outside of the 64 * normal bounds of the UI element. This does not provide a full gesture, 65 * but only the initial location of the movement/touch. 66 */ 67 public static final int ACTION_OUTSIDE = 4; 68 69 /** 70 * A non-primary pointer has gone down. The bits in 71 * {@link #ACTION_POINTER_MASK} indicate which pointer changed. 72 */ 73 public static final int ACTION_POINTER_DOWN = 5; 74 75 /** 76 * The primary pointer has gone done. 77 */ 78 public static final int ACTION_POINTER_1_DOWN = ACTION_POINTER_DOWN | 0x0000; 79 80 /** 81 * The secondary pointer has gone done. 82 */ 83 public static final int ACTION_POINTER_2_DOWN = ACTION_POINTER_DOWN | 0x0100; 84 85 /** 86 * The tertiary pointer has gone done. 87 */ 88 public static final int ACTION_POINTER_3_DOWN = ACTION_POINTER_DOWN | 0x0200; 89 90 /** 91 * A non-primary pointer has gone up. The bits in 92 * {@link #ACTION_POINTER_MASK} indicate which pointer changed. 93 */ 94 public static final int ACTION_POINTER_UP = 6; 95 96 /** 97 * The primary pointer has gone up. 98 */ 99 public static final int ACTION_POINTER_1_UP = ACTION_POINTER_UP | 0x0000; 100 101 /** 102 * The secondary pointer has gone up. 103 */ 104 public static final int ACTION_POINTER_2_UP = ACTION_POINTER_UP | 0x0100; 105 106 /** 107 * The tertiary pointer has gone up. 108 */ 109 public static final int ACTION_POINTER_3_UP = ACTION_POINTER_UP | 0x0200; 110 111 /** 112 * Bits in the action code that represent a pointer ID, used with 113 * {@link #ACTION_POINTER_DOWN} and {@link #ACTION_POINTER_UP}. Pointer IDs 114 * start at 0, with 0 being the primary (first) pointer in the motion. 115 */ 116 public static final int ACTION_POINTER_MASK = 0xff00; 117 118 /** 119 * Bit shift for the action bits holding the pointer identifier as 120 * defined by {@link #ACTION_POINTER_MASK}. 121 */ 122 public static final int ACTION_POINTER_SHIFT = 8; 123 124 private static final boolean TRACK_RECYCLED_LOCATION = false; 125 126 /** 127 * Flag indicating the motion event intersected the top edge of the screen. 128 */ 129 public static final int EDGE_TOP = 0x00000001; 130 131 /** 132 * Flag indicating the motion event intersected the bottom edge of the screen. 133 */ 134 public static final int EDGE_BOTTOM = 0x00000002; 135 136 /** 137 * Flag indicating the motion event intersected the left edge of the screen. 138 */ 139 public static final int EDGE_LEFT = 0x00000004; 140 141 /** 142 * Flag indicating the motion event intersected the right edge of the screen. 143 */ 144 public static final int EDGE_RIGHT = 0x00000008; 145 146 /** 147 * This is the part of the state data that holds the finger identifier 148 * for the sample. 149 */ 150 static private final int STATE_FINGER_ID_MASK = 0xff; 151 152 /** 153 * Special value for STATE_FINGER_ID_MASK indicating that the finger 154 * is not down in that sample. 155 */ 156 static private final int STATE_FINGER_ID_NONE = 0xff; 157 158 /** 159 * Offset for the sample's X coordinate. 160 * @hide 161 */ 162 static public final int SAMPLE_X = 0; 163 164 /** 165 * Offset for the sample's Y coordinate. 166 * @hide 167 */ 168 static public final int SAMPLE_Y = 1; 169 170 /** 171 * Offset for the sample's X coordinate. 172 * @hide 173 */ 174 static public final int SAMPLE_PRESSURE = 2; 175 176 /** 177 * Offset for the sample's X coordinate. 178 * @hide 179 */ 180 static public final int SAMPLE_SIZE = 3; 181 182 /** 183 * Number of data items for each sample. 184 * @hide 185 */ 186 static public final int NUM_SAMPLE_DATA = 4; 187 188 static private final int BASE_AVAIL_POINTERS = 2; 189 static private final int BASE_AVAIL_SAMPLES = 8; 190 191 static private final int MAX_RECYCLED = 10; 192 static private Object gRecyclerLock = new Object(); 193 static private int gRecyclerUsed = 0; 194 static private MotionEvent gRecyclerTop = null; 195 196 private long mDownTime; 197 private long mEventTimeNano; 198 private int mAction; 199 private float mRawX; 200 private float mRawY; 201 private float mXPrecision; 202 private float mYPrecision; 203 private int mDeviceId; 204 private int mEdgeFlags; 205 private int mMetaState; 206 207 // Here is the actual event data. Note that the order of the array 208 // is a little odd: the first entry is the most recent, and the ones 209 // following it are the historical data from oldest to newest. This 210 // allows us to easily retrieve the most recent data, without having 211 // to copy the arrays every time a new sample is added. 212 213 private int mNumPointers; 214 private int mNumSamples; 215 // Array of (mNumSamples * mNumPointers) size of control data. 216 private int[] mStateSamples; 217 // Array of (mNumSamples * mNumPointers * NUM_SAMPLE_DATA) size of event data. 218 private float[] mDataSamples; 219 // Array of mNumSamples size of time stamps. 220 private long[] mTimeSamples; 221 222 private MotionEvent mNext; 223 private RuntimeException mRecycledLocation; 224 private boolean mRecycled; 225 226 private MotionEvent() { 227 mStateSamples = new int[BASE_AVAIL_POINTERS*BASE_AVAIL_SAMPLES]; 228 mDataSamples = new float[BASE_AVAIL_POINTERS*BASE_AVAIL_SAMPLES*NUM_SAMPLE_DATA]; 229 mTimeSamples = new long[BASE_AVAIL_SAMPLES]; 230 } 231 232 static private MotionEvent obtain() { 233 synchronized (gRecyclerLock) { 234 if (gRecyclerTop == null) { 235 return new MotionEvent(); 236 } 237 MotionEvent ev = gRecyclerTop; 238 gRecyclerTop = ev.mNext; 239 gRecyclerUsed--; 240 ev.mRecycledLocation = null; 241 ev.mRecycled = false; 242 return ev; 243 } 244 } 245 246 /** 247 * Create a new MotionEvent, filling in all of the basic values that 248 * define the motion. 249 * 250 * @param downTime The time (in ms) when the user originally pressed down to start 251 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 252 * @param eventTime The the time (in ms) when this specific event was generated. This 253 * must be obtained from {@link SystemClock#uptimeMillis()}. 254 * @param eventTimeNano The the time (in ns) when this specific event was generated. This 255 * must be obtained from {@link System#nanoTime()}. 256 * @param action The kind of action being performed -- one of either 257 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 258 * {@link #ACTION_CANCEL}. 259 * @param x The X coordinate of this event. 260 * @param y The Y coordinate of this event. 261 * @param pressure The current pressure of this event. The pressure generally 262 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 263 * values higher than 1 may be generated depending on the calibration of 264 * the input device. 265 * @param size A scaled value of the approximate size of the area being pressed when 266 * touched with the finger. The actual value in pixels corresponding to the finger 267 * touch is normalized with a device specific range of values 268 * and scaled to a value between 0 and 1. 269 * @param metaState The state of any meta / modifier keys that were in effect when 270 * the event was generated. 271 * @param xPrecision The precision of the X coordinate being reported. 272 * @param yPrecision The precision of the Y coordinate being reported. 273 * @param deviceId The id for the device that this event came from. An id of 274 * zero indicates that the event didn't come from a physical device; other 275 * numbers are arbitrary and you shouldn't depend on the values. 276 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this 277 * MotionEvent. 278 * 279 * @hide 280 */ 281 static public MotionEvent obtainNano(long downTime, long eventTime, long eventTimeNano, 282 int action, int pointers, float[] inData, int metaState, 283 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 284 MotionEvent ev = obtain(); 285 ev.mDeviceId = deviceId; 286 ev.mEdgeFlags = edgeFlags; 287 ev.mDownTime = downTime; 288 ev.mEventTimeNano = eventTimeNano; 289 ev.mAction = action; 290 ev.mMetaState = metaState; 291 ev.mRawX = inData[SAMPLE_X]; 292 ev.mRawY = inData[SAMPLE_Y]; 293 ev.mXPrecision = xPrecision; 294 ev.mYPrecision = yPrecision; 295 ev.mNumPointers = pointers; 296 ev.mNumSamples = 1; 297 298 float[] data = ev.mDataSamples; 299 System.arraycopy(inData, 0, data, 0, pointers * NUM_SAMPLE_DATA); 300 301 int[] state = ev.mStateSamples; 302 while (pointers > 0) { 303 pointers--; 304 state[pointers] = pointers; 305 } 306 307 ev.mTimeSamples[0] = eventTime; 308 309 return ev; 310 } 311 312 /** 313 * Create a new MotionEvent, filling in all of the basic values that 314 * define the motion. 315 * 316 * @param downTime The time (in ms) when the user originally pressed down to start 317 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 318 * @param eventTime The the time (in ms) when this specific event was generated. This 319 * must be obtained from {@link SystemClock#uptimeMillis()}. 320 * @param action The kind of action being performed -- one of either 321 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 322 * {@link #ACTION_CANCEL}. 323 * @param x The X coordinate of this event. 324 * @param y The Y coordinate of this event. 325 * @param pressure The current pressure of this event. The pressure generally 326 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 327 * values higher than 1 may be generated depending on the calibration of 328 * the input device. 329 * @param size A scaled value of the approximate size of the area being pressed when 330 * touched with the finger. The actual value in pixels corresponding to the finger 331 * touch is normalized with a device specific range of values 332 * and scaled to a value between 0 and 1. 333 * @param metaState The state of any meta / modifier keys that were in effect when 334 * the event was generated. 335 * @param xPrecision The precision of the X coordinate being reported. 336 * @param yPrecision The precision of the Y coordinate being reported. 337 * @param deviceId The id for the device that this event came from. An id of 338 * zero indicates that the event didn't come from a physical device; other 339 * numbers are arbitrary and you shouldn't depend on the values. 340 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this 341 * MotionEvent. 342 */ 343 static public MotionEvent obtain(long downTime, long eventTime, int action, 344 float x, float y, float pressure, float size, int metaState, 345 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 346 MotionEvent ev = obtain(); 347 ev.mDeviceId = deviceId; 348 ev.mEdgeFlags = edgeFlags; 349 ev.mDownTime = downTime; 350 ev.mEventTimeNano = eventTime * 1000000; 351 ev.mAction = action; 352 ev.mMetaState = metaState; 353 ev.mXPrecision = xPrecision; 354 ev.mYPrecision = yPrecision; 355 356 ev.mNumPointers = 1; 357 ev.mNumSamples = 1; 358 int[] state = ev.mStateSamples; 359 state[0] = 0; 360 float[] data = ev.mDataSamples; 361 data[SAMPLE_X] = ev.mRawX = x; 362 data[SAMPLE_Y] = ev.mRawY = y; 363 data[SAMPLE_PRESSURE] = pressure; 364 data[SAMPLE_SIZE] = size; 365 ev.mTimeSamples[0] = eventTime; 366 367 return ev; 368 } 369 370 /** 371 * Create a new MotionEvent, filling in all of the basic values that 372 * define the motion. 373 * 374 * @param downTime The time (in ms) when the user originally pressed down to start 375 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 376 * @param eventTime The the time (in ms) when this specific event was generated. This 377 * must be obtained from {@link SystemClock#uptimeMillis()}. 378 * @param action The kind of action being performed -- one of either 379 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 380 * {@link #ACTION_CANCEL}. 381 * @param pointers The number of pointers that are active in this event. 382 * @param x The X coordinate of this event. 383 * @param y The Y coordinate of this event. 384 * @param pressure The current pressure of this event. The pressure generally 385 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 386 * values higher than 1 may be generated depending on the calibration of 387 * the input device. 388 * @param size A scaled value of the approximate size of the area being pressed when 389 * touched with the finger. The actual value in pixels corresponding to the finger 390 * touch is normalized with a device specific range of values 391 * and scaled to a value between 0 and 1. 392 * @param metaState The state of any meta / modifier keys that were in effect when 393 * the event was generated. 394 * @param xPrecision The precision of the X coordinate being reported. 395 * @param yPrecision The precision of the Y coordinate being reported. 396 * @param deviceId The id for the device that this event came from. An id of 397 * zero indicates that the event didn't come from a physical device; other 398 * numbers are arbitrary and you shouldn't depend on the values. 399 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this 400 * MotionEvent. 401 */ 402 static public MotionEvent obtain(long downTime, long eventTime, int action, 403 int pointers, float x, float y, float pressure, float size, int metaState, 404 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 405 MotionEvent ev = obtain(); 406 ev.mDeviceId = deviceId; 407 ev.mEdgeFlags = edgeFlags; 408 ev.mDownTime = downTime; 409 ev.mEventTimeNano = eventTime * 1000000; 410 ev.mAction = action; 411 ev.mNumPointers = pointers; 412 ev.mMetaState = metaState; 413 ev.mXPrecision = xPrecision; 414 ev.mYPrecision = yPrecision; 415 416 ev.mNumPointers = 1; 417 ev.mNumSamples = 1; 418 int[] state = ev.mStateSamples; 419 state[0] = 0; 420 float[] data = ev.mDataSamples; 421 data[SAMPLE_X] = ev.mRawX = x; 422 data[SAMPLE_Y] = ev.mRawY = y; 423 data[SAMPLE_PRESSURE] = pressure; 424 data[SAMPLE_SIZE] = size; 425 ev.mTimeSamples[0] = eventTime; 426 427 return ev; 428 } 429 430 /** 431 * Create a new MotionEvent, filling in a subset of the basic motion 432 * values. Those not specified here are: device id (always 0), pressure 433 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0). 434 * 435 * @param downTime The time (in ms) when the user originally pressed down to start 436 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 437 * @param eventTime The the time (in ms) when this specific event was generated. This 438 * must be obtained from {@link SystemClock#uptimeMillis()}. 439 * @param action The kind of action being performed -- one of either 440 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 441 * {@link #ACTION_CANCEL}. 442 * @param x The X coordinate of this event. 443 * @param y The Y coordinate of this event. 444 * @param metaState The state of any meta / modifier keys that were in effect when 445 * the event was generated. 446 */ 447 static public MotionEvent obtain(long downTime, long eventTime, int action, 448 float x, float y, int metaState) { 449 MotionEvent ev = obtain(); 450 ev.mDeviceId = 0; 451 ev.mEdgeFlags = 0; 452 ev.mDownTime = downTime; 453 ev.mEventTimeNano = eventTime * 1000000; 454 ev.mAction = action; 455 ev.mNumPointers = 1; 456 ev.mMetaState = metaState; 457 ev.mXPrecision = 1.0f; 458 ev.mYPrecision = 1.0f; 459 460 ev.mNumPointers = 1; 461 ev.mNumSamples = 1; 462 int[] state = ev.mStateSamples; 463 state[0] = 0; 464 float[] data = ev.mDataSamples; 465 data[SAMPLE_X] = ev.mRawX = x; 466 data[SAMPLE_Y] = ev.mRawY = y; 467 data[SAMPLE_PRESSURE] = 1.0f; 468 data[SAMPLE_SIZE] = 1.0f; 469 ev.mTimeSamples[0] = eventTime; 470 471 return ev; 472 } 473 474 /** 475 * Scales down the coordination of this event by the given scale. 476 * 477 * @hide 478 */ 479 public void scale(float scale) { 480 mRawX *= scale; 481 mRawY *= scale; 482 mXPrecision *= scale; 483 mYPrecision *= scale; 484 float[] history = mDataSamples; 485 final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA; 486 for (int i = 0; i < length; i += NUM_SAMPLE_DATA) { 487 history[i + SAMPLE_X] *= scale; 488 history[i + SAMPLE_Y] *= scale; 489 // no need to scale pressure 490 history[i + SAMPLE_SIZE] *= scale; // TODO: square this? 491 } 492 } 493 494 /** 495 * Create a new MotionEvent, copying from an existing one. 496 */ 497 static public MotionEvent obtain(MotionEvent o) { 498 MotionEvent ev = obtain(); 499 ev.mDeviceId = o.mDeviceId; 500 ev.mEdgeFlags = o.mEdgeFlags; 501 ev.mDownTime = o.mDownTime; 502 ev.mEventTimeNano = o.mEventTimeNano; 503 ev.mAction = o.mAction; 504 ev.mNumPointers = o.mNumPointers; 505 ev.mRawX = o.mRawX; 506 ev.mRawY = o.mRawY; 507 ev.mMetaState = o.mMetaState; 508 ev.mXPrecision = o.mXPrecision; 509 ev.mYPrecision = o.mYPrecision; 510 511 final int NT = ev.mNumSamples = o.mNumSamples; 512 if (ev.mTimeSamples.length >= NT) { 513 System.arraycopy(o.mTimeSamples, 0, ev.mTimeSamples, 0, NT); 514 } else { 515 ev.mTimeSamples = (long[])o.mTimeSamples.clone(); 516 } 517 518 final int NS = (ev.mNumPointers=o.mNumPointers) * NT; 519 if (ev.mStateSamples.length < NS) { 520 System.arraycopy(o.mStateSamples, 0, ev.mStateSamples, 0, NS); 521 } else { 522 ev.mStateSamples = (int[])o.mStateSamples.clone(); 523 } 524 525 final int ND = NS * NUM_SAMPLE_DATA; 526 if (ev.mDataSamples.length < ND) { 527 System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0, ND); 528 } else { 529 ev.mDataSamples = (float[])o.mDataSamples.clone(); 530 } 531 532 return ev; 533 } 534 535 /** 536 * Recycle the MotionEvent, to be re-used by a later caller. After calling 537 * this function you must not ever touch the event again. 538 */ 539 public void recycle() { 540 // Ensure recycle is only called once! 541 if (TRACK_RECYCLED_LOCATION) { 542 if (mRecycledLocation != null) { 543 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation); 544 } 545 mRecycledLocation = new RuntimeException("Last recycled here"); 546 } else if (mRecycled) { 547 throw new RuntimeException(toString() + " recycled twice!"); 548 } 549 550 //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation); 551 synchronized (gRecyclerLock) { 552 if (gRecyclerUsed < MAX_RECYCLED) { 553 gRecyclerUsed++; 554 mNumSamples = 0; 555 mNext = gRecyclerTop; 556 gRecyclerTop = this; 557 } 558 } 559 } 560 561 /** 562 * Return the kind of action being performed -- one of either 563 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 564 * {@link #ACTION_CANCEL}. 565 */ 566 public final int getAction() { 567 return mAction; 568 } 569 570 /** 571 * Returns the time (in ms) when the user originally pressed down to start 572 * a stream of position events. 573 */ 574 public final long getDownTime() { 575 return mDownTime; 576 } 577 578 /** 579 * Returns the time (in ms) when this specific event was generated. 580 */ 581 public final long getEventTime() { 582 return mTimeSamples[0]; 583 } 584 585 /** 586 * Returns the time (in ns) when this specific event was generated. 587 * The value is in nanosecond precision but it may not have nanosecond accuracy. 588 * 589 * @hide 590 */ 591 public final long getEventTimeNano() { 592 return mEventTimeNano; 593 } 594 595 /** 596 * The number of pointers of data contained in this event. Always 597 * >= 1. 598 */ 599 public final int getPointerCount() { 600 return mNumPointers; 601 } 602 603 /** 604 * {@link #getX(int)} for the first pointer (pointer 0). 605 */ 606 public final float getX() { 607 return mDataSamples[SAMPLE_X]; 608 } 609 610 /** 611 * {@link #getY(int)} for the first pointer (pointer 0). 612 */ 613 public final float getY() { 614 return mDataSamples[SAMPLE_Y]; 615 } 616 617 /** 618 * {@link #getPressure(int)} for the first pointer (pointer 0). 619 */ 620 public final float getPressure() { 621 return mDataSamples[SAMPLE_PRESSURE]; 622 } 623 624 /** 625 * {@link #getSize(int)} for the first pointer (pointer 0). 626 */ 627 public final float getSize() { 628 return mDataSamples[SAMPLE_SIZE]; 629 } 630 631 /** 632 * Returns the X coordinate of this event for the given pointer. 633 * Whole numbers are pixels; the 634 * value may have a fraction for input devices that are sub-pixel precise. 635 * @param pointer The desired pointer to retrieve. Value may be from 0 636 * (the first pointer) to {@link #getPointerCount()}-1. 637 */ 638 public final float getX(int pointer) { 639 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_X]; 640 } 641 642 /** 643 * Returns the Y coordinate of this event for the given pointer. 644 * Whole numbers are pixels; the 645 * value may have a fraction for input devices that are sub-pixel precise. 646 * @param pointer The desired pointer to retrieve. Value may be from 0 647 * (the first pointer) to {@link #getPointerCount()}-1. 648 */ 649 public final float getY(int pointer) { 650 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_Y]; 651 } 652 653 /** 654 * Returns the current pressure of this event for the given pointer. 655 * The pressure generally 656 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 657 * values higher than 1 may be generated depending on the calibration of 658 * the input device. 659 * @param pointer The desired pointer to retrieve. Value may be from 0 660 * (the first pointer) to {@link #getPointerCount()}-1. 661 */ 662 public final float getPressure(int pointer) { 663 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_PRESSURE]; 664 } 665 666 /** 667 * Returns a scaled value of the approximate size for the given pointer, 668 * representing the area of the screen being pressed. 669 * The actual value in pixels corresponding to the 670 * touch is normalized with the device specific range of values 671 * and scaled to a value between 0 and 1. The value of size can be used to 672 * determine fat touch events. 673 * @param pointer The desired pointer to retrieve. Value may be from 0 674 * (the first pointer) to {@link #getPointerCount()}-1. 675 */ 676 public final float getSize(int pointer) { 677 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_SIZE]; 678 } 679 680 /** 681 * Returns the state of any meta / modifier keys that were in effect when 682 * the event was generated. This is the same values as those 683 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}. 684 * 685 * @return an integer in which each bit set to 1 represents a pressed 686 * meta key 687 * 688 * @see KeyEvent#getMetaState() 689 */ 690 public final int getMetaState() { 691 return mMetaState; 692 } 693 694 /** 695 * Returns the original raw X coordinate of this event. For touch 696 * events on the screen, this is the original location of the event 697 * on the screen, before it had been adjusted for the containing window 698 * and views. 699 */ 700 public final float getRawX() { 701 return mRawX; 702 } 703 704 /** 705 * Returns the original raw Y coordinate of this event. For touch 706 * events on the screen, this is the original location of the event 707 * on the screen, before it had been adjusted for the containing window 708 * and views. 709 */ 710 public final float getRawY() { 711 return mRawY; 712 } 713 714 /** 715 * Return the precision of the X coordinates being reported. You can 716 * multiple this number with {@link #getX} to find the actual hardware 717 * value of the X coordinate. 718 * @return Returns the precision of X coordinates being reported. 719 */ 720 public final float getXPrecision() { 721 return mXPrecision; 722 } 723 724 /** 725 * Return the precision of the Y coordinates being reported. You can 726 * multiple this number with {@link #getY} to find the actual hardware 727 * value of the Y coordinate. 728 * @return Returns the precision of Y coordinates being reported. 729 */ 730 public final float getYPrecision() { 731 return mYPrecision; 732 } 733 734 /** 735 * Returns the number of historical points in this event. These are 736 * movements that have occurred between this event and the previous event. 737 * This only applies to ACTION_MOVE events -- all other actions will have 738 * a size of 0. 739 * 740 * @return Returns the number of historical points in the event. 741 */ 742 public final int getHistorySize() { 743 return mNumSamples - 1; 744 } 745 746 /** 747 * Returns the time that a historical movement occurred between this event 748 * and the previous event. Only applies to ACTION_MOVE events. 749 * 750 * @param pos Which historical value to return; must be less than 751 * {@link #getHistorySize} 752 * 753 * @see #getHistorySize 754 * @see #getEventTime 755 */ 756 public final long getHistoricalEventTime(int pos) { 757 return mTimeSamples[pos + 1]; 758 } 759 760 /** 761 * {@link #getHistoricalX(int)} for the first pointer (pointer 0). 762 */ 763 public final float getHistoricalX(int pos) { 764 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_X]; 765 } 766 767 /** 768 * {@link #getHistoricalY(int)} for the first pointer (pointer 0). 769 */ 770 public final float getHistoricalY(int pos) { 771 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_Y]; 772 } 773 774 /** 775 * {@link #getHistoricalPressure(int)} for the first pointer (pointer 0). 776 */ 777 public final float getHistoricalPressure(int pos) { 778 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_PRESSURE]; 779 } 780 781 /** 782 * {@link #getHistoricalSize(int)} for the first pointer (pointer 0). 783 */ 784 public final float getHistoricalSize(int pos) { 785 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_SIZE]; 786 } 787 788 /** 789 * Returns a historical X coordinate that occurred between this event 790 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 791 * 792 * @param pointer The desired pointer to retrieve. Value may be from 0 793 * (the first pointer) to {@link #getPointerCount()}-1. 794 * @param pos Which historical value to return; must be less than 795 * {@link #getHistorySize} 796 * 797 * @see #getHistorySize 798 * @see #getX 799 */ 800 public final float getHistoricalX(int pointer, int pos) { 801 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 802 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_X]; 803 } 804 805 /** 806 * Returns a historical Y coordinate that occurred between this event 807 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 808 * 809 * @param pointer The desired pointer to retrieve. Value may be from 0 810 * (the first pointer) to {@link #getPointerCount()}-1. 811 * @param pos Which historical value to return; must be less than 812 * {@link #getHistorySize} 813 * 814 * @see #getHistorySize 815 * @see #getY 816 */ 817 public final float getHistoricalY(int pointer, int pos) { 818 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 819 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_Y]; 820 } 821 822 /** 823 * Returns a historical pressure coordinate that occurred between this event 824 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 825 * 826 * @param pos Which historical value to return; must be less than 827 * {@link #getHistorySize} 828 * 829 * @param pointer The desired pointer to retrieve. Value may be from 0 830 * (the first pointer) to {@link #getPointerCount()}-1. 831 * @see #getHistorySize 832 * @see #getPressure 833 */ 834 public final float getHistoricalPressure(int pointer, int pos) { 835 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 836 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_PRESSURE]; 837 } 838 839 /** 840 * Returns a historical size coordinate that occurred between this event 841 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 842 * 843 * @param pos Which historical value to return; must be less than 844 * {@link #getHistorySize} 845 * 846 * @param pointer The desired pointer to retrieve. Value may be from 0 847 * (the first pointer) to {@link #getPointerCount()}-1. 848 * @see #getHistorySize 849 * @see #getSize 850 */ 851 public final float getHistoricalSize(int pointer, int pos) { 852 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 853 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_SIZE]; 854 } 855 856 /** 857 * Return the id for the device that this event came from. An id of 858 * zero indicates that the event didn't come from a physical device; other 859 * numbers are arbitrary and you shouldn't depend on the values. 860 */ 861 public final int getDeviceId() { 862 return mDeviceId; 863 } 864 865 /** 866 * Returns a bitfield indicating which edges, if any, where touched by this 867 * MotionEvent. For touch events, clients can use this to determine if the 868 * user's finger was touching the edge of the display. 869 * 870 * @see #EDGE_LEFT 871 * @see #EDGE_TOP 872 * @see #EDGE_RIGHT 873 * @see #EDGE_BOTTOM 874 */ 875 public final int getEdgeFlags() { 876 return mEdgeFlags; 877 } 878 879 880 /** 881 * Sets the bitfield indicating which edges, if any, where touched by this 882 * MotionEvent. 883 * 884 * @see #getEdgeFlags() 885 */ 886 public final void setEdgeFlags(int flags) { 887 mEdgeFlags = flags; 888 } 889 890 /** 891 * Sets this event's action. 892 */ 893 public final void setAction(int action) { 894 mAction = action; 895 } 896 897 /** 898 * Adjust this event's location. 899 * @param deltaX Amount to add to the current X coordinate of the event. 900 * @param deltaY Amount to add to the current Y coordinate of the event. 901 */ 902 public final void offsetLocation(float deltaX, float deltaY) { 903 final int N = mNumPointers*mNumSamples*4; 904 final float[] pos = mDataSamples; 905 for (int i=0; i<N; i+=NUM_SAMPLE_DATA) { 906 pos[i+SAMPLE_X] += deltaX; 907 pos[i+SAMPLE_Y] += deltaY; 908 } 909 } 910 911 /** 912 * Set this event's location. Applies {@link #offsetLocation} with a 913 * delta from the current location to the given new location. 914 * 915 * @param x New absolute X location. 916 * @param y New absolute Y location. 917 */ 918 public final void setLocation(float x, float y) { 919 float deltaX = x-mDataSamples[SAMPLE_X]; 920 float deltaY = y-mDataSamples[SAMPLE_Y]; 921 if (deltaX != 0 || deltaY != 0) { 922 offsetLocation(deltaX, deltaY); 923 } 924 } 925 926 /** 927 * Add a new movement to the batch of movements in this event. The event's 928 * current location, position and size is updated to the new values. In 929 * the future, the current values in the event will be added to a list of 930 * historic values. 931 * 932 * @param eventTime The time stamp for this data. 933 * @param x The new X position. 934 * @param y The new Y position. 935 * @param pressure The new pressure. 936 * @param size The new size. 937 * @param metaState Meta key state. 938 */ 939 public final void addBatch(long eventTime, float x, float y, 940 float pressure, float size, int metaState) { 941 int[] states = mStateSamples; 942 float[] data = mDataSamples; 943 long[] times = mTimeSamples; 944 945 final int NP = mNumPointers; 946 final int NS = mNumSamples; 947 final int NI = NP*NS; 948 final int ND = NI * NUM_SAMPLE_DATA; 949 if (states.length < (NI+NP)) { 950 // The state and data arrays are sized together, since their 951 // size is always a fixed factor from each other. 952 final int NEW_NI = NP * (NS+BASE_AVAIL_SAMPLES); 953 int[] newState = new int[NEW_NI]; 954 System.arraycopy(states, 0, newState, 0, NI); 955 mStateSamples = states = newState; 956 final int NEW_ND = NEW_NI * NUM_SAMPLE_DATA; 957 float[] newData = new float[NEW_ND]; 958 System.arraycopy(data, 0, newData, 0, ND); 959 mDataSamples = data = newData; 960 } 961 if (times.length <= NS) { 962 final int NEW_NS = NS + BASE_AVAIL_SAMPLES; 963 long[] newHistoryTimes = new long[NEW_NS]; 964 System.arraycopy(times, 0, newHistoryTimes, 0, NS); 965 mTimeSamples = times = newHistoryTimes; 966 } 967 968 times[NS] = times[0]; 969 times[0] = eventTime; 970 971 final int pos = NS*NUM_SAMPLE_DATA; 972 data[pos+SAMPLE_X] = data[SAMPLE_X]; 973 data[pos+SAMPLE_Y] = data[SAMPLE_Y]; 974 data[pos+SAMPLE_PRESSURE] = data[SAMPLE_PRESSURE]; 975 data[pos+SAMPLE_SIZE] = data[SAMPLE_SIZE]; 976 data[SAMPLE_X] = x; 977 data[SAMPLE_Y] = y; 978 data[SAMPLE_PRESSURE] = pressure; 979 data[SAMPLE_SIZE] = size; 980 mNumSamples = NS+1; 981 982 mRawX = x; 983 mRawY = y; 984 mMetaState |= metaState; 985 } 986 987 /** 988 * Add a new movement to the batch of movements in this event. The 989 * input data must contain (NUM_SAMPLE_DATA * {@link #getPointerCount()}) 990 * samples of data. 991 * 992 * @param eventTime The time stamp for this data. 993 * @param inData The actual data. 994 * @param metaState Meta key state. 995 * 996 * @hide 997 */ 998 public final void addBatch(long eventTime, float[] inData, int metaState) { 999 int[] states = mStateSamples; 1000 float[] data = mDataSamples; 1001 long[] times = mTimeSamples; 1002 1003 final int NP = mNumPointers; 1004 final int NS = mNumSamples; 1005 final int NI = NP*NS; 1006 final int ND = NI * NUM_SAMPLE_DATA; 1007 if (states.length < (NI+NP)) { 1008 // The state and data arrays are sized together, since their 1009 // size is always a fixed factor from each other. 1010 final int NEW_NI = NP * (NS+BASE_AVAIL_SAMPLES); 1011 int[] newState = new int[NEW_NI]; 1012 System.arraycopy(states, 0, newState, 0, NI); 1013 mStateSamples = states = newState; 1014 final int NEW_ND = NEW_NI * NUM_SAMPLE_DATA; 1015 float[] newData = new float[NEW_ND]; 1016 System.arraycopy(data, 0, newData, 0, ND); 1017 mDataSamples = data = newData; 1018 } 1019 if (times.length <= NS) { 1020 final int NEW_NS = NS + BASE_AVAIL_SAMPLES; 1021 long[] newHistoryTimes = new long[NEW_NS]; 1022 System.arraycopy(times, 0, newHistoryTimes, 0, NS); 1023 mTimeSamples = times = newHistoryTimes; 1024 } 1025 1026 times[NS] = times[0]; 1027 times[0] = eventTime; 1028 1029 System.arraycopy(data, 0, data, ND, mNumPointers*NUM_SAMPLE_DATA); 1030 System.arraycopy(inData, 0, data, 0, mNumPointers*NUM_SAMPLE_DATA); 1031 1032 mNumSamples = NS+1; 1033 1034 mRawX = inData[SAMPLE_X]; 1035 mRawY = inData[SAMPLE_Y]; 1036 mMetaState |= metaState; 1037 } 1038 1039 @Override 1040 public String toString() { 1041 return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this)) 1042 + " action=" + mAction + " x=" + getX() 1043 + " y=" + getY() + " pressure=" + getPressure() + " size=" + getSize() + "}"; 1044 } 1045 1046 public static final Parcelable.Creator<MotionEvent> CREATOR 1047 = new Parcelable.Creator<MotionEvent>() { 1048 public MotionEvent createFromParcel(Parcel in) { 1049 MotionEvent ev = obtain(); 1050 ev.readFromParcel(in); 1051 return ev; 1052 } 1053 1054 public MotionEvent[] newArray(int size) { 1055 return new MotionEvent[size]; 1056 } 1057 }; 1058 1059 public int describeContents() { 1060 return 0; 1061 } 1062 1063 public void writeToParcel(Parcel out, int flags) { 1064 out.writeLong(mDownTime); 1065 out.writeLong(mEventTimeNano); 1066 out.writeInt(mAction); 1067 out.writeInt(mMetaState); 1068 out.writeFloat(mRawX); 1069 out.writeFloat(mRawY); 1070 final int NP = mNumPointers; 1071 out.writeInt(NP); 1072 final int NS = mNumSamples; 1073 out.writeInt(NS); 1074 final int NI = NP*NS; 1075 if (NI > 0) { 1076 int i; 1077 int[] state = mStateSamples; 1078 for (i=0; i<NI; i++) { 1079 out.writeInt(state[i]); 1080 } 1081 final int NI4 = NI*NUM_SAMPLE_DATA; 1082 float[] history = mDataSamples; 1083 for (i=0; i<NI4; i++) { 1084 out.writeFloat(history[i]); 1085 } 1086 long[] times = mTimeSamples; 1087 for (i=0; i<NS; i++) { 1088 out.writeLong(times[i]); 1089 } 1090 } 1091 out.writeFloat(mXPrecision); 1092 out.writeFloat(mYPrecision); 1093 out.writeInt(mDeviceId); 1094 out.writeInt(mEdgeFlags); 1095 } 1096 1097 private void readFromParcel(Parcel in) { 1098 mDownTime = in.readLong(); 1099 mEventTimeNano = in.readLong(); 1100 mAction = in.readInt(); 1101 mMetaState = in.readInt(); 1102 mRawX = in.readFloat(); 1103 mRawY = in.readFloat(); 1104 final int NP = in.readInt(); 1105 mNumPointers = NP; 1106 final int NS = in.readInt(); 1107 mNumSamples = NS; 1108 final int NI = NP*NS; 1109 if (NI > 0) { 1110 final int NI4 = NI*4; 1111 int[] state = mStateSamples; 1112 if (state.length < NI) { 1113 mStateSamples = state = new int[NI]; 1114 } 1115 for (int i=0; i<NI; i++) { 1116 state[i] = in.readInt(); 1117 } 1118 float[] history = mDataSamples; 1119 if (history.length < NI4) { 1120 mDataSamples = history = new float[NI4]; 1121 } 1122 for (int i=0; i<NI4; i++) { 1123 history[i] = in.readFloat(); 1124 } 1125 long[] times = mTimeSamples; 1126 if (times == null || times.length < NS) { 1127 mTimeSamples = times = new long[NS]; 1128 } 1129 for (int i=0; i<NS; i++) { 1130 times[i] = in.readLong(); 1131 } 1132 } 1133 mXPrecision = in.readFloat(); 1134 mYPrecision = in.readFloat(); 1135 mDeviceId = in.readInt(); 1136 mEdgeFlags = in.readInt(); 1137 } 1138 1139} 1140