597
606
return _doit.getText( res );
600
synchronized private boolean checkForDuplicate(
601
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
602
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
604
621
_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)
610
623
// handle special cases
611
624
switch( merge_setting )
613
626
case Doit.ACTION_KEEP:
614
// if we keep contacts on duplicate, we better check for one
615
return !_contacts_cache.canLookup( cache_identifier );
627
// if we are skipping on a duplicate, check for one
617
630
case Doit.ACTION_PROMPT:
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 ) )
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;
623
// ok, it exists, so do prompt
635
// ok, duplicate exists, so do prompt
624
636
_doit._handler.sendMessage( Message.obtain( _doit._handler,
625
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
637
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
661
674
ContactsCache.CacheIdentifier cache_identifier =
662
675
contact.getCacheIdentifier();
664
// check to see if this contact is a duplicate and should be skipped
665
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
670
677
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
671
678
// finish( ACTION_ABORT );
673
// keep track of whether we've informed the UI of what we're doing
674
boolean ui_informed = false;
676
680
// attempt to lookup the id of an existing contact in the cache with
677
681
// this contact data's cache identifier
678
682
Long id = (Long)_contacts_cache.lookup( cache_identifier );
680
// does contact exist already?
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 )
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
693
// remove from device
694
_doit.getContentResolver().delete( contact_uri, null, null );
719
// create a new contact
720
id = _backend.addContact( contact._name );
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
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() );
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
745
catch( Backend.ContactCreationException e )
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 );
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
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 );
747
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() );
760
751
private void importContactPhones( Long id,
761
HashMap< String, ContactData.PreferredDetail > datas )
752
HashMap< String, ContactData.PreferredDetail > datas )
753
throws ContactCreationException
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();
769
755
// add phone numbers
756
Set< String > datas_keys = datas.keySet();
770
757
Iterator< String > i = datas_keys.iterator();
771
758
while( i.hasNext() ) {
772
759
String number = i.next();