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