PostalAddressListAdapter.java revision d820cdbefa159bdf4c281ac8102805fe9a165379
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.contacts.list;
17
18import android.app.patterns.CursorLoader;
19import android.content.ContentUris;
20import android.content.Context;
21import android.database.Cursor;
22import android.net.Uri;
23import android.provider.ContactsContract;
24import android.provider.ContactsContract.ContactCounts;
25import android.provider.ContactsContract.Data;
26import android.provider.ContactsContract.CommonDataKinds.Phone;
27import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
28import android.view.View;
29import android.view.ViewGroup;
30
31/**
32 * A cursor adapter for the {@link StructuredPostal#CONTENT_TYPE} content type.
33 */
34public class PostalAddressListAdapter extends ContactEntryListAdapter {
35
36    static final String[] POSTALS_PROJECTION = new String[] {
37        StructuredPostal._ID,                       // 0
38        StructuredPostal.TYPE,                      // 1
39        StructuredPostal.LABEL,                     // 2
40        StructuredPostal.DATA,                      // 3
41        StructuredPostal.DISPLAY_NAME_PRIMARY,      // 4
42        StructuredPostal.DISPLAY_NAME_ALTERNATIVE,  // 5
43        StructuredPostal.PHOTO_ID,                  // 6
44    };
45
46    protected static final int POSTAL_ID_COLUMN_INDEX = 0;
47    protected static final int POSTAL_TYPE_COLUMN_INDEX = 1;
48    protected static final int POSTAL_LABEL_COLUMN_INDEX = 2;
49    protected static final int POSTAL_ADDRESS_COLUMN_INDEX = 3;
50    protected static final int POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX = 4;
51    protected static final int POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX = 5;
52    protected static final int POSTAL_PHOTO_ID_COLUMN_INDEX = 6;
53
54    private CharSequence mUnknownNameText;
55    private int mDisplayNameColumnIndex;
56    private int mAlternativeDisplayNameColumnIndex;
57
58    public PostalAddressListAdapter(Context context) {
59        super(context);
60
61        mUnknownNameText = context.getText(android.R.string.unknownName);
62    }
63
64    @Override
65    public void configureLoader(CursorLoader loader) {
66        loader.setUri(buildSectionIndexerUri(StructuredPostal.CONTENT_URI));
67        loader.setProjection(POSTALS_PROJECTION);
68
69        if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
70            loader.setSortOrder(StructuredPostal.SORT_KEY_PRIMARY);
71        } else {
72            loader.setSortOrder(StructuredPostal.SORT_KEY_ALTERNATIVE);
73        }
74    }
75
76    protected static Uri buildSectionIndexerUri(Uri uri) {
77        return uri.buildUpon()
78                .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true").build();
79    }
80
81    @Override
82    public String getContactDisplayName() {
83        return getCursor().getString(mDisplayNameColumnIndex);
84    }
85
86    @Override
87    public void setContactNameDisplayOrder(int displayOrder) {
88        super.setContactNameDisplayOrder(displayOrder);
89        if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
90            mDisplayNameColumnIndex = POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX;
91            mAlternativeDisplayNameColumnIndex = POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX;
92        } else {
93            mDisplayNameColumnIndex = POSTAL_ALTERNATIVE_DISPLAY_NAME_COLUMN_INDEX;
94            mAlternativeDisplayNameColumnIndex = POSTAL_PRIMARY_DISPLAY_NAME_COLUMN_INDEX;
95        }
96    }
97
98    /**
99     * Builds a {@link Data#CONTENT_URI} for the current cursor
100     * position.
101     */
102    public Uri getDataUri() {
103        Cursor cursor = getCursor();
104        long id = cursor.getLong(POSTAL_ID_COLUMN_INDEX);
105        return ContentUris.withAppendedId(Data.CONTENT_URI, id);
106    }
107
108    @Override
109    public View newView(Context context, Cursor cursor, ViewGroup parent) {
110        final ContactListItemView view = new ContactListItemView(context, null);
111        view.setUnknownNameText(mUnknownNameText);
112        view.setTextWithHighlightingFactory(getTextWithHighlightingFactory());
113        return view;
114    }
115
116    @Override
117    public void bindView(View itemView, Context context, Cursor cursor) {
118        ContactListItemView view = (ContactListItemView)itemView;
119        bindSectionHeaderAndDivider(view, cursor);
120        bindName(view, cursor);
121        bindPhoto(view, cursor);
122        bindPostalAddress(view, cursor);
123    }
124
125    protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
126        CharSequence label = null;
127        if (!cursor.isNull(POSTAL_TYPE_COLUMN_INDEX)) {
128            final int type = cursor.getInt(POSTAL_TYPE_COLUMN_INDEX);
129            final String customLabel = cursor.getString(POSTAL_LABEL_COLUMN_INDEX);
130
131            // TODO cache
132            label = StructuredPostal.getTypeLabel(getContext().getResources(), type, label);
133        }
134        view.setLabel(label);
135        view.showData(cursor, POSTAL_ADDRESS_COLUMN_INDEX);
136    }
137
138    protected void bindSectionHeaderAndDivider(final ContactListItemView view, Cursor cursor) {
139        final int position = cursor.getPosition();
140        final int section = getSectionForPosition(position);
141        if (getPositionForSection(section) == position) {
142            String title = (String)getSections()[section];
143            view.setSectionHeader(title);
144        } else {
145            view.setDividerVisible(false);
146            view.setSectionHeader(null);
147        }
148
149        // move the divider for the last item in a section
150        if (getPositionForSection(section + 1) - 1 == position) {
151            view.setDividerVisible(false);
152        } else {
153            view.setDividerVisible(true);
154        }
155    }
156
157    protected void bindName(final ContactListItemView view, Cursor cursor) {
158        view.showDisplayName(cursor, mDisplayNameColumnIndex, isNameHighlightingEnabled(),
159                mAlternativeDisplayNameColumnIndex);
160//        view.showPhoneticName(cursor, PHONE_PHONETIC_NAME_COLUMN_INDEX);
161    }
162
163    protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
164        long photoId = 0;
165        if (!cursor.isNull(POSTAL_PHOTO_ID_COLUMN_INDEX)) {
166            photoId = cursor.getLong(POSTAL_PHOTO_ID_COLUMN_INDEX);
167        }
168
169        getPhotoLoader().loadPhoto(view.getPhotoView(), photoId);
170    }
171//
172//    protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
173//        view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
174//                SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
175//    }
176
177}
178