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.ContactCounts; |
| 27 | import android.provider.ContactsContract.Data; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 28 | import android.text.TextUtils; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
| 31 | |
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; |
| 34 | |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 35 | /** |
| 36 | * A cursor adapter for the {@link Email#CONTENT_TYPE} content type. |
| 37 | */ |
| 38 | public class EmailAddressListAdapter extends ContactEntryListAdapter { |
| 39 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 40 | 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 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 |
| 56 | Email.DISPLAY_NAME_ALTERNATIVE, // 5 |
| 57 | }; |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 58 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 59 | 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 68 | |
| 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 Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 84 | if (isSectionHeaderDisplayEnabled()) { |
| 85 | builder.appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true"); |
| 86 | } |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 87 | } |
| 88 | builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 89 | String.valueOf(directoryId)); |
Daisuke Miyakawa | 2476f0c | 2011-11-14 13:52:06 -0800 | [diff] [blame] | 90 | builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true"); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 91 | loader.setUri(builder.build()); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 92 | |
| 93 | if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) { |
| 94 | loader.setProjection(EmailQuery.PROJECTION_PRIMARY); |
| 95 | } else { |
| 96 | loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE); |
| 97 | } |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 98 | |
| 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 106 | @Override |
| 107 | public String getContactDisplayName(int position) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 108 | return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Builds a {@link Data#CONTENT_URI} for the current cursor |
| 113 | * position. |
| 114 | */ |
| 115 | public Uri getDataUri(int position) { |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 116 | long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 117 | 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 125 | 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 Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 140 | 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 Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 143 | |
| 144 | // TODO cache |
| 145 | label = Email.getTypeLabel(getContext().getResources(), type, customLabel); |
| 146 | } |
| 147 | view.setLabel(label); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 148 | view.showData(cursor, EmailQuery.EMAIL_ADDRESS); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 149 | } |
| 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 Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 170 | view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder()); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | protected void bindPhoto(final ContactListItemView view, Cursor cursor) { |
| 174 | long photoId = 0; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 175 | if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) { |
| 176 | photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Daniel Lehmann | 2f77c85 | 2012-03-30 15:25:31 -0700 | [diff] [blame] | 179 | getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false); |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 180 | } |
| 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 | } |