LegacyPostalAddressListAdapter.java revision 3c46291ef057eaf7b7f8ca5971e59bebe734a660
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.content.ContentUris;
19import android.content.Context;
20import android.content.CursorLoader;
21import android.database.Cursor;
22import android.net.Uri;
23import android.provider.Contacts.ContactMethods;
24import android.provider.Contacts.People;
25import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
26import android.view.View;
27import android.view.ViewGroup;
28
29/**
30 * A cursor adapter for the ContactMethods.CONTENT_TYPE content type.
31 */
32@SuppressWarnings("deprecation")
33public class LegacyPostalAddressListAdapter extends ContactEntryListAdapter {
34
35    static final String[] POSTALS_PROJECTION = new String[] {
36        ContactMethods._ID,     // 0
37        ContactMethods.TYPE,    // 1
38        ContactMethods.LABEL,   // 2
39        ContactMethods.DATA,    // 3
40        People.DISPLAY_NAME,    // 4
41        People.PHONETIC_NAME,   // 5
42    };
43
44    public static final int POSTAL_ID_COLUMN_INDEX = 0;
45    public static final int POSTAL_TYPE_COLUMN_INDEX = 1;
46    public static final int POSTAL_LABEL_COLUMN_INDEX = 2;
47    public static final int POSTAL_NUMBER_COLUMN_INDEX = 3;
48    public static final int POSTAL_DISPLAY_NAME_COLUMN_INDEX = 4;
49    public static final int POSTAL_PHONETIC_NAME_COLUMN_INDEX = 5;
50
51    private CharSequence mUnknownNameText;
52
53    public LegacyPostalAddressListAdapter(Context context) {
54        super(context);
55        mUnknownNameText = context.getText(android.R.string.unknownName);
56    }
57
58    @Override
59    public void configureLoader(CursorLoader loader) {
60        loader.setUri(ContactMethods.CONTENT_URI);
61        loader.setProjection(POSTALS_PROJECTION);
62        loader.setSortOrder(People.DISPLAY_NAME);
63        loader.setSelection(ContactMethods.KIND + "=" + android.provider.Contacts.KIND_POSTAL);
64    }
65
66    @Override
67    public String getContactDisplayName() {
68        return getCursor().getString(POSTAL_DISPLAY_NAME_COLUMN_INDEX);
69    }
70
71    public Uri getContactMethodUri() {
72        Cursor cursor = getCursor();
73        long id = cursor.getLong(POSTAL_ID_COLUMN_INDEX);
74        return ContentUris.withAppendedId(ContactMethods.CONTENT_URI, id);
75    }
76
77    @Override
78    public View newView(Context context, Cursor cursor, ViewGroup parent) {
79        final ContactListItemView view = new ContactListItemView(context, null);
80        view.setUnknownNameText(mUnknownNameText);
81        return view;
82    }
83
84    @Override
85    public void bindView(View itemView, Context context, Cursor cursor) {
86        ContactListItemView view = (ContactListItemView)itemView;
87        bindName(view, cursor);
88        bindPostalAddress(view, cursor);
89    }
90
91    protected void bindName(final ContactListItemView view, Cursor cursor) {
92        view.showDisplayName(cursor, POSTAL_DISPLAY_NAME_COLUMN_INDEX, false, 0);
93        view.showPhoneticName(cursor, POSTAL_PHONETIC_NAME_COLUMN_INDEX);
94    }
95
96    protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
97        CharSequence label = null;
98        if (!cursor.isNull(POSTAL_TYPE_COLUMN_INDEX)) {
99            final int type = cursor.getInt(POSTAL_TYPE_COLUMN_INDEX);
100            final String customLabel = cursor.getString(POSTAL_LABEL_COLUMN_INDEX);
101
102            // TODO cache
103            label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel);
104        }
105        view.setLabel(label);
106        view.showData(cursor, POSTAL_NUMBER_COLUMN_INDEX);
107    }
108}
109