Post the inline suggestion response handling to next message

* so that it can determine whether to delay the deletion or not
* also fix sample IME to clear suggestions when asked to

Test: manual
Bug: 159479887
Bug: 157515522

Change-Id: I71e6ac9f3593cdfa4e40b7664cbd2eec6343b5af
diff --git a/samples/AutofillKeyboard/src/com/example/android/autofillkeyboard/AutofillImeService.java b/samples/AutofillKeyboard/src/com/example/android/autofillkeyboard/AutofillImeService.java
index 32f765a..681601a 100644
--- a/samples/AutofillKeyboard/src/com/example/android/autofillkeyboard/AutofillImeService.java
+++ b/samples/AutofillKeyboard/src/com/example/android/autofillkeyboard/AutofillImeService.java
@@ -104,6 +104,7 @@
 
     private ResponseState mResponseState = ResponseState.RESET;
     private Runnable mDelayedDeletion;
+    private Runnable mPendingResponse;
 
     @Override
     public View onCreateInputView() {
@@ -125,7 +126,7 @@
         if(mKeyboard != null) {
             mKeyboard.reset();
         }
-        if (mResponseState == ResponseState.FINISH_INPUT) {
+        if (mResponseState == ResponseState.RECEIVE_RESPONSE) {
             mResponseState = ResponseState.START_INPUT;
         } else {
             mResponseState = ResponseState.RESET;
@@ -135,13 +136,32 @@
     @Override
     public void onFinishInput() {
         super.onFinishInput();
-        if (mResponseState == ResponseState.RECEIVE_RESPONSE) {
-            mResponseState = ResponseState.FINISH_INPUT;
-        } else {
-            mResponseState = ResponseState.RESET;
+    }
+
+    private void cancelPendingResponse() {
+        if (mPendingResponse != null) {
+            Log.d(TAG, "Canceling pending response");
+            mHandler.removeCallbacks(mPendingResponse);
+            mPendingResponse = null;
         }
     }
 
+    private void postPendingResponse(InlineSuggestionsResponse response) {
+        cancelPendingResponse();
+        final List<InlineSuggestion> inlineSuggestions = response.getInlineSuggestions();
+        mResponseState = ResponseState.RECEIVE_RESPONSE;
+        mPendingResponse = () -> {
+            mPendingResponse = null;
+            if (mResponseState == ResponseState.START_INPUT && inlineSuggestions.isEmpty()) {
+                scheduleDelayedDeletion();
+            } else {
+                inflateThenShowSuggestions(inlineSuggestions);
+            }
+            mResponseState = ResponseState.RESET;
+        };
+        mHandler.post(mPendingResponse);
+    }
+
     private void cancelDelayedDeletion(String msg) {
         if(mDelayedDeletion != null) {
             Log.d(TAG, msg + " canceling delayed deletion");
@@ -263,16 +283,7 @@
         Log.d(TAG,
                 "onInlineSuggestionsResponse() called: " + response.getInlineSuggestions().size());
         cancelDelayedDeletion("onInlineSuggestionsResponse");
-        final List<InlineSuggestion> inlineSuggestions = response.getInlineSuggestions();
-        mResponseState = ResponseState.RECEIVE_RESPONSE;
-        mHandler.post(() -> {
-            if (mResponseState == ResponseState.START_INPUT && inlineSuggestions.isEmpty()) {
-                scheduleDelayedDeletion();
-            } else {
-                inflateThenShowSuggestions(inlineSuggestions);
-            }
-            mResponseState = ResponseState.RESET;
-        });
+        postPendingResponse(response);
         return true;
     }
 
@@ -333,6 +344,12 @@
 
     private void inflateThenShowSuggestions( List<InlineSuggestion> inlineSuggestions) {
         final int totalSuggestionsCount = inlineSuggestions.size();
+        if (inlineSuggestions.isEmpty()) {
+            // clear the suggestions and then return
+            getMainExecutor().execute(() -> updateInlineSuggestionStrip(Collections.EMPTY_LIST));
+            return;
+        }
+
         final Map<Integer, SuggestionItem> suggestionMap = Collections.synchronizedMap((
                 new TreeMap<>()));
         final ExecutorService executor = Executors.newSingleThreadExecutor();
@@ -388,7 +405,6 @@
     enum ResponseState {
         RESET,
         RECEIVE_RESPONSE,
-        FINISH_INPUT,
         START_INPUT,
     }
 }