Prevent infinite posting of Runnables

If shrink() is called when the View's width is 0, and it's GONE, its
width will never be greater than 0, so it just keeps posting the
Runnable forever.

Bug: 18613863
Change-Id: I1b2fa598826b6fa47996597c835533de3a4af220
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index 6541180..bae6058 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -201,6 +201,7 @@
     private int mCheckedItem;
     private boolean mNoChips = false;
     private boolean mShouldShrink = true;
+    private boolean mRequiresShrinkWhenNotGone = false;
 
     // VisibleForTesting
     ArrayList<DrawableRecipientChip> mTemporaryRecipients;
@@ -594,13 +595,20 @@
             clearSelectedChip();
         } else {
             if (getWidth() <= 0) {
-                // We don't have the width yet which means the view hasn't been drawn yet
-                // and there is no reason to attempt to commit chips yet.
-                // This focus lost must be the result of an orientation change
-                // or an initial rendering.
-                // Re-post the shrink for later.
                 mHandler.removeCallbacks(mDelayedShrink);
-                mHandler.post(mDelayedShrink);
+
+                if (getVisibility() == GONE) {
+                    // We aren't going to have a width any time soon, so defer
+                    // this until we're not GONE.
+                    mRequiresShrinkWhenNotGone = true;
+                } else {
+                    // We don't have the width yet which means the view hasn't been drawn yet
+                    // and there is no reason to attempt to commit chips yet.
+                    // This focus lost must be the result of an orientation change
+                    // or an initial rendering.
+                    // Re-post the shrink for later.
+                    mHandler.post(mDelayedShrink);
+                }
                 return;
             }
             // Reset any pending chips as they would have been handled
@@ -3202,6 +3210,16 @@
         mAlternatePopupAnchor = v;
     }
 
+    @Override
+    public void setVisibility(int visibility) {
+        super.setVisibility(visibility);
+
+        if (visibility != GONE && mRequiresShrinkWhenNotGone) {
+            mRequiresShrinkWhenNotGone = false;
+            mHandler.post(mDelayedShrink);
+        }
+    }
+
     private static class ChipBitmapContainer {
         Bitmap bitmap;
         // information used for positioning the loaded icon