569
640
return _doit.getText( res );
572
protected boolean isImportRequired( ContactData contact )
573
throws AbortImportException, ContactNeedsMoreInfoException
576
return isImportRequired( contact, _merge_setting );
579
synchronized private boolean isImportRequired(
580
ContactData contact, int merge_setting )
581
throws AbortImportException, ContactNeedsMoreInfoException
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
583
656
_last_merge_decision = merge_setting;
585
// create a cache identifier which we can use to detect if this contact
586
// is valid for importing
587
ContactsCache.CacheIdentifier identifier =
588
ContactsCache.createIdentifier( contact );
589
if( identifier == null )
590
throw new ContactNeedsMoreInfoException();
592
658
// handle special cases
593
659
switch( merge_setting )
595
661
case Doit.ACTION_KEEP:
596
// if we keep contacts on duplicate, we better check for one
597
return !_contacts_cache.canLookup( identifier );
662
// if we are skipping on a duplicate, check for one
599
665
case Doit.ACTION_PROMPT:
600
// if we are prompting on duplicate, we better check for one and if
601
// the contact doesn'te exist, we want to import it
602
if( !_contacts_cache.canLookup( 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;
605
// ok, it exists, so do prompt
670
// ok, duplicate exists, so do prompt
606
671
_doit._handler.sendMessage( Message.obtain( _doit._handler,
607
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
672
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
706
// It is expected that we use contact.getCacheIdentifier() here. The
707
// contact we are passed should have been successfully finalise()d,
708
// which includes generating a valid cache identifier.
709
ContactsCache.CacheIdentifier cache_identifier =
710
contact.getCacheIdentifier();
640
712
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
713
// finish( ACTION_ABORT );
643
ContentValues values = new ContentValues();
644
boolean ui_informed = false;
647
// give the contact a chance to finalise it's data
650
// create something, from the contact data, that we can use to identify
651
// a cache entry and attempt to lookup the id of an existing contact in
653
ContactsCache.CacheIdentifier identifier =
654
ContactsCache.createIdentifier( contact );
655
if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
657
// does contact exist already?
660
// should we skip this import altogether?
661
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
664
Uri contact_uri = ContentUris.withAppendedId(
665
Contacts.People.CONTENT_URI, id );
667
// should we destroy the existing contact before importing?
668
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
715
// attempt to lookup the id of an existing contact in the cache with
716
// this contact data's cache identifier
717
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,
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
670
// remove from device
671
_doit.getContentResolver().delete( contact_uri, null, null );
754
// create a new contact
755
id = _backend.addContact( contact._name );
674
_contacts_cache.removeLookup( identifier );
675
_contacts_cache.removeAssociatedData( id );
677
// show that we're overwriting a contact
678
_doit._handler.sendEmptyMessage(
679
Doit.MESSAGE_CONTACTOVERWRITTEN );
682
// 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() );
687
// if we don't have a contact id yet (or if we did, but we destroyed it
688
// when we deleted the contact), we'll have to create a new contact
784
catch( Backend.ContactCreationException e )
691
// create a new contact
692
values.put( Contacts.People.NAME, contact._name );
693
Uri contact_uri = _doit.getContentResolver().insert(
694
Contacts.People.CONTENT_URI, values );
695
id = ContentUris.parseId( contact_uri );
696
if( id == null || id <= 0 )
697
showError( R.string.error_unabletoaddcontact );
699
// try to add them to the "My Contacts" group
701
Contacts.People.addToMyContactsGroup(
702
_doit.getContentResolver(), id );
704
catch( IllegalStateException e ) {
705
// ignore any failure
709
_contacts_cache.addLookup(
710
ContactsCache.createIdentifier( contact ), id );
712
// if we haven't already shown that we're overwriting a contact,
713
// show that we're creating a new contact
715
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
786
showError( R.string.error_unabletoaddcontact );
720
// if we haven't already shown that we're overwriting or creating a
721
// contact show that we're merging a contact
723
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
725
// import contact parts
726
if( contact.hasNumbers() )
727
importContactPhones( id, contact.getNumbers() );
728
if( contact.hasEmails() )
729
importContactEmails( id, contact.getEmails() );
730
if( contact.hasAddresses() )
731
importContactAddresses( id, contact.getAddresses() );
732
if( contact.hasOrganisations() )
733
importContactOrganisations( id, contact.getOrganisations() );
736
790
private void importContactPhones( Long id,
737
HashMap< String, ContactData.PreferredDetail > datas )
791
HashMap< String, ContactData.PreferredDetail > datas )
792
throws ContactCreationException
739
// get URI to contact's phones
740
Uri contact_phones_uri = Uri.withAppendedPath(
741
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
742
Contacts.People.Phones.CONTENT_DIRECTORY );
743
Set< String > datas_keys = datas.keySet();
745
794
// add phone numbers
795
Set< String > datas_keys = datas.keySet();
746
796
Iterator< String > i = datas_keys.iterator();
747
797
while( i.hasNext() ) {
748
798
String number = i.next();
749
799
ContactData.PreferredDetail data = datas.get( number );
751
// we don't want to add this number if it's crap, or it already
752
// exists (which would cause a duplicate to be created). We don't
753
// take in to account the type when checking for duplicates. This is
754
// intentional: types aren't really very reliable. We assume that
755
// if the number exists at all, it doesn't need importing. Because
756
// of this, we also can't update the cache (which we don't need to
757
// 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).
758
808
if( _contacts_cache.hasAssociatedNumber( id, number ) )
761
811
// add phone number
762
ContentValues values = new ContentValues();
763
values.put( Contacts.Phones.TYPE, data.getType() );
764
values.put( Contacts.Phones.NUMBER, number );
765
if( data.isPreferred() )
766
values.put( Contacts.Phones.ISPRIMARY, 1 );
767
_doit.getContentResolver().insert( contact_phones_uri, values );
812
_backend.addContactPhone( id, number, data );
769
814
// and add this address to the cache to prevent a addition of
770
815
// duplicate date from another file
775
820
private void importContactEmails( Long id,
776
HashMap< String, ContactData.PreferredDetail > datas )
821
HashMap< String, ContactData.PreferredDetail > datas )
822
throws ContactCreationException
778
// get URI to contact's contact methods
779
Uri contact_contact_methods_uri = Uri.withAppendedPath(
780
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
781
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
782
Set< String > datas_keys = datas.keySet();
784
824
// add email addresses
825
Set< String > datas_keys = datas.keySet();
785
826
Iterator< String > i = datas_keys.iterator();
786
827
while( i.hasNext() ) {
787
828
String email = i.next();
788
829
ContactData.PreferredDetail data = datas.get( email );
790
831
// we don't want to add this email address if it exists already or
791
// we would introduce duplicates.
832
// we would introduce duplicates
792
833
if( _contacts_cache.hasAssociatedEmail( id, email ) )
795
836
// add phone number
796
ContentValues values = new ContentValues();
797
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
798
values.put( Contacts.ContactMethods.DATA, email );
799
values.put( Contacts.ContactMethods.TYPE, data.getType() );
800
if( data.isPreferred() )
801
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
802
_doit.getContentResolver().insert( contact_contact_methods_uri,
837
_backend.addContactEmail( id, email, data );
805
839
// and add this address to the cache to prevent a addition of
806
840
// duplicate date from another file