Home | History | Annotate | Download | only in view
      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 
     19 import android.os.Parcel;
     20 import android.os.Parcelable;
     21 
     22 /**
     23  * Common base class for input events.
     24  */
     25 public 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      * Copies the event.
     72      *
     73      * @return A deep copy of the event.
     74      * @hide
     75      */
     76     public abstract InputEvent copy();
     77 
     78     /**
     79      * 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 
     87     /**
     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 
    109     /**
    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 
    116     public int describeContents() {
    117         return 0;
    118     }
    119 
    120     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     };
    137 }
    138