140
157
if( !_emails.containsKey( email ) )
141
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 ) );
145
170
@SuppressWarnings("serial")
293
322
_doit._handler.sendMessage( Message.obtain(
294
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
295
new Integer( maxProgress ) ) );
323
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
new Integer( maxProgress ) ) );
298
327
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
301
330
_doit._handler.sendMessage( Message.obtain(
302
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
303
new Integer( tmpProgress ) ) );
331
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
new Integer( tmpProgress ) ) );
306
335
protected void setProgress( int progress ) throws AbortImportException
309
338
_doit._handler.sendMessage( Message.obtain(
310
_doit._handler, Doit.MESSAGE_SETPROGRESS,
311
new Integer( progress ) ) );
339
_doit._handler, Doit.MESSAGE_SETPROGRESS,
340
new Integer( progress ) ) );
314
343
protected void finish( int action ) throws AbortImportException
350
379
case Doit.ACTION_KEEP:
351
380
// if we keep contacts on duplicate, we better check for one
352
return !_contacts.containsKey( name );
381
return !_contactsCache.exists( name );
354
383
case Doit.ACTION_PROMPT:
355
384
// if we are prompting on duplicate, we better check for one
356
if( !_contacts.containsKey( name ) )
385
if( !_contactsCache.exists( name ) )
359
388
// ok, it exists, so do prompt
360
389
_doit._handler.sendMessage( Message.obtain(
361
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
390
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
400
429
// does contact exist already?
401
430
Uri contactUri = null;
403
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
432
if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
405
434
// should we skip this import altogether?
406
435
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
408
437
// get contact's URI
409
438
contactUri = ContentUris.withAppendedId(
410
Contacts.People.CONTENT_URI, id );
439
Contacts.People.CONTENT_URI, id );
412
441
// should we destroy the existing contact before importing?
413
442
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
414
443
_doit.getContentResolver().delete( contactUri, null, null );
415
444
contactUri = null;
418
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
447
_doit._handler.sendEmptyMessage(
448
Doit.MESSAGE_CONTACTOVERWRITTEN );
419
449
uiInformed = true;
422
_contacts.remove( contact._name );
452
_contactsCache.remove( contact._name );
493
526
values.put( Contacts.Phones.NUMBER, phone._number );
494
527
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
495
528
_doit.getContentResolver().insert( contactPhonesUri, values );
498
// now add those phone numbers to the cache to prevent the addition of
499
// duplicate data from another file
500
i = phonesKeys.iterator();
501
while( i.hasNext() ) {
502
ContactData.PhoneData phone = phones.get( i.next() );
504
String number = sanitisePhoneNumber( phone._number );
505
if( number != null ) {
506
HashSet< String > numbers = _contactNumbers.get( contactId );
507
if( numbers == null ) {
508
_contactNumbers.put( contactId, new HashSet< String >() );
509
numbers = _contactNumbers.get( contactId );
511
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 );
526
546
while( i.hasNext() ) {
527
547
ContactData.EmailData email = emails.get( i.next() );
529
// like with phone numbers, we don't want to add this email address
530
// 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.
531
551
String address = sanitiseEmailAddress( email.getAddress() );
532
552
if( address == null ) continue;
533
HashSet< String > addresses = _contactEmails.get( contactId );
534
if( addresses != null && addresses.contains( address ) ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
536
555
// add phone number
537
556
ContentValues values = new ContentValues();
541
560
if( email.isPreferred() )
542
561
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
543
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 );
547
// now add those email addresses to the cache to prevent the addition of
548
// duplicate data from another file
549
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();
550
581
while( i.hasNext() ) {
551
ContactData.EmailData email = emails.get( i.next() );
553
String address = sanitiseEmailAddress( email.getAddress() );
554
if( address != null ) {
555
HashSet< String > addresses = _contactEmails.get( contactId );
556
if( addresses == null ) {
557
_contactEmails.put( contactId, new HashSet< String >() );
558
addresses = _contactEmails.get( contactId );
560
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() );
573
private void buildContactsCache() throws AbortImportException
576
setProgressMessage( R.string.doit_caching );
581
// init contacts caches
582
_contacts = new HashMap< String, Long >();
583
_contactNumbers = new HashMap< Long, HashSet< String > >();
584
_contactEmails = new HashMap< Long, HashSet< String > >();
586
// query and store map of contact names to ids
587
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
588
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
589
cols, null, null, null);
590
if( cur.moveToFirst() ) {
591
int idCol = cur.getColumnIndex( Contacts.People._ID );
592
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
594
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
595
} while( cur.moveToNext() );
598
// query and store map of contact ids to sets of phone numbers
599
cols = new String[] { Contacts.Phones.PERSON_ID,
600
Contacts.Phones.NUMBER };
601
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
602
cols, null, null, null);
603
if( cur.moveToFirst() ) {
604
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
605
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
607
Long id = cur.getLong( personIdCol );
608
String number = sanitisePhoneNumber(
609
cur.getString( numberCol ) );
610
if( number != null ) {
611
HashSet< String > numbers = _contactNumbers.get( id );
612
if( numbers == null ) {
613
_contactNumbers.put( id, new HashSet< String >() );
614
numbers = _contactNumbers.get( id );
616
numbers.add( number );
618
} while( cur.moveToNext() );
621
// query and store map of contact ids to sets of email addresses
622
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
623
Contacts.ContactMethods.DATA };
624
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
625
cols, Contacts.ContactMethods.KIND + " = ?",
626
new String[] { "" + Contacts.KIND_EMAIL }, null );
627
if( cur.moveToFirst() ) {
628
int personIdCol = cur.getColumnIndex(
629
Contacts.ContactMethods.PERSON_ID );
630
int addressCol = cur.getColumnIndex(
631
Contacts.ContactMethods.DATA );
633
Long id = cur.getLong( personIdCol );
634
String address = sanitiseEmailAddress(
635
cur.getString( addressCol ) );
636
if( address != null ) {
637
HashSet< String > addresses = _contactEmails.get( id );
638
if( addresses == null ) {
639
_contactEmails.put( id, new HashSet< String >() );
640
addresses = _contactEmails.get( id );
642
addresses.add( address );
644
} while( cur.moveToNext() );
648
private String sanitisePhoneNumber( String number )
612
static public String sanitisePhoneNumber( String number )
650
614
number = number.replaceAll( "[-\\(\\) ]", "" );
651
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
615
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
652
616
Matcher m = p.matcher( number );
653
617
if( m.lookingAt() ) return m.group( 0 );
657
private String sanitiseEmailAddress( String address )
621
static public String sanitiseEmailAddress( String address )
659
623
address = address.trim();
660
624
Pattern p = Pattern.compile(
661
"^[^ @]+@[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])?)+$" );
662
626
Matcher m = p.matcher( address );
663
627
if( m.matches() ) {
664
628
String[] bits = address.split( "@" );