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