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