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