blob: 4a32ae3f6a6afb0627aea67e826b7f7a48adc8f6 [file] [log] [blame]
Daniel Lehmannc86ace72011-03-23 21:04:29 -07001/*
2 * Copyright (C) 2011 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.net.Uri.Builder;
24import android.provider.ContactsContract;
Daniel Lehmann4ff31282011-07-11 14:38:36 -070025import android.provider.ContactsContract.CommonDataKinds.Email;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070026import android.provider.ContactsContract.ContactCounts;
27import android.provider.ContactsContract.Data;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070028import android.text.TextUtils;
29import android.view.View;
30import android.view.ViewGroup;
31
Chiao Chenga0233a02012-11-01 16:41:08 -070032import com.android.contacts.common.list.ContactEntryListAdapter;
33import com.android.contacts.common.list.ContactListItemView;
34
Daniel Lehmannc86ace72011-03-23 21:04:29 -070035/**
36 * A cursor adapter for the {@link Email#CONTENT_TYPE} content type.
37 */
38public class EmailAddressListAdapter extends ContactEntryListAdapter {
39
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070040 protected static class EmailQuery {
41 private static final String[] PROJECTION_PRIMARY = new String[] {
42 Email._ID, // 0
43 Email.TYPE, // 1
44 Email.LABEL, // 2
45 Email.DATA, // 3
46 Email.PHOTO_ID, // 4
47 Email.DISPLAY_NAME_PRIMARY, // 5
48 };
Daniel Lehmannc86ace72011-03-23 21:04:29 -070049
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070050 private static final String[] PROJECTION_ALTERNATIVE = new String[] {
51 Email._ID, // 0
52 Email.TYPE, // 1
53 Email.LABEL, // 2
54 Email.DATA, // 3
55 Email.PHOTO_ID, // 4
56 Email.DISPLAY_NAME_ALTERNATIVE, // 5
57 };
Daniel Lehmannc86ace72011-03-23 21:04:29 -070058
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070059 public static final int EMAIL_ID = 0;
60 public static final int EMAIL_TYPE = 1;
61 public static final int EMAIL_LABEL = 2;
62 public static final int EMAIL_ADDRESS = 3;
63 public static final int EMAIL_PHOTO_ID = 4;
64 public static final int EMAIL_DISPLAY_NAME = 5;
65 }
66
67 private final CharSequence mUnknownNameText;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070068
69 public EmailAddressListAdapter(Context context) {
70 super(context);
71
72 mUnknownNameText = context.getText(android.R.string.unknownName);
73 }
74
75 @Override
76 public void configureLoader(CursorLoader loader, long directoryId) {
77 final Builder builder;
78 if (isSearchMode()) {
79 builder = Email.CONTENT_FILTER_URI.buildUpon();
80 String query = getQueryString();
81 builder.appendPath(TextUtils.isEmpty(query) ? "" : query);
82 } else {
83 builder = Email.CONTENT_URI.buildUpon();
Daisuke Miyakawae231f192011-11-18 10:52:25 -080084 if (isSectionHeaderDisplayEnabled()) {
85 builder.appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true");
86 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -070087 }
88 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
89 String.valueOf(directoryId));
Daisuke Miyakawa2476f0c2011-11-14 13:52:06 -080090 builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
Daniel Lehmannc86ace72011-03-23 21:04:29 -070091 loader.setUri(builder.build());
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070092
93 if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) {
94 loader.setProjection(EmailQuery.PROJECTION_PRIMARY);
95 } else {
96 loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE);
97 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -070098
99 if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) {
100 loader.setSortOrder(Email.SORT_KEY_PRIMARY);
101 } else {
102 loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE);
103 }
104 }
105
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700106 @Override
107 public String getContactDisplayName(int position) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700108 return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700109 }
110
111 /**
112 * Builds a {@link Data#CONTENT_URI} for the current cursor
113 * position.
114 */
115 public Uri getDataUri(int position) {
Daisuke Miyakawae231f192011-11-18 10:52:25 -0800116 long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700117 return ContentUris.withAppendedId(Data.CONTENT_URI, id);
118 }
119
120 @Override
121 protected View newView(Context context, int partition, Cursor cursor, int position,
122 ViewGroup parent) {
123 final ContactListItemView view = new ContactListItemView(context, null);
124 view.setUnknownNameText(mUnknownNameText);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700125 view.setQuickContactEnabled(isQuickContactEnabled());
126 return view;
127 }
128
129 @Override
130 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
131 ContactListItemView view = (ContactListItemView)itemView;
132 bindSectionHeaderAndDivider(view, position);
133 bindName(view, cursor);
134 bindPhoto(view, cursor);
135 bindEmailAddress(view, cursor);
136 }
137
138 protected void bindEmailAddress(ContactListItemView view, Cursor cursor) {
139 CharSequence label = null;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700140 if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) {
141 final int type = cursor.getInt(EmailQuery.EMAIL_TYPE);
142 final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700143
144 // TODO cache
145 label = Email.getTypeLabel(getContext().getResources(), type, customLabel);
146 }
147 view.setLabel(label);
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700148 view.showData(cursor, EmailQuery.EMAIL_ADDRESS);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700149 }
150
151 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
152 final int section = getSectionForPosition(position);
153 if (getPositionForSection(section) == position) {
154 String title = (String)getSections()[section];
155 view.setSectionHeader(title);
156 } else {
157 view.setDividerVisible(false);
158 view.setSectionHeader(null);
159 }
160
161 // move the divider for the last item in a section
162 if (getPositionForSection(section + 1) - 1 == position) {
163 view.setDividerVisible(false);
164 } else {
165 view.setDividerVisible(true);
166 }
167 }
168
169 protected void bindName(final ContactListItemView view, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700170 view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder());
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700171 }
172
173 protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
174 long photoId = 0;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700175 if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) {
176 photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700177 }
178
Daniel Lehmann2f77c852012-03-30 15:25:31 -0700179 getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700180 }
181//
182// protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
183// view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
184// SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
185// }
186
187}