Merge "Moving section placement calculations to IndexerListAdapter"
diff --git a/src/com/android/contacts/list/ContactEntryListAdapter.java b/src/com/android/contacts/list/ContactEntryListAdapter.java
index 2d94eb0..85de839 100644
--- a/src/com/android/contacts/list/ContactEntryListAdapter.java
+++ b/src/com/android/contacts/list/ContactEntryListAdapter.java
@@ -31,7 +31,6 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.ListView;
 import android.widget.TextView;
 
 import java.util.HashSet;
@@ -64,25 +63,6 @@
 
     private boolean mSelectionVisible;
 
-    /**
-     * An item view is displayed differently depending on whether it is placed
-     * at the beginning, middle or end of a section. It also needs to know the
-     * section header when it is at the beginning of a section. This object
-     * captures all this configuration.
-     */
-    public static final class Placement {
-        private int position = ListView.INVALID_POSITION;
-        public boolean firstInSection;
-        public boolean lastInSection;
-        public String sectionHeader;
-
-        public void invalidate() {
-            position = ListView.INVALID_POSITION;
-        }
-    }
-
-    private Placement mPlacementCache = new Placement();
-
     public ContactEntryListAdapter(Context context) {
         super(context, R.layout.list_section, R.id.header_text);
         addPartitions();
@@ -276,8 +256,6 @@
 
     @Override
     public void changeCursor(int partitionIndex, Cursor cursor) {
-        mPlacementCache.invalidate();
-
         Partition partition = getPartition(partitionIndex);
         if (partition instanceof DirectoryPartition) {
             ((DirectoryPartition)partition).setLoading(false);
@@ -314,47 +292,17 @@
         }
     }
 
-    /**
-     * Computes the item's placement within its section and populates the {@code placement}
-     * object accordingly.  Please note that the returned object is volatile and should be
-     * copied if the result needs to be used later.
-     */
-    public Placement getItemPlacementInSection(int position) {
-        if (mPlacementCache.position == position) {
-            return mPlacementCache;
-        }
-
-        mPlacementCache.position = position;
-        if (isSectionHeaderDisplayEnabled()) {
-            int section = getSectionForPosition(position);
-            if (section != -1 && getPositionForSection(section) == position) {
-                mPlacementCache.firstInSection = true;
-                mPlacementCache.sectionHeader = (String)getSections()[section];
-            } else {
-                mPlacementCache.firstInSection = false;
-                mPlacementCache.sectionHeader = null;
-            }
-
-            mPlacementCache.lastInSection = (getPositionForSection(section + 1) - 1 == position);
-        } else {
-            mPlacementCache.firstInSection = false;
-            mPlacementCache.lastInSection = false;
-            mPlacementCache.sectionHeader = null;
-        }
-        return mPlacementCache;
-    }
-
     @Override
     public int getViewTypeCount() {
         // We need a separate view type for each item type, plus another one for
         // each type with header, plus one for "other".
-        return super.getItemViewTypeCount() * 2 + 1;
+        return getItemViewTypeCount() * 2 + 1;
     }
 
     @Override
-    public int getItemViewType(int position) {
-        int type = super.getItemViewType(position);
-        if (isSectionHeaderDisplayEnabled()) {
+    public int getItemViewType(int partitionIndex, int position) {
+        int type = super.getItemViewType(partitionIndex, position);
+        if (isSectionHeaderDisplayEnabled() && partitionIndex == getIndexedPartition()) {
             Placement placement = getItemPlacementInSection(position);
             return placement.firstInSection ? type : getItemViewTypeCount() + type;
         } else {
diff --git a/src/com/android/contacts/list/JoinContactListAdapter.java b/src/com/android/contacts/list/JoinContactListAdapter.java
index 09b8caf..7e162db 100644
--- a/src/com/android/contacts/list/JoinContactListAdapter.java
+++ b/src/com/android/contacts/list/JoinContactListAdapter.java
@@ -57,7 +57,7 @@
         setPinnedPartitionHeadersEnabled(true);
         setSectionHeaderDisplayEnabled(true);
         setIndexedPartition(PARTITION_ALL_CONTACTS);
-        mShowAllContactsViewType = super.getViewTypeCount();
+        mShowAllContactsViewType = getViewTypeCount() - 1;
     }
 
     @Override
@@ -141,12 +141,12 @@
     }
 
     @Override
-    public int getItemViewTypeCount() {
-        return super.getItemViewTypeCount() + 1;
+    public int getViewTypeCount() {
+        return super.getViewTypeCount() + 1;
     }
 
     @Override
-    protected int getItemViewType(int partition, int position) {
+    public int getItemViewType(int partition, int position) {
         if (partition == PARTITION_SHOW_ALL_CONTACTS) {
             return mShowAllContactsViewType;
         }
diff --git a/src/com/android/contacts/widget/IndexerListAdapter.java b/src/com/android/contacts/widget/IndexerListAdapter.java
index c39e2f2..b26c2dc 100644
--- a/src/com/android/contacts/widget/IndexerListAdapter.java
+++ b/src/com/android/contacts/widget/IndexerListAdapter.java
@@ -19,6 +19,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ListView;
 import android.widget.SectionIndexer;
 import android.widget.TextView;
 
@@ -38,6 +39,25 @@
     private TextView mTitleView;
 
     /**
+     * An item view is displayed differently depending on whether it is placed
+     * at the beginning, middle or end of a section. It also needs to know the
+     * section header when it is at the beginning of a section. This object
+     * captures all this configuration.
+     */
+    public static final class Placement {
+        private int position = ListView.INVALID_POSITION;
+        public boolean firstInSection;
+        public boolean lastInSection;
+        public String sectionHeader;
+
+        public void invalidate() {
+            position = ListView.INVALID_POSITION;
+        }
+    }
+
+    private Placement mPlacementCache = new Placement();
+
+    /**
      * Constructor.
      *
      * @param context
@@ -74,6 +94,7 @@
 
     public void setIndexer(SectionIndexer indexer) {
         mIndexer = indexer;
+        mPlacementCache.invalidate();
     }
 
     public Object[] getSections() {
@@ -172,4 +193,34 @@
             }
         }
     }
+
+    /**
+     * Computes the item's placement within its section and populates the {@code placement}
+     * object accordingly.  Please note that the returned object is volatile and should be
+     * copied if the result needs to be used later.
+     */
+    public Placement getItemPlacementInSection(int position) {
+        if (mPlacementCache.position == position) {
+            return mPlacementCache;
+        }
+
+        mPlacementCache.position = position;
+        if (isSectionHeaderDisplayEnabled()) {
+            int section = getSectionForPosition(position);
+            if (section != -1 && getPositionForSection(section) == position) {
+                mPlacementCache.firstInSection = true;
+                mPlacementCache.sectionHeader = (String)getSections()[section];
+            } else {
+                mPlacementCache.firstInSection = false;
+                mPlacementCache.sectionHeader = null;
+            }
+
+            mPlacementCache.lastInSection = (getPositionForSection(section + 1) - 1 == position);
+        } else {
+            mPlacementCache.firstInSection = false;
+            mPlacementCache.lastInSection = false;
+            mPlacementCache.sectionHeader = null;
+        }
+        return mPlacementCache;
+    }
 }