599
624
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
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
606
640
_last_merge_decision = merge_setting;
608
// it is ok to use contact.getCacheIdentifier(). The contact has already
609
// been finalised, which means a valid cache identifier will have been
610
// created for it (or it would have been skipped)
612
642
// handle special cases
613
643
switch( merge_setting )
615
645
case Doit.ACTION_KEEP:
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
646
// if we are skipping on a duplicate, check for one
619
649
case Doit.ACTION_PROMPT:
620
// if we are prompting on duplicate, we better check for one and if
621
// the contact doesn'te exist, we want to import it
622
if( !_contacts_cache.canLookup( cache_identifier ) )
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;
625
// ok, it exists, so do prompt
654
// ok, duplicate exists, so do prompt
626
655
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
656
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
660
// It is expected that we use contact.getCacheIdentifier() here. The
690
// It is expected that we use contact.getCacheIdentifier() here. The
661
691
// contact we are passed should have been successfully finalise()d,
662
692
// which includes generating a valid cache identifier.
663
693
ContactsCache.CacheIdentifier cache_identifier =
664
694
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
672
696
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
697
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
678
699
// attempt to lookup the id of an existing contact in the cache with
679
700
// this contact data's cache identifier
680
701
Long id = (Long)_contacts_cache.lookup( cache_identifier );
682
// does contact exist already?
685
// should we skip this import altogether?
686
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
688
// should we destroy the existing contact before importing?
689
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
703
// check to see if this contact should be skipped
704
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
707
// show that we're skipping a contact
708
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
717
contact_deleted = true;
719
// remove from device
720
_backend.deleteContact( id );
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
691
// remove from device
692
_backend.deleteContact( id );
738
// create a new contact
739
id = _backend.addContact( contact._name );
695
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
696
_contacts_cache.removeAssociatedData( id );
698
// show that we're overwriting a contact
699
_doit._handler.sendEmptyMessage(
700
Doit.MESSAGE_CONTACTOVERWRITTEN );
703
// discard the contact id
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 );
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() );
708
// if we don't have a contact id yet (or we did, but we destroyed it
709
// when we deleted the contact), we'll have to create a new contact
766
catch( Backend.ContactCreationException e )
712
// create a new contact
713
id = _backend.addContact( contact._name );
715
showError( R.string.error_unabletoaddcontact );
718
_contacts_cache.addLookup(
719
ContactsCache.createIdentifier( contact ), id );
721
// if we haven't already shown that we're overwriting a contact,
722
// show that we're creating a new contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
768
showError( R.string.error_unabletoaddcontact );
729
// if we haven't already shown that we're overwriting or creating a
730
// contact, show that we're merging a contact
732
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
734
// import contact parts
735
if( contact.hasNumbers() )
736
importContactPhones( id, contact.getNumbers() );
737
if( contact.hasEmails() )
738
importContactEmails( id, contact.getEmails() );
739
if( contact.hasAddresses() )
740
importContactAddresses( id, contact.getAddresses() );
741
if( contact.hasOrganisations() )
742
importContactOrganisations( id, contact.getOrganisations() );
745
772
private void importContactPhones( Long id,
746
HashMap< String, ContactData.PreferredDetail > datas )
773
HashMap< String, ContactData.PreferredDetail > datas )
774
throws ContactCreationException
748
776
// add phone numbers
749
777
Set< String > datas_keys = datas.keySet();
752
780
String number = i.next();
753
781
ContactData.PreferredDetail data = datas.get( number );
755
// we don't want to add this number if it's crap, or it already
756
// exists (which would cause a duplicate to be created). We don't
757
// take in to account the type when checking for duplicates. This is
758
// intentional: types aren't really very reliable. We assume that
759
// if the number exists at all, it doesn't need importing. Because
760
// of this, we also can't update the cache (which we don't need to
761
// anyway, so it's not a problem).
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).
762
790
if( _contacts_cache.hasAssociatedNumber( id, number ) )