Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.ex.chips; |
| 18 | |
| 19 | import android.accounts.Account; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.content.pm.PackageManager.NameNotFoundException; |
| 24 | import android.content.res.Resources; |
| 25 | import android.database.Cursor; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.os.Handler; |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 28 | import android.os.Message; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 29 | import android.provider.ContactsContract; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 30 | import android.provider.ContactsContract.Directory; |
| 31 | import android.text.TextUtils; |
| 32 | import android.text.util.Rfc822Token; |
| 33 | import android.util.Log; |
| 34 | import android.view.View; |
| 35 | import android.view.ViewGroup; |
| 36 | import android.widget.AutoCompleteTextView; |
| 37 | import android.widget.BaseAdapter; |
| 38 | import android.widget.Filter; |
| 39 | import android.widget.Filterable; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 40 | |
| 41 | import com.android.ex.chips.DropdownChipLayouter.AdapterType; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 42 | |
| 43 | import java.util.ArrayList; |
Scott Kennedy | fa7b0fb | 2014-04-18 14:40:22 -0700 | [diff] [blame] | 44 | import java.util.Collections; |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 45 | import java.util.HashSet; |
Daisuke Miyakawa | cf9337a | 2011-05-31 09:56:01 -0700 | [diff] [blame] | 46 | import java.util.LinkedHashMap; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 47 | import java.util.List; |
| 48 | import java.util.Map; |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 49 | import java.util.Set; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Adapter for showing a recipient list. |
| 53 | */ |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 54 | public class BaseRecipientAdapter extends BaseAdapter implements Filterable, AccountSpecifier, |
| 55 | PhotoManager.PhotoManagerCallback { |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 56 | private static final String TAG = "BaseRecipientAdapter"; |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 57 | |
Daisuke Miyakawa | 6b616f1 | 2011-09-19 17:08:40 -0700 | [diff] [blame] | 58 | private static final boolean DEBUG = false; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * The preferred number of results to be retrieved. This number may be |
| 62 | * exceeded if there are several directories configured, because we will use |
| 63 | * the same limit for all directories. |
| 64 | */ |
| 65 | private static final int DEFAULT_PREFERRED_MAX_RESULT_COUNT = 10; |
| 66 | |
| 67 | /** |
| 68 | * The number of extra entries requested to allow for duplicates. Duplicates |
| 69 | * are removed from the overall result. |
| 70 | */ |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 71 | static final int ALLOWANCE_FOR_DUPLICATES = 5; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 72 | |
Daisuke Miyakawa | 7537f84 | 2011-05-31 15:47:25 -0700 | [diff] [blame] | 73 | // This is ContactsContract.PRIMARY_ACCOUNT_NAME. Available from ICS as hidden |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 74 | static final String PRIMARY_ACCOUNT_NAME = "name_for_primary_account"; |
Daisuke Miyakawa | 7537f84 | 2011-05-31 15:47:25 -0700 | [diff] [blame] | 75 | // This is ContactsContract.PRIMARY_ACCOUNT_TYPE. Available from ICS as hidden |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 76 | static final String PRIMARY_ACCOUNT_TYPE = "type_for_primary_account"; |
Daisuke Miyakawa | 7537f84 | 2011-05-31 15:47:25 -0700 | [diff] [blame] | 77 | |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 78 | /** |
| 79 | * The "Waiting for more contacts" message will be displayed if search is not complete |
| 80 | * within this many milliseconds. |
| 81 | */ |
| 82 | private static final int MESSAGE_SEARCH_PENDING_DELAY = 1000; |
| 83 | /** Used to prepare "Waiting for more contacts" message. */ |
| 84 | private static final int MESSAGE_SEARCH_PENDING = 1; |
| 85 | |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 86 | public static final int QUERY_TYPE_EMAIL = 0; |
| 87 | public static final int QUERY_TYPE_PHONE = 1; |
| 88 | |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 89 | private final Queries.Query mQueryMode; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 90 | private final int mQueryType; |
| 91 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Model object for a {@link Directory} row. |
| 94 | */ |
| 95 | public final static class DirectorySearchParams { |
| 96 | public long directoryId; |
| 97 | public String directoryType; |
| 98 | public String displayName; |
| 99 | public String accountName; |
| 100 | public String accountType; |
| 101 | public CharSequence constraint; |
| 102 | public DirectoryFilter filter; |
| 103 | } |
| 104 | |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 105 | protected static class DirectoryListQuery { |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 106 | |
| 107 | public static final Uri URI = |
| 108 | Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "directories"); |
| 109 | public static final String[] PROJECTION = { |
| 110 | Directory._ID, // 0 |
| 111 | Directory.ACCOUNT_NAME, // 1 |
| 112 | Directory.ACCOUNT_TYPE, // 2 |
| 113 | Directory.DISPLAY_NAME, // 3 |
| 114 | Directory.PACKAGE_NAME, // 4 |
| 115 | Directory.TYPE_RESOURCE_ID, // 5 |
| 116 | }; |
| 117 | |
| 118 | public static final int ID = 0; |
| 119 | public static final int ACCOUNT_NAME = 1; |
| 120 | public static final int ACCOUNT_TYPE = 2; |
| 121 | public static final int DISPLAY_NAME = 3; |
| 122 | public static final int PACKAGE_NAME = 4; |
| 123 | public static final int TYPE_RESOURCE_ID = 5; |
| 124 | } |
| 125 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 126 | /** Used to temporarily hold results in Cursor objects. */ |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 127 | protected static class TemporaryEntry { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 128 | public final String displayName; |
| 129 | public final String destination; |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 130 | public final int destinationType; |
| 131 | public final String destinationLabel; |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 132 | public final long contactId; |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 133 | public final Long directoryId; |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 134 | public final long dataId; |
| 135 | public final String thumbnailUriString; |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 136 | public final int displayNameSource; |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 137 | public final String lookupKey; |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 138 | |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 139 | public TemporaryEntry( |
| 140 | String displayName, |
| 141 | String destination, |
| 142 | int destinationType, |
| 143 | String destinationLabel, |
| 144 | long contactId, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 145 | Long directoryId, |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 146 | long dataId, |
| 147 | String thumbnailUriString, |
Scott Kennedy | 514f8a7 | 2013-11-13 15:58:08 -0800 | [diff] [blame] | 148 | int displayNameSource, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 149 | String lookupKey) { |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 150 | this.displayName = displayName; |
| 151 | this.destination = destination; |
| 152 | this.destinationType = destinationType; |
| 153 | this.destinationLabel = destinationLabel; |
| 154 | this.contactId = contactId; |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 155 | this.directoryId = directoryId; |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 156 | this.dataId = dataId; |
| 157 | this.thumbnailUriString = thumbnailUriString; |
| 158 | this.displayNameSource = displayNameSource; |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 159 | this.lookupKey = lookupKey; |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 160 | } |
| 161 | |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 162 | public TemporaryEntry(Cursor cursor, Long directoryId) { |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 163 | this.displayName = cursor.getString(Queries.Query.NAME); |
| 164 | this.destination = cursor.getString(Queries.Query.DESTINATION); |
| 165 | this.destinationType = cursor.getInt(Queries.Query.DESTINATION_TYPE); |
| 166 | this.destinationLabel = cursor.getString(Queries.Query.DESTINATION_LABEL); |
| 167 | this.contactId = cursor.getLong(Queries.Query.CONTACT_ID); |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 168 | this.directoryId = directoryId; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 169 | this.dataId = cursor.getLong(Queries.Query.DATA_ID); |
| 170 | this.thumbnailUriString = cursor.getString(Queries.Query.PHOTO_THUMBNAIL_URI); |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 171 | this.displayNameSource = cursor.getInt(Queries.Query.DISPLAY_NAME_SOURCE); |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 172 | this.lookupKey = cursor.getString(Queries.Query.LOOKUP_KEY); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Used to pass results from {@link DefaultFilter#performFiltering(CharSequence)} to |
| 178 | * {@link DefaultFilter#publishResults(CharSequence, android.widget.Filter.FilterResults)} |
| 179 | */ |
| 180 | private static class DefaultFilterResult { |
| 181 | public final List<RecipientEntry> entries; |
| 182 | public final LinkedHashMap<Long, List<RecipientEntry>> entryMap; |
| 183 | public final List<RecipientEntry> nonAggregatedEntries; |
| 184 | public final Set<String> existingDestinations; |
| 185 | public final List<DirectorySearchParams> paramsList; |
| 186 | |
| 187 | public DefaultFilterResult(List<RecipientEntry> entries, |
| 188 | LinkedHashMap<Long, List<RecipientEntry>> entryMap, |
| 189 | List<RecipientEntry> nonAggregatedEntries, |
| 190 | Set<String> existingDestinations, |
| 191 | List<DirectorySearchParams> paramsList) { |
| 192 | this.entries = entries; |
| 193 | this.entryMap = entryMap; |
| 194 | this.nonAggregatedEntries = nonAggregatedEntries; |
| 195 | this.existingDestinations = existingDestinations; |
| 196 | this.paramsList = paramsList; |
| 197 | } |
| 198 | } |
| 199 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 200 | /** |
| 201 | * An asynchronous filter used for loading two data sets: email rows from the local |
| 202 | * contact provider and the list of {@link Directory}'s. |
| 203 | */ |
| 204 | private final class DefaultFilter extends Filter { |
| 205 | |
| 206 | @Override |
| 207 | protected FilterResults performFiltering(CharSequence constraint) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 208 | if (DEBUG) { |
| 209 | Log.d(TAG, "start filtering. constraint: " + constraint + ", thread:" |
| 210 | + Thread.currentThread()); |
| 211 | } |
| 212 | |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 213 | final FilterResults results = new FilterResults(); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 214 | Cursor defaultDirectoryCursor = null; |
| 215 | Cursor directoryCursor = null; |
| 216 | |
| 217 | if (TextUtils.isEmpty(constraint)) { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 218 | clearTempEntries(); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 219 | // Return empty results. |
| 220 | return results; |
| 221 | } |
| 222 | |
| 223 | try { |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 224 | defaultDirectoryCursor = doQuery(constraint, mPreferredMaxResultCount, |
| 225 | null /* directoryId */); |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 226 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 227 | if (defaultDirectoryCursor == null) { |
| 228 | if (DEBUG) { |
| 229 | Log.w(TAG, "null cursor returned for default Email filter query."); |
| 230 | } |
| 231 | } else { |
| 232 | // These variables will become mEntries, mEntryMap, mNonAggregatedEntries, and |
| 233 | // mExistingDestinations. Here we shouldn't use those member variables directly |
| 234 | // since this method is run outside the UI thread. |
| 235 | final LinkedHashMap<Long, List<RecipientEntry>> entryMap = |
| 236 | new LinkedHashMap<Long, List<RecipientEntry>>(); |
| 237 | final List<RecipientEntry> nonAggregatedEntries = |
| 238 | new ArrayList<RecipientEntry>(); |
| 239 | final Set<String> existingDestinations = new HashSet<String>(); |
| 240 | |
| 241 | while (defaultDirectoryCursor.moveToNext()) { |
| 242 | // Note: At this point each entry doesn't contain any photo |
| 243 | // (thus getPhotoBytes() returns null). |
Scott Kennedy | 514f8a7 | 2013-11-13 15:58:08 -0800 | [diff] [blame] | 244 | putOneEntry(new TemporaryEntry(defaultDirectoryCursor, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 245 | null /* directoryId */), |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 246 | true, entryMap, nonAggregatedEntries, existingDestinations); |
| 247 | } |
| 248 | |
| 249 | // We'll copy this result to mEntry in publicResults() (run in the UX thread). |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 250 | final List<RecipientEntry> entries = constructEntryList( |
| 251 | entryMap, nonAggregatedEntries); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 252 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 253 | final List<DirectorySearchParams> paramsList = |
| 254 | searchOtherDirectories(existingDestinations); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 255 | |
| 256 | results.values = new DefaultFilterResult( |
| 257 | entries, entryMap, nonAggregatedEntries, |
| 258 | existingDestinations, paramsList); |
Scott Kennedy | 2a87fbf | 2014-09-11 16:56:35 -0700 | [diff] [blame] | 259 | results.count = entries.size(); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 260 | } |
| 261 | } finally { |
| 262 | if (defaultDirectoryCursor != null) { |
| 263 | defaultDirectoryCursor.close(); |
| 264 | } |
| 265 | if (directoryCursor != null) { |
| 266 | directoryCursor.close(); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 269 | return results; |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | protected void publishResults(final CharSequence constraint, FilterResults results) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 274 | mCurrentConstraint = constraint; |
| 275 | |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 276 | clearTempEntries(); |
| 277 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 278 | if (results.values != null) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 279 | DefaultFilterResult defaultFilterResult = (DefaultFilterResult) results.values; |
| 280 | mEntryMap = defaultFilterResult.entryMap; |
| 281 | mNonAggregatedEntries = defaultFilterResult.nonAggregatedEntries; |
| 282 | mExistingDestinations = defaultFilterResult.existingDestinations; |
| 283 | |
Jin Cao | 31c33ef | 2014-09-08 10:46:41 -0700 | [diff] [blame] | 284 | cacheCurrentEntriesIfNeeded(defaultFilterResult.entries.size(), |
| 285 | defaultFilterResult.paramsList == null ? 0 : |
| 286 | defaultFilterResult.paramsList.size()); |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 287 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 288 | updateEntries(defaultFilterResult.entries); |
| 289 | |
| 290 | // We need to search other remote directories, doing other Filter requests. |
| 291 | if (defaultFilterResult.paramsList != null) { |
| 292 | final int limit = mPreferredMaxResultCount - |
| 293 | defaultFilterResult.existingDestinations.size(); |
| 294 | startSearchOtherDirectories(constraint, defaultFilterResult.paramsList, limit); |
| 295 | } |
Scott Kennedy | fa7b0fb | 2014-04-18 14:40:22 -0700 | [diff] [blame] | 296 | } else { |
| 297 | updateEntries(Collections.<RecipientEntry>emptyList()); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 298 | } |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public CharSequence convertResultToString(Object resultValue) { |
Mindy Pereira | f621a60 | 2011-05-31 10:09:35 -0700 | [diff] [blame] | 303 | final RecipientEntry entry = (RecipientEntry)resultValue; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 304 | final String displayName = entry.getDisplayName(); |
| 305 | final String emailAddress = entry.getDestination(); |
| 306 | if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, emailAddress)) { |
| 307 | return emailAddress; |
| 308 | } else { |
| 309 | return new Rfc822Token(displayName, emailAddress, null).toString(); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 314 | protected List<DirectorySearchParams> searchOtherDirectories(Set<String> existingDestinations) { |
| 315 | // After having local results, check the size of results. If the results are |
| 316 | // not enough, we search remote directories, which will take longer time. |
| 317 | final int limit = mPreferredMaxResultCount - existingDestinations.size(); |
| 318 | if (limit > 0) { |
| 319 | if (DEBUG) { |
| 320 | Log.d(TAG, "More entries should be needed (current: " |
| 321 | + existingDestinations.size() |
| 322 | + ", remaining limit: " + limit + ") "); |
| 323 | } |
| 324 | final Cursor directoryCursor = mContentResolver.query( |
| 325 | DirectoryListQuery.URI, DirectoryListQuery.PROJECTION, |
| 326 | null, null, null); |
| 327 | return setupOtherDirectories(mContext, directoryCursor, mAccount); |
| 328 | } else { |
| 329 | // We don't need to search other directories. |
| 330 | return null; |
| 331 | } |
| 332 | } |
| 333 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 334 | /** |
| 335 | * An asynchronous filter that performs search in a particular directory. |
| 336 | */ |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 337 | protected class DirectoryFilter extends Filter { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 338 | private final DirectorySearchParams mParams; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 339 | private int mLimit; |
| 340 | |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 341 | public DirectoryFilter(DirectorySearchParams params) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 342 | mParams = params; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | public synchronized void setLimit(int limit) { |
| 346 | this.mLimit = limit; |
| 347 | } |
| 348 | |
| 349 | public synchronized int getLimit() { |
| 350 | return this.mLimit; |
| 351 | } |
| 352 | |
| 353 | @Override |
| 354 | protected FilterResults performFiltering(CharSequence constraint) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 355 | if (DEBUG) { |
| 356 | Log.d(TAG, "DirectoryFilter#performFiltering. directoryId: " + mParams.directoryId |
| 357 | + ", constraint: " + constraint + ", thread: " + Thread.currentThread()); |
| 358 | } |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 359 | final FilterResults results = new FilterResults(); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 360 | results.values = null; |
| 361 | results.count = 0; |
| 362 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 363 | if (!TextUtils.isEmpty(constraint)) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 364 | final ArrayList<TemporaryEntry> tempEntries = new ArrayList<TemporaryEntry>(); |
| 365 | |
| 366 | Cursor cursor = null; |
| 367 | try { |
| 368 | // We don't want to pass this Cursor object to UI thread (b/5017608). |
| 369 | // Assuming the result should contain fairly small results (at most ~10), |
| 370 | // We just copy everything to local structure. |
| 371 | cursor = doQuery(constraint, getLimit(), mParams.directoryId); |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 372 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 373 | if (cursor != null) { |
| 374 | while (cursor.moveToNext()) { |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 375 | tempEntries.add(new TemporaryEntry(cursor, mParams.directoryId)); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | } finally { |
| 379 | if (cursor != null) { |
| 380 | cursor.close(); |
| 381 | } |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 382 | } |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 383 | if (!tempEntries.isEmpty()) { |
| 384 | results.values = tempEntries; |
Scott Kennedy | 2a87fbf | 2014-09-11 16:56:35 -0700 | [diff] [blame] | 385 | results.count = tempEntries.size(); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
| 389 | if (DEBUG) { |
| 390 | Log.v(TAG, "finished loading directory \"" + mParams.displayName + "\"" + |
| 391 | " with query " + constraint); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 394 | return results; |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | protected void publishResults(final CharSequence constraint, FilterResults results) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 399 | if (DEBUG) { |
| 400 | Log.d(TAG, "DirectoryFilter#publishResult. constraint: " + constraint |
| 401 | + ", mCurrentConstraint: " + mCurrentConstraint); |
| 402 | } |
| 403 | mDelayedMessageHandler.removeDelayedLoadMessage(); |
| 404 | // Check if the received result matches the current constraint |
| 405 | // If not - the user must have continued typing after the request was issued, which |
| 406 | // means several member variables (like mRemainingDirectoryLoad) are already |
| 407 | // overwritten so shouldn't be touched here anymore. |
| 408 | if (TextUtils.equals(constraint, mCurrentConstraint)) { |
| 409 | if (results.count > 0) { |
Andy Huang | dfd7e07 | 2011-11-18 14:57:32 -0800 | [diff] [blame] | 410 | @SuppressWarnings("unchecked") |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 411 | final ArrayList<TemporaryEntry> tempEntries = |
| 412 | (ArrayList<TemporaryEntry>) results.values; |
| 413 | |
| 414 | for (TemporaryEntry tempEntry : tempEntries) { |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 415 | putOneEntry(tempEntry, mParams.directoryId == Directory.DEFAULT); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | // If there are remaining directories, set up delayed message again. |
| 420 | mRemainingDirectoryCount--; |
| 421 | if (mRemainingDirectoryCount > 0) { |
| 422 | if (DEBUG) { |
| 423 | Log.d(TAG, "Resend delayed load message. Current mRemainingDirectoryLoad: " |
| 424 | + mRemainingDirectoryCount); |
| 425 | } |
| 426 | mDelayedMessageHandler.sendDelayedLoadMessage(); |
| 427 | } |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 428 | |
| 429 | // If this directory result has some items, or there are no more directories that |
| 430 | // we are waiting for, clear the temp results |
| 431 | if (results.count > 0 || mRemainingDirectoryCount == 0) { |
| 432 | // Clear the temp entries |
| 433 | clearTempEntries(); |
| 434 | } |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Show the list again without "waiting" message. |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 438 | updateEntries(constructEntryList()); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 442 | private final Context mContext; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 443 | private final ContentResolver mContentResolver; |
| 444 | private Account mAccount; |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 445 | protected final int mPreferredMaxResultCount; |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 446 | private DropdownChipLayouter mDropdownChipLayouter; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 447 | |
| 448 | /** |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 449 | * {@link #mEntries} is responsible for showing every result for this Adapter. To |
| 450 | * construct it, we use {@link #mEntryMap}, {@link #mNonAggregatedEntries}, and |
| 451 | * {@link #mExistingDestinations}. |
| 452 | * |
| 453 | * First, each destination (an email address or a phone number) with a valid contactId is |
| 454 | * inserted into {@link #mEntryMap} and grouped by the contactId. Destinations without valid |
| 455 | * contactId (possible if they aren't in local storage) are stored in |
| 456 | * {@link #mNonAggregatedEntries}. |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 457 | * Duplicates are removed using {@link #mExistingDestinations}. |
| 458 | * |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 459 | * After having all results from Cursor objects, all destinations in mEntryMap are copied to |
| 460 | * {@link #mEntries}. If the number of destinations is not enough (i.e. less than |
| 461 | * {@link #mPreferredMaxResultCount}), destinations in mNonAggregatedEntries are also used. |
| 462 | * |
| 463 | * These variables are only used in UI thread, thus should not be touched in |
| 464 | * performFiltering() methods. |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 465 | */ |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 466 | private LinkedHashMap<Long, List<RecipientEntry>> mEntryMap; |
| 467 | private List<RecipientEntry> mNonAggregatedEntries; |
| 468 | private Set<String> mExistingDestinations; |
| 469 | /** Note: use {@link #updateEntries(List)} to update this variable. */ |
| 470 | private List<RecipientEntry> mEntries; |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 471 | private List<RecipientEntry> mTempEntries; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 472 | |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 473 | /** The number of directories this adapter is waiting for results. */ |
| 474 | private int mRemainingDirectoryCount; |
| 475 | |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 476 | /** |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 477 | * Used to ignore asynchronous queries with a different constraint, which may happen when |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 478 | * users type characters quickly. |
| 479 | */ |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 480 | protected CharSequence mCurrentConstraint; |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 481 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 482 | /** |
| 483 | * Performs all photo querying as well as caching for repeated lookups. |
| 484 | */ |
| 485 | private PhotoManager mPhotoManager; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 486 | |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 487 | /** |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 488 | * Handler specific for maintaining "Waiting for more contacts" message, which will be shown |
| 489 | * when: |
| 490 | * - there are directories to be searched |
| 491 | * - results from directories are slow to come |
| 492 | */ |
| 493 | private final class DelayedMessageHandler extends Handler { |
| 494 | @Override |
| 495 | public void handleMessage(Message msg) { |
| 496 | if (mRemainingDirectoryCount > 0) { |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 497 | updateEntries(constructEntryList()); |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | public void sendDelayedLoadMessage() { |
| 502 | sendMessageDelayed(obtainMessage(MESSAGE_SEARCH_PENDING, 0, 0, null), |
| 503 | MESSAGE_SEARCH_PENDING_DELAY); |
| 504 | } |
| 505 | |
| 506 | public void removeDelayedLoadMessage() { |
| 507 | removeMessages(MESSAGE_SEARCH_PENDING); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | private final DelayedMessageHandler mDelayedMessageHandler = new DelayedMessageHandler(); |
| 512 | |
mindyp | 8c474ec | 2012-12-14 10:58:44 -0800 | [diff] [blame] | 513 | private EntriesUpdatedObserver mEntriesUpdatedObserver; |
| 514 | |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 515 | /** |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 516 | * Constructor for email queries. |
| 517 | */ |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 518 | public BaseRecipientAdapter(Context context) { |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 519 | this(context, DEFAULT_PREFERRED_MAX_RESULT_COUNT, QUERY_TYPE_EMAIL); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 520 | } |
| 521 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 522 | public BaseRecipientAdapter(Context context, int preferredMaxResultCount) { |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 523 | this(context, preferredMaxResultCount, QUERY_TYPE_EMAIL); |
| 524 | } |
| 525 | |
| 526 | public BaseRecipientAdapter(int queryMode, Context context) { |
| 527 | this(context, DEFAULT_PREFERRED_MAX_RESULT_COUNT, queryMode); |
| 528 | } |
| 529 | |
| 530 | public BaseRecipientAdapter(int queryMode, Context context, int preferredMaxResultCount) { |
| 531 | this(context, preferredMaxResultCount, queryMode); |
| 532 | } |
| 533 | |
| 534 | public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) { |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 535 | mContext = context; |
| 536 | mContentResolver = context.getContentResolver(); |
| 537 | mPreferredMaxResultCount = preferredMaxResultCount; |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 538 | mPhotoManager = new DefaultPhotoManager(mContentResolver); |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 539 | mQueryType = queryMode; |
| 540 | |
| 541 | if (queryMode == QUERY_TYPE_EMAIL) { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 542 | mQueryMode = Queries.EMAIL; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 543 | } else if (queryMode == QUERY_TYPE_PHONE) { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 544 | mQueryMode = Queries.PHONE; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 545 | } else { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 546 | mQueryMode = Queries.EMAIL; |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 547 | Log.e(TAG, "Unsupported query type: " + queryMode); |
| 548 | } |
| 549 | } |
| 550 | |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 551 | public Context getContext() { |
| 552 | return mContext; |
| 553 | } |
| 554 | |
Tom Taylor | 80f4abf | 2012-04-06 13:37:20 -0700 | [diff] [blame] | 555 | public int getQueryType() { |
| 556 | return mQueryType; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 559 | public void setDropdownChipLayouter(DropdownChipLayouter dropdownChipLayouter) { |
| 560 | mDropdownChipLayouter = dropdownChipLayouter; |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 561 | mDropdownChipLayouter.setQuery(mQueryMode); |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | public DropdownChipLayouter getDropdownChipLayouter() { |
| 565 | return mDropdownChipLayouter; |
| 566 | } |
| 567 | |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 568 | /** |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 569 | * Enables overriding the default photo manager that is used. |
| 570 | */ |
| 571 | public void setPhotoManager(PhotoManager photoManager) { |
| 572 | mPhotoManager = photoManager; |
| 573 | } |
| 574 | |
| 575 | public PhotoManager getPhotoManager() { |
| 576 | return mPhotoManager; |
| 577 | } |
| 578 | |
| 579 | /** |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 580 | * If true, forces using the {@link com.android.ex.chips.SingleRecipientArrayAdapter} |
| 581 | * instead of {@link com.android.ex.chips.RecipientAlternatesAdapter} when |
| 582 | * clicking on a chip. Default implementation returns {@code false}. |
| 583 | */ |
| 584 | public boolean forceShowAddress() { |
| 585 | return false; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Used to replace email addresses with chips. Default behavior |
| 590 | * queries the ContactsProvider for contact information about the contact. |
| 591 | * Derived classes should override this method if they wish to use a |
| 592 | * new data source. |
| 593 | * @param inAddresses addresses to query |
| 594 | * @param callback callback to return results in case of success or failure |
| 595 | */ |
| 596 | public void getMatchingRecipients(ArrayList<String> inAddresses, |
| 597 | RecipientAlternatesAdapter.RecipientMatchCallback callback) { |
| 598 | RecipientAlternatesAdapter.getMatchingRecipients( |
| 599 | getContext(), this, inAddresses, getAccount(), callback); |
| 600 | } |
| 601 | |
| 602 | /** |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 603 | * Set the account when known. Causes the search to prioritize contacts from that account. |
| 604 | */ |
Andy Huang | dfd7e07 | 2011-11-18 14:57:32 -0800 | [diff] [blame] | 605 | @Override |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 606 | public void setAccount(Account account) { |
| 607 | mAccount = account; |
| 608 | } |
| 609 | |
| 610 | /** Will be called from {@link AutoCompleteTextView} to prepare auto-complete list. */ |
| 611 | @Override |
| 612 | public Filter getFilter() { |
| 613 | return new DefaultFilter(); |
| 614 | } |
| 615 | |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 616 | /** |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 617 | * An extension to {@link RecipientAlternatesAdapter#getMatchingRecipients} that allows |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 618 | * additional sources of contacts to be considered as matching recipients. |
| 619 | * @param addresses A set of addresses to be matched |
| 620 | * @return A list of matches or null if none found |
| 621 | */ |
| 622 | public Map<String, RecipientEntry> getMatchingRecipients(Set<String> addresses) { |
| 623 | return null; |
| 624 | } |
| 625 | |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 626 | public static List<DirectorySearchParams> setupOtherDirectories(Context context, |
| 627 | Cursor directoryCursor, Account account) { |
| 628 | final PackageManager packageManager = context.getPackageManager(); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 629 | final List<DirectorySearchParams> paramsList = new ArrayList<DirectorySearchParams>(); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 630 | DirectorySearchParams preferredDirectory = null; |
| 631 | while (directoryCursor.moveToNext()) { |
| 632 | final long id = directoryCursor.getLong(DirectoryListQuery.ID); |
| 633 | |
| 634 | // Skip the local invisible directory, because the default directory already includes |
| 635 | // all local results. |
| 636 | if (id == Directory.LOCAL_INVISIBLE) { |
| 637 | continue; |
| 638 | } |
| 639 | |
| 640 | final DirectorySearchParams params = new DirectorySearchParams(); |
| 641 | final String packageName = directoryCursor.getString(DirectoryListQuery.PACKAGE_NAME); |
| 642 | final int resourceId = directoryCursor.getInt(DirectoryListQuery.TYPE_RESOURCE_ID); |
| 643 | params.directoryId = id; |
| 644 | params.displayName = directoryCursor.getString(DirectoryListQuery.DISPLAY_NAME); |
| 645 | params.accountName = directoryCursor.getString(DirectoryListQuery.ACCOUNT_NAME); |
| 646 | params.accountType = directoryCursor.getString(DirectoryListQuery.ACCOUNT_TYPE); |
| 647 | if (packageName != null && resourceId != 0) { |
| 648 | try { |
| 649 | final Resources resources = |
| 650 | packageManager.getResourcesForApplication(packageName); |
| 651 | params.directoryType = resources.getString(resourceId); |
| 652 | if (params.directoryType == null) { |
| 653 | Log.e(TAG, "Cannot resolve directory name: " |
| 654 | + resourceId + "@" + packageName); |
| 655 | } |
| 656 | } catch (NameNotFoundException e) { |
| 657 | Log.e(TAG, "Cannot resolve directory name: " |
| 658 | + resourceId + "@" + packageName, e); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // If an account has been provided and we found a directory that |
| 663 | // corresponds to that account, place that directory second, directly |
| 664 | // underneath the local contacts. |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 665 | if (account != null && account.name.equals(params.accountName) && |
| 666 | account.type.equals(params.accountType)) { |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 667 | preferredDirectory = params; |
| 668 | } else { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 669 | paramsList.add(params); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 670 | } |
| 671 | } |
| 672 | |
| 673 | if (preferredDirectory != null) { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 674 | paramsList.add(1, preferredDirectory); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 677 | return paramsList; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 681 | * Starts search in other directories using {@link Filter}. Results will be handled in |
| 682 | * {@link DirectoryFilter}. |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 683 | */ |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 684 | protected void startSearchOtherDirectories( |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 685 | CharSequence constraint, List<DirectorySearchParams> paramsList, int limit) { |
| 686 | final int count = paramsList.size(); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 687 | // Note: skipping the default partition (index 0), which has already been loaded |
| 688 | for (int i = 1; i < count; i++) { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 689 | final DirectorySearchParams params = paramsList.get(i); |
| 690 | params.constraint = constraint; |
| 691 | if (params.filter == null) { |
| 692 | params.filter = new DirectoryFilter(params); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 693 | } |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 694 | params.filter.setLimit(limit); |
| 695 | params.filter.filter(constraint); |
| 696 | } |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 697 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 698 | // Directory search started. We may show "waiting" message if directory results are slow |
| 699 | // enough. |
Daisuke Miyakawa | 4bb6a34 | 2011-07-11 10:28:02 -0700 | [diff] [blame] | 700 | mRemainingDirectoryCount = count - 1; |
| 701 | mDelayedMessageHandler.sendDelayedLoadMessage(); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 704 | /** |
| 705 | * Called whenever {@link com.android.ex.chips.BaseRecipientAdapter.DirectoryFilter} |
| 706 | * wants to add an additional entry to the results. Derived classes should override |
| 707 | * this method if they are not using the default data structures provided by |
| 708 | * {@link com.android.ex.chips.BaseRecipientAdapter} and are instead using their |
| 709 | * own data structures to store and collate data. |
| 710 | * @param entry the entry being added |
| 711 | * @param isAggregatedEntry |
| 712 | */ |
| 713 | protected void putOneEntry(TemporaryEntry entry, boolean isAggregatedEntry) { |
| 714 | putOneEntry(entry, isAggregatedEntry, |
| 715 | mEntryMap, mNonAggregatedEntries, mExistingDestinations); |
| 716 | } |
| 717 | |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 718 | private static void putOneEntry(TemporaryEntry entry, boolean isAggregatedEntry, |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 719 | LinkedHashMap<Long, List<RecipientEntry>> entryMap, |
| 720 | List<RecipientEntry> nonAggregatedEntries, |
| 721 | Set<String> existingDestinations) { |
| 722 | if (existingDestinations.contains(entry.destination)) { |
| 723 | return; |
| 724 | } |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 725 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 726 | existingDestinations.add(entry.destination); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 727 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 728 | if (!isAggregatedEntry) { |
| 729 | nonAggregatedEntries.add(RecipientEntry.constructTopLevelEntry( |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 730 | entry.displayName, |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 731 | entry.displayNameSource, |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 732 | entry.destination, entry.destinationType, entry.destinationLabel, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 733 | entry.contactId, entry.directoryId, entry.dataId, entry.thumbnailUriString, |
| 734 | true, entry.lookupKey)); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 735 | } else if (entryMap.containsKey(entry.contactId)) { |
| 736 | // We already have a section for the person. |
| 737 | final List<RecipientEntry> entryList = entryMap.get(entry.contactId); |
| 738 | entryList.add(RecipientEntry.constructSecondLevelEntry( |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 739 | entry.displayName, |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 740 | entry.displayNameSource, |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 741 | entry.destination, entry.destinationType, entry.destinationLabel, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 742 | entry.contactId, entry.directoryId, entry.dataId, entry.thumbnailUriString, |
| 743 | true, entry.lookupKey)); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 744 | } else { |
| 745 | final List<RecipientEntry> entryList = new ArrayList<RecipientEntry>(); |
| 746 | entryList.add(RecipientEntry.constructTopLevelEntry( |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 747 | entry.displayName, |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 748 | entry.displayNameSource, |
Daisuke Miyakawa | 7211747 | 2011-07-20 14:10:10 -0700 | [diff] [blame] | 749 | entry.destination, entry.destinationType, entry.destinationLabel, |
Scott Kennedy | 7a4e677 | 2013-11-21 14:31:33 -0800 | [diff] [blame] | 750 | entry.contactId, entry.directoryId, entry.dataId, entry.thumbnailUriString, |
| 751 | true, entry.lookupKey)); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 752 | entryMap.put(entry.contactId, entryList); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
| 756 | /** |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 757 | * Returns the actual list to use for this Adapter. Derived classes |
| 758 | * should override this method if overriding how the adapter stores and collates |
| 759 | * data. |
| 760 | */ |
| 761 | protected List<RecipientEntry> constructEntryList() { |
| 762 | return constructEntryList(mEntryMap, mNonAggregatedEntries); |
| 763 | } |
| 764 | |
| 765 | /** |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 766 | * Constructs an actual list for this Adapter using {@link #mEntryMap}. Also tries to |
| 767 | * fetch a cached photo for each contact entry (other than separators), or request another |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 768 | * thread to get one from directories. |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 769 | */ |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 770 | private List<RecipientEntry> constructEntryList( |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 771 | LinkedHashMap<Long, List<RecipientEntry>> entryMap, |
Scott Kennedy | f7e202d | 2013-03-06 21:38:10 -0800 | [diff] [blame] | 772 | List<RecipientEntry> nonAggregatedEntries) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 773 | final List<RecipientEntry> entries = new ArrayList<RecipientEntry>(); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 774 | int validEntryCount = 0; |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 775 | for (Map.Entry<Long, List<RecipientEntry>> mapEntry : entryMap.entrySet()) { |
Mindy Pereira | f621a60 | 2011-05-31 10:09:35 -0700 | [diff] [blame] | 776 | final List<RecipientEntry> entryList = mapEntry.getValue(); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 777 | final int size = entryList.size(); |
| 778 | for (int i = 0; i < size; i++) { |
Mindy Pereira | f621a60 | 2011-05-31 10:09:35 -0700 | [diff] [blame] | 779 | RecipientEntry entry = entryList.get(i); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 780 | entries.add(entry); |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 781 | mPhotoManager.populatePhotoBytesAsync(entry, this); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 782 | validEntryCount++; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 783 | } |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 784 | if (validEntryCount > mPreferredMaxResultCount) { |
| 785 | break; |
| 786 | } |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 787 | } |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 788 | if (validEntryCount <= mPreferredMaxResultCount) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 789 | for (RecipientEntry entry : nonAggregatedEntries) { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 790 | if (validEntryCount > mPreferredMaxResultCount) { |
| 791 | break; |
| 792 | } |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 793 | entries.add(entry); |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 794 | mPhotoManager.populatePhotoBytesAsync(entry, this); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 795 | validEntryCount++; |
| 796 | } |
| 797 | } |
| 798 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 799 | return entries; |
| 800 | } |
| 801 | |
mindyp | 8c474ec | 2012-12-14 10:58:44 -0800 | [diff] [blame] | 802 | |
Mike Schneider | 38fe984 | 2014-03-04 08:14:41 +0100 | [diff] [blame] | 803 | public interface EntriesUpdatedObserver { |
mindyp | 8c474ec | 2012-12-14 10:58:44 -0800 | [diff] [blame] | 804 | public void onChanged(List<RecipientEntry> entries); |
| 805 | } |
| 806 | |
| 807 | public void registerUpdateObserver(EntriesUpdatedObserver observer) { |
| 808 | mEntriesUpdatedObserver = observer; |
| 809 | } |
| 810 | |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 811 | /** Resets {@link #mEntries} and notify the event to its parent ListView. */ |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 812 | protected void updateEntries(List<RecipientEntry> newEntries) { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 813 | mEntries = newEntries; |
mindyp | 8c474ec | 2012-12-14 10:58:44 -0800 | [diff] [blame] | 814 | mEntriesUpdatedObserver.onChanged(newEntries); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 815 | notifyDataSetChanged(); |
| 816 | } |
| 817 | |
Jin Cao | 31c33ef | 2014-09-08 10:46:41 -0700 | [diff] [blame] | 818 | /** |
| 819 | * If there are no local results and we are searching alternate results, |
| 820 | * in the new result set, cache off what had been shown to the user for use until |
| 821 | * the first directory result is returned |
| 822 | * @param newEntryCount number of newly loaded entries |
| 823 | * @param paramListCount number of alternate filters it will search (including the current one). |
| 824 | */ |
| 825 | protected void cacheCurrentEntriesIfNeeded(int newEntryCount, int paramListCount) { |
| 826 | if (newEntryCount == 0 && paramListCount > 1) { |
| 827 | cacheCurrentEntries(); |
| 828 | } |
| 829 | } |
| 830 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 831 | protected void cacheCurrentEntries() { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 832 | mTempEntries = mEntries; |
| 833 | } |
| 834 | |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 835 | protected void clearTempEntries() { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 836 | mTempEntries = null; |
| 837 | } |
| 838 | |
Alon Albert | 76f1f2d | 2013-07-14 15:32:49 +0300 | [diff] [blame] | 839 | protected List<RecipientEntry> getEntries() { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 840 | return mTempEntries != null ? mTempEntries : mEntries; |
| 841 | } |
| 842 | |
Jin Cao | 35e82d4 | 2014-06-02 17:21:21 -0700 | [diff] [blame] | 843 | protected void fetchPhoto(final RecipientEntry entry, PhotoManager.PhotoManagerCallback cb) { |
| 844 | mPhotoManager.populatePhotoBytesAsync(entry, cb); |
Mindy Pereira | 6b6de62 | 2011-06-07 16:39:24 -0700 | [diff] [blame] | 845 | } |
| 846 | |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 847 | private Cursor doQuery(CharSequence constraint, int limit, Long directoryId) { |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 848 | final Uri.Builder builder = mQueryMode.getContentFilterUri().buildUpon() |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 849 | .appendPath(constraint.toString()) |
| 850 | .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, |
| 851 | String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES)); |
| 852 | if (directoryId != null) { |
| 853 | builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 854 | String.valueOf(directoryId)); |
| 855 | } |
| 856 | if (mAccount != null) { |
| 857 | builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name); |
| 858 | builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type); |
| 859 | } |
| 860 | final long start = System.currentTimeMillis(); |
| 861 | final Cursor cursor = mContentResolver.query( |
Andrew Sapperstein | 1db635b | 2014-04-29 13:07:57 -0700 | [diff] [blame] | 862 | builder.build(), mQueryMode.getProjection(), null, null, null); |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 863 | final long end = System.currentTimeMillis(); |
| 864 | if (DEBUG) { |
| 865 | Log.d(TAG, "Time for autocomplete (query: " + constraint |
| 866 | + ", directoryId: " + directoryId + ", num_of_results: " |
| 867 | + (cursor != null ? cursor.getCount() : "null") + "): " |
| 868 | + (end - start) + " ms"); |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 869 | } |
| 870 | return cursor; |
| 871 | } |
| 872 | |
Daisuke Miyakawa | cc20880 | 2011-07-21 15:10:06 -0700 | [diff] [blame] | 873 | // TODO: This won't be used at all. We should find better way to quit the thread.. |
| 874 | /*public void close() { |
Daisuke Miyakawa | 8383f44 | 2011-07-05 13:58:29 -0700 | [diff] [blame] | 875 | mEntries = null; |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 876 | mPhotoCacheMap.evictAll(); |
Daisuke Miyakawa | cc20880 | 2011-07-21 15:10:06 -0700 | [diff] [blame] | 877 | if (!sPhotoHandlerThread.quit()) { |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 878 | Log.w(TAG, "Failed to quit photo handler thread, ignoring it."); |
| 879 | } |
Daisuke Miyakawa | cc20880 | 2011-07-21 15:10:06 -0700 | [diff] [blame] | 880 | }*/ |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 881 | |
| 882 | @Override |
| 883 | public int getCount() { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 884 | final List<RecipientEntry> entries = getEntries(); |
| 885 | return entries != null ? entries.size() : 0; |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | @Override |
Scott Kennedy | 858e094 | 2013-10-10 11:50:01 -0700 | [diff] [blame] | 889 | public RecipientEntry getItem(int position) { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 890 | return getEntries().get(position); |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | @Override |
| 894 | public long getItemId(int position) { |
| 895 | return position; |
| 896 | } |
| 897 | |
| 898 | @Override |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 899 | public int getViewTypeCount() { |
Mindy Pereira | f621a60 | 2011-05-31 10:09:35 -0700 | [diff] [blame] | 900 | return RecipientEntry.ENTRY_TYPE_SIZE; |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public int getItemViewType(int position) { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 905 | return getEntries().get(position).getEntryType(); |
Daisuke Miyakawa | 74a977c | 2011-05-24 17:55:17 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | @Override |
Daisuke Miyakawa | 6d6bd68 | 2011-07-16 17:33:10 -0700 | [diff] [blame] | 909 | public boolean isEnabled(int position) { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 910 | return getEntries().get(position).isSelectable(); |
Daisuke Miyakawa | 6d6bd68 | 2011-07-16 17:33:10 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | @Override |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 914 | public View getView(int position, View convertView, ViewGroup parent) { |
Paul Westbrook | 37726c0 | 2012-10-25 10:27:38 -0700 | [diff] [blame] | 915 | final RecipientEntry entry = getEntries().get(position); |
Makoto Onuki | 00adb32 | 2012-05-01 16:50:41 -0700 | [diff] [blame] | 916 | |
Kevin Lin | d5ce95b | 2014-01-27 15:58:40 -0800 | [diff] [blame] | 917 | final String constraint = mCurrentConstraint == null ? null : |
| 918 | mCurrentConstraint.toString(); |
| 919 | |
Kevin Lin | b10d1c6 | 2014-01-24 12:45:00 -0800 | [diff] [blame] | 920 | return mDropdownChipLayouter.bindView(convertView, parent, entry, position, |
Kevin Lin | d5ce95b | 2014-01-27 15:58:40 -0800 | [diff] [blame] | 921 | AdapterType.BASE_RECIPIENT, constraint); |
Daisuke Miyakawa | d4baa3f | 2011-05-26 09:24:00 -0700 | [diff] [blame] | 922 | } |
mindyp | 16923ee | 2012-12-10 11:58:29 -0800 | [diff] [blame] | 923 | |
| 924 | public Account getAccount() { |
| 925 | return mAccount; |
| 926 | } |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 927 | |
| 928 | @Override |
Andrew Sapperstein | 50429c5 | 2014-06-12 15:12:10 -0700 | [diff] [blame] | 929 | public void onPhotoBytesPopulated() { |
| 930 | // Default implementation does nothing |
| 931 | } |
| 932 | |
| 933 | @Override |
Andrew Sapperstein | 8af0d3b | 2014-05-02 15:23:19 -0700 | [diff] [blame] | 934 | public void onPhotoBytesAsynchronouslyPopulated() { |
| 935 | notifyDataSetChanged(); |
| 936 | } |
Jin Cao | 0efdc53 | 2014-06-04 15:35:16 -0700 | [diff] [blame] | 937 | |
| 938 | @Override |
| 939 | public void onPhotoBytesAsyncLoadFailed() { |
Andrew Sapperstein | 50429c5 | 2014-06-12 15:12:10 -0700 | [diff] [blame] | 940 | // Default implementation does nothing |
Jin Cao | 0efdc53 | 2014-06-04 15:35:16 -0700 | [diff] [blame] | 941 | } |
Daisuke Miyakawa | 6ac7d0c | 2011-05-20 14:15:27 -0700 | [diff] [blame] | 942 | } |