Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.contacts.list; |
| 17 | |
| 18 | import android.content.ContentUris; |
| 19 | import android.content.Context; |
| 20 | import android.content.CursorLoader; |
| 21 | import android.database.Cursor; |
| 22 | import android.net.Uri; |
| 23 | import android.net.Uri.Builder; |
| 24 | import android.provider.ContactsContract; |
Daniel Lehmann | 4ff3128 | 2011-07-11 14:38:36 -0700 | [diff] [blame] | 25 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 26 | import android.provider.ContactsContract.Data; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 31 | import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 32 | import com.android.contacts.common.list.ContactEntryListAdapter; |
| 33 | import com.android.contacts.common.list.ContactListItemView; |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 35 | |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 36 | /** |
| 37 | * A cursor adapter for the {@link Email#CONTENT_TYPE} content type. |
| 38 | */ |
| 39 | public class EmailAddressListAdapter extends ContactEntryListAdapter { |
| 40 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 41 | 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 Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 48 | Email.LOOKUP_KEY, // 5 |
| 49 | Email.DISPLAY_NAME_PRIMARY, // 6 |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 50 | }; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 51 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 52 | 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 Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 58 | Email.LOOKUP_KEY, // 5 |
| 59 | Email.DISPLAY_NAME_ALTERNATIVE, // 6 |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 60 | }; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 61 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 62 | 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 Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 67 | public static final int EMAIL_LOOKUP_KEY = 5; |
| 68 | public static final int EMAIL_DISPLAY_NAME = 6; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private final CharSequence mUnknownNameText; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 72 | |
| 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 Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 88 | if (isSectionHeaderDisplayEnabled()) { |
Yorke Lee | 9d2b6d5 | 2014-09-04 14:58:56 -0700 | [diff] [blame] | 89 | builder.appendQueryParameter(Email.EXTRA_ADDRESS_BOOK_INDEX, "true"); |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 90 | } |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 91 | } |
| 92 | builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 93 | String.valueOf(directoryId)); |
Daisuke Miyakawa | 2476f0c | 2011-11-14 13:52:06 -0800 | [diff] [blame] | 94 | builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true"); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 95 | loader.setUri(builder.build()); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 96 | |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 97 | if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 98 | loader.setProjection(EmailQuery.PROJECTION_PRIMARY); |
| 99 | } else { |
| 100 | loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE); |
| 101 | } |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 102 | |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 103 | if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) { |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 104 | loader.setSortOrder(Email.SORT_KEY_PRIMARY); |
| 105 | } else { |
| 106 | loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE); |
| 107 | } |
| 108 | } |
| 109 | |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 110 | @Override |
| 111 | public String getContactDisplayName(int position) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 112 | return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Builds a {@link Data#CONTENT_URI} for the current cursor |
| 117 | * position. |
| 118 | */ |
| 119 | public Uri getDataUri(int position) { |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 120 | long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 121 | return ContentUris.withAppendedId(Data.CONTENT_URI, id); |
| 122 | } |
| 123 | |
| 124 | @Override |
Andrew Lee | 551da17 | 2014-04-28 11:43:47 -0700 | [diff] [blame] | 125 | 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 128 | view.setUnknownNameText(mUnknownNameText); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 129 | view.setQuickContactEnabled(isQuickContactEnabled()); |
| 130 | return view; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
Brian Attwell | c9e8107 | 2014-09-15 11:08:19 -0700 | [diff] [blame] | 135 | super.bindView(itemView, partition, cursor, position); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 136 | ContactListItemView view = (ContactListItemView)itemView; |
| 137 | bindSectionHeaderAndDivider(view, position); |
| 138 | bindName(view, cursor); |
Brian Attwell | 56151b8 | 2014-09-03 20:14:47 -0700 | [diff] [blame] | 139 | bindViewId(view, cursor, EmailQuery.EMAIL_ID); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 140 | bindPhoto(view, cursor); |
| 141 | bindEmailAddress(view, cursor); |
| 142 | } |
| 143 | |
| 144 | protected void bindEmailAddress(ContactListItemView view, Cursor cursor) { |
| 145 | CharSequence label = null; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 146 | 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 149 | |
| 150 | // TODO cache |
| 151 | label = Email.getTypeLabel(getContext().getResources(), type, customLabel); |
| 152 | } |
| 153 | view.setLabel(label); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 154 | view.showData(cursor, EmailQuery.EMAIL_ADDRESS); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 155 | } |
| 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 163 | view.setSectionHeader(null); |
| 164 | } |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | protected void bindName(final ContactListItemView view, Cursor cursor) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 168 | view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder()); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | protected void bindPhoto(final ContactListItemView view, Cursor cursor) { |
| 172 | long photoId = 0; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 173 | if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) { |
| 174 | photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 175 | } |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 176 | DefaultImageRequest request = null; |
| 177 | if (photoId == 0) { |
| 178 | request = getDefaultImageRequestFromCursor(cursor, EmailQuery.EMAIL_DISPLAY_NAME, |
| 179 | EmailQuery.EMAIL_LOOKUP_KEY); |
| 180 | } |
Yorke Lee | 41369d7 | 2014-04-28 18:17:51 -0700 | [diff] [blame] | 181 | getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(), |
| 182 | request); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 183 | } |
| 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 | } |