MotionEvent.java revision 9822d2b27330793ea4ba9c3316ef35f402f35fb4
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.mXPrecision = xPrecision; 292 ev.mYPrecision = yPrecision; 293 294 ev.mNumPointers = pointers; 295 ev.mNumSamples = 1; 296 297 float[] data = ev.mDataSamples; 298 System.arraycopy(inData, 0, data, 0, pointers * NUM_SAMPLE_DATA); 299 300 int[] state = ev.mStateSamples; 301 while (pointers > 0) { 302 pointers--; 303 state[pointers] = pointers; 304 } 305 306 ev.mTimeSamples[0] = eventTime; 307 308 return ev; 309 } 310 311 /** 312 * Create a new MotionEvent, filling in all of the basic values that 313 * define the motion. 314 * 315 * @param downTime The time (in ms) when the user originally pressed down to start 316 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 317 * @param eventTime The the time (in ms) when this specific event was generated. This 318 * must be obtained from {@link SystemClock#uptimeMillis()}. 319 * @param action The kind of action being performed -- one of either 320 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 321 * {@link #ACTION_CANCEL}. 322 * @param x The X coordinate of this event. 323 * @param y The Y coordinate of this event. 324 * @param pressure The current pressure of this event. The pressure generally 325 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 326 * values higher than 1 may be generated depending on the calibration of 327 * the input device. 328 * @param size A scaled value of the approximate size of the area being pressed when 329 * touched with the finger. The actual value in pixels corresponding to the finger 330 * touch is normalized with a device specific range of values 331 * and scaled to a value between 0 and 1. 332 * @param metaState The state of any meta / modifier keys that were in effect when 333 * the event was generated. 334 * @param xPrecision The precision of the X coordinate being reported. 335 * @param yPrecision The precision of the Y coordinate being reported. 336 * @param deviceId The id for the device that this event came from. An id of 337 * zero indicates that the event didn't come from a physical device; other 338 * numbers are arbitrary and you shouldn't depend on the values. 339 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this 340 * MotionEvent. 341 */ 342 static public MotionEvent obtain(long downTime, long eventTime, int action, 343 float x, float y, float pressure, float size, int metaState, 344 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 345 MotionEvent ev = obtain(); 346 ev.mDeviceId = deviceId; 347 ev.mEdgeFlags = edgeFlags; 348 ev.mDownTime = downTime; 349 ev.mEventTimeNano = eventTime * 1000000; 350 ev.mAction = action; 351 ev.mMetaState = metaState; 352 ev.mXPrecision = xPrecision; 353 ev.mYPrecision = yPrecision; 354 355 ev.mNumPointers = 1; 356 ev.mNumSamples = 1; 357 int[] state = ev.mStateSamples; 358 state[0] = 0; 359 float[] data = ev.mDataSamples; 360 data[SAMPLE_X] = ev.mRawX = x; 361 data[SAMPLE_Y] = ev.mRawY = y; 362 data[SAMPLE_PRESSURE] = pressure; 363 data[SAMPLE_SIZE] = size; 364 ev.mTimeSamples[0] = eventTime; 365 366 return ev; 367 } 368 369 /** 370 * Create a new MotionEvent, filling in all of the basic values that 371 * define the motion. 372 * 373 * @param downTime The time (in ms) when the user originally pressed down to start 374 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 375 * @param eventTime The the time (in ms) when this specific event was generated. This 376 * must be obtained from {@link SystemClock#uptimeMillis()}. 377 * @param action The kind of action being performed -- one of either 378 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 379 * {@link #ACTION_CANCEL}. 380 * @param pointers The number of pointers that are active in this event. 381 * @param x The X coordinate of this event. 382 * @param y The Y coordinate of this event. 383 * @param pressure The current pressure of this event. The pressure generally 384 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 385 * values higher than 1 may be generated depending on the calibration of 386 * the input device. 387 * @param size A scaled value of the approximate size of the area being pressed when 388 * touched with the finger. The actual value in pixels corresponding to the finger 389 * touch is normalized with a device specific range of values 390 * and scaled to a value between 0 and 1. 391 * @param metaState The state of any meta / modifier keys that were in effect when 392 * the event was generated. 393 * @param xPrecision The precision of the X coordinate being reported. 394 * @param yPrecision The precision of the Y coordinate being reported. 395 * @param deviceId The id for the device that this event came from. An id of 396 * zero indicates that the event didn't come from a physical device; other 397 * numbers are arbitrary and you shouldn't depend on the values. 398 * @param edgeFlags A bitfield indicating which edges, if any, where touched by this 399 * MotionEvent. 400 */ 401 static public MotionEvent obtain(long downTime, long eventTime, int action, 402 int pointers, float x, float y, float pressure, float size, int metaState, 403 float xPrecision, float yPrecision, int deviceId, int edgeFlags) { 404 MotionEvent ev = obtain(); 405 ev.mDeviceId = deviceId; 406 ev.mEdgeFlags = edgeFlags; 407 ev.mDownTime = downTime; 408 ev.mEventTimeNano = eventTime * 1000000; 409 ev.mAction = action; 410 ev.mNumPointers = pointers; 411 ev.mMetaState = metaState; 412 ev.mXPrecision = xPrecision; 413 ev.mYPrecision = yPrecision; 414 415 ev.mNumPointers = 1; 416 ev.mNumSamples = 1; 417 int[] state = ev.mStateSamples; 418 state[0] = 0; 419 float[] data = ev.mDataSamples; 420 data[SAMPLE_X] = ev.mRawX = x; 421 data[SAMPLE_Y] = ev.mRawY = y; 422 data[SAMPLE_PRESSURE] = pressure; 423 data[SAMPLE_SIZE] = size; 424 ev.mTimeSamples[0] = eventTime; 425 426 return ev; 427 } 428 429 /** 430 * Create a new MotionEvent, filling in a subset of the basic motion 431 * values. Those not specified here are: device id (always 0), pressure 432 * and size (always 1), x and y precision (always 1), and edgeFlags (always 0). 433 * 434 * @param downTime The time (in ms) when the user originally pressed down to start 435 * a stream of position events. This must be obtained from {@link SystemClock#uptimeMillis()}. 436 * @param eventTime The the time (in ms) when this specific event was generated. This 437 * must be obtained from {@link SystemClock#uptimeMillis()}. 438 * @param action The kind of action being performed -- one of either 439 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 440 * {@link #ACTION_CANCEL}. 441 * @param x The X coordinate of this event. 442 * @param y The Y coordinate of this event. 443 * @param metaState The state of any meta / modifier keys that were in effect when 444 * the event was generated. 445 */ 446 static public MotionEvent obtain(long downTime, long eventTime, int action, 447 float x, float y, int metaState) { 448 MotionEvent ev = obtain(); 449 ev.mDeviceId = 0; 450 ev.mEdgeFlags = 0; 451 ev.mDownTime = downTime; 452 ev.mEventTimeNano = eventTime * 1000000; 453 ev.mAction = action; 454 ev.mNumPointers = 1; 455 ev.mMetaState = metaState; 456 ev.mXPrecision = 1.0f; 457 ev.mYPrecision = 1.0f; 458 459 ev.mNumPointers = 1; 460 ev.mNumSamples = 1; 461 int[] state = ev.mStateSamples; 462 state[0] = 0; 463 float[] data = ev.mDataSamples; 464 data[SAMPLE_X] = ev.mRawX = x; 465 data[SAMPLE_Y] = ev.mRawY = y; 466 data[SAMPLE_PRESSURE] = 1.0f; 467 data[SAMPLE_SIZE] = 1.0f; 468 ev.mTimeSamples[0] = eventTime; 469 470 return ev; 471 } 472 473 /** 474 * Scales down the coordination of this event by the given scale. 475 * 476 * @hide 477 */ 478 public void scale(float scale) { 479 mRawX *= scale; 480 mRawY *= scale; 481 mXPrecision *= scale; 482 mYPrecision *= scale; 483 float[] history = mDataSamples; 484 final int length = mNumPointers * mNumSamples * NUM_SAMPLE_DATA; 485 for (int i = 0; i < length; i += NUM_SAMPLE_DATA) { 486 history[i + SAMPLE_X] *= scale; 487 history[i + SAMPLE_Y] *= scale; 488 // no need to scale pressure 489 history[i + SAMPLE_SIZE] *= scale; // TODO: square this? 490 } 491 } 492 493 /** 494 * Create a new MotionEvent, copying from an existing one. 495 */ 496 static public MotionEvent obtain(MotionEvent o) { 497 MotionEvent ev = obtain(); 498 ev.mDeviceId = o.mDeviceId; 499 ev.mEdgeFlags = o.mEdgeFlags; 500 ev.mDownTime = o.mDownTime; 501 ev.mEventTimeNano = o.mEventTimeNano; 502 ev.mAction = o.mAction; 503 ev.mNumPointers = o.mNumPointers; 504 ev.mRawX = o.mRawX; 505 ev.mRawY = o.mRawY; 506 ev.mMetaState = o.mMetaState; 507 ev.mXPrecision = o.mXPrecision; 508 ev.mYPrecision = o.mYPrecision; 509 510 final int NT = ev.mNumSamples = o.mNumSamples; 511 if (ev.mTimeSamples.length < NT) { 512 System.arraycopy(o.mTimeSamples, 0, ev.mTimeSamples, 0, NT); 513 } else { 514 ev.mTimeSamples = (long[])o.mTimeSamples.clone(); 515 } 516 517 final int NS = (ev.mNumPointers=o.mNumPointers) * NT; 518 if (ev.mStateSamples.length < NS) { 519 System.arraycopy(o.mStateSamples, 0, ev.mStateSamples, 0, NS); 520 } else { 521 ev.mStateSamples = (int[])o.mStateSamples.clone(); 522 } 523 524 final int ND = NS * NUM_SAMPLE_DATA; 525 if (ev.mDataSamples.length < ND) { 526 System.arraycopy(o.mDataSamples, 0, ev.mDataSamples, 0, ND); 527 } else { 528 ev.mDataSamples = (float[])o.mDataSamples.clone(); 529 } 530 531 return ev; 532 } 533 534 /** 535 * Recycle the MotionEvent, to be re-used by a later caller. After calling 536 * this function you must not ever touch the event again. 537 */ 538 public void recycle() { 539 // Ensure recycle is only called once! 540 if (TRACK_RECYCLED_LOCATION) { 541 if (mRecycledLocation != null) { 542 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation); 543 } 544 mRecycledLocation = new RuntimeException("Last recycled here"); 545 } else if (mRecycled) { 546 throw new RuntimeException(toString() + " recycled twice!"); 547 } 548 549 //Log.w("MotionEvent", "Recycling event " + this, mRecycledLocation); 550 synchronized (gRecyclerLock) { 551 if (gRecyclerUsed < MAX_RECYCLED) { 552 gRecyclerUsed++; 553 mNumSamples = 0; 554 mNext = gRecyclerTop; 555 gRecyclerTop = this; 556 } 557 } 558 } 559 560 /** 561 * Return the kind of action being performed -- one of either 562 * {@link #ACTION_DOWN}, {@link #ACTION_MOVE}, {@link #ACTION_UP}, or 563 * {@link #ACTION_CANCEL}. 564 */ 565 public final int getAction() { 566 return mAction; 567 } 568 569 /** 570 * Returns the time (in ms) when the user originally pressed down to start 571 * a stream of position events. 572 */ 573 public final long getDownTime() { 574 return mDownTime; 575 } 576 577 /** 578 * Returns the time (in ms) when this specific event was generated. 579 */ 580 public final long getEventTime() { 581 return mTimeSamples[0]; 582 } 583 584 /** 585 * Returns the time (in ns) when this specific event was generated. 586 * The value is in nanosecond precision but it may not have nanosecond accuracy. 587 * 588 * @hide 589 */ 590 public final long getEventTimeNano() { 591 return mEventTimeNano; 592 } 593 594 /** 595 * The number of pointers of data contained in this event. Always 596 * >= 1. 597 */ 598 public final int getPointerCount() { 599 return mNumPointers; 600 } 601 602 /** 603 * {@link #getX(int)} for the first pointer (pointer 0). 604 */ 605 public final float getX() { 606 return mDataSamples[SAMPLE_X]; 607 } 608 609 /** 610 * {@link #getY(int)} for the first pointer (pointer 0). 611 */ 612 public final float getY() { 613 return mDataSamples[SAMPLE_Y]; 614 } 615 616 /** 617 * {@link #getPressure(int)} for the first pointer (pointer 0). 618 */ 619 public final float getPressure() { 620 return mDataSamples[SAMPLE_PRESSURE]; 621 } 622 623 /** 624 * {@link #getSize(int)} for the first pointer (pointer 0). 625 */ 626 public final float getSize() { 627 return mDataSamples[SAMPLE_SIZE]; 628 } 629 630 /** 631 * Returns the X coordinate of this event for the given pointer. 632 * Whole numbers are pixels; the 633 * value may have a fraction for input devices that are sub-pixel precise. 634 * @param pointer The desired pointer to retrieve. Value may be from 0 635 * (the first pointer) to {@link #getPointerCount()}-1. 636 */ 637 public final float getX(int pointer) { 638 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_X]; 639 } 640 641 /** 642 * Returns the Y coordinate of this event for the given pointer. 643 * Whole numbers are pixels; the 644 * value may have a fraction for input devices that are sub-pixel precise. 645 * @param pointer The desired pointer to retrieve. Value may be from 0 646 * (the first pointer) to {@link #getPointerCount()}-1. 647 */ 648 public final float getY(int pointer) { 649 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_Y]; 650 } 651 652 /** 653 * Returns the current pressure of this event for the given pointer. 654 * The pressure generally 655 * ranges from 0 (no pressure at all) to 1 (normal pressure), however 656 * values higher than 1 may be generated depending on the calibration of 657 * the input device. 658 * @param pointer The desired pointer to retrieve. Value may be from 0 659 * (the first pointer) to {@link #getPointerCount()}-1. 660 */ 661 public final float getPressure(int pointer) { 662 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_PRESSURE]; 663 } 664 665 /** 666 * Returns a scaled value of the approximate size for the given pointer, 667 * representing the area of the screen being pressed. 668 * The actual value in pixels corresponding to the 669 * touch is normalized with the device specific range of values 670 * and scaled to a value between 0 and 1. The value of size can be used to 671 * determine fat touch events. 672 * @param pointer The desired pointer to retrieve. Value may be from 0 673 * (the first pointer) to {@link #getPointerCount()}-1. 674 */ 675 public final float getSize(int pointer) { 676 return mDataSamples[(pointer*NUM_SAMPLE_DATA) + SAMPLE_SIZE]; 677 } 678 679 /** 680 * Returns the state of any meta / modifier keys that were in effect when 681 * the event was generated. This is the same values as those 682 * returned by {@link KeyEvent#getMetaState() KeyEvent.getMetaState}. 683 * 684 * @return an integer in which each bit set to 1 represents a pressed 685 * meta key 686 * 687 * @see KeyEvent#getMetaState() 688 */ 689 public final int getMetaState() { 690 return mMetaState; 691 } 692 693 /** 694 * Returns the original raw X coordinate of this event. For touch 695 * events on the screen, this is the original location of the event 696 * on the screen, before it had been adjusted for the containing window 697 * and views. 698 */ 699 public final float getRawX() { 700 return mRawX; 701 } 702 703 /** 704 * Returns the original raw Y coordinate of this event. For touch 705 * events on the screen, this is the original location of the event 706 * on the screen, before it had been adjusted for the containing window 707 * and views. 708 */ 709 public final float getRawY() { 710 return mRawY; 711 } 712 713 /** 714 * Return the precision of the X coordinates being reported. You can 715 * multiple this number with {@link #getX} to find the actual hardware 716 * value of the X coordinate. 717 * @return Returns the precision of X coordinates being reported. 718 */ 719 public final float getXPrecision() { 720 return mXPrecision; 721 } 722 723 /** 724 * Return the precision of the Y coordinates being reported. You can 725 * multiple this number with {@link #getY} to find the actual hardware 726 * value of the Y coordinate. 727 * @return Returns the precision of Y coordinates being reported. 728 */ 729 public final float getYPrecision() { 730 return mYPrecision; 731 } 732 733 /** 734 * Returns the number of historical points in this event. These are 735 * movements that have occurred between this event and the previous event. 736 * This only applies to ACTION_MOVE events -- all other actions will have 737 * a size of 0. 738 * 739 * @return Returns the number of historical points in the event. 740 */ 741 public final int getHistorySize() { 742 return mNumSamples - 1; 743 } 744 745 /** 746 * Returns the time that a historical movement occurred between this event 747 * and the previous event. Only applies to ACTION_MOVE events. 748 * 749 * @param pos Which historical value to return; must be less than 750 * {@link #getHistorySize} 751 * 752 * @see #getHistorySize 753 * @see #getEventTime 754 */ 755 public final long getHistoricalEventTime(int pos) { 756 return mTimeSamples[pos + 1]; 757 } 758 759 /** 760 * {@link #getHistoricalX(int)} for the first pointer (pointer 0). 761 */ 762 public final float getHistoricalX(int pos) { 763 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_X]; 764 } 765 766 /** 767 * {@link #getHistoricalY(int)} for the first pointer (pointer 0). 768 */ 769 public final float getHistoricalY(int pos) { 770 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_Y]; 771 } 772 773 /** 774 * {@link #getHistoricalPressure(int)} for the first pointer (pointer 0). 775 */ 776 public final float getHistoricalPressure(int pos) { 777 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_PRESSURE]; 778 } 779 780 /** 781 * {@link #getHistoricalSize(int)} for the first pointer (pointer 0). 782 */ 783 public final float getHistoricalSize(int pos) { 784 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) + SAMPLE_SIZE]; 785 } 786 787 /** 788 * Returns a historical X coordinate that occurred between this event 789 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 790 * 791 * @param pointer The desired pointer to retrieve. Value may be from 0 792 * (the first pointer) to {@link #getPointerCount()}-1. 793 * @param pos Which historical value to return; must be less than 794 * {@link #getHistorySize} 795 * 796 * @see #getHistorySize 797 * @see #getX 798 */ 799 public final float getHistoricalX(int pointer, int pos) { 800 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 801 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_X]; 802 } 803 804 /** 805 * Returns a historical Y coordinate that occurred between this event 806 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 807 * 808 * @param pointer The desired pointer to retrieve. Value may be from 0 809 * (the first pointer) to {@link #getPointerCount()}-1. 810 * @param pos Which historical value to return; must be less than 811 * {@link #getHistorySize} 812 * 813 * @see #getHistorySize 814 * @see #getY 815 */ 816 public final float getHistoricalY(int pointer, int pos) { 817 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 818 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_Y]; 819 } 820 821 /** 822 * Returns a historical pressure coordinate that occurred between this event 823 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 824 * 825 * @param pos Which historical value to return; must be less than 826 * {@link #getHistorySize} 827 * 828 * @param pointer The desired pointer to retrieve. Value may be from 0 829 * (the first pointer) to {@link #getPointerCount()}-1. 830 * @see #getHistorySize 831 * @see #getPressure 832 */ 833 public final float getHistoricalPressure(int pointer, int pos) { 834 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 835 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_PRESSURE]; 836 } 837 838 /** 839 * Returns a historical size coordinate that occurred between this event 840 * and the previous event for the given pointer. Only applies to ACTION_MOVE events. 841 * 842 * @param pos Which historical value to return; must be less than 843 * {@link #getHistorySize} 844 * 845 * @param pointer The desired pointer to retrieve. Value may be from 0 846 * (the first pointer) to {@link #getPointerCount()}-1. 847 * @see #getHistorySize 848 * @see #getSize 849 */ 850 public final float getHistoricalSize(int pointer, int pos) { 851 return mDataSamples[((pos + 1) * NUM_SAMPLE_DATA * mNumPointers) 852 + (pointer * NUM_SAMPLE_DATA) + SAMPLE_SIZE]; 853 } 854 855 /** 856 * Return the id for the device that this event came from. An id of 857 * zero indicates that the event didn't come from a physical device; other 858 * numbers are arbitrary and you shouldn't depend on the values. 859 */ 860 public final int getDeviceId() { 861 return mDeviceId; 862 } 863 864 /** 865 * Returns a bitfield indicating which edges, if any, where touched by this 866 * MotionEvent. For touch events, clients can use this to determine if the 867 * user's finger was touching the edge of the display. 868 * 869 * @see #EDGE_LEFT 870 * @see #EDGE_TOP 871 * @see #EDGE_RIGHT 872 * @see #EDGE_BOTTOM 873 */ 874 public final int getEdgeFlags() { 875 return mEdgeFlags; 876 } 877 878 879 /** 880 * Sets the bitfield indicating which edges, if any, where touched by this 881 * MotionEvent. 882 * 883 * @see #getEdgeFlags() 884 */ 885 public final void setEdgeFlags(int flags) { 886 mEdgeFlags = flags; 887 } 888 889 /** 890 * Sets this event's action. 891 */ 892 public final void setAction(int action) { 893 mAction = action; 894 } 895 896 /** 897 * Adjust this event's location. 898 * @param deltaX Amount to add to the current X coordinate of the event. 899 * @param deltaY Amount to add to the current Y coordinate of the event. 900 */ 901 public final void offsetLocation(float deltaX, float deltaY) { 902 final int N = mNumPointers*mNumSamples*4; 903 final float[] pos = mDataSamples; 904 for (int i=0; i<N; i+=NUM_SAMPLE_DATA) { 905 pos[i+SAMPLE_X] += deltaX; 906 pos[i+SAMPLE_Y] += deltaY; 907 } 908 } 909 910 /** 911 * Set this event's location. Applies {@link #offsetLocation} with a 912 * delta from the current location to the given new location. 913 * 914 * @param x New absolute X location. 915 * @param y New absolute Y location. 916 */ 917 public final void setLocation(float x, float y) { 918 float deltaX = x-mDataSamples[SAMPLE_X]; 919 float deltaY = y-mDataSamples[SAMPLE_Y]; 920 if (deltaX != 0 || deltaY != 0) { 921 offsetLocation(deltaX, deltaY); 922 } 923 } 924 925 /** 926 * Add a new movement to the batch of movements in this event. The event's 927 * current location, position and size is updated to the new values. In 928 * the future, the current values in the event will be added to a list of 929 * historic values. 930 * 931 * @param eventTime The time stamp for this data. 932 * @param x The new X position. 933 * @param y The new Y position. 934 * @param pressure The new pressure. 935 * @param size The new size. 936 * @param metaState Meta key state. 937 */ 938 public final void addBatch(long eventTime, float x, float y, 939 float pressure, float size, int metaState) { 940 int[] states = mStateSamples; 941 float[] data = mDataSamples; 942 long[] times = mTimeSamples; 943 944 final int NP = mNumPointers; 945 final int NS = mNumSamples; 946 final int NI = NP*NS; 947 final int ND = NI * NUM_SAMPLE_DATA; 948 if (states.length < (NI+NP)) { 949 // The state and data arrays are sized together, since their 950 // size is always a fixed factor from each other. 951 final int NEW_NI = NP * (NS+BASE_AVAIL_SAMPLES); 952 int[] newState = new int[NEW_NI]; 953 System.arraycopy(states, 0, newState, 0, NI); 954 mStateSamples = states = newState; 955 final int NEW_ND = NEW_NI * NUM_SAMPLE_DATA; 956 float[] newData = new float[NEW_ND]; 957 System.arraycopy(data, 0, newData, 0, ND); 958 mDataSamples = data = newData; 959 } 960 if (times.length <= NS) { 961 final int NEW_NS = NS + BASE_AVAIL_SAMPLES; 962 long[] newHistoryTimes = new long[NEW_NS]; 963 System.arraycopy(times, 0, newHistoryTimes, 0, NS); 964 mTimeSamples = times = newHistoryTimes; 965 } 966 967 times[NS] = times[0]; 968 times[0] = eventTime; 969 970 final int pos = NS*NUM_SAMPLE_DATA; 971 data[pos+SAMPLE_X] = data[SAMPLE_X]; 972 data[pos+SAMPLE_Y] = data[SAMPLE_Y]; 973 data[pos+SAMPLE_PRESSURE] = data[SAMPLE_PRESSURE]; 974 data[pos+SAMPLE_SIZE] = data[SAMPLE_SIZE]; 975 data[SAMPLE_X] = x; 976 data[SAMPLE_Y] = y; 977 data[SAMPLE_PRESSURE] = pressure; 978 data[SAMPLE_SIZE] = size; 979 mNumSamples = NS+1; 980 981 mRawX = x; 982 mRawY = y; 983 mMetaState |= metaState; 984 } 985 986 /** 987 * Add a new movement to the batch of movements in this event. The 988 * input data must contain (NUM_SAMPLE_DATA * {@link #getPointerCount()}) 989 * samples of data. 990 * 991 * @param eventTime The time stamp for this data. 992 * @param inData The actual data. 993 * @param metaState Meta key state. 994 * 995 * @hide 996 */ 997 public final void addBatch(long eventTime, float[] inData, int metaState) { 998 int[] states = mStateSamples; 999 float[] data = mDataSamples; 1000 long[] times = mTimeSamples; 1001 1002 final int NP = mNumPointers; 1003 final int NS = mNumSamples; 1004 final int NI = NP*NS; 1005 final int ND = NI * NUM_SAMPLE_DATA; 1006 if (states.length < (NI+NP)) { 1007 // The state and data arrays are sized together, since their 1008 // size is always a fixed factor from each other. 1009 final int NEW_NI = NP * (NS+BASE_AVAIL_SAMPLES); 1010 int[] newState = new int[NEW_NI]; 1011 System.arraycopy(states, 0, newState, 0, NI); 1012 mStateSamples = states = newState; 1013 final int NEW_ND = NEW_NI * NUM_SAMPLE_DATA; 1014 float[] newData = new float[NEW_ND]; 1015 System.arraycopy(data, 0, newData, 0, ND); 1016 mDataSamples = data = newData; 1017 } 1018 if (times.length <= NS) { 1019 final int NEW_NS = NS + BASE_AVAIL_SAMPLES; 1020 long[] newHistoryTimes = new long[NEW_NS]; 1021 System.arraycopy(times, 0, newHistoryTimes, 0, NS); 1022 mTimeSamples = times = newHistoryTimes; 1023 } 1024 1025 times[NS] = times[0]; 1026 times[0] = eventTime; 1027 1028 System.arraycopy(data, 0, data, ND, mNumPointers*NUM_SAMPLE_DATA); 1029 System.arraycopy(inData, 0, data, 0, mNumPointers*NUM_SAMPLE_DATA); 1030 1031 mNumSamples = NS+1; 1032 1033 mRawX = inData[SAMPLE_X]; 1034 mRawY = inData[SAMPLE_Y]; 1035 mMetaState |= metaState; 1036 } 1037 1038 @Override 1039 public String toString() { 1040 return "MotionEvent{" + Integer.toHexString(System.identityHashCode(this)) 1041 + " action=" + mAction + " x=" + getX() 1042 + " y=" + getY() + " pressure=" + getPressure() + " size=" + getSize() + "}"; 1043 } 1044 1045 public static final Parcelable.Creator<MotionEvent> CREATOR 1046 = new Parcelable.Creator<MotionEvent>() { 1047 public MotionEvent createFromParcel(Parcel in) { 1048 MotionEvent ev = obtain(); 1049 ev.readFromParcel(in); 1050 return ev; 1051 } 1052 1053 public MotionEvent[] newArray(int size) { 1054 return new MotionEvent[size]; 1055 } 1056 }; 1057 1058 public int describeContents() { 1059 return 0; 1060 } 1061 1062 public void writeToParcel(Parcel out, int flags) { 1063 out.writeLong(mDownTime); 1064 out.writeLong(mEventTimeNano); 1065 out.writeInt(mAction); 1066 out.writeInt(mMetaState); 1067 out.writeFloat(mRawX); 1068 out.writeFloat(mRawY); 1069 final int NP = mNumPointers; 1070 out.writeInt(NP); 1071 final int NS = mNumSamples; 1072 out.writeInt(NS); 1073 final int NI = NP*NS; 1074 if (NI > 0) { 1075 int i; 1076 int[] state = mStateSamples; 1077 for (i=0; i<NI; i++) { 1078 out.writeInt(state[i]); 1079 } 1080 final int NI4 = NI*NUM_SAMPLE_DATA; 1081 float[] history = mDataSamples; 1082 for (i=0; i<NI4; i++) { 1083 out.writeFloat(history[i]); 1084 } 1085 long[] times = mTimeSamples; 1086 for (i=0; i<NS; i++) { 1087 out.writeLong(times[i]); 1088 } 1089 } 1090 out.writeFloat(mXPrecision); 1091 out.writeFloat(mYPrecision); 1092 out.writeInt(mDeviceId); 1093 out.writeInt(mEdgeFlags); 1094 } 1095 1096 private void readFromParcel(Parcel in) { 1097 mDownTime = in.readLong(); 1098 mEventTimeNano = in.readLong(); 1099 mAction = in.readInt(); 1100 mMetaState = in.readInt(); 1101 mRawX = in.readFloat(); 1102 mRawY = in.readFloat(); 1103 final int NP = in.readInt(); 1104 mNumPointers = NP; 1105 final int NS = in.readInt(); 1106 mNumSamples = NS; 1107 final int NI = NP*NS; 1108 if (NI > 0) { 1109 final int NI4 = NI*4; 1110 int[] state = mStateSamples; 1111 if (state.length < NI) { 1112 mStateSamples = state = new int[NI]; 1113 } 1114 for (int i=0; i<NI; i++) { 1115 state[i] = in.readInt(); 1116 } 1117 float[] history = mDataSamples; 1118 if (history.length < NI4) { 1119 mDataSamples = history = new float[NI4]; 1120 } 1121 for (int i=0; i<NI4; i++) { 1122 history[i] = in.readFloat(); 1123 } 1124 long[] times = mTimeSamples; 1125 if (times == null || times.length < NS) { 1126 mTimeSamples = times = new long[NS]; 1127 } 1128 for (int i=0; i<NS; i++) { 1129 times[i] = in.readLong(); 1130 } 1131 } 1132 mXPrecision = in.readFloat(); 1133 mYPrecision = in.readFloat(); 1134 mDeviceId = in.readInt(); 1135 mEdgeFlags = in.readInt(); 1136 } 1137 1138} 1139