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