583
640
return _doit.getText( res );
586
synchronized private boolean checkForDuplicate(
587
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
throws AbortImportException
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
590
656
_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)
596
658
// handle special cases
597
659
switch( merge_setting )
599
661
case Doit.ACTION_KEEP:
600
// if we keep contacts on duplicate, we better check for one
601
return !_contacts_cache.canLookup( cache_identifier );
662
// if we are skipping on a duplicate, check for one
603
665
case Doit.ACTION_PROMPT:
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 ) )
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;
609
// ok, it exists, so do prompt
670
// ok, duplicate exists, so do prompt
610
671
_doit._handler.sendMessage( Message.obtain( _doit._handler,
611
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
672
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
644
// It is expected that we use contact.getCacheIdentifier() here. The
706
// It is expected that we use contact.getCacheIdentifier() here. The
645
707
// contact we are passed should have been successfully finalise()d,
646
708
// which includes generating a valid cache identifier.
647
709
ContactsCache.CacheIdentifier cache_identifier =
648
710
contact.getCacheIdentifier();
650
// check to see if this contact is a duplicate and should be skipped
651
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
656
712
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
657
713
// finish( ACTION_ABORT );
659
// keep track of whether we've informed the UI of what we're doing
660
boolean ui_informed = false;
662
715
// attempt to lookup the id of an existing contact in the cache with
663
716
// this contact data's cache identifier
664
717
Long id = (Long)_contacts_cache.lookup( cache_identifier );
666
// does contact exist already?
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 )
719
// check to see if this contact should be skipped
720
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
723
// show that we're skipping a contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
733
contact_deleted = true;
735
// remove from device
736
_backend.deleteContact( id );
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
679
// remove from device
680
_doit.getContentResolver().delete( contact_uri, null, null );
754
// create a new contact
755
id = _backend.addContact( contact._name );
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
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 );
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() );
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
784
catch( Backend.ContactCreationException e )
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 );
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
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 );
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() );
746
790
private void importContactPhones( Long id,
747
HashMap< String, ContactData.PreferredDetail > datas )
791
HashMap< String, ContactData.PreferredDetail > datas )
792
throws ContactCreationException
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();
755
794
// add phone numbers
795
Set< String > datas_keys = datas.keySet();
756
796
Iterator< String > i = datas_keys.iterator();
757
797
while( i.hasNext() ) {
758
798
String number = i.next();
759
799
ContactData.PreferredDetail data = datas.get( number );
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).
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).
768
808
if( _contacts_cache.hasAssociatedNumber( id, number ) )
771
811
// add phone number
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 );
812
_backend.addContactPhone( id, number, data );
779
814
// and add this address to the cache to prevent a addition of
780
815
// duplicate date from another file
785
820
private void importContactEmails( Long id,
786
HashMap< String, ContactData.PreferredDetail > datas )
821
HashMap< String, ContactData.PreferredDetail > datas )
822
throws ContactCreationException
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();
794
824
// add email addresses
825
Set< String > datas_keys = datas.keySet();
795
826
Iterator< String > i = datas_keys.iterator();
796
827
while( i.hasNext() ) {
797
828
String email = i.next();
798
829
ContactData.PreferredDetail data = datas.get( email );
800
831
// we don't want to add this email address if it exists already or
801
// we would introduce duplicates.
832
// we would introduce duplicates
802
833
if( _contacts_cache.hasAssociatedEmail( id, email ) )
805
836
// add phone number
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,
837
_backend.addContactEmail( id, email, data );
815
839
// and add this address to the cache to prevent a addition of
816
840
// duplicate date from another file