/
BaseEmailAddressAdapter.java
602 lines (524 loc) · 23.1 KB
/
BaseEmailAddressAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.myandroid.common.contacts;
import com.myandroid.common.widget.CompositeCursorAdapter;
import android.accounts.Account;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.Contacts;
import android.text.TextUtils;
import android.text.util.Rfc822Token;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import java.util.ArrayList;
import java.util.List;
/**
* A base class for email address autocomplete adapters. It uses
* {@link Email#CONTENT_FILTER_URI} to search for data rows by email address
* and/or contact name. It also searches registered {@link Directory}'s.
*/
public abstract class BaseEmailAddressAdapter extends CompositeCursorAdapter implements Filterable {
private static final String TAG = "BaseEmailAddressAdapter";
// TODO: revert to references to the Directory class as soon as the
// issue with the dependency on SDK 8 is resolved
// This is Directory.LOCAL_INVISIBLE
private static final long DIRECTORY_LOCAL_INVISIBLE = 1;
// This is ContactsContract.DIRECTORY_PARAM_KEY
private static final String DIRECTORY_PARAM_KEY = "directory";
// This is ContactsContract.LIMIT_PARAM_KEY
private static final String LIMIT_PARAM_KEY = "limit";
// This is ContactsContract.PRIMARY_ACCOUNT_NAME
private static final String PRIMARY_ACCOUNT_NAME = "name_for_primary_account";
// This is ContactsContract.PRIMARY_ACCOUNT_TYPE
private static final String PRIMARY_ACCOUNT_TYPE = "type_for_primary_account";
/**
* The preferred number of results to be retrieved. This number may be
* exceeded if there are several directories configured, because we will use
* the same limit for all directories.
*/
private static final int DEFAULT_PREFERRED_MAX_RESULT_COUNT = 10;
/**
* The number of extra entries requested to allow for duplicates. Duplicates
* are removed from the overall result.
*/
private static final int ALLOWANCE_FOR_DUPLICATES = 5;
/**
* The "Searching..." message will be displayed if search is not complete
* within this many milliseconds.
*/
private static final int MESSAGE_SEARCH_PENDING_DELAY = 1000;
private static final int MESSAGE_SEARCH_PENDING = 1;
/**
* Model object for a {@link Directory} row. There is a partition in the
* {@link CompositeCursorAdapter} for every directory (except
* {@link Directory#LOCAL_INVISIBLE}.
*/
public final static class DirectoryPartition extends CompositeCursorAdapter.Partition {
public long directoryId;
public String directoryType;
public String displayName;
public String accountName;
public String accountType;
public boolean loading;
public CharSequence constraint;
public DirectoryPartitionFilter filter;
public DirectoryPartition() {
super(false, false);
}
}
private static class EmailQuery {
public static final String[] PROJECTION = {
Contacts.DISPLAY_NAME, // 0
Email.DATA // 1
};
public static final int NAME = 0;
public static final int ADDRESS = 1;
}
private static class DirectoryListQuery {
// TODO: revert to references to the Directory class as soon as the
// issue with the dependency on SDK 8 is resolved
public static final Uri URI =
Uri.withAppendedPath(ContactsContract.AUTHORITY_URI, "directories");
private static final String DIRECTORY_ID = "_id";
private static final String DIRECTORY_ACCOUNT_NAME = "accountName";
private static final String DIRECTORY_ACCOUNT_TYPE = "accountType";
private static final String DIRECTORY_DISPLAY_NAME = "displayName";
private static final String DIRECTORY_PACKAGE_NAME = "packageName";
private static final String DIRECTORY_TYPE_RESOURCE_ID = "typeResourceId";
public static final String[] PROJECTION = {
DIRECTORY_ID, // 0
DIRECTORY_ACCOUNT_NAME, // 1
DIRECTORY_ACCOUNT_TYPE, // 2
DIRECTORY_DISPLAY_NAME, // 3
DIRECTORY_PACKAGE_NAME, // 4
DIRECTORY_TYPE_RESOURCE_ID, // 5
};
public static final int ID = 0;
public static final int ACCOUNT_NAME = 1;
public static final int ACCOUNT_TYPE = 2;
public static final int DISPLAY_NAME = 3;
public static final int PACKAGE_NAME = 4;
public static final int TYPE_RESOURCE_ID = 5;
}
/**
* A fake column name that indicates a "Searching..." item in the list.
*/
private static final String SEARCHING_CURSOR_MARKER = "searching";
/**
* An asynchronous filter used for loading two data sets: email rows from the local
* contact provider and the list of {@link Directory}'s.
*/
private final class DefaultPartitionFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Cursor directoryCursor = null;
if (!mDirectoriesLoaded) {
directoryCursor = mContentResolver.query(
DirectoryListQuery.URI, DirectoryListQuery.PROJECTION, null, null, null);
mDirectoriesLoaded = true;
}
FilterResults results = new FilterResults();
Cursor cursor = null;
if (!TextUtils.isEmpty(constraint)) {
Uri.Builder builder = Email.CONTENT_FILTER_URI.buildUpon()
.appendPath(constraint.toString())
.appendQueryParameter(LIMIT_PARAM_KEY,
String.valueOf(mPreferredMaxResultCount));
if (mAccount != null) {
builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name);
builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type);
}
Uri uri = builder.build();
cursor = mContentResolver.query(uri, EmailQuery.PROJECTION, null, null, null);
results.count = cursor.getCount();
}
results.values = new Cursor[] { directoryCursor, cursor };
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.values != null) {
Cursor[] cursors = (Cursor[]) results.values;
onDirectoryLoadFinished(constraint, cursors[0], cursors[1]);
}
results.count = getCount();
}
@Override
public CharSequence convertResultToString(Object resultValue) {
return makeDisplayString((Cursor) resultValue);
}
}
/**
* An asynchronous filter that performs search in a particular directory.
*/
private final class DirectoryPartitionFilter extends Filter {
private final int mPartitionIndex;
private final long mDirectoryId;
private int mLimit;
public DirectoryPartitionFilter(int partitionIndex, long directoryId) {
this.mPartitionIndex = partitionIndex;
this.mDirectoryId = directoryId;
}
public synchronized void setLimit(int limit) {
this.mLimit = limit;
}
public synchronized int getLimit() {
return this.mLimit;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (!TextUtils.isEmpty(constraint)) {
Uri uri = Email.CONTENT_FILTER_URI.buildUpon()
.appendPath(constraint.toString())
.appendQueryParameter(DIRECTORY_PARAM_KEY, String.valueOf(mDirectoryId))
.appendQueryParameter(LIMIT_PARAM_KEY,
String.valueOf(getLimit() + ALLOWANCE_FOR_DUPLICATES))
.build();
Cursor cursor = mContentResolver.query(
uri, EmailQuery.PROJECTION, null, null, null);
results.values = cursor;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Cursor cursor = (Cursor) results.values;
onPartitionLoadFinished(constraint, mPartitionIndex, cursor);
results.count = getCount();
}
}
protected final ContentResolver mContentResolver;
private boolean mDirectoriesLoaded;
private Account mAccount;
private int mPreferredMaxResultCount;
private Handler mHandler;
public BaseEmailAddressAdapter(Context context) {
this(context, DEFAULT_PREFERRED_MAX_RESULT_COUNT);
}
public BaseEmailAddressAdapter(Context context, int preferredMaxResultCount) {
super(context);
mContentResolver = context.getContentResolver();
mPreferredMaxResultCount = preferredMaxResultCount;
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
showSearchPendingIfNotComplete(msg.arg1);
}
};
}
/**
* Set the account when known. Causes the search to prioritize contacts from
* that account.
*/
public void setAccount(Account account) {
mAccount = account;
}
/**
* Override to create a view for line item in the autocomplete suggestion list UI.
*/
protected abstract View inflateItemView(ViewGroup parent);
/**
* Override to populate the autocomplete suggestion line item UI with data.
*/
protected abstract void bindView(View view, String directoryType, String directoryName,
String displayName, String emailAddress);
/**
* Override to create a view for a "Searching directory" line item, which is
* displayed temporarily while the corresponding filter is running.
*/
protected abstract View inflateItemViewLoading(ViewGroup parent);
/**
* Override to populate the "Searching directory" line item UI with data.
*/
protected abstract void bindViewLoading(View view, String directoryType, String directoryName);
@Override
protected int getItemViewType(int partitionIndex, int position) {
DirectoryPartition partition = (DirectoryPartition)getPartition(partitionIndex);
return partition.loading ? 1 : 0;
}
@Override
protected View newView(Context context, int partitionIndex, Cursor cursor,
int position, ViewGroup parent) {
DirectoryPartition partition = (DirectoryPartition)getPartition(partitionIndex);
if (partition.loading) {
return inflateItemViewLoading(parent);
} else {
return inflateItemView(parent);
}
}
@Override
protected void bindView(View v, int partition, Cursor cursor, int position) {
DirectoryPartition directoryPartition = (DirectoryPartition)getPartition(partition);
String directoryType = directoryPartition.directoryType;
String directoryName = directoryPartition.displayName;
if (directoryPartition.loading) {
bindViewLoading(v, directoryType, directoryName);
} else {
String displayName = cursor.getString(EmailQuery.NAME);
String emailAddress = cursor.getString(EmailQuery.ADDRESS);
if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, emailAddress)) {
displayName = emailAddress;
emailAddress = null;
}
bindView(v, directoryType, directoryName, displayName, emailAddress);
}
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
protected boolean isEnabled(int partitionIndex, int position) {
// The "Searching..." item should not be selectable
return !isLoading(partitionIndex);
}
private boolean isLoading(int partitionIndex) {
return ((DirectoryPartition)getPartition(partitionIndex)).loading;
}
@Override
public Filter getFilter() {
return new DefaultPartitionFilter();
}
/**
* Handles the result of the initial call, which brings back the list of
* directories as well as the search results for the local directories.
*/
protected void onDirectoryLoadFinished(
CharSequence constraint, Cursor directoryCursor, Cursor defaultPartitionCursor) {
if (directoryCursor != null) {
PackageManager packageManager = getContext().getPackageManager();
DirectoryPartition preferredDirectory = null;
List<DirectoryPartition> directories = new ArrayList<DirectoryPartition>();
while (directoryCursor.moveToNext()) {
long id = directoryCursor.getLong(DirectoryListQuery.ID);
// Skip the local invisible directory, because the default directory
// already includes all local results.
if (id == DIRECTORY_LOCAL_INVISIBLE) {
continue;
}
DirectoryPartition partition = new DirectoryPartition();
partition.directoryId = id;
partition.displayName = directoryCursor.getString(DirectoryListQuery.DISPLAY_NAME);
partition.accountName = directoryCursor.getString(DirectoryListQuery.ACCOUNT_NAME);
partition.accountType = directoryCursor.getString(DirectoryListQuery.ACCOUNT_TYPE);
String packageName = directoryCursor.getString(DirectoryListQuery.PACKAGE_NAME);
int resourceId = directoryCursor.getInt(DirectoryListQuery.TYPE_RESOURCE_ID);
if (packageName != null && resourceId != 0) {
try {
Resources resources =
packageManager.getResourcesForApplication(packageName);
partition.directoryType = resources.getString(resourceId);
if (partition.directoryType == null) {
Log.e(TAG, "Cannot resolve directory name: "
+ resourceId + "@" + packageName);
}
} catch (NameNotFoundException e) {
Log.e(TAG, "Cannot resolve directory name: "
+ resourceId + "@" + packageName, e);
}
}
// If an account has been provided and we found a directory that
// corresponds to that account, place that directory second, directly
// underneath the local contacts.
if (mAccount != null && mAccount.name.equals(partition.accountName) &&
mAccount.type.equals(partition.accountType)) {
preferredDirectory = partition;
} else {
directories.add(partition);
}
}
if (preferredDirectory != null) {
directories.add(1, preferredDirectory);
}
for (DirectoryPartition partition : directories) {
addPartition(partition);
}
}
int count = getPartitionCount();
int limit = 0;
// Since we will be changing several partitions at once, hold the data change
// notifications
setNotificationsEnabled(false);
try {
// The filter has loaded results for the default partition too.
if (defaultPartitionCursor != null && getPartitionCount() > 0) {
changeCursor(0, defaultPartitionCursor);
}
int defaultPartitionCount = (defaultPartitionCursor == null ? 0
: defaultPartitionCursor.getCount());
limit = mPreferredMaxResultCount - defaultPartitionCount;
// Show non-default directories as "loading"
// Note: skipping the default partition (index 0), which has already been loaded
for (int i = 1; i < count; i++) {
DirectoryPartition partition = (DirectoryPartition) getPartition(i);
partition.constraint = constraint;
if (limit > 0) {
if (!partition.loading) {
partition.loading = true;
changeCursor(i, null);
}
} else {
partition.loading = false;
changeCursor(i, null);
}
}
} finally {
setNotificationsEnabled(true);
}
// Start search in other directories
// Note: skipping the default partition (index 0), which has already been loaded
for (int i = 1; i < count; i++) {
DirectoryPartition partition = (DirectoryPartition) getPartition(i);
if (partition.loading) {
mHandler.removeMessages(MESSAGE_SEARCH_PENDING, partition);
Message msg = mHandler.obtainMessage(MESSAGE_SEARCH_PENDING, i, 0, partition);
mHandler.sendMessageDelayed(msg, MESSAGE_SEARCH_PENDING_DELAY);
if (partition.filter == null) {
partition.filter = new DirectoryPartitionFilter(i, partition.directoryId);
}
partition.filter.setLimit(limit);
partition.filter.filter(constraint);
} else {
if (partition.filter != null) {
// Cancel any previous loading request
partition.filter.filter(null);
}
}
}
}
void showSearchPendingIfNotComplete(int partitionIndex) {
if (partitionIndex < getPartitionCount()) {
DirectoryPartition partition = (DirectoryPartition) getPartition(partitionIndex);
if (partition.loading) {
changeCursor(partitionIndex, createLoadingCursor());
}
}
}
/**
* Creates a dummy cursor to represent the "Searching directory..." item.
*/
private Cursor createLoadingCursor() {
MatrixCursor cursor = new MatrixCursor(new String[]{SEARCHING_CURSOR_MARKER});
cursor.addRow(new Object[]{""});
return cursor;
}
public void onPartitionLoadFinished(
CharSequence constraint, int partitionIndex, Cursor cursor) {
if (partitionIndex < getPartitionCount()) {
DirectoryPartition partition = (DirectoryPartition) getPartition(partitionIndex);
// Check if the received result matches the current constraint
// If not - the user must have continued typing after the request
// was issued
if (partition.loading && TextUtils.equals(constraint, partition.constraint)) {
partition.loading = false;
mHandler.removeMessages(MESSAGE_SEARCH_PENDING, partition);
changeCursor(partitionIndex, removeDuplicatesAndTruncate(partitionIndex, cursor));
} else {
// We got the result for an unexpected query (the user is still typing)
// Just ignore this result
if (cursor != null) {
cursor.close();
}
}
} else if (cursor != null) {
cursor.close();
}
}
/**
* Post-process the cursor to eliminate duplicates. Closes the original cursor
* and returns a new one.
*/
private Cursor removeDuplicatesAndTruncate(int partition, Cursor cursor) {
if (cursor == null) {
return null;
}
if (cursor.getCount() <= DEFAULT_PREFERRED_MAX_RESULT_COUNT
&& !hasDuplicates(cursor, partition)) {
return cursor;
}
int count = 0;
MatrixCursor newCursor = new MatrixCursor(EmailQuery.PROJECTION);
cursor.moveToPosition(-1);
while (cursor.moveToNext() && count < DEFAULT_PREFERRED_MAX_RESULT_COUNT) {
String displayName = cursor.getString(EmailQuery.NAME);
String emailAddress = cursor.getString(EmailQuery.ADDRESS);
if (!isDuplicate(emailAddress, partition)) {
newCursor.addRow(new Object[]{displayName, emailAddress});
count++;
}
}
cursor.close();
return newCursor;
}
private boolean hasDuplicates(Cursor cursor, int partition) {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
String emailAddress = cursor.getString(EmailQuery.ADDRESS);
if (isDuplicate(emailAddress, partition)) {
return true;
}
}
return false;
}
/**
* Checks if the supplied email address is already present in partitions other
* than the supplied one.
*/
private boolean isDuplicate(String emailAddress, int excludePartition) {
int partitionCount = getPartitionCount();
for (int partition = 0; partition < partitionCount; partition++) {
if (partition != excludePartition && !isLoading(partition)) {
Cursor cursor = getCursor(partition);
if (cursor != null) {
cursor.moveToPosition(-1);
while (cursor.moveToNext()) {
String address = cursor.getString(EmailQuery.ADDRESS);
if (TextUtils.equals(emailAddress, address)) {
return true;
}
}
}
}
}
return false;
}
private final String makeDisplayString(Cursor cursor) {
if (cursor.getColumnName(0).equals(SEARCHING_CURSOR_MARKER)) {
return "";
}
String displayName = cursor.getString(EmailQuery.NAME);
String emailAddress = cursor.getString(EmailQuery.ADDRESS);
if (TextUtils.isEmpty(displayName) || TextUtils.equals(displayName, emailAddress)) {
return emailAddress;
} else {
return new Rfc822Token(displayName, emailAddress, null).toString();
}
}
}