blob: d19f960b82f844f08872740ad0033cad0f6b7de5 [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.Data;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070027import android.text.TextUtils;
28import android.view.View;
29import android.view.ViewGroup;
30
Yorke Leef6774502014-02-20 10:21:26 -080031import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
Chiao Chenga0233a02012-11-01 16:41:08 -070032import com.android.contacts.common.list.ContactEntryListAdapter;
33import com.android.contacts.common.list.ContactListItemView;
Yorke Leec9bb2172014-07-10 11:38:34 -070034import com.android.contacts.common.preference.ContactsPreferences;
Chiao Chenga0233a02012-11-01 16:41:08 -070035
Daniel Lehmannc86ace72011-03-23 21:04:29 -070036/**
37 * A cursor adapter for the {@link Email#CONTENT_TYPE} content type.
38 */
39public class EmailAddressListAdapter extends ContactEntryListAdapter {
40
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070041 protected static class EmailQuery {
42 private static final String[] PROJECTION_PRIMARY = new String[] {
43 Email._ID, // 0
44 Email.TYPE, // 1
45 Email.LABEL, // 2
46 Email.DATA, // 3
47 Email.PHOTO_ID, // 4
Yorke Leef6774502014-02-20 10:21:26 -080048 Email.LOOKUP_KEY, // 5
49 Email.DISPLAY_NAME_PRIMARY, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070050 };
Daniel Lehmannc86ace72011-03-23 21:04:29 -070051
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070052 private static final String[] PROJECTION_ALTERNATIVE = new String[] {
53 Email._ID, // 0
54 Email.TYPE, // 1
55 Email.LABEL, // 2
56 Email.DATA, // 3
57 Email.PHOTO_ID, // 4
Yorke Leef6774502014-02-20 10:21:26 -080058 Email.LOOKUP_KEY, // 5
59 Email.DISPLAY_NAME_ALTERNATIVE, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070060 };
Daniel Lehmannc86ace72011-03-23 21:04:29 -070061
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070062 public static final int EMAIL_ID = 0;
63 public static final int EMAIL_TYPE = 1;
64 public static final int EMAIL_LABEL = 2;
65 public static final int EMAIL_ADDRESS = 3;
66 public static final int EMAIL_PHOTO_ID = 4;
Yorke Leef6774502014-02-20 10:21:26 -080067 public static final int EMAIL_LOOKUP_KEY = 5;
68 public static final int EMAIL_DISPLAY_NAME = 6;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070069 }
70
71 private final CharSequence mUnknownNameText;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070072
73 public EmailAddressListAdapter(Context context) {
74 super(context);
75
76 mUnknownNameText = context.getText(android.R.string.unknownName);
77 }
78
79 @Override
80 public void configureLoader(CursorLoader loader, long directoryId) {
81 final Builder builder;
82 if (isSearchMode()) {
83 builder = Email.CONTENT_FILTER_URI.buildUpon();
84 String query = getQueryString();
85 builder.appendPath(TextUtils.isEmpty(query) ? "" : query);
86 } else {
87 builder = Email.CONTENT_URI.buildUpon();
Daisuke Miyakawae231f192011-11-18 10:52:25 -080088 if (isSectionHeaderDisplayEnabled()) {
Yorke Lee9d2b6d52014-09-04 14:58:56 -070089 builder.appendQueryParameter(Email.EXTRA_ADDRESS_BOOK_INDEX, "true");
Daisuke Miyakawae231f192011-11-18 10:52:25 -080090 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -070091 }
92 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
93 String.valueOf(directoryId));
Daisuke Miyakawa2476f0c2011-11-14 13:52:06 -080094 builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
Daniel Lehmannc86ace72011-03-23 21:04:29 -070095 loader.setUri(builder.build());
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070096
Yorke Leec9bb2172014-07-10 11:38:34 -070097 if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070098 loader.setProjection(EmailQuery.PROJECTION_PRIMARY);
99 } else {
100 loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE);
101 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700102
Yorke Leec9bb2172014-07-10 11:38:34 -0700103 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700104 loader.setSortOrder(Email.SORT_KEY_PRIMARY);
105 } else {
106 loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE);
107 }
108 }
109
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700110 @Override
111 public String getContactDisplayName(int position) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700112 return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700113 }
114
115 /**
116 * Builds a {@link Data#CONTENT_URI} for the current cursor
117 * position.
118 */
119 public Uri getDataUri(int position) {
Daisuke Miyakawae231f192011-11-18 10:52:25 -0800120 long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700121 return ContentUris.withAppendedId(Data.CONTENT_URI, id);
122 }
123
124 @Override
Andrew Lee551da172014-04-28 11:43:47 -0700125 protected ContactListItemView newView(
126 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
127 ContactListItemView view = super.newView(context, partition, cursor, position, parent);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700128 view.setUnknownNameText(mUnknownNameText);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700129 view.setQuickContactEnabled(isQuickContactEnabled());
130 return view;
131 }
132
133 @Override
134 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -0700135 super.bindView(itemView, partition, cursor, position);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700136 ContactListItemView view = (ContactListItemView)itemView;
137 bindSectionHeaderAndDivider(view, position);
138 bindName(view, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700139 bindViewId(view, cursor, EmailQuery.EMAIL_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700140 bindPhoto(view, cursor);
141 bindEmailAddress(view, cursor);
142 }
143
144 protected void bindEmailAddress(ContactListItemView view, Cursor cursor) {
145 CharSequence label = null;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700146 if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) {
147 final int type = cursor.getInt(EmailQuery.EMAIL_TYPE);
148 final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700149
150 // TODO cache
151 label = Email.getTypeLabel(getContext().getResources(), type, customLabel);
152 }
153 view.setLabel(label);
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700154 view.showData(cursor, EmailQuery.EMAIL_ADDRESS);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700155 }
156
157 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
158 final int section = getSectionForPosition(position);
159 if (getPositionForSection(section) == position) {
160 String title = (String)getSections()[section];
161 view.setSectionHeader(title);
162 } else {
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700163 view.setSectionHeader(null);
164 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700165 }
166
167 protected void bindName(final ContactListItemView view, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700168 view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder());
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700169 }
170
171 protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
172 long photoId = 0;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700173 if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) {
174 photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700175 }
Yorke Leef6774502014-02-20 10:21:26 -0800176 DefaultImageRequest request = null;
177 if (photoId == 0) {
178 request = getDefaultImageRequestFromCursor(cursor, EmailQuery.EMAIL_DISPLAY_NAME,
179 EmailQuery.EMAIL_LOOKUP_KEY);
180 }
Yorke Lee41369d72014-04-28 18:17:51 -0700181 getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(),
182 request);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700183 }
184//
185// protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
186// view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
187// SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
188// }
189
190}