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
17package android.view;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22/**
23 * Common base class for input events.
24 */
25public abstract class InputEvent implements Parcelable {
26    /** @hide */
27    protected int mDeviceId;
28    /** @hide */
29    protected int mSource;
30
31    /** @hide */
32    protected static final int PARCEL_TOKEN_MOTION_EVENT = 1;
33    /** @hide */
34    protected static final int PARCEL_TOKEN_KEY_EVENT = 2;
35
36    /*package*/ InputEvent() {
37    }
38
39    /**
40     * Gets the id for the device that this event came from.  An id of
41     * zero indicates that the event didn't come from a physical device
42     * and maps to the default keymap.  The other numbers are arbitrary and
43     * you shouldn't depend on the values.
44     *
45     * @return The device id.
46     * @see InputDevice#getDevice
47     */
48    public final int getDeviceId() {
49        return mDeviceId;
50    }
51
52    /**
53     * Gets the device that this event came from.
54     *
55     * @return The device, or null if unknown.
56     */
57    public final InputDevice getDevice() {
58        return InputDevice.getDevice(mDeviceId);
59    }
60
61    /**
62     * Gets the source of the event.
63     *
64     * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown.
65     * @see InputDevice#getSourceInfo
66     */
67    public final int getSource() {
68        return mSource;
69    }
70
71    /**
72     * Modifies the source of the event.
73     * @param source The source.
74     *
75     * @hide
76     */
77    public final void setSource(int source) {
78        mSource = source;
79    }
80
81    public int describeContents() {
82        return 0;
83    }
84
85    /** @hide */
86    protected final void readBaseFromParcel(Parcel in) {
87        mDeviceId = in.readInt();
88        mSource = in.readInt();
89    }
90
91    /** @hide */
92    protected final void writeBaseToParcel(Parcel out) {
93        out.writeInt(mDeviceId);
94        out.writeInt(mSource);
95    }
96
97    public static final Parcelable.Creator<InputEvent> CREATOR
98            = new Parcelable.Creator<InputEvent>() {
99        public InputEvent createFromParcel(Parcel in) {
100            int token = in.readInt();
101            if (token == PARCEL_TOKEN_KEY_EVENT) {
102                return KeyEvent.createFromParcelBody(in);
103            } else if (token == PARCEL_TOKEN_MOTION_EVENT) {
104                return MotionEvent.createFromParcelBody(in);
105            } else {
106                throw new IllegalStateException("Unexpected input event type token in parcel.");
107            }
108        }
109
110        public InputEvent[] newArray(int size) {
111            return new InputEvent[size];
112        }
113    };
114}
115