624
569
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
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
640
583
_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();
642
592
// handle special cases
643
593
switch( merge_setting )
645
595
case Doit.ACTION_KEEP:
646
// if we are skipping on a duplicate, check for one
596
// if we keep contacts on duplicate, we better check for one
597
return !_contacts_cache.canLookup( identifier );
649
599
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;
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 ) )
654
// ok, duplicate exists, so do prompt
605
// ok, it exists, so do prompt
655
606
_doit._handler.sendMessage( Message.obtain( _doit._handler,
656
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
607
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
690
// It is expected that we use contact.getCacheIdentifier() here. The
691
// contact we are passed should have been successfully finalise()d,
692
// which includes generating a valid cache identifier.
693
ContactsCache.CacheIdentifier cache_identifier =
694
contact.getCacheIdentifier();
696
640
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
697
641
// finish( ACTION_ABORT );
699
// attempt to lookup the id of an existing contact in the cache with
700
// this contact data's cache identifier
701
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,
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?
707
// show that we're skipping a contact
708
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
670
// remove from device
671
_doit.getContentResolver().delete( contact_uri, null, null );
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
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 )
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
717
contact_deleted = true;
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 );
719
// remove from device
720
_backend.deleteContact( id );
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
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 );
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 );
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 );
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() );
772
736
private void importContactPhones( Long id,
773
HashMap< String, ContactData.PreferredDetail > datas )
774
throws ContactCreationException
737
HashMap< String, ContactData.PreferredDetail > datas )
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();
776
745
// add phone numbers
777
Set< String > datas_keys = datas.keySet();
778
746
Iterator< String > i = datas_keys.iterator();
779
747
while( i.hasNext() ) {
780
748
String number = i.next();
781
749
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).
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).
790
758
if( _contacts_cache.hasAssociatedNumber( id, number ) )
793
761
// add phone number
794
_backend.addContactPhone( id, number, data );
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 );
796
769
// and add this address to the cache to prevent a addition of
797
770
// duplicate date from another file
802
775
private void importContactEmails( Long id,
803
HashMap< String, ContactData.PreferredDetail > datas )
804
throws ContactCreationException
776
HashMap< String, ContactData.PreferredDetail > datas )
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();
806
784
// add email addresses
807
Set< String > datas_keys = datas.keySet();
808
785
Iterator< String > i = datas_keys.iterator();
809
786
while( i.hasNext() ) {
810
787
String email = i.next();
811
788
ContactData.PreferredDetail data = datas.get( email );
813
790
// we don't want to add this email address if it exists already or
814
// we would introduce duplicates
791
// we would introduce duplicates.
815
792
if( _contacts_cache.hasAssociatedEmail( id, email ) )
818
795
// add phone number
819
_backend.addContactEmail( id, email, data );
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,
821
805
// and add this address to the cache to prevent a addition of
822
806
// duplicate date from another file