Add support for new input sources.

Added several new coordinate values to MotionEvents to capture
touch major/minor area, tool major/minor area and orientation.

Renamed NDK input constants per convention.

Added InputDevice class in Java which will eventually provide
useful information about available input devices.

Added APIs for manufacturing new MotionEvent objects with multiple
pointers and all necessary coordinate data.

Fixed a bug in the input dispatcher where it could get stuck with
a pointer down forever.

Fixed a bug in the WindowManager where the input window list could
end up containing stale removed windows.

Fixed a bug in the WindowManager where the input channel was being
removed only after the final animation transition had taken place
which caused spurious WINDOW DIED log messages to be printed.

Change-Id: Ie55084da319b20aad29b28a0499b8dd98bb5da68
diff --git a/native/include/android/input.h b/native/include/android/input.h
index 25dd68e..ce79cd4 100644
--- a/native/include/android/input.h
+++ b/native/include/android/input.h
@@ -49,49 +49,21 @@
 #endif
 
 /*
- * Input device classes.
- */
-enum {
-    /* The input device is a keyboard. */
-    INPUT_DEVICE_CLASS_KEYBOARD      = 0x00000001,
-
-    /* The input device is an alpha-numeric keyboard (not just a dial pad). */
-    INPUT_DEVICE_CLASS_ALPHAKEY      = 0x00000002,
-
-    /* The input device is a touchscreen (either single-touch or multi-touch). */
-    INPUT_DEVICE_CLASS_TOUCHSCREEN   = 0x00000004,
-
-    /* The input device is a trackball. */
-    INPUT_DEVICE_CLASS_TRACKBALL     = 0x00000008,
-
-    /* The input device is a multi-touch touchscreen. */
-    INPUT_DEVICE_CLASS_TOUCHSCREEN_MT= 0x00000010,
-
-    /* The input device is a directional pad. */
-    INPUT_DEVICE_CLASS_DPAD          = 0x00000020,
-
-    /* The input device is a gamepad (implies keyboard). */
-    INPUT_DEVICE_CLASS_GAMEPAD       = 0x00000040
-};
-
-/*
  * Key states (may be returned by queries about the current state of a
  * particular key code, scan code or switch).
- *
- * XXX should we call this BUTTON_STATE_XXX?
  */
 enum {
     /* The key state is unknown or the requested key itself is not supported. */
-    KEY_STATE_UNKNOWN = -1,
+    AKEY_STATE_UNKNOWN = -1,
 
     /* The key is up. */
-    KEY_STATE_UP = 0,
+    AKEY_STATE_UP = 0,
 
     /* The key is down. */
-    KEY_STATE_DOWN = 1,
+    AKEY_STATE_DOWN = 1,
 
     /* The key is down but is a virtual key press that is being emulated by the system. */
-    KEY_STATE_VIRTUAL = 2
+    AKEY_STATE_VIRTUAL = 2
 };
 
 /*
@@ -99,28 +71,28 @@
  */
 enum {
     /* No meta keys are pressed. */
-    META_NONE = 0,
+    AMETA_NONE = 0,
 
     /* This mask is used to check whether one of the ALT meta keys is pressed. */
-    META_ALT_ON = 0x02,
+    AMETA_ALT_ON = 0x02,
 
     /* This mask is used to check whether the left ALT meta key is pressed. */
-    META_ALT_LEFT_ON = 0x10,
+    AMETA_ALT_LEFT_ON = 0x10,
 
     /* This mask is used to check whether the right ALT meta key is pressed. */
-    META_ALT_RIGHT_ON = 0x20,
+    AMETA_ALT_RIGHT_ON = 0x20,
 
     /* This mask is used to check whether one of the SHIFT meta keys is pressed. */
-    META_SHIFT_ON = 0x01,
+    AMETA_SHIFT_ON = 0x01,
 
     /* This mask is used to check whether the left SHIFT meta key is pressed. */
-    META_SHIFT_LEFT_ON = 0x40,
+    AMETA_SHIFT_LEFT_ON = 0x40,
 
     /* This mask is used to check whether the right SHIFT meta key is pressed. */
-    META_SHIFT_RIGHT_ON = 0x80,
+    AMETA_SHIFT_RIGHT_ON = 0x80,
 
     /* This mask is used to check whether the SYM meta key is pressed. */
-    META_SYM_ON = 0x04
+    AMETA_SYM_ON = 0x04
 };
 
 /*
@@ -137,10 +109,10 @@
  */
 enum {
     /* Indicates that the input event is a key event. */
-    INPUT_EVENT_TYPE_KEY = 1,
+    AINPUT_EVENT_TYPE_KEY = 1,
 
     /* Indicates that the input event is a motion event. */
-    INPUT_EVENT_TYPE_MOTION = 2
+    AINPUT_EVENT_TYPE_MOTION = 2
 };
 
 /*
@@ -148,16 +120,16 @@
  */
 enum {
     /* The key has been pressed down. */
-    KEY_EVENT_ACTION_DOWN = 0,
+    AKEY_EVENT_ACTION_DOWN = 0,
 
     /* The key has been released. */
-    KEY_EVENT_ACTION_UP = 1,
+    AKEY_EVENT_ACTION_UP = 1,
 
     /* Multiple duplicate key events have occurred in a row, or a complex string is
      * being delivered.  The repeat_count property of the key event contains the number
      * of times the given key code should be executed.
      */
-    KEY_EVENT_ACTION_MULTIPLE = 2
+    AKEY_EVENT_ACTION_MULTIPLE = 2
 };
 
 /*
@@ -165,25 +137,25 @@
  */
 enum {
     /* This mask is set if the device woke because of this key event. */
-    KEY_EVENT_FLAG_WOKE_HERE = 0x1,
+    AKEY_EVENT_FLAG_WOKE_HERE = 0x1,
 
     /* This mask is set if the key event was generated by a software keyboard. */
-    KEY_EVENT_FLAG_SOFT_KEYBOARD = 0x2,
+    AKEY_EVENT_FLAG_SOFT_KEYBOARD = 0x2,
 
     /* This mask is set if we don't want the key event to cause us to leave touch mode. */
-    KEY_EVENT_FLAG_KEEP_TOUCH_MODE = 0x4,
+    AKEY_EVENT_FLAG_KEEP_TOUCH_MODE = 0x4,
 
     /* This mask is set if an event was known to come from a trusted part
      * of the system.  That is, the event is known to come from the user,
      * and could not have been spoofed by a third party component. */
-    KEY_EVENT_FLAG_FROM_SYSTEM = 0x8,
+    AKEY_EVENT_FLAG_FROM_SYSTEM = 0x8,
 
     /* This mask is used for compatibility, to identify enter keys that are
      * coming from an IME whose enter key has been auto-labelled "next" or
      * "done".  This allows TextView to dispatch these as normal enter keys
      * for old applications, but still do the appropriate action when
      * receiving them. */
-    KEY_EVENT_FLAG_EDITOR_ACTION = 0x10,
+    AKEY_EVENT_FLAG_EDITOR_ACTION = 0x10,
 
     /* When associated with up key events, this indicates that the key press
      * has been canceled.  Typically this is used with virtual touch screen
@@ -193,26 +165,26 @@
      * key.  Note that for this to work, the application can not perform an
      * action for a key until it receives an up or the long press timeout has
      * expired. */
-    KEY_EVENT_FLAG_CANCELED = 0x20,
+    AKEY_EVENT_FLAG_CANCELED = 0x20,
 
     /* This key event was generated by a virtual (on-screen) hard key area.
      * Typically this is an area of the touchscreen, outside of the regular
      * display, dedicated to "hardware" buttons. */
-    KEY_EVENT_FLAG_VIRTUAL_HARD_KEY = 0x40,
+    AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY = 0x40,
 
     /* This flag is set for the first key repeat that occurs after the
      * long press timeout. */
-    KEY_EVENT_FLAG_LONG_PRESS = 0x80,
+    AKEY_EVENT_FLAG_LONG_PRESS = 0x80,
 
-    /* Set when a key event has KEY_EVENT_FLAG_CANCELED set because a long
+    /* Set when a key event has AKEY_EVENT_FLAG_CANCELED set because a long
      * press action was executed while it was down. */
-    KEY_EVENT_FLAG_CANCELED_LONG_PRESS = 0x100,
+    AKEY_EVENT_FLAG_CANCELED_LONG_PRESS = 0x100,
 
-    /* Set for KEY_EVENT_ACTION_UP when this event's key code is still being
+    /* Set for AKEY_EVENT_ACTION_UP when this event's key code is still being
      * tracked from its initial down.  That is, somebody requested that tracking
      * started on the key down and a long press has not caused
      * the tracking to be canceled. */
-    KEY_EVENT_FLAG_TRACKING = 0x200
+    AKEY_EVENT_FLAG_TRACKING = 0x200
 };
 
 /*
@@ -220,57 +192,57 @@
  */
 
 /* Bit shift for the action bits holding the pointer index as
- * defined by MOTION_EVENT_ACTION_POINTER_INDEX_MASK.
+ * defined by AMOTION_EVENT_ACTION_POINTER_INDEX_MASK.
  */
-#define MOTION_EVENT_ACTION_POINTER_INDEX_SHIFT 8
+#define AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT 8
 
 enum {
     /* Bit mask of the parts of the action code that are the action itself.
      */
-    MOTION_EVENT_ACTION_MASK = 0xff,
+    AMOTION_EVENT_ACTION_MASK = 0xff,
 
     /* Bits in the action code that represent a pointer index, used with
-     * MOTION_EVENT_ACTION_POINTER_DOWN and MOTION_EVENT_ACTION_POINTER_UP.  Shifting
-     * down by MOTION_EVENT_ACTION_POINTER_INDEX_SHIFT provides the actual pointer
+     * AMOTION_EVENT_ACTION_POINTER_DOWN and AMOTION_EVENT_ACTION_POINTER_UP.  Shifting
+     * down by AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT provides the actual pointer
      * index where the data for the pointer going up or down can be found.
      */
-    MOTION_EVENT_ACTION_POINTER_INDEX_MASK  = 0xff00,
+    AMOTION_EVENT_ACTION_POINTER_INDEX_MASK  = 0xff00,
 
     /* A pressed gesture has started, the motion contains the initial starting location.
      */
-    MOTION_EVENT_ACTION_DOWN = 0,
+    AMOTION_EVENT_ACTION_DOWN = 0,
 
     /* A pressed gesture has finished, the motion contains the final release location
      * as well as any intermediate points since the last down or move event.
      */
-    MOTION_EVENT_ACTION_UP = 1,
+    AMOTION_EVENT_ACTION_UP = 1,
 
-    /* A change has happened during a press gesture (between MOTION_EVENT_ACTION_DOWN and
-     * MOTION_EVENT_ACTION_UP).  The motion contains the most recent point, as well as
+    /* A change has happened during a press gesture (between AMOTION_EVENT_ACTION_DOWN and
+     * AMOTION_EVENT_ACTION_UP).  The motion contains the most recent point, as well as
      * any intermediate points since the last down or move event.
      */
-    MOTION_EVENT_ACTION_MOVE = 2,
+    AMOTION_EVENT_ACTION_MOVE = 2,
 
     /* The current gesture has been aborted.
      * You will not receive any more points in it.  You should treat this as
      * an up event, but not perform any action that you normally would.
      */
-    MOTION_EVENT_ACTION_CANCEL = 3,
+    AMOTION_EVENT_ACTION_CANCEL = 3,
 
     /* A movement has happened outside of the normal bounds of the UI element.
      * This does not provide a full gesture, but only the initial location of the movement/touch.
      */
-    MOTION_EVENT_ACTION_OUTSIDE = 4,
+    AMOTION_EVENT_ACTION_OUTSIDE = 4,
 
     /* A non-primary pointer has gone down.
-     * The bits in MOTION_EVENT_ACTION_POINTER_INDEX_MASK indicate which pointer changed.
+     * The bits in AMOTION_EVENT_ACTION_POINTER_INDEX_MASK indicate which pointer changed.
      */
-    MOTION_EVENT_ACTION_POINTER_DOWN = 5,
+    AMOTION_EVENT_ACTION_POINTER_DOWN = 5,
 
     /* A non-primary pointer has gone up.
-     * The bits in MOTION_EVENT_ACTION_POINTER_INDEX_MASK indicate which pointer changed.
+     * The bits in AMOTION_EVENT_ACTION_POINTER_INDEX_MASK indicate which pointer changed.
      */
-    MOTION_EVENT_ACTION_POINTER_UP = 6
+    AMOTION_EVENT_ACTION_POINTER_UP = 6
 };
 
 /*
@@ -278,39 +250,50 @@
  */
 enum {
     /* No edges intersected */
-    MOTION_EVENT_EDGE_FLAG_NONE = 0,
+    AMOTION_EVENT_EDGE_FLAG_NONE = 0,
 
     /* Flag indicating the motion event intersected the top edge of the screen. */
-    MOTION_EVENT_EDGE_FLAG_TOP = 0x01,
+    AMOTION_EVENT_EDGE_FLAG_TOP = 0x01,
 
     /* Flag indicating the motion event intersected the bottom edge of the screen. */
-    MOTION_EVENT_EDGE_FLAG_BOTTOM = 0x02,
+    AMOTION_EVENT_EDGE_FLAG_BOTTOM = 0x02,
 
     /* Flag indicating the motion event intersected the left edge of the screen. */
-    MOTION_EVENT_EDGE_FLAG_LEFT = 0x04,
+    AMOTION_EVENT_EDGE_FLAG_LEFT = 0x04,
 
     /* Flag indicating the motion event intersected the right edge of the screen. */
-    MOTION_EVENT_EDGE_FLAG_RIGHT = 0x08
+    AMOTION_EVENT_EDGE_FLAG_RIGHT = 0x08
 };
 
 /*
- * Specifies the logical nature of an input event.
- * For example, the nature distinguishes between motion events that represent touches and
- * those that represent trackball moves.
+ * Input sources.
  *
- * XXX This concept is tentative.  Another idea would be to associate events with logical
- *     controllers rather than physical devices.   The interpretation of an event would
- *     be made with respect to the nature of the controller that is considered the logical
- *     source of an event.  The decoupling is beneficial since multiple physical (and virtual)
- *     devices could be responsible for producing events that would be associated with
- *     various logical controllers.  For example, the hard keyboard, on screen keyboard,
- *     and peripheral keyboard could be mapped onto a single logical "keyboard" controller
- *     (or treated independently, if desired).
+ * The appropriate interpretation for an input event depends on its source.
+ * Refer to the documentation on android.view.InputDevice for more details about input sources
+ * and their correct interpretation.
  */
 enum {
-    INPUT_EVENT_NATURE_KEY = 1,
-    INPUT_EVENT_NATURE_TOUCH = 2,
-    INPUT_EVENT_NATURE_TRACKBALL = 3
+    AINPUT_SOURCE_CLASS_MASK = 0x000000ff,
+
+    AINPUT_SOURCE_CLASS_BUTTON = 0x00000001,
+    AINPUT_SOURCE_CLASS_POINTER = 0x00000002,
+    AINPUT_SOURCE_CLASS_NAVIGATION = 0x00000004,
+    AINPUT_SOURCE_CLASS_POSITION = 0x00000008,
+    AINPUT_SOURCE_CLASS_JOYSTICK = 0x00000010,
+};
+
+enum {
+    AINPUT_SOURCE_UNKNOWN = 0x00000000,
+
+    AINPUT_SOURCE_KEYBOARD = 0x00000100 | AINPUT_SOURCE_CLASS_BUTTON,
+    AINPUT_SOURCE_DPAD = 0x00000200 | AINPUT_SOURCE_CLASS_BUTTON,
+    AINPUT_SOURCE_GAMEPAD = 0x00000400 | AINPUT_SOURCE_CLASS_BUTTON,
+    AINPUT_SOURCE_TOUCHSCREEN = 0x00001000 | AINPUT_SOURCE_CLASS_POINTER,
+    AINPUT_SOURCE_MOUSE = 0x00002000 | AINPUT_SOURCE_CLASS_POINTER,
+    AINPUT_SOURCE_TRACKBALL = 0x00010000 | AINPUT_SOURCE_CLASS_NAVIGATION,
+    AINPUT_SOURCE_TOUCHPAD = 0x00100000 | AINPUT_SOURCE_CLASS_POSITION,
+    AINPUT_SOURCE_JOYSTICK_LEFT = 0x01000000 | AINPUT_SOURCE_CLASS_JOYSTICK,
+    AINPUT_SOURCE_JOYSTICK_RIGHT = 0x02000000 | AINPUT_SOURCE_CLASS_JOYSTICK,
 };
 
 /*
@@ -337,8 +320,8 @@
  */
 int32_t AInputEvent_getDeviceId(const AInputEvent* event);
 
-/* Get the input event nature. */
-int32_t AInputEvent_getNature(const AInputEvent* event);
+/* Get the input event source. */
+int32_t AInputEvent_getSource(const AInputEvent* event);
 
 /*** Accessors for key events only. ***/
 
@@ -466,11 +449,41 @@
  * determine fat touch events. */
 float AMotionEvent_getSize(const AInputEvent* motion_event, size_t pointer_index);
 
+/* Get the current length of the major axis of an ellipse that describes the touch area
+ * at the point of contact for the given pointer index. */
+float AMotionEvent_getTouchMajor(const AInputEvent* motion_event, size_t pointer_index);
+
+/* Get the current length of the minor axis of an ellipse that describes the touch area
+ * at the point of contact for the given pointer index. */
+float AMotionEvent_getTouchMinor(const AInputEvent* motion_event, size_t pointer_index);
+
+/* Get the current length of the major axis of an ellipse that describes the size
+ * of the approaching tool for the given pointer index.
+ * The tool area represents the estimated size of the finger or pen that is
+ * touching the device independent of its actual touch area at the point of contact. */
+float AMotionEvent_getToolMajor(const AInputEvent* motion_event, size_t pointer_index);
+
+/* Get the current length of the minor axis of an ellipse that describes the size
+ * of the approaching tool for the given pointer index.
+ * The tool area represents the estimated size of the finger or pen that is
+ * touching the device independent of its actual touch area at the point of contact. */
+float AMotionEvent_getToolMinor(const AInputEvent* motion_event, size_t pointer_index);
+
+/* Get the current orientation of the touch area and tool area in radians clockwise from
+ * vertical for the given pointer index.
+ * An angle of 0 degrees indicates that the major axis of contact is oriented
+ * upwards, is perfectly circular or is of unknown orientation.  A positive angle
+ * indicates that the major axis of contact is oriented to the right.  A negative angle
+ * indicates that the major axis of contact is oriented to the left.
+ * The full range is from -PI/4 radians (finger pointing fully left) to PI/4 radians
+ * (finger pointing fully right). */
+float AMotionEvent_getOrientation(const AInputEvent* motion_event, size_t pointer_index);
+
 /* Get the number of historical points in this event.  These are movements that
  * have occurred between this event and the previous event.  This only applies
- * to MOTION_EVENT_ACTION_MOVE events -- all other actions will have a size of 0.
+ * to AMOTION_EVENT_ACTION_MOVE events -- all other actions will have a size of 0.
  * Historical samples are indexed from oldest to newest. */
-size_t AMotionEvent_get_history_size(const AInputEvent* motion_event);
+size_t AMotionEvent_getHistorySize(const AInputEvent* motion_event);
 
 /* Get the time that a historical movement occurred between this event and
  * the previous event, in the java.lang.System.nanoTime() time base. */
@@ -527,6 +540,47 @@
 float AMotionEvent_getHistoricalSize(AInputEvent* motion_event, size_t pointer_index,
         size_t history_index);
 
+/* Get the historical length of the major axis of an ellipse that describes the touch area
+ * at the point of contact for the given pointer index that
+ * occurred between this event and the previous motion event. */
+float AMotionEvent_getHistoricalTouchMajor(const AInputEvent* motion_event, size_t pointer_index,
+        size_t history_index);
+
+/* Get the historical length of the minor axis of an ellipse that describes the touch area
+ * at the point of contact for the given pointer index that
+ * occurred between this event and the previous motion event. */
+float AMotionEvent_getHistoricalTouchMinor(const AInputEvent* motion_event, size_t pointer_index,
+        size_t history_index);
+
+/* Get the historical length of the major axis of an ellipse that describes the size
+ * of the approaching tool for the given pointer index that
+ * occurred between this event and the previous motion event.
+ * The tool area represents the estimated size of the finger or pen that is
+ * touching the device independent of its actual touch area at the point of contact. */
+float AMotionEvent_getHistoricalToolMajor(const AInputEvent* motion_event, size_t pointer_index,
+        size_t history_index);
+
+/* Get the historical length of the minor axis of an ellipse that describes the size
+ * of the approaching tool for the given pointer index that
+ * occurred between this event and the previous motion event.
+ * The tool area represents the estimated size of the finger or pen that is
+ * touching the device independent of its actual touch area at the point of contact. */
+float AMotionEvent_getHistoricalToolMinor(const AInputEvent* motion_event, size_t pointer_index,
+        size_t history_index);
+
+/* Get the historical orientation of the touch area and tool area in radians clockwise from
+ * vertical for the given pointer index that
+ * occurred between this event and the previous motion event.
+ * An angle of 0 degrees indicates that the major axis of contact is oriented
+ * upwards, is perfectly circular or is of unknown orientation.  A positive angle
+ * indicates that the major axis of contact is oriented to the right.  A negative angle
+ * indicates that the major axis of contact is oriented to the left.
+ * The full range is from -PI/4 radians (finger pointing fully left) to PI/4 radians
+ * (finger pointing fully right). */
+float AMotionEvent_getHistoricalOrientation(const AInputEvent* motion_event, size_t pointer_index,
+        size_t history_index);
+
+
 /*
  * Input queue
  *