blob: f874e1df5548231013a357fcf7dc87e33e9d18a9 [file] [log] [blame]
Jeff Brownc5ed5912010-07-14 18:48:53 -07001/*
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
17package android.view;
18
Mathew Inwoode5ad5982018-08-17 15:07:52 +010019import android.annotation.UnsupportedAppUsage;
Jeff Brown6ec402b2010-07-28 15:48:59 -070020import android.os.Parcel;
Jeff Brownc5ed5912010-07-14 18:48:53 -070021import android.os.Parcelable;
22
Jeff Brown32cbc38552011-12-01 14:01:49 -080023import java.util.concurrent.atomic.AtomicInteger;
24
Jeff Brownc5ed5912010-07-14 18:48:53 -070025/**
26 * Common base class for input events.
27 */
28public abstract class InputEvent implements Parcelable {
Jeff Brown69206512010-09-12 17:17:30 -070029 /** @hide */
Jeff Brown6ec402b2010-07-28 15:48:59 -070030 protected static final int PARCEL_TOKEN_MOTION_EVENT = 1;
31 /** @hide */
32 protected static final int PARCEL_TOKEN_KEY_EVENT = 2;
Jeff Brown32cbc38552011-12-01 14:01:49 -080033
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 Brownc5ed5912010-07-14 18:48:53 -070046 /*package*/ InputEvent() {
Jeff Brown32cbc38552011-12-01 14:01:49 -080047 mSeq = mNextSeq.getAndIncrement();
Jeff Brownc5ed5912010-07-14 18:48:53 -070048 }
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.
55 *
56 * @return The device id.
57 * @see InputDevice#getDevice
58 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080059 public abstract int getDeviceId();
60
Jeff Brownc5ed5912010-07-14 18:48:53 -070061 /**
Jeff Browne33348b2010-07-15 23:54:05 -070062 * Gets the device that this event came from.
63 *
64 * @return The device, or null if unknown.
65 */
66 public final InputDevice getDevice() {
Jeff Brown91c69ab2011-02-14 17:03:18 -080067 return InputDevice.getDevice(getDeviceId());
Jeff Browne33348b2010-07-15 23:54:05 -070068 }
Jeff Brown91c69ab2011-02-14 17:03:18 -080069
Jeff Browne33348b2010-07-15 23:54:05 -070070 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -070071 * Gets the source of the event.
72 *
Jeff Browne33348b2010-07-15 23:54:05 -070073 * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown.
John Spurlock600cba92013-04-18 08:53:56 -040074 * @see InputDevice#getSources
Jeff Brownc5ed5912010-07-14 18:48:53 -070075 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080076 public abstract int getSource();
77
Jeff Brownc5ed5912010-07-14 18:48:53 -070078 /**
79 * Modifies the source of the event.
Jeff Brown91c69ab2011-02-14 17:03:18 -080080 *
81 * @param source The new source.
Jeff Brownc5ed5912010-07-14 18:48:53 -070082 * @hide
83 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080084 public abstract void setSource(int source);
85
Jeff Brown0029c662011-03-30 02:25:18 -070086 /**
Michael Wright74e41562013-03-08 14:58:14 -080087 * 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 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -080099 * Copies the event.
100 *
101 * @return A deep copy of the event.
102 * @hide
103 */
104 public abstract InputEvent copy();
105
106 /**
Jeff Brown0029c662011-03-30 02:25:18 -0700107 * Recycles the event.
108 * This method should only be used by the system since applications do not
109 * expect {@link KeyEvent} objects to be recycled, although {@link MotionEvent}
110 * objects are fine. See {@link KeyEvent#recycle()} for details.
111 * @hide
112 */
Jeff Brown32cbc38552011-12-01 14:01:49 -0800113 public void recycle() {
114 if (TRACK_RECYCLED_LOCATION) {
115 if (mRecycledLocation != null) {
116 throw new RuntimeException(toString() + " recycled twice!", mRecycledLocation);
117 }
118 mRecycledLocation = new RuntimeException("Last recycled here");
119 } else {
120 if (mRecycled) {
121 throw new RuntimeException(toString() + " recycled twice!");
122 }
123 mRecycled = true;
124 }
125 }
126
127 /**
Jeff Brown92cc2d82011-12-02 01:19:47 -0800128 * Conditionally recycled the event if it is appropriate to do so after
129 * dispatching the event to an application.
130 *
131 * If the event is a {@link MotionEvent} then it is recycled.
132 *
133 * If the event is a {@link KeyEvent} then it is NOT recycled, because applications
134 * expect key events to be immutable so once the event has been dispatched to
135 * the application we can no longer recycle it.
136 * @hide
137 */
138 public void recycleIfNeededAfterDispatch() {
139 recycle();
140 }
141
142 /**
Jeff Brown32cbc38552011-12-01 14:01:49 -0800143 * Reinitializes the event on reuse (after recycling).
144 * @hide
145 */
146 protected void prepareForReuse() {
147 mRecycled = false;
148 mRecycledLocation = null;
149 mSeq = mNextSeq.getAndIncrement();
150 }
Jeff Brown0029c662011-03-30 02:25:18 -0700151
Jeff Brown21bc5c92011-02-28 18:27:14 -0800152 /**
153 * Gets a private flag that indicates when the system has detected that this input event
154 * may be inconsistent with respect to the sequence of previously delivered input events,
155 * such as when a key up event is sent but the key was not down or when a pointer
156 * move event is sent but the pointer is not down.
157 *
158 * @return True if this event is tainted.
159 * @hide
160 */
161 public abstract boolean isTainted();
162
163 /**
164 * Sets a private flag that indicates when the system has detected that this input event
165 * may be inconsistent with respect to the sequence of previously delivered input events,
166 * such as when a key up event is sent but the key was not down or when a pointer
167 * move event is sent but the pointer is not down.
168 *
169 * @param tainted True if this event is tainted.
170 * @hide
171 */
172 public abstract void setTainted(boolean tainted);
173
Jeff Brown4e91a182011-04-07 11:38:09 -0700174 /**
Jeff Brownb11499d2012-04-20 19:54:22 -0700175 * Retrieve the time this event occurred,
176 * in the {@link android.os.SystemClock#uptimeMillis} time base.
177 *
178 * @return Returns the time this event occurred,
179 * in the {@link android.os.SystemClock#uptimeMillis} time base.
180 */
181 public abstract long getEventTime();
182
183 /**
184 * Retrieve the time this event occurred,
185 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
186 * nanosecond (instead of millisecond) precision.
187 * <p>
Jeff Brown4e91a182011-04-07 11:38:09 -0700188 * The value is in nanosecond precision but it may not have nanosecond accuracy.
Jeff Brownb11499d2012-04-20 19:54:22 -0700189 * </p>
190 *
191 * @return Returns the time this event occurred,
192 * in the {@link android.os.SystemClock#uptimeMillis} time base but with
193 * nanosecond (instead of millisecond) precision.
194 *
Jeff Brown4e91a182011-04-07 11:38:09 -0700195 * @hide
196 */
197 public abstract long getEventTimeNano();
198
Jeff Brown32cbc38552011-12-01 14:01:49 -0800199 /**
Wale Ogunwalec3672cd2014-11-05 15:17:35 -0800200 * Marks the input event as being canceled.
201 *
202 * @hide
203 */
204 public abstract void cancel();
205
206 /**
Jeff Brown32cbc38552011-12-01 14:01:49 -0800207 * Gets the unique sequence number of this event.
208 * Every input event that is created or received by a process has a
209 * unique sequence number. Moreover, a new sequence number is obtained
210 * each time an event object is recycled.
211 *
212 * Sequence numbers are only guaranteed to be locally unique within a process.
213 * Sequence numbers are not preserved when events are parceled.
214 *
215 * @return The unique sequence number of this event.
216 * @hide
217 */
Mathew Inwoode5ad5982018-08-17 15:07:52 +0100218 @UnsupportedAppUsage
Jeff Brown32cbc38552011-12-01 14:01:49 -0800219 public int getSequenceNumber() {
220 return mSeq;
221 }
222
Jeff Brown69206512010-09-12 17:17:30 -0700223 public int describeContents() {
Jeff Brown6ec402b2010-07-28 15:48:59 -0700224 return 0;
225 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800226
Jeff Brown6ec402b2010-07-28 15:48:59 -0700227 public static final Parcelable.Creator<InputEvent> CREATOR
228 = new Parcelable.Creator<InputEvent>() {
229 public InputEvent createFromParcel(Parcel in) {
230 int token = in.readInt();
231 if (token == PARCEL_TOKEN_KEY_EVENT) {
232 return KeyEvent.createFromParcelBody(in);
233 } else if (token == PARCEL_TOKEN_MOTION_EVENT) {
234 return MotionEvent.createFromParcelBody(in);
235 } else {
236 throw new IllegalStateException("Unexpected input event type token in parcel.");
237 }
238 }
239
240 public InputEvent[] newArray(int size) {
241 return new InputEvent[size];
242 }
243 };
Jeff Brownc5ed5912010-07-14 18:48:53 -0700244}