Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 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 | |
| 17 | package android.view; |
| 18 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 19 | import android.os.Parcel; |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 20 | import android.os.Parcelable; |
| 21 | |
| 22 | /** |
| 23 | * Common base class for input events. |
| 24 | */ |
| 25 | public abstract class InputEvent implements Parcelable { |
Jeff Brown | 6920651 | 2010-09-12 17:17:30 -0700 | [diff] [blame] | 26 | /** @hide */ |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 27 | protected static final int PARCEL_TOKEN_MOTION_EVENT = 1; |
| 28 | /** @hide */ |
| 29 | protected static final int PARCEL_TOKEN_KEY_EVENT = 2; |
| 30 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 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 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 43 | public abstract int getDeviceId(); |
| 44 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 45 | /** |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 46 | * Gets the device that this event came from. |
| 47 | * |
| 48 | * @return The device, or null if unknown. |
| 49 | */ |
| 50 | public final InputDevice getDevice() { |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 51 | return InputDevice.getDevice(getDeviceId()); |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 52 | } |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 53 | |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 54 | /** |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 55 | * Gets the source of the event. |
| 56 | * |
Jeff Brown | e33348b | 2010-07-15 23:54:05 -0700 | [diff] [blame] | 57 | * @return The event source or {@link InputDevice#SOURCE_UNKNOWN} if unknown. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 58 | * @see InputDevice#getSourceInfo |
| 59 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 60 | public abstract int getSource(); |
| 61 | |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Modifies the source of the event. |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 64 | * |
| 65 | * @param source The new source. |
Jeff Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 66 | * @hide |
| 67 | */ |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 68 | public abstract void setSource(int source); |
| 69 | |
Jeff Brown | 6920651 | 2010-09-12 17:17:30 -0700 | [diff] [blame] | 70 | public int describeContents() { |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 71 | return 0; |
| 72 | } |
Jeff Brown | 91c69ab | 2011-02-14 17:03:18 -0800 | [diff] [blame] | 73 | |
Jeff Brown | 6ec402b | 2010-07-28 15:48:59 -0700 | [diff] [blame] | 74 | 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 Brown | c5ed591 | 2010-07-14 18:48:53 -0700 | [diff] [blame] | 91 | } |