Prevent a potential ANR

Move this code into an AsyncTask, since createAlternatesAdapter() can
take a while to return.

Bug: 8519276
Change-Id: I7b3f30ab13ed894e80ac26c365dcb3cbe178dd37
diff --git a/chips/src/com/android/ex/chips/RecipientEditTextView.java b/chips/src/com/android/ex/chips/RecipientEditTextView.java
index d7cd7fa..41bab18 100644
--- a/chips/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/chips/src/com/android/ex/chips/RecipientEditTextView.java
@@ -1412,36 +1412,45 @@
         }
     }
 
-    private void showAlternates(DrawableRecipientChip currentChip, ListPopupWindow alternatesPopup,
-            int width) {
-        int line = getLayout().getLineForOffset(getChipStart(currentChip));
-        int bottom;
-        if (line == getLineCount() -1) {
-            bottom = 0;
-        } else {
-            bottom = -(int) ((mChipHeight + (2 * mLineSpacingExtra)) * (Math.abs(getLineCount() - 1
-                    - line)));
-        }
-        // Align the alternates popup with the left side of the View,
-        // regardless of the position of the chip tapped.
-        alternatesPopup.setWidth(width);
-        alternatesPopup.setAnchorView(this);
-        alternatesPopup.setVerticalOffset(bottom);
-        alternatesPopup.setAdapter(createAlternatesAdapter(currentChip));
-        alternatesPopup.setOnItemClickListener(mAlternatesListener);
-        // Clear the checked item.
-        mCheckedItem = -1;
-        alternatesPopup.show();
-        ListView listView = alternatesPopup.getListView();
-        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
-        // Checked item would be -1 if the adapter has not
-        // loaded the view that should be checked yet. The
-        // variable will be set correctly when onCheckedItemChanged
-        // is called in a separate thread.
-        if (mCheckedItem != -1) {
-            listView.setItemChecked(mCheckedItem, true);
-            mCheckedItem = -1;
-        }
+    private void showAlternates(final DrawableRecipientChip currentChip,
+            final ListPopupWindow alternatesPopup, final int width) {
+        new AsyncTask<Void, Void, ListAdapter>() {
+            @Override
+            protected ListAdapter doInBackground(final Void... params) {
+                return createAlternatesAdapter(currentChip);
+            }
+
+            protected void onPostExecute(final ListAdapter result) {
+                int line = getLayout().getLineForOffset(getChipStart(currentChip));
+                int bottom;
+                if (line == getLineCount() -1) {
+                    bottom = 0;
+                } else {
+                    bottom = -(int) ((mChipHeight + (2 * mLineSpacingExtra)) * (Math
+                            .abs(getLineCount() - 1 - line)));
+                }
+                // Align the alternates popup with the left side of the View,
+                // regardless of the position of the chip tapped.
+                alternatesPopup.setWidth(width);
+                alternatesPopup.setAnchorView(RecipientEditTextView.this);
+                alternatesPopup.setVerticalOffset(bottom);
+                alternatesPopup.setAdapter(result);
+                alternatesPopup.setOnItemClickListener(mAlternatesListener);
+                // Clear the checked item.
+                mCheckedItem = -1;
+                alternatesPopup.show();
+                ListView listView = alternatesPopup.getListView();
+                listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+                // Checked item would be -1 if the adapter has not
+                // loaded the view that should be checked yet. The
+                // variable will be set correctly when onCheckedItemChanged
+                // is called in a separate thread.
+                if (mCheckedItem != -1) {
+                    listView.setItemChecked(mCheckedItem, true);
+                    mCheckedItem = -1;
+                }
+            }
+        }.execute((Void[]) null);
     }
 
     private ListAdapter createAlternatesAdapter(DrawableRecipientChip chip) {