624
568
return _doit.getText( res );
628
* Should we skip a contact, given whether it exists or not and the current
629
* merge setting? This routine handles throwing up a prompt, if required.
630
* @param contact_detail the display name of the contact
631
* @param exists true if this contact matches one in the cache
632
* @param merge_setting the merge setting to use
633
* @return true if the contact should be skipped outright
634
* @throws AbortImportException
636
synchronized private boolean shouldWeSkipContact( String contact_detail,
637
boolean exists, int merge_setting ) throws AbortImportException
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
639
582
_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();
641
591
// handle special cases
642
592
switch( merge_setting )
644
594
case Doit.ACTION_KEEP:
645
// if we are skipping on a duplicate, check for one
595
// if we keep contacts on duplicate, we better check for one
596
return !_contactsCache.canLookup( identifier );
648
598
case Doit.ACTION_PROMPT:
649
// if we are prompting on duplicate, then we can say that we won't
650
// skip if there isn't one
651
if( !exists ) return false;
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 ) )
653
// ok, duplicate exists, so do prompt
604
// ok, it exists, so do prompt
654
605
_doit._handler.sendMessage( Message.obtain( _doit._handler,
655
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
606
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
689
// It is expected that we use contact.getCacheIdentifier() here. The
690
// contact we are passed should have been successfully finalise()d,
691
// which includes generating a valid cache identifier.
692
ContactsCache.CacheIdentifier cache_identifier =
693
contact.getCacheIdentifier();
695
639
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
696
640
// finish( ACTION_ABORT );
698
// attempt to lookup the id of an existing contact in the cache with
699
// this contact data's cache identifier
700
Long id = (Long)_contacts_cache.lookup( cache_identifier );
702
// check to see if this contact should be skipped
703
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
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?
706
// show that we're skipping a contact
707
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
669
// remove from device
670
_doit.getContentResolver().delete( contactUri, null, null );
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
711
// if a contact exists, and we're overwriting, destroy the existing
712
// contact before importing
713
boolean contact_deleted = false;
714
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
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
716
contact_deleted = true;
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 );
718
// remove from device
719
_backend.deleteContact( id );
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
722
_contacts_cache.removeLookup( cache_identifier );
723
_contacts_cache.removeAssociatedData( id );
725
// show that we're overwriting a contact
726
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
728
// discard the contact id
733
// if we don't have a contact id yet (or we did, but we destroyed it
734
// when we deleted the contact), we'll have to create a new contact
737
// create a new contact
738
id = _backend.addContact( contact._name );
741
_contacts_cache.addLookup( cache_identifier, id );
743
// if we haven't already shown that we're overwriting a contact,
744
// show that we're creating a new contact
745
if( !contact_deleted )
746
_doit._handler.sendEmptyMessage(
747
Doit.MESSAGE_CONTACTCREATED );
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 );
750
// show that we're merging with an existing contact
751
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
753
// import contact parts
754
if( contact.hasNumbers() )
755
importContactPhones( id, contact.getNumbers() );
756
if( contact.hasEmails() )
757
importContactEmails( id, contact.getEmails() );
758
if( contact.hasAddresses() )
759
importContactAddresses( id, contact.getAddresses() );
760
if( contact.hasOrganisations() )
761
importContactOrganisations( id, contact.getOrganisations() );
762
if( contact.hasNotes() )
763
importContactNotes( id, contact.getNotes() );
765
catch( Backend.ContactCreationException e )
767
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() );
771
735
private void importContactPhones( Long id,
772
HashMap< String, ContactData.PreferredDetail > datas )
773
throws ContactCreationException
736
HashMap< String, ContactData.PreferredDetail > datas )
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();
775
744
// add phone numbers
776
Set< String > datas_keys = datas.keySet();
777
Iterator< String > i = datas_keys.iterator();
745
Iterator< String > i = datasKeys.iterator();
778
746
while( i.hasNext() ) {
779
747
String number = i.next();
780
748
ContactData.PreferredDetail data = datas.get( number );
786
754
// if the number exists at all, it doesn't need importing. Because
787
755
// of this, we also can't update the cache (which we don't need to
788
756
// anyway, so it's not a problem).
789
if( _contacts_cache.hasAssociatedNumber( id, number ) )
757
if( _contactsCache.hasAssociatedNumber( id, number ) )
792
760
// add phone number
793
_backend.addContactPhone( id, number, data );
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 );
795
768
// and add this address to the cache to prevent a addition of
796
769
// duplicate date from another file
797
_contacts_cache.addAssociatedNumber( id, number );
770
_contactsCache.addAssociatedNumber( id, number );
801
774
private void importContactEmails( Long id,
802
HashMap< String, ContactData.PreferredDetail > datas )
803
throws ContactCreationException
775
HashMap< String, ContactData.PreferredDetail > datas )
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();
805
783
// add email addresses
806
Set< String > datas_keys = datas.keySet();
807
Iterator< String > i = datas_keys.iterator();
784
Iterator< String > i = datasKeys.iterator();
808
785
while( i.hasNext() ) {
809
786
String email = i.next();
810
787
ContactData.PreferredDetail data = datas.get( email );
812
789
// we don't want to add this email address if it exists already or
813
790
// we would introduce duplicates.
814
if( _contacts_cache.hasAssociatedEmail( id, email ) )
791
if( _contactsCache.hasAssociatedEmail( id, email ) )
817
794
// add phone number
818
_backend.addContactEmail( id, email, data );
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,
820
804
// and add this address to the cache to prevent a addition of
821
805
// duplicate date from another file
822
_contacts_cache.addAssociatedEmail( id, email );
806
_contactsCache.addAssociatedEmail( id, email );
826
810
private void importContactAddresses( Long id,
827
811
HashMap< String, ContactData.TypeDetail > datas )
828
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 );
831
Set< String > datas_keys = datas.keySet();
832
Iterator< String > i = datas_keys.iterator();
819
Set< String > datasKeys = datas.keySet();
820
Iterator< String > i = datasKeys.iterator();
833
821
while( i.hasNext() ) {
834
822
String address = i.next();
835
823
ContactData.TypeDetail data = datas.get( address );
837
825
// we don't want to add this address if it exists already or we
838
826
// would introduce duplicates
839
if( _contacts_cache.hasAssociatedAddress( id, address ) )
827
if( _contactsCache.hasAssociatedAddress( id, address ) )
842
830
// add postal address
843
_backend.addContactAddresses( id, address, data );
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,
845
838
// and add this address to the cache to prevent a addition of
846
839
// duplicate date from another file
847
_contacts_cache.addAssociatedAddress( id, address );
840
_contactsCache.addAssociatedAddress( id, address );
851
844
private void importContactOrganisations( Long id,
852
845
HashMap< String, ContactData.ExtraDetail > datas )
853
throws ContactCreationException
856
Set< String > datas_keys = datas.keySet();
857
Iterator< String > i = datas_keys.iterator();
848
Set< String > datasKeys = datas.keySet();
849
Iterator< String > i = datasKeys.iterator();
858
850
while( i.hasNext() ) {
859
851
String organisation = i.next();
860
852
ContactData.ExtraDetail data = datas.get( organisation );
862
854
// we don't want to add this address if it exists already or we
863
855
// would introduce duplicates
864
if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
856
if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
867
859
// add organisation address
868
_backend.addContactOrganisation( id, organisation, data );
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 );
870
870
// and add this address to the cache to prevent a addition of
871
871
// duplicate date from another file
872
_contacts_cache.addAssociatedOrganisation( id, organisation );
876
private void importContactNotes( Long id,
877
HashSet< String > datas )
878
throws ContactCreationException
881
Iterator< String > i = datas.iterator();
882
while( i.hasNext() ) {
883
String note = i.next();
885
// we don't want to add this note if it exists already or we would
886
// introduce duplicates
887
if( _contacts_cache.hasAssociatedNote( id, note ) )
891
_backend.addContactNote( id, note );
893
// and add this note to the cache to prevent a addition of duplicate
894
// date from another file
895
_contacts_cache.addAssociatedNote( id, note );
872
_contactsCache.addAssociatedOrganisation( id, organisation );
900
876
synchronized protected void checkAbort() throws AbortImportException