1
package org.waxworlds.importcontacts;
3
import java.util.HashMap;
4
import java.util.HashSet;
5
import java.util.Iterator;
7
import java.util.regex.Matcher;
8
import java.util.regex.Pattern;
10
import android.content.ContentUris;
11
import android.content.ContentValues;
12
import android.content.SharedPreferences;
13
import android.database.Cursor;
14
import android.net.Uri;
15
import android.os.Message;
16
import android.provider.Contacts;
18
public class Importer extends Thread
20
public final static int MESSAGE_FINISHED = 0;
21
public final static int MESSAGE_FINISHED_BACK = 1;
22
public final static int MESSAGE_ERROR = 2;
23
public final static int MESSAGE_CONTINUEORABORT = 3;
24
public final static int MESSAGE_SETPROGRESSMESSAGE = 4;
25
public final static int MESSAGE_SETMAXPROGRESS = 5;
26
public final static int MESSAGE_SETTMPPROGRESS = 6;
27
public final static int MESSAGE_SETPROGRESS = 7;
28
public final static int MESSAGE_MERGEPROMPT = 8;
29
public final static int MESSAGE_CONTACTOVERWRITTEN = 9;
30
public final static int MESSAGE_CONTACTCREATED = 10;
31
public final static int MESSAGE_CONTACTMERGED = 11;
32
public final static int MESSAGE_CONTACTSKIPPED = 12;
34
public final static int RESPONSE_NEGATIVE = 0;
35
public final static int RESPONSE_POSITIVE = 1;
37
public final static int RESPONSEEXTRA_NONE = 0;
38
public final static int RESPONSEEXTRA_ALWAYS = 1;
41
private int _response;
42
private int _responseExtra;
43
private HashMap< String, Long > _contacts;
44
private HashMap< Long, HashSet< String > > _contactNumbers;
45
private HashMap< Long, HashSet< String > > _contactEmails;
46
private int _mergeSetting;
47
private int _lastMergeDecision;
48
private boolean _abort = false;
50
public class ContactData
54
public String _number;
56
public boolean _isPreferred;
58
public PhoneData( String number, int type, boolean isPreferred ) {
61
_isPreferred = isPreferred;
64
public String getNumber() {
68
public int getType() {
72
public boolean isPreferred() {
79
private String _email;
81
private boolean _isPreferred;
83
public EmailData( String email, int type, boolean isPreferred ) {
86
_isPreferred = isPreferred;
89
public String getAddress() {
93
public int getType() {
97
public boolean isPreferred() {
102
public String _name = null;
103
public HashMap< String, PhoneData > _phones = null;
104
public HashMap< String, EmailData > _emails = null;
106
protected void setName( String name )
111
public String getName()
116
protected void addPhone( String number, int type, boolean isPreferred )
118
if( _phones == null ) _phones = new HashMap< String, PhoneData >();
119
if( !_phones.containsKey( number ) )
121
new PhoneData( number, type, isPreferred ) );
124
protected void addEmail( String email, int type, boolean isPreferred )
126
if( _emails == null ) _emails = new HashMap< String, EmailData >();
127
if( !_emails.containsKey( email ) )
128
_emails.put( email, new EmailData( email, type, isPreferred ) );
132
protected class AbortImportException extends Exception { };
134
public Importer( Doit doit )
138
SharedPreferences prefs = getSharedPreferences();
139
_mergeSetting = prefs.getInt( "merge_setting", 0 );
147
// cache current contact names
148
buildContactsCache();
156
catch( AbortImportException e )
160
protected void onImport() throws AbortImportException
166
wake( 0, RESPONSEEXTRA_NONE );
169
public void wake( int response )
171
wake( response, RESPONSEEXTRA_NONE );
174
synchronized public void wake( int response, int responseExtra )
176
_response = response;
177
_responseExtra = responseExtra;
181
synchronized public void setAbort()
186
protected SharedPreferences getSharedPreferences()
188
return _doit.getSharedPreferences();
191
protected void showError( int res ) throws AbortImportException
193
showError( _doit.getText( res ).toString() );
196
synchronized protected void showError( String message )
197
throws AbortImportException
200
_doit._handler.sendMessage( Message.obtain(
201
_doit._handler, MESSAGE_ERROR, message ) );
205
catch( InterruptedException e ) { }
209
protected void showFatalError( int res ) throws AbortImportException
211
showFatalError( _doit.getText( res ).toString() );
214
synchronized protected void showFatalError( String message )
215
throws AbortImportException
218
_doit._handler.sendMessage( Message.obtain(
219
_doit._handler, MESSAGE_ERROR, message ) );
223
catch( InterruptedException e ) { }
227
protected boolean showContinue( int res ) throws AbortImportException
229
return showContinue( _doit.getText( res ).toString() );
232
synchronized protected boolean showContinue( String message )
233
throws AbortImportException
236
_doit._handler.sendMessage( Message.obtain(
237
_doit._handler, MESSAGE_CONTINUEORABORT, message ) );
241
catch( InterruptedException e ) { }
242
return _response == RESPONSE_POSITIVE;
245
protected void setProgressMessage( int res ) throws AbortImportException
248
_doit._handler.sendMessage( Message.obtain( _doit._handler,
249
MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
252
protected void setProgressMax( int maxProgress )
253
throws AbortImportException
256
_doit._handler.sendMessage( Message.obtain(
257
_doit._handler, MESSAGE_SETMAXPROGRESS,
258
new Integer( maxProgress ) ) );
261
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
264
_doit._handler.sendMessage( Message.obtain(
265
_doit._handler, MESSAGE_SETTMPPROGRESS,
266
new Integer( tmpProgress ) ) );
269
protected void setProgress( int progress ) throws AbortImportException
272
_doit._handler.sendMessage( Message.obtain(
273
_doit._handler, MESSAGE_SETPROGRESS,
274
new Integer( progress ) ) );
277
protected void finish() throws AbortImportException
282
protected void abort() throws AbortImportException
287
protected CharSequence getText( int res )
289
return _doit.getText( res );
292
protected boolean isImportRequired( String name )
293
throws AbortImportException
296
return isImportRequired( name, _mergeSetting );
299
synchronized private boolean isImportRequired( String name, int mergeSetting )
301
_lastMergeDecision = mergeSetting;
303
// handle special cases
304
switch( mergeSetting )
306
case R.id.merge_keep:
307
// if we keep contacts on duplicate, we better check for one
308
return !_contacts.containsKey( name );
310
case R.id.merge_prompt:
311
// if we are prompting on duplicate, we better check for one
312
if( !_contacts.containsKey( name ) )
315
// ok, it exists, so do prompt
316
_doit._handler.sendMessage( Message.obtain(
317
_doit._handler, MESSAGE_MERGEPROMPT, name ) );
321
catch( InterruptedException e ) { }
323
// if "always" was selected, make choice permenant
324
if( _responseExtra == RESPONSEEXTRA_ALWAYS )
325
_mergeSetting = _response;
327
// recurse, with out new merge setting
328
return isImportRequired( name, _response );
331
// for all other cases (either overwriting or merging) we will need the
336
protected void skipContact() throws AbortImportException
339
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
342
protected void importContact( ContactData contact )
343
throws AbortImportException
347
if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
350
ContentValues values = new ContentValues();
351
boolean uiInformed = false;
353
// does contact exist already?
354
Uri contactUri = null;
356
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
358
// should we skip this import altogether?
359
if( _lastMergeDecision == R.id.merge_keep ) return;
362
contactUri = ContentUris.withAppendedId(
363
Contacts.People.CONTENT_URI, id );
365
// should we destroy the existing contact before importing?
366
if( _lastMergeDecision == R.id.merge_overwrite ) {
367
_doit.getContentResolver().delete( contactUri, null, null );
371
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
375
_contacts.remove( contact._name );
379
// if we don't have a contact URI it is because the contact never
380
// existed or because we deleted it
381
if( contactUri == null )
383
// create a new contact
384
values.put( Contacts.People.NAME, contact._name );
385
contactUri = _doit.getContentResolver().insert(
386
Contacts.People.CONTENT_URI, values );
387
id = ContentUris.parseId( contactUri );
388
if( id <= 0 ) return; // shouldn't happen!
390
// add them to the "My Contacts" group
391
Contacts.People.addToGroup(
392
_doit.getContentResolver(), id,
393
Contacts.Groups.GROUP_MY_CONTACTS );
396
_contacts.put( contact._name, id );
400
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
407
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
409
// import contact parts
410
if( contact._phones != null )
411
importContactPhones( contactUri, contact._phones );
412
if( contact._emails != null )
413
importContactEmails( contactUri, contact._emails );
416
private void importContactPhones( Uri contactUri,
417
HashMap< String, ContactData.PhoneData > phones )
419
Long contactId = ContentUris.parseId( contactUri );
420
Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
421
Contacts.People.Phones.CONTENT_DIRECTORY );
424
Set phonesKeys = phones.keySet();
425
Iterator i = phonesKeys.iterator();
426
while( i.hasNext() ) {
427
ContactData.PhoneData phone = phones.get( i.next() );
429
// we don't want to add this number if it's crap, or it already
430
// exists (which would cause a duplicate to be created). We don't
431
// take in to account the type when checking for duplicates. This is
432
// intentional: types aren't really very reliable. We assume that
433
// if the number exists at all, it doesn't need importing. Because
434
// of this, we also can't update the cache (which we don't need to
435
// anyway, so it's not a problem).
436
String number = sanitisePhoneNumber( phone._number );
437
if( number == null ) continue;
438
HashSet< String > numbers = _contactNumbers.get( contactId );
439
if( numbers != null && numbers.contains( number ) ) continue;
442
ContentValues values = new ContentValues();
443
values.put( Contacts.Phones.TYPE, phone._type );
444
values.put( Contacts.Phones.NUMBER, phone._number );
445
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
446
_doit.getContentResolver().insert( contactPhonesUri, values );
450
private void importContactEmails( Uri contactUri,
451
HashMap< String, ContactData.EmailData > emails )
453
Long contactId = ContentUris.parseId( contactUri );
454
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
455
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
458
Set emailsKeys = emails.keySet();
459
Iterator i = emailsKeys.iterator();
460
while( i.hasNext() ) {
461
ContactData.EmailData email = emails.get( i.next() );
463
// like with phone numbers, we don't want to add this email address
464
// if it exists already or we would introduce duplicates.
465
String address = sanitiseEmailAddress( email.getAddress() );
466
if( address == null ) continue;
467
HashSet< String > addresses = _contactEmails.get( contactId );
468
if( addresses != null && addresses.contains( address ) ) continue;
471
ContentValues values = new ContentValues();
472
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
473
values.put( Contacts.ContactMethods.DATA, email.getAddress() );
474
values.put( Contacts.ContactMethods.TYPE, email.getType() );
475
if( email.isPreferred() )
476
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
477
_doit.getContentResolver().insert( contactContactMethodsUri,
482
synchronized private void finish( boolean offerBack )
483
throws AbortImportException
485
// notify Doit that we're finished
486
_doit._handler.sendEmptyMessage(
487
offerBack? MESSAGE_FINISHED_BACK : MESSAGE_FINISHED );
490
throw new AbortImportException();
493
synchronized private void checkAbort() throws AbortImportException
497
throw new AbortImportException();
501
private void buildContactsCache() throws AbortImportException
504
setProgressMessage( R.string.doit_caching );
509
// init contacts caches
510
_contacts = new HashMap< String, Long >();
511
_contactNumbers = new HashMap< Long, HashSet< String > >();
512
_contactEmails = new HashMap< Long, HashSet< String > >();
514
// query and store map of contact names to ids
515
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
516
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
517
cols, null, null, null);
518
if( cur.moveToFirst() ) {
519
int idCol = cur.getColumnIndex( Contacts.People._ID );
520
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
522
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
523
} while( cur.moveToNext() );
526
// query and store map of contact ids to sets of phone numbers
527
cols = new String[] { Contacts.Phones.PERSON_ID,
528
Contacts.Phones.NUMBER };
529
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
530
cols, null, null, null);
531
if( cur.moveToFirst() ) {
532
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
533
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
535
Long id = cur.getLong( personIdCol );
536
String number = sanitisePhoneNumber(
537
cur.getString( numberCol ) );
538
if( number != null ) {
539
HashSet< String > numbers = _contactNumbers.get( id );
540
if( numbers == null ) {
541
_contactNumbers.put( id, new HashSet< String >() );
542
numbers = _contactNumbers.get( id );
544
numbers.add( number );
546
} while( cur.moveToNext() );
549
// query and store map of contact ids to sets of email addresses
550
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
551
Contacts.ContactMethods.DATA };
552
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
553
cols, Contacts.ContactMethods.KIND + " = ?",
554
new String[] { "" + Contacts.KIND_EMAIL }, null );
555
if( cur.moveToFirst() ) {
556
int personIdCol = cur.getColumnIndex(
557
Contacts.ContactMethods.PERSON_ID );
558
int addressCol = cur.getColumnIndex(
559
Contacts.ContactMethods.DATA );
561
Long id = cur.getLong( personIdCol );
562
String address = sanitiseEmailAddress(
563
cur.getString( addressCol ) );
564
if( address != null ) {
565
HashSet< String > addresses = _contactEmails.get( id );
566
if( addresses == null ) {
567
_contactEmails.put( id, new HashSet< String >() );
568
addresses = _contactEmails.get( id );
570
addresses.add( address );
572
} while( cur.moveToNext() );
576
private String sanitisePhoneNumber( String number )
578
number = number.replaceAll( "[-\\(\\) ]", "" );
579
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
580
Matcher m = p.matcher( number );
581
if( m.lookingAt() ) return m.group( 0 );
585
private String sanitiseEmailAddress( String address )
587
address = address.trim();
588
Pattern p = Pattern.compile(
589
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
590
Matcher m = p.matcher( address );
592
String[] bits = address.split( "@" );
593
return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();