141
157
if( !_emails.containsKey( email ) )
142
158
_emails.put( email, new EmailData( email, type, isPreferred ) );
161
protected void addAddress( String address, int type )
163
if( _addresses == null ) _addresses =
164
new HashMap< String, AddressData >();
165
if( !_addresses.containsKey( address ) )
166
_addresses.put( address, new AddressData( address, type ) );
170
@SuppressWarnings("serial")
146
171
protected class AbortImportException extends Exception { };
148
173
public Importer( Doit doit )
291
322
_doit._handler.sendMessage( Message.obtain(
292
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
293
new Integer( maxProgress ) ) );
323
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
new Integer( maxProgress ) ) );
296
327
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
299
330
_doit._handler.sendMessage( Message.obtain(
300
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
301
new Integer( tmpProgress ) ) );
331
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
new Integer( tmpProgress ) ) );
304
335
protected void setProgress( int progress ) throws AbortImportException
307
338
_doit._handler.sendMessage( Message.obtain(
308
_doit._handler, Doit.MESSAGE_SETPROGRESS,
309
new Integer( progress ) ) );
339
_doit._handler, Doit.MESSAGE_SETPROGRESS,
340
new Integer( progress ) ) );
312
343
protected void finish( int action ) throws AbortImportException
349
379
case Doit.ACTION_KEEP:
350
380
// if we keep contacts on duplicate, we better check for one
351
return !_contacts.containsKey( name );
381
return !_contactsCache.exists( name );
353
383
case Doit.ACTION_PROMPT:
354
384
// if we are prompting on duplicate, we better check for one
355
if( !_contacts.containsKey( name ) )
385
if( !_contactsCache.exists( name ) )
358
388
// ok, it exists, so do prompt
359
389
_doit._handler.sendMessage( Message.obtain(
360
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
390
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
399
429
// does contact exist already?
400
430
Uri contactUri = null;
402
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
432
if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
404
434
// should we skip this import altogether?
405
435
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
407
437
// get contact's URI
408
438
contactUri = ContentUris.withAppendedId(
409
Contacts.People.CONTENT_URI, id );
439
Contacts.People.CONTENT_URI, id );
411
441
// should we destroy the existing contact before importing?
412
442
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
413
443
_doit.getContentResolver().delete( contactUri, null, null );
414
444
contactUri = null;
417
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
447
_doit._handler.sendEmptyMessage(
448
Doit.MESSAGE_CONTACTOVERWRITTEN );
418
449
uiInformed = true;
421
_contacts.remove( contact._name );
452
_contactsCache.remove( contact._name );
429
460
// create a new contact
430
461
values.put( Contacts.People.NAME, contact._name );
431
462
contactUri = _doit.getContentResolver().insert(
432
Contacts.People.CONTENT_URI, values );
463
Contacts.People.CONTENT_URI, values );
433
464
id = ContentUris.parseId( contactUri );
434
465
if( id <= 0 ) return; // shouldn't happen!
436
// add them to the "My Contacts" group
437
Contacts.People.addToGroup(
438
_doit.getContentResolver(), id,
439
Contacts.Groups.GROUP_MY_CONTACTS );
467
// try to add them to the "My Contacts" group
469
Contacts.People.addToMyContactsGroup(
470
_doit.getContentResolver(), id );
472
catch( IllegalStateException e ) {
473
// ignore any failure
442
_contacts.put( contact._name, id );
477
_contactsCache.put( id, contact._name );
445
480
if( !uiInformed ) {
490
526
values.put( Contacts.Phones.NUMBER, phone._number );
491
527
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
492
528
_doit.getContentResolver().insert( contactPhonesUri, values );
495
// now add those phone numbers to the cache to prevent the addition of
496
// duplicate data from another file
497
i = phonesKeys.iterator();
498
while( i.hasNext() ) {
499
ContactData.PhoneData phone = phones.get( i.next() );
501
String number = sanitisePhoneNumber( phone._number );
502
if( number != null ) {
503
HashSet< String > numbers = _contactNumbers.get( contactId );
504
if( numbers == null ) {
505
_contactNumbers.put( contactId, new HashSet< String >() );
506
numbers = _contactNumbers.get( contactId );
508
numbers.add( number );
530
// and add this address to the cache to prevent a addition of
531
// duplicate date from another file
532
_contactsCache.addNumber( contactId, number );
516
539
Long contactId = ContentUris.parseId( contactUri );
517
540
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
518
541
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
519
Set emailsKeys = emails.keySet();
542
Set< String > emailsKeys = emails.keySet();
521
544
// add email addresses
522
Iterator i = emailsKeys.iterator();
545
Iterator< String > i = emailsKeys.iterator();
523
546
while( i.hasNext() ) {
524
547
ContactData.EmailData email = emails.get( i.next() );
526
// like with phone numbers, we don't want to add this email address
527
// if it exists already or we would introduce duplicates.
549
// we don't want to add this email address if it exists already or
550
// we would introduce duplicates.
528
551
String address = sanitiseEmailAddress( email.getAddress() );
529
552
if( address == null ) continue;
530
HashSet< String > addresses = _contactEmails.get( contactId );
531
if( addresses != null && addresses.contains( address ) ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
533
555
// add phone number
534
556
ContentValues values = new ContentValues();
538
560
if( email.isPreferred() )
539
561
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
540
562
_doit.getContentResolver().insert( contactContactMethodsUri,
565
// and add this address to the cache to prevent a addition of
566
// duplicate date from another file
567
_contactsCache.addEmail( contactId, address );
544
// now add those email addresses to the cache to prevent the addition of
545
// duplicate data from another file
546
i = emailsKeys.iterator();
571
private void importContactAddresses( Uri contactUri,
572
HashMap< String, ContactData.AddressData > addresses )
574
Long contactId = ContentUris.parseId( contactUri );
575
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
576
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
577
Set< String > addressesKeys = addresses.keySet();
580
Iterator< String > i = addressesKeys.iterator();
547
581
while( i.hasNext() ) {
548
ContactData.EmailData email = emails.get( i.next() );
550
String address = sanitiseEmailAddress( email.getAddress() );
551
if( address != null ) {
552
HashSet< String > addresses = _contactEmails.get( contactId );
553
if( addresses == null ) {
554
_contactEmails.put( contactId, new HashSet< String >() );
555
addresses = _contactEmails.get( contactId );
557
addresses.add( address );
582
ContactData.AddressData address = addresses.get( i.next() );
584
// we don't want to add this address if it exists already or we
585
// would introduce duplicates
586
if( address == null ) continue;
587
if( _contactsCache.hasAddress( contactId, address.getAddress() ) )
590
// add postal address
591
ContentValues values = new ContentValues();
592
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
593
values.put( Contacts.ContactMethods.DATA, address.getAddress() );
594
values.put( Contacts.ContactMethods.TYPE, address.getType() );
595
_doit.getContentResolver().insert( contactContactMethodsUri,
598
// and add this address to the cache to prevent a addition of
599
// duplicate date from another file
600
_contactsCache.addAddress( contactId, address.getAddress() );
570
private void buildContactsCache() throws AbortImportException
573
setProgressMessage( R.string.doit_caching );
578
// init contacts caches
579
_contacts = new HashMap< String, Long >();
580
_contactNumbers = new HashMap< Long, HashSet< String > >();
581
_contactEmails = new HashMap< Long, HashSet< String > >();
583
// query and store map of contact names to ids
584
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
585
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
586
cols, null, null, null);
587
if( cur.moveToFirst() ) {
588
int idCol = cur.getColumnIndex( Contacts.People._ID );
589
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
591
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
592
} while( cur.moveToNext() );
595
// query and store map of contact ids to sets of phone numbers
596
cols = new String[] { Contacts.Phones.PERSON_ID,
597
Contacts.Phones.NUMBER };
598
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
599
cols, null, null, null);
600
if( cur.moveToFirst() ) {
601
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
602
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
604
Long id = cur.getLong( personIdCol );
605
String number = sanitisePhoneNumber(
606
cur.getString( numberCol ) );
607
if( number != null ) {
608
HashSet< String > numbers = _contactNumbers.get( id );
609
if( numbers == null ) {
610
_contactNumbers.put( id, new HashSet< String >() );
611
numbers = _contactNumbers.get( id );
613
numbers.add( number );
615
} while( cur.moveToNext() );
618
// query and store map of contact ids to sets of email addresses
619
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
620
Contacts.ContactMethods.DATA };
621
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
622
cols, Contacts.ContactMethods.KIND + " = ?",
623
new String[] { "" + Contacts.KIND_EMAIL }, null );
624
if( cur.moveToFirst() ) {
625
int personIdCol = cur.getColumnIndex(
626
Contacts.ContactMethods.PERSON_ID );
627
int addressCol = cur.getColumnIndex(
628
Contacts.ContactMethods.DATA );
630
Long id = cur.getLong( personIdCol );
631
String address = sanitiseEmailAddress(
632
cur.getString( addressCol ) );
633
if( address != null ) {
634
HashSet< String > addresses = _contactEmails.get( id );
635
if( addresses == null ) {
636
_contactEmails.put( id, new HashSet< String >() );
637
addresses = _contactEmails.get( id );
639
addresses.add( address );
641
} while( cur.moveToNext() );
645
private String sanitisePhoneNumber( String number )
612
static public String sanitisePhoneNumber( String number )
647
614
number = number.replaceAll( "[-\\(\\) ]", "" );
648
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
615
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
649
616
Matcher m = p.matcher( number );
650
617
if( m.lookingAt() ) return m.group( 0 );
654
private String sanitiseEmailAddress( String address )
621
static public String sanitiseEmailAddress( String address )
656
623
address = address.trim();
657
624
Pattern p = Pattern.compile(
658
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
625
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
659
626
Matcher m = p.matcher( address );
660
627
if( m.matches() ) {
661
628
String[] bits = address.split( "@" );