51
52
private Doit _doit;
52
53
private int _response;
53
54
private int _responseExtra;
55
private HashMap< String, Long > _contacts;
56
private HashMap< Long, HashSet< String > > _contactNumbers;
57
private HashMap< Long, HashSet< String > > _contactEmails;
54
58
private int _mergeSetting;
55
59
private int _lastMergeDecision;
56
60
private boolean _abort = false;
57
61
private boolean _isFinished = false;
58
private ContactsCache _contactsCache = null;
60
63
public class ContactData
114
private String _address;
117
public AddressData( String address, int type ) {
122
public String getAddress() {
126
public int getType() {
131
115
public String _name = null;
132
116
public HashMap< String, PhoneData > _phones = null;
133
117
public HashMap< String, EmailData > _emails = null;
134
public HashMap< String, AddressData > _addresses = null;
136
119
protected void setName( String name )
157
140
if( !_emails.containsKey( email ) )
158
141
_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
145
@SuppressWarnings("serial")
379
350
case Doit.ACTION_KEEP:
380
351
// if we keep contacts on duplicate, we better check for one
381
return !_contactsCache.exists( name );
352
return !_contacts.containsKey( name );
383
354
case Doit.ACTION_PROMPT:
384
355
// if we are prompting on duplicate, we better check for one
385
if( !_contactsCache.exists( name ) )
356
if( !_contacts.containsKey( name ) )
388
359
// ok, it exists, so do prompt
526
496
values.put( Contacts.Phones.NUMBER, phone._number );
527
497
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
528
498
_doit.getContentResolver().insert( contactPhonesUri, values );
530
// and add this address to the cache to prevent a addition of
531
// duplicate date from another file
532
_contactsCache.addNumber( contactId, number );
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 );
546
529
while( i.hasNext() ) {
547
530
ContactData.EmailData email = emails.get( i.next() );
549
// we don't want to add this email address if it exists already or
550
// 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.
551
534
String address = sanitiseEmailAddress( email.getAddress() );
552
535
if( address == null ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
536
HashSet< String > addresses = _contactEmails.get( contactId );
537
if( addresses != null && addresses.contains( address ) ) continue;
555
539
// add phone number
556
540
ContentValues values = new ContentValues();
561
545
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
562
546
_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 );
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
// now add those email addresses to the cache to prevent the addition of
551
// duplicate data from another file
552
i = emailsKeys.iterator();
581
553
while( i.hasNext() ) {
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() );
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 );
612
static public String sanitisePhoneNumber( String number )
576
private void buildContactsCache() throws AbortImportException
579
setProgressMessage( R.string.doit_caching );
584
// init contacts caches
585
_contacts = new HashMap< String, Long >();
586
_contactNumbers = new HashMap< Long, HashSet< String > >();
587
_contactEmails = new HashMap< Long, HashSet< String > >();
589
// query and store map of contact names to ids
590
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
591
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
592
cols, null, null, null);
593
if( cur.moveToFirst() ) {
594
int idCol = cur.getColumnIndex( Contacts.People._ID );
595
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
597
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
598
} while( cur.moveToNext() );
601
// query and store map of contact ids to sets of phone numbers
602
cols = new String[] { Contacts.Phones.PERSON_ID,
603
Contacts.Phones.NUMBER };
604
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
605
cols, null, null, null);
606
if( cur.moveToFirst() ) {
607
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
608
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
610
Long id = cur.getLong( personIdCol );
611
String number = sanitisePhoneNumber(
612
cur.getString( numberCol ) );
613
if( number != null ) {
614
HashSet< String > numbers = _contactNumbers.get( id );
615
if( numbers == null ) {
616
_contactNumbers.put( id, new HashSet< String >() );
617
numbers = _contactNumbers.get( id );
619
numbers.add( number );
621
} while( cur.moveToNext() );
624
// query and store map of contact ids to sets of email addresses
625
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
626
Contacts.ContactMethods.DATA };
627
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
628
cols, Contacts.ContactMethods.KIND + " = ?",
629
new String[] { "" + Contacts.KIND_EMAIL }, null );
630
if( cur.moveToFirst() ) {
631
int personIdCol = cur.getColumnIndex(
632
Contacts.ContactMethods.PERSON_ID );
633
int addressCol = cur.getColumnIndex(
634
Contacts.ContactMethods.DATA );
636
Long id = cur.getLong( personIdCol );
637
String address = sanitiseEmailAddress(
638
cur.getString( addressCol ) );
639
if( address != null ) {
640
HashSet< String > addresses = _contactEmails.get( id );
641
if( addresses == null ) {
642
_contactEmails.put( id, new HashSet< String >() );
643
addresses = _contactEmails.get( id );
645
addresses.add( address );
647
} while( cur.moveToNext() );
651
private String sanitisePhoneNumber( String number )
614
653
number = number.replaceAll( "[-\\(\\) ]", "" );
615
654
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );