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