Making selection visible in contact browser.

Change-Id: I936c4d3fdec8061e62b5d551189ec87408d574f9
diff --git a/res/drawable/list_item_checked_bg.9.png b/res/drawable/list_item_checked_bg.9.png
new file mode 100644
index 0000000..8b773d5
--- /dev/null
+++ b/res/drawable/list_item_checked_bg.9.png
Binary files differ
diff --git a/src/com/android/contacts/activities/ContactListActivity.java b/src/com/android/contacts/activities/ContactListActivity.java
index 2cd6f2b..18a3bcb 100644
--- a/src/com/android/contacts/activities/ContactListActivity.java
+++ b/src/com/android/contacts/activities/ContactListActivity.java
@@ -289,6 +289,7 @@
                 fragment.setQueryString(mRequest.getQueryString());
                 fragment.setDirectorySearchEnabled(mRequest.isDirectorySearchEnabled());
                 fragment.setAizyEnabled(!mRequest.isSearchMode());
+                fragment.setSelectionVisible(mTwoPaneLayout);
                 return fragment;
             }
 
@@ -301,6 +302,7 @@
                 fragment.setOnContactListActionListener(new ContactBrowserActionListener());
                 fragment.setFrequentlyContactedContactsIncluded(false);
                 fragment.setStarredContactsIncluded(true);
+                fragment.setSelectionVisible(mTwoPaneLayout);
                 return fragment;
             }
 
@@ -309,6 +311,7 @@
                 fragment.setOnContactListActionListener(new ContactBrowserActionListener());
                 fragment.setFrequentlyContactedContactsIncluded(true);
                 fragment.setStarredContactsIncluded(false);
+                fragment.setSelectionVisible(mTwoPaneLayout);
                 return fragment;
             }
 
@@ -317,6 +320,7 @@
                 fragment.setOnContactListActionListener(new ContactBrowserActionListener());
                 fragment.setFrequentlyContactedContactsIncluded(true);
                 fragment.setStarredContactsIncluded(true);
+                fragment.setSelectionVisible(mTwoPaneLayout);
                 return fragment;
             }
 
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index 566a533..0602097 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -89,6 +89,7 @@
     private boolean mAizyEnabled;
     private String mQueryString;
     private boolean mDirectorySearchEnabled;
+    private boolean mSelectionVisible;
 
     private T mAdapter;
     private View mView;
@@ -388,6 +389,21 @@
         return mPhotoLoaderEnabled;
     }
 
+    /**
+     * Returns true if the list is supposed to visually highlight the selected item.
+     */
+    public boolean isSelectionVisible() {
+        return mSelectionVisible;
+    }
+
+    public void setSelectionVisible(boolean flag) {
+        this.mSelectionVisible = flag;
+        if (mListView != null) {
+            mListView.setChoiceMode(
+                    mSelectionVisible ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
+        }
+    }
+
     public void setSearchMode(boolean flag) {
         if (mSearchMode != flag) {
             mSearchMode = flag;
@@ -534,6 +550,9 @@
         // We manually save/restore the listview state
         mListView.setSaveEnabled(false);
 
+        mListView.setChoiceMode(
+                mSelectionVisible ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE);
+
         if (mContextMenuAdapter != null) {
             mListView.setOnCreateContextMenuListener(mContextMenuAdapter);
         }
diff --git a/src/com/android/contacts/list/ContactListItemView.java b/src/com/android/contacts/list/ContactListItemView.java
index 28444a4..2606425 100644
--- a/src/com/android/contacts/list/ContactListItemView.java
+++ b/src/com/android/contacts/list/ContactListItemView.java
@@ -39,6 +39,7 @@
 import android.view.Gravity;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Checkable;
 import android.widget.ImageView;
 import android.widget.QuickContactBadge;
 import android.widget.TextView;
@@ -47,13 +48,15 @@
 /**
  * A custom view for an item in the contact list.
  */
-public class ContactListItemView extends ViewGroup {
+public class ContactListItemView extends ViewGroup implements Checkable {
 
     private static final int QUICK_CONTACT_BADGE_STYLE =
             com.android.internal.R.attr.quickContactBadgeStyleWindowMedium;
 
     protected final Context mContext;
 
+    private boolean mChecked;
+
     private final int mPreferredHeight;
     private final int mVerticalDividerMargin;
     private final int mPaddingTop;
@@ -66,6 +69,8 @@
     private final int mPresenceIconMargin;
     private final int mHeaderTextWidth;
 
+    private Drawable mCheckedBackgroundDrawable;
+
     private boolean mHorizontalDividerVisible = true;
     private Drawable mHorizontalDividerDrawable;
     private int mHorizontalDividerHeight;
@@ -262,6 +267,11 @@
             topBound += mHeaderBackgroundHeight;
         }
 
+        if (mChecked) {
+            ensureCheckedBackgroundDivider();
+            mCheckedBackgroundDrawable.setBounds(0, topBound, width, height);
+        }
+
         // Positions of views on the left are fixed and so are those on the right side.
         // The stretchable part of the layout is in the middle.  So, we will start off
         // by laying out the left and right sides. Then we will allocate the remainder
@@ -280,6 +290,7 @@
         }
 
         topBound += mPaddingTop;
+
         int bottomBound = height - mPaddingBottom;
 
         // Text lines, centered vertically
@@ -391,6 +402,17 @@
     }
 
     /**
+     * Loads the drawable for the item background used when the item is checked.
+     */
+    private void ensureCheckedBackgroundDivider() {
+        if (mCheckedBackgroundDrawable == null) {
+            mCheckedBackgroundDrawable = mContext.getResources().getDrawable(
+                    R.drawable.list_item_checked_bg);
+            mCheckedBackgroundDrawable.setBounds(0, 0, getWidth(), getHeight());
+        }
+    }
+
+    /**
      * Loads the drawable for the vertical divider if it has not yet been loaded.
      */
     private void ensureVerticalDivider() {
@@ -443,6 +465,9 @@
 
     @Override
     public void dispatchDraw(Canvas canvas) {
+        if (mChecked) {
+            mCheckedBackgroundDrawable.draw(canvas);
+        }
         if (mHeaderVisible) {
             mHeaderBackgroundDrawable.draw(canvas);
         }
@@ -818,4 +843,22 @@
         cursor.copyStringToBuffer(dataColumnIndex, dataBuffer);
         setData(dataBuffer.data, dataBuffer.sizeCopied);
     }
+
+    @Override
+    public boolean isChecked() {
+        return mChecked;
+    }
+
+    @Override
+    public void setChecked(boolean checked) {
+        if (mChecked != checked) {
+            mChecked = checked;
+            requestLayout();
+        }
+    }
+
+    @Override
+    public void toggle() {
+        setChecked(!mChecked);
+    }
 }