Fixing size of selector in contact list

Section headers are drawn as part of
a list item and since they can be translucent,
they are affected by the default selector.

Solution: disable the default selector
and simulate its behavior in the custom draw
method.

Change-Id: I06c0bfb8ad173bb1437b9ee757eb78d680bab661
diff --git a/res/drawable-hdpi/list_item_pressed_bg.9.png b/res/drawable-hdpi/list_item_pressed_bg.9.png
new file mode 100644
index 0000000..74d8e53
--- /dev/null
+++ b/res/drawable-hdpi/list_item_pressed_bg.9.png
Binary files differ
diff --git a/res/drawable-mdpi/list_item_pressed_bg.9.png b/res/drawable-mdpi/list_item_pressed_bg.9.png
new file mode 100644
index 0000000..74d8e53
--- /dev/null
+++ b/res/drawable-mdpi/list_item_pressed_bg.9.png
Binary files differ
diff --git a/res/values-xlarge/styles.xml b/res/values-xlarge/styles.xml
index 9281e66..5bb1ed5 100644
--- a/res/values-xlarge/styles.xml
+++ b/res/values-xlarge/styles.xml
@@ -16,6 +16,7 @@
 
 <resources>
     <style name="ContactBrowserTheme" parent="@android:Theme.Light.Holo">
+        <item name="pressedBackground">@drawable/list_item_pressed_bg</item>
     </style>
     <style name="ContactPickerTheme" parent="@android:Theme.Dialog">
     </style>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index c52bb1f..b2e9b2e 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -113,8 +113,14 @@
     <style name="DummyAnimation">
         <item name="android:windowExitAnimation">@anim/dummy_animation</item>
     </style>
-    
+
+    <declare-styleable name="ContactListItemView">
+        <!-- An attribute that specifies a custom drawable for the pressed state in the contact list-->
+        <attr name="pressedBackground" format="reference"/>
+    </declare-styleable>
+
     <style name="ContactBrowserTheme" parent="@android:Theme">
+        <item name="pressedBackground">@*android:drawable/list_selector_background</item>
     </style>
 
     <style name="ContactPickerTheme" parent="@android:Theme">
diff --git a/src/com/android/contacts/ContactEntryListView.java b/src/com/android/contacts/ContactEntryListView.java
index 21ff25e..b5dfbeb 100644
--- a/src/com/android/contacts/ContactEntryListView.java
+++ b/src/com/android/contacts/ContactEntryListView.java
@@ -21,6 +21,10 @@
 import com.android.contacts.widget.TextHighlightingAnimation;
 
 import android.content.Context;
+import android.graphics.Canvas;
+import android.graphics.ColorFilter;
+import android.graphics.PixelFormat;
+import android.graphics.drawable.Drawable;
 import android.util.AttributeSet;
 import android.widget.AbsListView;
 import android.widget.ListAdapter;
@@ -37,6 +41,9 @@
             new ContactNameHighlightingAnimation(this, TEXT_HIGHLIGHTING_ANIMATION_DURATION);
 
     private boolean mHighlightNamesWhenScrolling;
+    private Drawable mDefaultSelector;
+    private boolean mSelectionVisible;
+    private ContactEntryListAdapter mAdapter;
 
     public ContactEntryListView(Context context) {
         this(context, null);
@@ -50,6 +57,7 @@
         super(context, attrs, defStyle);
         setPinnedHeaderBackgroundColor(
                 context.getResources().getColor(R.color.pinned_header_background));
+        mDefaultSelector = getSelector();
     }
 
     public TextHighlightingAnimation getTextHighlightingAnimation() {
@@ -64,6 +72,19 @@
         mHighlightNamesWhenScrolling = flag;
     }
 
+    public void setSelectionVisible(boolean selectionVisible) {
+        if (selectionVisible != mSelectionVisible) {
+            mSelectionVisible = selectionVisible;
+            if (selectionVisible) {
+                // When a persistent selection is handled by the adapter,
+                // we want to disable the standard selection drawing.
+                setSelector(new EmptyDrawable());
+            } else {
+                setSelector(mDefaultSelector);
+            }
+        }
+    }
+
     @Override
     public void setAdapter(ListAdapter adapter) {
         super.setAdapter(adapter);
@@ -84,4 +105,28 @@
             }
         }
     }
+
+    /**
+     * A drawable that is ignored.  We have to use an empty drawable instead
+     * of null, because ListView does not allow selection to be null.
+     */
+    private class EmptyDrawable extends Drawable {
+
+        @Override
+        public void draw(Canvas canvas) {
+        }
+
+        @Override
+        public int getOpacity() {
+            return PixelFormat.OPAQUE;
+        }
+
+        @Override
+        public void setAlpha(int alpha) {
+        }
+
+        @Override
+        public void setColorFilter(ColorFilter cf) {
+        }
+    }
 }
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index 29d40bb..235f8f3 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -283,6 +283,11 @@
 
         loadPreferences();
 
+        if (mListView instanceof ContactEntryListView) {
+            ContactEntryListView listView = (ContactEntryListView)mListView;
+            listView.setSelectionVisible(isSelectionVisible());
+        }
+
         mForceLoad = false;
         mLoadDirectoryList = true;
         mLoadPriorityDirectoriesOnly = true;
diff --git a/src/com/android/contacts/list/ContactListItemView.java b/src/com/android/contacts/list/ContactListItemView.java
index af429e4..97a1358 100644
--- a/src/com/android/contacts/list/ContactListItemView.java
+++ b/src/com/android/contacts/list/ContactListItemView.java
@@ -68,6 +68,7 @@
     private final int mPresenceIconMargin;
     private final int mHeaderTextWidth;
 
+    private Drawable mPressedBackgroundDrawable;
     private Drawable mSelectedBackgroundDrawable;
 
     private boolean mHorizontalDividerVisible = true;
@@ -143,6 +144,13 @@
                 a.getDimensionPixelSize(android.R.styleable.Theme_listPreferredItemHeight, 0);
         a.recycle();
 
+        mPressedBackgroundDrawable = getResources().getDrawable(R.drawable.list_item_pressed_bg);
+
+        a = getContext().obtainStyledAttributes(attrs,R.styleable.ContactListItemView);
+        mPressedBackgroundDrawable = a.getDrawable(
+                R.styleable.ContactListItemView_pressedBackground);
+        a.recycle();
+
         Resources resources = context.getResources();
         mVerticalDividerMargin =
                 resources.getDimensionPixelOffset(R.dimen.list_item_vertical_divider_margin);
@@ -285,10 +293,12 @@
         }
 
         if (mItemSelected) {
-            ensureCheckedBackgroundDivider();
+            ensureSelectedBackgroundDrawable();
             mSelectedBackgroundDrawable.setBounds(0, topBound, width, bottomBound);
         }
 
+        mPressedBackgroundDrawable.setBounds(0, topBound, width, bottomBound);
+
         topBound += mPaddingTop;
         bottomBound -= mPaddingBottom;
 
@@ -411,7 +421,7 @@
     /**
      * Loads the drawable for the item background used when the item is checked.
      */
-    private void ensureCheckedBackgroundDivider() {
+    private void ensureSelectedBackgroundDrawable() {
         if (mSelectedBackgroundDrawable == null) {
             mSelectedBackgroundDrawable = mContext.getResources().getDrawable(
                     R.drawable.list_item_checked_bg);
@@ -476,8 +486,17 @@
     }
 
     @Override
+    protected void drawableStateChanged() {
+        super.drawableStateChanged();
+        mPressedBackgroundDrawable.setState(getDrawableState());
+        invalidate();
+    }
+
+    @Override
     public void dispatchDraw(Canvas canvas) {
-        if (mItemSelected) {
+        if (isPressed()) {
+            mPressedBackgroundDrawable.draw(canvas);
+        } else if (mItemSelected) {
             mSelectedBackgroundDrawable.draw(canvas);
         }
         if (mHeaderVisible) {