Merge "Extend space LED for autocompletion" into gingerbread
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
index ba901ed..c449b36 100644
--- a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
+++ b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
@@ -965,12 +965,64 @@
             return false;
         boolean result = onLongPress(popupKey);
         if (result) {
-            tracker.setAlreadyProcessed();
             dismissKeyPreview();
+            tracker.setAlreadyProcessed();
         }
         return result;
     }
 
+    private View inflateMiniKeyboardContainer(Key popupKey) {
+        int popupKeyboardId = popupKey.popupResId;
+        LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(
+                Context.LAYOUT_INFLATER_SERVICE);
+        View container = inflater.inflate(mPopupLayout, null);
+        if (container == null)
+            throw new NullPointerException();
+
+        mMiniKeyboard = (LatinKeyboardBaseView)container.findViewById(R.id.LatinKeyboardBaseView);
+        mMiniKeyboard.setOnKeyboardActionListener(new OnKeyboardActionListener() {
+            public void onKey(int primaryCode, int[] keyCodes, int x, int y) {
+                mKeyboardActionListener.onKey(primaryCode, keyCodes, x, y);
+                dismissPopupKeyboard();
+            }
+
+            public void onText(CharSequence text) {
+                mKeyboardActionListener.onText(text);
+                dismissPopupKeyboard();
+            }
+
+            public void swipeLeft() {
+            }
+            public void swipeRight() {
+            }
+            public void swipeUp() {
+            }
+            public void swipeDown() {
+            }
+            public void onPress(int primaryCode) {
+                mKeyboardActionListener.onPress(primaryCode);
+            }
+            public void onRelease(int primaryCode) {
+                mKeyboardActionListener.onRelease(primaryCode);
+            }
+        });
+
+        Keyboard keyboard;
+        if (popupKey.popupCharacters != null) {
+            keyboard = new Keyboard(getContext(), popupKeyboardId, popupKey.popupCharacters,
+                    -1, getPaddingLeft() + getPaddingRight());
+        } else {
+            keyboard = new Keyboard(getContext(), popupKeyboardId);
+        }
+        mMiniKeyboard.setKeyboard(keyboard);
+        mMiniKeyboard.setPopupParent(this);
+
+        container.measure(MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
+                MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
+
+        return container;
+    }
+
     /**
      * Called when a key is long pressed. By default this will open any popup keyboard associated
      * with this key through the attributes popupLayout and popupCharacters.
@@ -979,91 +1031,50 @@
      * method on the base class if the subclass doesn't wish to handle the call.
      */
     protected boolean onLongPress(Key popupKey) {
-        int popupKeyboardId = popupKey.popupResId;
+        // TODO if popupKey.popupCharacters has only one letter, send it as key without opening
+        // mini keyboard.
 
-        if (popupKeyboardId != 0) {
-            View container = mMiniKeyboardCache.get(popupKey);
-            if (container == null) {
-                LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
-                        Context.LAYOUT_INFLATER_SERVICE);
-                container = inflater.inflate(mPopupLayout, null);
-                mMiniKeyboard = (LatinKeyboardBaseView) container.findViewById(
-                       R.id.LatinKeyboardBaseView);
-                mMiniKeyboard.setOnKeyboardActionListener(new OnKeyboardActionListener() {
-                    public void onKey(int primaryCode, int[] keyCodes, int x, int y) {
-                        mKeyboardActionListener.onKey(primaryCode, keyCodes, x, y);
-                        dismissPopupKeyboard();
-                    }
+        if (popupKey.popupResId == 0)
+            return false;
 
-                    public void onText(CharSequence text) {
-                        mKeyboardActionListener.onText(text);
-                        dismissPopupKeyboard();
-                    }
-
-                    public void swipeLeft() { }
-                    public void swipeRight() { }
-                    public void swipeUp() { }
-                    public void swipeDown() { }
-                    public void onPress(int primaryCode) {
-                        mKeyboardActionListener.onPress(primaryCode);
-                    }
-                    public void onRelease(int primaryCode) {
-                        mKeyboardActionListener.onRelease(primaryCode);
-                    }
-                });
-
-                Keyboard keyboard;
-                if (popupKey.popupCharacters != null) {
-                    keyboard = new Keyboard(getContext(), popupKeyboardId,
-                            popupKey.popupCharacters, -1, getPaddingLeft() + getPaddingRight());
-                } else {
-                    keyboard = new Keyboard(getContext(), popupKeyboardId);
-                }
-                mMiniKeyboard.setKeyboard(keyboard);
-                mMiniKeyboard.setPopupParent(this);
-                container.measure(
-                        MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
-                        MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
-
-                mMiniKeyboardCache.put(popupKey, container);
-            } else {
-                mMiniKeyboard = (LatinKeyboardBaseView) container.findViewById(
-                        R.id.LatinKeyboardBaseView);
-            }
-            if (mWindowOffset == null) {
-                mWindowOffset = new int[2];
-                getLocationInWindow(mWindowOffset);
-            }
-            int popupX = popupKey.x + popupKey.width + getPaddingLeft();
-            int popupY = popupKey.y + getPaddingTop();
-            popupX -= container.getMeasuredWidth();
-            popupY -= container.getMeasuredHeight();
-            popupX += mWindowOffset[0];
-            popupY += mWindowOffset[1];
-            final int x = popupX + container.getPaddingRight();
-            final int y = popupY + container.getPaddingBottom();
-            mMiniKeyboardOriginX = (x < 0 ? 0 : x) + container.getPaddingLeft();
-            mMiniKeyboardOriginY = y + container.getPaddingTop();
-            mMiniKeyboard.setPopupOffset((x < 0) ? 0 : x, y);
-            mMiniKeyboard.setShifted(isShifted());
-            mMiniKeyboard.setPreviewEnabled(isPreviewEnabled());
-            mMiniKeyboardPopup.setContentView(container);
-            mMiniKeyboardPopup.setWidth(container.getMeasuredWidth());
-            mMiniKeyboardPopup.setHeight(container.getMeasuredHeight());
-            mMiniKeyboardPopup.showAtLocation(this, Gravity.NO_GRAVITY, x, y);
-
-            // Inject down event on the key to mini keyboard.
-            long eventTime = System.currentTimeMillis();
-            mMiniKeyboardPopupTime = eventTime;
-            MotionEvent downEvent = generateMiniKeyboardMotionEvent(MotionEvent.ACTION_DOWN,
-                    popupKey.x + popupKey.width / 2, popupKey.y + popupKey.height / 2, eventTime);
-            mMiniKeyboard.onTouchEvent(downEvent);
-            downEvent.recycle();
-
-            invalidateAllKeys();
-            return true;
+        View container = mMiniKeyboardCache.get(popupKey);
+        if (container == null) {
+            container = inflateMiniKeyboardContainer(popupKey);
+            mMiniKeyboardCache.put(popupKey, container);
         }
-        return false;
+        mMiniKeyboard = (LatinKeyboardBaseView)container.findViewById(R.id.LatinKeyboardBaseView);
+        if (mWindowOffset == null) {
+            mWindowOffset = new int[2];
+            getLocationInWindow(mWindowOffset);
+        }
+        int popupX = popupKey.x + popupKey.width + getPaddingLeft();
+        int popupY = popupKey.y + getPaddingTop();
+        popupX -= container.getMeasuredWidth();
+        popupY -= container.getMeasuredHeight();
+        popupX += mWindowOffset[0];
+        popupY += mWindowOffset[1];
+        final int x = popupX + container.getPaddingRight();
+        final int y = popupY + container.getPaddingBottom();
+        mMiniKeyboardOriginX = (x < 0 ? 0 : x) + container.getPaddingLeft();
+        mMiniKeyboardOriginY = y + container.getPaddingTop();
+        mMiniKeyboard.setPopupOffset((x < 0) ? 0 : x, y);
+        mMiniKeyboard.setShifted(isShifted());
+        mMiniKeyboard.setPreviewEnabled(isPreviewEnabled());
+        mMiniKeyboardPopup.setContentView(container);
+        mMiniKeyboardPopup.setWidth(container.getMeasuredWidth());
+        mMiniKeyboardPopup.setHeight(container.getMeasuredHeight());
+        mMiniKeyboardPopup.showAtLocation(this, Gravity.NO_GRAVITY, x, y);
+
+        // Inject down event on the key to mini keyboard.
+        long eventTime = System.currentTimeMillis();
+        mMiniKeyboardPopupTime = eventTime;
+        MotionEvent downEvent = generateMiniKeyboardMotionEvent(MotionEvent.ACTION_DOWN, popupKey.x
+                + popupKey.width / 2, popupKey.y + popupKey.height / 2, eventTime);
+        mMiniKeyboard.onTouchEvent(downEvent);
+        downEvent.recycle();
+
+        invalidateAllKeys();
+        return true;
     }
 
     private MotionEvent generateMiniKeyboardMotionEvent(int action, int x, int y, long eventTime) {
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardView.java
index 9236d68..71ca8b8 100644
--- a/java/src/com/android/inputmethod/latin/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/latin/LatinKeyboardView.java
@@ -42,7 +42,7 @@
 
     /** Whether we've started dropping move events because we found a big jump */
     private boolean mDroppingEvents;
-    /** 
+    /**
      * Whether multi-touch disambiguation needs to be disabled if a real multi-touch event has
      * occured
      */
@@ -52,6 +52,9 @@
     /** The y coordinate of the last row */
     private int mLastRowY;
 
+    // This is local working variable for onLongPress().
+    private int[] mKeyCodes = new int[1];
+
     public LatinKeyboardView(Context context, AttributeSet attrs) {
         super(context, attrs);
     }
@@ -75,17 +78,37 @@
         setKeyboardLocal(k);
     }
 
+    private static boolean hasOneDigitAlternate(Key key) {
+        final CharSequence alternates = key.popupCharacters;
+        if (alternates == null)
+            return false;
+        final String altChars = alternates.toString();
+        if (altChars.codePointCount(0, altChars.length()) != 1)
+            return false;
+        final int altCode = altChars.codePointAt(0);
+        return altCode >= '0' && altCode <= '9';
+    }
+
     @Override
     protected boolean onLongPress(Key key) {
-        if (key.codes[0] == KEYCODE_OPTIONS) {
+        int primaryCode = key.codes[0];
+        if (primaryCode == KEYCODE_OPTIONS) {
             getOnKeyboardActionListener().onKey(KEYCODE_OPTIONS_LONGPRESS, null,
                     LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
                     LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
             return true;
-        } else if (key.codes[0] == '0' && getKeyboard() == mPhoneKeyboard) {
+        } else if (primaryCode == '0' && getKeyboard() == mPhoneKeyboard) {
             // Long pressing on 0 in phone number keypad gives you a '+'.
             getOnKeyboardActionListener().onKey(
-                    '+', null, LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
+                    '+', null,
+                    LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
+                    LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
+            return true;
+        } else if (hasOneDigitAlternate(key)) {
+            mKeyCodes[0] = primaryCode = key.popupCharacters.charAt(0);
+            // when there is only one alternate character, send it as key action.
+            getOnKeyboardActionListener().onKey(primaryCode, mKeyCodes,
+                    LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE,
                     LatinKeyboardBaseView.NOT_A_TOUCH_COORDINATE);
             return true;
         } else {
@@ -121,10 +144,10 @@
      * that could be due to a multi-touch being treated as a move by the firmware or hardware.
      * Once a sudden jump is detected, all subsequent move events are discarded
      * until an UP is received.<P>
-     * When a sudden jump is detected, an UP event is simulated at the last position and when 
+     * When a sudden jump is detected, an UP event is simulated at the last position and when
      * the sudden moves subside, a DOWN event is simulated for the second key.
      * @param me the motion event
-     * @return true if the event was consumed, so that it doesn't continue to be handled by 
+     * @return true if the event was consumed, so that it doesn't continue to be handled by
      * KeyboardView.
      */
     private boolean handleSuddenJump(MotionEvent me) {
@@ -232,9 +255,9 @@
     static final boolean DEBUG_LINE = false;
     private static final int MSG_TOUCH_DOWN = 1;
     private static final int MSG_TOUCH_UP = 2;
-    
+
     Handler mHandler2;
-    
+
     private String mStringToPlay;
     private int mStringIndex;
     private boolean mDownDelivered;
@@ -254,7 +277,7 @@
                         removeMessages(MSG_TOUCH_DOWN);
                         removeMessages(MSG_TOUCH_UP);
                         if (mPlaying == false) return;
-                        
+
                         switch (msg.what) {
                             case MSG_TOUCH_DOWN:
                                 if (mStringIndex >= mStringToPlay.length()) {
@@ -262,7 +285,7 @@
                                     return;
                                 }
                                 char c = mStringToPlay.charAt(mStringIndex);
-                                while (c > 255 || mAsciiKeys[(int) c] == null) {
+                                while (c > 255 || mAsciiKeys[c] == null) {
                                     mStringIndex++;
                                     if (mStringIndex >= mStringToPlay.length()) {
                                         mPlaying = false;
@@ -272,8 +295,8 @@
                                 }
                                 int x = mAsciiKeys[c].x + 10;
                                 int y = mAsciiKeys[c].y + 26;
-                                MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(), 
-                                        SystemClock.uptimeMillis(), 
+                                MotionEvent me = MotionEvent.obtain(SystemClock.uptimeMillis(),
+                                        SystemClock.uptimeMillis(),
                                         MotionEvent.ACTION_DOWN, x, y, 0);
                                 LatinKeyboardView.this.dispatchTouchEvent(me);
                                 me.recycle();
@@ -286,9 +309,9 @@
                                 int x2 = mAsciiKeys[cUp].x + 10;
                                 int y2 = mAsciiKeys[cUp].y + 26;
                                 mStringIndex++;
-                                
-                                MotionEvent me2 = MotionEvent.obtain(SystemClock.uptimeMillis(), 
-                                        SystemClock.uptimeMillis(), 
+
+                                MotionEvent me2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
+                                        SystemClock.uptimeMillis(),
                                         MotionEvent.ACTION_UP, x2, y2, 0);
                                 LatinKeyboardView.this.dispatchTouchEvent(me2);
                                 me2.recycle();
@@ -309,7 +332,7 @@
         // Get the keys on this keyboard
         for (int i = 0; i < keys.size(); i++) {
             int code = keys.get(i).codes[0];
-            if (code >= 0 && code <= 255) { 
+            if (code >= 0 && code <= 255) {
                 mAsciiKeys[code] = keys.get(i);
             }
         }
diff --git a/java/src/com/android/inputmethod/latin/PointerTracker.java b/java/src/com/android/inputmethod/latin/PointerTracker.java
index a4447d5..2685e87 100644
--- a/java/src/com/android/inputmethod/latin/PointerTracker.java
+++ b/java/src/com/android/inputmethod/latin/PointerTracker.java
@@ -124,6 +124,8 @@
     }
 
     public void updateKey(int keyIndex) {
+        if (mKeyAlreadyProcessed)
+            return;
         int oldKeyIndex = mPreviousKey;
         mPreviousKey = keyIndex;
         if (keyIndex != oldKeyIndex) {
@@ -172,6 +174,8 @@
     }
 
     public void onMoveEvent(int x, int y, long eventTime) {
+        if (mKeyAlreadyProcessed)
+            return;
         int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
         if (isValidKeyIndex(keyIndex)) {
             if (mCurrentKey == NOT_A_KEY) {
@@ -215,6 +219,8 @@
     }
 
     public void onUpEvent(int x, int y, long eventTime) {
+        if (mKeyAlreadyProcessed)
+            return;
         if (DEBUG)
             debugLog("onUpEvent  :", x, y);
         int keyIndex = mKeyDetector.getKeyIndexAndNearbyCodes(x, y, null);
@@ -235,7 +241,7 @@
         }
         showKeyPreviewAndUpdateKey(NOT_A_KEY);
         // If we're not on a repeating key (which sends on a DOWN event)
-        if (!wasInKeyRepeat && !mKeyAlreadyProcessed) {
+        if (!wasInKeyRepeat) {
             detectAndSendKey(mCurrentKey, (int)x, (int)y, eventTime);
         }
         if (isValidKeyIndex(keyIndex))