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();
639
712
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
640
713
// 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 )
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
669
// remove from device
670
_doit.getContentResolver().delete( contactUri, null, null );
754
// create a new contact
755
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
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() );
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
784
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 );
786
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
790
private void importContactPhones( Long id,
736
HashMap< String, ContactData.PreferredDetail > datas )
791
HashMap< String, ContactData.PreferredDetail > datas )
792
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
794
// add phone numbers
745
Iterator< String > i = datasKeys.iterator();
795
Set< String > datas_keys = datas.keySet();
796
Iterator< String > i = datas_keys.iterator();
746
797
while( i.hasNext() ) {
747
798
String number = i.next();
748
799
ContactData.PreferredDetail data = datas.get( number );
750
// we don't want to add this number if it's crap, or it already
751
// exists (which would cause a duplicate to be created). We don't
752
// take in to account the type when checking for duplicates. This is
753
// intentional: types aren't really very reliable. We assume that
754
// if the number exists at all, it doesn't need importing. Because
755
// of this, we also can't update the cache (which we don't need to
756
// anyway, so it's not a problem).
757
if( _contactsCache.hasAssociatedNumber( id, 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).
808
if( _contacts_cache.hasAssociatedNumber( id, number ) )
760
811
// 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 );
812
_backend.addContactPhone( id, number, data );
768
814
// and add this address to the cache to prevent a addition of
769
815
// duplicate date from another file
770
_contactsCache.addAssociatedNumber( id, number );
816
_contacts_cache.addAssociatedNumber( id, number );
774
820
private void importContactEmails( Long id,
775
HashMap< String, ContactData.PreferredDetail > datas )
821
HashMap< String, ContactData.PreferredDetail > datas )
822
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
824
// add email addresses
784
Iterator< String > i = datasKeys.iterator();
825
Set< String > datas_keys = datas.keySet();
826
Iterator< String > i = datas_keys.iterator();
785
827
while( i.hasNext() ) {
786
828
String email = i.next();
787
829
ContactData.PreferredDetail data = datas.get( email );
789
831
// we don't want to add this email address if it exists already or
790
// we would introduce duplicates.
791
if( _contactsCache.hasAssociatedEmail( id, email ) )
832
// we would introduce duplicates
833
if( _contacts_cache.hasAssociatedEmail( id, email ) )
794
836
// 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,
837
_backend.addContactEmail( id, email, data );
804
839
// and add this address to the cache to prevent a addition of
805
840
// duplicate date from another file
806
_contactsCache.addAssociatedEmail( id, email );
841
_contacts_cache.addAssociatedEmail( id, email );
810
845
private void importContactAddresses( Long id,
811
846
HashMap< String, ContactData.TypeDetail > datas )
847
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();
850
Set< String > datas_keys = datas.keySet();
851
Iterator< String > i = datas_keys.iterator();
821
852
while( i.hasNext() ) {
822
853
String address = i.next();
823
854
ContactData.TypeDetail data = datas.get( address );
825
856
// we don't want to add this address if it exists already or we
826
857
// would introduce duplicates
827
if( _contactsCache.hasAssociatedAddress( id, address ) )
858
if( _contacts_cache.hasAssociatedAddress( id, address ) )
830
861
// 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,
862
_backend.addContactAddresses( id, address, data );
838
864
// and add this address to the cache to prevent a addition of
839
865
// duplicate date from another file
840
_contactsCache.addAssociatedAddress( id, address );
866
_contacts_cache.addAssociatedAddress( id, address );
844
870
private void importContactOrganisations( Long id,
845
871
HashMap< String, ContactData.ExtraDetail > datas )
872
throws ContactCreationException
848
Set< String > datasKeys = datas.keySet();
849
Iterator< String > i = datasKeys.iterator();
875
Set< String > datas_keys = datas.keySet();
876
Iterator< String > i = datas_keys.iterator();
850
877
while( i.hasNext() ) {
851
878
String organisation = i.next();
852
879
ContactData.ExtraDetail data = datas.get( organisation );
854
881
// we don't want to add this address if it exists already or we
855
882
// would introduce duplicates
856
if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
883
if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
859
886
// 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 );
887
_backend.addContactOrganisation( id, organisation, data );
870
889
// and add this address to the cache to prevent a addition of
871
890
// duplicate date from another file
872
_contactsCache.addAssociatedOrganisation( id, organisation );
891
_contacts_cache.addAssociatedOrganisation( id, organisation );
895
private void importContactNotes( Long id, HashSet< String > datas )
896
throws ContactCreationException
899
Iterator< String > i = datas.iterator();
900
while( i.hasNext() ) {
901
String note = i.next();
903
// we don't want to add this note if it exists already or we would
904
// introduce duplicates
905
if( _contacts_cache.hasAssociatedNote( id, note ) )
909
_backend.addContactNote( id, note );
911
// and add this note to the cache to prevent a addition of duplicate
912
// date from another file
913
_contacts_cache.addAssociatedNote( id, note );
917
private void importContactBirthday( Long id, String birthday )
918
throws ContactCreationException
920
// we don't want to import this birthday if it already exists
921
if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
925
_backend.addContactBirthday( id, birthday );
927
// and update the cache
928
_contacts_cache.addAssociatedBirthday( id, birthday );
876
931
synchronized protected void checkAbort() throws AbortImportException