Show a header containing the total number of contacts.

Bug: 2138779
diff --git a/res/layout-finger/total_contacts.xml b/res/layout-finger/total_contacts.xml
new file mode 100644
index 0000000..1221ef3
--- /dev/null
+++ b/res/layout-finger/total_contacts.xml
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2009 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<TextView xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/totalContactsText"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    style="?android:attr/listSeparatorTextViewStyle"
+    android:textColor="@*android:color/dim_foreground_dark"
+    android:textSize="12sp"
+    android:textStyle="normal"
+    android:background="@drawable/section_dark"
+    android:paddingLeft="7dp"
+    android:gravity="left|center_vertical"
+    android:visibility="gone"
+/>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 316792d..141c166 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -408,6 +408,12 @@
     <!-- Section header in the Edit Contacts screen for the "Add more items" button -->
     <string name="listSeparatorMore_edit">More</string>
 
+    <!-- Displayed at the top of the contacts showing the total number of contacts visible when "Only contacts with phones" is selected -->
+    <string name="listTotalPhoneContacts"><xliff:g id="num">%s</xliff:g> contacts with phone numbers</string>
+
+    <!-- Displayed at the top of the contacts showing the total number of contacts visible when "Only contacts with phones" not selected -->
+    <string name="listTotalAllContacts"><xliff:g id="num">%s</xliff:g> contacts</string>
+
     <!-- The description text for the social activity stream tab. Space is limited for this string, so the shorter the better -->
     <string name="socialStreamIconLabel">Social</string>
 
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 6a433d1..e03d5db 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -16,12 +16,11 @@
 
 package com.android.contacts;
 
-import com.android.contacts.model.ContactsSource;
 import com.android.contacts.model.Sources;
 import com.android.contacts.ui.DisplayGroupsActivity;
 import com.android.contacts.ui.DisplayGroupsActivity.Prefs;
-import com.android.contacts.util.Constants;
 import com.android.contacts.util.AccountSelectionUtil;
+import com.android.contacts.util.Constants;
 
 import android.accounts.Account;
 import android.app.Activity;
@@ -175,11 +174,13 @@
     static final int MODE_MASK_SHOW_CALL_BUTTON = 0x02000000;
     /** Mask to disable fasttrack (images will show as normal images) */
     static final int MODE_MASK_DISABLE_FASTTRACK = 0x01000000;
+    /** Mask to show the total number of contacts at the top */
+    static final int MODE_MASK_SHOW_NUMBER_OF_CONTACTS = 0x00800000;
 
     /** Unknown mode */
     static final int MODE_UNKNOWN = 0;
     /** Default mode */
-    static final int MODE_DEFAULT = 4 | MODE_MASK_SHOW_PHOTOS;
+    static final int MODE_DEFAULT = 4 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
     /** Custom mode */
     static final int MODE_CUSTOM = 8;
     /** Show all starred contacts */
@@ -556,9 +557,14 @@
             list.setTextFilterEnabled(true);
         }
 
+        final LayoutInflater inflater = getLayoutInflater();
+        if ((mMode & MODE_MASK_SHOW_NUMBER_OF_CONTACTS) != 0) {
+            View totalContacts = inflater.inflate(R.layout.total_contacts, list, false);
+            list.addHeaderView(totalContacts);
+        }
+
         if ((mMode & MODE_MASK_CREATE_NEW) != 0) {
             // Add the header for creating a new contact
-            final LayoutInflater inflater = getLayoutInflater();
             View header = inflater.inflate(R.layout.create_new_contact, list, false);
             list.addHeaderView(header);
         }
@@ -570,6 +576,11 @@
         setListAdapter(mAdapter);
         getListView().setOnScrollListener(mAdapter);
 
+        if ((mMode & MODE_MASK_SHOW_NUMBER_OF_CONTACTS) != 0) {
+            TextView totalContacts = (TextView) findViewById(R.id.totalContactsText);
+            totalContacts.setVisibility(View.VISIBLE);
+        }
+
         // We manually save/restore the listview state
         list.setSaveEnabled(false);
 
@@ -1065,6 +1076,13 @@
                 getSystemService(Context.INPUT_METHOD_SERVICE);
         inputMethodManager.hideSoftInputFromWindow(mList.getWindowToken(), 0);
 
+        if ((mMode & MODE_MASK_SHOW_NUMBER_OF_CONTACTS) != 0) {
+            if (position == 0) {
+                return;
+            }
+            position--;
+        }
+
         if (mMode == MODE_INSERT_OR_EDIT_CONTACT) {
             Intent intent;
             if (position == 0) {
@@ -2326,7 +2344,9 @@
 
             // Get the split between starred and frequent items, if the mode is strequent
             mFrequentSeparatorPos = ListView.INVALID_POSITION;
-            if (cursor != null && cursor.getCount() > 0 && mMode == MODE_STREQUENT) {
+            int cursorCount = 0;
+            if (cursor != null && (cursorCount = cursor.getCount()) > 0
+                    && mMode == MODE_STREQUENT) {
                 cursor.move(-1);
                 for (int i = 0; cursor.moveToNext(); i++) {
                     int starred = cursor.getInt(SUMMARY_STARRED_COLUMN_INDEX);
@@ -2341,7 +2361,12 @@
             }
 
             super.changeCursor(cursor);
-
+            if ((mMode & MODE_MASK_SHOW_NUMBER_OF_CONTACTS) != 0) {
+                TextView totalContacts = (TextView) findViewById(R.id.totalContactsText);
+                int stringId = mDisplayOnlyPhones
+                    ? R.string.listTotalPhoneContacts : R.string.listTotalAllContacts;
+                totalContacts.setText(getString(stringId, cursorCount));
+            }
             // Update the indexer for the fast scroll widget
             updateIndexer(cursor);
         }