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