Delay closing the alternates popup when the user selects an alternate.

Per UX, this gives the user a better idea of what has just occured.
Using temporary assets.

Change-Id: Ied49f899d7999356cc01a672ecb22f492901d55c
diff --git a/list_selector_pressed_holo_dark.9.png b/list_selector_pressed_holo_dark.9.png
new file mode 100644
index 0000000..0ed5ba3
--- /dev/null
+++ b/list_selector_pressed_holo_dark.9.png
Binary files differ
diff --git a/res/drawable-hdpi/alternate_list_background.png b/res/drawable-hdpi/alternate_list_background.png
new file mode 100644
index 0000000..ad79a76
--- /dev/null
+++ b/res/drawable-hdpi/alternate_list_background.png
Binary files differ
diff --git a/res/drawable-hdpi/chip_checkmark.png b/res/drawable-hdpi/chip_checkmark.png
index cac823b..cc0b617 100644
--- a/res/drawable-hdpi/chip_checkmark.png
+++ b/res/drawable-hdpi/chip_checkmark.png
Binary files differ
diff --git a/res/drawable-mdpi/alternate_list_background.png b/res/drawable-mdpi/alternate_list_background.png
new file mode 100644
index 0000000..ad79a76
--- /dev/null
+++ b/res/drawable-mdpi/alternate_list_background.png
Binary files differ
diff --git a/res/drawable/list_item_background.xml b/res/drawable/list_item_background.xml
new file mode 100644
index 0000000..18033af
--- /dev/null
+++ b/res/drawable/list_item_background.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<selector xmlns:android="http://schemas.android.com/apk/res/android">
+    <item android:state_activated="true"    android:drawable="@drawable/alternate_list_background" />
+    <item android:state_checked="true"    android:drawable="@drawable/alternate_list_background" />
+    <item android:state_selected="true"    android:drawable="@drawable/alternate_list_background" />
+</selector>
\ No newline at end of file
diff --git a/res/layout/chips_alternate_item.xml b/res/layout/chips_alternate_item.xml
index b5c4e9d..0d69659 100644
--- a/res/layout/chips_alternate_item.xml
+++ b/res/layout/chips_alternate_item.xml
@@ -20,7 +20,8 @@
     android:layout_height="wrap_content"
     android:minHeight="30dip"
     android:orientation="horizontal"
-    android:gravity="left|center_vertical">
+    android:gravity="left|center_vertical"
+    android:background="@drawable/list_item_background">
     <FrameLayout
         android:layout_width="50dip"
         android:layout_height="wrap_content">
diff --git a/src/com/android/ex/chips/RecipientAlternatesAdapter.java b/src/com/android/ex/chips/RecipientAlternatesAdapter.java
index 335ba1d..3d2c87b 100644
--- a/src/com/android/ex/chips/RecipientAlternatesAdapter.java
+++ b/src/com/android/ex/chips/RecipientAlternatesAdapter.java
@@ -33,19 +33,17 @@
 
     private final int mLayoutId;
 
-    private final int mSelectedLayoutId;
-
     private final long mCurrentId;
 
-    public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId,
-            int selectedViewId) {
+    private int mCheckedItemPosition = -1;
+
+    public RecipientAlternatesAdapter(Context context, long contactId, long currentId, int viewId) {
         super(context, context.getContentResolver().query(Email.CONTENT_URI, EmailQuery.PROJECTION,
                 Email.CONTACT_ID + " =?", new String[] {
                     String.valueOf(contactId)
                 }, null), 0);
         mLayoutInflater = LayoutInflater.from(context);
         mLayoutId = viewId;
-        mSelectedLayoutId = selectedViewId;
         mCurrentId = currentId;
     }
 
@@ -69,7 +67,10 @@
         Cursor cursor = getCursor();
         cursor.moveToPosition(position);
         if (convertView == null) {
-            convertView = newView(cursor.getLong(EmailQuery.DATA_ID) == mCurrentId);
+            convertView = newView();
+        }
+        if (cursor.getLong(EmailQuery.DATA_ID) == mCurrentId) {
+            mCheckedItemPosition = position;
         }
         bindView(convertView, convertView.getContext(), cursor);
         return convertView;
@@ -101,11 +102,17 @@
 
     @Override
     public View newView(Context context, Cursor cursor, ViewGroup parent) {
-        return newView(false);
+        return newView();
     }
 
-    private View newView(boolean isSelected) {
-        return isSelected ? mLayoutInflater.inflate(mSelectedLayoutId, null) : mLayoutInflater
-                .inflate(mLayoutId, null);
+    private View newView() {
+        return mLayoutInflater.inflate(mLayoutId, null);
+    }
+
+    /**
+     * Get the position of the item that should be checked.
+     */
+    public int getCheckedItemPosition() {
+        return mCheckedItemPosition;
     }
 }
diff --git a/src/com/android/ex/chips/RecipientEditTextView.java b/src/com/android/ex/chips/RecipientEditTextView.java
index e5b1f7f..8e00919 100644
--- a/src/com/android/ex/chips/RecipientEditTextView.java
+++ b/src/com/android/ex/chips/RecipientEditTextView.java
@@ -26,6 +26,8 @@
 import android.graphics.RectF;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
+import android.os.Handler;
+import android.os.Message;
 import android.text.Editable;
 import android.text.Layout;
 import android.text.Spannable;
@@ -47,8 +49,8 @@
 import android.view.ActionMode.Callback;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
-import android.widget.AutoCompleteTextView.Validator;
 import android.widget.ListPopupWindow;
+import android.widget.ListView;
 import android.widget.MultiAutoCompleteTextView;
 
 import java.util.Collection;
@@ -108,6 +110,12 @@
 
     private Drawable mInvalidChipBackground;
 
+    private Handler mHandler;
+
+    private static int DISMISS = "dismiss".hashCode();
+
+    private static final long DISMISS_DELAY = 300;
+
     public RecipientEditTextView(Context context, AttributeSet attrs) {
         super(context, attrs);
         setSuggestionsEnabled(false);
@@ -129,6 +137,16 @@
                 // TODO: find a better way to unfocus a chip when a user starts typing.
             }
         });
+        mHandler = new Handler() {
+            @Override
+            public void handleMessage(Message msg) {
+                if (msg.what == DISMISS) {
+                    ((ListPopupWindow)msg.obj).dismiss();
+                    return;
+                }
+                super.handleMessage(msg);
+            }
+        };
     }
 
     @Override
@@ -1051,10 +1069,8 @@
             mAlternatesPopup = new ListPopupWindow(getContext());
 
             if (!mAlternatesPopup.isShowing()) {
-                mAlternatesAdapter = new RecipientAlternatesAdapter(
-                        getContext(),
-                        mEntry.getContactId(), mEntry.getDataId(),
-                        mAlternatesLayout, mAlternatesSelectedLayout);
+                mAlternatesAdapter = new RecipientAlternatesAdapter(getContext(),
+                        mEntry.getContactId(), mEntry.getDataId(), mAlternatesLayout);
                 mAnchorView.setLeft(mLeft);
                 mAnchorView.setRight(mLeft);
                 mAlternatesPopup.setAnchorView(mAnchorView);
@@ -1062,6 +1078,9 @@
                 mAlternatesPopup.setWidth(getWidth());
                 mAlternatesPopup.setOnItemClickListener(this);
                 mAlternatesPopup.show();
+                ListView listView = mAlternatesPopup.getListView();
+                listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+                listView.setItemChecked(mAlternatesAdapter.getCheckedItemPosition(), true);
             }
         }
 
@@ -1138,7 +1157,9 @@
          */
         @Override
         public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
-            mAlternatesPopup.dismiss();
+            Message delayed = Message.obtain(mHandler, DISMISS);
+            delayed.obj = mAlternatesPopup;
+            mHandler.sendMessageDelayed(delayed, DISMISS_DELAY);
             clearComposingText();
             replaceChip(mAlternatesAdapter.getRecipientEntry(position));
         }