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; 25 import android.provider.ContactsContract.CommonDataKinds.Email; 26 import android.provider.ContactsContract.ContactCounts; 27 import android.provider.ContactsContract.Data; 28 import android.text.TextUtils; 29 import android.view.View; 30 import android.view.ViewGroup; 31 32 /** 33 * A cursor adapter for the {@link Email#CONTENT_TYPE} content type. 34 */ 35 public class EmailAddressListAdapter extends ContactEntryListAdapter { 36 37 protected static class EmailQuery { 38 private static final String[] PROJECTION_PRIMARY = new String[] { 39 Email._ID, // 0 40 Email.TYPE, // 1 41 Email.LABEL, // 2 42 Email.DATA, // 3 43 Email.PHOTO_ID, // 4 44 Email.DISPLAY_NAME_PRIMARY, // 5 45 }; 46 47 private static final String[] PROJECTION_ALTERNATIVE = new String[] { 48 Email._ID, // 0 49 Email.TYPE, // 1 50 Email.LABEL, // 2 51 Email.DATA, // 3 52 Email.PHOTO_ID, // 4 53 Email.DISPLAY_NAME_ALTERNATIVE, // 5 54 }; 55 56 public static final int EMAIL_ID = 0; 57 public static final int EMAIL_TYPE = 1; 58 public static final int EMAIL_LABEL = 2; 59 public static final int EMAIL_ADDRESS = 3; 60 public static final int EMAIL_PHOTO_ID = 4; 61 public static final int EMAIL_DISPLAY_NAME = 5; 62 } 63 64 private final CharSequence mUnknownNameText; 65 66 public EmailAddressListAdapter(Context context) { 67 super(context); 68 69 mUnknownNameText = context.getText(android.R.string.unknownName); 70 } 71 72 @Override 73 public void configureLoader(CursorLoader loader, long directoryId) { 74 final Builder builder; 75 if (isSearchMode()) { 76 builder = Email.CONTENT_FILTER_URI.buildUpon(); 77 String query = getQueryString(); 78 builder.appendPath(TextUtils.isEmpty(query) ? "" : query); 79 } else { 80 builder = Email.CONTENT_URI.buildUpon(); 81 if (isSectionHeaderDisplayEnabled()) { 82 builder.appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true"); 83 } 84 } 85 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 86 String.valueOf(directoryId)); 87 builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true"); 88 loader.setUri(builder.build()); 89 90 if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) { 91 loader.setProjection(EmailQuery.PROJECTION_PRIMARY); 92 } else { 93 loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE); 94 } 95 96 if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) { 97 loader.setSortOrder(Email.SORT_KEY_PRIMARY); 98 } else { 99 loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE); 100 } 101 } 102 103 @Override 104 public String getContactDisplayName(int position) { 105 return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME); 106 } 107 108 /** 109 * Builds a {@link Data#CONTENT_URI} for the current cursor 110 * position. 111 */ 112 public Uri getDataUri(int position) { 113 long id = ((Cursor) getItem(position)).getLong(EmailQuery.EMAIL_ID); 114 return ContentUris.withAppendedId(Data.CONTENT_URI, id); 115 } 116 117 @Override 118 protected View newView(Context context, int partition, Cursor cursor, int position, 119 ViewGroup parent) { 120 final ContactListItemView view = new ContactListItemView(context, null); 121 view.setUnknownNameText(mUnknownNameText); 122 view.setQuickContactEnabled(isQuickContactEnabled()); 123 return view; 124 } 125 126 @Override 127 protected void bindView(View itemView, int partition, Cursor cursor, int position) { 128 ContactListItemView view = (ContactListItemView)itemView; 129 bindSectionHeaderAndDivider(view, position); 130 bindName(view, cursor); 131 bindPhoto(view, cursor); 132 bindEmailAddress(view, cursor); 133 } 134 135 protected void bindEmailAddress(ContactListItemView view, Cursor cursor) { 136 CharSequence label = null; 137 if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) { 138 final int type = cursor.getInt(EmailQuery.EMAIL_TYPE); 139 final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL); 140 141 // TODO cache 142 label = Email.getTypeLabel(getContext().getResources(), type, customLabel); 143 } 144 view.setLabel(label); 145 view.showData(cursor, EmailQuery.EMAIL_ADDRESS); 146 } 147 148 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) { 149 final int section = getSectionForPosition(position); 150 if (getPositionForSection(section) == position) { 151 String title = (String)getSections()[section]; 152 view.setSectionHeader(title); 153 } else { 154 view.setDividerVisible(false); 155 view.setSectionHeader(null); 156 } 157 158 // move the divider for the last item in a section 159 if (getPositionForSection(section + 1) - 1 == position) { 160 view.setDividerVisible(false); 161 } else { 162 view.setDividerVisible(true); 163 } 164 } 165 166 protected void bindName(final ContactListItemView view, Cursor cursor) { 167 view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder()); 168 } 169 170 protected void bindPhoto(final ContactListItemView view, Cursor cursor) { 171 long photoId = 0; 172 if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) { 173 photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID); 174 } 175 176 getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false); 177 } 178 // 179 // protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) { 180 // view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX, 181 // SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX); 182 // } 183 184 } 185