127
@SuppressWarnings("serial")
128
protected class ContactNotIdentifiableException extends Exception
125
132
protected String _name = null;
126
133
protected String _primary_organisation = null;
127
protected boolean _primary_organisation_is_preferred = false;
134
protected boolean _primary_organisation_is_preferred;
128
135
protected String _primary_number = null;
129
protected boolean _primary_number_is_preferred = false;
136
protected int _primary_number_type;
137
protected boolean _primary_number_is_preferred;
130
138
protected String _primary_email = null;
131
protected boolean _primary_email_is_preferred = false;
139
protected boolean _primary_email_is_preferred;
132
140
protected HashMap< String, ExtraDetail > _organisations = null;
133
141
protected HashMap< String, PreferredDetail > _numbers = null;
134
142
protected HashMap< String, PreferredDetail > _emails = null;
135
143
protected HashMap< String, TypeDetail > _addresses = null;
145
private ContactsCache.CacheIdentifier _cache_identifier = null;
137
147
protected void setName( String name )
569
606
return _doit.getText( res );
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
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
583
621
_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();
592
623
// handle special cases
593
624
switch( merge_setting )
595
626
case Doit.ACTION_KEEP:
596
// if we keep contacts on duplicate, we better check for one
597
return !_contacts_cache.canLookup( identifier );
627
// if we are skipping on a duplicate, check for one
599
630
case Doit.ACTION_PROMPT:
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 ) )
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;
605
// ok, it exists, so do prompt
635
// ok, duplicate exists, so do prompt
606
636
_doit._handler.sendMessage( Message.obtain( _doit._handler,
607
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
637
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
671
// It is expected that we use contact.getCacheIdentifier() here. The
672
// contact we are passed should have been successfully finalise()d,
673
// which includes generating a valid cache identifier.
674
ContactsCache.CacheIdentifier cache_identifier =
675
contact.getCacheIdentifier();
640
677
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
678
// finish( ACTION_ABORT );
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?
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 )
680
// attempt to lookup the id of an existing contact in the cache with
681
// this contact data's cache identifier
682
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,
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
670
// remove from device
671
_doit.getContentResolver().delete( contact_uri, null, null );
719
// create a new contact
720
id = _backend.addContact( contact._name );
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
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() );
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
745
catch( Backend.ContactCreationException e )
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 );
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
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 );
747
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() );
736
751
private void importContactPhones( Long id,
737
HashMap< String, ContactData.PreferredDetail > datas )
752
HashMap< String, ContactData.PreferredDetail > datas )
753
throws ContactCreationException
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();
745
755
// add phone numbers
756
Set< String > datas_keys = datas.keySet();
746
757
Iterator< String > i = datas_keys.iterator();
747
758
while( i.hasNext() ) {
748
759
String number = i.next();