Attempting to fix the pinned header in contacts.

Partial - work in progress.

Change-Id: I8df0115df84e86be878f1b1b6462a6eb9380863f
diff --git a/src/com/android/contacts/list/ContactBrowseListFragment.java b/src/com/android/contacts/list/ContactBrowseListFragment.java
index 5ae425f..0f95779 100644
--- a/src/com/android/contacts/list/ContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/ContactBrowseListFragment.java
@@ -22,9 +22,12 @@
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ListView;
+import android.widget.TextView;
 
 /**
  * Fragment containing a contact list used for browsing (as compared to
@@ -113,6 +116,13 @@
         }
     }
 
+    @Override
+    protected View createView(LayoutInflater inflater, ViewGroup container) {
+        View view = super.createView(inflater, container);
+        getListView().addHeaderView(inflater.inflate(R.layout.total_contacts, null, false));
+        return view;
+    }
+
     public void setEditMode(boolean flag) {
         mEditMode = flag;
     }
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index fefa718..175d352 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -96,6 +96,19 @@
         return mAdapter;
     }
 
+    public void setListAdapter(T adapter) {
+        mAdapter = adapter;
+        mListView.setAdapter(mAdapter);
+        if (isPhotoLoaderEnabled()) {
+            mAdapter.setPhotoLoader(mPhotoLoader);
+        }
+        ((ContactsListActivity)getActivity()).setupListView(mAdapter, mListView);
+    }
+
+    public ListView getListView() {
+        return mListView;
+    }
+
     // TODO make abstract
     @Override
     protected Loader<Cursor> onCreateLoader(int id, Bundle args) {
@@ -114,6 +127,26 @@
             String text = getQuantityText(data.getCount(),
                     R.string.listFoundAllContactsZero, R.plurals.listFoundAllContacts);
             foundContactsText.setText(text);
+
+            TextView totalContacts = (TextView) mView.findViewById(R.id.totalContactsText);
+
+            int count = data.getCount();
+
+            if (isSearchMode()
+                    && !TextUtils.isEmpty(getQueryString())) {
+                text = getQuantityText(count, R.string.listFoundAllContactsZero,
+                        R.plurals.searchFoundContacts);
+            } else {
+                // TODO
+//            if (contactsListActivity.mDisplayOnlyPhones) {
+//                text = contactsListActivity.getQuantityText(count,
+//                        R.string.listTotalPhoneContactsZero, R.plurals.listTotalPhoneContacts);
+//            } else {
+                text = getQuantityText(count,
+                        R.string.listTotalAllContactsZero, R.plurals.listTotalAllContacts);
+//            }
+            }
+            totalContacts.setText(text);
         }
     }
 
@@ -222,26 +255,27 @@
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container) {
-        mView = inflateView(inflater, container);
+        mView = createView(inflater, container);
         mAdapter = createListAdapter();
-        configureView(mView);
+        setListAdapter(mAdapter);
         return mView;
     }
 
-    protected void configureView(View view) {
-        mListView = (ListView)view.findViewById(android.R.id.list);
+    protected View createView(LayoutInflater inflater, ViewGroup container) {
+        mView = inflateView(inflater, container);
+
+        mListView = (ListView)mView.findViewById(android.R.id.list);
         if (mListView == null) {
             throw new RuntimeException(
                     "Your content must have a ListView whose id attribute is " +
                     "'android.R.id.list'");
         }
 
-        View emptyView = view.findViewById(com.android.internal.R.id.empty);
+        View emptyView = mView.findViewById(com.android.internal.R.id.empty);
         if (emptyView != null) {
             mListView.setEmptyView(emptyView);
         }
 
-        mListView.setAdapter(mAdapter);
         mListView.setOnItemClickListener(this);
         mListView.setOnFocusChangeListener(this);
         mListView.setOnTouchListener(this);
@@ -257,17 +291,14 @@
             mListView.setOnCreateContextMenuListener(mContextMenuAdapter);
         }
 
-        configurePinnedHeader();
-
         if (isPhotoLoaderEnabled()) {
             mPhotoLoader =
                 new ContactPhotoLoader(getActivity(), R.drawable.ic_contact_list_picture);
-            mAdapter.setPhotoLoader(mPhotoLoader);
             mListView.setOnScrollListener(this);
         }
 
         if (isSearchMode()) {
-            mSearchEditText = (SearchEditText)view.findViewById(R.id.search_src_text);
+            mSearchEditText = (SearchEditText)mView.findViewById(R.id.search_src_text);
             mSearchEditText.setText(getQueryString());
             mSearchEditText.addTextChangedListener(this);
             mSearchEditText.setOnEditorActionListener(this);
@@ -275,14 +306,13 @@
         }
 
         if (isSearchResultsMode()) {
-            TextView titleText = (TextView)view.findViewById(R.id.search_results_for);
+            TextView titleText = (TextView)mView.findViewById(R.id.search_results_for);
             if (titleText != null) {
                 titleText.setText(Html.fromHtml(getActivity().getString(R.string.search_results_for,
                         "<b>" + getQueryString() + "</b>")));
             }
         }
-
-        ((ContactsListActivity)getActivity()).setupListView(mAdapter, mListView);
+        return mView;
     }
 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
@@ -314,17 +344,6 @@
         super.onDestroy();
     }
 
-    private void configurePinnedHeader() {
-        if (!mSectionHeaderDisplayEnabled) {
-            return;
-        }
-
-        if (mListView instanceof PinnedHeaderListView) {
-            PinnedHeaderListView pinnedHeaderList = (PinnedHeaderListView)mListView;
-            View headerView = mAdapter.createPinnedHeaderView(pinnedHeaderList);
-            pinnedHeaderList.setPinnedHeaderView(headerView);
-        }
-    }
 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         hideSoftKeyboard();
diff --git a/src/com/android/contacts/list/PinnedHeaderListAdapter.java b/src/com/android/contacts/list/PinnedHeaderListAdapter.java
index 27deef3..dd1542e 100644
--- a/src/com/android/contacts/list/PinnedHeaderListAdapter.java
+++ b/src/com/android/contacts/list/PinnedHeaderListAdapter.java
@@ -22,6 +22,7 @@
 import android.content.res.ColorStateList;
 import android.graphics.Color;
 import android.graphics.drawable.Drawable;
+import android.opengl.Visibility;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -96,7 +97,7 @@
      * visible or partially pushed up out of the view.
      */
     public int getPinnedHeaderState(int position) {
-        if (mIndexer == null || mCursor == null || mCursor.getCount() == 0) {
+        if (mIndexer == null || position < 0 || mCursor == null || mCursor.getCount() == 0) {
             return PINNED_HEADER_GONE;
         }
 
diff --git a/src/com/android/contacts/widget/PinnedHeaderListView.java b/src/com/android/contacts/widget/PinnedHeaderListView.java
index 24e5638..bdc66d5 100644
--- a/src/com/android/contacts/widget/PinnedHeaderListView.java
+++ b/src/com/android/contacts/widget/PinnedHeaderListView.java
@@ -20,6 +20,7 @@
 import android.graphics.Canvas;
 import android.util.AttributeSet;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.AbsListView;
 import android.widget.ListAdapter;
 import android.widget.ListView;
@@ -53,6 +54,11 @@
         public static final int PINNED_HEADER_PUSHED_UP = 2;
 
         /**
+         * Creates the pinned header view.
+         */
+        View createPinnedHeaderView(ViewGroup parent);
+
+        /**
          * Computes the desired state of the pinned header for the given
          * position of the first visible list item. Allowed return values are
          * {@link #PINNED_HEADER_GONE}, {@link #PINNED_HEADER_VISIBLE} or
@@ -75,9 +81,7 @@
     private PinnedHeaderAdapter mAdapter;
     private View mHeaderView;
     private boolean mHeaderViewVisible;
-
     private int mHeaderViewWidth;
-
     private int mHeaderViewHeight;
 
     private OnScrollListener mOnScrollListener;
@@ -112,6 +116,8 @@
     public void setAdapter(ListAdapter adapter) {
         super.setAdapter(adapter);
         mAdapter = (PinnedHeaderAdapter)adapter;
+        View headerView = mAdapter.createPinnedHeaderView(this);
+        setPinnedHeaderView(headerView);
     }
 
     @Override
@@ -124,7 +130,7 @@
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         if (mHeaderView != null) {
-            configureHeaderView(getFirstVisiblePosition());
+            configureHeaderView(getFirstVisiblePosition() - getHeaderViewsCount());
             measureChild(mHeaderView, widthMeasureSpec, heightMeasureSpec);
             mHeaderViewWidth = mHeaderView.getMeasuredWidth();
             mHeaderViewHeight = mHeaderView.getMeasuredHeight();
@@ -135,13 +141,13 @@
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         super.onLayout(changed, left, top, right, bottom);
         if (mHeaderView != null) {
-            mHeaderView.layout(0, 0, mHeaderViewWidth, mHeaderViewHeight);
+            mHeaderView.layout(0, mHeaderView.getTop(), mHeaderViewWidth, mHeaderViewHeight);
         }
     }
 
     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
             int totalItemCount) {
-        configureHeaderView(firstVisibleItem);
+        configureHeaderView(firstVisibleItem - getHeaderViewsCount());
         if (mOnScrollListener != null) {
             mOnScrollListener.onScroll(this, firstVisibleItem, visibleItemCount, totalItemCount);
         }
@@ -177,8 +183,7 @@
             case PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP: {
                 View firstView = getChildAt(0);
                 int bottom = firstView.getBottom();
-                int itemHeight = firstView.getHeight();
-                int headerHeight = mHeaderView.getHeight();
+                int headerHeight = mHeaderViewHeight;
                 int y;
                 int alpha;
                 if (bottom < headerHeight) {
@@ -190,7 +195,7 @@
                 }
                 mAdapter.configurePinnedHeader(mHeaderView, position, alpha);
                 if (mHeaderView.getTop() != y) {
-                    mHeaderView.layout(0, y, mHeaderViewWidth, mHeaderViewHeight + y);
+                    mHeaderView.layout(0, y, headerHeight, headerHeight + y);
                 }
                 mHeaderViewVisible = true;
                 break;