Home | History | Annotate | Download | only in chips
      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;
     26 import android.graphics.Bitmap;
     27 import android.graphics.BitmapFactory;
     28 import android.net.Uri;
     29 import android.os.AsyncTask;
     30 import android.os.Handler;
     31 import android.os.Message;
     32 import android.provider.ContactsContract;
     33 import android.provider.ContactsContract.CommonDataKinds.Photo;
     34 import android.provider.ContactsContract.Directory;
     35 import android.text.TextUtils;
     36 import android.text.util.Rfc822Token;
     37 import android.util.Log;
     38 import android.util.LruCache;
     39 import android.view.LayoutInflater;
     40 import android.view.View;
     41 import android.view.ViewGroup;
     42 import android.widget.AutoCompleteTextView;
     43 import android.widget.BaseAdapter;
     44 import android.widget.Filter;
     45 import android.widget.Filterable;
     46 import android.widget.ImageView;
     47 import android.widget.TextView;
     48 
     49 import java.util.ArrayList;
     50 import java.util.HashSet;
     51 import java.util.LinkedHashMap;
     52 import java.util.List;
     53 import java.util.Map;
     54 import java.util.Set;
     55 
     56 /**
     57  * Adapter for showing a recipient list.
     58  */
     59 public 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                 // Return empty results.
    208                 return results;
    209             }
    210 
    211             try {
    212                 defaultDirectoryCursor = doQuery(constraint, mPreferredMaxResultCount, null);
    213 
    214                 if (defaultDirectoryCursor == null) {
    215                     if (DEBUG) {
    216                         Log.w(TAG, "null cursor returned for default Email filter query.");
    217                     }
    218                 } else {
    219                     // These variables will become mEntries, mEntryMap, mNonAggregatedEntries, and
    220                     // mExistingDestinations. Here we shouldn't use those member variables directly
    221                     // since this method is run outside the UI thread.
    222                     final LinkedHashMap<Long, List<RecipientEntry>> entryMap =
    223                             new LinkedHashMap<Long, List<RecipientEntry>>();
    224                     final List<RecipientEntry> nonAggregatedEntries =
    225                             new ArrayList<RecipientEntry>();
    226                     final Set<String> existingDestinations = new HashSet<String>();
    227 
    228                     while (defaultDirectoryCursor.moveToNext()) {
    229                         // Note: At this point each entry doesn't contain any photo
    230                         // (thus getPhotoBytes() returns null).
    231                         putOneEntry(new TemporaryEntry(defaultDirectoryCursor),
    232                                 true, entryMap, nonAggregatedEntries, existingDestinations);
    233                     }
    234 
    235                     // We'll copy this result to mEntry in publicResults() (run in the UX thread).
    236                     final List<RecipientEntry> entries = constructEntryList(false,
    237                             entryMap, nonAggregatedEntries, existingDestinations);
    238 
    239                     // After having local results, check the size of results. If the results are
    240                     // not enough, we search remote directories, which will take longer time.
    241                     final int limit = mPreferredMaxResultCount - existingDestinations.size();
    242                     final List<DirectorySearchParams> paramsList;
    243                     if (limit > 0) {
    244                         if (DEBUG) {
    245                             Log.d(TAG, "More entries should be needed (current: "
    246                                     + existingDestinations.size()
    247                                     + ", remaining limit: " + limit + ") ");
    248                         }
    249                         directoryCursor = mContentResolver.query(
    250                                 DirectoryListQuery.URI, DirectoryListQuery.PROJECTION,
    251                                 null, null, null);
    252                         paramsList = setupOtherDirectories(directoryCursor);
    253                     } else {
    254                         // We don't need to search other directories.
    255                         paramsList = null;
    256                     }
    257 
    258                     results.values = new DefaultFilterResult(
    259                             entries, entryMap, nonAggregatedEntries,
    260                             existingDestinations, paramsList);
    261                     results.count = 1;
    262                 }
    263             } finally {
    264                 if (defaultDirectoryCursor != null) {
    265                     defaultDirectoryCursor.close();
    266                 }
    267                 if (directoryCursor != null) {
    268                     directoryCursor.close();
    269                 }
    270             }
    271             return results;
    272         }
    273 
    274         @Override
    275         protected void publishResults(final CharSequence constraint, FilterResults results) {
    276             // If a user types a string very quickly and database is slow, "constraint" refers to
    277             // an older text which shows inconsistent results for users obsolete (b/4998713).
    278             // TODO: Fix it.
    279             mCurrentConstraint = constraint;
    280 
    281             if (results.values != null) {
    282                 DefaultFilterResult defaultFilterResult = (DefaultFilterResult) results.values;
    283                 mEntryMap = defaultFilterResult.entryMap;
    284                 mNonAggregatedEntries = defaultFilterResult.nonAggregatedEntries;
    285                 mExistingDestinations = defaultFilterResult.existingDestinations;
    286 
    287                 updateEntries(defaultFilterResult.entries);
    288 
    289                 // We need to search other remote directories, doing other Filter requests.
    290                 if (defaultFilterResult.paramsList != null) {
    291                     final int limit = mPreferredMaxResultCount -
    292                             defaultFilterResult.existingDestinations.size();
    293                     startSearchOtherDirectories(constraint, defaultFilterResult.paramsList, limit);
    294                 }
    295             }
    296 
    297         }
    298 
    299         @Override
    300         public CharSequence convertResultToString(Object resultValue) {
    301             final RecipientEntry entry = (RecipientEntry)resultValue;
    302             final String displayName = entry.getDisplayName();
    303             final String emailAddress = entry.getDestination();
    304             if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, emailAddress)) {
    305                  return emailAddress;
    306             } else {
    307                 return new Rfc822Token(displayName, emailAddress, null).toString();
    308             }
    309         }
    310     }
    311 
    312     /**
    313      * An asynchronous filter that performs search in a particular directory.
    314      */
    315     private final class DirectoryFilter extends Filter {
    316         private final DirectorySearchParams mParams;
    317         private int mLimit;
    318 
    319         public DirectoryFilter(DirectorySearchParams params) {
    320             mParams = params;
    321         }
    322 
    323         public synchronized void setLimit(int limit) {
    324             this.mLimit = limit;
    325         }
    326 
    327         public synchronized int getLimit() {
    328             return this.mLimit;
    329         }
    330 
    331         @Override
    332         protected FilterResults performFiltering(CharSequence constraint) {
    333             if (DEBUG) {
    334                 Log.d(TAG, "DirectoryFilter#performFiltering. directoryId: " + mParams.directoryId
    335                         + ", constraint: " + constraint + ", thread: " + Thread.currentThread());
    336             }
    337             final FilterResults results = new FilterResults();
    338             results.values = null;
    339             results.count = 0;
    340 
    341             if (!TextUtils.isEmpty(constraint)) {
    342                 final ArrayList<TemporaryEntry> tempEntries = new ArrayList<TemporaryEntry>();
    343 
    344                 Cursor cursor = null;
    345                 try {
    346                     // We don't want to pass this Cursor object to UI thread (b/5017608).
    347                     // Assuming the result should contain fairly small results (at most ~10),
    348                     // We just copy everything to local structure.
    349                     cursor = doQuery(constraint, getLimit(), mParams.directoryId);
    350 
    351                     if (cursor != null) {
    352                         while (cursor.moveToNext()) {
    353                             tempEntries.add(new TemporaryEntry(cursor));
    354                         }
    355                     }
    356                 } finally {
    357                     if (cursor != null) {
    358                         cursor.close();
    359                     }
    360                 }
    361                 if (!tempEntries.isEmpty()) {
    362                     results.values = tempEntries;
    363                     results.count = 1;
    364                 }
    365             }
    366 
    367             if (DEBUG) {
    368                 Log.v(TAG, "finished loading directory \"" + mParams.displayName + "\"" +
    369                         " with query " + constraint);
    370             }
    371 
    372             return results;
    373         }
    374 
    375         @Override
    376         protected void publishResults(final CharSequence constraint, FilterResults results) {
    377             if (DEBUG) {
    378                 Log.d(TAG, "DirectoryFilter#publishResult. constraint: " + constraint
    379                         + ", mCurrentConstraint: " + mCurrentConstraint);
    380             }
    381             mDelayedMessageHandler.removeDelayedLoadMessage();
    382             // Check if the received result matches the current constraint
    383             // If not - the user must have continued typing after the request was issued, which
    384             // means several member variables (like mRemainingDirectoryLoad) are already
    385             // overwritten so shouldn't be touched here anymore.
    386             if (TextUtils.equals(constraint, mCurrentConstraint)) {
    387                 if (results.count > 0) {
    388                     @SuppressWarnings("unchecked")
    389                     final ArrayList<TemporaryEntry> tempEntries =
    390                             (ArrayList<TemporaryEntry>) results.values;
    391 
    392                     for (TemporaryEntry tempEntry : tempEntries) {
    393                         putOneEntry(tempEntry, mParams.directoryId == Directory.DEFAULT,
    394                                 mEntryMap, mNonAggregatedEntries, mExistingDestinations);
    395                     }
    396                 }
    397 
    398                 // If there are remaining directories, set up delayed message again.
    399                 mRemainingDirectoryCount--;
    400                 if (mRemainingDirectoryCount > 0) {
    401                     if (DEBUG) {
    402                         Log.d(TAG, "Resend delayed load message. Current mRemainingDirectoryLoad: "
    403                                 + mRemainingDirectoryCount);
    404                     }
    405                     mDelayedMessageHandler.sendDelayedLoadMessage();
    406                 }
    407             }
    408 
    409             // Show the list again without "waiting" message.
    410             updateEntries(constructEntryList(false,
    411                     mEntryMap, mNonAggregatedEntries, mExistingDestinations));
    412         }
    413     }
    414 
    415     private final Context mContext;
    416     private final ContentResolver mContentResolver;
    417     private final LayoutInflater mInflater;
    418     private Account mAccount;
    419     private final int mPreferredMaxResultCount;
    420     private final Handler mHandler = new Handler();
    421 
    422     /**
    423      * {@link #mEntries} is responsible for showing every result for this Adapter. To
    424      * construct it, we use {@link #mEntryMap}, {@link #mNonAggregatedEntries}, and
    425      * {@link #mExistingDestinations}.
    426      *
    427      * First, each destination (an email address or a phone number) with a valid contactId is
    428      * inserted into {@link #mEntryMap} and grouped by the contactId. Destinations without valid
    429      * contactId (possible if they aren't in local storage) are stored in
    430      * {@link #mNonAggregatedEntries}.
    431      * Duplicates are removed using {@link #mExistingDestinations}.
    432      *
    433      * After having all results from Cursor objects, all destinations in mEntryMap are copied to
    434      * {@link #mEntries}. If the number of destinations is not enough (i.e. less than
    435      * {@link #mPreferredMaxResultCount}), destinations in mNonAggregatedEntries are also used.
    436      *
    437      * These variables are only used in UI thread, thus should not be touched in
    438      * performFiltering() methods.
    439      */
    440     private LinkedHashMap<Long, List<RecipientEntry>> mEntryMap;
    441     private List<RecipientEntry> mNonAggregatedEntries;
    442     private Set<String> mExistingDestinations;
    443     /** Note: use {@link #updateEntries(List)} to update this variable. */
    444     private List<RecipientEntry> mEntries;
    445 
    446     /** The number of directories this adapter is waiting for results. */
    447     private int mRemainingDirectoryCount;
    448 
    449     /**
    450      * Used to ignore asynchronous queries with a different constraint, which may happen when
    451      * users type characters quickly.
    452      */
    453     private CharSequence mCurrentConstraint;
    454 
    455     private final LruCache<Uri, byte[]> mPhotoCacheMap;
    456 
    457     /**
    458      * Handler specific for maintaining "Waiting for more contacts" message, which will be shown
    459      * when:
    460      * - there are directories to be searched
    461      * - results from directories are slow to come
    462      */
    463     private final class DelayedMessageHandler extends Handler {
    464         @Override
    465         public void handleMessage(Message msg) {
    466             if (mRemainingDirectoryCount > 0) {
    467                 updateEntries(constructEntryList(true,
    468                         mEntryMap, mNonAggregatedEntries, mExistingDestinations));
    469             }
    470         }
    471 
    472         public void sendDelayedLoadMessage() {
    473             sendMessageDelayed(obtainMessage(MESSAGE_SEARCH_PENDING, 0, 0, null),
    474                     MESSAGE_SEARCH_PENDING_DELAY);
    475         }
    476 
    477         public void removeDelayedLoadMessage() {
    478             removeMessages(MESSAGE_SEARCH_PENDING);
    479         }
    480     }
    481 
    482     private final DelayedMessageHandler mDelayedMessageHandler = new DelayedMessageHandler();
    483 
    484     /**
    485      * Constructor for email queries.
    486      */
    487     public BaseRecipientAdapter(Context context) {
    488         this(context, DEFAULT_PREFERRED_MAX_RESULT_COUNT, QUERY_TYPE_EMAIL);
    489     }
    490 
    491     public BaseRecipientAdapter(Context context, int preferredMaxResultCount) {
    492         this(context, preferredMaxResultCount, QUERY_TYPE_EMAIL);
    493     }
    494 
    495     public BaseRecipientAdapter(int queryMode, Context context) {
    496         this(context, DEFAULT_PREFERRED_MAX_RESULT_COUNT, queryMode);
    497     }
    498 
    499     public BaseRecipientAdapter(int queryMode, Context context, int preferredMaxResultCount) {
    500         this(context, preferredMaxResultCount, queryMode);
    501     }
    502 
    503     public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) {
    504         mContext = context;
    505         mContentResolver = context.getContentResolver();
    506         mInflater = LayoutInflater.from(context);
    507         mPreferredMaxResultCount = preferredMaxResultCount;
    508         mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
    509         mQueryType = queryMode;
    510 
    511         if (queryMode == QUERY_TYPE_EMAIL) {
    512             mQuery = Queries.EMAIL;
    513         } else if (queryMode == QUERY_TYPE_PHONE) {
    514             mQuery = Queries.PHONE;
    515         } else {
    516             mQuery = Queries.EMAIL;
    517             Log.e(TAG, "Unsupported query type: " + queryMode);
    518         }
    519     }
    520 
    521     public int getQueryType() {
    522         return mQueryType;
    523     }
    524 
    525     /**
    526      * Set the account when known. Causes the search to prioritize contacts from that account.
    527      */
    528     @Override
    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 void putOneEntry(TemporaryEntry entry, boolean isAggregatedEntry,
    617             LinkedHashMap<Long, List<RecipientEntry>> entryMap,
    618             List<RecipientEntry> nonAggregatedEntries,
    619             Set<String> existingDestinations) {
    620         if (existingDestinations.contains(entry.destination)) {
    621             return;
    622         }
    623 
    624         existingDestinations.add(entry.destination);
    625 
    626         if (!isAggregatedEntry) {
    627             nonAggregatedEntries.add(RecipientEntry.constructTopLevelEntry(
    628                     entry.displayName,
    629                     entry.displayNameSource,
    630                     entry.destination, entry.destinationType, entry.destinationLabel,
    631                     entry.contactId, entry.dataId, entry.thumbnailUriString));
    632         } else if (entryMap.containsKey(entry.contactId)) {
    633             // We already have a section for the person.
    634             final List<RecipientEntry> entryList = entryMap.get(entry.contactId);
    635             entryList.add(RecipientEntry.constructSecondLevelEntry(
    636                     entry.displayName,
    637                     entry.displayNameSource,
    638                     entry.destination, entry.destinationType, entry.destinationLabel,
    639                     entry.contactId, entry.dataId, entry.thumbnailUriString));
    640         } else {
    641             final List<RecipientEntry> entryList = new ArrayList<RecipientEntry>();
    642             entryList.add(RecipientEntry.constructTopLevelEntry(
    643                     entry.displayName,
    644                     entry.displayNameSource,
    645                     entry.destination, entry.destinationType, entry.destinationLabel,
    646                     entry.contactId, entry.dataId, entry.thumbnailUriString));
    647             entryMap.put(entry.contactId, entryList);
    648         }
    649     }
    650 
    651     /**
    652      * Constructs an actual list for this Adapter using {@link #mEntryMap}. Also tries to
    653      * fetch a cached photo for each contact entry (other than separators), or request another
    654      * thread to get one from directories.
    655      */
    656     private List<RecipientEntry> constructEntryList(
    657             boolean showMessageIfDirectoryLoadRemaining,
    658             LinkedHashMap<Long, List<RecipientEntry>> entryMap,
    659             List<RecipientEntry> nonAggregatedEntries,
    660             Set<String> existingDestinations) {
    661         final List<RecipientEntry> entries = new ArrayList<RecipientEntry>();
    662         int validEntryCount = 0;
    663         for (Map.Entry<Long, List<RecipientEntry>> mapEntry : entryMap.entrySet()) {
    664             final List<RecipientEntry> entryList = mapEntry.getValue();
    665             final int size = entryList.size();
    666             for (int i = 0; i < size; i++) {
    667                 RecipientEntry entry = entryList.get(i);
    668                 entries.add(entry);
    669                 tryFetchPhoto(entry);
    670                 validEntryCount++;
    671             }
    672             if (validEntryCount > mPreferredMaxResultCount) {
    673                 break;
    674             }
    675         }
    676         if (validEntryCount <= mPreferredMaxResultCount) {
    677             for (RecipientEntry entry : nonAggregatedEntries) {
    678                 if (validEntryCount > mPreferredMaxResultCount) {
    679                     break;
    680                 }
    681                 entries.add(entry);
    682                 tryFetchPhoto(entry);
    683 
    684                 validEntryCount++;
    685             }
    686         }
    687 
    688         return entries;
    689     }
    690 
    691     /** Resets {@link #mEntries} and notify the event to its parent ListView. */
    692     private void updateEntries(List<RecipientEntry> newEntries) {
    693         mEntries = newEntries;
    694         notifyDataSetChanged();
    695     }
    696 
    697     private void tryFetchPhoto(final RecipientEntry entry) {
    698         final Uri photoThumbnailUri = entry.getPhotoThumbnailUri();
    699         if (photoThumbnailUri != null) {
    700             final byte[] photoBytes = mPhotoCacheMap.get(photoThumbnailUri);
    701             if (photoBytes != null) {
    702                 entry.setPhotoBytes(photoBytes);
    703                 // notifyDataSetChanged() should be called by a caller.
    704             } else {
    705                 if (DEBUG) {
    706                     Log.d(TAG, "No photo cache for " + entry.getDisplayName()
    707                             + ". Fetch one asynchronously");
    708                 }
    709                 fetchPhotoAsync(entry, photoThumbnailUri);
    710             }
    711         }
    712     }
    713 
    714     private void fetchPhotoAsync(final RecipientEntry entry, final Uri photoThumbnailUri) {
    715         final AsyncTask<Void, Void, Void> photoLoadTask = new AsyncTask<Void, Void, Void>() {
    716             @Override
    717             protected Void doInBackground(Void... params) {
    718                 final Cursor photoCursor = mContentResolver.query(
    719                         photoThumbnailUri, PhotoQuery.PROJECTION, null, null, null);
    720                 if (photoCursor != null) {
    721                     try {
    722                         if (photoCursor.moveToFirst()) {
    723                             final byte[] photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO);
    724                             entry.setPhotoBytes(photoBytes);
    725 
    726                             mHandler.post(new Runnable() {
    727                                 @Override
    728                                 public void run() {
    729                                     mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
    730                                     notifyDataSetChanged();
    731                                 }
    732                             });
    733                         }
    734                     } finally {
    735                         photoCursor.close();
    736                     }
    737                 }
    738                 return null;
    739             }
    740         };
    741         photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    742     }
    743 
    744     protected void fetchPhoto(final RecipientEntry entry, final Uri photoThumbnailUri) {
    745         byte[] photoBytes = mPhotoCacheMap.get(photoThumbnailUri);
    746         if (photoBytes != null) {
    747             entry.setPhotoBytes(photoBytes);
    748             return;
    749         }
    750         final Cursor photoCursor = mContentResolver.query(photoThumbnailUri, PhotoQuery.PROJECTION,
    751                 null, null, null);
    752         if (photoCursor != null) {
    753             try {
    754                 if (photoCursor.moveToFirst()) {
    755                     photoBytes = photoCursor.getBlob(PhotoQuery.PHOTO);
    756                     entry.setPhotoBytes(photoBytes);
    757                     mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
    758                 }
    759             } finally {
    760                 photoCursor.close();
    761             }
    762         }
    763     }
    764 
    765     private Cursor doQuery(CharSequence constraint, int limit, Long directoryId) {
    766         final Uri.Builder builder = mQuery.getContentFilterUri().buildUpon()
    767                 .appendPath(constraint.toString())
    768                 .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
    769                         String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES));
    770         if (directoryId != null) {
    771             builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
    772                     String.valueOf(directoryId));
    773         }
    774         if (mAccount != null) {
    775             builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name);
    776             builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type);
    777         }
    778         final long start = System.currentTimeMillis();
    779         final Cursor cursor = mContentResolver.query(
    780                 builder.build(), mQuery.getProjection(), null, null, null);
    781         final long end = System.currentTimeMillis();
    782         if (DEBUG) {
    783             Log.d(TAG, "Time for autocomplete (query: " + constraint
    784                     + ", directoryId: " + directoryId + ", num_of_results: "
    785                     + (cursor != null ? cursor.getCount() : "null") + "): "
    786                     + (end - start) + " ms");
    787         }
    788         return cursor;
    789     }
    790 
    791     // TODO: This won't be used at all. We should find better way to quit the thread..
    792     /*public void close() {
    793         mEntries = null;
    794         mPhotoCacheMap.evictAll();
    795         if (!sPhotoHandlerThread.quit()) {
    796             Log.w(TAG, "Failed to quit photo handler thread, ignoring it.");
    797         }
    798     }*/
    799 
    800     @Override
    801     public int getCount() {
    802         return mEntries != null ? mEntries.size() : 0;
    803     }
    804 
    805     @Override
    806     public Object getItem(int position) {
    807         return mEntries.get(position);
    808     }
    809 
    810     @Override
    811     public long getItemId(int position) {
    812         return position;
    813     }
    814 
    815     @Override
    816     public int getViewTypeCount() {
    817         return RecipientEntry.ENTRY_TYPE_SIZE;
    818     }
    819 
    820     @Override
    821     public int getItemViewType(int position) {
    822         return mEntries.get(position).getEntryType();
    823     }
    824 
    825     @Override
    826     public boolean isEnabled(int position) {
    827         return mEntries.get(position).isSelectable();
    828     }
    829 
    830     @Override
    831     public View getView(int position, View convertView, ViewGroup parent) {
    832         final RecipientEntry entry = mEntries.get(position);
    833         String displayName = entry.getDisplayName();
    834         String destination = entry.getDestination();
    835         if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, destination)) {
    836             displayName = destination;
    837 
    838             // We only show the destination for secondary entries, so clear it
    839             // only for the first level.
    840             if (entry.isFirstLevel()) {
    841                 destination = null;
    842             }
    843         }
    844 
    845         final View itemView = convertView != null ? convertView : mInflater.inflate(
    846                 getItemLayout(), parent, false);
    847         final TextView displayNameView = (TextView) itemView.findViewById(getDisplayNameId());
    848         final TextView destinationView = (TextView) itemView.findViewById(getDestinationId());
    849         final TextView destinationTypeView = (TextView) itemView
    850                 .findViewById(getDestinationTypeId());
    851         final ImageView imageView = (ImageView) itemView.findViewById(getPhotoId());
    852         displayNameView.setText(displayName);
    853         if (!TextUtils.isEmpty(destination)) {
    854             destinationView.setText(destination);
    855         } else {
    856             destinationView.setText(null);
    857         }
    858         if (destinationTypeView != null) {
    859             final CharSequence destinationType = mQuery
    860                     .getTypeLabel(mContext.getResources(), entry.getDestinationType(),
    861                             entry.getDestinationLabel()).toString().toUpperCase();
    862 
    863             destinationTypeView.setText(destinationType);
    864         }
    865 
    866         if (entry.isFirstLevel()) {
    867             displayNameView.setVisibility(View.VISIBLE);
    868             if (imageView != null) {
    869                 imageView.setVisibility(View.VISIBLE);
    870                 final byte[] photoBytes = entry.getPhotoBytes();
    871                 if (photoBytes != null && imageView != null) {
    872                     final Bitmap photo = BitmapFactory.decodeByteArray(photoBytes, 0,
    873                             photoBytes.length);
    874                     imageView.setImageBitmap(photo);
    875                 } else {
    876                     imageView.setImageResource(getDefaultPhotoResource());
    877                 }
    878             }
    879         } else {
    880             displayNameView.setVisibility(View.GONE);
    881             if (imageView != null) {
    882                 imageView.setVisibility(View.INVISIBLE);
    883             }
    884         }
    885         return itemView;
    886     }
    887 
    888     /**
    889      * Returns a layout id for each item inside auto-complete list.
    890      *
    891      * Each View must contain two TextViews (for display name and destination) and one ImageView
    892      * (for photo). Ids for those should be available via {@link #getDisplayNameId()},
    893      * {@link #getDestinationId()}, and {@link #getPhotoId()}.
    894      */
    895     protected int getItemLayout() {
    896         return R.layout.chips_recipient_dropdown_item;
    897     }
    898 
    899     /**
    900      * Returns a resource ID representing an image which should be shown when ther's no relevant
    901      * photo is available.
    902      */
    903     protected int getDefaultPhotoResource() {
    904         return R.drawable.ic_contact_picture;
    905     }
    906 
    907     /**
    908      * Returns an id for TextView in an item View for showing a display name. By default
    909      * {@link android.R.id#title} is returned.
    910      */
    911     protected int getDisplayNameId() {
    912         return android.R.id.title;
    913     }
    914 
    915     /**
    916      * Returns an id for TextView in an item View for showing a destination
    917      * (an email address or a phone number).
    918      * By default {@link android.R.id#text1} is returned.
    919      */
    920     protected int getDestinationId() {
    921         return android.R.id.text1;
    922     }
    923 
    924     /**
    925      * Returns an id for TextView in an item View for showing the type of the destination.
    926      * By default {@link android.R.id#text2} is returned.
    927      */
    928     protected int getDestinationTypeId() {
    929         return android.R.id.text2;
    930     }
    931 
    932     /**
    933      * Returns an id for ImageView in an item View for showing photo image for a person. In default
    934      * {@link android.R.id#icon} is returned.
    935      */
    936     protected int getPhotoId() {
    937         return android.R.id.icon;
    938     }
    939 }
    940