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