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