56
55
private HashMap< String, Long > _contacts;
57
56
private HashMap< Long, HashSet< String > > _contactNumbers;
58
57
private HashMap< Long, HashSet< String > > _contactEmails;
59
private HashMap< Long, HashSet< String > > _contactAddresses;
60
58
private int _mergeSetting;
61
59
private int _lastMergeDecision;
62
60
private boolean _abort = false;
119
private String _address;
122
public AddressData( String address, int type ) {
127
public String getAddress() {
131
public int getType() {
136
115
public String _name = null;
137
116
public HashMap< String, PhoneData > _phones = null;
138
117
public HashMap< String, EmailData > _emails = null;
139
public HashMap< String, AddressData > _addresses = null;
141
119
protected void setName( String name )
162
140
if( !_emails.containsKey( email ) )
163
141
_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 ) );
175
145
@SuppressWarnings("serial")
493
463
importContactPhones( contactUri, contact._phones );
494
464
if( contact._emails != null )
495
465
importContactEmails( contactUri, contact._emails );
496
if( contact._addresses != null )
497
importContactAddresses( contactUri, contact._addresses );
500
468
private void importContactPhones( Uri contactUri,
519
487
// anyway, so it's not a problem).
520
488
String number = sanitisePhoneNumber( phone._number );
521
489
if( number == null ) continue;
522
HashSet< String > cache = _contactNumbers.get( contactId );
523
if( cache != null && cache.contains( number ) ) continue;
490
HashSet< String > numbers = _contactNumbers.get( contactId );
491
if( numbers != null && numbers.contains( number ) ) continue;
525
493
// add phone number
526
494
ContentValues values = new ContentValues();
528
496
values.put( Contacts.Phones.NUMBER, phone._number );
529
497
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
530
498
_doit.getContentResolver().insert( contactPhonesUri, values );
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 );
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 );
552
529
while( i.hasNext() ) {
553
530
ContactData.EmailData email = emails.get( i.next() );
555
// we don't want to add this email address if it exists already or
556
// we would introduce duplicates.
532
// like with phone numbers, we don't want to add this email address
533
// if it exists already or we would introduce duplicates.
557
534
String address = sanitiseEmailAddress( email.getAddress() );
558
535
if( address == null ) continue;
559
HashSet< String > cache = _contactEmails.get( contactId );
560
if( cache != null && cache.contains( address ) ) continue;
536
HashSet< String > addresses = _contactEmails.get( contactId );
537
if( addresses != null && addresses.contains( address ) ) continue;
562
539
// add phone number
563
540
ContentValues values = new ContentValues();
568
545
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
569
546
_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 );
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();
550
// now add those email addresses to the cache to prevent the addition of
551
// duplicate data from another file
552
i = emailsKeys.iterator();
592
553
while( i.hasNext() ) {
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 );
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 );
616
cache.add( address.getAddress() );
637
585
_contacts = new HashMap< String, Long >();
638
586
_contactNumbers = new HashMap< Long, HashSet< String > >();
639
587
_contactEmails = new HashMap< Long, HashSet< String > >();
640
_contactAddresses = new HashMap< Long, HashSet< String > >();
642
589
// query and store map of contact names to ids
643
590
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
666
613
if( number != null ) {
667
614
HashSet< String > numbers = _contactNumbers.get( id );
668
615
if( numbers == null ) {
669
numbers = new HashSet< String >();
670
_contactNumbers.put( id, numbers );
616
_contactNumbers.put( id, new HashSet< String >() );
617
numbers = _contactNumbers.get( id );
672
619
numbers.add( number );
692
639
if( address != null ) {
693
640
HashSet< String > addresses = _contactEmails.get( id );
694
641
if( addresses == null ) {
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 );
642
_contactEmails.put( id, new HashSet< String >() );
643
addresses = _contactEmails.get( id );
723
645
addresses.add( address );