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