blob: c0c23530dc14b69098baf30e08cdc65553cfd048 [file] [log] [blame]
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -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 Plotnikovd820cdb2010-05-06 14:11:55 -070018import android.content.ContentUris;
19import android.content.Context;
Jeff Hamilton3c462912010-05-15 02:20:01 -050020import android.content.CursorLoader;
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070021import android.database.Cursor;
22import android.net.Uri;
Daisuke Miyakawae231f192011-11-18 10:52:25 -080023import android.net.Uri.Builder;
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070024import android.provider.ContactsContract;
Daisuke Miyakawae231f192011-11-18 10:52:25 -080025import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070026import android.provider.ContactsContract.Data;
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070027import android.view.View;
28import android.view.ViewGroup;
29
Gary Mai0a49afa2016-12-05 15:53:58 -080030import com.android.contacts.ContactPhotoManager.DefaultImageRequest;
Gary Mai69c182a2016-12-05 13:07:03 -080031import com.android.contacts.preference.ContactsPreferences;
Chiao Chenga0233a02012-11-01 16:41:08 -070032
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070033/**
34 * A cursor adapter for the {@link StructuredPostal#CONTENT_TYPE} content type.
35 */
36public class PostalAddressListAdapter extends ContactEntryListAdapter {
37
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070038 protected static class PostalQuery {
39 private static final String[] PROJECTION_PRIMARY = new String[] {
40 StructuredPostal._ID, // 0
41 StructuredPostal.TYPE, // 1
42 StructuredPostal.LABEL, // 2
43 StructuredPostal.DATA, // 3
44 StructuredPostal.PHOTO_ID, // 4
Yorke Leef6774502014-02-20 10:21:26 -080045 StructuredPostal.LOOKUP_KEY, // 5
46 StructuredPostal.DISPLAY_NAME_PRIMARY, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070047 };
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070048
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070049 private static final String[] PROJECTION_ALTERNATIVE = new String[] {
50 StructuredPostal._ID, // 0
51 StructuredPostal.TYPE, // 1
52 StructuredPostal.LABEL, // 2
53 StructuredPostal.DATA, // 3
54 StructuredPostal.PHOTO_ID, // 4
Yorke Leef6774502014-02-20 10:21:26 -080055 StructuredPostal.LOOKUP_KEY, // 5
56 StructuredPostal.DISPLAY_NAME_ALTERNATIVE, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070057 };
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070058
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070059 public static final int POSTAL_ID = 0;
60 public static final int POSTAL_TYPE = 1;
61 public static final int POSTAL_LABEL = 2;
62 public static final int POSTAL_ADDRESS = 3;
63 public static final int POSTAL_PHOTO_ID = 4;
Yorke Leef6774502014-02-20 10:21:26 -080064 public static final int POSTAL_LOOKUP_KEY = 5;
65 public static final int POSTAL_DISPLAY_NAME = 6;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070066 }
67
68 private final CharSequence mUnknownNameText;
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070069
70 public PostalAddressListAdapter(Context context) {
71 super(context);
72
73 mUnknownNameText = context.getText(android.R.string.unknownName);
74 }
75
76 @Override
Dmitri Plotnikovd5061fe2010-06-07 15:06:58 -070077 public void configureLoader(CursorLoader loader, long directoryId) {
Daisuke Miyakawae231f192011-11-18 10:52:25 -080078 final Builder builder = StructuredPostal.CONTENT_URI.buildUpon()
79 .appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
80 if (isSectionHeaderDisplayEnabled()) {
Yorke Lee9d2b6d52014-09-04 14:58:56 -070081 builder.appendQueryParameter(StructuredPostal.EXTRA_ADDRESS_BOOK_INDEX, "true");
Daisuke Miyakawae231f192011-11-18 10:52:25 -080082 }
83 loader.setUri(builder.build());
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070084
Yorke Leec9bb2172014-07-10 11:38:34 -070085 if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070086 loader.setProjection(PostalQuery.PROJECTION_PRIMARY);
87 } else {
88 loader.setProjection(PostalQuery.PROJECTION_ALTERNATIVE);
89 }
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070090
Yorke Leec9bb2172014-07-10 11:38:34 -070091 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070092 loader.setSortOrder(StructuredPostal.SORT_KEY_PRIMARY);
93 } else {
94 loader.setSortOrder(StructuredPostal.SORT_KEY_ALTERNATIVE);
95 }
96 }
97
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -070098 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -070099 public String getContactDisplayName(int position) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700100 return ((Cursor) getItem(position)).getString(PostalQuery.POSTAL_DISPLAY_NAME);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700101 }
102
103 /**
104 * Builds a {@link Data#CONTENT_URI} for the current cursor
105 * position.
106 */
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700107 public Uri getDataUri(int position) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700108 long id = ((Cursor)getItem(position)).getLong(PostalQuery.POSTAL_ID);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700109 return ContentUris.withAppendedId(Data.CONTENT_URI, id);
110 }
111
112 @Override
Andrew Lee551da172014-04-28 11:43:47 -0700113 protected ContactListItemView newView(
114 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
115 ContactListItemView view = super.newView(context, partition, cursor, position, parent);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700116 view.setUnknownNameText(mUnknownNameText);
Dmitri Plotnikov98fa1b02011-01-13 16:54:29 -0800117 view.setQuickContactEnabled(isQuickContactEnabled());
Andrew Lee551da172014-04-28 11:43:47 -0700118 view.setIsSectionHeaderEnabled(isSectionHeaderDisplayEnabled());
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700119 return view;
120 }
121
122 @Override
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700123 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -0700124 super.bindView(itemView, partition, cursor, position);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700125 ContactListItemView view = (ContactListItemView)itemView;
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700126 bindSectionHeaderAndDivider(view, position);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700127 bindName(view, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700128 bindViewId(view, cursor, PostalQuery.POSTAL_ID);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700129 bindPhoto(view, cursor);
130 bindPostalAddress(view, cursor);
131 }
132
133 protected void bindPostalAddress(ContactListItemView view, Cursor cursor) {
134 CharSequence label = null;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700135 if (!cursor.isNull(PostalQuery.POSTAL_TYPE)) {
136 final int type = cursor.getInt(PostalQuery.POSTAL_TYPE);
137 final String customLabel = cursor.getString(PostalQuery.POSTAL_LABEL);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700138
139 // TODO cache
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700140 label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700141 }
142 view.setLabel(label);
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700143 view.showData(cursor, PostalQuery.POSTAL_ADDRESS);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700144 }
145
Dmitri Plotnikove1247222010-06-02 18:14:21 -0700146 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700147 final int section = getSectionForPosition(position);
148 if (getPositionForSection(section) == position) {
149 String title = (String)getSections()[section];
150 view.setSectionHeader(title);
151 } else {
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700152 view.setSectionHeader(null);
153 }
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700154 }
155
156 protected void bindName(final ContactListItemView view, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700157 view.showDisplayName(cursor, PostalQuery.POSTAL_DISPLAY_NAME, getContactNameDisplayOrder());
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700158 }
159
160 protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
161 long photoId = 0;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700162 if (!cursor.isNull(PostalQuery.POSTAL_PHOTO_ID)) {
163 photoId = cursor.getLong(PostalQuery.POSTAL_PHOTO_ID);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700164 }
165
Yorke Leef6774502014-02-20 10:21:26 -0800166 DefaultImageRequest request = null;
167 if (photoId == 0) {
168 request = getDefaultImageRequestFromCursor(cursor, PostalQuery.POSTAL_DISPLAY_NAME,
169 PostalQuery.POSTAL_LOOKUP_KEY);
170 }
171
Yorke Lee41369d72014-04-28 18:17:51 -0700172 getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(),
173 request);
Dmitri Plotnikovd820cdb2010-05-06 14:11:55 -0700174 }
175//
176// protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
177// view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
178// SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
179// }
180
181}