640
583
return _doit.getText( res );
644
* Should we skip a contact, given whether it exists or not and the current
645
* merge setting? This routine handles throwing up a prompt, if required.
647
* @param contact_detail the display name of the contact
648
* @param exists true if this contact matches one in the cache
649
* @param merge_setting the merge setting to use
650
* @return true if the contact should be skipped outright
651
* @throws AbortImportException
653
synchronized private boolean shouldWeSkipContact( String contact_detail,
654
boolean exists, int merge_setting ) throws AbortImportException
586
synchronized private boolean checkForDuplicate(
587
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
throws AbortImportException
656
590
_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)
658
596
// handle special cases
659
597
switch( merge_setting )
661
599
case Doit.ACTION_KEEP:
662
// if we are skipping on a duplicate, check for one
600
// if we keep contacts on duplicate, we better check for one
601
return !_contacts_cache.canLookup( cache_identifier );
665
603
case Doit.ACTION_PROMPT:
666
// if we are prompting on duplicate, then we can say that we won't
667
// skip if there isn't one
668
if( !exists ) return false;
604
// if we are prompting on duplicate, we better check for one and if
605
// the contact doesn'te exist, we want to import it
606
if( !_contacts_cache.canLookup( cache_identifier ) )
670
// ok, duplicate exists, so do prompt
609
// ok, it exists, so do prompt
671
610
_doit._handler.sendMessage( Message.obtain( _doit._handler,
672
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
611
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
706
// It is expected that we use contact.getCacheIdentifier() here. The
644
// It is expected that we use contact.getCacheIdentifier() here. The
707
645
// contact we are passed should have been successfully finalise()d,
708
646
// which includes generating a valid cache identifier.
709
647
ContactsCache.CacheIdentifier cache_identifier =
710
648
contact.getCacheIdentifier();
650
// check to see if this contact is a duplicate and should be skipped
651
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
712
656
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
713
657
// finish( ACTION_ABORT );
659
// keep track of whether we've informed the UI of what we're doing
660
boolean ui_informed = false;
715
662
// attempt to lookup the id of an existing contact in the cache with
716
663
// this contact data's cache identifier
717
664
Long id = (Long)_contacts_cache.lookup( cache_identifier );
719
// check to see if this contact should be skipped
720
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
666
// does contact exist already?
723
// show that we're skipping a contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
669
// should we skip this import altogether?
670
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
673
Uri contact_uri = ContentUris.withAppendedId(
674
Contacts.People.CONTENT_URI, id );
676
// should we destroy the existing contact before importing?
677
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
679
// remove from device
680
_doit.getContentResolver().delete( contact_uri, null, null );
683
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
684
_contacts_cache.removeAssociatedData( id );
686
// show that we're overwriting a contact
687
_doit._handler.sendEmptyMessage(
688
Doit.MESSAGE_CONTACTOVERWRITTEN );
691
// discard the contact id
728
// if a contact exists, and we're overwriting, destroy the existing
729
// contact before importing
730
boolean contact_deleted = false;
731
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
696
// if we don't have a contact id yet (or if we did, but we destroyed it
697
// when we deleted the contact), we'll have to create a new contact
733
contact_deleted = true;
700
// create a new contact
701
ContentValues values = new ContentValues();
702
values.put( Contacts.People.NAME, contact._name );
703
Uri contact_uri = _doit.getContentResolver().insert(
704
Contacts.People.CONTENT_URI, values );
705
id = ContentUris.parseId( contact_uri );
706
if( id == null || id <= 0 )
707
showError( R.string.error_unabletoaddcontact );
735
// remove from device
736
_backend.deleteContact( id );
709
// try to add them to the "My Contacts" group
711
Contacts.People.addToMyContactsGroup(
712
_doit.getContentResolver(), id );
714
catch( IllegalStateException e ) {
715
// ignore any failure
739
_contacts_cache.removeLookup( cache_identifier );
740
_contacts_cache.removeAssociatedData( id );
742
// show that we're overwriting a contact
743
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
745
// discard the contact id
750
// if we don't have a contact id yet (or we did, but we destroyed it
751
// when we deleted the contact), we'll have to create a new contact
754
// create a new contact
755
id = _backend.addContact( contact._name );
758
_contacts_cache.addLookup( cache_identifier, id );
760
// if we haven't already shown that we're overwriting a contact,
761
// show that we're creating a new contact
762
if( !contact_deleted )
763
_doit._handler.sendEmptyMessage(
764
Doit.MESSAGE_CONTACTCREATED );
719
_contacts_cache.addLookup(
720
ContactsCache.createIdentifier( contact ), id );
722
// if we haven't already shown that we're overwriting a contact,
723
// show that we're creating a new contact
725
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
767
// show that we're merging with an existing contact
768
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
770
// import contact parts
771
if( contact.hasNumbers() )
772
importContactPhones( id, contact.getNumbers() );
773
if( contact.hasEmails() )
774
importContactEmails( id, contact.getEmails() );
775
if( contact.hasAddresses() )
776
importContactAddresses( id, contact.getAddresses() );
777
if( contact.hasOrganisations() )
778
importContactOrganisations( id, contact.getOrganisations() );
779
if( contact.hasNotes() )
780
importContactNotes( id, contact.getNotes() );
781
if( contact.hasBirthday() )
782
importContactBirthday( id, contact.getBirthday() );
784
catch( Backend.ContactCreationException e )
786
showError( R.string.error_unabletoaddcontact );
730
// if we haven't already shown that we're overwriting or creating a
731
// contact, show that we're merging a contact
733
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
735
// import contact parts
736
if( contact.hasNumbers() )
737
importContactPhones( id, contact.getNumbers() );
738
if( contact.hasEmails() )
739
importContactEmails( id, contact.getEmails() );
740
if( contact.hasAddresses() )
741
importContactAddresses( id, contact.getAddresses() );
742
if( contact.hasOrganisations() )
743
importContactOrganisations( id, contact.getOrganisations() );
790
746
private void importContactPhones( Long id,
791
HashMap< String, ContactData.PreferredDetail > datas )
792
throws ContactCreationException
747
HashMap< String, ContactData.PreferredDetail > datas )
749
// get URI to contact's phones
750
Uri contact_phones_uri = Uri.withAppendedPath(
751
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
752
Contacts.People.Phones.CONTENT_DIRECTORY );
753
Set< String > datas_keys = datas.keySet();
794
755
// add phone numbers
795
Set< String > datas_keys = datas.keySet();
796
756
Iterator< String > i = datas_keys.iterator();
797
757
while( i.hasNext() ) {
798
758
String number = i.next();
799
759
ContactData.PreferredDetail data = datas.get( number );
801
// We don't want to add this number if it's crap, or it already
802
// exists (which would cause a duplicate to be created). We don't
803
// take in to account the type when checking for duplicates. This
804
// is intentional: types aren't really very reliable. We assume
805
// that if the number exists at all, it doesn't need importing.
806
// Because of this, we also can't update the cache (which we don't
807
// need to anyway, so it's not a problem).
761
// we don't want to add this number if it's crap, or it already
762
// exists (which would cause a duplicate to be created). We don't
763
// take in to account the type when checking for duplicates. This is
764
// intentional: types aren't really very reliable. We assume that
765
// if the number exists at all, it doesn't need importing. Because
766
// of this, we also can't update the cache (which we don't need to
767
// anyway, so it's not a problem).
808
768
if( _contacts_cache.hasAssociatedNumber( id, number ) )
811
771
// add phone number
812
_backend.addContactPhone( id, number, data );
772
ContentValues values = new ContentValues();
773
values.put( Contacts.Phones.TYPE, data.getType() );
774
values.put( Contacts.Phones.NUMBER, number );
775
if( data.isPreferred() )
776
values.put( Contacts.Phones.ISPRIMARY, 1 );
777
_doit.getContentResolver().insert( contact_phones_uri, values );
814
779
// and add this address to the cache to prevent a addition of
815
780
// duplicate date from another file
820
785
private void importContactEmails( Long id,
821
HashMap< String, ContactData.PreferredDetail > datas )
822
throws ContactCreationException
786
HashMap< String, ContactData.PreferredDetail > datas )
788
// get URI to contact's contact methods
789
Uri contact_contact_methods_uri = Uri.withAppendedPath(
790
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
791
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
792
Set< String > datas_keys = datas.keySet();
824
794
// add email addresses
825
Set< String > datas_keys = datas.keySet();
826
795
Iterator< String > i = datas_keys.iterator();
827
796
while( i.hasNext() ) {
828
797
String email = i.next();
829
798
ContactData.PreferredDetail data = datas.get( email );
831
800
// we don't want to add this email address if it exists already or
832
// we would introduce duplicates
801
// we would introduce duplicates.
833
802
if( _contacts_cache.hasAssociatedEmail( id, email ) )
836
805
// add phone number
837
_backend.addContactEmail( id, email, data );
806
ContentValues values = new ContentValues();
807
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
808
values.put( Contacts.ContactMethods.DATA, email );
809
values.put( Contacts.ContactMethods.TYPE, data.getType() );
810
if( data.isPreferred() )
811
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
812
_doit.getContentResolver().insert( contact_contact_methods_uri,
839
815
// and add this address to the cache to prevent a addition of
840
816
// duplicate date from another file