Postpone onDisplayCompletions until onStartInputView has been executed

Bug: 5108267
Change-Id: I42d297ce4e4913e5dab5f5d647b1d13dc860b884
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index ddda184..32649d5 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -241,7 +241,8 @@
         private static final int MSG_SET_BIGRAM_PREDICTIONS = 7;
         private static final int MSG_START_ORIENTATION_CHANGE = 8;
         private static final int MSG_START_INPUT_VIEW = 9;
-        private static final int MSG_RESTORE_KEYBOARD_LAYOUT = 10;
+        private static final int MSG_DISPLAY_COMPLETIONS = 10;
+        private static final int MSG_RESTORE_KEYBOARD_LAYOUT = 11;
 
         public UIHandler(LatinIME outerInstance) {
             super(outerInstance);
@@ -293,6 +294,9 @@
             case MSG_START_INPUT_VIEW:
                 latinIme.onStartInputView((EditorInfo)msg.obj, false);
                 break;
+            case MSG_DISPLAY_COMPLETIONS:
+                latinIme.onDisplayCompletions((CompletionInfo[])msg.obj);
+                break;
             case MSG_RESTORE_KEYBOARD_LAYOUT:
                 removeMessages(MSG_UPDATE_SHIFT_STATE);
                 ((KeyboardLayoutState)msg.obj).restore();
@@ -417,6 +421,18 @@
             }
             return false;
         }
+
+        public boolean postDisplayCompletions(CompletionInfo[] applicationSpecifiedCompletions) {
+            if (hasMessages(MSG_START_INPUT_VIEW) || hasMessages(MSG_DISPLAY_COMPLETIONS)) {
+                removeMessages(MSG_DISPLAY_COMPLETIONS);
+                // Postpone onDisplayCompletions by ACCUMULATE_START_INPUT_VIEW_DELAY.
+                sendMessageDelayed(
+                        obtainMessage(MSG_DISPLAY_COMPLETIONS, applicationSpecifiedCompletions),
+                        ACCUMULATE_START_INPUT_VIEW_DELAY);
+                return true;
+            }
+            return false;
+        }
     }
 
     @Override
@@ -923,6 +939,9 @@
 
     @Override
     public void onDisplayCompletions(CompletionInfo[] applicationSpecifiedCompletions) {
+        if (mHandler.postDisplayCompletions(applicationSpecifiedCompletions)) {
+            return;
+        }
         if (DEBUG) {
             Log.i(TAG, "Received completions:");
             if (applicationSpecifiedCompletions != null) {