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