Postal address picker now works with Loaders and Fragments.

(except search)

Change-Id: Ib7f20270a71a5234d505696e76e1f97f659eef22
diff --git a/src/com/android/contacts/ContactsListActivity.java b/src/com/android/contacts/ContactsListActivity.java
index 512b182..e0634ec 100644
--- a/src/com/android/contacts/ContactsListActivity.java
+++ b/src/com/android/contacts/ContactsListActivity.java
@@ -29,7 +29,9 @@
 import com.android.contacts.list.OnContactBrowserActionListener;
 import com.android.contacts.list.OnContactPickerActionListener;
 import com.android.contacts.list.OnPhoneNumberPickerActionListener;
+import com.android.contacts.list.OnPostalAddressPickerActionListener;
 import com.android.contacts.list.PhoneNumberPickerFragment;
+import com.android.contacts.list.PostalAddressPickerFragment;
 import com.android.contacts.list.StrequentContactListFragment;
 import com.android.contacts.model.ContactsSource;
 import com.android.contacts.model.Sources;
@@ -713,10 +715,25 @@
             }
             case MODE_LEGACY_PICK_POSTAL:
             case MODE_PICK_POSTAL: {
-                mListFragment = new DefaultContactListFragment();
-                if (mMode == MODE_LEGACY_PICK_POSTAL) {
-                    mListFragment.setLegacyCompatibility(true);
+                PostalAddressPickerFragment fragment = new PostalAddressPickerFragment();
+                if (mMode == MODE_LEGACY_PICK_PHONE) {
+                    fragment.setLegacyCompatibility(true);
                 }
+                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();
+                    }
+                });
+                mListFragment = fragment;
                 break;
             }
             case MODE_PICK_MULTIPLE_PHONES: {
diff --git a/src/com/android/contacts/list/OnPostalAddressPickerActionListener.java b/src/com/android/contacts/list/OnPostalAddressPickerActionListener.java
new file mode 100644
index 0000000..a43dfe4
--- /dev/null
+++ b/src/com/android/contacts/list/OnPostalAddressPickerActionListener.java
@@ -0,0 +1,34 @@
+/*
+ * 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.net.Uri;
+
+/**
+ * Action callbacks that can be sent by a postal address picker.
+ */
+public interface OnPostalAddressPickerActionListener  {
+
+    /**
+     * Returns the selected phone number to the requester.
+     */
+    void onPickPostalAddressAction(Uri dataUri);
+
+    /**
+     * Searches all contacts for the specified string an show results for browsing.
+     */
+    void onSearchAllContactsAction(String string);
+}
diff --git a/src/com/android/contacts/list/PhoneNumberPickerFragment.java b/src/com/android/contacts/list/PhoneNumberPickerFragment.java
index f155637..315ac82 100644
--- a/src/com/android/contacts/list/PhoneNumberPickerFragment.java
+++ b/src/com/android/contacts/list/PhoneNumberPickerFragment.java
@@ -25,8 +25,7 @@
 import android.view.ViewGroup;
 
 /**
- * Fragment containing a contact list used for browsing (as compared to
- * picking a contact with one of the PICK intents).
+ * Fragment containing a phone number list for picking.
  */
 public class PhoneNumberPickerFragment extends ContactEntryListFragment<PhoneNumberListAdapter>
         implements OnShortcutIntentCreatedListener {
diff --git a/src/com/android/contacts/list/PostalAddressListAdapter.java b/src/com/android/contacts/list/PostalAddressListAdapter.java
new file mode 100644
index 0000000..1b645fe
--- /dev/null
+++ b/src/com/android/contacts/list/PostalAddressListAdapter.java
@@ -0,0 +1,177 @@
+/*
+ * 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.ContactsContract;
+import android.provider.ContactsContract.ContactCounts;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.CommonDataKinds.Phone;
+import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * A cursor adapter for the {@link StructuredPostal#CONTENT_TYPE} content type.
+ */
+public class PostalAddressListAdapter extends ContactEntryListAdapter {
+
+    static final String[] POSTALS_PROJECTION = new String[] {
+        StructuredPostal._ID,                       // 0
+        StructuredPostal.TYPE,                      // 1
+        StructuredPostal.LABEL,                     // 2
+        StructuredPostal.DATA,                      // 3
+        StructuredPostal.DISPLAY_NAME_PRIMARY,      // 4
+        StructuredPostal.DISPLAY_NAME_ALTERNATIVE,  // 5
+        StructuredPostal.PHOTO_ID,                  // 6
+    };
+
+    protected static final int POSTAL_ID_COLUMN_INDEX = 0;
+    protected static final int POSTAL_TYPE_COLUMN_INDEX = 1;
+    protected static final int POSTAL_LABEL_COLUMN_INDEX = 2;
+    protected static final int POSTAL_ADDRESS_COLUMN_INDEX = 3;
+    protected static final int POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX = 4;
+    protected static final int POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX = 5;
+    protected static final int POSTAL_PHOTO_ID_COLUMN_INDEX = 6;
+
+    private CharSequence mUnknownNameText;
+    private int mDisplayNameColumnIndex;
+    private int mAlternativeDisplayNameColumnIndex;
+
+    public PostalAddressListAdapter(Context context) {
+        super(context);
+
+        mUnknownNameText = context.getText(android.R.string.unknownName);
+    }
+
+    @Override
+    public void configureLoader(CursorLoader loader) {
+        loader.setUri(buildSectionIndexerUri(StructuredPostal.CONTENT_URI));
+        loader.setProjection(POSTALS_PROJECTION);
+
+        if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
+            loader.setSortOrder(StructuredPostal.SORT_KEY_PRIMARY);
+        } else {
+            loader.setSortOrder(StructuredPostal.SORT_KEY_ALTERNATIVE);
+        }
+    }
+
+    protected static Uri buildSectionIndexerUri(Uri uri) {
+        return uri.buildUpon()
+                .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true").build();
+    }
+
+    @Override
+    public String getContactDisplayName() {
+        return getCursor().getString(mDisplayNameColumnIndex);
+    }
+
+    @Override
+    public void setContactNameDisplayOrder(int displayOrder) {
+        super.setContactNameDisplayOrder(displayOrder);
+        if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
+            mDisplayNameColumnIndex = POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX;
+            mAlternativeDisplayNameColumnIndex = POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX;
+        } else {
+            mDisplayNameColumnIndex = POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX;
+            mAlternativeDisplayNameColumnIndex = POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX;
+        }
+    }
+
+    /**
+     * Builds a {@link Data#CONTENT_URI} for the current cursor
+     * position.
+     */
+    public Uri getDataUri() {
+        Cursor cursor = getCursor();
+        long id = cursor.getLong(POSTAL_ID_COLUMN_INDEX);
+        return ContentUris.withAppendedId(Data.CONTENT_URI, id);
+    }
+
+    @Override
+    public View newView(Context context, Cursor cursor, ViewGroup parent) {
+        final ContactListItemView view = new ContactListItemView(context, null);
+        view.setUnknownNameText(mUnknownNameText);
+        view.setTextWithHighlightingFactory(getTextWithHighlightingFactory());
+        return view;
+    }
+
+    @Override
+    public void bindView(View itemView, Context context, Cursor cursor) {
+        ContactListItemView view = (ContactListItemView)itemView;
+        bindSectionHeaderAndDivider(view, cursor);
+        bindName(view, cursor);
+        bindPhoto(view, cursor);
+        bindPostalAddress(view, cursor);
+    }
+
+    protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
+        CharSequence label = null;
+        if (!cursor.isNull(POSTAL_TYPE_COLUMN_INDEX)) {
+            final int type = cursor.getInt(POSTAL_TYPE_COLUMN_INDEX);
+            final String customLabel = cursor.getString(POSTAL_LABEL_COLUMN_INDEX);
+
+            // TODO cache
+            label = StructuredPostal.getTypeLabel(getContext().getResources(), type, label);
+        }
+        view.setLabel(label);
+        view.showData(cursor, POSTAL_ADDRESS_COLUMN_INDEX);
+    }
+
+    protected void bindSectionHeaderAndDivider(final ContactListItemView view, Cursor cursor) {
+        final int position = cursor.getPosition();
+        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);
+        }
+    }
+
+    protected void bindName(final ContactListItemView view, Cursor cursor) {
+        view.showDisplayName(cursor, mDisplayNameColumnIndex, isNameHighlightingEnabled(),
+                mAlternativeDisplayNameColumnIndex);
+//        view.showPhoneticName(cursor, PHONE_PHONETIC_NAME_COLUMN_INDEX);
+    }
+
+    protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
+        long photoId = 0;
+        if (!cursor.isNull(POSTAL_PHOTO_ID_COLUMN_INDEX)) {
+            photoId = cursor.getLong(POSTAL_PHOTO_ID_COLUMN_INDEX);
+        }
+
+        getPhotoLoader().loadPhoto(view.getPhotoView(), photoId);
+    }
+//
+//    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);
+//    }
+
+}
diff --git a/src/com/android/contacts/list/PostalAddressPickerFragment.java b/src/com/android/contacts/list/PostalAddressPickerFragment.java
new file mode 100644
index 0000000..133ce10
--- /dev/null
+++ b/src/com/android/contacts/list/PostalAddressPickerFragment.java
@@ -0,0 +1,84 @@
+/*
+ * 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.R;
+
+import android.net.Uri;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+/**
+ * Fragment containing a postal address list for picking.
+ */
+public class PostalAddressPickerFragment
+        extends ContactEntryListFragment<PostalAddressListAdapter> {
+    private OnPostalAddressPickerActionListener mListener;
+
+    public PostalAddressPickerFragment() {
+        setPhotoLoaderEnabled(true);
+        setSectionHeaderDisplayEnabled(true);
+    }
+
+    public void setOnPostalAddressPickerActionListener(
+            OnPostalAddressPickerActionListener listener) {
+        this.mListener = listener;
+    }
+
+    @Override
+    protected void onItemClick(int position, long id) {
+        PostalAddressListAdapter adapter = getAdapter();
+//        if (adapter.isSearchAllContactsItemPosition(position)) {
+//            searchAllContacts();
+//        } else {
+        adapter.moveToPosition(position);
+        pickPostalAddress(adapter.getDataUri());
+//        }
+    }
+
+    @Override
+    protected PostalAddressListAdapter createListAdapter() {
+        PostalAddressListAdapter adapter = new PostalAddressListAdapter(getActivity());
+
+        adapter.setSectionHeaderDisplayEnabled(true);
+        adapter.setDisplayPhotos(true);
+
+        adapter.setSearchMode(isSearchMode());
+        adapter.setSearchResultsMode(isSearchResultsMode());
+        adapter.setQueryString(getQueryString());
+
+        adapter.setContactNameDisplayOrder(getContactNameDisplayOrder());
+        adapter.setSortOrder(getSortOrder());
+
+        return adapter;
+    }
+
+    @Override
+    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
+        if (isSearchMode()) {
+            return inflater.inflate(R.layout.contacts_search_content, null);
+        } else if (isSearchResultsMode()) {
+            return inflater.inflate(R.layout.contacts_list_search_results, null);
+        } else {
+            return inflater.inflate(R.layout.contacts_list_content, null);
+        }
+    }
+
+    public void pickPostalAddress(Uri uri) {
+        mListener.onPickPostalAddressAction(uri);
+    }
+}