Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 20 | import android.os.Parcel; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 21 | import android.os.Parcelable; |
| 22 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicInteger; |
| 24 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 25 | /** |
| 26 | * Common base class for input events. |
| 27 | */ |
| 28 | public abstract class InputEvent implements Parcelable { |
Jeff Brown | 6920651 | 2010-09-12 17:17:30 -0700 | [diff] [blame] | 29 | /** @hide */ |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 30 | protected static final int PARCEL_TOKEN_MOTION_EVENT = 1; |
| 31 | /** @hide */ |
| 32 | protected static final int PARCEL_TOKEN_KEY_EVENT = 2; |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 33 | |
| 34 | // Next sequence number. |
| 35 | private static final AtomicInteger mNextSeq = new AtomicInteger(); |
| 36 | |
| 37 | /** @hide */ |
| 38 | protected int mSeq; |
| 39 | |
| 40 | /** @hide */ |
| 41 | protected boolean mRecycled; |
| 42 | |
| 43 | private static final boolean TRACK_RECYCLED_LOCATION = false; |
| 44 | private RuntimeException mRecycledLocation; |
| 45 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 46 | /*package*/ InputEvent() { |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 47 | mSeq = mNextSeq.getAndIncrement(); |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Gets the id for the device that this event came from. An id of |
| 52 | * zero indicates that the event didn't come from a physical device |
| 53 | * and maps to the default keymap. The other numbers are arbitrary and |
| 54 | * you shouldn't depend on the values. |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 55 | * |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 56 | * @return The device id. |
| 57 | * @see InputDevice#getDevice |
| 58 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 59 | public abstract int getDeviceId(); |
| 60 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 61 | /** |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 62 | * Gets the device that this event came from. |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 63 | * |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 64 | * @return The device, or null if unknown. |
| 65 | */ |
| 66 | public final InputDevice getDevice() { |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 67 | return InputDevice.getDevice(getDeviceId()); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 68 | } |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 69 | |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 70 | /** |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 71 | * Gets the source of the event. |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 72 | * |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 73 | * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown. |
John Spurlock | 600cba9 | 2013-04-18 08:53:56 -0400 | [diff] [blame] | 74 | * @see InputDevice#getSources |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 75 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 76 | public abstract int getSource(); |
| 77 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Modifies the source of the event. |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 80 | * |
| 81 | * @param source The new source. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 82 | * @hide |
| 83 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 84 | public abstract void setSource(int source); |
| 85 | |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 86 | /** |
Michael Wright | 74e4156 | 2013-03-08 14:58:14 -0800 | [diff] [blame] | 87 | * Determines whether the event is from the given source. |
| 88 | * |
| 89 | * @param source The input source to check against. This can be a specific device type, such as |
| 90 | * {@link InputDevice#SOURCE_TOUCH_NAVIGATION}, or a more generic device class, such as |
| 91 | * {@link InputDevice#SOURCE_CLASS_POINTER}. |
| 92 | * @return Whether the event is from the given source. |
| 93 | */ |
| 94 | public boolean isFromSource(int source) { |
| 95 | return (getSource() & source) == source; |
| 96 | } |
| 97 | |
| 98 | /** |
Siarhei Vishniakou | 91fa08f | 2018-06-08 22:49:30 +0100 | [diff] [blame] | 99 | * Gets the display id of the event. |
| 100 | * @return The display id associated with the event. |
| 101 | * @hide |
| 102 | */ |
| 103 | public abstract int getDisplayId(); |
| 104 | |
| 105 | /** |
| 106 | * Modifies the display id associated with the event |
| 107 | * @param displayId |
| 108 | * @hide |
| 109 | */ |
| 110 | public abstract void setDisplayId(int displayId); |
| 111 | /** |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 112 | * Copies the event. |
| 113 | * |
| 114 | * @return A deep copy of the event. |
| 115 | * @hide |
| 116 | */ |
| 117 | public abstract InputEvent copy(); |
| 118 | |
| 119 | /** |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 120 | * Recycles the event. |
| 121 | * This method should only be used by the system since applications do not |
| 122 | * expect {@link KeyEvent} objects to be recycled, although {@link MotionEvent} |
| 123 | * objects are fine. See {@link KeyEvent#recycle()} for details. |
| 124 | * @hide |
| 125 | */ |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 126 | public void recycle() { |
| 127 | if (TRACK_RECYCLED_LOCATION) { |
| 128 | if (mRecycledLocation != null) { |
| 129 | throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation); |
| 130 | } |
| 131 | mRecycledLocation = new RuntimeException("Last recycled here"); |
| 132 | } else { |
| 133 | if (mRecycled) { |
| 134 | throw new RuntimeException(toString() + " recycled twice!"); |
| 135 | } |
| 136 | mRecycled = true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
Jeff Brown | 92cc2d8 | 2011-12-02 01:19:47 -0800 | [diff] [blame] | 141 | * Conditionally recycled the event if it is appropriate to do so after |
| 142 | * dispatching the event to an application. |
| 143 | * |
| 144 | * If the event is a {@link MotionEvent} then it is recycled. |
| 145 | * |
| 146 | * If the event is a {@link KeyEvent} then it is NOT recycled, because applications |
| 147 | * expect key events to be immutable so once the event has been dispatched to |
| 148 | * the application we can no longer recycle it. |
| 149 | * @hide |
| 150 | */ |
| 151 | public void recycleIfNeededAfterDispatch() { |
| 152 | recycle(); |
| 153 | } |
| 154 | |
| 155 | /** |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 156 | * Reinitializes the event on reuse (after recycling). |
| 157 | * @hide |
| 158 | */ |
| 159 | protected void prepareForReuse() { |
| 160 | mRecycled = false; |
| 161 | mRecycledLocation = null; |
| 162 | mSeq = mNextSeq.getAndIncrement(); |
| 163 | } |
Jeff Brown | 0029c66 | 2011-03-30 02:25:18 -0700 | [diff] [blame] | 164 | |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Gets a private flag that indicates when the system has detected that this input event |
| 167 | * may be inconsistent with respect to the sequence of previously delivered input events, |
| 168 | * such as when a key up event is sent but the key was not down or when a pointer |
| 169 | * move event is sent but the pointer is not down. |
| 170 | * |
| 171 | * @return True if this event is tainted. |
| 172 | * @hide |
| 173 | */ |
| 174 | public abstract boolean isTainted(); |
| 175 | |
| 176 | /** |
| 177 | * Sets a private flag that indicates when the system has detected that this input event |
| 178 | * may be inconsistent with respect to the sequence of previously delivered input events, |
| 179 | * such as when a key up event is sent but the key was not down or when a pointer |
| 180 | * move event is sent but the pointer is not down. |
| 181 | * |
| 182 | * @param tainted True if this event is tainted. |
| 183 | * @hide |
| 184 | */ |
| 185 | public abstract void setTainted(boolean tainted); |
| 186 | |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 187 | /** |
Jeff Brown | b11499d | 2012-04-20 19:54:22 -0700 | [diff] [blame] | 188 | * Retrieve the time this event occurred, |
| 189 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 190 | * |
| 191 | * @return Returns the time this event occurred, |
| 192 | * in the {@link android.os.SystemClock#uptimeMillis} time base. |
| 193 | */ |
| 194 | public abstract long getEventTime(); |
| 195 | |
| 196 | /** |
| 197 | * Retrieve the time this event occurred, |
| 198 | * in the {@link android.os.SystemClock#uptimeMillis} time base but with |
| 199 | * nanosecond (instead of millisecond) precision. |
| 200 | * <p> |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 201 | * The value is in nanosecond precision but it may not have nanosecond accuracy. |
Jeff Brown | b11499d | 2012-04-20 19:54:22 -0700 | [diff] [blame] | 202 | * </p> |
| 203 | * |
| 204 | * @return Returns the time this event occurred, |
| 205 | * in the {@link android.os.SystemClock#uptimeMillis} time base but with |
| 206 | * nanosecond (instead of millisecond) precision. |
| 207 | * |
Jeff Brown | 4e91a18 | 2011-04-07 11:38:09 -0700 | [diff] [blame] | 208 | * @hide |
| 209 | */ |
| 210 | public abstract long getEventTimeNano(); |
| 211 | |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 212 | /** |
Wale Ogunwale | c3672cd | 2014-11-05 15:17:35 -0800 | [diff] [blame] | 213 | * Marks the input event as being canceled. |
| 214 | * |
| 215 | * @hide |
| 216 | */ |
| 217 | public abstract void cancel(); |
| 218 | |
| 219 | /** |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 220 | * Gets the unique sequence number of this event. |
| 221 | * Every input event that is created or received by a process has a |
| 222 | * unique sequence number. Moreover, a new sequence number is obtained |
| 223 | * each time an event object is recycled. |
| 224 | * |
| 225 | * Sequence numbers are only guaranteed to be locally unique within a process. |
| 226 | * Sequence numbers are not preserved when events are parceled. |
| 227 | * |
| 228 | * @return The unique sequence number of this event. |
| 229 | * @hide |
| 230 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 231 | @UnsupportedAppUsage |
Jeff Brown | 32cbc3855 | 2011-12-01 14:01:49 -0800 | [diff] [blame] | 232 | public int getSequenceNumber() { |
| 233 | return mSeq; |
| 234 | } |
| 235 | |
Jeff Brown | 6920651 | 2010-09-12 17:17:30 -0700 | [diff] [blame] | 236 | public int describeContents() { |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 237 | return 0; |
| 238 | } |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 239 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 240 | public static final @android.annotation.NonNull Parcelable.Creator<InputEvent> CREATOR |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 241 | = new Parcelable.Creator<InputEvent>() { |
| 242 | public InputEvent createFromParcel(Parcel in) { |
| 243 | int token = in.readInt(); |
| 244 | if (token == PARCEL_TOKEN_KEY_EVENT) { |
| 245 | return KeyEvent.createFromParcelBody(in); |
| 246 | } else if (token == PARCEL_TOKEN_MOTION_EVENT) { |
| 247 | return MotionEvent.createFromParcelBody(in); |
| 248 | } else { |
| 249 | throw new IllegalStateException("Unexpected input event type token in parcel."); |
| 250 | } |
| 251 | } |
Siarhei Vishniakou | 85ddfff | 2018-01-31 16:49:36 -0800 | [diff] [blame] | 252 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 253 | public InputEvent[] newArray(int size) { |
| 254 | return new InputEvent[size]; |
| 255 | } |
| 256 | }; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 257 | } |