InputEvent.java revision 0029c66203ab9ded4342976bf7a17bb63af8c44a
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 static final int PARCEL_TOKEN_MOTION_EVENT = 1;
28    /** @hide */
29    protected static final int PARCEL_TOKEN_KEY_EVENT = 2;
30
31    /*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     */
43    public abstract int getDeviceId();
44
45    /**
46     * Gets the device that this event came from.
47     *
48     * @return The device, or null if unknown.
49     */
50    public final InputDevice getDevice() {
51        return InputDevice.getDevice(getDeviceId());
52    }
53
54    /**
55     * Gets the source of the event.
56     *
57     * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown.
58     * @see InputDevice#getSourceInfo
59     */
60    public abstract int getSource();
61
62    /**
63     * Modifies the source of the event.
64     *
65     * @param source The new source.
66     * @hide
67     */
68    public abstract void setSource(int source);
69
70    /**
71     * Recycles the event.
72     * This method should only be used by the system since applications do not
73     * expect {@link KeyEvent} objects to be recycled, although {@link MotionEvent}
74     * objects are fine.  See {@link KeyEvent#recycle()} for details.
75     * @hide
76     */
77    public abstract void recycle();
78
79    public int describeContents() {
80        return 0;
81    }
82
83    public static final Parcelable.Creator<InputEvent> CREATOR
84            = new Parcelable.Creator<InputEvent>() {
85        public InputEvent createFromParcel(Parcel in) {
86            int token = in.readInt();
87            if (token == PARCEL_TOKEN_KEY_EVENT) {
88                return KeyEvent.createFromParcelBody(in);
89            } else if (token == PARCEL_TOKEN_MOTION_EVENT) {
90                return MotionEvent.createFromParcelBody(in);
91            } else {
92                throw new IllegalStateException("Unexpected input event type token in parcel.");
93            }
94        }
95
96        public InputEvent[] newArray(int size) {
97            return new InputEvent[size];
98        }
99    };
100}
101