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 } 82 builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, 83 String.valueOf(directoryId)); 84 builder.appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true"); 85 loader.setUri(builder.build()); 86 87 if (getContactNameDisplayOrder() == ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY) { 88 loader.setProjection(EmailQuery.PROJECTION_PRIMARY); 89 } else { 90 loader.setProjection(EmailQuery.PROJECTION_ALTERNATIVE); 91 } 92 93 if (getSortOrder() == ContactsContract.Preferences.SORT_ORDER_PRIMARY) { 94 loader.setSortOrder(Email.SORT_KEY_PRIMARY); 95 } else { 96 loader.setSortOrder(Email.SORT_KEY_ALTERNATIVE); 97 } 98 } 99 100 protected static Builder buildSectionIndexerUri(Uri uri) { 101 return uri.buildUpon() 102 .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true"); 103 } 104 105 @Override 106 public String getContactDisplayName(int position) { 107 return ((Cursor) getItem(position)).getString(EmailQuery.EMAIL_DISPLAY_NAME); 108 } 109 110 /** 111 * Builds a {@link Data#CONTENT_URI} for the current cursor 112 * position. 113 */ 114 public Uri getDataUri(int position) { 115 long id = ((Cursor)getItem(position)).getLong(EmailQuery.EMAIL_ID); 116 return ContentUris.withAppendedId(Data.CONTENT_URI, id); 117 } 118 119 @Override 120 protected View newView(Context context, int partition, Cursor cursor, int position, 121 ViewGroup parent) { 122 final ContactListItemView view = new ContactListItemView(context, null); 123 view.setUnknownNameText(mUnknownNameText); 124 view.setQuickContactEnabled(isQuickContactEnabled()); 125 return view; 126 } 127 128 @Override 129 protected void bindView(View itemView, int partition, Cursor cursor, int position) { 130 ContactListItemView view = (ContactListItemView)itemView; 131 bindSectionHeaderAndDivider(view, position); 132 bindName(view, cursor); 133 bindPhoto(view, cursor); 134 bindEmailAddress(view, cursor); 135 } 136 137 protected void bindEmailAddress(ContactListItemView view, Cursor cursor) { 138 CharSequence label = null; 139 if (!cursor.isNull(EmailQuery.EMAIL_TYPE)) { 140 final int type = cursor.getInt(EmailQuery.EMAIL_TYPE); 141 final String customLabel = cursor.getString(EmailQuery.EMAIL_LABEL); 142 143 // TODO cache 144 label = Email.getTypeLabel(getContext().getResources(), type, customLabel); 145 } 146 view.setLabel(label); 147 view.showData(cursor, EmailQuery.EMAIL_ADDRESS); 148 } 149 150 protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) { 151 final int section = getSectionForPosition(position); 152 if (getPositionForSection(section) == position) { 153 String title = (String)getSections()[section]; 154 view.setSectionHeader(title); 155 } else { 156 view.setDividerVisible(false); 157 view.setSectionHeader(null); 158 } 159 160 // move the divider for the last item in a section 161 if (getPositionForSection(section + 1) - 1 == position) { 162 view.setDividerVisible(false); 163 } else { 164 view.setDividerVisible(true); 165 } 166 } 167 168 protected void bindName(final ContactListItemView view, Cursor cursor) { 169 view.showDisplayName(cursor, EmailQuery.EMAIL_DISPLAY_NAME, getContactNameDisplayOrder()); 170 } 171 172 protected void bindPhoto(final ContactListItemView view, Cursor cursor) { 173 long photoId = 0; 174 if (!cursor.isNull(EmailQuery.EMAIL_PHOTO_ID)) { 175 photoId = cursor.getLong(EmailQuery.EMAIL_PHOTO_ID); 176 } 177 178 getPhotoLoader().loadPhoto(view.getPhotoView(), photoId, false, false); 179 } 180 // 181 // protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) { 182 // view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX, 183 // SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX); 184 // } 185 186 } 187