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