Now supporting legacy contact picker in the Loader/Fragment solution.

(Except search)

Change-Id: Ia36f286dc3009f0a388be2802d97ee296d74d1e8
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 03bf5e7..17c466d 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -607,6 +607,8 @@
                 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: {
                 ContactPickerFragment fragment = new ContactPickerFragment();
@@ -614,10 +616,16 @@
                     fragment.setSectionHeaderDisplayEnabled(true);
                 }
 
-                if (mMode == MODE_PICK_OR_CREATE_CONTACT) {
+                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() {
diff --git a/src/com/android/contacts/list/ContactListAdapter.java b/src/com/android/contacts/list/ContactListAdapter.java
index e13ba58..e6fe10a 100644
--- a/src/com/android/contacts/list/ContactListAdapter.java
+++ b/src/com/android/contacts/list/ContactListAdapter.java
@@ -32,51 +32,48 @@
  */
 public abstract class ContactListAdapter extends ContactEntryListAdapter {
 
-    protected 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
+    protected static final String[] 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.CONTACT_PRESENCE,              // 5
+        Contacts.PHOTO_ID,                      // 6
+        Contacts.LOOKUP_KEY,                    // 7
+        Contacts.PHONETIC_NAME,                 // 8
+        Contacts.HAS_PHONE_NUMBER,              // 9
     };
 
-    protected 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
+    protected static final String[] 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.CONTACT_PRESENCE,              // 5
+        Contacts.PHOTO_ID,                      // 6
+        Contacts.LOOKUP_KEY,                    // 7
+        Contacts.PHONETIC_NAME,                 // 8
+        Contacts.HAS_PHONE_NUMBER,              // 9
+        SearchSnippetColumns.SNIPPET_MIMETYPE,  // 10
+        SearchSnippetColumns.SNIPPET_DATA1,     // 11
+        SearchSnippetColumns.SNIPPET_DATA4,     // 12
     };
 
-    protected static final int SUMMARY_ID_COLUMN_INDEX = 0;
-    protected static final int SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 1;
-    protected static final int SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 2;
-    protected static final int SUMMARY_SORT_KEY_PRIMARY_COLUMN_INDEX = 3;
-    protected static final int SUMMARY_STARRED_COLUMN_INDEX = 4;
-    protected static final int SUMMARY_TIMES_CONTACTED_COLUMN_INDEX = 5;
-    protected static final int SUMMARY_PRESENCE_STATUS_COLUMN_INDEX = 6;
-    protected static final int SUMMARY_PHOTO_ID_COLUMN_INDEX = 7;
-    protected static final int SUMMARY_LOOKUP_KEY_COLUMN_INDEX = 8;
-    protected static final int SUMMARY_PHONETIC_NAME_COLUMN_INDEX = 9;
-    protected static final int SUMMARY_HAS_PHONE_COLUMN_INDEX = 10;
-    protected static final int SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX = 11;
-    protected static final int SUMMARY_SNIPPET_DATA1_COLUMN_INDEX = 12;
-    protected static final int SUMMARY_SNIPPET_DATA4_COLUMN_INDEX = 13;
+    protected static final int CONTACT_ID_COLUMN_INDEX = 0;
+    protected static final int CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX = 1;
+    protected static final int CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX = 2;
+    protected static final int CONTACT_SORT_KEY_PRIMARY_COLUMN_INDEX = 3;
+    protected static final int CONTACT_STARRED_COLUMN_INDEX = 4;
+    protected static final int CONTACT_PRESENCE_STATUS_COLUMN_INDEX = 5;
+    protected static final int CONTACT_PHOTO_ID_COLUMN_INDEX = 6;
+    protected static final int CONTACT_LOOKUP_KEY_COLUMN_INDEX = 7;
+    protected static final int CONTACT_PHONETIC_NAME_COLUMN_INDEX = 8;
+    protected static final int CONTACT_HAS_PHONE_COLUMN_INDEX = 9;
+    protected static final int CONTACT_SNIPPET_MIMETYPE_COLUMN_INDEX = 10;
+    protected static final int CONTACT_SNIPPET_DATA1_COLUMN_INDEX = 11;
+    protected static final int CONTACT_SNIPPET_DATA4_COLUMN_INDEX = 12;
 
     private boolean mQuickContactEnabled;
     private CharSequence mUnknownNameText;
@@ -99,11 +96,11 @@
     }
 
     public boolean getHasPhoneNumber() {
-        return getCursor().getInt(SUMMARY_HAS_PHONE_COLUMN_INDEX) != 0;
+        return getCursor().getInt(CONTACT_HAS_PHONE_COLUMN_INDEX) != 0;
     }
 
     public boolean isContactStarred() {
-        return getCursor().getInt(SUMMARY_STARRED_COLUMN_INDEX) != 0;
+        return getCursor().getInt(CONTACT_STARRED_COLUMN_INDEX) != 0;
     }
 
     @Override
@@ -123,11 +120,11 @@
     public void setContactNameDisplayOrder(int displayOrder) {
         super.setContactNameDisplayOrder(displayOrder);
         if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
-            mDisplayNameColumnIndex = SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
-            mAlternativeDisplayNameColumnIndex = SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
+            mDisplayNameColumnIndex = CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
+            mAlternativeDisplayNameColumnIndex = CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
         } else {
-            mDisplayNameColumnIndex = SUMMARY_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
-            mAlternativeDisplayNameColumnIndex = SUMMARY_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
+            mDisplayNameColumnIndex = CONTACT_DISPLAY_NAME_ALTERNATIVE_COLUMN_INDEX;
+            mAlternativeDisplayNameColumnIndex = CONTACT_DISPLAY_NAME_PRIMARY_COLUMN_INDEX;
         }
     }
 
@@ -137,8 +134,8 @@
      */
     public Uri getContactUri() {
         Cursor cursor = getCursor();
-        long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX);
-        String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY_COLUMN_INDEX);
+        long contactId = cursor.getLong(CONTACT_ID_COLUMN_INDEX);
+        String lookupKey = cursor.getString(CONTACT_LOOKUP_KEY_COLUMN_INDEX);
         return Contacts.getLookupUri(contactId, lookupKey);
     }
 
@@ -172,8 +169,8 @@
     protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
         // Set the photo, if available
         long photoId = 0;
-        if (!cursor.isNull(SUMMARY_PHOTO_ID_COLUMN_INDEX)) {
-            photoId = cursor.getLong(SUMMARY_PHOTO_ID_COLUMN_INDEX);
+        if (!cursor.isNull(CONTACT_PHOTO_ID_COLUMN_INDEX)) {
+            photoId = cursor.getLong(CONTACT_PHOTO_ID_COLUMN_INDEX);
         }
 
         getPhotoLoader().loadPhoto(view.getPhotoView(), photoId);
@@ -181,8 +178,8 @@
 
     protected void bindQuickContact(final ContactListItemView view, Cursor cursor) {
         long photoId = 0;
-        if (!cursor.isNull(SUMMARY_PHOTO_ID_COLUMN_INDEX)) {
-            photoId = cursor.getLong(SUMMARY_PHOTO_ID_COLUMN_INDEX);
+        if (!cursor.isNull(CONTACT_PHOTO_ID_COLUMN_INDEX)) {
+            photoId = cursor.getLong(CONTACT_PHOTO_ID_COLUMN_INDEX);
         }
 
         QuickContactBadge quickContact = view.getQuickContact();
@@ -193,15 +190,15 @@
     protected void bindName(final ContactListItemView view, Cursor cursor) {
         view.showDisplayName(cursor, mDisplayNameColumnIndex, isNameHighlightingEnabled(),
                 mAlternativeDisplayNameColumnIndex);
-        view.showPhoneticName(cursor, SUMMARY_PHONETIC_NAME_COLUMN_INDEX);
+        view.showPhoneticName(cursor, CONTACT_PHONETIC_NAME_COLUMN_INDEX);
     }
 
     protected void bindPresence(final ContactListItemView view, Cursor cursor) {
-        view.showPresence(cursor, SUMMARY_PRESENCE_STATUS_COLUMN_INDEX);
+        view.showPresence(cursor, CONTACT_PRESENCE_STATUS_COLUMN_INDEX);
     }
 
     protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
-        view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
-                SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
+        view.showSnippet(cursor, CONTACT_SNIPPET_MIMETYPE_COLUMN_INDEX,
+                CONTACT_SNIPPET_DATA1_COLUMN_INDEX, CONTACT_SNIPPET_DATA4_COLUMN_INDEX);
     }
 }
diff --git a/src/com/android/contacts/list/ContactPickerFragment.java b/src/com/android/contacts/list/ContactPickerFragment.java
index 78c5493..0cbfcba 100644
--- a/src/com/android/contacts/list/ContactPickerFragment.java
+++ b/src/com/android/contacts/list/ContactPickerFragment.java
@@ -29,7 +29,7 @@
  * Fragment for the contact list used for browsing contacts (as compared to
  * picking a contact with one of the PICK or SHORTCUT intents).
  */
-public class ContactPickerFragment extends ContactEntryListFragment<ContactListAdapter>
+public class ContactPickerFragment extends ContactEntryListFragment<ContactEntryListAdapter>
         implements OnShortcutIntentCreatedListener {
 
     private OnContactPickerActionListener mListener;
@@ -83,32 +83,46 @@
         if (isSearchAllContactsItemPosition(position)) {
             mListener.onSearchAllContactsAction((String)null);
         } else {
-            ContactListAdapter adapter = getAdapter();
+            Uri uri;
+            ContactEntryListAdapter adapter = getAdapter();
             adapter.moveToPosition(position);
+            if (isLegacyCompatibility()) {
+                uri = ((LegacyContactListAdapter)adapter).getPersonUri();
+            } else {
+                uri = ((ContactListAdapter)adapter).getContactUri();
+            }
             if (mShortcutRequested) {
                 ShortcutIntentBuilder builder = new ShortcutIntentBuilder(getActivity(), this);
-                builder.createContactShortcutIntent(adapter.getContactUri());
+                builder.createContactShortcutIntent(uri);
             } else {
-                mListener.onPickContactAction(adapter.getContactUri());
+                mListener.onPickContactAction(uri);
             }
         }
     }
 
     @Override
-    protected ContactListAdapter createListAdapter() {
-        ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
-        adapter.setSectionHeaderDisplayEnabled(true);
+    protected ContactEntryListAdapter createListAdapter() {
+        if (!isLegacyCompatibility()) {
+            ContactListAdapter adapter = new DefaultContactListAdapter(getActivity());
+            adapter.setSectionHeaderDisplayEnabled(true);
+            adapter.setDisplayPhotos(true);
+            adapter.setQuickContactEnabled(false);
+            return adapter;
+        } else {
+            LegacyContactListAdapter adapter = new LegacyContactListAdapter(getActivity());
+            adapter.setSectionHeaderDisplayEnabled(false);
+            adapter.setDisplayPhotos(false);
+            return adapter;
+        }
+    }
 
-        adapter.setDisplayPhotos(true);
-        adapter.setQuickContactEnabled(false);
-
-        adapter.setSearchMode(isSearchMode());
-        adapter.setSearchResultsMode(isSearchResultsMode());
+    @Override
+    protected void configureAdapter() {
+        super.configureAdapter();
+        ContactEntryListAdapter adapter = getAdapter();
 
         // If "Create new contact" is shown, don't display the empty list UI
         adapter.setEmptyListEnabled(!isCreateContactEnabled());
-
-        return adapter;
     }
 
     @Override
diff --git a/src/com/android/contacts/list/DefaultContactListAdapter.java b/src/com/android/contacts/list/DefaultContactListAdapter.java
index 5322041..0a108d2 100644
--- a/src/com/android/contacts/list/DefaultContactListAdapter.java
+++ b/src/com/android/contacts/list/DefaultContactListAdapter.java
@@ -47,7 +47,7 @@
             String query = getQueryString();
             uri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
                     TextUtils.isEmpty(query) ? "" : Uri.encode(query));
-            loader.setProjection(CONTACTS_SUMMARY_FILTER_PROJECTION);
+            loader.setProjection(FILTER_PROJECTION);
             if (!isSearchResultsMode()) {
                 if (mContactsWithPhoneNumbersOnly) {
                     loader.setSelection(Contacts.IN_VISIBLE_GROUP + "=1 AND "
@@ -58,7 +58,7 @@
             }
         } else {
             uri = Contacts.CONTENT_URI;
-            loader.setProjection(CONTACTS_SUMMARY_PROJECTION);
+            loader.setProjection(PROJECTION);
             if (mContactsWithPhoneNumbersOnly) {
                 loader.setSelection(Contacts.IN_VISIBLE_GROUP + "=1 AND "
                         + Contacts.HAS_PHONE_NUMBER + "=1");
diff --git a/src/com/android/contacts/list/LegacyContactListAdapter.java b/src/com/android/contacts/list/LegacyContactListAdapter.java
new file mode 100644
index 0000000..fdca9b0
--- /dev/null
+++ b/src/com/android/contacts/list/LegacyContactListAdapter.java
@@ -0,0 +1,104 @@
+/*
+ * 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.app.patterns.CursorLoader;
+import android.content.ContentUris;
+import android.content.Context;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.Contacts.People;
+import android.provider.ContactsContract.Contacts;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ListView;
+
+/**
+ * A cursor adapter for the People.CONTENT_TYPE content type.
+ */
+@SuppressWarnings("deprecation")
+public class LegacyContactListAdapter extends ContactEntryListAdapter {
+
+    static final String[] PEOPLE_PROJECTION = new String[] {
+        People._ID,                         // 0
+        People.DISPLAY_NAME,                // 1
+        People.PHONETIC_NAME,               // 2
+        People.STARRED,                     // 3
+        People.PRESENCE_STATUS,             // 4
+    };
+
+    protected static final int PERSON_ID_COLUMN_INDEX = 0;
+    protected static final int PERSON_DISPLAY_NAME_COLUMN_INDEX = 1;
+    protected static final int PERSON_PHONETIC_NAME_COLUMN_INDEX = 2;
+    protected static final int PERSON_STARRED_COLUMN_INDEX = 3;
+    protected static final int PERSON_PRESENCE_STATUS_COLUMN_INDEX = 4;
+
+    private CharSequence mUnknownNameText;
+
+    public LegacyContactListAdapter(Context context) {
+        super(context);
+        mUnknownNameText = context.getText(android.R.string.unknownName);
+    }
+
+    @Override
+    public void configureLoader(CursorLoader loader) {
+        loader.setUri(People.CONTENT_URI);
+        loader.setProjection(PEOPLE_PROJECTION);
+        loader.setSortOrder(People.DISPLAY_NAME);
+    }
+
+    public boolean isContactStarred() {
+        return getCursor().getInt(PERSON_STARRED_COLUMN_INDEX) != 0;
+    }
+
+    @Override
+    public String getContactDisplayName() {
+        return getCursor().getString(PERSON_DISPLAY_NAME_COLUMN_INDEX);
+    }
+
+    /**
+     * Builds the {@link Contacts#CONTENT_LOOKUP_URI} for the given
+     * {@link ListView} position.
+     */
+    public Uri getPersonUri() {
+        Cursor cursor = getCursor();
+        long personId = cursor.getLong(PERSON_ID_COLUMN_INDEX);
+        return ContentUris.withAppendedId(People.CONTENT_URI, personId);
+    }
+
+    @Override
+    public View newView(Context context, Cursor cursor, ViewGroup parent) {
+        final ContactListItemView view = new ContactListItemView(context, null);
+        view.setUnknownNameText(mUnknownNameText);
+        return view;
+    }
+
+    @Override
+    public void bindView(View itemView, Context context, Cursor cursor) {
+        ContactListItemView view = (ContactListItemView)itemView;
+        bindName(view, cursor);
+        bindPresence(view, cursor);
+    }
+
+    protected void bindName(final ContactListItemView view, Cursor cursor) {
+        view.showDisplayName(cursor, PERSON_DISPLAY_NAME_COLUMN_INDEX, false, 0);
+        view.showPhoneticName(cursor, PERSON_PHONETIC_NAME_COLUMN_INDEX);
+    }
+
+    protected void bindPresence(final ContactListItemView view, Cursor cursor) {
+        view.showPresence(cursor, PERSON_PRESENCE_STATUS_COLUMN_INDEX);
+    }
+}
diff --git a/src/com/android/contacts/list/StrequentContactListAdapter.java b/src/com/android/contacts/list/StrequentContactListAdapter.java
index 8760160..239f804 100644
--- a/src/com/android/contacts/list/StrequentContactListAdapter.java
+++ b/src/com/android/contacts/list/StrequentContactListAdapter.java
@@ -52,7 +52,7 @@
     @Override
     public void configureLoader(CursorLoader loader) {
         loader.setUri(Contacts.CONTENT_STREQUENT_URI);
-        loader.setProjection(CONTACTS_SUMMARY_PROJECTION);
+        loader.setProjection(PROJECTION);
         if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
             loader.setSortOrder(Contacts.SORT_KEY_PRIMARY);
         } else {
@@ -70,7 +70,7 @@
         if (cursor != null && (count = cursor.getCount()) > 0) {
             cursor.moveToPosition(-1);
             for (int i = 0; cursor.moveToNext(); i++) {
-                int starred = cursor.getInt(SUMMARY_STARRED_COLUMN_INDEX);
+                int starred = cursor.getInt(CONTACT_STARRED_COLUMN_INDEX);
                 if (starred == 0) {
                     if (i > 0) {
                         // Only add the separator when there are starred items present
@@ -169,6 +169,5 @@
         } else {
             view.hideCallButton();
         }
-
     }
 }