blob: c3fa57d0b223614eb0b9a39d68588bf94b0e10e6 [file] [log] [blame]
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -07001/*
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
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070018import android.content.ContentUris;
19import android.content.Context;
Jeff Hamilton3c462912010-05-15 02:20:01 -050020import android.content.CursorLoader;
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070021import 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
Dmitri Plotnikovd5061fe2010-06-07 15:06:58 -070059 public void configureLoader(CursorLoader loader, long directoryId) {
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070060 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
Dmitri Plotnikove1247222010-06-02 18:14:21 -070067 public String getContactDisplayName(int position) {
68 return ((Cursor)getItem(position)).getString(POSTAL_DISPLAY_NAME_COLUMN_INDEX);
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070069 }
70
Dmitri Plotnikove1247222010-06-02 18:14:21 -070071 public Uri getContactMethodUri(int position) {
72 Cursor cursor = ((Cursor)getItem(position));
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070073 long id = cursor.getLong(POSTAL_ID_COLUMN_INDEX);
74 return ContentUris.withAppendedId(ContactMethods.CONTENT_URI, id);
75 }
76
77 @Override
Andrew Lee551da172014-04-28 11:43:47 -070078 protected ContactListItemView newView(
79 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070080 final ContactListItemView view = new ContactListItemView(context, null);
81 view.setUnknownNameText(mUnknownNameText);
82 return view;
83 }
84
85 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -070086 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -070087 super.bindView(itemView, partition, cursor, position);
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070088 ContactListItemView view = (ContactListItemView)itemView;
89 bindName(view, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -070090 bindViewId(view, cursor, POSTAL_ID_COLUMN_INDEX);
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070091 bindPostalAddress(view, cursor);
92 }
93
94 protected void bindName(final ContactListItemView view, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070095 view.showDisplayName(cursor, POSTAL_DISPLAY_NAME_COLUMN_INDEX,
Dave Santoroda5bf1c2011-05-03 10:30:34 -070096 getContactNameDisplayOrder());
Dmitri Plotnikovc28390b2010-05-07 11:20:32 -070097 view.showPhoneticName(cursor, POSTAL_PHONETIC_NAME_COLUMN_INDEX);
98 }
99
100 protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
101 CharSequence label = null;
102 if (!cursor.isNull(POSTAL_TYPE_COLUMN_INDEX)) {
103 final int type = cursor.getInt(POSTAL_TYPE_COLUMN_INDEX);
104 final String customLabel = cursor.getString(POSTAL_LABEL_COLUMN_INDEX);
105
106 // TODO cache
107 label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel);
108 }
109 view.setLabel(label);
110 view.showData(cursor, POSTAL_NUMBER_COLUMN_INDEX);
111 }
112}