56
56
private HashMap< String, Long > _contacts;
57
57
private HashMap< Long, HashSet< String > > _contactNumbers;
58
58
private HashMap< Long, HashSet< String > > _contactEmails;
59
private HashMap< Long, HashSet< String > > _contactAddresses;
59
60
private int _mergeSetting;
60
61
private int _lastMergeDecision;
61
62
private boolean _abort = false;
119
private String _address;
122
public AddressData( String address, int type ) {
127
public String getAddress() {
131
public int getType() {
116
136
public String _name = null;
117
137
public HashMap< String, PhoneData > _phones = null;
118
138
public HashMap< String, EmailData > _emails = null;
139
public HashMap< String, AddressData > _addresses = null;
120
141
protected void setName( String name )
132
153
if( _phones == null ) _phones = new HashMap< String, PhoneData >();
133
154
if( !_phones.containsKey( number ) )
134
155
_phones.put( number,
135
new PhoneData( number, type, isPreferred ) );
156
new PhoneData( number, type, isPreferred ) );
138
159
protected void addEmail( String email, int type, boolean isPreferred )
141
162
if( !_emails.containsKey( email ) )
142
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 ) );
175
@SuppressWarnings("serial")
146
176
protected class AbortImportException extends Exception { };
148
178
public Importer( Doit doit )
227
257
_doit._handler.sendMessage( Message.obtain(
228
_doit._handler, Doit.MESSAGE_ERROR, message ) );
258
_doit._handler, Doit.MESSAGE_ERROR, message ) );
232
262
catch( InterruptedException e ) { }
233
264
// no need to check if an abortion happened during the wait, we are
234
265
// about to finish anyway!
235
266
finish( ACTION_ABORT );
247
278
_doit._handler.sendMessage( Message.obtain(
248
_doit._handler, Doit.MESSAGE_ERROR, message ) );
279
_doit._handler, Doit.MESSAGE_ERROR, message ) );
252
283
catch( InterruptedException e ) { }
253
285
// no need to check if an abortion happened during the wait, we are
254
286
// about to finish anyway!
255
287
finish( ACTION_ABORT );
291
323
_doit._handler.sendMessage( Message.obtain(
292
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
293
new Integer( maxProgress ) ) );
324
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
325
new Integer( maxProgress ) ) );
296
328
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
299
331
_doit._handler.sendMessage( Message.obtain(
300
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
301
new Integer( tmpProgress ) ) );
332
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
333
new Integer( tmpProgress ) ) );
304
336
protected void setProgress( int progress ) throws AbortImportException
307
339
_doit._handler.sendMessage( Message.obtain(
308
_doit._handler, Doit.MESSAGE_SETPROGRESS,
309
new Integer( progress ) ) );
340
_doit._handler, Doit.MESSAGE_SETPROGRESS,
341
new Integer( progress ) ) );
312
344
protected void finish( int action ) throws AbortImportException
318
case ACTION_GOBACK: message = Doit.MESSAGE_FINISHED_GOBACK; break;
319
case ACTION_ALLDONE: message = Doit.MESSAGE_FINISHED_ALLDONE; break;
350
case ACTION_ALLDONE: message = Doit.MESSAGE_ALLDONE; break;
320
351
default: // fall through
321
case ACTION_ABORT: message = Doit.MESSAGE_FINISHED; break;
352
case ACTION_ABORT: message = Doit.MESSAGE_ABORT; break;
323
354
_doit._handler.sendEmptyMessage( message );
358
389
// ok, it exists, so do prompt
359
390
_doit._handler.sendMessage( Message.obtain(
360
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
391
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
370
401
if( _responseExtra == RESPONSEEXTRA_ALWAYS )
371
402
_mergeSetting = _response;
373
// recurse, with out new merge setting
404
// recurse, with our new merge setting
374
405
return isImportRequired( name, _response );
407
438
// get contact's URI
408
439
contactUri = ContentUris.withAppendedId(
409
Contacts.People.CONTENT_URI, id );
440
Contacts.People.CONTENT_URI, id );
411
442
// should we destroy the existing contact before importing?
412
443
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
413
444
_doit.getContentResolver().delete( contactUri, null, null );
414
445
contactUri = null;
417
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
448
_doit._handler.sendEmptyMessage(
449
Doit.MESSAGE_CONTACTOVERWRITTEN );
418
450
uiInformed = true;
429
461
// create a new contact
430
462
values.put( Contacts.People.NAME, contact._name );
431
463
contactUri = _doit.getContentResolver().insert(
432
Contacts.People.CONTENT_URI, values );
464
Contacts.People.CONTENT_URI, values );
433
465
id = ContentUris.parseId( contactUri );
434
466
if( id <= 0 ) return; // shouldn't happen!
436
// add them to the "My Contacts" group
437
Contacts.People.addToMyContactsGroup(
468
// try to add them to the "My Contacts" group
470
Contacts.People.addToMyContactsGroup(
438
471
_doit.getContentResolver(), id );
473
catch( IllegalStateException e ) {
474
// ignore any failure
441
478
_contacts.put( contact._name, id );
456
493
importContactPhones( contactUri, contact._phones );
457
494
if( contact._emails != null )
458
495
importContactEmails( contactUri, contact._emails );
496
if( contact._addresses != null )
497
importContactAddresses( contactUri, contact._addresses );
461
500
private void importContactPhones( Uri contactUri,
464
503
Long contactId = ContentUris.parseId( contactUri );
465
504
Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
466
505
Contacts.People.Phones.CONTENT_DIRECTORY );
467
Set phonesKeys = phones.keySet();
506
Set< String > phonesKeys = phones.keySet();
469
508
// add phone numbers
470
Iterator i = phonesKeys.iterator();
509
Iterator< String > i = phonesKeys.iterator();
471
510
while( i.hasNext() ) {
472
511
ContactData.PhoneData phone = phones.get( i.next() );
480
519
// anyway, so it's not a problem).
481
520
String number = sanitisePhoneNumber( phone._number );
482
521
if( number == null ) continue;
483
HashSet< String > numbers = _contactNumbers.get( contactId );
484
if( numbers != null && numbers.contains( number ) ) continue;
522
HashSet< String > cache = _contactNumbers.get( contactId );
523
if( cache != null && cache.contains( number ) ) continue;
486
525
// add phone number
487
526
ContentValues values = new ContentValues();
489
528
values.put( Contacts.Phones.NUMBER, phone._number );
490
529
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
491
530
_doit.getContentResolver().insert( contactPhonesUri, values );
494
// now add those phone numbers to the cache to prevent the addition of
495
// duplicate data from another file
496
i = phonesKeys.iterator();
497
while( i.hasNext() ) {
498
ContactData.PhoneData phone = phones.get( i.next() );
500
String number = sanitisePhoneNumber( phone._number );
501
if( number != null ) {
502
HashSet< String > numbers = _contactNumbers.get( contactId );
503
if( numbers == null ) {
504
_contactNumbers.put( contactId, new HashSet< String >() );
505
numbers = _contactNumbers.get( contactId );
507
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 );
515
545
Long contactId = ContentUris.parseId( contactUri );
516
546
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
517
547
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
518
Set emailsKeys = emails.keySet();
548
Set< String > emailsKeys = emails.keySet();
520
550
// add email addresses
521
Iterator i = emailsKeys.iterator();
551
Iterator< String > i = emailsKeys.iterator();
522
552
while( i.hasNext() ) {
523
553
ContactData.EmailData email = emails.get( i.next() );
525
// like with phone numbers, we don't want to add this email address
526
// 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.
527
557
String address = sanitiseEmailAddress( email.getAddress() );
528
558
if( address == null ) continue;
529
HashSet< String > addresses = _contactEmails.get( contactId );
530
if( addresses != null && addresses.contains( address ) ) continue;
559
HashSet< String > cache = _contactEmails.get( contactId );
560
if( cache != null && cache.contains( address ) ) continue;
532
562
// add phone number
533
563
ContentValues values = new ContentValues();
537
567
if( email.isPreferred() )
538
568
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
539
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 );
543
// now add those email addresses to the cache to prevent the addition of
544
// duplicate data from another file
545
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();
546
592
while( i.hasNext() ) {
547
ContactData.EmailData email = emails.get( i.next() );
549
String address = sanitiseEmailAddress( email.getAddress() );
550
if( address != null ) {
551
HashSet< String > addresses = _contactEmails.get( contactId );
552
if( addresses == null ) {
553
_contactEmails.put( contactId, new HashSet< String >() );
554
addresses = _contactEmails.get( contactId );
556
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() );
578
637
_contacts = new HashMap< String, Long >();
579
638
_contactNumbers = new HashMap< Long, HashSet< String > >();
580
639
_contactEmails = new HashMap< Long, HashSet< String > >();
640
_contactAddresses = new HashMap< Long, HashSet< String > >();
582
642
// query and store map of contact names to ids
583
643
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
584
644
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
585
cols, null, null, null);
645
cols, null, null, null);
586
646
if( cur.moveToFirst() ) {
587
647
int idCol = cur.getColumnIndex( Contacts.People._ID );
588
648
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
595
655
cols = new String[] { Contacts.Phones.PERSON_ID,
596
656
Contacts.Phones.NUMBER };
597
657
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
598
cols, null, null, null);
658
cols, null, null, null);
599
659
if( cur.moveToFirst() ) {
600
660
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
601
661
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
622
682
new String[] { "" + Contacts.KIND_EMAIL }, null );
623
683
if( cur.moveToFirst() ) {
624
684
int personIdCol = cur.getColumnIndex(
625
Contacts.ContactMethods.PERSON_ID );
685
Contacts.ContactMethods.PERSON_ID );
626
686
int addressCol = cur.getColumnIndex(
627
Contacts.ContactMethods.DATA );
687
Contacts.ContactMethods.DATA );
629
689
Long id = cur.getLong( personIdCol );
630
690
String address = sanitiseEmailAddress(
631
cur.getString( addressCol ) );
691
cur.getString( addressCol ) );
632
692
if( address != null ) {
633
693
HashSet< String > addresses = _contactEmails.get( id );
634
694
if( addresses == null ) {
635
_contactEmails.put( id, new HashSet< String >() );
636
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 );
638
723
addresses.add( address );
644
729
private String sanitisePhoneNumber( String number )
646
731
number = number.replaceAll( "[-\\(\\) ]", "" );
647
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
732
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
648
733
Matcher m = p.matcher( number );
649
734
if( m.lookingAt() ) return m.group( 0 );
655
740
address = address.trim();
656
741
Pattern p = Pattern.compile(
657
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
742
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
658
743
Matcher m = p.matcher( address );
659
744
if( m.matches() ) {
660
745
String[] bits = address.split( "@" );