blob: f6aeb398fa13f0837bbb454157614cd96b5ea48a [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 Brown69206512010-09-12 17:17:30 -070070 public int describeContents() {
Jeff Brown6ec402b2010-07-28 15:48:59 -070071 return 0;
72 }
Jeff Brown91c69ab2011-02-14 17:03:18 -080073
Jeff Brown6ec402b2010-07-28 15:48:59 -070074 public static final Parcelable.Creator<InputEvent> CREATOR
75 = new Parcelable.Creator<InputEvent>() {
76 public InputEvent createFromParcel(Parcel in) {
77 int token = in.readInt();
78 if (token == PARCEL_TOKEN_KEY_EVENT) {
79 return KeyEvent.createFromParcelBody(in);
80 } else if (token == PARCEL_TOKEN_MOTION_EVENT) {
81 return MotionEvent.createFromParcelBody(in);
82 } else {
83 throw new IllegalStateException("Unexpected input event type token in parcel.");
84 }
85 }
86
87 public InputEvent[] newArray(int size) {
88 return new InputEvent[size];
89 }
90 };
Jeff Brownc5ed5912010-07-14 18:48:53 -070091}