1c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown/* 2c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Copyright (C) 2010 The Android Open Source Project 3c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * 4c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Licensed under the Apache License, Version 2.0 (the "License"); 5c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * you may not use this file except in compliance with the License. 6c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * You may obtain a copy of the License at 7c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * 8c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * http://www.apache.org/licenses/LICENSE-2.0 9c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * 10c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Unless required by applicable law or agreed to in writing, software 11c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * distributed under the License is distributed on an "AS IS" BASIS, 12c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * See the License for the specific language governing permissions and 14c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * limitations under the License. 15c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown */ 16c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown 17c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brownpackage android.view; 18c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown 196ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brownimport android.os.Parcel; 20c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brownimport android.os.Parcelable; 21c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown 22c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown/** 23c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Common base class for input events. 24c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown */ 25c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brownpublic abstract class InputEvent implements Parcelable { 26692065128e66de77470de2c50ead2bef0452952aJeff Brown /** @hide */ 276ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown protected static final int PARCEL_TOKEN_MOTION_EVENT = 1; 286ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown /** @hide */ 296ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown protected static final int PARCEL_TOKEN_KEY_EVENT = 2; 306ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown 31c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown /*package*/ InputEvent() { 32c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown } 33c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown 34c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown /** 35c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Gets the id for the device that this event came from. An id of 36c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * zero indicates that the event didn't come from a physical device 37c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * and maps to the default keymap. The other numbers are arbitrary and 38c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * you shouldn't depend on the values. 39c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * 40c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * @return The device id. 41c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * @see InputDevice#getDevice 42c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown */ 4391c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown public abstract int getDeviceId(); 4491c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown 45c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown /** 46e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown * Gets the device that this event came from. 47e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown * 48e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown * @return The device, or null if unknown. 49e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown */ 50e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown public final InputDevice getDevice() { 5191c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown return InputDevice.getDevice(getDeviceId()); 52e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown } 5391c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown 54e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown /** 55c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Gets the source of the event. 56c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * 57e33348ba54cd68d6936cffd4507037c14d4b10c2Jeff Brown * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown. 58c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * @see InputDevice#getSourceInfo 59c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown */ 6091c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown public abstract int getSource(); 6191c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown 62c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown /** 63c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * Modifies the source of the event. 6491c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown * 6591c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown * @param source The new source. 66c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown * @hide 67c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown */ 6891c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown public abstract void setSource(int source); 6991c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown 700029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown /** 7121bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * Copies the event. 7221bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * 7321bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @return A deep copy of the event. 7421bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @hide 7521bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown */ 7621bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown public abstract InputEvent copy(); 7721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown 7821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown /** 790029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * Recycles the event. 800029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * This method should only be used by the system since applications do not 810029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * expect {@link KeyEvent} objects to be recycled, although {@link MotionEvent} 820029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * objects are fine. See {@link KeyEvent#recycle()} for details. 830029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown * @hide 840029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown */ 850029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown public abstract void recycle(); 860029c66203ab9ded4342976bf7a17bb63af8c44aJeff Brown 8721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown /** 8821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * Gets a private flag that indicates when the system has detected that this input event 8921bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * may be inconsistent with respect to the sequence of previously delivered input events, 9021bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * such as when a key up event is sent but the key was not down or when a pointer 9121bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * move event is sent but the pointer is not down. 9221bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * 9321bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @return True if this event is tainted. 9421bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @hide 9521bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown */ 9621bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown public abstract boolean isTainted(); 9721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown 9821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown /** 9921bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * Sets a private flag that indicates when the system has detected that this input event 10021bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * may be inconsistent with respect to the sequence of previously delivered input events, 10121bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * such as when a key up event is sent but the key was not down or when a pointer 10221bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * move event is sent but the pointer is not down. 10321bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * 10421bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @param tainted True if this event is tainted. 10521bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown * @hide 10621bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown */ 10721bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown public abstract void setTainted(boolean tainted); 10821bc5c917d4ee2a9b2b8173091e6bba85eaff899Jeff Brown 1094e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown /** 1104e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown * Returns the time (in ns) when this specific event was generated. 1114e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown * The value is in nanosecond precision but it may not have nanosecond accuracy. 1124e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown * @hide 1134e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown */ 1144e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown public abstract long getEventTimeNano(); 1154e91a180be46c0c7c3bf398d4df4cbe2404216b5Jeff Brown 116692065128e66de77470de2c50ead2bef0452952aJeff Brown public int describeContents() { 1176ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown return 0; 1186ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } 11991c69ab01539f7ba28708f41ec1835cc2920d0a0Jeff Brown 1206ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown public static final Parcelable.Creator<InputEvent> CREATOR 1216ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown = new Parcelable.Creator<InputEvent>() { 1226ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown public InputEvent createFromParcel(Parcel in) { 1236ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown int token = in.readInt(); 1246ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown if (token == PARCEL_TOKEN_KEY_EVENT) { 1256ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown return KeyEvent.createFromParcelBody(in); 1266ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } else if (token == PARCEL_TOKEN_MOTION_EVENT) { 1276ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown return MotionEvent.createFromParcelBody(in); 1286ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } else { 1296ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown throw new IllegalStateException("Unexpected input event type token in parcel."); 1306ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } 1316ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } 1326ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown 1336ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown public InputEvent[] newArray(int size) { 1346ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown return new InputEvent[size]; 1356ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown } 1366ec402b5ae33c8927694d8522b4cc6a5c8ba974eJeff Brown }; 137c5ed5910c9ef066cec6a13bbb404ec57b1e92637Jeff Brown} 138