606
599
return _doit.getText( res );
610
* Should we skip a contact, given whether it exists or not and the current
611
* merge setting? This routine handles throwing up a prompt, if required.
612
* @param contact_detail the display name of the contact
613
* @param exists true if this contact matches one in the cache
614
* @param merge_setting the merge setting to use
615
* @return true if the contact should be skipped outright
616
* @throws AbortImportException
618
synchronized private boolean shouldWeSkipContact( String contact_detail,
619
boolean exists, int merge_setting ) throws AbortImportException
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
621
606
_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)
623
612
// handle special cases
624
613
switch( merge_setting )
626
615
case Doit.ACTION_KEEP:
627
// if we are skipping on a duplicate, check for one
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
630
619
case Doit.ACTION_PROMPT:
631
// if we are prompting on duplicate, then we can say that we won't
632
// skip if there isn't one
633
if( !exists ) return false;
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 ) )
635
// ok, duplicate exists, so do prompt
625
// ok, it exists, so do prompt
636
626
_doit._handler.sendMessage( Message.obtain( _doit._handler,
637
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
674
663
ContactsCache.CacheIdentifier cache_identifier =
675
664
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
677
672
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
678
673
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
680
678
// attempt to lookup the id of an existing contact in the cache with
681
679
// this contact data's cache identifier
682
680
Long id = (Long)_contacts_cache.lookup( cache_identifier );
684
// check to see if this contact should be skipped
685
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
682
// does contact exist already?
688
// show that we're skipping a contact
689
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
691
// remove from device
692
_backend.deleteContact( id );
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
693
// if a contact exists, and we're overwriting, destroy the existing
694
// contact before importing
695
boolean contact_deleted = false;
696
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
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
698
contact_deleted = true;
700
// remove from device
701
_backend.deleteContact( id );
712
// create a new contact
713
id = _backend.addContact( contact._name );
715
showError( R.string.error_unabletoaddcontact );
704
_contacts_cache.removeLookup( cache_identifier );
705
_contacts_cache.removeAssociatedData( id );
707
// show that we're overwriting a contact
708
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
710
// discard the contact id
715
// if we don't have a contact id yet (or we did, but we destroyed it
716
// when we deleted the contact), we'll have to create a new contact
719
// create a new contact
720
id = _backend.addContact( contact._name );
723
_contacts_cache.addLookup( cache_identifier, id );
725
// if we haven't already shown that we're overwriting a contact,
726
// show that we're creating a new contact
727
if( !contact_deleted )
728
_doit._handler.sendEmptyMessage(
729
Doit.MESSAGE_CONTACTCREATED );
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 );
732
// show that we're merging with an existing contact
733
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
735
// import contact parts
736
if( contact.hasNumbers() )
737
importContactPhones( id, contact.getNumbers() );
738
if( contact.hasEmails() )
739
importContactEmails( id, contact.getEmails() );
740
if( contact.hasAddresses() )
741
importContactAddresses( id, contact.getAddresses() );
742
if( contact.hasOrganisations() )
743
importContactOrganisations( id, contact.getOrganisations() );
745
catch( Backend.ContactCreationException e )
747
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() );
751
745
private void importContactPhones( Long id,
752
HashMap< String, ContactData.PreferredDetail > datas )
753
throws ContactCreationException
746
HashMap< String, ContactData.PreferredDetail > datas )
755
748
// add phone numbers
756
749
Set< String > datas_keys = datas.keySet();