Refactoring contact list mode resolution.

Also, removing tons of obsolete code.

Change-Id: Ib661fae4f7568e18bb98d5f5e85d1ba75dc32cdb
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 1de9c55..a015d62 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -18,13 +18,11 @@
 
 import com.android.contacts.list.CallOrSmsInitiator;
 import com.android.contacts.list.ContactBrowseListContextMenuAdapter;
-import com.android.contacts.list.ContactEntryListAdapter;
 import com.android.contacts.list.ContactEntryListFragment;
-import com.android.contacts.list.ContactItemListAdapter;
 import com.android.contacts.list.ContactPickerFragment;
 import com.android.contacts.list.ContactsIntentResolver;
+import com.android.contacts.list.ContactsRequest;
 import com.android.contacts.list.DefaultContactBrowseListFragment;
-import com.android.contacts.list.MultiplePhonePickerFragment;
 import com.android.contacts.list.OnContactBrowserActionListener;
 import com.android.contacts.list.OnContactPickerActionListener;
 import com.android.contacts.list.OnPhoneNumberPickerActionListener;
@@ -44,35 +42,19 @@
 import android.app.Dialog;
 import android.app.FragmentTransaction;
 import android.app.SearchManager;
-import android.content.AsyncQueryHandler;
-import android.content.ContentResolver;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
-import android.content.UriMatcher;
 import android.content.res.Resources;
 import android.database.Cursor;
-import android.database.MatrixCursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.ContactsContract;
 import android.provider.Settings;
-import android.provider.Contacts.ContactMethods;
-import android.provider.Contacts.People;
-import android.provider.Contacts.PeopleColumns;
-import android.provider.Contacts.Phones;
-import android.provider.ContactsContract.ContactCounts;
 import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.Data;
-import android.provider.ContactsContract.Intents;
 import android.provider.ContactsContract.RawContacts;
-import android.provider.ContactsContract.SearchSnippetColumns;
-import android.provider.ContactsContract.CommonDataKinds.Email;
-import android.provider.ContactsContract.CommonDataKinds.Phone;
-import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
-import android.provider.ContactsContract.Intents.Insert;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
@@ -85,12 +67,9 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
-import android.widget.ListAdapter;
-import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import java.lang.ref.WeakReference;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -114,277 +93,23 @@
 
     public static final String AUTHORITIES_FILTER_KEY = "authorities";
 
-    private static final Uri CONTACTS_CONTENT_URI_WITH_LETTER_COUNTS =
-            buildSectionIndexerUri(Contacts.CONTENT_URI);
-
-    /** Mask for picker mode */
-    public static final int MODE_MASK_PICKER = 0x80000000;
-    /** Mask for no presence mode */
-    public static final int MODE_MASK_NO_PRESENCE = 0x40000000;
-    /** Mask for enabling list filtering */
-    public static final int MODE_MASK_NO_FILTER = 0x20000000;
-    /** Mask for having a "create new contact" header in the list */
-    public static final int MODE_MASK_CREATE_NEW = 0x10000000;
-    /** Mask for showing photos in the list */
-    public static final int MODE_MASK_SHOW_PHOTOS = 0x08000000;
-    /** Mask for hiding additional information e.g. primary phone number in the list */
-    public static final int MODE_MASK_NO_DATA = 0x04000000;
-    /** Mask for showing a call button in the list */
-    public static final int MODE_MASK_SHOW_CALL_BUTTON = 0x02000000;
-    /** Mask to disable quickcontact (images will show as normal images) */
-    public static final int MODE_MASK_DISABLE_QUIKCCONTACT = 0x01000000;
-    /** Mask to show the total number of contacts at the top */
-    public static final int MODE_MASK_SHOW_NUMBER_OF_CONTACTS = 0x00800000;
-
-    /** Unknown mode */
-    public static final int MODE_UNKNOWN = 0;
-    /** Default mode */
-    public static final int MODE_DEFAULT = 4 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-    /** Custom mode */
-    public static final int MODE_CUSTOM = 8;
-    /** Show all starred contacts */
-    public static final int MODE_STARRED = 20 | MODE_MASK_SHOW_PHOTOS;
-    /** Show frequently contacted contacts */
-    public static final int MODE_FREQUENT = 30 | MODE_MASK_SHOW_PHOTOS;
-    /** Show starred and the frequent */
-    public static final int MODE_STREQUENT = 35 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_SHOW_CALL_BUTTON;
-    /** Show all contacts and pick them when clicking */
-    public static final int MODE_PICK_CONTACT = 40 | MODE_MASK_PICKER | MODE_MASK_SHOW_PHOTOS
-            | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all contacts as well as the option to create a new one */
-    public static final int MODE_PICK_OR_CREATE_CONTACT = 42 | MODE_MASK_PICKER | MODE_MASK_CREATE_NEW
-            | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all people through the legacy provider and pick them when clicking */
-    public static final int MODE_LEGACY_PICK_PERSON = 43 | MODE_MASK_PICKER
-            | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all people through the legacy provider as well as the option to create a new one */
-    public static final int MODE_LEGACY_PICK_OR_CREATE_PERSON = 44 | MODE_MASK_PICKER
-            | MODE_MASK_CREATE_NEW | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all contacts and pick them when clicking, and allow creating a new contact */
-    public static final int MODE_INSERT_OR_EDIT_CONTACT = 45 | MODE_MASK_PICKER | MODE_MASK_CREATE_NEW
-            | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all phone numbers and pick them when clicking */
-    public static final int MODE_PICK_PHONE = 50 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE;
-    /** Show all phone numbers through the legacy provider and pick them when clicking */
-    public static final int MODE_LEGACY_PICK_PHONE =
-            51 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    /** Show all postal addresses and pick them when clicking */
-    public static final int MODE_PICK_POSTAL =
-            55 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    /** Show all postal addresses and pick them when clicking */
-    public static final int MODE_LEGACY_PICK_POSTAL =
-            56 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    public static final int MODE_GROUP = 57 | MODE_MASK_SHOW_PHOTOS;
-    /** Run a search query */
-    public static final int MODE_QUERY = 60 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_NO_FILTER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-    /** Run a search query in PICK mode, but that still launches to VIEW */
-    public static final int MODE_QUERY_PICK_TO_VIEW = 65 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_PICKER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Run a search query in a PICK mode */
-    public static final int MODE_QUERY_PICK = 75 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_NO_FILTER
-            | MODE_MASK_PICKER | MODE_MASK_DISABLE_QUIKCCONTACT | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Run a search query in a PICK_PHONE mode */
-    public static final int MODE_QUERY_PICK_PHONE = 80 | MODE_MASK_NO_FILTER | MODE_MASK_PICKER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Run a search query in PICK mode, but that still launches to EDIT */
-    public static final int MODE_QUERY_PICK_TO_EDIT = 85 | MODE_MASK_NO_FILTER | MODE_MASK_SHOW_PHOTOS
-            | MODE_MASK_PICKER | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /**
-     * Show all phone numbers and do multiple pick when clicking. This mode has phone filtering
-     * feature, but doesn't support 'search for all contacts'.
-     */
-    public static final int MODE_PICK_MULTIPLE_PHONES = 80 | MODE_MASK_PICKER
-            | MODE_MASK_NO_PRESENCE | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-
-    /**
-     * An action used to do perform search while in a contact picker.  It is initiated
-     * by the ContactListActivity itself.
-     */
-    protected static final String ACTION_SEARCH_INTERNAL = "com.android.contacts.INTERNAL_SEARCH";
-
-    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
-        Contacts._ID,                       // 0
-        Contacts.DISPLAY_NAME_PRIMARY,      // 1
-        Contacts.DISPLAY_NAME_ALTERNATIVE,  // 2
-        Contacts.SORT_KEY_PRIMARY,          // 3
-        Contacts.STARRED,                   // 4
-        Contacts.TIMES_CONTACTED,           // 5
-        Contacts.CONTACT_PRESENCE,          // 6
-        Contacts.PHOTO_ID,                  // 7
-        Contacts.LOOKUP_KEY,                // 8
-        Contacts.PHONETIC_NAME,             // 9
-        Contacts.HAS_PHONE_NUMBER,          // 10
-    };
-    static final String[] CONTACTS_SUMMARY_PROJECTION_FROM_EMAIL = new String[] {
-        Contacts._ID,                       // 0
-        Contacts.DISPLAY_NAME_PRIMARY,      // 1
-        Contacts.DISPLAY_NAME_ALTERNATIVE,  // 2
-        Contacts.SORT_KEY_PRIMARY,          // 3
-        Contacts.STARRED,                   // 4
-        Contacts.TIMES_CONTACTED,           // 5
-        Contacts.CONTACT_PRESENCE,          // 6
-        Contacts.PHOTO_ID,                  // 7
-        Contacts.LOOKUP_KEY,                // 8
-        Contacts.PHONETIC_NAME,             // 9
-        // email lookup doesn't included HAS_PHONE_NUMBER in projection
-    };
-
-    static final String[] CONTACTS_SUMMARY_FILTER_PROJECTION = new String[] {
-        Contacts._ID,                       // 0
-        Contacts.DISPLAY_NAME_PRIMARY,      // 1
-        Contacts.DISPLAY_NAME_ALTERNATIVE,  // 2
-        Contacts.SORT_KEY_PRIMARY,          // 3
-        Contacts.STARRED,                   // 4
-        Contacts.TIMES_CONTACTED,           // 5
-        Contacts.CONTACT_PRESENCE,          // 6
-        Contacts.PHOTO_ID,                  // 7
-        Contacts.LOOKUP_KEY,                // 8
-        Contacts.PHONETIC_NAME,             // 9
-        Contacts.HAS_PHONE_NUMBER,          // 10
-        SearchSnippetColumns.SNIPPET_MIMETYPE, // 11
-        SearchSnippetColumns.SNIPPET_DATA1,     // 12
-        SearchSnippetColumns.SNIPPET_DATA4,     // 13
-    };
-
-    static final String[] LEGACY_PEOPLE_PROJECTION = new String[] {
-        People._ID,                         // 0
-        People.DISPLAY_NAME,                // 1
-        People.DISPLAY_NAME,                // 2
-        People.DISPLAY_NAME,                // 3
-        People.STARRED,                     // 4
-        PeopleColumns.TIMES_CONTACTED,      // 5
-        People.PRESENCE_STATUS,             // 6
-    };
-    public static final int SUMMARY_ID_COLUMN_INDEX = 0;
-    public static final int SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 1;
-    public static final int SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 2;
-    static final int SUMMARY_SORT_KEY_PRIMARY_COLUMN_INDEX = 3;
-    public static final int SUMMARY_STARRED_COLUMN_INDEX = 4;
-    static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 5;
-    public static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 6;
-    public static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 7;
-    public static final int SUMMARY_LOOKUP_KEY_COLUMN_INDEX = 8;
-    public static final int SUMMARY_PHONETIC_NAME_COLUMN_INDEX = 9;
-    public static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 10;
-    public static final int SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX = 11;
-    public static final int SUMMARY_SNIPPET_DATA1_COLUMN_INDEX = 12;
-    public static final int SUMMARY_SNIPPET_DATA4_COLUMN_INDEX = 13;
-
-    static final String[] PHONES_PROJECTION = new String[] {
-        Phone._ID, //0
-        Phone.TYPE, //1
-        Phone.LABEL, //2
-        Phone.NUMBER, //3
-        Phone.DISPLAY_NAME, // 4
-        Phone.CONTACT_ID, // 5
-        Contacts.SORT_KEY_PRIMARY, // 6
-        Contacts.PHOTO_ID, // 7
-    };
-    static final String[] LEGACY_PHONES_PROJECTION = new String[] {
-        Phones._ID, //0
-        Phones.TYPE, //1
-        Phones.LABEL, //2
-        Phones.NUMBER, //3
-        People.DISPLAY_NAME, // 4
-    };
-    public static final int PHONE_ID_COLUMN_INDEX = 0;
-    public static final int PHONE_TYPE_COLUMN_INDEX = 1;
-    public static final int PHONE_LABEL_COLUMN_INDEX = 2;
-    public static final int PHONE_NUMBER_COLUMN_INDEX = 3;
-    public static final int PHONE_DISPLAY_NAME_COLUMN_INDEX = 4;
-    public static final int PHONE_CONTACT_ID_COLUMN_INDEX = 5;
-    static final int PHONE_SORT_KEY_PRIMARY_COLUMN_INDEX = 6;
-    public static final int PHONE_PHOTO_ID_COLUMN_INDEX = 7;
-
-    static final String[] POSTALS_PROJECTION = new String[] {
-        StructuredPostal._ID, //0
-        StructuredPostal.TYPE, //1
-        StructuredPostal.LABEL, //2
-        StructuredPostal.DATA, //3
-        StructuredPostal.DISPLAY_NAME, // 4
-    };
-    static final String[] LEGACY_POSTALS_PROJECTION = new String[] {
-        ContactMethods._ID, //0
-        ContactMethods.TYPE, //1
-        ContactMethods.LABEL, //2
-        ContactMethods.DATA, //3
-        People.DISPLAY_NAME, // 4
-    };
     static final String[] RAW_CONTACTS_PROJECTION = new String[] {
         RawContacts._ID, //0
         RawContacts.CONTACT_ID, //1
         RawContacts.ACCOUNT_TYPE, //2
     };
 
-    static final int POSTAL_ID_COLUMN_INDEX = 0;
-    public static final int POSTAL_TYPE_COLUMN_INDEX = 1;
-    public static final int POSTAL_LABEL_COLUMN_INDEX = 2;
-    public static final int POSTAL_ADDRESS_COLUMN_INDEX = 3;
-    public static final int POSTAL_DISPLAY_NAME_COLUMN_INDEX = 4;
-
-    protected static final int QUERY_TOKEN = 42;
-
     static final String KEY_PICKER_MODE = "picker_mode";
 
-    public ContactEntryListAdapter mAdapter;
-    public ContactListEmptyView mEmptyView;
-
-
-    public int mMode = MODE_DEFAULT;
-    private boolean mRunQueriesSynchronously;
-    protected QueryHandler mQueryHandler;
-    Uri mSelectedContactUri;
-
-    public boolean mDisplayOnlyPhones;
-
-    private String mGroupName;
+    private Uri mSelectedContactUri;
 
     private ArrayList<Long> mWritableRawContactIds = new ArrayList<Long>();
     private int  mWritableSourcesCnt;
     private int  mReadOnlySourcesCnt;
 
-    public String mShortcutAction;
-
-    /**
-     * Internal query type when in mode {@link #MODE_QUERY_PICK_TO_VIEW}.
-     */
-    public int mQueryMode = QUERY_MODE_NONE;
-
-    public static final int QUERY_MODE_NONE = -1;
-    private static final int QUERY_MODE_MAILTO = 1;
-    private static final int QUERY_MODE_TEL = 2;
-
-    public boolean mSearchMode;
-    public boolean mSearchResultsMode;
-    public boolean mShowNumberOfContacts;
-
-    public boolean mShowSearchSnippets;
-    private boolean mSearchInitiated;
-
-    private String mInitialFilter;
-
-    protected static final String CLAUSE_ONLY_VISIBLE = Contacts.IN_VISIBLE_GROUP + "=1";
-    private static final String CLAUSE_ONLY_PHONES = Contacts.HAS_PHONE_NUMBER + "=1";
-
-
-    // Uri matcher for contact id
-    private static final int CONTACTS_ID = 1001;
-    private static final UriMatcher sContactsIdMatcher;
-
     final String[] sLookupProjection = new String[] {
             Contacts.LOOKUP_KEY
     };
-
-    static {
-        sContactsIdMatcher = new UriMatcher(UriMatcher.NO_MATCH);
-        sContactsIdMatcher.addURI(ContactsContract.AUTHORITY, "contacts/#", CONTACTS_ID);
-    }
-
     private class DeleteClickListener implements DialogInterface.OnClickListener {
         public void onClick(DialogInterface dialog, int which) {
             if (mSelectedContactUri != null) {
@@ -393,17 +118,17 @@
         }
     }
 
-    // The size of a home screen shortcut icon.
-    public int mDisplayOrder;
-    private int mSortOrder;
-
     private ContactsIntentResolver mIntentResolver;
-    protected ContactEntryListFragment mListFragment;
-
-    private ListView mListView;
+    protected ContactEntryListFragment<?> mListFragment;
 
     protected CallOrSmsInitiator mCallOrSmsInitiator;
 
+    private int mActionCode;
+
+    private boolean mSearchInitiated;
+
+    private ContactsRequest mRequest;
+
     public ContactsListActivity() {
         mIntentResolver = new ContactsIntentResolver(this);
     }
@@ -412,336 +137,278 @@
      * Visible for testing: makes queries run on the UI thread.
      */
     /* package */ void runQueriesSynchronously() {
-        mRunQueriesSynchronously = true;
+        // TODO
     }
 
     @Override
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
 
-        // Resolve the intent
-        final Intent intent = getIntent();
-        if (!resolveIntent(intent)) {
+        // Extract relevant information from the intent
+        mRequest = mIntentResolver.resolveIntent(getIntent());
+        if (!mRequest.isValid()) {
+            setResult(RESULT_CANCELED);
+            finish();
             return;
         }
 
+        Intent redirect = mRequest.getRedirectIntent();
+        if (redirect != null) {
+            // Need to start a different activity
+            startActivity(redirect);
+            finish();
+            return;
+        }
+
+        setTitle(mRequest.getActivityTitle());
+
+        onCreateFragment();
+
         FragmentTransaction transaction = openFragmentTransaction();
         transaction.add(mListFragment, android.R.id.content);
         transaction.commit();
     }
 
-    protected boolean resolveIntent(final Intent intent) {
-        mIntentResolver.setIntent(intent);
-
-        if (!mIntentResolver.isValid()) {           // Invalid intent
-            setResult(RESULT_CANCELED);
-            finish();
-            return false;
-        }
-
-        Intent redirect = mIntentResolver.getRedirectIntent();
-        if (redirect != null) {             // Need to start a different activity
-            startActivity(redirect);
-            finish();
-            return false;
-        }
-
-        setTitle(mIntentResolver.getActivityTitle());
-
-
-        // This is strictly temporary. Its purpose is to allow us to refactor this class in
-        // small increments.  We should expect all of these modes to go away.
-        mMode = mIntentResolver.mMode;
-        mGroupName = mIntentResolver.mGroupName;
-        mQueryMode = mIntentResolver.mQueryMode;
-        mSearchMode = mIntentResolver.mSearchMode;
-        mShowSearchSnippets = mIntentResolver.mShowSearchSnippets;
-        mInitialFilter = mIntentResolver.mInitialFilter;
-        mShortcutAction = mIntentResolver.mShortcutAction;
-        mSearchResultsMode = mIntentResolver.mSearchResultsMode;
-        mShowNumberOfContacts = mIntentResolver.mShowNumberOfContacts;
-        mGroupName = mIntentResolver.mGroupName;
-
-        switch (mMode) {
-            case MODE_DEFAULT:
-            case MODE_CUSTOM:
-            case MODE_INSERT_OR_EDIT_CONTACT:
-            case MODE_QUERY_PICK_TO_EDIT:
-            case MODE_QUERY: {
+    /**
+     * Creates the fragment based on the current request.
+     */
+    private void onCreateFragment() {
+        mActionCode = mRequest.getActionCode();
+        switch (mActionCode) {
+            case ContactsRequest.ACTION_DEFAULT:
+            case ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT: {
                 DefaultContactBrowseListFragment fragment = new DefaultContactBrowseListFragment();
-                if (!mSearchMode) {
-                    fragment.setSectionHeaderDisplayEnabled(true);
-                }
+                fragment.setOnContactListActionListener(new ContactBrowserActionListener());
 
-                if (mMode == MODE_INSERT_OR_EDIT_CONTACT ||
-                        mMode == MODE_QUERY_PICK_TO_EDIT) {
+                if (mActionCode == ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT) {
                     fragment.setEditMode(true);
-                }
-
-                if (mMode == MODE_INSERT_OR_EDIT_CONTACT) {
                     fragment.setCreateContactEnabled(true);
                 }
 
-                if (mMode == MODE_QUERY) {
-                    fragment.setSearchResultsMode(true);
-                }
+                fragment.setDisplayWithPhonesOnlyOption(mRequest.getDisplayWithPhonesOnlyOption());
 
-                fragment.setContactsWithPhonesOnlyRestrictionEnabled(
-                        mIntentResolver.isContactsWithPhonesOnlyRestrictionEnabled());
-                fragment.setVisibleContactsRestrictionEnabled(!mSearchResultsMode &&
-                        mIntentResolver.isVisibleContactsRestrictionEnabled());
+                fragment.setVisibleContactsRestrictionEnabled(
+                        !mRequest.isSearchResultsMode()
+                        && mRequest.getDisplayOnlyVisible());
 
-                fragment.setOnContactListActionListener(new OnContactBrowserActionListener() {
-                    public void onSearchAllContactsAction(String string) {
-                        doSearch();
-                    }
-
-                    public void onViewContactAction(Uri contactLookupUri) {
-                        startActivity(new Intent(Intent.ACTION_VIEW, contactLookupUri));
-                    }
-
-                    public void onCreateNewContactAction() {
-                        Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
-                        Bundle extras = getIntent().getExtras();
-                        if (extras != null) {
-                            intent.putExtras(extras);
-                        }
-                        startActivity(intent);
-                    }
-
-                    public void onEditContactAction(Uri contactLookupUri) {
-                        Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
-                        Bundle extras = getIntent().getExtras();
-                        if (extras != null) {
-                            intent.putExtras(extras);
-                        }
-                        startActivity(intent);
-                    }
-
-                    public void onAddToFavoritesAction(Uri contactUri) {
-                        ContentValues values = new ContentValues(1);
-                        values.put(Contacts.STARRED, 1);
-                        getContentResolver().update(contactUri, values, null, null);
-                    }
-
-                    public void onRemoveFromFavoritesAction(Uri contactUri) {
-                        ContentValues values = new ContentValues(1);
-                        values.put(Contacts.STARRED, 0);
-                        getContentResolver().update(contactUri, values, null, null);
-                    }
-
-                    public void onCallContactAction(Uri contactUri) {
-                        getCallOrSmsInitiator().initiateCall(contactUri);
-                    }
-
-                    public void onSmsContactAction(Uri contactUri) {
-                        getCallOrSmsInitiator().initiateSms(contactUri);
-                    }
-
-                    public void onDeleteContactAction(Uri contactUri) {
-                        doContactDelete(contactUri);
-                    }
-
-                    public void onFinishAction() {
-                        onBackPressed();
-                    }
-                });
                 fragment.setContextMenuAdapter(new ContactBrowseListContextMenuAdapter(fragment));
+                fragment.setSearchMode(mRequest.isSearchMode());
+                fragment.setSearchResultsMode(mRequest.isSearchResultsMode());
+                fragment.setQueryString(mRequest.getQueryString());
                 mListFragment = fragment;
                 break;
             }
-            case MODE_FREQUENT:
-            case MODE_STARRED:
-            case MODE_STREQUENT: {
+
+            case ContactsRequest.ACTION_GROUP: {
+                throw new UnsupportedOperationException("Not yet implemented");
+            }
+
+            case ContactsRequest.ACTION_STARRED: {
                 StrequentContactListFragment fragment = new StrequentContactListFragment();
-                if (mMode == MODE_FREQUENT) {
-                    fragment.setFrequentlyContactedContactsIncluded(true);
-                    fragment.setStarredContactsIncluded(false);
-                } else if (mMode == MODE_STARRED) {
-                    fragment.setFrequentlyContactedContactsIncluded(false);
-                    fragment.setStarredContactsIncluded(true);
-                } else {
-                    fragment.setFrequentlyContactedContactsIncluded(true);
-                    fragment.setStarredContactsIncluded(true);
-                }
-
-                fragment.setOnContactListActionListener(new OnContactBrowserActionListener() {
-                    public void onSearchAllContactsAction(String string) {
-                        doSearch();
-                    }
-
-                    public void onViewContactAction(Uri contactLookupUri) {
-                        startActivity(new Intent(Intent.ACTION_VIEW, contactLookupUri));
-                    }
-
-                    public void onCreateNewContactAction() {
-                        Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
-                        Bundle extras = getIntent().getExtras();
-                        if (extras != null) {
-                            intent.putExtras(extras);
-                        }
-                        startActivity(intent);
-                    }
-
-                    public void onEditContactAction(Uri contactLookupUri) {
-                        Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
-                        Bundle extras = getIntent().getExtras();
-                        if (extras != null) {
-                            intent.putExtras(extras);
-                        }
-                        startActivity(intent);
-                    }
-
-                    public void onAddToFavoritesAction(Uri contactUri) {
-                        ContentValues values = new ContentValues(1);
-                        values.put(Contacts.STARRED, 1);
-                        getContentResolver().update(contactUri, values, null, null);
-                    }
-
-                    public void onRemoveFromFavoritesAction(Uri contactUri) {
-                        ContentValues values = new ContentValues(1);
-                        values.put(Contacts.STARRED, 0);
-                        getContentResolver().update(contactUri, values, null, null);
-                    }
-
-                    public void onCallContactAction(Uri contactUri) {
-                        getCallOrSmsInitiator().initiateCall(contactUri);
-                    }
-
-                    public void onSmsContactAction(Uri contactUri) {
-                        getCallOrSmsInitiator().initiateSms(contactUri);
-                    }
-
-                    public void onDeleteContactAction(Uri contactUri) {
-                        doContactDelete(contactUri);
-                    }
-
-                    public void onFinishAction() {
-                        onBackPressed();
-                    }
-                });
-                fragment.setContextMenuAdapter(new ContactBrowseListContextMenuAdapter(fragment));
+                fragment.setOnContactListActionListener(new ContactBrowserActionListener());
+                fragment.setFrequentlyContactedContactsIncluded(false);
+                fragment.setStarredContactsIncluded(true);
                 mListFragment = fragment;
                 break;
             }
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON:
-            case MODE_PICK_CONTACT:
-            case MODE_PICK_OR_CREATE_CONTACT: {
+
+            case ContactsRequest.ACTION_FREQUENT: {
+                StrequentContactListFragment fragment = new StrequentContactListFragment();
+                fragment.setOnContactListActionListener(new ContactBrowserActionListener());
+                fragment.setFrequentlyContactedContactsIncluded(true);
+                fragment.setStarredContactsIncluded(false);
+                mListFragment = fragment;
+                break;
+            }
+
+            case ContactsRequest.ACTION_STREQUENT: {
+                StrequentContactListFragment fragment = new StrequentContactListFragment();
+                fragment.setOnContactListActionListener(new ContactBrowserActionListener());
+                fragment.setFrequentlyContactedContactsIncluded(true);
+                fragment.setStarredContactsIncluded(true);
+                mListFragment = fragment;
+                break;
+            }
+
+            case ContactsRequest.ACTION_PICK_CONTACT: {
                 ContactPickerFragment fragment = new ContactPickerFragment();
-                if (!mSearchMode) {
-                    fragment.setSectionHeaderDisplayEnabled(true);
-                }
-
-                if (mMode == MODE_PICK_OR_CREATE_CONTACT
-                        || mMode == MODE_LEGACY_PICK_OR_CREATE_PERSON) {
-                    fragment.setCreateContactEnabled(true);
-                }
-
-                if (mMode == MODE_LEGACY_PICK_PERSON ||
-                        mMode == MODE_LEGACY_PICK_OR_CREATE_PERSON) {
-                    fragment.setLegacyCompatibility(true);
-                }
-
-                fragment.setShortcutRequested(mShortcutAction != null);
-
-                fragment.setOnContactPickerActionListener(new OnContactPickerActionListener() {
-                    public void onSearchAllContactsAction(String string) {
-                        doSearch();
-                    }
-
-                    public void onCreateNewContactAction() {
-                        Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
-                        startActivityAndForwardResult(intent);
-                    }
-
-                    public void onPickContactAction(Uri contactUri) {
-                        Intent intent = new Intent();
-                        setResult(RESULT_OK, intent.setData(contactUri));
-                        finish();
-                    }
-
-                    public void onShortcutIntentCreated(Intent intent) {
-                        setResult(RESULT_OK, intent);
-                        finish();
-                    }
-
-                    // TODO: finish action to support search in the picker
-                });
-
+                fragment.setOnContactPickerActionListener(new ContactPickerActionListener());
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
                 mListFragment = fragment;
                 break;
             }
-            case MODE_LEGACY_PICK_PHONE:
-            case MODE_PICK_PHONE: {
+
+            case ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT: {
+                ContactPickerFragment fragment = new ContactPickerFragment();
+                fragment.setOnContactPickerActionListener(new ContactPickerActionListener());
+                fragment.setCreateContactEnabled(true);
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
+                mListFragment = fragment;
+                break;
+            }
+
+            case ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT: {
+                ContactPickerFragment fragment = new ContactPickerFragment();
+                fragment.setOnContactPickerActionListener(new ContactPickerActionListener());
+                fragment.setCreateContactEnabled(true);
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
+                fragment.setShortcutRequested(true);
+                mListFragment = fragment;
+                break;
+            }
+
+            case ContactsRequest.ACTION_PICK_PHONE: {
                 PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
-                if (mMode == MODE_LEGACY_PICK_PHONE) {
-                    fragment.setLegacyCompatibility(true);
-                }
-                fragment.setSectionHeaderDisplayEnabled(false);
-                fragment.setShortcutAction(mShortcutAction);
                 fragment.setOnPhoneNumberPickerActionListener(
-                        new OnPhoneNumberPickerActionListener() {
-
-                    public void onPickPhoneNumberAction(Uri dataUri) {
-                        Intent intent = new Intent();
-                        setResult(RESULT_OK, intent.setData(dataUri));
-                        finish();
-                    }
-
-                    public void onSearchAllContactsAction(String string) {
-                        doSearch();
-                    }
-
-                    public void onShortcutIntentCreated(Intent intent) {
-                        setResult(RESULT_OK, intent);
-                        finish();
-                    }
-                });
+                        new PhoneNumberPickerActionListener());
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
                 mListFragment = fragment;
                 break;
             }
-            case MODE_LEGACY_PICK_POSTAL:
-            case MODE_PICK_POSTAL: {
-                PostalAddressPickerFragment fragment = new PostalAddressPickerFragment();
-                if (mMode == MODE_LEGACY_PICK_POSTAL) {
-                    fragment.setLegacyCompatibility(true);
-                }
+
+            case ContactsRequest.ACTION_CREATE_SHORTCUT_CALL: {
+                PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
+                fragment.setOnPhoneNumberPickerActionListener(
+                        new PhoneNumberPickerActionListener());
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
                 fragment.setSectionHeaderDisplayEnabled(false);
-                fragment.setOnPostalAddressPickerActionListener(
-                        new OnPostalAddressPickerActionListener() {
-
-                    public void onPickPostalAddressAction(Uri dataUri) {
-                        Intent intent = new Intent();
-                        setResult(RESULT_OK, intent.setData(dataUri));
-                        finish();
-                    }
-
-                    public void onSearchAllContactsAction(String string) {
-                        doSearch();
-                    }
-                });
+                fragment.setShortcutAction(Intent.ACTION_CALL);
                 mListFragment = fragment;
                 break;
             }
-            case MODE_PICK_MULTIPLE_PHONES: {
-                mListFragment = new MultiplePhonePickerFragment();
+
+            case ContactsRequest.ACTION_CREATE_SHORTCUT_SMS: {
+                PhoneNumberPickerFragment fragment = new PhoneNumberPickerFragment();
+                fragment.setOnPhoneNumberPickerActionListener(
+                        new PhoneNumberPickerActionListener());
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
+                fragment.setSectionHeaderDisplayEnabled(false);
+                fragment.setShortcutAction(Intent.ACTION_SENDTO);
+                mListFragment = fragment;
                 break;
             }
-            default: {
-                throw new UnsupportedOperationException();
+
+            case ContactsRequest.ACTION_PICK_POSTAL: {
+                PostalAddressPickerFragment fragment = new PostalAddressPickerFragment();
+                fragment.setOnPostalAddressPickerActionListener(
+                        new PostalAddressPickerActionListener());
+                fragment.setLegacyCompatibilityMode(mRequest.isLegacyCompatibilityMode());
+                mListFragment = fragment;
+                break;
             }
+
+            default:
+                throw new IllegalStateException("Invalid action code: " + mActionCode);
+        }
+    }
+
+    private final class ContactBrowserActionListener implements OnContactBrowserActionListener {
+        public void onSearchAllContactsAction(String queryString) {
+            searchAllContacts(queryString, false);
         }
 
-        mListFragment.setSearchMode(mSearchMode);
-        mListFragment.setSearchResultsMode(mSearchResultsMode);
-        mListFragment.setQueryString(mInitialFilter);
-
-        if ((mMode & MODE_MASK_SHOW_PHOTOS) == MODE_MASK_SHOW_PHOTOS) {
-            mListFragment.setPhotoLoaderEnabled(true);
+        public void onViewContactAction(Uri contactLookupUri) {
+            startActivity(new Intent(Intent.ACTION_VIEW, contactLookupUri));
         }
 
-        return true;
+        public void onCreateNewContactAction() {
+            Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
+            Bundle extras = getIntent().getExtras();
+            if (extras != null) {
+                intent.putExtras(extras);
+            }
+            startActivity(intent);
+        }
+
+        public void onEditContactAction(Uri contactLookupUri) {
+            Intent intent = new Intent(Intent.ACTION_EDIT, contactLookupUri);
+            Bundle extras = getIntent().getExtras();
+            if (extras != null) {
+                intent.putExtras(extras);
+            }
+            startActivity(intent);
+        }
+
+        public void onAddToFavoritesAction(Uri contactUri) {
+            ContentValues values = new ContentValues(1);
+            values.put(Contacts.STARRED, 1);
+            getContentResolver().update(contactUri, values, null, null);
+        }
+
+        public void onRemoveFromFavoritesAction(Uri contactUri) {
+            ContentValues values = new ContentValues(1);
+            values.put(Contacts.STARRED, 0);
+            getContentResolver().update(contactUri, values, null, null);
+        }
+
+        public void onCallContactAction(Uri contactUri) {
+            getCallOrSmsInitiator().initiateCall(contactUri);
+        }
+
+        public void onSmsContactAction(Uri contactUri) {
+            getCallOrSmsInitiator().initiateSms(contactUri);
+        }
+
+        public void onDeleteContactAction(Uri contactUri) {
+            doContactDelete(contactUri);
+        }
+
+        public void onFinishAction() {
+            onBackPressed();
+        }
+    }
+
+    private final class ContactPickerActionListener implements OnContactPickerActionListener {
+        public void onSearchAllContactsAction(String queryString) {
+            searchAllContacts(queryString, true);
+        }
+
+        public void onCreateNewContactAction() {
+            Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
+            startActivityAndForwardResult(intent);
+        }
+
+        public void onPickContactAction(Uri contactUri) {
+            Intent intent = new Intent();
+            setResult(RESULT_OK, intent.setData(contactUri));
+            finish();
+        }
+
+        public void onShortcutIntentCreated(Intent intent) {
+            setResult(RESULT_OK, intent);
+            finish();
+        }
+    }
+
+    private final class PhoneNumberPickerActionListener implements
+            OnPhoneNumberPickerActionListener {
+        public void onSearchAllContactsAction(String queryString) {
+            searchAllContacts(queryString, true);
+        }
+
+        public void onPickPhoneNumberAction(Uri dataUri) {
+            Intent intent = new Intent();
+            setResult(RESULT_OK, intent.setData(dataUri));
+            finish();
+        }
+
+        public void onShortcutIntentCreated(Intent intent) {
+            setResult(RESULT_OK, intent);
+            finish();
+        }
+    }
+
+    private final class PostalAddressPickerActionListener implements
+            OnPostalAddressPickerActionListener {
+        public void onSearchAllContactsAction(String queryString) {
+            searchAllContacts(queryString, true);
+        }
+
+        public void onPickPostalAddressAction(Uri dataUri) {
+            Intent intent = new Intent();
+            setResult(RESULT_OK, intent.setData(dataUri));
+            finish();
+        }
     }
 
     public void startActivityAndForwardResult(final Intent intent) {
@@ -756,44 +423,23 @@
         finish();
     }
 
-    public int getSummaryDisplayNameColumnIndex() {
-        if (mDisplayOrder == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
-            return SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
-        } else {
-            return SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
-        }
-    }
-
-    @Override
-    protected void onStop() {
-        super.onStop();
-
-        if (mMode == MODE_QUERY) {
-            // Make sure the search box is closed
-            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
-            searchManager.stopSearch();
-        }
-    }
-
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         super.onCreateOptionsMenu(menu);
 
-        // If Contacts was invoked by another Activity simply as a way of
-        // picking a contact, don't show the options menu
-        if ((mMode & MODE_MASK_PICKER) == MODE_MASK_PICKER) {
-            return false;
+        // TODO what other actions need the menu?
+        if (mActionCode == ContactsRequest.ACTION_DEFAULT ||
+                mActionCode == ContactsRequest.ACTION_STREQUENT) {
+            MenuInflater inflater = getMenuInflater();
+            inflater.inflate(R.menu.list, menu);
         }
-
-        MenuInflater inflater = getMenuInflater();
-        inflater.inflate(R.menu.list, menu);
         return true;
     }
 
     @Override
     public boolean onPrepareOptionsMenu(Menu menu) {
-        final boolean defaultMode = (mMode == MODE_DEFAULT);
-        menu.findItem(R.id.menu_display_groups).setVisible(defaultMode);
+        menu.findItem(R.id.menu_display_groups).setVisible(
+                mActionCode == ContactsRequest.ACTION_DEFAULT);
         return true;
     }
 
@@ -841,62 +487,34 @@
         if (globalSearch) {
             super.startSearch(initialQuery, selectInitialQuery, appSearchData, globalSearch);
         } else {
-            if (!mSearchMode && (mMode & MODE_MASK_NO_FILTER) == 0) {
-                if ((mMode & MODE_MASK_PICKER) != 0) {
-                    ContactsSearchManager.startSearchForResult(this, initialQuery,
-                            SUBACTIVITY_FILTER, null);
-                } else {
-                    ContactsSearchManager.startSearch(this, initialQuery);
-                }
-            }
+//            if (!mSearchMode && (mMode & MODE_MASK_NO_FILTER) == 0) {
+//                if ((mMode & MODE_MASK_PICKER) != 0) {
+//                    ContactsSearchManager.startSearchForResult(this, initialQuery,
+//                            SUBACTIVITY_FILTER, null);
+//                } else {
+                    ContactsSearchManager.startSearch(this, initialQuery, mRequest);
+//                }
+//            }
         }
     }
 
     /**
-     * Performs filtering of the list based on the search query entered in the
-     * search text edit.
-     */
-    protected void onSearchTextChanged() {
-    }
-
-    /**
      * Starts a new activity that will run a search query and display search results.
      */
-    protected void doSearch() {
+    protected void searchAllContacts(String queryString, boolean returnResult) {
         String query = mListFragment.getQueryString();
         if (TextUtils.isEmpty(query)) {
             return;
         }
 
         Intent intent = new Intent(this, SearchResultsActivity.class);
-        Intent originalIntent = getIntent();
-        Bundle originalExtras = originalIntent.getExtras();
-        if (originalExtras != null) {
-            intent.putExtras(originalExtras);
-        }
-
+        intent.setAction(Intent.ACTION_SEARCH);
         intent.putExtra(SearchManager.QUERY, query);
-        if ((mMode & MODE_MASK_PICKER) != 0) {
-            intent.setAction(ACTION_SEARCH_INTERNAL);
-            intent.putExtra(SHORTCUT_ACTION_KEY, mShortcutAction);
-            if (mShortcutAction != null) {
-                if (Intent.ACTION_CALL.equals(mShortcutAction)
-                        || Intent.ACTION_SENDTO.equals(mShortcutAction)) {
-                    intent.putExtra(Insert.PHONE, query);
-                }
-            } else {
-                switch (mQueryMode) {
-                    case QUERY_MODE_MAILTO:
-                        intent.putExtra(Insert.EMAIL, query);
-                        break;
-                    case QUERY_MODE_TEL:
-                        intent.putExtra(Insert.PHONE, query);
-                        break;
-                }
-            }
+        intent.putExtra(ContactsSearchManager.ORIGINAL_REQUEST_KEY, mRequest);
+
+        if (returnResult) {
             startActivityForResult(intent, SUBACTIVITY_SEARCH);
         } else {
-            intent.setAction(Intent.ACTION_SEARCH);
             startActivity(intent);
         }
     }
@@ -1034,7 +652,7 @@
 
     private void doShareVisibleContacts() {
         final Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
-                sLookupProjection, getContactSelection(), null, null);
+                sLookupProjection, Contacts.IN_VISIBLE_GROUP + "!=0", null, null);
         try {
             if (!cursor.moveToFirst()) {
                 Toast.makeText(this, R.string.share_error, Toast.LENGTH_SHORT).show();
@@ -1078,41 +696,41 @@
         AccountSelectionUtil.doImport(this, resId, (size == 1 ? accountList.get(0) : null));
     }
 
-    @Override
-    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-        switch (requestCode) {
-            case SUBACTIVITY_NEW_CONTACT:
-                if (resultCode == RESULT_OK) {
-                    returnPickerResult(null, data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME),
-                            data.getData());
-                }
-                break;
-
-            case SUBACTIVITY_VIEW_CONTACT:
-                if (resultCode == RESULT_OK) {
-                    mAdapter.notifyDataSetChanged();
-                }
-                break;
-
-            case SUBACTIVITY_DISPLAY_GROUP:
-                // Mark as just created so we re-run the view query
-//                mJustCreated = true;
-                break;
-
-            case SUBACTIVITY_FILTER:
-            case SUBACTIVITY_SEARCH:
-                // Pass through results of filter or search UI
-                if (resultCode == RESULT_OK) {
-                    setResult(RESULT_OK, data);
-                    finish();
-                } else if (resultCode == RESULT_CANCELED && mMode == MODE_PICK_MULTIPLE_PHONES) {
-                    // Finish the activity if the sub activity was canceled as back key is used
-                    // to confirm user selection in MODE_PICK_MULTIPLE_PHONES.
-                    finish();
-                }
-                break;
-        }
-    }
+//    @Override
+//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
+//        switch (requestCode) {
+//            case SUBACTIVITY_NEW_CONTACT:
+//                if (resultCode == RESULT_OK) {
+////                    returnPickerResult(null, data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME),
+////                            data.getData());
+//                }
+//                break;
+//
+//            case SUBACTIVITY_VIEW_CONTACT:
+//                if (resultCode == RESULT_OK) {
+//                    mAdapter.notifyDataSetChanged();
+//                }
+//                break;
+//
+//            case SUBACTIVITY_DISPLAY_GROUP:
+//                // Mark as just created so we re-run the view query
+////                mJustCreated = true;
+//                break;
+//
+//            case SUBACTIVITY_FILTER:
+//            case SUBACTIVITY_SEARCH:
+//                // Pass through results of filter or search UI
+//                if (resultCode == RESULT_OK) {
+//                    setResult(RESULT_OK, data);
+//                    finish();
+//                } else if (resultCode == RESULT_CANCELED && mMode == MODE_PICK_MULTIPLE_PHONES) {
+//                    // Finish the activity if the sub activity was canceled as back key is used
+//                    // to confirm user selection in MODE_PICK_MULTIPLE_PHONES.
+//                    finish();
+//                }
+//                break;
+//        }
+//    }
 
     @Override
     public boolean onContextItemSelected(MenuItem item) {
@@ -1130,7 +748,8 @@
      */
     @Override
     public boolean dispatchKeyEvent(KeyEvent event) {
-        if (!mSearchMode && (mMode & MODE_MASK_NO_FILTER) == 0 && !mSearchInitiated) {
+        if (!mSearchInitiated && !mRequest.isSearchMode()
+                && !mRequest.isSearchResultsMode()) {
             int unicodeChar = event.getUnicodeChar();
             if (unicodeChar != 0) {
                 mSearchInitiated = true;
@@ -1143,13 +762,14 @@
 
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
+        // TODO move to the fragment
         switch (keyCode) {
-            case KeyEvent.KEYCODE_CALL: {
-                if (callSelection()) {
-                    return true;
-                }
-                break;
-            }
+//            case KeyEvent.KEYCODE_CALL: {
+//                if (callSelection()) {
+//                    return true;
+//                }
+//                break;
+//            }
 
             case KeyEvent.KEYCODE_DEL: {
                 if (deleteSelection()) {
@@ -1163,18 +783,17 @@
     }
 
     private boolean deleteSelection() {
-        if ((mMode & MODE_MASK_PICKER) != 0) {
-            return false;
-        }
-
-        final int position = mListView.getSelectedItemPosition();
-        if (position != ListView.INVALID_POSITION) {
-            Uri contactUri = getContactUri(position);
-            if (contactUri != null) {
-                doContactDelete(contactUri);
-                return true;
-            }
-        }
+        // TODO move to the fragment
+//        if (mActionCode == ContactsRequest.ACTION_DEFAULT) {
+//            final int position = mListView.getSelectedItemPosition();
+//            if (position != ListView.INVALID_POSITION) {
+//                Uri contactUri = getContactUri(position);
+//                if (contactUri != null) {
+//                    doContactDelete(contactUri);
+//                    return true;
+//                }
+//            }
+//        }
         return false;
     }
 
@@ -1221,657 +840,6 @@
         }
     }
 
-    public void onListItemClick(int position, long id) {
-        if (mSearchMode &&
-                ((ContactItemListAdapter)(mAdapter)).isSearchAllContactsItemPosition(position)) {
-            doSearch();
-        } else if (mMode == MODE_INSERT_OR_EDIT_CONTACT || mMode == MODE_QUERY_PICK_TO_EDIT) {
-            Intent intent;
-            if (position == 0 && !mSearchMode && mMode != MODE_QUERY_PICK_TO_EDIT) {
-                intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
-            } else {
-                intent = new Intent(Intent.ACTION_EDIT, getSelectedUri(position));
-            }
-            intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
-            Bundle extras = getIntent().getExtras();
-            if (extras != null) {
-                intent.putExtras(extras);
-            }
-            intent.putExtra(KEY_PICKER_MODE, (mMode & MODE_MASK_PICKER) == MODE_MASK_PICKER);
-
-            startActivity(intent);
-            finish();
-        } else if ((mMode & MODE_MASK_CREATE_NEW) == MODE_MASK_CREATE_NEW
-                && position == 0) {
-            Intent newContact = new Intent(Intents.Insert.ACTION, Contacts.CONTENT_URI);
-            startActivityForResult(newContact, SUBACTIVITY_NEW_CONTACT);
-        } else if (id > 0) {
-            final Uri uri = getSelectedUri(position);
-            if ((mMode & MODE_MASK_PICKER) == 0) {
-                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-                startActivityForResult(intent, SUBACTIVITY_VIEW_CONTACT);
-            } else if (mMode == MODE_QUERY_PICK_TO_VIEW) {
-                // Started with query that should launch to view contact
-                final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
-                startActivity(intent);
-                finish();
-            } else if (mMode == MODE_PICK_PHONE || mMode == MODE_QUERY_PICK_PHONE) {
-                Cursor c = (Cursor) mAdapter.getItem(position);
-                returnPickerResult(c, c.getString(PHONE_DISPLAY_NAME_COLUMN_INDEX), uri);
-            } else if ((mMode & MODE_MASK_PICKER) != 0) {
-                Cursor c = (Cursor) mAdapter.getItem(position);
-                returnPickerResult(c, c.getString(getSummaryDisplayNameColumnIndex()), uri);
-            } else if (mMode == MODE_PICK_POSTAL
-                    || mMode == MODE_LEGACY_PICK_POSTAL
-                    || mMode == MODE_LEGACY_PICK_PHONE) {
-                returnPickerResult(null, null, uri);
-            }
-        } else {
-            signalError();
-        }
-    }
-
-    @Deprecated
-    private void returnPickerResult(Cursor c, String string, Uri uri) {
-    }
-
-    protected Uri getUriToQuery() {
-        switch(mMode) {
-            case MODE_FREQUENT:
-            case MODE_STARRED:
-                return Contacts.CONTENT_URI;
-
-            case MODE_DEFAULT:
-            case MODE_CUSTOM:
-            case MODE_INSERT_OR_EDIT_CONTACT:
-            case MODE_PICK_CONTACT:
-            case MODE_PICK_OR_CREATE_CONTACT:{
-                return CONTACTS_CONTENT_URI_WITH_LETTER_COUNTS;
-            }
-            case MODE_STREQUENT: {
-                return Contacts.CONTENT_STREQUENT_URI;
-            }
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                return People.CONTENT_URI;
-            }
-            case MODE_PICK_MULTIPLE_PHONES:
-            case MODE_PICK_PHONE: {
-                return buildSectionIndexerUri(Phone.CONTENT_URI);
-            }
-            case MODE_LEGACY_PICK_PHONE: {
-                return Phones.CONTENT_URI;
-            }
-            case MODE_PICK_POSTAL: {
-                return buildSectionIndexerUri(StructuredPostal.CONTENT_URI);
-            }
-            case MODE_LEGACY_PICK_POSTAL: {
-                return ContactMethods.CONTENT_URI;
-            }
-            case MODE_QUERY_PICK_TO_VIEW: {
-                if (mQueryMode == QUERY_MODE_MAILTO) {
-                    return Uri.withAppendedPath(Email.CONTENT_FILTER_URI,
-                            Uri.encode(mInitialFilter));
-                } else if (mQueryMode == QUERY_MODE_TEL) {
-                    return Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,
-                            Uri.encode(mInitialFilter));
-                }
-                return CONTACTS_CONTENT_URI_WITH_LETTER_COUNTS;
-            }
-            case MODE_QUERY:
-            case MODE_QUERY_PICK:
-            case MODE_QUERY_PICK_TO_EDIT: {
-                return getContactFilterUri(mInitialFilter);
-            }
-            case MODE_QUERY_PICK_PHONE: {
-                return Uri.withAppendedPath(Phone.CONTENT_FILTER_URI,
-                        Uri.encode(mInitialFilter));
-            }
-            case MODE_GROUP: {
-                return Uri.withAppendedPath(Contacts.CONTENT_GROUP_URI, mGroupName);
-            }
-            default: {
-                throw new IllegalStateException("Can't generate URI: Unsupported Mode.");
-            }
-        }
-    }
-
-    /**
-     * Build the {@link Contacts#CONTENT_LOOKUP_URI} for the given
-     * {@link ListView} position, using {@link #mAdapter}.
-     */
-    private Uri getContactUri(int position) {
-        if (position == ListView.INVALID_POSITION) {
-            throw new IllegalArgumentException("Position not in list bounds");
-        }
-
-        final Cursor cursor = (Cursor)mAdapter.getItem(position);
-        if (cursor == null) {
-            return null;
-        }
-
-        switch(mMode) {
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                final long personId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
-                return ContentUris.withAppendedId(People.CONTENT_URI, personId);
-            }
-
-            default: {
-                // Build and return soft, lookup reference
-                final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
-                final String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY_COLUMN_INDEX);
-                return Contacts.getLookupUri(contactId, lookupKey);
-            }
-        }
-    }
-
-    /**
-     * Build the {@link Uri} for the given {@link ListView} position, which can
-     * be used as result when in {@link #MODE_MASK_PICKER} mode.
-     */
-    protected Uri getSelectedUri(int position) {
-        if (position == ListView.INVALID_POSITION) {
-            throw new IllegalArgumentException("Position not in list bounds");
-        }
-
-        final long id = mAdapter.getItemId(position);
-        switch(mMode) {
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                return ContentUris.withAppendedId(People.CONTENT_URI, id);
-            }
-            case MODE_PICK_PHONE:
-            case MODE_QUERY_PICK_PHONE: {
-                return ContentUris.withAppendedId(Data.CONTENT_URI, id);
-            }
-            case MODE_LEGACY_PICK_PHONE: {
-                return ContentUris.withAppendedId(Phones.CONTENT_URI, id);
-            }
-            case MODE_PICK_POSTAL: {
-                return ContentUris.withAppendedId(Data.CONTENT_URI, id);
-            }
-            case MODE_LEGACY_PICK_POSTAL: {
-                return ContentUris.withAppendedId(ContactMethods.CONTENT_URI, id);
-            }
-            default: {
-                return getContactUri(position);
-            }
-        }
-    }
-
-    String[] getProjectionForQuery() {
-        switch(mMode) {
-            case MODE_STREQUENT:
-            case MODE_FREQUENT:
-            case MODE_STARRED:
-            case MODE_DEFAULT:
-            case MODE_CUSTOM:
-            case MODE_INSERT_OR_EDIT_CONTACT:
-            case MODE_GROUP:
-            case MODE_PICK_CONTACT:
-            case MODE_PICK_OR_CREATE_CONTACT: {
-                return mSearchMode
-                        ? CONTACTS_SUMMARY_FILTER_PROJECTION
-                        : CONTACTS_SUMMARY_PROJECTION;
-            }
-            case MODE_QUERY:
-            case MODE_QUERY_PICK:
-            case MODE_QUERY_PICK_TO_EDIT: {
-                return CONTACTS_SUMMARY_FILTER_PROJECTION;
-            }
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                return LEGACY_PEOPLE_PROJECTION ;
-            }
-            case MODE_QUERY_PICK_PHONE:
-            case MODE_PICK_MULTIPLE_PHONES:
-            case MODE_PICK_PHONE: {
-                return PHONES_PROJECTION;
-            }
-            case MODE_LEGACY_PICK_PHONE: {
-                return LEGACY_PHONES_PROJECTION;
-            }
-            case MODE_PICK_POSTAL: {
-                return POSTALS_PROJECTION;
-            }
-            case MODE_LEGACY_PICK_POSTAL: {
-                return LEGACY_POSTALS_PROJECTION;
-            }
-            case MODE_QUERY_PICK_TO_VIEW: {
-                if (mQueryMode == QUERY_MODE_MAILTO) {
-                    return CONTACTS_SUMMARY_PROJECTION_FROM_EMAIL;
-                } else if (mQueryMode == QUERY_MODE_TEL) {
-                    return PHONES_PROJECTION;
-                }
-                break;
-            }
-        }
-
-        // Default to normal aggregate projection
-        return CONTACTS_SUMMARY_PROJECTION;
-    }
-
-    /**
-     * Return the selection arguments for a default query based on the
-     * {@link #mDisplayOnlyPhones} flag.
-     */
-    private String getContactSelection() {
-//        if (mDisplayOnlyPhones) {
-//            return CLAUSE_ONLY_VISIBLE + " AND " + CLAUSE_ONLY_PHONES;
-//        } else {
-            return CLAUSE_ONLY_VISIBLE;
-//        }
-    }
-
-    protected Uri getContactFilterUri(String filter) {
-        Uri baseUri;
-        if (!TextUtils.isEmpty(filter)) {
-            baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI, Uri.encode(filter));
-        } else {
-            baseUri = Contacts.CONTENT_URI;
-        }
-
-        if (mListFragment.isSectionHeaderDisplayEnabled()) {
-            return buildSectionIndexerUri(baseUri);
-        } else {
-            return baseUri;
-        }
-    }
-
-    private Uri getPeopleFilterUri(String filter) {
-        if (!TextUtils.isEmpty(filter)) {
-            return Uri.withAppendedPath(People.CONTENT_FILTER_URI, Uri.encode(filter));
-        } else {
-            return People.CONTENT_URI;
-        }
-    }
-
-    private static Uri buildSectionIndexerUri(Uri uri) {
-        return uri.buildUpon()
-                .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true").build();
-    }
-
-
-    protected String getSortOrder(String[] projectionType) {
-        String sortKey;
-        if (mSortOrder == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
-            sortKey = Contacts.SORT_KEY_PRIMARY;
-        } else {
-            sortKey = Contacts.SORT_KEY_ALTERNATIVE;
-        }
-        switch (mMode) {
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON:
-                sortKey = Contacts.DISPLAY_NAME;
-                break;
-            case MODE_LEGACY_PICK_PHONE:
-                sortKey = People.DISPLAY_NAME;
-                break;
-        }
-        return sortKey;
-    }
-
-    public void startQuery() {
-
-        // Disabled
-        if (true) {
-            return;
-        }
-
-        if (mSearchResultsMode) {
-            TextView foundContactsText = (TextView)findViewById(R.id.search_results_found);
-            foundContactsText.setText(R.string.search_results_searching);
-        }
-
-        if (mEmptyView != null) {
-            mEmptyView.hide();
-        }
-
-        // TODO reintroduce the loading state handling
-//        mAdapter.setLoading(true);
-
-        // Cancel any pending queries
-        mQueryHandler.cancelOperation(QUERY_TOKEN);
-
-        String[] projection = getProjectionForQuery();
-        if (mSearchMode && TextUtils.isEmpty(mListFragment.getQueryString())) {
-            mAdapter.changeCursor(new MatrixCursor(projection));
-            return;
-        }
-
-        String callingPackage = getCallingPackage();
-        Uri uri = getUriToQuery();
-        if (!TextUtils.isEmpty(callingPackage)) {
-            uri = uri.buildUpon()
-                    .appendQueryParameter(ContactsContract.REQUESTING_PACKAGE_PARAM_KEY,
-                            callingPackage)
-                    .build();
-        }
-
-        startQuery(uri, projection);
-    }
-
-    protected void startQuery(Uri uri, String[] projection) {
-        // Kick off the new query
-        switch (mMode) {
-            case MODE_GROUP:
-            case MODE_DEFAULT:
-            case MODE_CUSTOM:
-            case MODE_PICK_CONTACT:
-            case MODE_PICK_OR_CREATE_CONTACT:
-            case MODE_INSERT_OR_EDIT_CONTACT:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, getContactSelection(),
-                        null, getSortOrder(projection));
-                break;
-
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, null, null,
-                        People.DISPLAY_NAME);
-                break;
-            }
-            case MODE_PICK_POSTAL:
-            case MODE_QUERY:
-            case MODE_QUERY_PICK:
-            case MODE_QUERY_PICK_PHONE:
-            case MODE_QUERY_PICK_TO_VIEW:
-            case MODE_QUERY_PICK_TO_EDIT: {
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, null, null,
-                        getSortOrder(projection));
-                break;
-            }
-
-            case MODE_STARRED:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
-                        projection, Contacts.STARRED + "=1", null,
-                        getSortOrder(projection));
-                break;
-
-            case MODE_FREQUENT:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
-                        projection,
-                        Contacts.TIMES_CONTACTED + " > 0", null,
-                        Contacts.TIMES_CONTACTED + " DESC, "
-                        + getSortOrder(projection));
-                break;
-
-            case MODE_STREQUENT:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, null, null, null);
-                break;
-
-            case MODE_PICK_PHONE:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
-                        projection, CLAUSE_ONLY_VISIBLE, null, getSortOrder(projection));
-                break;
-
-            case MODE_LEGACY_PICK_PHONE:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
-                        projection, null, null, Phones.DISPLAY_NAME);
-                break;
-
-            case MODE_LEGACY_PICK_POSTAL:
-                mQueryHandler.startQuery(QUERY_TOKEN, null, uri,
-                        projection,
-                        ContactMethods.KIND + "=" + android.provider.Contacts.KIND_POSTAL, null,
-                        ContactMethods.DISPLAY_NAME);
-                break;
-        }
-    }
-
-    protected void startQuery(Uri uri, String[] projection, String selection,
-            String[] selectionArgs, String sortOrder) {
-        mQueryHandler.startQuery(QUERY_TOKEN, null, uri, projection, selection, selectionArgs,
-                sortOrder);
-    }
-
-    /**
-     * Called from a background thread to do the filter and return the resulting cursor.
-     *
-     * @param filter the text that was entered to filter on
-     * @return a cursor with the results of the filter
-     */
-    public Cursor doFilter(String filter) {
-        String[] projection = getProjectionForQuery();
-        if (mSearchMode && TextUtils.isEmpty(mListFragment.getQueryString())) {
-            return new MatrixCursor(projection);
-        }
-
-        final ContentResolver resolver = getContentResolver();
-        switch (mMode) {
-            case MODE_DEFAULT:
-            case MODE_CUSTOM:
-            case MODE_PICK_CONTACT:
-            case MODE_PICK_OR_CREATE_CONTACT:
-            case MODE_INSERT_OR_EDIT_CONTACT: {
-                return resolver.query(getContactFilterUri(filter), projection,
-                        getContactSelection(), null, getSortOrder(projection));
-            }
-
-            case MODE_LEGACY_PICK_PERSON:
-            case MODE_LEGACY_PICK_OR_CREATE_PERSON: {
-                return resolver.query(getPeopleFilterUri(filter), projection, null, null,
-                        People.DISPLAY_NAME);
-            }
-
-            case MODE_STARRED: {
-                return resolver.query(getContactFilterUri(filter), projection,
-                        Contacts.STARRED + "=1", null,
-                        getSortOrder(projection));
-            }
-
-            case MODE_FREQUENT: {
-                return resolver.query(getContactFilterUri(filter), projection,
-                        Contacts.TIMES_CONTACTED + " > 0", null,
-                        Contacts.TIMES_CONTACTED + " DESC, "
-                        + getSortOrder(projection));
-            }
-
-            case MODE_STREQUENT: {
-                Uri uri;
-                if (!TextUtils.isEmpty(filter)) {
-                    uri = Uri.withAppendedPath(Contacts.CONTENT_STREQUENT_FILTER_URI,
-                            Uri.encode(filter));
-                } else {
-                    uri = Contacts.CONTENT_STREQUENT_URI;
-                }
-                return resolver.query(uri, projection, null, null, null);
-            }
-
-            case MODE_PICK_PHONE: {
-                Uri uri = getUriToQuery();
-                if (!TextUtils.isEmpty(filter)) {
-                    uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(filter));
-                }
-                return resolver.query(uri, projection, CLAUSE_ONLY_VISIBLE, null,
-                        getSortOrder(projection));
-            }
-
-            case MODE_LEGACY_PICK_PHONE: {
-                //TODO: Support filtering here (bug 2092503)
-                break;
-            }
-        }
-        throw new UnsupportedOperationException("filtering not allowed in mode " + mMode);
-    }
-
-
-    /**
-     * Calls the currently selected list item.
-     * @return true if the call was initiated, false otherwise
-     */
-    boolean callSelection() {
-        ListView list = mListView;
-        if (list.hasFocus()) {
-            Cursor cursor = (Cursor) list.getSelectedItem();
-            return callContact(cursor);
-        }
-        return false;
-    }
-
-    boolean callContact(Cursor cursor) {
-        return callOrSmsContact(cursor, false /*call*/);
-    }
-
-    boolean smsContact(Cursor cursor) {
-        return callOrSmsContact(cursor, true /*sms*/);
-    }
-
-    /**
-     * Calls the contact which the cursor is point to.
-     * @return true if the call was initiated, false otherwise
-     */
-    boolean callOrSmsContact(Cursor cursor, boolean sendSms) {
-        if (cursor == null) {
-            return false;
-        }
-
-        switch (mMode) {
-            case MODE_PICK_PHONE:
-            case MODE_LEGACY_PICK_PHONE:
-            case MODE_QUERY_PICK_PHONE: {
-                String phone = cursor.getString(PHONE_NUMBER_COLUMN_INDEX);
-                if (sendSms) {
-                    ContactsUtils.initiateSms(this, phone);
-                } else {
-                    ContactsUtils.initiateCall(this, phone);
-                }
-                return true;
-            }
-
-            case MODE_PICK_POSTAL:
-            case MODE_LEGACY_PICK_POSTAL: {
-                return false;
-            }
-
-            default: {
-
-                boolean hasPhone = cursor.getInt(SUMMARY_HAS_PHONE_COLUMN_INDEX) != 0;
-                if (!hasPhone) {
-                    // There is no phone number.
-                    signalError();
-                    return false;
-                }
-
-                String phone = null;
-                Cursor phonesCursor = null;
-                phonesCursor = queryPhoneNumbers(cursor.getLong(SUMMARY_ID_COLUMN_INDEX));
-                if (phonesCursor == null || phonesCursor.getCount() == 0) {
-                    // No valid number
-                    signalError();
-                    return false;
-                } else if (phonesCursor.getCount() == 1) {
-                    // only one number, call it.
-                    phone = phonesCursor.getString(phonesCursor.getColumnIndex(Phone.NUMBER));
-                } else {
-                    phonesCursor.moveToPosition(-1);
-                    while (phonesCursor.moveToNext()) {
-                        if (phonesCursor.getInt(phonesCursor.
-                                getColumnIndex(Phone.IS_SUPER_PRIMARY)) != 0) {
-                            // Found super primary, call it.
-                            phone = phonesCursor.
-                            getString(phonesCursor.getColumnIndex(Phone.NUMBER));
-                            break;
-                        }
-                    }
-                }
-
-                if (phone == null) {
-                    // Display dialog to choose a number to call.
-                    PhoneDisambigDialog phoneDialog = new PhoneDisambigDialog(
-                            this, phonesCursor, sendSms);
-                    phoneDialog.show();
-                } else {
-                    if (sendSms) {
-                        ContactsUtils.initiateSms(this, phone);
-                    } else {
-                        ContactsUtils.initiateCall(this, phone);
-                    }
-                }
-            }
-        }
-        return true;
-    }
-
-    // TODO: eliminate
-    @Deprecated
-    private Cursor queryPhoneNumbers(long contactId) {
-        Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
-        Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY);
-
-        Cursor c = getContentResolver().query(dataUri,
-                new String[] {Phone._ID, Phone.NUMBER, Phone.IS_SUPER_PRIMARY,
-                        RawContacts.ACCOUNT_TYPE, Phone.TYPE, Phone.LABEL},
-                Data.MIMETYPE + "=?", new String[] {Phone.CONTENT_ITEM_TYPE}, null);
-        if (c != null && c.moveToFirst()) {
-            return c;
-        }
-        return null;
-    }
-
-    // TODO: fix PluralRules to handle zero correctly and use Resources.getQuantityText directly
-    public String getQuantityText(int count, int zeroResourceId, int pluralResourceId) {
-        if (count == 0) {
-            return getString(zeroResourceId);
-        } else {
-            String format = getResources().getQuantityText(pluralResourceId, count).toString();
-            return String.format(format, count);
-        }
-    }
-
-    /**
-     * Signal an error to the user.
-     */
-    void signalError() {
-        //TODO play an error beep or something...
-    }
-
-    Cursor getItemForView(View view) {
-        int index = mListView.getPositionForView(view);
-        if (index < 0) {
-            return null;
-        }
-        return (Cursor) mListView.getAdapter().getItem(index);
-    }
-
-    protected class QueryHandler extends AsyncQueryHandler {
-        protected final WeakReference<ContactsListActivity> mActivity;
-
-        public QueryHandler(Context context) {
-            super(context.getContentResolver());
-            mActivity = new WeakReference<ContactsListActivity>((ContactsListActivity) context);
-        }
-
-        @Override
-        public void startQuery(int token, Object cookie, Uri uri, String[] projection,
-                String selection, String[] selectionArgs, String orderBy) {
-            final ContactsListActivity activity = mActivity.get();
-            if (activity != null && activity.mRunQueriesSynchronously) {
-                Cursor cursor = getContentResolver().query(uri, projection, selection,
-                        selectionArgs, orderBy);
-                onQueryComplete(token, cookie, cursor);
-            } else {
-                super.startQuery(token, cookie, uri, projection, selection, selectionArgs, orderBy);
-            }
-        }
-
-        @Override
-        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
-            final ContactsListActivity activity = mActivity.get();
-            if (activity != null && !activity.isFinishing()) {
-                activity.onQueryComplete(cursor);
-            } else {
-                if (cursor != null) {
-                    cursor.close();
-                }
-            }
-        }
-    }
-
-    protected void onQueryComplete(Cursor cursor) {
-        mAdapter.changeCursor(cursor);
-    }
-
     private CallOrSmsInitiator getCallOrSmsInitiator() {
         if (mCallOrSmsInitiator == null) {
             mCallOrSmsInitiator = new CallOrSmsInitiator(this);
diff --git a/src/com/android/contacts/ContactsSearchManager.java b/src/com/android/contacts/ContactsSearchManager.java
index 2297817..340d7d6 100644
--- a/src/com/android/contacts/ContactsSearchManager.java
+++ b/src/com/android/contacts/ContactsSearchManager.java
@@ -16,6 +16,8 @@
 
 package com.android.contacts;
 
+import com.android.contacts.list.ContactsRequest;
+
 import android.app.Activity;
 import android.content.Intent;
 import android.os.Bundle;
@@ -46,6 +48,14 @@
     public static final String ORIGINAL_TYPE_EXTRA_KEY = "originalType";
 
     /**
+     * An extra that provides context for search UI and defines the scope for
+     * the search queries.
+     */
+    public static final String ORIGINAL_ACTION_CODE_EXTRA_KEY = "originalActionCode";
+
+    public static final String ORIGINAL_REQUEST_KEY = "originalRequest";
+
+    /**
      * Starts the contact list activity in the search mode.
      */
     public static void startSearch(Activity context, String initialQuery) {
@@ -53,13 +63,18 @@
     }
 
     public static void startSearchForResult(Activity context, String initialQuery,
-            int requestCode, Bundle includedExtras) {
+            int requestCode, ContactsRequest originalRequest) {
         context.startActivityForResult(
-                buildIntent(context, initialQuery, includedExtras), requestCode);
+                buildIntent(context, initialQuery, originalRequest), requestCode);
+    }
+
+    public static void startSearch(Activity context, String initialQuery,
+            ContactsRequest originalRequest) {
+        context.startActivity(buildIntent(context, initialQuery, originalRequest));
     }
 
     private static Intent buildIntent(
-            Activity context, String initialQuery, Bundle includedExtras) {
+            Activity context, String initialQuery, ContactsRequest originalRequest) {
         Intent intent = new Intent();
         intent.setData(ContactsContract.Contacts.CONTENT_URI);
         intent.setAction(UI.FILTER_CONTACTS_ACTION);
@@ -70,11 +85,8 @@
             intent.putExtras(originalExtras);
         }
         intent.putExtra(UI.FILTER_TEXT_EXTRA_KEY, initialQuery);
-        intent.putExtra(ORIGINAL_ACTION_EXTRA_KEY, originalIntent.getAction());
-        intent.putExtra(ORIGINAL_COMPONENT_EXTRA_KEY, originalIntent.getComponent().getClassName());
-        intent.putExtra(ORIGINAL_TYPE_EXTRA_KEY, originalIntent.getType());
-        if (includedExtras != null) {
-            intent.putExtras(includedExtras);
+        if (originalRequest != null) {
+            intent.putExtra(ORIGINAL_REQUEST_KEY, originalRequest);
         }
         return intent;
     }
diff --git a/src/com/android/contacts/list/ContactEntryListFragment.java b/src/com/android/contacts/list/ContactEntryListFragment.java
index e24a554..c894ba7 100644
--- a/src/com/android/contacts/list/ContactEntryListFragment.java
+++ b/src/com/android/contacts/list/ContactEntryListFragment.java
@@ -207,7 +207,7 @@
     }
 
     public boolean isSectionHeaderDisplayEnabled() {
-        return mSectionHeaderDisplayEnabled;
+        return mSectionHeaderDisplayEnabled && !mSearchMode;
     }
 
     public void setPhotoLoaderEnabled(boolean flag) {
@@ -248,11 +248,11 @@
         }
     }
 
-    public boolean isLegacyCompatibility() {
+    public boolean isLegacyCompatibilityMode() {
         return mLegacyCompatibility;
     }
 
-    public void setLegacyCompatibility(boolean flag) {
+    public void setLegacyCompatibilityMode(boolean flag) {
         mLegacyCompatibility = flag;
     }
 
@@ -738,4 +738,13 @@
                 (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
         return telephonyManager.hasIccCard();
     }
+
+    // TODO integrate into picker fragments
+//    protected Uri buildCallingPackageUri(Uri uri) {
+//        String callingPackage = getContext().getCallingPackage();
+//        if (!TextUtils.isEmpty(callingPackage)) {
+//            uri = uri.buildUpon().appendQueryParameter(
+//                    ContactsContract.REQUESTING_PACKAGE_PARAM_KEY, callingPackage).build();
+//        }
+//    }
 }
diff --git a/src/com/android/contacts/list/ContactItemListAdapter.java b/src/com/android/contacts/list/ContactItemListAdapter.java
deleted file mode 100644
index bfae731..0000000
--- a/src/com/android/contacts/list/ContactItemListAdapter.java
+++ /dev/null
@@ -1,714 +0,0 @@
-/*
- * Copyright (C) 2010 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.
- */
-package com.android.contacts.list;
-
-import com.android.contacts.ContactPresenceIconUtil;
-import com.android.contacts.ContactsListActivity;
-import com.android.contacts.ContactsSectionIndexer;
-import com.android.contacts.R;
-import com.android.contacts.widget.TextWithHighlighting;
-
-import android.app.patterns.CursorLoader;
-import android.content.Context;
-import android.database.CharArrayBuffer;
-import android.database.Cursor;
-import android.graphics.drawable.Drawable;
-import android.os.Bundle;
-import android.provider.ContactsContract;
-import android.provider.ContactsContract.ContactCounts;
-import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.ProviderStatus;
-import android.provider.ContactsContract.CommonDataKinds.Email;
-import android.provider.ContactsContract.CommonDataKinds.Nickname;
-import android.provider.ContactsContract.CommonDataKinds.Organization;
-import android.provider.ContactsContract.CommonDataKinds.Phone;
-import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
-import android.text.TextUtils;
-import android.view.LayoutInflater;
-import android.view.View;
-import android.view.ViewGroup;
-import android.widget.Filter;
-import android.widget.ImageView;
-import android.widget.ListView;
-import android.widget.QuickContactBadge;
-import android.widget.TextView;
-
-@Deprecated
-public class ContactItemListAdapter extends ContactEntryListAdapter {
-
-    private final ContactsListActivity contactsListActivity;
-    private boolean mLoading = true;
-    protected CharSequence mUnknownNameText;
-    protected boolean mDisplayPhotos = false;
-    private boolean mDisplayCallButton = false;
-    protected boolean mDisplayAdditionalData = true;
-    private int mFrequentSeparatorPos = ListView.INVALID_POSITION;
-
-    public ContactItemListAdapter(ContactsListActivity contactsListActivity) {
-        super(contactsListActivity);
-        this.contactsListActivity = contactsListActivity;
-
-        mUnknownNameText = contactsListActivity.getText(android.R.string.unknownName);
-
-        // Do not display the second line of text if in a specific SEARCH query mode, usually for
-        // matching a specific E-mail or phone number. Any contact details
-        // shown would be identical, and columns might not even be present
-        // in the returned cursor.
-        if (contactsListActivity.mMode != ContactsListActivity.MODE_QUERY_PICK_PHONE
-                && contactsListActivity.mQueryMode != ContactsListActivity.QUERY_MODE_NONE) {
-            mDisplayAdditionalData = false;
-        }
-
-        if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_NO_DATA) ==
-                ContactsListActivity.MODE_MASK_NO_DATA) {
-            mDisplayAdditionalData = false;
-        }
-
-        if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_SHOW_CALL_BUTTON) ==
-                ContactsListActivity.MODE_MASK_SHOW_CALL_BUTTON) {
-            mDisplayCallButton = true;
-        }
-    }
-
-    @Override
-    public void setDisplayPhotos(boolean flag) {
-        mDisplayPhotos = flag;
-    }
-
-    /**
-     * Callback on the UI thread when the content observer on the backing cursor fires.
-     * Instead of calling requery we need to do an async query so that the requery doesn't
-     * block the UI thread for a long time.
-     */
-    @Override
-    public void onContentChanged() {
-        CharSequence constraint = getQueryString();
-        if (!TextUtils.isEmpty(constraint)) {
-            // Reset the filter state then start an async filter operation
-            Filter filter = getFilter();
-            filter.filter(constraint);
-        } else {
-            // Start an async query
-            contactsListActivity.startQuery();
-        }
-    }
-
-    public void setLoading(boolean loading) {
-        mLoading = loading;
-    }
-
-    @Override
-    public boolean isEmpty() {
-//        if (contactsListActivity.mProviderStatus != ProviderStatus.STATUS_NORMAL) {
-//            return true;
-//        }
-
-        if (contactsListActivity.mSearchMode) {
-            return TextUtils.isEmpty(getQueryString());
-        } else if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_CREATE_NEW) ==
-                ContactsListActivity.MODE_MASK_CREATE_NEW) {
-            // This mode mask adds a header and we always want it to show up, even
-            // if the list is empty, so always claim the list is not empty.
-            return false;
-        } else {
-            if (mCursor == null || mLoading) {
-                // We don't want the empty state to show when loading.
-                return false;
-            } else {
-                return super.isEmpty();
-            }
-        }
-    }
-
-    @Override
-    public int getItemViewType(int position) {
-        if (position == 0 && (contactsListActivity.mShowNumberOfContacts ||
-                (contactsListActivity.mMode & ContactsListActivity.MODE_MASK_CREATE_NEW) != 0)) {
-            return IGNORE_ITEM_VIEW_TYPE;
-        }
-
-        if (isSearchAllContactsItemPosition(position)) {
-            return IGNORE_ITEM_VIEW_TYPE;
-        }
-
-        if (getSeparatorId(position) != 0) {
-            // We don't want the separator view to be recycled.
-            return IGNORE_ITEM_VIEW_TYPE;
-        }
-        return super.getItemViewType(position);
-    }
-
-    @Override
-    public View getView(int position, View convertView, ViewGroup parent) {
-        if (!mDataValid) {
-            throw new IllegalStateException(
-                    "this should only be called when the cursor is valid");
-        }
-
-        // handle the total contacts item
-        if (position == 0 && contactsListActivity.mShowNumberOfContacts) {
-            return getTotalContactCountView(parent);
-        }
-
-        if (position == 0
-                && (contactsListActivity.mMode & ContactsListActivity.MODE_MASK_CREATE_NEW) != 0) {
-            // Add the header for creating a new contact
-            return contactsListActivity.getLayoutInflater().inflate(R.layout.create_new_contact,
-                    parent, false);
-        }
-
-        if (isSearchAllContactsItemPosition(position)) {
-            return contactsListActivity.getLayoutInflater().
-                    inflate(R.layout.contacts_list_search_all_item, parent, false);
-        }
-
-        // Handle the separator specially
-        int separatorId = getSeparatorId(position);
-        if (separatorId != 0) {
-            TextView view = (TextView) contactsListActivity.getLayoutInflater().
-                    inflate(R.layout.list_separator, parent, false);
-            view.setText(separatorId);
-            return view;
-        }
-
-        int realPosition = getRealPosition(position);
-        if (!mCursor.moveToPosition(realPosition)) {
-            throw new IllegalStateException("couldn't move cursor to position " + position);
-        }
-
-        boolean newView;
-        View v;
-        if (convertView == null || convertView.getTag() == null) {
-            newView = true;
-            v = newView(getContext(), mCursor, parent);
-        } else {
-            newView = false;
-            v = convertView;
-        }
-        bindView(v, getContext(), mCursor);
-        bindSectionHeader(v, realPosition, isSectionHeaderDisplayEnabled());
-        return v;
-    }
-
-    private View getTotalContactCountView(ViewGroup parent) {
-        final LayoutInflater inflater = contactsListActivity.getLayoutInflater();
-        View view = inflater.inflate(R.layout.total_contacts, parent, false);
-
-        TextView totalContacts = (TextView) view.findViewById(R.id.totalContactsText);
-
-        String text;
-        int count = getRealCount();
-
-        if (contactsListActivity.mSearchMode
-                && !TextUtils.isEmpty(getQueryString())) {
-            text = contactsListActivity.getQuantityText(count, R.string.listFoundAllContactsZero,
-                    R.plurals.searchFoundContacts);
-        } else {
-            if (contactsListActivity.mDisplayOnlyPhones) {
-                text = contactsListActivity.getQuantityText(count,
-                        R.string.listTotalPhoneContactsZero, R.plurals.listTotalPhoneContacts);
-            } else {
-                text = contactsListActivity.getQuantityText(count,
-                        R.string.listTotalAllContactsZero, R.plurals.listTotalAllContacts);
-            }
-        }
-        totalContacts.setText(text);
-        return view;
-    }
-
-    @Override
-    public boolean isSearchAllContactsItemPosition(int position) {
-        return contactsListActivity.mSearchMode && contactsListActivity.mMode != ContactsListActivity.MODE_PICK_MULTIPLE_PHONES && position == getCount() - 1;
-    }
-
-    private int getSeparatorId(int position) {
-        int separatorId = 0;
-        if (position == mFrequentSeparatorPos) {
-            separatorId = R.string.favoritesFrquentSeparator;
-        }
-        return separatorId;
-    }
-
-    @Override
-    public View newView(Context context, Cursor cursor, ViewGroup parent) {
-        final ContactListItemView view = new ContactListItemView(context, null);
-//        view.setOnCallButtonClickListener(contactsListActivity);
-        return view;
-    }
-
-    @Override
-    public void bindView(View itemView, Context context, Cursor cursor) {
-        final ContactListItemView view = (ContactListItemView)itemView;
-
-        int typeColumnIndex;
-        int dataColumnIndex;
-        int labelColumnIndex;
-        int defaultType;
-        int nameColumnIndex;
-        int phoneticNameColumnIndex;
-        int photoColumnIndex = ContactsListActivity.SUMMARY_PHOTO_ID_COLUMN_INDEX;
-        boolean displayAdditionalData = mDisplayAdditionalData;
-        boolean highlightingEnabled = false;
-        switch(contactsListActivity.mMode) {
-            case ContactsListActivity.MODE_PICK_MULTIPLE_PHONES:
-            case ContactsListActivity.MODE_PICK_PHONE:
-            case ContactsListActivity.MODE_LEGACY_PICK_PHONE:
-            case ContactsListActivity.MODE_QUERY_PICK_PHONE: {
-                nameColumnIndex = ContactsListActivity.PHONE_DISPLAY_NAME_COLUMN_INDEX;
-                phoneticNameColumnIndex = -1;
-                dataColumnIndex = ContactsListActivity.PHONE_NUMBER_COLUMN_INDEX;
-                typeColumnIndex = ContactsListActivity.PHONE_TYPE_COLUMN_INDEX;
-                labelColumnIndex = ContactsListActivity.PHONE_LABEL_COLUMN_INDEX;
-                defaultType = Phone.TYPE_HOME;
-                photoColumnIndex = ContactsListActivity.PHONE_PHOTO_ID_COLUMN_INDEX;
-                break;
-            }
-            case ContactsListActivity.MODE_PICK_POSTAL:
-            case ContactsListActivity.MODE_LEGACY_PICK_POSTAL: {
-                nameColumnIndex = ContactsListActivity.POSTAL_DISPLAY_NAME_COLUMN_INDEX;
-                phoneticNameColumnIndex = -1;
-                dataColumnIndex = ContactsListActivity.POSTAL_ADDRESS_COLUMN_INDEX;
-                typeColumnIndex = ContactsListActivity.POSTAL_TYPE_COLUMN_INDEX;
-                labelColumnIndex = ContactsListActivity.POSTAL_LABEL_COLUMN_INDEX;
-                defaultType = StructuredPostal.TYPE_HOME;
-                break;
-            }
-            default: {
-                nameColumnIndex = contactsListActivity.getSummaryDisplayNameColumnIndex();
-                if (contactsListActivity.mMode == ContactsListActivity.MODE_LEGACY_PICK_PERSON
-                        || contactsListActivity.mMode ==
-                            ContactsListActivity.MODE_LEGACY_PICK_OR_CREATE_PERSON) {
-                    phoneticNameColumnIndex = -1;
-                } else {
-                    phoneticNameColumnIndex =
-                        ContactsListActivity.SUMMARY_PHONETIC_NAME_COLUMN_INDEX;
-                }
-                dataColumnIndex = -1;
-                typeColumnIndex = -1;
-                labelColumnIndex = -1;
-                defaultType = Phone.TYPE_HOME;
-                displayAdditionalData = false;
-                highlightingEnabled = isNameHighlightingEnabled()
-                        && contactsListActivity.mMode != ContactsListActivity.MODE_STREQUENT;
-            }
-        }
-
-        // Set the name
-        cursor.copyStringToBuffer(nameColumnIndex, view.nameBuffer);
-        TextView nameView = view.getNameTextView();
-        int size = view.nameBuffer.sizeCopied;
-        if (size != 0) {
-            if (highlightingEnabled) {
-                if (view.textWithHighlighting == null) {
-                    view.textWithHighlighting =
-                            getTextWithHighlightingFactory().createTextWithHighlighting();
-                }
-                buildDisplayNameWithHighlighting(nameView, cursor, view.nameBuffer,
-                        view.highlightedTextBuffer, view.textWithHighlighting);
-            } else {
-                nameView.setText(view.nameBuffer.data, 0, size);
-            }
-        } else {
-            nameView.setText(mUnknownNameText);
-        }
-
-        // Make the call button visible if requested.
-        if (mDisplayCallButton
-                && cursor.getColumnCount() > ContactsListActivity.SUMMARY_HAS_PHONE_COLUMN_INDEX
-                && cursor.getInt(ContactsListActivity.SUMMARY_HAS_PHONE_COLUMN_INDEX) != 0) {
-            int pos = cursor.getPosition();
-            view.showCallButton(android.R.id.button1, pos);
-        } else {
-            view.hideCallButton();
-        }
-
-        // Set the photo, if requested
-        if (mDisplayPhotos) {
-            boolean useQuickContact = (contactsListActivity.mMode
-                    & ContactsListActivity.MODE_MASK_DISABLE_QUIKCCONTACT) == 0;
-
-            long photoId = 0;
-            if (!cursor.isNull(photoColumnIndex)) {
-                photoId = cursor.getLong(photoColumnIndex);
-            }
-
-            ImageView viewToUse;
-            if (useQuickContact) {
-                // Build soft lookup reference
-                final long contactId =
-                        cursor.getLong(ContactsListActivity.SUMMARY_ID_COLUMN_INDEX);
-                final String lookupKey =
-                        cursor.getString(ContactsListActivity.SUMMARY_LOOKUP_KEY_COLUMN_INDEX);
-                QuickContactBadge quickContact = view.getQuickContact();
-                quickContact.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));
-                viewToUse = quickContact;
-            } else {
-                viewToUse = view.getPhotoView();
-            }
-
-            getPhotoLoader().loadPhoto(viewToUse, photoId);
-        }
-
-        if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_NO_PRESENCE) == 0) {
-            // Set the proper icon (star or presence or nothing)
-            int serverStatus;
-            if (!cursor.isNull(ContactsListActivity.SUMMARY_PRESENCE_STATUS_COLUMN_INDEX)) {
-                serverStatus =
-                        cursor.getInt(ContactsListActivity.SUMMARY_PRESENCE_STATUS_COLUMN_INDEX);
-                Drawable icon = ContactPresenceIconUtil.getPresenceIcon(getContext(), serverStatus);
-                if (icon != null) {
-                    view.setPresence(icon);
-                } else {
-                    view.setPresence(null);
-                }
-            } else {
-                view.setPresence(null);
-            }
-        } else {
-            view.setPresence(null);
-        }
-
-        if (contactsListActivity.mShowSearchSnippets) {
-            boolean showSnippet = false;
-            String snippetMimeType =
-                    cursor.getString(ContactsListActivity.SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX);
-            if (Email.CONTENT_ITEM_TYPE.equals(snippetMimeType)) {
-                String email =
-                        cursor.getString(ContactsListActivity.SUMMARY_SNIPPET_DATA1_COLUMN_INDEX);
-                if (!TextUtils.isEmpty(email)) {
-                    view.setSnippet(email);
-                    showSnippet = true;
-                }
-            } else if (Organization.CONTENT_ITEM_TYPE.equals(snippetMimeType)) {
-                String company =
-                        cursor.getString(ContactsListActivity.SUMMARY_SNIPPET_DATA1_COLUMN_INDEX);
-                String title =
-                        cursor.getString(ContactsListActivity.SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
-                if (!TextUtils.isEmpty(company)) {
-                    if (!TextUtils.isEmpty(title)) {
-                        view.setSnippet(company + " / " + title);
-                    } else {
-                        view.setSnippet(company);
-                    }
-                    showSnippet = true;
-                } else if (!TextUtils.isEmpty(title)) {
-                    view.setSnippet(title);
-                    showSnippet = true;
-                }
-            } else if (Nickname.CONTENT_ITEM_TYPE.equals(snippetMimeType)) {
-                String nickname =
-                        cursor.getString(ContactsListActivity.SUMMARY_SNIPPET_DATA1_COLUMN_INDEX);
-                if (!TextUtils.isEmpty(nickname)) {
-                    view.setSnippet(nickname);
-                    showSnippet = true;
-                }
-            }
-
-            if (!showSnippet) {
-                view.setSnippet(null);
-            }
-        }
-
-        if (!displayAdditionalData) {
-            if (phoneticNameColumnIndex != -1) {
-
-                // Set the name
-                cursor.copyStringToBuffer(phoneticNameColumnIndex, view.phoneticNameBuffer);
-                int phoneticNameSize = view.phoneticNameBuffer.sizeCopied;
-                if (phoneticNameSize != 0) {
-                    view.setLabel(view.phoneticNameBuffer.data, phoneticNameSize);
-                } else {
-                    view.setLabel(null);
-                }
-            } else {
-                view.setLabel(null);
-            }
-            return;
-        }
-
-        // Set the data.
-        cursor.copyStringToBuffer(dataColumnIndex, view.dataBuffer);
-
-        size = view.dataBuffer.sizeCopied;
-        view.setData(view.dataBuffer.data, size);
-
-        // Set the label.
-        if (!cursor.isNull(typeColumnIndex)) {
-            final int type = cursor.getInt(typeColumnIndex);
-            final String label = cursor.getString(labelColumnIndex);
-
-            if (contactsListActivity.mMode == ContactsListActivity.MODE_LEGACY_PICK_POSTAL
-                    || contactsListActivity.mMode == ContactsListActivity.MODE_PICK_POSTAL) {
-                // TODO cache
-                view.setLabel(StructuredPostal.getTypeLabel(context.getResources(), type,
-                        label));
-            } else {
-                // TODO cache
-                view.setLabel(Phone.getTypeLabel(context.getResources(), type, label));
-            }
-        } else {
-            view.setLabel(null);
-        }
-    }
-
-    /**
-     * Computes the span of the display name that has highlighted parts and configures
-     * the display name text view accordingly.
-     */
-    protected void buildDisplayNameWithHighlighting(TextView textView, Cursor cursor,
-            CharArrayBuffer buffer1, CharArrayBuffer buffer2,
-            TextWithHighlighting textWithHighlighting) {
-        int oppositeDisplayOrderColumnIndex;
-        if (contactsListActivity.mDisplayOrder ==
-                ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
-            oppositeDisplayOrderColumnIndex =
-                    ContactsListActivity.SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
-        } else {
-            oppositeDisplayOrderColumnIndex =
-                    ContactsListActivity.SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
-        }
-        cursor.copyStringToBuffer(oppositeDisplayOrderColumnIndex, buffer2);
-
-        textWithHighlighting.setText(buffer1, buffer2);
-        textView.setText(textWithHighlighting);
-    }
-
-    protected void bindSectionHeader(View itemView, int position, boolean displaySectionHeaders) {
-        final ContactListItemView view = (ContactListItemView)itemView;
-        if (!displaySectionHeaders) {
-            view.setSectionHeader(null);
-            view.setDividerVisible(true);
-        } else {
-            final int section = getSectionForPosition(position);
-            if (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);
-            } else {
-                view.setDividerVisible(true);
-            }
-        }
-    }
-
-    @Override
-    public void changeCursor(Cursor cursor) {
-        if (cursor != null) {
-            setLoading(false);
-        }
-
-        // Get the split between starred and frequent items, if the mode is strequent
-        mFrequentSeparatorPos = ListView.INVALID_POSITION;
-        int cursorCount = 0;
-        if (cursor != null && (cursorCount = cursor.getCount()) > 0
-                && contactsListActivity.mMode == ContactsListActivity.MODE_STREQUENT) {
-            cursor.move(-1);
-            for (int i = 0; cursor.moveToNext(); i++) {
-                int starred = cursor.getInt(ContactsListActivity.SUMMARY_STARRED_COLUMN_INDEX);
-                if (starred == 0) {
-                    if (i > 0) {
-                        // Only add the separator when there are starred items present
-                        mFrequentSeparatorPos = i;
-                    }
-                    break;
-                }
-            }
-        }
-
-        if (cursor != null && contactsListActivity.mSearchResultsMode) {
-            TextView foundContactsText = (TextView)contactsListActivity
-                    .findViewById(R.id.search_results_found);
-            String text = contactsListActivity.getQuantityText(cursor.getCount(),
-                    R.string.listFoundAllContactsZero, R.plurals.listFoundAllContacts);
-            foundContactsText.setText(text);
-        }
-
-        if (contactsListActivity.mEmptyView != null && (cursor == null || cursor.getCount() == 0)) {
-            prepareEmptyView();
-        }
-
-        super.changeCursor(cursor);
-
-        // Update the indexer for the fast scroll widget
-        updateIndexer(cursor);
-    }
-
-    protected void prepareEmptyView() {
-        contactsListActivity.mEmptyView.show(contactsListActivity.mSearchMode,
-                contactsListActivity.mDisplayOnlyPhones,
-                contactsListActivity.mMode == ContactsListActivity.MODE_STREQUENT
-                || contactsListActivity.mMode == ContactsListActivity.MODE_STARRED,
-                contactsListActivity.mMode == ContactsListActivity.MODE_QUERY
-                || contactsListActivity.mMode == ContactsListActivity.MODE_QUERY_PICK
-                || contactsListActivity.mMode == ContactsListActivity.MODE_QUERY_PICK_PHONE
-                || contactsListActivity.mMode == ContactsListActivity.MODE_QUERY_PICK_TO_VIEW
-                || contactsListActivity.mMode == ContactsListActivity.MODE_QUERY_PICK_TO_EDIT,
-                contactsListActivity.mShortcutAction != null,
-                false,
-                false);
-    }
-
-    private void updateIndexer(Cursor cursor) {
-        if (cursor == null) {
-            setIndexer(null);
-            return;
-        }
-
-        Bundle bundle = cursor.getExtras();
-        if (bundle.containsKey(ContactCounts.EXTRA_ADDRESS_BOOK_INDEX_TITLES)) {
-            String sections[] =
-                bundle.getStringArray(ContactCounts.EXTRA_ADDRESS_BOOK_INDEX_TITLES);
-            int counts[] = bundle.getIntArray(ContactCounts.EXTRA_ADDRESS_BOOK_INDEX_COUNTS);
-            setIndexer(new ContactsSectionIndexer(sections, counts));
-        } else {
-            setIndexer(null);
-        }
-    }
-
-    /**
-     * Run the query on a helper thread. Beware that this code does not run
-     * on the main UI thread!
-     */
-    @Override
-    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
-        return contactsListActivity.doFilter(constraint.toString());
-    }
-
-    @Override
-    public boolean areAllItemsEnabled() {
-        return contactsListActivity.mMode != ContactsListActivity.MODE_STARRED
-            && !contactsListActivity.mShowNumberOfContacts;
-    }
-
-    @Override
-    public boolean isEnabled(int position) {
-        if (contactsListActivity.mShowNumberOfContacts) {
-            if (position == 0) {
-                return false;
-            }
-            position--;
-        }
-        return position != mFrequentSeparatorPos;
-    }
-
-    @Override
-    public int getCount() {
-        if (!mDataValid) {
-            return 0;
-        }
-        int superCount = super.getCount();
-
-        if (contactsListActivity.mShowNumberOfContacts
-                && (contactsListActivity.mSearchMode || superCount > 0)) {
-            // We don't want to count this header if it's the only thing visible, so that
-            // the empty text will display.
-            superCount++;
-        }
-
-        if (contactsListActivity.mSearchMode
-                && contactsListActivity.mMode != ContactsListActivity.MODE_PICK_MULTIPLE_PHONES) {
-            // Last element in the list is the "Find
-            superCount++;
-        }
-
-        // We do not show the "Create New" button in Search mode
-        if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_CREATE_NEW) != 0
-                && !contactsListActivity.mSearchMode) {
-            // Count the "Create new contact" line
-            superCount++;
-        }
-
-        if (mFrequentSeparatorPos != ListView.INVALID_POSITION) {
-            // When showing strequent list, we have an additional list item - the separator.
-            return superCount + 1;
-        } else {
-            return superCount;
-        }
-    }
-
-    /**
-     * Gets the actual count of contacts and excludes all the headers.
-     */
-    public int getRealCount() {
-        return super.getCount();
-    }
-
-    protected int getRealPosition(int pos) {
-        if (contactsListActivity.mShowNumberOfContacts) {
-            pos--;
-        }
-
-        if ((contactsListActivity.mMode & ContactsListActivity.MODE_MASK_CREATE_NEW) != 0
-                && !contactsListActivity.mSearchMode) {
-            return pos - 1;
-        }
-
-        if (mFrequentSeparatorPos == ListView.INVALID_POSITION) {
-            // No separator, identity map
-            return pos;
-        } else if (pos <= mFrequentSeparatorPos) {
-            // Before or at the separator, identity map
-            return pos;
-        } else {
-            // After the separator, remove 1 from the pos to get the real underlying pos
-            return pos - 1;
-        }
-    }
-
-    @Override
-    public Object getItem(int pos) {
-        if (isSearchAllContactsItemPosition(pos)){
-            return null;
-        } else {
-            int realPosition = getRealPosition(pos);
-            if (realPosition < 0) {
-                return null;
-            }
-            return super.getItem(realPosition);
-        }
-    }
-
-    @Override
-    public long getItemId(int pos) {
-        if (isSearchAllContactsItemPosition(pos)) {
-            return 0;
-        }
-        int realPosition = getRealPosition(pos);
-        if (realPosition < 0) {
-            return 0;
-        }
-        return super.getItemId(realPosition);
-    }
-
-    @Override
-    public void configureLoader(CursorLoader loader) {
-    }
-
-    @Override
-    public String getContactDisplayName() {
-        return "TODO";
-    }
-
-    public boolean isContactStarred() {
-        return false;
-    }
-}
\ No newline at end of file
diff --git a/src/com/android/contacts/list/ContactPickerFragment.java b/src/com/android/contacts/list/ContactPickerFragment.java
index 0cbfcba..d2b550a 100644
--- a/src/com/android/contacts/list/ContactPickerFragment.java
+++ b/src/com/android/contacts/list/ContactPickerFragment.java
@@ -86,7 +86,7 @@
             Uri uri;
             ContactEntryListAdapter adapter = getAdapter();
             adapter.moveToPosition(position);
-            if (isLegacyCompatibility()) {
+            if (isLegacyCompatibilityMode()) {
                 uri = ((LegacyContactListAdapter)adapter).getPersonUri();
             } else {
                 uri = ((ContactListAdapter)adapter).getContactUri();
@@ -102,7 +102,7 @@
 
     @Override
     protected ContactEntryListAdapter createListAdapter() {
-        if (!isLegacyCompatibility()) {
+        if (!isLegacyCompatibilityMode()) {
             ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
             adapter.setSectionHeaderDisplayEnabled(true);
             adapter.setDisplayPhotos(true);
diff --git a/src/com/android/contacts/list/ContactsIntentResolver.java b/src/com/android/contacts/list/ContactsIntentResolver.java
index a43b525..beab168 100644
--- a/src/com/android/contacts/list/ContactsIntentResolver.java
+++ b/src/com/android/contacts/list/ContactsIntentResolver.java
@@ -17,21 +17,16 @@
 package com.android.contacts.list;
 
 import com.android.contacts.ContactsSearchManager;
-import com.android.contacts.JoinContactActivity;
 import com.android.contacts.R;
-import com.android.contacts.ui.ContactsPreferences;
-import com.android.contacts.ui.ContactsPreferencesActivity.Prefs;
 
 import android.app.Activity;
 import android.app.SearchManager;
 import android.content.ContentUris;
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.content.UriMatcher;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
-import android.preference.PreferenceManager;
 import android.provider.ContactsContract;
 import android.provider.Contacts.ContactMethods;
 import android.provider.Contacts.People;
@@ -42,7 +37,6 @@
 import android.provider.ContactsContract.RawContacts;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
-import android.provider.ContactsContract.Intents.Insert;
 import android.provider.ContactsContract.Intents.UI;
 import android.text.TextUtils;
 import android.util.Log;
@@ -56,120 +50,6 @@
 
     private static final String TAG = "ContactsListActivity";
 
-    private static final String SHORTCUT_ACTION_KEY = "shortcutAction";
-
-    /**
-     * The action for the join contact activity.
-     * <p>
-     * Input: extra field {@link #EXTRA_AGGREGATE_ID} is the aggregate ID.
-     *
-     * TODO: move to {@link ContactsContract}.
-     */
-    public static final String JOIN_AGGREGATE =
-            "com.android.contacts.action.JOIN_AGGREGATE";
-
-    /**
-     * Used with {@link #JOIN_AGGREGATE} to give it the target for aggregation.
-     * <p>
-     * Type: LONG
-     */
-    public static final String EXTRA_AGGREGATE_ID =
-            "com.android.contacts.action.AGGREGATE_ID";
-
-    /** Mask for picker mode */
-    static final int MODE_MASK_PICKER = 0x80000000;
-    /** Mask for no presence mode */
-    static final int MODE_MASK_NO_PRESENCE = 0x40000000;
-    /** Mask for enabling list filtering */
-    static final int MODE_MASK_NO_FILTER = 0x20000000;
-    /** Mask for having a "create new contact" header in the list */
-    static final int MODE_MASK_CREATE_NEW = 0x10000000;
-    /** Mask for showing photos in the list */
-    static final int MODE_MASK_SHOW_PHOTOS = 0x08000000;
-    /** Mask for hiding additional information e.g. primary phone number in the list */
-    static final int MODE_MASK_NO_DATA = 0x04000000;
-    /** Mask for showing a call button in the list */
-    static final int MODE_MASK_SHOW_CALL_BUTTON = 0x02000000;
-    /** Mask to disable quickcontact (images will show as normal images) */
-    static final int MODE_MASK_DISABLE_QUIKCCONTACT = 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 | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-    /** Custom mode */
-    static final int MODE_CUSTOM = 8;
-    /** Show all starred contacts */
-    static final int MODE_STARRED = 20 | MODE_MASK_SHOW_PHOTOS;
-    /** Show frequently contacted contacts */
-    static final int MODE_FREQUENT = 30 | MODE_MASK_SHOW_PHOTOS;
-    /** Show starred and the frequent */
-    static final int MODE_STREQUENT = 35 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_SHOW_CALL_BUTTON;
-    /** Show all contacts and pick them when clicking */
-    static final int MODE_PICK_CONTACT = 40 | MODE_MASK_PICKER | MODE_MASK_SHOW_PHOTOS
-            | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all contacts as well as the option to create a new one */
-    static final int MODE_PICK_OR_CREATE_CONTACT = 42 | MODE_MASK_PICKER | MODE_MASK_CREATE_NEW
-            | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all people through the legacy provider and pick them when clicking */
-    static final int MODE_LEGACY_PICK_PERSON = 43 | MODE_MASK_PICKER
-            | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all people through the legacy provider as well as the option to create a new one */
-    static final int MODE_LEGACY_PICK_OR_CREATE_PERSON = 44 | MODE_MASK_PICKER
-            | MODE_MASK_CREATE_NEW | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all contacts and pick them when clicking, and allow creating a new contact */
-    static final int MODE_INSERT_OR_EDIT_CONTACT = 45 | MODE_MASK_PICKER | MODE_MASK_CREATE_NEW
-            | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-    /** Show all phone numbers and pick them when clicking */
-    static final int MODE_PICK_PHONE = 50 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE;
-    /** Show all phone numbers through the legacy provider and pick them when clicking */
-    static final int MODE_LEGACY_PICK_PHONE =
-            51 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    /** Show all postal addresses and pick them when clicking */
-    static final int MODE_PICK_POSTAL =
-            55 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    /** Show all postal addresses and pick them when clicking */
-    static final int MODE_LEGACY_PICK_POSTAL =
-            56 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE | MODE_MASK_NO_FILTER;
-    static final int MODE_GROUP = 57 | MODE_MASK_SHOW_PHOTOS;
-    /** Run a search query */
-    static final int MODE_QUERY = 60 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_NO_FILTER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-    /** Run a search query in PICK mode, but that still launches to VIEW */
-    static final int MODE_QUERY_PICK_TO_VIEW = 65 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_PICKER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Show join suggestions followed by an A-Z list */
-    static final int MODE_JOIN_CONTACT = 70 | MODE_MASK_PICKER | MODE_MASK_NO_PRESENCE
-            | MODE_MASK_NO_DATA | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-
-    /** Run a search query in a PICK mode */
-    static final int MODE_QUERY_PICK = 75 | MODE_MASK_SHOW_PHOTOS | MODE_MASK_NO_FILTER
-            | MODE_MASK_PICKER | MODE_MASK_DISABLE_QUIKCCONTACT | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Run a search query in a PICK_PHONE mode */
-    static final int MODE_QUERY_PICK_PHONE = 80 | MODE_MASK_NO_FILTER | MODE_MASK_PICKER
-            | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /** Run a search query in PICK mode, but that still launches to EDIT */
-    static final int MODE_QUERY_PICK_TO_EDIT = 85 | MODE_MASK_NO_FILTER | MODE_MASK_SHOW_PHOTOS
-            | MODE_MASK_PICKER | MODE_MASK_SHOW_NUMBER_OF_CONTACTS;
-
-    /**
-     * Show all phone numbers and do multiple pick when clicking. This mode has phone filtering
-     * feature, but doesn't support 'search for all contacts'.
-     */
-    static final int MODE_PICK_MULTIPLE_PHONES = 80 | MODE_MASK_PICKER
-            | MODE_MASK_NO_PRESENCE | MODE_MASK_SHOW_PHOTOS | MODE_MASK_DISABLE_QUIKCCONTACT;
-
-    /**
-     * An action used to do perform search while in a contact picker.  It is initiated
-     * by the ContactListActivity itself.
-     */
-    private static final String ACTION_SEARCH_INTERNAL = "com.android.contacts.INTERNAL_SEARCH";
-
     // Uri matcher for contact id
     private static final int CONTACTS_ID = 1001;
     private static final UriMatcher sContactsIdMatcher;
@@ -179,163 +59,97 @@
     }
 
     private final Activity mContext;
-    private boolean mValid = true;
-    private Intent mRedirectIntent;
-    private CharSequence mTitle;
-
-    // TODO: make all these fields private.  They should only remain public while we
-    // are refactoring ContactListActivity.
-    public int mMode = MODE_DEFAULT;
-    public int mQueryMode = QUERY_MODE_NONE;
-    public boolean mSearchMode;
-    public boolean mShowSearchSnippets;
-    public String mInitialFilter;
-    public boolean mDisplayOnlyPhones;
-    public String mShortcutAction;
-    public boolean mSearchResultsMode;
-    public boolean mShowNumberOfContacts;
-    public String mGroupName;
-
-    /**
-     * Internal query type when in mode {@link #MODE_QUERY_PICK_TO_VIEW}.
-     */
-    private static final int QUERY_MODE_NONE = -1;
-    private static final int QUERY_MODE_MAILTO = 1;
-    private static final int QUERY_MODE_TEL = 2;
 
     public ContactsIntentResolver(Activity context) {
         this.mContext = context;
     }
 
-    public void setIntent(Intent intent) {
+    public ContactsRequest resolveIntent(Intent intent) {
+        ContactsRequest request = new ContactsRequest();
+        request.setDisplayOnlyVisible(true);
+
         String action = intent.getAction();
         String component = intent.getComponent().getClassName();
-
-        // Allow the title to be set to a custom String using an extra on the intent
-        String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
-        if (title != null) {
-            mTitle = title;
-        }
-
         String type = intent.getType();
 
-        // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
-        // of some other action. Let's retrieve the original action to provide proper
-        // context for the search queries.
-        if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
-            mSearchMode = true;
-            mShowSearchSnippets = true;
-            Bundle extras = intent.getExtras();
-            if (extras != null) {
-                mInitialFilter = extras.getString(UI.FILTER_TEXT_EXTRA_KEY);
-                String originalAction =
-                        extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
-                if (originalAction != null) {
-                    action = originalAction;
-                }
-                String originalComponent =
-                        extras.getString(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY);
-                if (originalComponent != null) {
-                    component = originalComponent;
-                }
-                String originalType =
-                    extras.getString(ContactsSearchManager.ORIGINAL_TYPE_EXTRA_KEY);
-                if (originalType != null) {
-                    type = originalType;
-                }
-            } else {
-                mInitialFilter = null;
-            }
-        }
-
         Log.i(TAG, "Called with action: " + action);
-        mMode = MODE_UNKNOWN;
-        if (UI.LIST_DEFAULT.equals(action) || UI.FILTER_CONTACTS_ACTION.equals(action)) {
-            mMode = MODE_DEFAULT;
-            // When mDefaultMode is true the mode is set in onResume(), since the preferneces
-            // activity may change it whenever this activity isn't running
-        } else if (UI.LIST_GROUP_ACTION.equals(action)) {
-            mMode = MODE_GROUP;
-            mGroupName = intent.getStringExtra(UI.GROUP_NAME_EXTRA_KEY);
-            if (TextUtils.isEmpty(mGroupName)) {
-                mValid = false;
-                return;
-            }
+
+        if (UI.LIST_DEFAULT.equals(action) ) {
+            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
+            request.setDisplayWithPhonesOnlyOption(
+                    ContactsRequest.DISPLAY_ONLY_WITH_PHONES_PREFERENCE);
         } else if (UI.LIST_ALL_CONTACTS_ACTION.equals(action)) {
-            mMode = MODE_CUSTOM;
-            mDisplayOnlyPhones = false;
-        } else if (UI.LIST_STARRED_ACTION.equals(action)) {
-            mMode = mSearchMode ? MODE_DEFAULT : MODE_STARRED;
-        } else if (UI.LIST_FREQUENT_ACTION.equals(action)) {
-            mMode = mSearchMode ? MODE_DEFAULT : MODE_FREQUENT;
-        } else if (UI.LIST_STREQUENT_ACTION.equals(action)) {
-            mMode = mSearchMode ? MODE_DEFAULT : MODE_STREQUENT;
+            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
+            request.setDisplayWithPhonesOnlyOption(
+                    ContactsRequest.DISPLAY_ONLY_WITH_PHONES_DISABLED);
+            request.setDisplayOnlyVisible(false);
         } else if (UI.LIST_CONTACTS_WITH_PHONES_ACTION.equals(action)) {
-            mMode = MODE_CUSTOM;
-            mDisplayOnlyPhones = true;
+            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
+            request.setDisplayWithPhonesOnlyOption(
+                    ContactsRequest.DISPLAY_ONLY_WITH_PHONES_ENABLED);
+        } else if (UI.LIST_STARRED_ACTION.equals(action)) {
+            request.setActionCode(ContactsRequest.ACTION_STARRED);
+        } else if (UI.LIST_FREQUENT_ACTION.equals(action)) {
+            request.setActionCode(ContactsRequest.ACTION_FREQUENT);
+        } else if (UI.LIST_STREQUENT_ACTION.equals(action)) {
+            request.setActionCode(ContactsRequest.ACTION_STREQUENT);
+        } else if (UI.LIST_GROUP_ACTION.equals(action)) {
+            request.setActionCode(ContactsRequest.ACTION_GROUP);
+            String groupName = intent.getStringExtra(UI.GROUP_NAME_EXTRA_KEY);
+            if (TextUtils.isEmpty(groupName)) {
+                Log.e(TAG, "Intent missing a required extra: " + UI.GROUP_NAME_EXTRA_KEY);
+                request.setValid(false);
+            } else {
+                request.setGroupName(groupName);
+            }
         } else if (Intent.ACTION_PICK.equals(action)) {
-            // XXX These should be showing the data from the URI given in
-            // the Intent.
-           // TODO : Does it work in mSearchMode?
             final String resolvedType = intent.resolveType(mContext);
             if (Contacts.CONTENT_TYPE.equals(resolvedType)) {
-                mMode = MODE_PICK_CONTACT;
+                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
             } else if (People.CONTENT_TYPE.equals(resolvedType)) {
-                mMode = MODE_LEGACY_PICK_PERSON;
+                request.setActionCode(ContactsRequest.ACTION_PICK_CONTACT);
+                request.setLegacyCompatibilityMode(true);
             } else if (Phone.CONTENT_TYPE.equals(resolvedType)) {
-                mMode = MODE_PICK_PHONE;
+                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
             } else if (Phones.CONTENT_TYPE.equals(resolvedType)) {
-                mMode = MODE_LEGACY_PICK_PHONE;
+                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
+                request.setLegacyCompatibilityMode(true);
             } else if (StructuredPostal.CONTENT_TYPE.equals(resolvedType)) {
-                mMode = MODE_PICK_POSTAL;
+                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
             } else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(resolvedType)) {
-                mMode = MODE_LEGACY_PICK_POSTAL;
+                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
+                request.setLegacyCompatibilityMode(true);
             }
         } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
             if (component.equals("alias.DialShortcut")) {
-                mMode = MODE_PICK_PHONE;
-                mShortcutAction = Intent.ACTION_CALL;
-                mShowSearchSnippets = false;
-                mTitle = mContext.getString(R.string.callShortcutActivityTitle);
+                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CALL);
+                request.setActivityTitle(mContext.getString(R.string.callShortcutActivityTitle));
             } else if (component.equals("alias.MessageShortcut")) {
-                mMode = MODE_PICK_PHONE;
-                mShortcutAction = Intent.ACTION_SENDTO;
-                mShowSearchSnippets = false;
-                mTitle = mContext.getString(R.string.messageShortcutActivityTitle);
-            } else if (mSearchMode) {
-                mMode = MODE_PICK_CONTACT;
-                mShortcutAction = Intent.ACTION_VIEW;
-                mTitle = mContext.getString(R.string.shortcutActivityTitle);
+                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_SMS);
+                request.setActivityTitle(mContext.getString(R.string.messageShortcutActivityTitle));
             } else {
-                mMode = MODE_PICK_OR_CREATE_CONTACT;
-                mShortcutAction = Intent.ACTION_VIEW;
-                mTitle = mContext.getString(R.string.shortcutActivityTitle);
+                request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CONTACT);
+                request.setActivityTitle(mContext.getString(R.string.shortcutActivityTitle));
             }
         } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
             if (Contacts.CONTENT_ITEM_TYPE.equals(type)) {
-                if (mSearchMode) {
-                    mMode = MODE_PICK_CONTACT;
-                } else {
-                    mMode = MODE_PICK_OR_CREATE_CONTACT;
-                }
+                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
             } else if (Phone.CONTENT_ITEM_TYPE.equals(type)) {
-                mMode = MODE_PICK_PHONE;
+                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
             } else if (Phones.CONTENT_ITEM_TYPE.equals(type)) {
-                mMode = MODE_LEGACY_PICK_PHONE;
+                request.setActionCode(ContactsRequest.ACTION_PICK_PHONE);
+                request.setLegacyCompatibilityMode(true);
             } else if (StructuredPostal.CONTENT_ITEM_TYPE.equals(type)) {
-                mMode = MODE_PICK_POSTAL;
+                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
             } else if (ContactMethods.CONTENT_POSTAL_ITEM_TYPE.equals(type)) {
-                mMode = MODE_LEGACY_PICK_POSTAL;
+                request.setActionCode(ContactsRequest.ACTION_PICK_POSTAL);
+                request.setLegacyCompatibilityMode(true);
             }  else if (People.CONTENT_ITEM_TYPE.equals(type)) {
-                if (mSearchMode) {
-                    mMode = MODE_LEGACY_PICK_PERSON;
-                } else {
-                    mMode = MODE_LEGACY_PICK_OR_CREATE_PERSON;
-                }
+                request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
+                request.setLegacyCompatibilityMode(true);
             }
-
         } else if (Intent.ACTION_INSERT_OR_EDIT.equals(action)) {
-            mMode = MODE_INSERT_OR_EDIT_CONTACT;
+            request.setActionCode(ContactsRequest.ACTION_INSERT_OR_EDIT_CONTACT);
         } else if (Intent.ACTION_SEARCH.equals(action)) {
             // See if the suggestion was clicked with a search action key (call button)
             if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG))) {
@@ -343,54 +157,61 @@
                 if (!TextUtils.isEmpty(query)) {
                     Intent newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
                             Uri.fromParts("tel", query, null));
-                    mRedirectIntent = newIntent;
+                    request.setRedirectIntent(newIntent);
                 }
-                return;
-            }
-
-            // See if search request has extras to specify query
-            if (intent.hasExtra(Insert.EMAIL)) {
-                mMode = MODE_QUERY_PICK_TO_VIEW;
-                mQueryMode = QUERY_MODE_MAILTO;
-                mInitialFilter = intent.getStringExtra(Insert.EMAIL);
-            } else if (intent.hasExtra(Insert.PHONE)) {
-                mMode = MODE_QUERY_PICK_TO_VIEW;
-                mQueryMode = QUERY_MODE_TEL;
-                mInitialFilter = intent.getStringExtra(Insert.PHONE);
             } else {
-                // Otherwise handle the more normal search case
-                mMode = MODE_QUERY;
-                mShowSearchSnippets = true;
-                mInitialFilter = intent.getStringExtra(SearchManager.QUERY);
+                request.setQueryString(intent.getStringExtra(SearchManager.QUERY));
+                request.setSearchResultsMode(true);
             }
-            mSearchResultsMode = true;
-        } else if (ACTION_SEARCH_INTERNAL.equals(action)) {
-            String originalAction = null;
+        } else if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
+            // When we get a FILTER_CONTACTS_ACTION, it represents search in the context
+            // of some other action. Let's retrieve the original action to provide proper
+            // context for the search queries.
+            request.setActionCode(ContactsRequest.ACTION_DEFAULT);
             Bundle extras = intent.getExtras();
             if (extras != null) {
-                originalAction = extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
-            }
-            mShortcutAction = intent.getStringExtra(SHORTCUT_ACTION_KEY);
+                request.setQueryString(extras.getString(UI.FILTER_TEXT_EXTRA_KEY));
 
-            if (Intent.ACTION_INSERT_OR_EDIT.equals(originalAction)) {
-                mMode = MODE_QUERY_PICK_TO_EDIT;
-                mShowSearchSnippets = true;
-                mInitialFilter = intent.getStringExtra(SearchManager.QUERY);
-            } else if (mShortcutAction != null && intent.hasExtra(Insert.PHONE)) {
-                mMode = MODE_QUERY_PICK_PHONE;
-                mQueryMode = QUERY_MODE_TEL;
-                mInitialFilter = intent.getStringExtra(Insert.PHONE);
-            } else {
-                mMode = MODE_QUERY_PICK;
-                mQueryMode = QUERY_MODE_NONE;
-                mShowSearchSnippets = true;
-                mInitialFilter = intent.getStringExtra(SearchManager.QUERY);
+                ContactsRequest originalRequest =
+                        (ContactsRequest)extras.get(ContactsSearchManager.ORIGINAL_REQUEST_KEY);
+                if (originalRequest != null) {
+                    request.copyFrom(originalRequest);
+                }
             }
-            mSearchResultsMode = true;
+
+            if (request == null) {
+                request = new ContactsRequest();
+            }
+
+            request.setSearchMode(true);
+//        } else if (ACTION_SEARCH_INTERNAL.equals(action)) {
+//            String originalAction = null;
+//            Bundle extras = intent.getExtras();
+//            if (extras != null) {
+//                originalAction = extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
+//            }
+//            mShortcutAction = intent.getStringExtra(SHORTCUT_ACTION_KEY);
+//
+//            if (Intent.ACTION_INSERT_OR_EDIT.equals(originalAction)) {
+//                request.setActionCode(ContactsRequest.MODE_QUERY_PICK_TO_EDIT;
+//                mShowSearchSnippets = true;
+//                mQueryString = intent.getStringExtra(SearchManager.QUERY);
+//            } else if (mShortcutAction != null && intent.hasExtra(Insert.PHONE)) {
+//                request.setActionCode(ContactsRequest.MODE_QUERY_PICK_PHONE;
+//                mQueryMode = QUERY_MODE_TEL;
+//                mQueryString = intent.getStringExtra(Insert.PHONE);
+//            } else {
+//                request.setActionCode(ContactsRequest.MODE_QUERY_PICK;
+//                mQueryMode = QUERY_MODE_NONE;
+//                mShowSearchSnippets = true;
+//                mQueryString = intent.getStringExtra(SearchManager.QUERY);
+//            }
+//            mSearchResultsMode = true;
         // Since this is the filter activity it receives all intents
         // dispatched from the SearchManager for security reasons
         // so we need to re-dispatch from here to the intended target.
         } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) {
+            // TODO show the disambig dialog instead of guessing the number
             Uri data = intent.getData();
             Uri telUri = null;
             if (sContactsIdMatcher.match(data) == CONTACTS_ID) {
@@ -412,55 +233,29 @@
             } else {
                 newIntent = new Intent(Intent.ACTION_VIEW, data);
             }
-            mRedirectIntent = newIntent;
-            return;
+            request.setRedirectIntent(newIntent);
         } else if (Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED.equals(action)) {
             Intent newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData());
-            mRedirectIntent = newIntent;
-            return;
+            request.setRedirectIntent(newIntent);
         } else if (Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED.equals(action)) {
             // TODO actually support this in EditContactActivity.
             String number = intent.getData().getSchemeSpecificPart();
             Intent newIntent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
             newIntent.putExtra(Intents.Insert.PHONE, number);
-            mRedirectIntent = newIntent;
-            return;
-        } else if (JoinContactActivity.JOIN_CONTACT.equals(action)) {
-            mMode = MODE_PICK_CONTACT;
-        } else if (Intents.ACTION_GET_MULTIPLE_PHONES.equals(action)) {
-            if (mSearchMode) {
-                mShowSearchSnippets = false;
-            }
-            if (Phone.CONTENT_TYPE.equals(type)) {
-                mMode = MODE_PICK_MULTIPLE_PHONES;
-            } else {
-                // TODO support other content types
-                Log.e(TAG, "Intent " + action + " is not supported for type " + type);
-                mValid = false;
-            }
-        }
-        if (mMode == MODE_UNKNOWN) {
-            mMode = MODE_DEFAULT;
+            request.setRedirectIntent(newIntent);
         }
 
-        if (((mMode & MODE_MASK_SHOW_NUMBER_OF_CONTACTS) != 0 || mSearchMode)
-                && !mSearchResultsMode) {
-            mShowNumberOfContacts = true;
+        // Allow the title to be set to a custom String using an extra on the intent
+        String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
+        if (title != null) {
+            request.setActivityTitle(title);
         }
+
+        return request;
     }
 
-    public boolean isValid() {
-        return mValid;
-    }
-
-    public Intent getRedirectIntent() {
-        return mRedirectIntent;
-    }
-
-    public CharSequence getActivityTitle() {
-        return mTitle;
-    }
-
+    // TODO replace with a disabmig dialog
+    @Deprecated
     private Cursor queryPhoneNumbers(long contactId) {
         Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
         Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY);
@@ -474,21 +269,4 @@
         }
         return null;
     }
-
-    public boolean isVisibleContactsRestrictionEnabled() {
-        return mMode != MODE_CUSTOM && !mSearchResultsMode;
-    }
-
-    public boolean isContactsWithPhonesOnlyRestrictionEnabled() {
-        if (mMode == MODE_CUSTOM) {
-            return mDisplayOnlyPhones;
-        }
-
-        if (mMode == MODE_DEFAULT) {
-            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
-            return prefs.getBoolean(Prefs.DISPLAY_ONLY_PHONES, Prefs.DISPLAY_ONLY_PHONES_DEFAULT);
-        }
-
-        return false;
-    }
 }
diff --git a/src/com/android/contacts/list/ContactsRequest.java b/src/com/android/contacts/list/ContactsRequest.java
new file mode 100644
index 0000000..6f2e2e4
--- /dev/null
+++ b/src/com/android/contacts/list/ContactsRequest.java
@@ -0,0 +1,229 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+package com.android.contacts.list;
+
+import android.content.Intent;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+/**
+ * Parsed form of the intent sent to the Contacts application.
+ */
+public class ContactsRequest implements Parcelable {
+
+    /** Default mode: browse contacts */
+    public static final int ACTION_DEFAULT = 10;
+
+    /** Show contents of a specific group */
+    public static final int ACTION_GROUP = 20;
+
+    /** Show all starred contacts */
+    public static final int ACTION_STARRED = 30;
+
+    /** Show frequently contacted contacts */
+    public static final int ACTION_FREQUENT = 40;
+
+    /** Show starred and the frequent */
+    public static final int ACTION_STREQUENT = 50;
+
+    /** Show all contacts and pick them when clicking */
+    public static final int ACTION_PICK_CONTACT = 60;
+
+    /** Show all contacts as well as the option to create a new one */
+    public static final int ACTION_PICK_OR_CREATE_CONTACT = 70;
+
+    /** Show all contacts and pick them for edit when clicking, and allow creating a new contact */
+    public static final int ACTION_INSERT_OR_EDIT_CONTACT = 80;
+
+    /** Show all phone numbers and pick them when clicking */
+    public static final int ACTION_PICK_PHONE = 90;
+
+    /** Show all postal addresses and pick them when clicking */
+    public static final int ACTION_PICK_POSTAL = 100;
+
+    /** Show all contacts and create a shortcut for the picked contact */
+    public static final int ACTION_CREATE_SHORTCUT_CONTACT = 110;
+
+    /** Show all phone numbers and create a call shortcut for the picked number */
+    public static final int ACTION_CREATE_SHORTCUT_CALL = 120;
+
+    /** Show all phone numbers and create an SMS shortcut for the picked number */
+    public static final int ACTION_CREATE_SHORTCUT_SMS = 130;
+
+    private boolean mValid = true;
+    private int mActionCode = ACTION_DEFAULT;
+    private Intent mRedirectIntent;
+    private CharSequence mTitle;
+    private boolean mSearchMode;
+    private boolean mSearchResultsMode;
+    private String mQueryString;
+
+    public static final int DISPLAY_ONLY_WITH_PHONES_PREFERENCE = 0;
+    public static final int DISPLAY_ONLY_WITH_PHONES_ENABLED = 1;
+    public static final int DISPLAY_ONLY_WITH_PHONES_DISABLED = 2;
+
+    private int mDisplayOnlyWithPhones;
+    private boolean mDisplayOnlyVisible;
+    private String mGroupName;
+    private boolean mLegacyCompatibilityMode;
+
+    /**
+     * Copies all fields.
+     */
+    public void copyFrom(ContactsRequest request) {
+        mValid = request.mValid;
+        mActionCode = request.mActionCode;
+        mRedirectIntent = request.mRedirectIntent;
+        mTitle = request.mTitle;
+        mSearchMode = request.mSearchMode;
+        mSearchResultsMode = request.mSearchResultsMode;
+        mQueryString = request.mQueryString;
+        mDisplayOnlyWithPhones = request.mDisplayOnlyWithPhones;
+        mDisplayOnlyVisible = request.mDisplayOnlyVisible;
+        mGroupName = request.mGroupName;
+        mLegacyCompatibilityMode = request.mLegacyCompatibilityMode;
+    }
+
+    public static Parcelable.Creator<ContactsRequest> CREATOR = new Creator<ContactsRequest>() {
+
+        public ContactsRequest[] newArray(int size) {
+            return new ContactsRequest[size];
+        }
+
+        public ContactsRequest createFromParcel(Parcel source) {
+            ContactsRequest request = new ContactsRequest();
+            request.mValid = source.readInt() != 0;
+            request.mActionCode = source.readInt();
+            request.mRedirectIntent = source.readParcelable(this.getClass().getClassLoader());
+            request.mTitle = source.readCharSequence();
+            request.mSearchMode = source.readInt() != 0;
+            request.mSearchResultsMode = source.readInt() != 0;
+            request.mQueryString = source.readString();
+            request.mDisplayOnlyWithPhones = source.readInt();
+            request.mDisplayOnlyVisible = source.readInt() != 0;
+            request.mGroupName = source.readString();
+            request.mLegacyCompatibilityMode  = source.readInt() != 0;
+            return request;
+        }
+    };
+
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeInt(mValid ? 1 : 0);
+        dest.writeInt(mActionCode);
+        dest.writeParcelable(mRedirectIntent, 0);
+        dest.writeCharSequence(mTitle);
+        dest.writeInt(mSearchMode ? 1 : 0);
+        dest.writeInt(mSearchResultsMode ? 1 : 0);
+        dest.writeString(mQueryString);
+        dest.writeInt(mDisplayOnlyWithPhones);
+        dest.writeInt(mDisplayOnlyVisible ? 1 : 0);
+        dest.writeString(mGroupName);
+        dest.writeInt(mLegacyCompatibilityMode ? 1 : 0);
+    }
+
+    public int describeContents() {
+        return 0;
+    }
+
+    public boolean isValid() {
+        return mValid;
+    }
+
+    public void setValid(boolean flag) {
+        mValid = flag;
+    }
+
+    public Intent getRedirectIntent() {
+        return mRedirectIntent;
+    }
+
+    public void setRedirectIntent(Intent intent) {
+        mRedirectIntent = intent;
+    }
+
+    public void setActivityTitle(CharSequence title) {
+        mTitle = title;
+    }
+
+    public CharSequence getActivityTitle() {
+        return mTitle;
+    }
+
+    public int getActionCode() {
+        return mActionCode;
+    }
+
+    public void setActionCode(int actionCode) {
+        mActionCode = actionCode;
+    }
+
+    public boolean getDisplayOnlyVisible() {
+        return mDisplayOnlyVisible;
+    }
+
+    public void setDisplayOnlyVisible(boolean flag) {
+        mDisplayOnlyVisible = flag;
+    }
+
+    public int getDisplayWithPhonesOnlyOption() {
+        return mDisplayOnlyWithPhones;
+    }
+
+    public void setDisplayWithPhonesOnlyOption(int option) {
+        mDisplayOnlyWithPhones = option;
+    }
+
+    public boolean isSearchMode() {
+        return mSearchMode;
+    }
+
+    public void setSearchMode(boolean flag) {
+        mSearchMode = flag;
+    }
+
+    public boolean isSearchResultsMode() {
+        return mSearchResultsMode;
+    }
+
+    public void setSearchResultsMode(boolean flag) {
+        mSearchResultsMode = flag;
+    }
+
+    public String getQueryString() {
+        return mQueryString;
+    }
+
+    public void setQueryString(String string) {
+        mQueryString = string;
+    }
+
+    public String getGroupName() {
+        return mGroupName;
+    }
+
+    public void setGroupName(String groupName) {
+        mGroupName = groupName;
+    }
+
+    public boolean isLegacyCompatibilityMode() {
+        return mLegacyCompatibilityMode;
+    }
+
+    public void setLegacyCompatibilityMode(boolean flag) {
+        mLegacyCompatibilityMode = flag;
+    }
+}
diff --git a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
index 9ea68b7..45380bc 100644
--- a/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
+++ b/src/com/android/contacts/list/DefaultContactBrowseListFragment.java
@@ -16,8 +16,11 @@
 package com.android.contacts.list;
 
 import com.android.contacts.R;
+import com.android.contacts.ui.ContactsPreferencesActivity.Prefs;
 
+import android.content.SharedPreferences;
 import android.database.Cursor;
+import android.preference.PreferenceManager;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -31,7 +34,7 @@
 
     private boolean mEditMode;
     private boolean mCreateContactEnabled;
-    private boolean mContactsWithPhonesOnlyRestrictionEnabled;
+    private int mDisplayWithPhonesOnlyOption = ContactsRequest.DISPLAY_ONLY_WITH_PHONES_DISABLED;
     private boolean mVisibleContactsRestrictionEnabled = true;
 
     // TODO: Remove this horrible hack once the framework can lookup fragments via findFragmentById
@@ -40,21 +43,37 @@
 
     public DefaultContactBrowseListFragment() {
         setPhotoLoaderEnabled(true);
+        setSectionHeaderDisplayEnabled(true);
 
         sLastFragment = this;
     }
 
     @Override
     protected void prepareEmptyView() {
-        if (mContactsWithPhonesOnlyRestrictionEnabled) {
+        if (isShowingContactsWithPhonesOnly()) {
             setEmptyText(R.string.noContactsWithPhoneNumbers);
         } else {
             super.prepareEmptyView();
         }
     }
 
-    public void setContactsWithPhonesOnlyRestrictionEnabled(boolean flag) {
-        mContactsWithPhonesOnlyRestrictionEnabled = flag;
+    private boolean isShowingContactsWithPhonesOnly() {
+        switch (mDisplayWithPhonesOnlyOption) {
+            case ContactsRequest.DISPLAY_ONLY_WITH_PHONES_DISABLED:
+                return false;
+            case ContactsRequest.DISPLAY_ONLY_WITH_PHONES_ENABLED:
+                return true;
+            case ContactsRequest.DISPLAY_ONLY_WITH_PHONES_PREFERENCE:
+                SharedPreferences prefs = PreferenceManager
+                        .getDefaultSharedPreferences(getActivity());
+                return prefs.getBoolean(Prefs.DISPLAY_ONLY_PHONES,
+                        Prefs.DISPLAY_ONLY_PHONES_DEFAULT);
+        }
+        return false;
+    }
+
+    public void setDisplayWithPhonesOnlyOption(int displayWithPhonesOnly) {
+        mDisplayWithPhonesOnlyOption = displayWithPhonesOnly;
         configureAdapter();
     }
 
@@ -98,7 +117,7 @@
 
         DefaultContactListAdapter adapter = (DefaultContactListAdapter)getAdapter();
         if (adapter != null) {
-            adapter.setContactsWithPhoneNumbersOnly(mContactsWithPhonesOnlyRestrictionEnabled);
+            adapter.setContactsWithPhoneNumbersOnly(isShowingContactsWithPhonesOnly());
             adapter.setVisibleContactsOnly(mVisibleContactsRestrictionEnabled);
         }
     }
diff --git a/src/com/android/contacts/list/PhoneNumberPickerFragment.java b/src/com/android/contacts/list/PhoneNumberPickerFragment.java
index d52da50..6e2bb31 100644
--- a/src/com/android/contacts/list/PhoneNumberPickerFragment.java
+++ b/src/com/android/contacts/list/PhoneNumberPickerFragment.java
@@ -51,7 +51,7 @@
 
     @Override
     protected void onItemClick(int position, long id) {
-        if (!isLegacyCompatibility()) {
+        if (!isLegacyCompatibilityMode()) {
             PhoneNumberListAdapter adapter = (PhoneNumberListAdapter)getAdapter();
 //          if (adapter.isSearchAllContactsItemPosition(position)) {
 //              searchAllContacts();
@@ -68,7 +68,7 @@
 
     @Override
     protected ContactEntryListAdapter createListAdapter() {
-        if (!isLegacyCompatibility()) {
+        if (!isLegacyCompatibilityMode()) {
             PhoneNumberListAdapter adapter = new PhoneNumberListAdapter(getActivity());
             adapter.setSectionHeaderDisplayEnabled(true);
             adapter.setDisplayPhotos(true);
@@ -96,7 +96,7 @@
         if (mShortcutAction == null) {
             mListener.onPickPhoneNumberAction(uri);
         } else {
-            if (isLegacyCompatibility()) {
+            if (isLegacyCompatibilityMode()) {
                 throw new UnsupportedOperationException();
             }
             ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
diff --git a/src/com/android/contacts/list/PostalAddressPickerFragment.java b/src/com/android/contacts/list/PostalAddressPickerFragment.java
index a012768..734873b 100644
--- a/src/com/android/contacts/list/PostalAddressPickerFragment.java
+++ b/src/com/android/contacts/list/PostalAddressPickerFragment.java
@@ -41,7 +41,7 @@
 
     @Override
     protected void onItemClick(int position, long id) {
-        if (!isLegacyCompatibility()) {
+        if (!isLegacyCompatibilityMode()) {
             PostalAddressListAdapter adapter = (PostalAddressListAdapter)getAdapter();
 //          if (adapter.isSearchAllContactsItemPosition(position)) {
 //              searchAllContacts();
@@ -58,7 +58,7 @@
 
     @Override
     protected ContactEntryListAdapter createListAdapter() {
-        if (!isLegacyCompatibility()) {
+        if (!isLegacyCompatibilityMode()) {
             PostalAddressListAdapter adapter = new PostalAddressListAdapter(getActivity());
             adapter.setSectionHeaderDisplayEnabled(true);
             adapter.setDisplayPhotos(true);