624
597
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.
631
* @param contact_detail the display name of the contact
632
* @param exists true if this contact matches one in the cache
633
* @param merge_setting the merge setting to use
634
* @return true if the contact should be skipped outright
635
* @throws AbortImportException
637
synchronized private boolean shouldWeSkipContact( String contact_detail,
638
boolean exists, int merge_setting ) throws AbortImportException
600
synchronized private boolean checkForDuplicate(
601
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
602
throws AbortImportException
640
604
_last_merge_decision = merge_setting;
606
// it is ok to use contact.getCacheIdentifier(). The contact has already
607
// been finalised, which means a valid cache identifier will have been
608
// created for it (or it would have been skipped)
642
610
// handle special cases
643
611
switch( merge_setting )
645
613
case Doit.ACTION_KEEP:
646
// if we are skipping on a duplicate, check for one
614
// if we keep contacts on duplicate, we better check for one
615
return !_contacts_cache.canLookup( cache_identifier );
649
617
case Doit.ACTION_PROMPT:
650
// if we are prompting on duplicate, then we can say that we won't
651
// skip if there isn't one
652
if( !exists ) return false;
618
// if we are prompting on duplicate, we better check for one and if
619
// the contact doesn'te exist, we want to import it
620
if( !_contacts_cache.canLookup( cache_identifier ) )
654
// ok, duplicate exists, so do prompt
623
// ok, it exists, so do prompt
655
624
_doit._handler.sendMessage( Message.obtain( _doit._handler,
656
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
625
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
690
// It is expected that we use contact.getCacheIdentifier() here. The
658
// It is expected that we use contact.getCacheIdentifier() here. The
691
659
// contact we are passed should have been successfully finalise()d,
692
660
// which includes generating a valid cache identifier.
693
661
ContactsCache.CacheIdentifier cache_identifier =
694
662
contact.getCacheIdentifier();
664
// check to see if this contact is a duplicate and should be skipped
665
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
696
670
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
697
671
// finish( ACTION_ABORT );
673
// keep track of whether we've informed the UI of what we're doing
674
boolean ui_informed = false;
699
676
// attempt to lookup the id of an existing contact in the cache with
700
677
// this contact data's cache identifier
701
678
Long id = (Long)_contacts_cache.lookup( cache_identifier );
703
// check to see if this contact should be skipped
704
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
680
// does contact exist already?
707
// show that we're skipping a contact
708
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
683
// should we skip this import altogether?
684
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
Uri contact_uri = ContentUris.withAppendedId(
688
Contacts.People.CONTENT_URI, id );
690
// should we destroy the existing contact before importing?
691
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
693
// remove from device
694
_doit.getContentResolver().delete( contact_uri, null, null );
697
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
698
_contacts_cache.removeAssociatedData( id );
700
// show that we're overwriting a contact
701
_doit._handler.sendEmptyMessage(
702
Doit.MESSAGE_CONTACTOVERWRITTEN );
705
// discard the contact id
712
// if a contact exists, and we're overwriting, destroy the existing
713
// contact before importing
714
boolean contact_deleted = false;
715
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
710
// if we don't have a contact id yet (or if we did, but we destroyed it
711
// when we deleted the contact), we'll have to create a new contact
717
contact_deleted = true;
714
// create a new contact
715
ContentValues values = new ContentValues();
716
values.put( Contacts.People.NAME, contact._name );
717
Uri contact_uri = _doit.getContentResolver().insert(
718
Contacts.People.CONTENT_URI, values );
719
id = ContentUris.parseId( contact_uri );
720
if( id == null || id <= 0 )
721
showError( R.string.error_unabletoaddcontact );
719
// remove from device
720
_backend.deleteContact( id );
723
// try to add them to the "My Contacts" group
725
Contacts.People.addToMyContactsGroup(
726
_doit.getContentResolver(), id );
728
catch( IllegalStateException e ) {
729
// ignore any failure
723
_contacts_cache.removeLookup( cache_identifier );
724
_contacts_cache.removeAssociatedData( id );
726
// show that we're overwriting a contact
727
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
729
// discard the contact id
734
// if we don't have a contact id yet (or we did, but we destroyed it
735
// when we deleted the contact), we'll have to create a new contact
738
// create a new contact
739
id = _backend.addContact( contact._name );
742
_contacts_cache.addLookup( cache_identifier, id );
744
// if we haven't already shown that we're overwriting a contact,
745
// show that we're creating a new contact
746
if( !contact_deleted )
747
_doit._handler.sendEmptyMessage(
748
Doit.MESSAGE_CONTACTCREATED );
733
_contacts_cache.addLookup(
734
ContactsCache.createIdentifier( contact ), id );
736
// if we haven't already shown that we're overwriting a contact,
737
// show that we're creating a new contact
739
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
751
// show that we're merging with an existing contact
752
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
754
// import contact parts
755
if( contact.hasNumbers() )
756
importContactPhones( id, contact.getNumbers() );
757
if( contact.hasEmails() )
758
importContactEmails( id, contact.getEmails() );
759
if( contact.hasAddresses() )
760
importContactAddresses( id, contact.getAddresses() );
761
if( contact.hasOrganisations() )
762
importContactOrganisations( id, contact.getOrganisations() );
763
if( contact.hasNotes() )
764
importContactNotes( id, contact.getNotes() );
766
catch( Backend.ContactCreationException e )
768
showError( R.string.error_unabletoaddcontact );
744
// if we haven't already shown that we're overwriting or creating a
745
// contact, show that we're merging a contact
747
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
749
// import contact parts
750
if( contact.hasNumbers() )
751
importContactPhones( id, contact.getNumbers() );
752
if( contact.hasEmails() )
753
importContactEmails( id, contact.getEmails() );
754
if( contact.hasAddresses() )
755
importContactAddresses( id, contact.getAddresses() );
756
if( contact.hasOrganisations() )
757
importContactOrganisations( id, contact.getOrganisations() );
772
760
private void importContactPhones( Long id,
773
HashMap< String, ContactData.PreferredDetail > datas )
774
throws ContactCreationException
761
HashMap< String, ContactData.PreferredDetail > datas )
763
// get URI to contact's phones
764
Uri contact_phones_uri = Uri.withAppendedPath(
765
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
766
Contacts.People.Phones.CONTENT_DIRECTORY );
767
Set< String > datas_keys = datas.keySet();
776
769
// add phone numbers
777
Set< String > datas_keys = datas.keySet();
778
770
Iterator< String > i = datas_keys.iterator();
779
771
while( i.hasNext() ) {
780
772
String number = i.next();
781
773
ContactData.PreferredDetail data = datas.get( number );
783
// We don't want to add this number if it's crap, or it already
784
// exists (which would cause a duplicate to be created). We don't
785
// take in to account the type when checking for duplicates. This
786
// is intentional: types aren't really very reliable. We assume
787
// that if the number exists at all, it doesn't need importing.
788
// Because of this, we also can't update the cache (which we don't
789
// need to anyway, so it's not a problem).
775
// we don't want to add this number if it's crap, or it already
776
// exists (which would cause a duplicate to be created). We don't
777
// take in to account the type when checking for duplicates. This is
778
// intentional: types aren't really very reliable. We assume that
779
// if the number exists at all, it doesn't need importing. Because
780
// of this, we also can't update the cache (which we don't need to
781
// anyway, so it's not a problem).
790
782
if( _contacts_cache.hasAssociatedNumber( id, number ) )
793
785
// add phone number
794
_backend.addContactPhone( id, number, data );
786
ContentValues values = new ContentValues();
787
values.put( Contacts.Phones.TYPE, data.getType() );
788
values.put( Contacts.Phones.NUMBER, number );
789
if( data.isPreferred() )
790
values.put( Contacts.Phones.ISPRIMARY, 1 );
791
_doit.getContentResolver().insert( contact_phones_uri, values );
796
793
// and add this address to the cache to prevent a addition of
797
794
// duplicate date from another file
802
799
private void importContactEmails( Long id,
803
HashMap< String, ContactData.PreferredDetail > datas )
804
throws ContactCreationException
800
HashMap< String, ContactData.PreferredDetail > datas )
802
// get URI to contact's contact methods
803
Uri contact_contact_methods_uri = Uri.withAppendedPath(
804
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
805
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
806
Set< String > datas_keys = datas.keySet();
806
808
// add email addresses
807
Set< String > datas_keys = datas.keySet();
808
809
Iterator< String > i = datas_keys.iterator();
809
810
while( i.hasNext() ) {
810
811
String email = i.next();
811
812
ContactData.PreferredDetail data = datas.get( email );
813
814
// we don't want to add this email address if it exists already or
814
// we would introduce duplicates
815
// we would introduce duplicates.
815
816
if( _contacts_cache.hasAssociatedEmail( id, email ) )
818
819
// add phone number
819
_backend.addContactEmail( id, email, data );
820
ContentValues values = new ContentValues();
821
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
822
values.put( Contacts.ContactMethods.DATA, email );
823
values.put( Contacts.ContactMethods.TYPE, data.getType() );
824
if( data.isPreferred() )
825
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
826
_doit.getContentResolver().insert( contact_contact_methods_uri,
821
829
// and add this address to the cache to prevent a addition of
822
830
// duplicate date from another file