Revert "Show key preview instantaneously"

This reverts commit 09f530e248a3caf0037ca89aa79a836385a03fab.

Change-Id: I3106cd22cc1eac750f05bce26df6af3026dfaf86
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
index e3bad80..4007c2b 100644
--- a/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
+++ b/java/src/com/android/inputmethod/latin/LatinKeyboardBaseView.java
@@ -178,6 +178,7 @@
     /** Listener for {@link OnKeyboardActionListener}. */
     private OnKeyboardActionListener mKeyboardActionListener;
 
+    private static final int DELAY_BEFORE_PREVIEW = 0;
     private static final int DELAY_AFTER_PREVIEW = 70;
     private static final int DEBOUNCE_TIME = 70;
 
@@ -244,15 +245,19 @@
     UIHandler mHandler = new UIHandler();
 
     class UIHandler extends Handler {
-        private static final int MSG_DISMISS_PREVIEW = 0;
-        private static final int MSG_REPEAT_KEY = 1;
-        private static final int MSG_LOGPRESS_KEY = 2;
+        private static final int MSG_POPUP_PREVIEW = 1;
+        private static final int MSG_DISMISS_PREVIEW = 2;
+        private static final int MSG_REPEAT_KEY = 3;
+        private static final int MSG_LOGPRESS_KEY = 4;
 
         private boolean mInKeyRepeat;
 
         @Override
         public void handleMessage(Message msg) {
             switch (msg.what) {
+                case MSG_POPUP_PREVIEW:
+                    showKey(msg.arg1);
+                    break;
                 case MSG_DISMISS_PREVIEW:
                     mPreviewText.setVisibility(INVISIBLE);
                     break;
@@ -266,6 +271,15 @@
             }
         }
 
+        public void popupPreview(int keyIndex, long delay) {
+            removeMessages(MSG_POPUP_PREVIEW);
+            sendMessageDelayed(obtainMessage(MSG_POPUP_PREVIEW, keyIndex, 0), delay);
+        }
+
+        public void cancelPopupPreview() {
+            removeMessages(MSG_POPUP_PREVIEW);
+        }
+
         public void dismissPreview(long delay) {
             sendMessageDelayed(obtainMessage(MSG_DISMISS_PREVIEW), delay);
         }
@@ -304,6 +318,7 @@
 
         public void cancelAllMessages() {
             cancelKeyTimers();
+            cancelPopupPreview();
             cancelDismissPreview();
         }
     };
@@ -598,6 +613,7 @@
         }
         // Remove any pending messages, except dismissing preview
         mHandler.cancelKeyTimers();
+        mHandler.cancelPopupPreview();
         mKeyboard = keyboard;
         LatinImeLogger.onSetKeyboard(mKeyboard);
         List<Key> keys = mKeyboard.getKeys();
@@ -969,11 +985,17 @@
         // If key changed and preview is on ...
         if (oldKeyIndex != mCurrentKeyIndex && mShowPreview) {
             if (keyIndex == NOT_A_KEY) {
+                mHandler.cancelPopupPreview();
                 if (previewPopup.isShowing()) {
                     mHandler.dismissPreview(DELAY_AFTER_PREVIEW);
                 }
             } else {
-                showKey(keyIndex);
+                if (previewPopup.isShowing() && mPreviewText.getVisibility() == VISIBLE) {
+                    // Show right away, if it's already visible and finger is moving around
+                    showKey(keyIndex);
+                } else {
+                    mHandler.popupPreview(keyIndex, DELAY_BEFORE_PREVIEW);
+                }
             }
         }
     }
@@ -1350,6 +1372,7 @@
         int keyIndex = mProximityKeyDetector.getKeyIndexAndNearbyCodes(touchX, touchY, null);
         boolean wasInKeyRepeat = mHandler.isInKeyRepeat();
         mHandler.cancelKeyTimers();
+        mHandler.cancelPopupPreview();
         if (mDebouncer.isMinorMoveBounce(touchX, touchY, keyIndex, mCurrentKey)) {
             mDebouncer.updateTimeDebouncing(eventTime);
         } else {
@@ -1372,6 +1395,7 @@
 
     private void onCancelEvent(int touchX, int touchY, long eventTime) {
         mHandler.cancelKeyTimers();
+        mHandler.cancelPopupPreview();
         dismissPopupKeyboard();
         mAbortKey = true;
         showPreview(NOT_A_KEY);