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