568
606
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
610
* Should we skip a contact, given whether it exists or not and the current
611
* merge setting? This routine handles throwing up a prompt, if required.
612
* @param contact_detail the display name of the contact
613
* @param exists true if this contact matches one in the cache
614
* @param merge_setting the merge setting to use
615
* @return true if the contact should be skipped outright
616
* @throws AbortImportException
618
synchronized private boolean shouldWeSkipContact( String contact_detail,
619
boolean exists, int merge_setting ) throws AbortImportException
582
621
_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();
591
623
// handle special cases
592
624
switch( merge_setting )
594
626
case Doit.ACTION_KEEP:
595
// if we keep contacts on duplicate, we better check for one
596
return !_contactsCache.canLookup( identifier );
627
// if we are skipping on a duplicate, check for one
598
630
case Doit.ACTION_PROMPT:
599
// if we are prompting on duplicate, we better check for one and if
600
// the contact doesn'te exist, we want to import it
601
if( !_contactsCache.canLookup( identifier ) )
631
// if we are prompting on duplicate, then we can say that we won't
632
// skip if there isn't one
633
if( !exists ) return false;
604
// ok, it exists, so do prompt
635
// ok, duplicate exists, so do prompt
605
636
_doit._handler.sendMessage( Message.obtain( _doit._handler,
606
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
637
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
671
// It is expected that we use contact.getCacheIdentifier() here. The
672
// contact we are passed should have been successfully finalise()d,
673
// which includes generating a valid cache identifier.
674
ContactsCache.CacheIdentifier cache_identifier =
675
contact.getCacheIdentifier();
639
677
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
640
678
// 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 );
656
// does contact exist already?
659
// should we skip this import altogether?
660
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
663
Uri contactUri = ContentUris.withAppendedId(
664
Contacts.People.CONTENT_URI, id );
666
// should we destroy the existing contact before importing?
667
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
680
// attempt to lookup the id of an existing contact in the cache with
681
// this contact data's cache identifier
682
Long id = (Long)_contacts_cache.lookup( cache_identifier );
684
// check to see if this contact should be skipped
685
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
688
// show that we're skipping a contact
689
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
693
// if a contact exists, and we're overwriting, destroy the existing
694
// contact before importing
695
boolean contact_deleted = false;
696
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
698
contact_deleted = true;
700
// remove from device
701
_backend.deleteContact( id );
704
_contacts_cache.removeLookup( cache_identifier );
705
_contacts_cache.removeAssociatedData( id );
707
// show that we're overwriting a contact
708
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
710
// discard the contact id
715
// if we don't have a contact id yet (or we did, but we destroyed it
716
// when we deleted the contact), we'll have to create a new contact
669
// remove from device
670
_doit.getContentResolver().delete( contactUri, null, null );
719
// create a new contact
720
id = _backend.addContact( contact._name );
673
_contactsCache.removeLookup( identifier );
674
_contactsCache.removeAssociatedData( id );
676
// show that we're overwriting a contact
677
_doit._handler.sendEmptyMessage(
678
Doit.MESSAGE_CONTACTOVERWRITTEN );
681
// discard the contact id
723
_contacts_cache.addLookup( cache_identifier, id );
725
// if we haven't already shown that we're overwriting a contact,
726
// show that we're creating a new contact
727
if( !contact_deleted )
728
_doit._handler.sendEmptyMessage(
729
Doit.MESSAGE_CONTACTCREATED );
732
// show that we're merging with an existing 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() );
686
// if we don't have a contact id yet (or if we did, but we destroyed it
687
// when we deleted the contact), we'll have to create a new contact
745
catch( Backend.ContactCreationException e )
690
// create a new contact
691
values.put( Contacts.People.NAME, contact._name );
692
Uri contactUri = _doit.getContentResolver().insert(
693
Contacts.People.CONTENT_URI, values );
694
id = ContentUris.parseId( contactUri );
695
if( id == null || id <= 0 )
696
showError( R.string.error_unabletoaddcontact );
698
// try to add them to the "My Contacts" group
700
Contacts.People.addToMyContactsGroup(
701
_doit.getContentResolver(), id );
703
catch( IllegalStateException e ) {
704
// ignore any failure
708
_contactsCache.addLookup(
709
ContactsCache.createIdentifier( contact ), id );
711
// if we haven't already shown that we're overwriting a contact,
712
// show that we're creating a new contact
714
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
747
showError( R.string.error_unabletoaddcontact );
719
// if we haven't already shown that we're overwriting or creating a
720
// contact show that we're merging a contact
722
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
724
// import contact parts
725
if( contact.hasNumbers() )
726
importContactPhones( id, contact.getNumbers() );
727
if( contact.hasEmails() )
728
importContactEmails( id, contact.getEmails() );
729
if( contact.hasAddresses() )
730
importContactAddresses( id, contact.getAddresses() );
731
if( contact.hasOrganisations() )
732
importContactOrganisations( id, contact.getOrganisations() );
735
751
private void importContactPhones( Long id,
736
HashMap< String, ContactData.PreferredDetail > datas )
752
HashMap< String, ContactData.PreferredDetail > datas )
753
throws ContactCreationException
738
// get URI to contact's phones
739
Uri contactPhonesUri = Uri.withAppendedPath(
740
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
741
Contacts.People.Phones.CONTENT_DIRECTORY );
742
Set< String > datasKeys = datas.keySet();
744
755
// add phone numbers
745
Iterator< String > i = datasKeys.iterator();
756
Set< String > datas_keys = datas.keySet();
757
Iterator< String > i = datas_keys.iterator();
746
758
while( i.hasNext() ) {
747
759
String number = i.next();
748
760
ContactData.PreferredDetail data = datas.get( number );
754
766
// if the number exists at all, it doesn't need importing. Because
755
767
// of this, we also can't update the cache (which we don't need to
756
768
// anyway, so it's not a problem).
757
if( _contactsCache.hasAssociatedNumber( id, number ) )
769
if( _contacts_cache.hasAssociatedNumber( id, number ) )
760
772
// add phone number
761
ContentValues values = new ContentValues();
762
values.put( Contacts.Phones.TYPE, data.getType() );
763
values.put( Contacts.Phones.NUMBER, number );
764
if( data.isPreferred() )
765
values.put( Contacts.Phones.ISPRIMARY, 1 );
766
_doit.getContentResolver().insert( contactPhonesUri, values );
773
_backend.addContactPhone( id, number, data );
768
775
// and add this address to the cache to prevent a addition of
769
776
// duplicate date from another file
770
_contactsCache.addAssociatedNumber( id, number );
777
_contacts_cache.addAssociatedNumber( id, number );
774
781
private void importContactEmails( Long id,
775
HashMap< String, ContactData.PreferredDetail > datas )
782
HashMap< String, ContactData.PreferredDetail > datas )
783
throws ContactCreationException
777
// get URI to contact's contact methods
778
Uri contactContactMethodsUri = Uri.withAppendedPath(
779
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
780
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
781
Set< String > datasKeys = datas.keySet();
783
785
// add email addresses
784
Iterator< String > i = datasKeys.iterator();
786
Set< String > datas_keys = datas.keySet();
787
Iterator< String > i = datas_keys.iterator();
785
788
while( i.hasNext() ) {
786
789
String email = i.next();
787
790
ContactData.PreferredDetail data = datas.get( email );
789
792
// we don't want to add this email address if it exists already or
790
793
// we would introduce duplicates.
791
if( _contactsCache.hasAssociatedEmail( id, email ) )
794
if( _contacts_cache.hasAssociatedEmail( id, email ) )
794
797
// add phone number
795
ContentValues values = new ContentValues();
796
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
797
values.put( Contacts.ContactMethods.DATA, email );
798
values.put( Contacts.ContactMethods.TYPE, data.getType() );
799
if( data.isPreferred() )
800
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
801
_doit.getContentResolver().insert( contactContactMethodsUri,
798
_backend.addContactEmail( id, email, data );
804
800
// and add this address to the cache to prevent a addition of
805
801
// duplicate date from another file
806
_contactsCache.addAssociatedEmail( id, email );
802
_contacts_cache.addAssociatedEmail( id, email );
810
806
private void importContactAddresses( Long id,
811
807
HashMap< String, ContactData.TypeDetail > datas )
808
throws ContactCreationException
813
// get URI to contact's contact methods
814
Uri contactContactMethodsUri = Uri.withAppendedPath(
815
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
816
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
819
Set< String > datasKeys = datas.keySet();
820
Iterator< String > i = datasKeys.iterator();
811
Set< String > datas_keys = datas.keySet();
812
Iterator< String > i = datas_keys.iterator();
821
813
while( i.hasNext() ) {
822
814
String address = i.next();
823
815
ContactData.TypeDetail data = datas.get( address );
825
817
// we don't want to add this address if it exists already or we
826
818
// would introduce duplicates
827
if( _contactsCache.hasAssociatedAddress( id, address ) )
819
if( _contacts_cache.hasAssociatedAddress( id, address ) )
830
822
// add postal address
831
ContentValues values = new ContentValues();
832
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
833
values.put( Contacts.ContactMethods.DATA, address );
834
values.put( Contacts.ContactMethods.TYPE, data.getType() );
835
_doit.getContentResolver().insert( contactContactMethodsUri,
823
_backend.addContactAddresses( id, address, data );
838
825
// and add this address to the cache to prevent a addition of
839
826
// duplicate date from another file
840
_contactsCache.addAssociatedAddress( id, address );
827
_contacts_cache.addAssociatedAddress( id, address );
844
831
private void importContactOrganisations( Long id,
845
832
HashMap< String, ContactData.ExtraDetail > datas )
833
throws ContactCreationException
848
Set< String > datasKeys = datas.keySet();
849
Iterator< String > i = datasKeys.iterator();
836
Set< String > datas_keys = datas.keySet();
837
Iterator< String > i = datas_keys.iterator();
850
838
while( i.hasNext() ) {
851
839
String organisation = i.next();
852
840
ContactData.ExtraDetail data = datas.get( organisation );
854
842
// we don't want to add this address if it exists already or we
855
843
// would introduce duplicates
856
if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
844
if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
859
847
// add organisation address
860
ContentValues values = new ContentValues();
861
values.put( Contacts.Organizations.PERSON_ID, id );
862
values.put( Contacts.Organizations.COMPANY, organisation );
863
values.put( Contacts.ContactMethods.TYPE,
864
Contacts.OrganizationColumns.TYPE_WORK );
865
if( data.getExtra() != null )
866
values.put( Contacts.Organizations.TITLE, data.getExtra() );
867
_doit.getContentResolver().insert(
868
Contacts.Organizations.CONTENT_URI, values );
848
_backend.addContactOrganisation( id, organisation, data );
870
850
// and add this address to the cache to prevent a addition of
871
851
// duplicate date from another file
872
_contactsCache.addAssociatedOrganisation( id, organisation );
852
_contacts_cache.addAssociatedOrganisation( id, organisation );