127
@SuppressWarnings("serial")
128
protected class ContactNotIdentifiableException extends Exception
132
125
protected String _name = null;
133
126
protected String _primary_organisation = null;
134
protected boolean _primary_organisation_is_preferred;
127
protected boolean _primary_organisation_is_preferred = false;
135
128
protected String _primary_number = null;
136
protected int _primary_number_type;
137
protected boolean _primary_number_is_preferred;
129
protected boolean _primary_number_is_preferred = false;
138
130
protected String _primary_email = null;
139
protected boolean _primary_email_is_preferred;
131
protected boolean _primary_email_is_preferred = false;
140
132
protected HashMap< String, ExtraDetail > _organisations = null;
141
133
protected HashMap< String, PreferredDetail > _numbers = null;
142
134
protected HashMap< String, PreferredDetail > _emails = null;
143
135
protected HashMap< String, TypeDetail > _addresses = null;
144
protected HashSet< String > _notes = null;
146
private ContactsCache.CacheIdentifier _cache_identifier = null;
148
137
protected void setName( String name )
624
569
return _doit.getText( res );
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.
630
* @param contact_detail the display name of the contact
631
* @param exists true if this contact matches one in the cache
632
* @param merge_setting the merge setting to use
633
* @return true if the contact should be skipped outright
634
* @throws AbortImportException
636
synchronized private boolean shouldWeSkipContact( String contact_detail,
637
boolean exists, int merge_setting ) throws AbortImportException
572
protected boolean isImportRequired( ContactData contact )
573
throws AbortImportException, ContactNeedsMoreInfoException
576
return isImportRequired( contact, _merge_setting );
579
synchronized private boolean isImportRequired(
580
ContactData contact, int merge_setting )
581
throws AbortImportException, ContactNeedsMoreInfoException
639
583
_last_merge_decision = merge_setting;
585
// create a cache identifier which we can use to detect if this contact
586
// is valid for importing
587
ContactsCache.CacheIdentifier identifier =
588
ContactsCache.createIdentifier( contact );
589
if( identifier == null )
590
throw new ContactNeedsMoreInfoException();
641
592
// handle special cases
642
593
switch( merge_setting )
644
595
case Doit.ACTION_KEEP:
645
// if we are skipping on a duplicate, check for one
596
// if we keep contacts on duplicate, we better check for one
597
return !_contacts_cache.canLookup( identifier );
648
599
case Doit.ACTION_PROMPT:
649
// if we are prompting on duplicate, then we can say that we won't
650
// skip if there isn't one
651
if( !exists ) return false;
600
// if we are prompting on duplicate, we better check for one and if
601
// the contact doesn'te exist, we want to import it
602
if( !_contacts_cache.canLookup( identifier ) )
653
// ok, duplicate exists, so do prompt
605
// ok, it exists, so do prompt
654
606
_doit._handler.sendMessage( Message.obtain( _doit._handler,
655
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
607
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
689
// It is expected that we use contact.getCacheIdentifier() here. The
690
// contact we are passed should have been successfully finalise()d,
691
// which includes generating a valid cache identifier.
692
ContactsCache.CacheIdentifier cache_identifier =
693
contact.getCacheIdentifier();
695
640
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
696
641
// finish( ACTION_ABORT );
698
// attempt to lookup the id of an existing contact in the cache with
699
// this contact data's cache identifier
700
Long id = (Long)_contacts_cache.lookup( cache_identifier );
702
// check to see if this contact should be skipped
703
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
643
ContentValues values = new ContentValues();
644
boolean ui_informed = false;
647
// give the contact a chance to finalise it's data
650
// create something, from the contact data, that we can use to identify
651
// a cache entry and attempt to lookup the id of an existing contact in
653
ContactsCache.CacheIdentifier identifier =
654
ContactsCache.createIdentifier( contact );
655
if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
657
// does contact exist already?
706
// show that we're skipping a contact
707
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
660
// should we skip this import altogether?
661
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
664
Uri contact_uri = ContentUris.withAppendedId(
665
Contacts.People.CONTENT_URI, id );
667
// should we destroy the existing contact before importing?
668
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
670
// remove from device
671
_doit.getContentResolver().delete( contact_uri, null, null );
674
_contacts_cache.removeLookup( identifier );
675
_contacts_cache.removeAssociatedData( id );
677
// show that we're overwriting a contact
678
_doit._handler.sendEmptyMessage(
679
Doit.MESSAGE_CONTACTOVERWRITTEN );
682
// discard the contact id
711
// if a contact exists, and we're overwriting, destroy the existing
712
// contact before importing
713
boolean contact_deleted = false;
714
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
687
// if we don't have a contact id yet (or if we did, but we destroyed it
688
// when we deleted the contact), we'll have to create a new contact
716
contact_deleted = true;
691
// create a new contact
692
values.put( Contacts.People.NAME, contact._name );
693
Uri contact_uri = _doit.getContentResolver().insert(
694
Contacts.People.CONTENT_URI, values );
695
id = ContentUris.parseId( contact_uri );
696
if( id == null || id <= 0 )
697
showError( R.string.error_unabletoaddcontact );
718
// remove from device
719
_backend.deleteContact( id );
699
// try to add them to the "My Contacts" group
701
Contacts.People.addToMyContactsGroup(
702
_doit.getContentResolver(), id );
704
catch( IllegalStateException e ) {
705
// ignore any failure
722
_contacts_cache.removeLookup( cache_identifier );
723
_contacts_cache.removeAssociatedData( id );
725
// show that we're overwriting a contact
726
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
728
// discard the contact id
733
// if we don't have a contact id yet (or we did, but we destroyed it
734
// when we deleted the contact), we'll have to create a new contact
737
// create a new contact
738
id = _backend.addContact( contact._name );
741
_contacts_cache.addLookup( cache_identifier, id );
743
// if we haven't already shown that we're overwriting a contact,
744
// show that we're creating a new contact
745
if( !contact_deleted )
746
_doit._handler.sendEmptyMessage(
747
Doit.MESSAGE_CONTACTCREATED );
709
_contacts_cache.addLookup(
710
ContactsCache.createIdentifier( contact ), id );
712
// if we haven't already shown that we're overwriting a contact,
713
// show that we're creating a new contact
715
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
750
// show that we're merging with an existing contact
751
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
753
// import contact parts
754
if( contact.hasNumbers() )
755
importContactPhones( id, contact.getNumbers() );
756
if( contact.hasEmails() )
757
importContactEmails( id, contact.getEmails() );
758
if( contact.hasAddresses() )
759
importContactAddresses( id, contact.getAddresses() );
760
if( contact.hasOrganisations() )
761
importContactOrganisations( id, contact.getOrganisations() );
762
if( contact.hasNotes() )
763
importContactNotes( id, contact.getNotes() );
765
catch( Backend.ContactCreationException e )
767
showError( R.string.error_unabletoaddcontact );
720
// if we haven't already shown that we're overwriting or creating a
721
// contact show that we're merging a contact
723
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
725
// import contact parts
726
if( contact.hasNumbers() )
727
importContactPhones( id, contact.getNumbers() );
728
if( contact.hasEmails() )
729
importContactEmails( id, contact.getEmails() );
730
if( contact.hasAddresses() )
731
importContactAddresses( id, contact.getAddresses() );
732
if( contact.hasOrganisations() )
733
importContactOrganisations( id, contact.getOrganisations() );
771
736
private void importContactPhones( Long id,
772
HashMap< String, ContactData.PreferredDetail > datas )
773
throws ContactCreationException
737
HashMap< String, ContactData.PreferredDetail > datas )
739
// get URI to contact's phones
740
Uri contact_phones_uri = Uri.withAppendedPath(
741
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
742
Contacts.People.Phones.CONTENT_DIRECTORY );
743
Set< String > datas_keys = datas.keySet();
775
745
// add phone numbers
776
Set< String > datas_keys = datas.keySet();
777
746
Iterator< String > i = datas_keys.iterator();
778
747
while( i.hasNext() ) {
779
748
String number = i.next();