Remove all key preview from placer when window gets hide

Bug: 5130302
Change-Id: I70e3737c33bfbac99366ab36d3437b0801f5140a
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index aab58b5..da3aa50 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -35,6 +35,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 import com.android.inputmethod.compat.FrameLayoutCompatUtils;
@@ -349,8 +350,11 @@
      * @param keyboard the keyboard to display in this view
      */
     public void setKeyboard(Keyboard keyboard) {
-        // Remove any pending messages, except dismissing preview
+        // Remove any pending dismissing preview
         mDrawingHandler.cancelAllShowKeyPreviews();
+        if (mKeyboard != null) {
+            PointerTracker.dismissAllKeyPreviews();
+        }
         mKeyboard = keyboard;
         LatinImeLogger.onSetKeyboard(keyboard);
         requestLayout();
@@ -788,14 +792,15 @@
 
     private void addKeyPreview(TextView keyPreview) {
         if (mPreviewPlacer == null) {
-            mPreviewPlacer = FrameLayoutCompatUtils.getPlacer(
-                    (ViewGroup)getRootView().findViewById(android.R.id.content));
+            mPreviewPlacer = new RelativeLayout(getContext());
+            final ViewGroup windowContentView =
+                    (ViewGroup)getRootView().findViewById(android.R.id.content);
+            windowContentView.addView(mPreviewPlacer);
         }
-        final ViewGroup placer = mPreviewPlacer;
-        placer.addView(keyPreview, FrameLayoutCompatUtils.newLayoutParam(placer, 0, 0));
+        mPreviewPlacer.addView(
+                keyPreview, FrameLayoutCompatUtils.newLayoutParam(mPreviewPlacer, 0, 0));
     }
 
-    // TODO: Introduce minimum duration for displaying key previews
     private void showKey(final int keyIndex, PointerTracker tracker) {
         final TextView previewText = tracker.getKeyPreviewText();
         // If the key preview has no parent view yet, add it to the ViewGroup which can place
@@ -914,5 +919,8 @@
     public void onDetachedFromWindow() {
         super.onDetachedFromWindow();
         closing();
+        if (mPreviewPlacer != null) {
+            mPreviewPlacer.removeAllViews();
+        }
     }
 }
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
index abf28c7..12aadcb 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardBaseView.java
@@ -281,9 +281,6 @@
      */
     @Override
     public void setKeyboard(Keyboard keyboard) {
-        if (getKeyboard() != null) {
-            PointerTracker.dismissAllKeyPreviews();
-        }
         // Remove any pending messages, except dismissing preview
         mKeyTimerHandler.cancelKeyTimers();
         super.setKeyboard(keyboard);