Initial checkin of spot presentation for touchpad gestures.

Added a new PointerIcon API (hidden for now) for loading
pointer icons.

Fixed a starvation problem in the native Looper's sendMessage
implementation which caused new messages to be posted ahead
of old messages sent with sendMessageDelayed.

Redesigned the touch pad gestures to be defined in terms of
more fluid finger / spot movements.  The objective is to reinforce
the natural mapping between fingers and spots which means there
must not be any discontinuities in spot motion relative to
the fingers.

Removed the SpotController stub and folded its responsibilities
into PointerController.

Change-Id: I5126b1e69d95252fda7f2a684c9287e239a57163
diff --git a/services/input/PointerController.h b/services/input/PointerController.h
index d467a5a..afd6371 100644
--- a/services/input/PointerController.h
+++ b/services/input/PointerController.h
@@ -30,7 +30,10 @@
 namespace android {
 
 /**
- * Interface for tracking a single (mouse) pointer.
+ * Interface for tracking a mouse / touch pad pointer and touch pad spots.
+ *
+ * The spots are sprites on screen that visually represent the positions of
+ * fingers
  *
  * The pointer controller is responsible for providing synchronization and for tracking
  * display orientation changes if needed.
@@ -64,8 +67,95 @@
     /* Fades the pointer out now. */
     virtual void fade() = 0;
 
-    /* Makes the pointer visible if it has faded out. */
+    /* Makes the pointer visible if it has faded out.
+     * The pointer never unfades itself automatically.  This method must be called
+     * by the client whenever the pointer is moved or a button is pressed and it
+     * wants to ensure that the pointer becomes visible again. */
     virtual void unfade() = 0;
+
+    enum Presentation {
+        // Show the mouse pointer.
+        PRESENTATION_POINTER,
+        // Show spots and a spot anchor in place of the mouse pointer.
+        PRESENTATION_SPOT,
+    };
+
+    /* Sets the mode of the pointer controller. */
+    virtual void setPresentation(Presentation presentation) = 0;
+
+    // Describes the current gesture.
+    enum SpotGesture {
+        // No gesture.
+        // Do not display any spots.
+        SPOT_GESTURE_NEUTRAL,
+        // Tap at current location.
+        // Briefly display one spot at the tapped location.
+        SPOT_GESTURE_TAP,
+        // Button pressed but no finger is down.
+        // Display spot at pressed location.
+        SPOT_GESTURE_BUTTON_CLICK,
+        // Button pressed and a finger is down.
+        // Display spot at pressed location.
+        SPOT_GESTURE_BUTTON_DRAG,
+        // One finger down and hovering.
+        // Display spot at the hovered location.
+        SPOT_GESTURE_HOVER,
+        // Two fingers down but not sure in which direction they are moving so we consider
+        // it a press at the pointer location.
+        // Display two spots near the pointer location.
+        SPOT_GESTURE_PRESS,
+        // Two fingers down and moving in same direction.
+        // Display two spots near the pointer location.
+        SPOT_GESTURE_SWIPE,
+        // Two or more fingers down and moving in arbitrary directions.
+        // Display two or more spots near the pointer location, one for each finger.
+        SPOT_GESTURE_FREEFORM,
+    };
+
+    /* Sets the spots for the current gesture.
+     * The spots are not subject to the inactivity timeout like the pointer
+     * itself it since they are expected to remain visible for so long as
+     * the fingers are on the touch pad.
+     *
+     * The values of the AMOTION_EVENT_AXIS_PRESSURE axis is significant.
+     * For spotCoords, pressure != 0 indicates that the spot's location is being
+     * pressed (not hovering).
+     */
+    virtual void setSpots(SpotGesture spotGesture,
+            const PointerCoords* spotCoords, const uint32_t* spotIdToIndex,
+            BitSet32 spotIdBits) = 0;
+
+    /* Removes all spots. */
+    virtual void clearSpots() = 0;
+};
+
+
+/*
+ * Pointer resources.
+ */
+struct PointerResources {
+    SpriteIcon spotHover;
+    SpriteIcon spotTouch;
+    SpriteIcon spotAnchor;
+};
+
+
+/*
+ * Pointer controller policy interface.
+ *
+ * The pointer controller policy is used by the pointer controller to interact with
+ * the Window Manager and other system components.
+ *
+ * The actual implementation is partially supported by callbacks into the DVM
+ * via JNI.  This interface is also mocked in the unit tests.
+ */
+class PointerControllerPolicyInterface : public virtual RefBase {
+protected:
+    PointerControllerPolicyInterface() { }
+    virtual ~PointerControllerPolicyInterface() { }
+
+public:
+    virtual void loadPointerResources(PointerResources* outResources) = 0;
 };
 
 
@@ -79,12 +169,13 @@
     virtual ~PointerController();
 
 public:
-    enum InactivityFadeDelay {
-        INACTIVITY_FADE_DELAY_NORMAL = 0,
-        INACTIVITY_FADE_DELAY_SHORT = 1,
+    enum InactivityTimeout {
+        INACTIVITY_TIMEOUT_NORMAL = 0,
+        INACTIVITY_TIMEOUT_SHORT = 1,
     };
 
-    PointerController(const sp<Looper>& looper, const sp<SpriteController>& spriteController);
+    PointerController(const sp<PointerControllerPolicyInterface>& policy,
+            const sp<Looper>& looper, const sp<SpriteController>& spriteController);
 
     virtual bool getBounds(float* outMinX, float* outMinY,
             float* outMaxX, float* outMaxY) const;
@@ -96,51 +187,101 @@
     virtual void fade();
     virtual void unfade();
 
+    virtual void setPresentation(Presentation presentation);
+    virtual void setSpots(SpotGesture spotGesture,
+            const PointerCoords* spotCoords, const uint32_t* spotIdToIndex, BitSet32 spotIdBits);
+    virtual void clearSpots();
+
     void setDisplaySize(int32_t width, int32_t height);
     void setDisplayOrientation(int32_t orientation);
-    void setPointerIcon(const SkBitmap* bitmap, float hotSpotX, float hotSpotY);
-    void setInactivityFadeDelay(InactivityFadeDelay inactivityFadeDelay);
+    void setPointerIcon(const SpriteIcon& icon);
+    void setInactivityTimeout(InactivityTimeout inactivityTimeout);
 
 private:
+    static const size_t MAX_RECYCLED_SPRITES = 12;
+    static const size_t MAX_SPOTS = 12;
+
     enum {
-        MSG_FADE_STEP = 0,
+        MSG_ANIMATE,
+        MSG_INACTIVITY_TIMEOUT,
+    };
+
+    struct Spot {
+        static const uint32_t INVALID_ID = 0xffffffff;
+
+        uint32_t id;
+        sp<Sprite> sprite;
+        float alpha;
+        float scale;
+        float x, y;
+
+        inline Spot(uint32_t id, const sp<Sprite>& sprite)
+                : id(id), sprite(sprite), alpha(1.0f), scale(1.0f),
+                  x(0.0f), y(0.0f), lastIcon(NULL) { }
+
+        void updateSprite(const SpriteIcon* icon, float x, float y);
+
+    private:
+        const SpriteIcon* lastIcon;
     };
 
     mutable Mutex mLock;
 
+    sp<PointerControllerPolicyInterface> mPolicy;
     sp<Looper> mLooper;
     sp<SpriteController> mSpriteController;
     sp<WeakMessageHandler> mHandler;
 
+    PointerResources mResources;
+
     struct Locked {
+        bool animationPending;
+        nsecs_t animationTime;
+
         int32_t displayWidth;
         int32_t displayHeight;
         int32_t displayOrientation;
 
+        InactivityTimeout inactivityTimeout;
+
+        Presentation presentation;
+        bool presentationChanged;
+
+        bool pointerIsFading;
         float pointerX;
         float pointerY;
+        float pointerAlpha;
+        sp<Sprite> pointerSprite;
+        SpriteIcon pointerIcon;
+        bool pointerIconChanged;
+
         uint32_t buttonState;
 
-        float fadeAlpha;
-        InactivityFadeDelay inactivityFadeDelay;
-
-        bool visible;
-
-        sp<Sprite> sprite;
+        Vector<Spot*> spots;
+        Vector<sp<Sprite> > recycledSprites;
     } mLocked;
 
     bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
     void setPositionLocked(float x, float y);
-    void updateLocked();
 
     void handleMessage(const Message& message);
-    bool unfadeBeforeUpdateLocked();
-    void startFadeLocked();
-    void startInactivityFadeDelayLocked();
-    void fadeStepLocked();
-    bool isFadingLocked();
-    nsecs_t getInactivityFadeDelayTimeLocked();
-    void sendFadeStepMessageDelayedLocked(nsecs_t delayTime);
+    void doAnimate();
+    void doInactivityTimeout();
+
+    void startAnimationLocked();
+
+    void resetInactivityTimeoutLocked();
+    void sendImmediateInactivityTimeoutLocked();
+    void updatePointerLocked();
+
+    Spot* getSpotLocked(uint32_t id);
+    Spot* createAndAddSpotLocked(uint32_t id);
+    Spot* removeFirstFadingSpotLocked();
+    void releaseSpotLocked(Spot* spot);
+    void fadeOutAndReleaseSpotLocked(Spot* spot);
+    void fadeOutAndReleaseAllSpotsLocked();
+
+    void loadResources();
 };
 
 } // namespace android