Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 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 | */ |
| 16 | package com.android.contacts.list; |
| 17 | |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 18 | import android.content.ContentUris; |
| 19 | import android.content.Context; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 20 | import android.content.CursorLoader; |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 21 | import android.database.Cursor; |
| 22 | import android.net.Uri; |
| 23 | import android.provider.Contacts.ContactMethods; |
| 24 | import android.provider.Contacts.People; |
| 25 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
| 26 | import android.view.View; |
| 27 | import android.view.ViewGroup; |
| 28 | |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 29 | import com.android.contacts.common.list.ContactEntryListAdapter; |
| 30 | import com.android.contacts.common.list.ContactListItemView; |
| 31 | |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 32 | /** |
| 33 | * A cursor adapter for the ContactMethods.CONTENT_TYPE content type. |
| 34 | */ |
| 35 | @SuppressWarnings("deprecation") |
| 36 | public class LegacyPostalAddressListAdapter extends ContactEntryListAdapter { |
| 37 | |
| 38 | static final String[] POSTALS_PROJECTION = new String[] { |
| 39 | ContactMethods._ID, // 0 |
| 40 | ContactMethods.TYPE, // 1 |
| 41 | ContactMethods.LABEL, // 2 |
| 42 | ContactMethods.DATA, // 3 |
| 43 | People.DISPLAY_NAME, // 4 |
| 44 | People.PHONETIC_NAME, // 5 |
| 45 | }; |
| 46 | |
| 47 | public static final int POSTAL_ID_COLUMN_INDEX = 0; |
| 48 | public static final int POSTAL_TYPE_COLUMN_INDEX = 1; |
| 49 | public static final int POSTAL_LABEL_COLUMN_INDEX = 2; |
| 50 | public static final int POSTAL_NUMBER_COLUMN_INDEX = 3; |
| 51 | public static final int POSTAL_DISPLAY_NAME_COLUMN_INDEX = 4; |
| 52 | public static final int POSTAL_PHONETIC_NAME_COLUMN_INDEX = 5; |
| 53 | |
| 54 | private CharSequence mUnknownNameText; |
| 55 | |
| 56 | public LegacyPostalAddressListAdapter(Context context) { |
| 57 | super(context); |
| 58 | mUnknownNameText = context.getText(android.R.string.unknownName); |
| 59 | } |
| 60 | |
| 61 | @Override |
Dmitri Plotnikov | d5061fe | 2010-06-07 15:06:58 -0700 | [diff] [blame] | 62 | public void configureLoader(CursorLoader loader, long directoryId) { |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 63 | loader.setUri(ContactMethods.CONTENT_URI); |
| 64 | loader.setProjection(POSTALS_PROJECTION); |
| 65 | loader.setSortOrder(People.DISPLAY_NAME); |
| 66 | loader.setSelection(ContactMethods.KIND + "=" + android.provider.Contacts.KIND_POSTAL); |
| 67 | } |
| 68 | |
| 69 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 70 | public String getContactDisplayName(int position) { |
| 71 | return ((Cursor)getItem(position)).getString(POSTAL_DISPLAY_NAME_COLUMN_INDEX); |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 74 | public Uri getContactMethodUri(int position) { |
| 75 | Cursor cursor = ((Cursor)getItem(position)); |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 76 | long id = cursor.getLong(POSTAL_ID_COLUMN_INDEX); |
| 77 | return ContentUris.withAppendedId(ContactMethods.CONTENT_URI, id); |
| 78 | } |
| 79 | |
| 80 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 81 | protected View newView(Context context, int partition, Cursor cursor, int position, |
| 82 | ViewGroup parent) { |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 83 | final ContactListItemView view = new ContactListItemView(context, null); |
| 84 | view.setUnknownNameText(mUnknownNameText); |
| 85 | return view; |
| 86 | } |
| 87 | |
| 88 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 89 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 90 | ContactListItemView view = (ContactListItemView)itemView; |
| 91 | bindName(view, cursor); |
| 92 | bindPostalAddress(view, cursor); |
| 93 | } |
| 94 | |
| 95 | protected void bindName(final ContactListItemView view, Cursor cursor) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 96 | view.showDisplayName(cursor, POSTAL_DISPLAY_NAME_COLUMN_INDEX, |
Dave Santoro | da5bf1c | 2011-05-03 10:30:34 -0700 | [diff] [blame] | 97 | getContactNameDisplayOrder()); |
Dmitri Plotnikov | c28390b | 2010-05-07 11:20:32 -0700 | [diff] [blame] | 98 | view.showPhoneticName(cursor, POSTAL_PHONETIC_NAME_COLUMN_INDEX); |
| 99 | } |
| 100 | |
| 101 | protected void bindPostalAddress(ContactListItemView view, Cursor cursor) { |
| 102 | CharSequence label = null; |
| 103 | if (!cursor.isNull(POSTAL_TYPE_COLUMN_INDEX)) { |
| 104 | final int type = cursor.getInt(POSTAL_TYPE_COLUMN_INDEX); |
| 105 | final String customLabel = cursor.getString(POSTAL_LABEL_COLUMN_INDEX); |
| 106 | |
| 107 | // TODO cache |
| 108 | label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel); |
| 109 | } |
| 110 | view.setLabel(label); |
| 111 | view.showData(cursor, POSTAL_NUMBER_COLUMN_INDEX); |
| 112 | } |
| 113 | } |