blob: 01ddcc9bf1a411ccb7bb9e93d331a0d3f8eb3566 [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
Jeff Brown6ec402b2010-07-28 15:48:59 -070019import android.os.Parcel;
Jeff Brownc5ed5912010-07-14 18:48:53 -070020import android.os.Parcelable;
21
22/**
23 * Common base class for input events.
24 */
25public abstract class InputEvent implements Parcelable {
Jeff Brown69206512010-09-12 17:17:30 -070026 /** @hide */
Jeff Brown6ec402b2010-07-28 15:48:59 -070027 protected static final int PARCEL_TOKEN_MOTION_EVENT = 1;
28 /** @hide */
29 protected static final int PARCEL_TOKEN_KEY_EVENT = 2;
30
Jeff Brownc5ed5912010-07-14 18:48:53 -070031 /*package*/ InputEvent() {
32 }
33
34 /**
35 * Gets the id for the device that this event came from. An id of
36 * zero indicates that the event didn't come from a physical device
37 * and maps to the default keymap. The other numbers are arbitrary and
38 * you shouldn't depend on the values.
39 *
40 * @return The device id.
41 * @see InputDevice#getDevice
42 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080043 public abstract int getDeviceId();
44
Jeff Brownc5ed5912010-07-14 18:48:53 -070045 /**
Jeff Browne33348b2010-07-15 23:54:05 -070046 * Gets the device that this event came from.
47 *
48 * @return The device, or null if unknown.
49 */
50 public final InputDevice getDevice() {
Jeff Brown91c69ab2011-02-14 17:03:18 -080051 return InputDevice.getDevice(getDeviceId());
Jeff Browne33348b2010-07-15 23:54:05 -070052 }
Jeff Brown91c69ab2011-02-14 17:03:18 -080053
Jeff Browne33348b2010-07-15 23:54:05 -070054 /**
Jeff Brownc5ed5912010-07-14 18:48:53 -070055 * Gets the source of the event.
56 *
Jeff Browne33348b2010-07-15 23:54:05 -070057 * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown.
Jeff Brownc5ed5912010-07-14 18:48:53 -070058 * @see InputDevice#getSourceInfo
59 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080060 public abstract int getSource();
61
Jeff Brownc5ed5912010-07-14 18:48:53 -070062 /**
63 * Modifies the source of the event.
Jeff Brown91c69ab2011-02-14 17:03:18 -080064 *
65 * @param source The new source.
Jeff Brownc5ed5912010-07-14 18:48:53 -070066 * @hide
67 */
Jeff Brown91c69ab2011-02-14 17:03:18 -080068 public abstract void setSource(int source);
69
Jeff Brown0029c662011-03-30 02:25:18 -070070 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -080071 * Copies the event.
72 *
73 * @return A deep copy of the event.
74 * @hide
75 */
76 public abstract InputEvent copy();
77
78 /**
Jeff Brown0029c662011-03-30 02:25:18 -070079 * Recycles the event.
80 * This method should only be used by the system since applications do not
81 * expect {@link KeyEvent} objects to be recycled, although {@link MotionEvent}
82 * objects are fine. See {@link KeyEvent#recycle()} for details.
83 * @hide
84 */
85 public abstract void recycle();
86
Jeff Brown21bc5c92011-02-28 18:27:14 -080087 /**
88 * Gets a private flag that indicates when the system has detected that this input event
89 * may be inconsistent with respect to the sequence of previously delivered input events,
90 * such as when a key up event is sent but the key was not down or when a pointer
91 * move event is sent but the pointer is not down.
92 *
93 * @return True if this event is tainted.
94 * @hide
95 */
96 public abstract boolean isTainted();
97
98 /**
99 * Sets a private flag that indicates when the system has detected that this input event
100 * may be inconsistent with respect to the sequence of previously delivered input events,
101 * such as when a key up event is sent but the key was not down or when a pointer
102 * move event is sent but the pointer is not down.
103 *
104 * @param tainted True if this event is tainted.
105 * @hide
106 */
107 public abstract void setTainted(boolean tainted);
108
Jeff Brown4e91a182011-04-07 11:38:09 -0700109 /**
110 * Returns the time (in ns) when this specific event was generated.
111 * The value is in nanosecond precision but it may not have nanosecond accuracy.
112 * @hide
113 */
114 public abstract long getEventTimeNano();
115
Jeff Brown69206512010-09-12 17:17:30 -0700116 public int describeContents() {
Jeff Brown6ec402b2010-07-28 15:48:59 -0700117 return 0;
118 }
Jeff Brown91c69ab2011-02-14 17:03:18 -0800119
Jeff Brown6ec402b2010-07-28 15:48:59 -0700120 public static final Parcelable.Creator<InputEvent> CREATOR
121 = new Parcelable.Creator<InputEvent>() {
122 public InputEvent createFromParcel(Parcel in) {
123 int token = in.readInt();
124 if (token == PARCEL_TOKEN_KEY_EVENT) {
125 return KeyEvent.createFromParcelBody(in);
126 } else if (token == PARCEL_TOKEN_MOTION_EVENT) {
127 return MotionEvent.createFromParcelBody(in);
128 } else {
129 throw new IllegalStateException("Unexpected input event type token in parcel.");
130 }
131 }
132
133 public InputEvent[] newArray(int size) {
134 return new InputEvent[size];
135 }
136 };
Jeff Brownc5ed5912010-07-14 18:48:53 -0700137}