55
55
private int _last_merge_decision;
56
56
private boolean _abort = false;
57
57
private boolean _is_finished = false;
58
private ContactsCache _contactsCache = null;
60
@SuppressWarnings("serial")
61
protected class ContactNeedsMoreInfoException extends Exception
58
private ContactsCache _contacts_cache = null;
66
61
* Data about a contact
120
@SuppressWarnings("serial")
121
protected class ContactNotIdentifiableException extends Exception
125
125
protected String _name = null;
126
126
protected String _primary_organisation = null;
127
127
protected boolean _primary_organisation_is_preferred = false;
134
134
protected HashMap< String, PreferredDetail > _emails = null;
135
135
protected HashMap< String, TypeDetail > _addresses = null;
137
private ContactsCache.CacheIdentifier _cache_identifier = null;
137
139
protected void setName( String name )
328
330
protected void finalise()
331
throws ContactNotIdentifiableException
330
333
// ensure that if there is a primary number, it is preferred so
331
334
// that there is always one preferred number. Android will assign
349
352
_organisations.put( _primary_organisation,
350
353
new ExtraDetail( 0, true, data.getExtra() ) );
356
// create a cache identifier from this contact data, which can be
357
// used to look-up an existing contact
358
_cache_identifier = ContactsCache.createIdentifier( this );
359
if( _cache_identifier == null )
360
throw new ContactNotIdentifiableException();
363
public ContactsCache.CacheIdentifier getCacheIdentifier()
365
return _cache_identifier;
354
368
private String sanitisePhoneNumber( String number )
394
408
setProgressMessage( R.string.doit_caching );
396
410
// build a cache of existing contacts
397
_contactsCache = new ContactsCache();
398
_contactsCache.buildCache( _doit );
411
_contacts_cache = new ContactsCache();
412
_contacts_cache.buildCache( _doit );
522
536
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
525
protected void setProgressMax( int maxProgress )
539
protected void setProgressMax( int max_progress )
526
540
throws AbortImportException
529
543
_doit._handler.sendMessage( Message.obtain(
530
544
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
new Integer( maxProgress ) ) );
545
new Integer( max_progress ) ) );
534
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
548
protected void setTmpProgress( int tmp_progress )
549
throws AbortImportException
537
552
_doit._handler.sendMessage( Message.obtain(
538
553
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
539
new Integer( tmpProgress ) ) );
554
new Integer( tmp_progress ) ) );
542
557
protected void setProgress( int progress ) throws AbortImportException
568
583
return _doit.getText( res );
571
protected boolean isImportRequired( ContactData contact )
572
throws AbortImportException, ContactNeedsMoreInfoException
575
return isImportRequired( contact, _merge_setting );
578
synchronized private boolean isImportRequired(
579
ContactData contact, int merge_setting )
580
throws AbortImportException, ContactNeedsMoreInfoException
586
synchronized private boolean checkForDuplicate(
587
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
throws AbortImportException
582
590
_last_merge_decision = merge_setting;
584
// create a cache identifier which we can use to detect if this contact
585
// is valid for importing
586
ContactsCache.CacheIdentifier identifier =
587
ContactsCache.createIdentifier( contact );
588
if( identifier == null )
589
throw new ContactNeedsMoreInfoException();
592
// it is ok to use contact.getCacheIdentifier(). The contact has already
593
// been finalised, which means a valid cache identifier will have been
594
// created for it (or it would have been skipped)
591
596
// handle special cases
592
597
switch( merge_setting )
594
599
case Doit.ACTION_KEEP:
595
600
// if we keep contacts on duplicate, we better check for one
596
return !_contactsCache.canLookup( identifier );
601
return !_contacts_cache.canLookup( cache_identifier );
598
603
case Doit.ACTION_PROMPT:
599
604
// if we are prompting on duplicate, we better check for one and if
600
605
// the contact doesn'te exist, we want to import it
601
if( !_contactsCache.canLookup( identifier ) )
606
if( !_contacts_cache.canLookup( cache_identifier ) )
604
609
// ok, it exists, so do prompt
605
610
_doit._handler.sendMessage( Message.obtain( _doit._handler,
606
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
611
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
617
622
_merge_setting = _response;
619
624
// recurse, with our new merge setting
620
return isImportRequired( contact, _response );
625
return checkForDuplicate( cache_identifier, _response );
623
628
// for all other cases (either overwriting or merging) we will need the
644
// It is expected that we use contact.getCacheIdentifier() here. The
645
// contact we are passed should have been successfully finalise()d,
646
// which includes generating a valid cache identifier.
647
ContactsCache.CacheIdentifier cache_identifier =
648
contact.getCacheIdentifier();
650
// check to see if this contact is a duplicate and should be skipped
651
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
639
656
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
640
657
// finish( ACTION_ABORT );
642
ContentValues values = new ContentValues();
643
boolean uiInformed = false;
646
// give the contact a chance to finalise it's data
649
// create something, from the contact data, that we can use to identify
650
// a cache entry and attempt to lookup the id of an existing contact in
652
ContactsCache.CacheIdentifier identifier =
653
ContactsCache.createIdentifier( contact );
654
if( identifier != null ) id = (Long)_contactsCache.lookup( identifier );
659
// keep track of whether we've informed the UI of what we're doing
660
boolean ui_informed = false;
662
// attempt to lookup the id of an existing contact in the cache with
663
// this contact data's cache identifier
664
Long id = (Long)_contacts_cache.lookup( cache_identifier );
656
666
// does contact exist already?
660
670
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
662
672
// get contact's URI
663
Uri contactUri = ContentUris.withAppendedId(
673
Uri contact_uri = ContentUris.withAppendedId(
664
674
Contacts.People.CONTENT_URI, id );
666
676
// should we destroy the existing contact before importing?
667
677
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
669
679
// remove from device
670
_doit.getContentResolver().delete( contactUri, null, null );
680
_doit.getContentResolver().delete( contact_uri, null, null );
673
_contactsCache.removeLookup( identifier );
674
_contactsCache.removeAssociatedData( id );
683
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
684
_contacts_cache.removeAssociatedData( id );
676
686
// show that we're overwriting a contact
677
687
_doit._handler.sendEmptyMessage(
678
688
Doit.MESSAGE_CONTACTOVERWRITTEN );
681
691
// discard the contact id
690
700
// create a new contact
701
ContentValues values = new ContentValues();
691
702
values.put( Contacts.People.NAME, contact._name );
692
Uri contactUri = _doit.getContentResolver().insert(
703
Uri contact_uri = _doit.getContentResolver().insert(
693
704
Contacts.People.CONTENT_URI, values );
694
id = ContentUris.parseId( contactUri );
705
id = ContentUris.parseId( contact_uri );
695
706
if( id == null || id <= 0 )
696
707
showError( R.string.error_unabletoaddcontact );
708
_contactsCache.addLookup(
719
_contacts_cache.addLookup(
709
720
ContactsCache.createIdentifier( contact ), id );
711
722
// if we haven't already shown that we're overwriting a contact,
712
723
// show that we're creating a new contact
714
725
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
719
730
// if we haven't already shown that we're overwriting or creating a
720
// contact show that we're merging a contact
731
// contact, show that we're merging a contact
722
733
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
724
735
// import contact parts
736
747
HashMap< String, ContactData.PreferredDetail > datas )
738
749
// get URI to contact's phones
739
Uri contactPhonesUri = Uri.withAppendedPath(
750
Uri contact_phones_uri = Uri.withAppendedPath(
740
751
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
741
752
Contacts.People.Phones.CONTENT_DIRECTORY );
742
Set< String > datasKeys = datas.keySet();
753
Set< String > datas_keys = datas.keySet();
744
755
// add phone numbers
745
Iterator< String > i = datasKeys.iterator();
756
Iterator< String > i = datas_keys.iterator();
746
757
while( i.hasNext() ) {
747
758
String number = i.next();
748
759
ContactData.PreferredDetail data = datas.get( number );
754
765
// if the number exists at all, it doesn't need importing. Because
755
766
// of this, we also can't update the cache (which we don't need to
756
767
// anyway, so it's not a problem).
757
if( _contactsCache.hasAssociatedNumber( id, number ) )
768
if( _contacts_cache.hasAssociatedNumber( id, number ) )
760
771
// add phone number
763
774
values.put( Contacts.Phones.NUMBER, number );
764
775
if( data.isPreferred() )
765
776
values.put( Contacts.Phones.ISPRIMARY, 1 );
766
_doit.getContentResolver().insert( contactPhonesUri, values );
777
_doit.getContentResolver().insert( contact_phones_uri, values );
768
779
// and add this address to the cache to prevent a addition of
769
780
// duplicate date from another file
770
_contactsCache.addAssociatedNumber( id, number );
781
_contacts_cache.addAssociatedNumber( id, number );
775
786
HashMap< String, ContactData.PreferredDetail > datas )
777
788
// get URI to contact's contact methods
778
Uri contactContactMethodsUri = Uri.withAppendedPath(
789
Uri contact_contact_methods_uri = Uri.withAppendedPath(
779
790
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
780
791
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
781
Set< String > datasKeys = datas.keySet();
792
Set< String > datas_keys = datas.keySet();
783
794
// add email addresses
784
Iterator< String > i = datasKeys.iterator();
795
Iterator< String > i = datas_keys.iterator();
785
796
while( i.hasNext() ) {
786
797
String email = i.next();
787
798
ContactData.PreferredDetail data = datas.get( email );
789
800
// we don't want to add this email address if it exists already or
790
801
// we would introduce duplicates.
791
if( _contactsCache.hasAssociatedEmail( id, email ) )
802
if( _contacts_cache.hasAssociatedEmail( id, email ) )
794
805
// add phone number
798
809
values.put( Contacts.ContactMethods.TYPE, data.getType() );
799
810
if( data.isPreferred() )
800
811
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
801
_doit.getContentResolver().insert( contactContactMethodsUri,
812
_doit.getContentResolver().insert( contact_contact_methods_uri,
804
815
// and add this address to the cache to prevent a addition of
805
816
// duplicate date from another file
806
_contactsCache.addAssociatedEmail( id, email );
817
_contacts_cache.addAssociatedEmail( id, email );
811
822
HashMap< String, ContactData.TypeDetail > datas )
813
824
// get URI to contact's contact methods
814
Uri contactContactMethodsUri = Uri.withAppendedPath(
825
Uri contact_contact_methods_uri = Uri.withAppendedPath(
815
826
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
816
827
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
819
Set< String > datasKeys = datas.keySet();
820
Iterator< String > i = datasKeys.iterator();
830
Set< String > datas_keys = datas.keySet();
831
Iterator< String > i = datas_keys.iterator();
821
832
while( i.hasNext() ) {
822
833
String address = i.next();
823
834
ContactData.TypeDetail data = datas.get( address );
825
836
// we don't want to add this address if it exists already or we
826
837
// would introduce duplicates
827
if( _contactsCache.hasAssociatedAddress( id, address ) )
838
if( _contacts_cache.hasAssociatedAddress( id, address ) )
830
841
// add postal address
832
843
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
833
844
values.put( Contacts.ContactMethods.DATA, address );
834
845
values.put( Contacts.ContactMethods.TYPE, data.getType() );
835
_doit.getContentResolver().insert( contactContactMethodsUri,
846
_doit.getContentResolver().insert( contact_contact_methods_uri,
838
849
// and add this address to the cache to prevent a addition of
839
850
// duplicate date from another file
840
_contactsCache.addAssociatedAddress( id, address );
851
_contacts_cache.addAssociatedAddress( id, address );
845
856
HashMap< String, ContactData.ExtraDetail > datas )
848
Set< String > datasKeys = datas.keySet();
849
Iterator< String > i = datasKeys.iterator();
859
Set< String > datas_keys = datas.keySet();
860
Iterator< String > i = datas_keys.iterator();
850
861
while( i.hasNext() ) {
851
862
String organisation = i.next();
852
863
ContactData.ExtraDetail data = datas.get( organisation );
854
865
// we don't want to add this address if it exists already or we
855
866
// would introduce duplicates
856
if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
867
if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
859
870
// add organisation address
870
881
// and add this address to the cache to prevent a addition of
871
882
// duplicate date from another file
872
_contactsCache.addAssociatedOrganisation( id, organisation );
883
_contacts_cache.addAssociatedOrganisation( id, organisation );