55
56
private HashMap< String, Long > _contacts;
56
57
private HashMap< Long, HashSet< String > > _contactNumbers;
57
58
private HashMap< Long, HashSet< String > > _contactEmails;
59
private HashMap< Long, HashSet< String > > _contactAddresses;
58
60
private int _mergeSetting;
59
61
private int _lastMergeDecision;
60
62
private boolean _abort = false;
119
private String _address;
122
public AddressData( String address, int type ) {
127
public String getAddress() {
131
public int getType() {
115
136
public String _name = null;
116
137
public HashMap< String, PhoneData > _phones = null;
117
138
public HashMap< String, EmailData > _emails = null;
139
public HashMap< String, AddressData > _addresses = null;
119
141
protected void setName( String name )
140
162
if( !_emails.containsKey( email ) )
141
163
_emails.put( email, new EmailData( email, type, isPreferred ) );
166
protected void addAddress( String address, int type )
168
if( _addresses == null ) _addresses =
169
new HashMap< String, AddressData >();
170
if( !_addresses.containsKey( address ) )
171
_addresses.put( address, new AddressData( address, type ) );
145
175
@SuppressWarnings("serial")
463
493
importContactPhones( contactUri, contact._phones );
464
494
if( contact._emails != null )
465
495
importContactEmails( contactUri, contact._emails );
496
if( contact._addresses != null )
497
importContactAddresses( contactUri, contact._addresses );
468
500
private void importContactPhones( Uri contactUri,
487
519
// anyway, so it's not a problem).
488
520
String number = sanitisePhoneNumber( phone._number );
489
521
if( number == null ) continue;
490
HashSet< String > numbers = _contactNumbers.get( contactId );
491
if( numbers != null && numbers.contains( number ) ) continue;
522
HashSet< String > cache = _contactNumbers.get( contactId );
523
if( cache != null && cache.contains( number ) ) continue;
493
525
// add phone number
494
526
ContentValues values = new ContentValues();
496
528
values.put( Contacts.Phones.NUMBER, phone._number );
497
529
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
498
530
_doit.getContentResolver().insert( contactPhonesUri, values );
501
// now add those phone numbers to the cache to prevent the addition of
502
// duplicate data from another file
503
i = phonesKeys.iterator();
504
while( i.hasNext() ) {
505
ContactData.PhoneData phone = phones.get( i.next() );
507
String number = sanitisePhoneNumber( phone._number );
508
if( number != null ) {
509
HashSet< String > numbers = _contactNumbers.get( contactId );
510
if( numbers == null ) {
511
_contactNumbers.put( contactId, new HashSet< String >() );
512
numbers = _contactNumbers.get( contactId );
514
numbers.add( number );
532
// and add this address to the cache to prevent a addition of
533
// duplicate date from another file
534
if( cache == null ) {
535
cache = new HashSet< String >();
536
_contactNumbers.put( contactId, cache );
529
552
while( i.hasNext() ) {
530
553
ContactData.EmailData email = emails.get( i.next() );
532
// like with phone numbers, we don't want to add this email address
533
// if it exists already or we would introduce duplicates.
555
// we don't want to add this email address if it exists already or
556
// we would introduce duplicates.
534
557
String address = sanitiseEmailAddress( email.getAddress() );
535
558
if( address == null ) continue;
536
HashSet< String > addresses = _contactEmails.get( contactId );
537
if( addresses != null && addresses.contains( address ) ) continue;
559
HashSet< String > cache = _contactEmails.get( contactId );
560
if( cache != null && cache.contains( address ) ) continue;
539
562
// add phone number
540
563
ContentValues values = new ContentValues();
545
568
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
546
569
_doit.getContentResolver().insert( contactContactMethodsUri,
572
// and add this address to the cache to prevent a addition of
573
// duplicate date from another file
574
if( cache == null ) {
575
cache = new HashSet< String >();
576
_contactEmails.put( contactId, cache );
578
cache.add( address );
550
// now add those email addresses to the cache to prevent the addition of
551
// duplicate data from another file
552
i = emailsKeys.iterator();
582
private void importContactAddresses( Uri contactUri,
583
HashMap< String, ContactData.AddressData > addresses )
585
Long contactId = ContentUris.parseId( contactUri );
586
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
587
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
588
Set< String > addressesKeys = addresses.keySet();
591
Iterator< String > i = addressesKeys.iterator();
553
592
while( i.hasNext() ) {
554
ContactData.EmailData email = emails.get( i.next() );
556
String address = sanitiseEmailAddress( email.getAddress() );
557
if( address != null ) {
558
HashSet< String > addresses = _contactEmails.get( contactId );
559
if( addresses == null ) {
560
_contactEmails.put( contactId, new HashSet< String >() );
561
addresses = _contactEmails.get( contactId );
563
addresses.add( address );
593
ContactData.AddressData address = addresses.get( i.next() );
595
// we don't want to add this address if it exists already or we
596
// would introduce duplicates
597
if( address == null ) continue;
598
HashSet< String > cache = _contactAddresses.get( contactId );
599
if( cache != null && cache.contains( address.getAddress() ) )
602
// add postal address
603
ContentValues values = new ContentValues();
604
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
605
values.put( Contacts.ContactMethods.DATA, address.getAddress() );
606
values.put( Contacts.ContactMethods.TYPE, address.getType() );
607
_doit.getContentResolver().insert( contactContactMethodsUri,
610
// and add this address to the cache to prevent a addition of
611
// duplicate date from another file
612
if( cache == null ) {
613
cache = new HashSet< String >();
614
_contactAddresses.put( contactId, cache );
616
cache.add( address.getAddress() );
585
637
_contacts = new HashMap< String, Long >();
586
638
_contactNumbers = new HashMap< Long, HashSet< String > >();
587
639
_contactEmails = new HashMap< Long, HashSet< String > >();
640
_contactAddresses = new HashMap< Long, HashSet< String > >();
589
642
// query and store map of contact names to ids
590
643
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
613
666
if( number != null ) {
614
667
HashSet< String > numbers = _contactNumbers.get( id );
615
668
if( numbers == null ) {
616
_contactNumbers.put( id, new HashSet< String >() );
617
numbers = _contactNumbers.get( id );
669
numbers = new HashSet< String >();
670
_contactNumbers.put( id, numbers );
619
672
numbers.add( number );
639
692
if( address != null ) {
640
693
HashSet< String > addresses = _contactEmails.get( id );
641
694
if( addresses == null ) {
642
_contactEmails.put( id, new HashSet< String >() );
643
addresses = _contactEmails.get( id );
695
addresses = new HashSet< String >();
696
_contactEmails.put( id, addresses );
698
addresses.add( address );
700
} while( cur.moveToNext() );
703
// query and store map of contact ids to sets of postal addresses
704
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
705
Contacts.ContactMethods.DATA };
706
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
707
cols, Contacts.ContactMethods.KIND + " = ?",
708
new String[] { "" + Contacts.KIND_POSTAL }, null );
709
if( cur.moveToFirst() ) {
710
int personIdCol = cur.getColumnIndex(
711
Contacts.ContactMethods.PERSON_ID );
712
int addressCol = cur.getColumnIndex(
713
Contacts.ContactMethods.DATA );
715
Long id = cur.getLong( personIdCol );
716
String address = cur.getString( addressCol );
717
if( address != null ) {
718
HashSet< String > addresses = _contactAddresses.get( id );
719
if( addresses == null ) {
720
addresses = new HashSet< String >();
721
_contactAddresses.put( id, addresses );
645
723
addresses.add( address );