599
606
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
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
606
621
_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
623
// handle special cases
613
624
switch( merge_setting )
615
626
case Doit.ACTION_KEEP:
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
627
// if we are skipping on a duplicate, check for one
619
630
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 ) )
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;
625
// ok, it exists, so do prompt
635
// ok, duplicate exists, so do prompt
626
636
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
637
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
663
674
ContactsCache.CacheIdentifier cache_identifier =
664
675
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
672
677
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
678
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
678
680
// attempt to lookup the id of an existing contact in the cache with
679
681
// this contact data's cache identifier
680
682
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 )
684
// check to see if this contact should be skipped
685
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
688
// show that we're skipping a contact
689
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
698
contact_deleted = true;
700
// remove from device
701
_backend.deleteContact( id );
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
691
// remove from device
692
_backend.deleteContact( id );
719
// create a new contact
720
id = _backend.addContact( contact._name );
695
_contacts_cache.removeLookup( cache_identifier );
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
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 );
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() );
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
745
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( cache_identifier, id );
720
// if we haven't already shown that we're overwriting a contact,
721
// show that we're creating a new contact
723
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
747
showError( R.string.error_unabletoaddcontact );
728
// if we haven't already shown that we're overwriting or creating a
729
// contact, show that we're merging a contact
731
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
733
// import contact parts
734
if( contact.hasNumbers() )
735
importContactPhones( id, contact.getNumbers() );
736
if( contact.hasEmails() )
737
importContactEmails( id, contact.getEmails() );
738
if( contact.hasAddresses() )
739
importContactAddresses( id, contact.getAddresses() );
740
if( contact.hasOrganisations() )
741
importContactOrganisations( id, contact.getOrganisations() );
744
751
private void importContactPhones( Long id,
745
HashMap< String, ContactData.PreferredDetail > datas )
752
HashMap< String, ContactData.PreferredDetail > datas )
753
throws ContactCreationException
747
755
// add phone numbers
748
756
Set< String > datas_keys = datas.keySet();