Making Directory search UI work in the Goop mode

Change-Id: I5ed3909179b7f65a9defe3ab5b50ebe07521f9ca
diff --git a/src/com/android/contacts/list/ContactEntryListAdapter.java b/src/com/android/contacts/list/ContactEntryListAdapter.java
index dac1d62..4ff7f0c 100644
--- a/src/com/android/contacts/list/ContactEntryListAdapter.java
+++ b/src/com/android/contacts/list/ContactEntryListAdapter.java
@@ -26,7 +26,6 @@
 import android.database.Cursor;
 import android.os.Bundle;
 import android.provider.ContactsContract.ContactCounts;
-import android.provider.ContactsContract.Directory;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -93,8 +92,13 @@
     }
 
     public void setSearchMode(boolean flag) {
-        mSearchMode = flag;
-        setPinnedPartitionHeadersEnabled(flag);
+        if (mSearchMode != flag) {
+            mSearchMode = flag;
+
+            // Search mode change may mean a new data type in the list.
+            // Let's drop current data to avoid confusion
+            resetPartitions();
+        }
     }
 
     public boolean isSearchResultsMode() {
@@ -233,6 +237,7 @@
     @Override
     protected void bindHeaderView(View view, int partition, Cursor cursor) {
         DirectoryPartition directoryPartition = mPartitions.get(partition);
+
         TextView directoryTypeTextView = (TextView)view.findViewById(R.id.directory_type);
         directoryTypeTextView.setText(directoryPartition.directoryType);
         TextView displayNameTextView = (TextView)view.findViewById(R.id.display_name);
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index 261fef5..07f6d48 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -164,15 +164,7 @@
             mProviderStatusLoader = new ProviderStatusLoader(getActivity());
         }
 
-        if (mSearchMode) {
-            startLoading(DIRECTORY_LOADER_ID, null);
-        } else {
-            DirectoryPartition directoryPartition = new DirectoryPartition();
-            directoryPartition.directoryId = Directory.DEFAULT;
-            directoryPartition.partitionIndex = 0;
-            mDirectoryPartitions.add(directoryPartition);
-            startLoading(false);
-        }
+        loadPartitions();
     }
 
     @Override
@@ -254,12 +246,37 @@
         }
     }
 
+    protected void loadPartitions() {
+        for (DirectoryPartition partition : mDirectoryPartitions) {
+            CursorLoader loader = (CursorLoader)getLoader(partition.partitionIndex);
+            if (loader != null) {
+                loader.cancelLoad();
+            }
+        }
+
+        mDirectoryPartitions.clear();
+        if (mSearchMode) {
+            startLoading(DIRECTORY_LOADER_ID, null);
+        } else {
+            DirectoryPartition directoryPartition = new DirectoryPartition();
+            directoryPartition.directoryId = Directory.DEFAULT;
+            directoryPartition.partitionIndex = 0;
+            mDirectoryPartitions.add(directoryPartition);
+            if (mAdapter != null) {
+                mAdapter.resetPartitions();
+                mAdapter.addDirectoryPartition(directoryPartition);
+            }
+            reloadData();
+        }
+    }
+
     protected void reloadData() {
         startLoading(true);
     }
 
     protected void startLoading(boolean forceLoad) {
         configureAdapter();
+
         for (DirectoryPartition partition : mDirectoryPartitions) {
             CursorLoader loader = (CursorLoader)getLoader(partition.partitionIndex);
             if (loader == null) {
@@ -298,10 +315,13 @@
 
     public void setSectionHeaderDisplayEnabled(boolean flag) {
         mSectionHeaderDisplayEnabled = flag;
+        if (mAdapter != null) {
+            mAdapter.setSectionHeaderDisplayEnabled(flag);
+        }
     }
 
     public boolean isSectionHeaderDisplayEnabled() {
-        return mSectionHeaderDisplayEnabled && !mSearchMode;
+        return mSectionHeaderDisplayEnabled;
     }
 
     public void setPhotoLoaderEnabled(boolean flag) {
@@ -314,9 +334,15 @@
     }
 
     public void setSearchMode(boolean flag) {
-        mSearchMode = flag;
-        if (mAdapter != null) {
-            mAdapter.setSearchMode(flag);
+        if (mSearchMode != flag) {
+            mSearchMode = flag;
+            // TODO not always
+            setSectionHeaderDisplayEnabled(!mSearchMode);
+            if (mAdapter != null) {
+                mAdapter.setSearchMode(flag);
+                mAdapter.setPinnedPartitionHeadersEnabled(flag);
+                loadPartitions();
+            }
         }
     }
 
@@ -504,9 +530,11 @@
     protected void configureAdapter() {
         if (mAdapter != null) {
             mAdapter.setQueryString(mQueryString);
+            mAdapter.setPinnedPartitionHeadersEnabled(mSearchMode);
             mAdapter.setContactNameDisplayOrder(mDisplayOrder);
             mAdapter.setSortOrder(mSortOrder);
             mAdapter.setNameHighlightingEnabled(isNameHighlighingEnabled());
+            mAdapter.setSectionHeaderDisplayEnabled(mSectionHeaderDisplayEnabled);
         }
     }
 
diff --git a/src/com/android/contacts/list/ContactListAdapter.java b/src/com/android/contacts/list/ContactListAdapter.java
index b2c9dbc..4c602c8 100644
--- a/src/com/android/contacts/list/ContactListAdapter.java
+++ b/src/com/android/contacts/list/ContactListAdapter.java
@@ -153,19 +153,24 @@
     }
 
     protected void bindSectionHeaderAndDivider(ContactListItemView view, int position) {
-        final int section = getSectionForPosition(position);
-        if (section != -1 && getPositionForSection(section) == position) {
-            String title = (String)getSections()[section];
-            view.setSectionHeader(title);
-        } else {
-            view.setDividerVisible(false);
-            view.setSectionHeader(null);
-        }
+        if (isSectionHeaderDisplayEnabled()) {
+            final int section = getSectionForPosition(position);
+            if (section != -1 && getPositionForSection(section) == position) {
+                String title = (String)getSections()[section];
+                view.setSectionHeader(title);
+            } else {
+                view.setDividerVisible(false);
+                view.setSectionHeader(null);
+            }
 
-        // move the divider for the last item in a section
-        if (getPositionForSection(section + 1) - 1 == position) {
-            view.setDividerVisible(false);
+            // move the divider for the last item in a section
+            if (getPositionForSection(section + 1) - 1 == position) {
+                view.setDividerVisible(false);
+            } else {
+                view.setDividerVisible(true);
+            }
         } else {
+            view.setSectionHeader(null);
             view.setDividerVisible(true);
         }
     }
diff --git a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
index c38adba..ea3ca93 100644
--- a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
@@ -24,6 +24,8 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.FrameLayout;
+import android.widget.ListAdapter;
 import android.widget.TextView;
 
 /**
@@ -36,6 +38,7 @@
     private boolean mCreateContactEnabled;
     private int mDisplayWithPhonesOnlyOption = ContactsRequest.DISPLAY_ONLY_WITH_PHONES_DISABLED;
     private boolean mVisibleContactsRestrictionEnabled = true;
+    private View mHeaderView;
 
     public DefaultContactBrowseListFragment() {
         setPhotoLoaderEnabled(true);
@@ -122,21 +125,32 @@
     @Override
     protected void onCreateView(LayoutInflater inflater, ViewGroup container) {
         super.onCreateView(inflater, container);
-        if (!isSearchMode() && !isSearchResultsMode()) {
-            // In the search-results mode the count is shown in the fat header above the list
-            getListView().addHeaderView(inflater.inflate(R.layout.total_contacts, null, false));
+
+        // Putting the header view inside a container will allow us to make
+        // it invisible later. See checkHeaderViewVisibility()
+        FrameLayout headerContainer = new FrameLayout(inflater.getContext());
+        mHeaderView = inflater.inflate(R.layout.total_contacts, null, false);
+        headerContainer.addView(mHeaderView);
+        getListView().addHeaderView(headerContainer);
+        checkHeaderViewVisibility();
+    }
+
+    @Override
+    public void setSearchMode(boolean flag) {
+        super.setSearchMode(flag);
+        checkHeaderViewVisibility();
+    }
+
+    private void checkHeaderViewVisibility() {
+        if (mHeaderView != null) {
+            mHeaderView.setVisibility(isSearchMode() ? View.GONE : View.VISIBLE);
         }
     }
 
     @Override
     protected void showCount(int partitionIndex, Cursor data) {
-        int count = data.getCount();
-        if (isSearchResultsMode()) {
-            TextView countText = (TextView)getView().findViewById(R.id.search_results_found);
-            String text = getQuantityText(data.getCount(),
-                    R.string.listFoundAllContactsZero, R.plurals.listFoundAllContacts);
-            countText.setText(text);
-        } else if (!isSearchMode()){
+        if (!isSearchMode()) {
+            int count = data.getCount();
             // TODO
             // if (contactsListActivity.mDisplayOnlyPhones) {
             // text = contactsListActivity.getQuantityText(count,
diff --git a/src/com/android/contacts/widget/IndexerListAdapter.java b/src/com/android/contacts/widget/IndexerListAdapter.java
index d68b3e6..d640328 100644
--- a/src/com/android/contacts/widget/IndexerListAdapter.java
+++ b/src/com/android/contacts/widget/IndexerListAdapter.java
@@ -35,6 +35,7 @@
     private int mIndexedPartition = 0;
     private boolean mSectionHeaderDisplayEnabled;
     private View mHeader;
+    private TextView mTitleView;
 
     /**
      * Constructor.
@@ -116,6 +117,7 @@
             if (mHeader == null) {
                 mHeader = LayoutInflater.from(mContext).
                         inflate(mSectionHeaderLayoutResId, parent, false);
+                mTitleView = (TextView)mHeader.findViewById(mSectionHeaderTextViewId);
             }
             return mHeader;
         } else {
@@ -151,12 +153,7 @@
                 listView.setHeaderInvisible(index);
             } else {
                 String title = (String)mIndexer.getSections()[section];
-                TextView titleView = (TextView)mHeader.getTag();
-                if (titleView == null) {
-                    titleView = (TextView)mHeader.findViewById(mSectionHeaderTextViewId);
-                    mHeader.setTag(titleView);
-                }
-                titleView.setText(title);
+                mTitleView.setText(title);
 
                 // Compute the item position where the current partition begins
                 int partitionStart = getPositionForPartition(mIndexedPartition);
diff --git a/src/com/android/contacts/widget/PinnedHeaderListAdapter.java b/src/com/android/contacts/widget/PinnedHeaderListAdapter.java
index e7a9553..e227192 100644
--- a/src/com/android/contacts/widget/PinnedHeaderListAdapter.java
+++ b/src/com/android/contacts/widget/PinnedHeaderListAdapter.java
@@ -25,6 +25,8 @@
 public abstract class PinnedHeaderListAdapter extends CompositeCursorAdapter
         implements PinnedHeaderListView.PinnedHeaderAdapter {
 
+    public static final int PARTITION_HEADER_TYPE = 0;
+
     private boolean mPinnedPartitionHeadersEnabled;
     private boolean mHeaderVisibility[];
 
@@ -63,9 +65,16 @@
      */
     public View getPinnedHeaderView(int partition, View convertView, ViewGroup parent) {
         if (hasHeader(partition)) {
-            View view = convertView;
+            View view = null;
+            if (convertView != null) {
+                Integer headerType = (Integer)convertView.getTag();
+                if (headerType != null && headerType == PARTITION_HEADER_TYPE) {
+                    view = convertView;
+                }
+            }
             if (view == null) {
                 view = newHeaderView(getContext(), partition, null, parent);
+                view.setTag(PARTITION_HEADER_TYPE);
                 view.setFocusable(false);
                 view.setEnabled(false);
             }
@@ -95,12 +104,14 @@
             }
         }
 
+        int headerViewsCount = listView.getHeaderViewsCount();
+
         // Starting at the top, find and pin headers for partitions preceding the visible one(s)
         int maxTopHeader = -1;
         int topHeaderHeight = 0;
         for (int i = 0; i < size; i++) {
             if (mHeaderVisibility[i]) {
-                int position = listView.getPositionAt(topHeaderHeight);
+                int position = listView.getPositionAt(topHeaderHeight) - headerViewsCount;
                 int partition = getPartitionForPosition(position);
                 if (i > partition) {
                     break;
@@ -118,7 +129,8 @@
         int listHeight = listView.getHeight();
         for (int i = size; --i > maxTopHeader;) {
             if (mHeaderVisibility[i]) {
-                int position = listView.getPositionAt(listHeight - bottomHeaderHeight);
+                int position = listView.getPositionAt(listHeight - bottomHeaderHeight)
+                        - headerViewsCount;
                 if (position < 0) {
                     break;
                 }
diff --git a/src/com/android/contacts/widget/PinnedHeaderListView.java b/src/com/android/contacts/widget/PinnedHeaderListView.java
index 87cc34f..d90d1c4 100644
--- a/src/com/android/contacts/widget/PinnedHeaderListView.java
+++ b/src/com/android/contacts/widget/PinnedHeaderListView.java
@@ -336,7 +336,7 @@
     }
 
     @Override
-    public boolean onTouchEvent(MotionEvent ev) {
+    public boolean onInterceptTouchEvent(MotionEvent ev) {
         if (mScrollState == SCROLL_STATE_IDLE) {
             final int y = (int)ev.getY();
             for (int i = mSize; --i >= 0;) {
@@ -351,7 +351,7 @@
             }
         }
 
-        return super.onTouchEvent(ev);
+        return super.onInterceptTouchEvent(ev);
     }
 
     private boolean smoothScrollToPartition(int partition) {
@@ -360,7 +360,8 @@
             return false;
         }
 
-        smoothScrollToSelectionFromTop(position, getTotalTopPinnedHeaderHeight());
+        smoothScrollToSelectionFromTop(position + getHeaderViewsCount(),
+                getTotalTopPinnedHeaderHeight());
         return true;
     }