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