Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 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 | |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 18 | import android.content.ContentUris; |
| 19 | import android.content.Context; |
Jeff Hamilton | 3c46291 | 2010-05-15 02:20:01 -0500 | [diff] [blame] | 20 | import android.content.CursorLoader; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 21 | import android.database.Cursor; |
| 22 | import android.net.Uri; |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 23 | import android.net.Uri.Builder; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 24 | import android.provider.ContactsContract; |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 25 | import android.provider.ContactsContract.CommonDataKinds.StructuredPostal; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 26 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 30 | import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 31 | import com.android.contacts.common.list.ContactEntryListAdapter; |
| 32 | import com.android.contacts.common.list.ContactListItemView; |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 33 | import com.android.contacts.common.preference.ContactsPreferences; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 34 | |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 35 | /** |
| 36 | * A cursor adapter for the {@link StructuredPostal#CONTENT_TYPE} content type. |
| 37 | */ |
| 38 | public class PostalAddressListAdapter extends ContactEntryListAdapter { |
| 39 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 40 | protected static class PostalQuery { |
| 41 | private static final String[] PROJECTION_PRIMARY = new String[] { |
| 42 | StructuredPostal._ID, // 0 |
| 43 | StructuredPostal.TYPE, // 1 |
| 44 | StructuredPostal.LABEL, // 2 |
| 45 | StructuredPostal.DATA, // 3 |
| 46 | StructuredPostal.PHOTO_ID, // 4 |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 47 | StructuredPostal.LOOKUP_KEY, // 5 |
| 48 | StructuredPostal.DISPLAY_NAME_PRIMARY, // 6 |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 49 | }; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 50 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 51 | private static final String[] PROJECTION_ALTERNATIVE = new String[] { |
| 52 | StructuredPostal._ID, // 0 |
| 53 | StructuredPostal.TYPE, // 1 |
| 54 | StructuredPostal.LABEL, // 2 |
| 55 | StructuredPostal.DATA, // 3 |
| 56 | StructuredPostal.PHOTO_ID, // 4 |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 57 | StructuredPostal.LOOKUP_KEY, // 5 |
| 58 | StructuredPostal.DISPLAY_NAME_ALTERNATIVE, // 6 |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 59 | }; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 60 | |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 61 | public static final int POSTAL_ID = 0; |
| 62 | public static final int POSTAL_TYPE = 1; |
| 63 | public static final int POSTAL_LABEL = 2; |
| 64 | public static final int POSTAL_ADDRESS = 3; |
| 65 | public static final int POSTAL_PHOTO_ID = 4; |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 66 | public static final int POSTAL_LOOKUP_KEY = 5; |
| 67 | public static final int POSTAL_DISPLAY_NAME = 6; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | private final CharSequence mUnknownNameText; |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 71 | |
| 72 | public PostalAddressListAdapter(Context context) { |
| 73 | super(context); |
| 74 | |
| 75 | mUnknownNameText = context.getText(android.R.string.unknownName); |
| 76 | } |
| 77 | |
| 78 | @Override |
Dmitri Plotnikov | d5061fe | 2010-06-07 15:06:58 -0700 | [diff] [blame] | 79 | public void configureLoader(CursorLoader loader, long directoryId) { |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 80 | final Builder builder = StructuredPostal.CONTENT_URI.buildUpon() |
| 81 | .appendQueryParameter(ContactsContract.REMOVE_DUPLICATE_ENTRIES, "true"); |
| 82 | if (isSectionHeaderDisplayEnabled()) { |
Yorke Lee | 9d2b6d5 | 2014-09-04 14:58:56 -0700 | [diff] [blame] | 83 | builder.appendQueryParameter(StructuredPostal.EXTRA_ADDRESS_BOOK_INDEX, "true"); |
Daisuke Miyakawa | e231f19 | 2011-11-18 10:52:25 -0800 | [diff] [blame] | 84 | } |
| 85 | loader.setUri(builder.build()); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 86 | |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 87 | if (getContactNameDisplayOrder() == ContactsPreferences.DISPLAY_ORDER_PRIMARY) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 88 | loader.setProjection(PostalQuery.PROJECTION_PRIMARY); |
| 89 | } else { |
| 90 | loader.setProjection(PostalQuery.PROJECTION_ALTERNATIVE); |
| 91 | } |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 92 | |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 93 | if (getSortOrder() == ContactsPreferences.SORT_ORDER_PRIMARY) { |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 94 | loader.setSortOrder(StructuredPostal.SORT_KEY_PRIMARY); |
| 95 | } else { |
| 96 | loader.setSortOrder(StructuredPostal.SORT_KEY_ALTERNATIVE); |
| 97 | } |
| 98 | } |
| 99 | |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 100 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 101 | public String getContactDisplayName(int position) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 102 | return ((Cursor) getItem(position)).getString(PostalQuery.POSTAL_DISPLAY_NAME); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Builds a {@link Data#CONTENT_URI} for the current cursor |
| 107 | * position. |
| 108 | */ |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 109 | public Uri getDataUri(int position) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 110 | long id = ((Cursor)getItem(position)).getLong(PostalQuery.POSTAL_ID); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 111 | return ContentUris.withAppendedId(Data.CONTENT_URI, id); |
| 112 | } |
| 113 | |
| 114 | @Override |
Andrew Lee | 551da17 | 2014-04-28 11:43:47 -0700 | [diff] [blame] | 115 | protected ContactListItemView newView( |
| 116 | Context context, int partition, Cursor cursor, int position, ViewGroup parent) { |
| 117 | ContactListItemView view = super.newView(context, partition, cursor, position, parent); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 118 | view.setUnknownNameText(mUnknownNameText); |
Dmitri Plotnikov | 98fa1b0 | 2011-01-13 16:54:29 -0800 | [diff] [blame] | 119 | view.setQuickContactEnabled(isQuickContactEnabled()); |
Andrew Lee | 551da17 | 2014-04-28 11:43:47 -0700 | [diff] [blame] | 120 | view.setIsSectionHeaderEnabled(isSectionHeaderDisplayEnabled()); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 121 | return view; |
| 122 | } |
| 123 | |
| 124 | @Override |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 125 | protected void bindView(View itemView, int partition, Cursor cursor, int position) { |
Brian Attwell | c9e8107 | 2014-09-15 11:08:19 -0700 | [diff] [blame] | 126 | super.bindView(itemView, partition, cursor, position); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 127 | ContactListItemView view = (ContactListItemView)itemView; |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 128 | bindSectionHeaderAndDivider(view, position); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 129 | bindName(view, cursor); |
Brian Attwell | 56151b8 | 2014-09-03 20:14:47 -0700 | [diff] [blame] | 130 | bindViewId(view, cursor, PostalQuery.POSTAL_ID); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 131 | bindPhoto(view, cursor); |
| 132 | bindPostalAddress(view, cursor); |
| 133 | } |
| 134 | |
| 135 | protected void bindPostalAddress(ContactListItemView view, Cursor cursor) { |
| 136 | CharSequence label = null; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 137 | if (!cursor.isNull(PostalQuery.POSTAL_TYPE)) { |
| 138 | final int type = cursor.getInt(PostalQuery.POSTAL_TYPE); |
| 139 | final String customLabel = cursor.getString(PostalQuery.POSTAL_LABEL); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 140 | |
| 141 | // TODO cache |
Daniel Lehmann | c86ace7 | 2011-03-23 21:04:29 -0700 | [diff] [blame] | 142 | label = StructuredPostal.getTypeLabel(getContext().getResources(), type, customLabel); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 143 | } |
| 144 | view.setLabel(label); |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 145 | view.showData(cursor, PostalQuery.POSTAL_ADDRESS); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Dmitri Plotnikov | e124722 | 2010-06-02 18:14:21 -0700 | [diff] [blame] | 148 | protected void bindSectionHeaderAndDivider(final ContactListItemView view, int position) { |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 149 | final int section = getSectionForPosition(position); |
| 150 | if (getPositionForSection(section) == position) { |
| 151 | String title = (String)getSections()[section]; |
| 152 | view.setSectionHeader(title); |
| 153 | } else { |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 154 | view.setSectionHeader(null); |
| 155 | } |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | protected void bindName(final ContactListItemView view, Cursor cursor) { |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 159 | view.showDisplayName(cursor, PostalQuery.POSTAL_DISPLAY_NAME, getContactNameDisplayOrder()); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | protected void bindPhoto(final ContactListItemView view, Cursor cursor) { |
| 163 | long photoId = 0; |
Daisuke Miyakawa | ed90ea5 | 2011-10-31 09:22:52 -0700 | [diff] [blame] | 164 | if (!cursor.isNull(PostalQuery.POSTAL_PHOTO_ID)) { |
| 165 | photoId = cursor.getLong(PostalQuery.POSTAL_PHOTO_ID); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Yorke Lee | f677450 | 2014-02-20 10:21:26 -0800 | [diff] [blame] | 168 | DefaultImageRequest request = null; |
| 169 | if (photoId == 0) { |
| 170 | request = getDefaultImageRequestFromCursor(cursor, PostalQuery.POSTAL_DISPLAY_NAME, |
| 171 | PostalQuery.POSTAL_LOOKUP_KEY); |
| 172 | } |
| 173 | |
Yorke Lee | 41369d7 | 2014-04-28 18:17:51 -0700 | [diff] [blame] | 174 | getPhotoLoader().loadThumbnail(view.getPhotoView(), photoId, false, getCircularPhotos(), |
| 175 | request); |
Dmitri Plotnikov | d820cdb | 2010-05-06 14:11:55 -0700 | [diff] [blame] | 176 | } |
| 177 | // |
| 178 | // protected void bindSearchSnippet(final ContactListItemView view, Cursor cursor) { |
| 179 | // view.showSnippet(cursor, SUMMARY_SNIPPET_MIMETYPE_COLUMN_INDEX, |
| 180 | // SUMMARY_SNIPPET_DATA1_COLUMN_INDEX, SUMMARY_SNIPPET_DATA4_COLUMN_INDEX); |
| 181 | // } |
| 182 | |
| 183 | } |