blob: 04297a469a3b232adaf9586a14cf98d26c3457dd [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
Gary Mai0a49afa2016-12-05 15:53:58 -080031import com.android.contacts.ContactPhotoManager.DefaultImageRequest;
Gary Mai69c182a2016-12-05 13:07:03 -080032import com.android.contacts.preference.ContactsPreferences;
Chiao Chenga0233a02012-11-01 16:41:08 -070033
Daniel Lehmannc86ace72011-03-23 21:04:29 -070034/**
35 * A cursor adapter for the {@link Email#CONTENT_TYPE} content type.
36 */
37public class EmailAddressListAdapter extends ContactEntryListAdapter {
38
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070039 protected static class EmailQuery {
40 private static final String[] PROJECTION_PRIMARY = new String[] {
41 Email._ID, // 0
42 Email.TYPE, // 1
43 Email.LABEL, // 2
44 Email.DATA, // 3
45 Email.PHOTO_ID, // 4
Yorke Leef6774502014-02-20 10:21:26 -080046 Email.LOOKUP_KEY, // 5
47 Email.DISPLAY_NAME_PRIMARY, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070048 };
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
Yorke Leef6774502014-02-20 10:21:26 -080056 Email.LOOKUP_KEY, // 5
57 Email.DISPLAY_NAME_ALTERNATIVE, // 6
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070058 };
Daniel Lehmannc86ace72011-03-23 21:04:29 -070059
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070060 public static final int EMAIL_ID = 0;
61 public static final int EMAIL_TYPE = 1;
62 public static final int EMAIL_LABEL = 2;
63 public static final int EMAIL_ADDRESS = 3;
64 public static final int EMAIL_PHOTO_ID = 4;
Yorke Leef6774502014-02-20 10:21:26 -080065 public static final int EMAIL_LOOKUP_KEY = 5;
66 public static final int EMAIL_DISPLAY_NAME = 6;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070067 }
68
69 private final CharSequence mUnknownNameText;
Daniel Lehmannc86ace72011-03-23 21:04:29 -070070
71 public EmailAddressListAdapter(Context context) {
72 super(context);
73
74 mUnknownNameText = context.getText(android.R.string.unknownName);
75 }
76
77 @Override
78 public void configureLoader(CursorLoader loader, long directoryId) {
79 final Builder builder;
80 if (isSearchMode()) {
81 builder = Email.CONTENT_FILTER_URI.buildUpon();
82 String query = getQueryString();
83 builder.appendPath(TextUtils.isEmpty(query) ? "" : query);
84 } else {
85 builder = Email.CONTENT_URI.buildUpon();
Daisuke Miyakawae231f192011-11-18 10:52:25 -080086 if (isSectionHeaderDisplayEnabled()) {
Yorke Lee9d2b6d52014-09-04 14:58:56 -070087 builder.appendQueryParameter(Email.EXTRA_ADDRESS_BOOK_INDEX, "true");
Daisuke Miyakawae231f192011-11-18 10:52:25 -080088 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -070089 }
90 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
91 String.valueOf(directoryId));
Daisuke Miyakawa2476f0c2011-11-14 13:52:06 -080092 builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true");
Daniel Lehmannc86ace72011-03-23 21:04:29 -070093 loader.setUri(builder.build());
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070094
Yorke Leec9bb2172014-07-10 11:38:34 -070095 if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -070096 loader.setProjection(EmailQuery.PROJECTION_PRIMARY);
97 } else {
98 loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE);
99 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700100
Yorke Leec9bb2172014-07-10 11:38:34 -0700101 if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) {
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700102 loader.setSortOrder(Email.SORT_KEY_PRIMARY);
103 } else {
104 loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE);
105 }
106 }
107
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700108 @Override
109 public String getContactDisplayName(int position) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700110 return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700111 }
112
113 /**
114 * Builds a {@link Data#CONTENT_URI} for the current cursor
115 * position.
116 */
117 public Uri getDataUri(int position) {
Daisuke Miyakawae231f192011-11-18 10:52:25 -0800118 long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700119 return ContentUris.withAppendedId(Data.CONTENT_URI, id);
120 }
121
122 @Override
Andrew Lee551da172014-04-28 11:43:47 -0700123 protected ContactListItemView newView(
124 Context context, int partition, Cursor cursor, int position, ViewGroup parent) {
125 ContactListItemView view = super.newView(context, partition, cursor, position, parent);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700126 view.setUnknownNameText(mUnknownNameText);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700127 view.setQuickContactEnabled(isQuickContactEnabled());
128 return view;
129 }
130
131 @Override
132 protected void bindView(View itemView, int partition, Cursor cursor, int position) {
Brian Attwellc9e81072014-09-15 11:08:19 -0700133 super.bindView(itemView, partition, cursor, position);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700134 ContactListItemView view = (ContactListItemView)itemView;
135 bindSectionHeaderAndDivider(view, position);
136 bindName(view, cursor);
Brian Attwell56151b82014-09-03 20:14:47 -0700137 bindViewId(view, cursor, EmailQuery.EMAIL_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700138 bindPhoto(view, cursor);
139 bindEmailAddress(view, cursor);
140 }
141
142 protected void bindEmailAddress(ContactListItemView view, Cursor cursor) {
143 CharSequence label = null;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700144 if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) {
145 final int type = cursor.getInt(EmailQuery.EMAIL_TYPE);
146 final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700147
148 // TODO cache
149 label = Email.getTypeLabel(getContext().getResources(), type, customLabel);
150 }
151 view.setLabel(label);
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700152 view.showData(cursor, EmailQuery.EMAIL_ADDRESS);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700153 }
154
155 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) {
156 final int section = getSectionForPosition(position);
157 if (getPositionForSection(section) == position) {
158 String title = (String)getSections()[section];
159 view.setSectionHeader(title);
160 } else {
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700161 view.setSectionHeader(null);
162 }
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700163 }
164
165 protected void bindName(final ContactListItemView view, Cursor cursor) {
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700166 view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder());
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700167 }
168
169 protected void bindPhoto(final ContactListItemView view, Cursor cursor) {
170 long photoId = 0;
Daisuke Miyakawaed90ea52011-10-31 09:22:52 -0700171 if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) {
172 photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700173 }
Yorke Leef6774502014-02-20 10:21:26 -0800174 DefaultImageRequest request = null;
175 if (photoId == 0) {
176 request = getDefaultImageRequestFromCursor(cursor, EmailQuery.EMAIL_DISPLAY_NAME,
177 EmailQuery.EMAIL_LOOKUP_KEY);
178 }
Yorke Lee41369d72014-04-28 18:17:51 -0700179 getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(),
180 request);
Daniel Lehmannc86ace72011-03-23 21:04:29 -0700181 }
182//
183// protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) {
184// view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX,
185// SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX);
186// }
187
188}