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