337
346
return _addresses;
349
protected void addNote( String note )
351
if( _notes == null ) _notes = new HashSet< String >();
352
if( !_notes.contains( note ) )
356
public boolean hasNotes()
358
return _notes != null && _notes.size() > 0;
361
public HashSet< String > getNotes()
366
public void setBirthday( String birthday )
368
_birthday = birthday;
371
public boolean hasBirthday()
373
return _birthday != null;
376
public String getBirthday()
340
381
protected void finalise()
341
382
throws ContactNotIdentifiableException
343
// ensure that if there is a primary number, it is preferred so
344
// that there is always one preferred number. Android will assign
384
// Ensure that if there is a primary number, it is preferred so
385
// that there is always one preferred number. Android will assign
345
386
// preference to one anyway so we might as well decide one sensibly.
346
387
if( _primary_number != null ) {
347
388
PreferredDetail data = _numbers.get( _primary_number );
599
640
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
644
* Should we skip a contact, given whether it exists or not and the current
645
* merge setting? This routine handles throwing up a prompt, if required.
647
* @param contact_detail the display name of the contact
648
* @param exists true if this contact matches one in the cache
649
* @param merge_setting the merge setting to use
650
* @return true if the contact should be skipped outright
651
* @throws AbortImportException
653
synchronized private boolean shouldWeSkipContact( String contact_detail,
654
boolean exists, int merge_setting ) throws AbortImportException
606
656
_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
658
// handle special cases
613
659
switch( merge_setting )
615
661
case Doit.ACTION_KEEP:
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
662
// if we are skipping on a duplicate, check for one
619
665
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 ) )
666
// if we are prompting on duplicate, then we can say that we won't
667
// skip if there isn't one
668
if( !exists ) return false;
625
// ok, it exists, so do prompt
670
// ok, duplicate exists, so do prompt
626
671
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
672
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
660
// It is expected that we use contact.getCacheIdentifier() here. The
706
// It is expected that we use contact.getCacheIdentifier() here. The
661
707
// contact we are passed should have been successfully finalise()d,
662
708
// which includes generating a valid cache identifier.
663
709
ContactsCache.CacheIdentifier cache_identifier =
664
710
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
672
712
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
713
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
678
715
// attempt to lookup the id of an existing contact in the cache with
679
716
// this contact data's cache identifier
680
717
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 )
719
// check to see if this contact should be skipped
720
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
723
// show that we're skipping a contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
728
// if a contact exists, and we're overwriting, destroy the existing
729
// contact before importing
730
boolean contact_deleted = false;
731
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
733
contact_deleted = true;
735
// remove from device
736
_backend.deleteContact( id );
739
_contacts_cache.removeLookup( cache_identifier );
740
_contacts_cache.removeAssociatedData( id );
742
// show that we're overwriting a contact
743
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
745
// discard the contact id
750
// if we don't have a contact id yet (or we did, but we destroyed it
751
// when we deleted the contact), we'll have to create a new contact
691
// remove from device
692
_backend.deleteContact( id );
754
// create a new contact
755
id = _backend.addContact( contact._name );
695
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
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
758
_contacts_cache.addLookup( cache_identifier, id );
760
// if we haven't already shown that we're overwriting a contact,
761
// show that we're creating a new contact
762
if( !contact_deleted )
763
_doit._handler.sendEmptyMessage(
764
Doit.MESSAGE_CONTACTCREATED );
767
// show that we're merging with an existing contact
768
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
770
// import contact parts
771
if( contact.hasNumbers() )
772
importContactPhones( id, contact.getNumbers() );
773
if( contact.hasEmails() )
774
importContactEmails( id, contact.getEmails() );
775
if( contact.hasAddresses() )
776
importContactAddresses( id, contact.getAddresses() );
777
if( contact.hasOrganisations() )
778
importContactOrganisations( id, contact.getOrganisations() );
779
if( contact.hasNotes() )
780
importContactNotes( id, contact.getNotes() );
781
if( contact.hasBirthday() )
782
importContactBirthday( id, contact.getBirthday() );
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
784
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(
719
ContactsCache.createIdentifier( contact ), id );
721
// if we haven't already shown that we're overwriting a contact,
722
// show that we're creating a new contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
786
showError( R.string.error_unabletoaddcontact );
729
// if we haven't already shown that we're overwriting or creating a
730
// contact, show that we're merging a contact
732
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
734
// import contact parts
735
if( contact.hasNumbers() )
736
importContactPhones( id, contact.getNumbers() );
737
if( contact.hasEmails() )
738
importContactEmails( id, contact.getEmails() );
739
if( contact.hasAddresses() )
740
importContactAddresses( id, contact.getAddresses() );
741
if( contact.hasOrganisations() )
742
importContactOrganisations( id, contact.getOrganisations() );
745
790
private void importContactPhones( Long id,
746
HashMap< String, ContactData.PreferredDetail > datas )
791
HashMap< String, ContactData.PreferredDetail > datas )
792
throws ContactCreationException
748
794
// add phone numbers
749
795
Set< String > datas_keys = datas.keySet();
752
798
String number = i.next();
753
799
ContactData.PreferredDetail data = datas.get( number );
755
// we don't want to add this number if it's crap, or it already
756
// exists (which would cause a duplicate to be created). We don't
757
// take in to account the type when checking for duplicates. This is
758
// intentional: types aren't really very reliable. We assume that
759
// if the number exists at all, it doesn't need importing. Because
760
// of this, we also can't update the cache (which we don't need to
761
// anyway, so it's not a problem).
801
// We don't want to add this number if it's crap, or it already
802
// exists (which would cause a duplicate to be created). We don't
803
// take in to account the type when checking for duplicates. This
804
// is intentional: types aren't really very reliable. We assume
805
// that if the number exists at all, it doesn't need importing.
806
// Because of this, we also can't update the cache (which we don't
807
// need to anyway, so it's not a problem).
762
808
if( _contacts_cache.hasAssociatedNumber( id, number ) )
895
private void importContactNotes( Long id, HashSet< String > datas )
896
throws ContactCreationException
899
Iterator< String > i = datas.iterator();
900
while( i.hasNext() ) {
901
String note = i.next();
903
// we don't want to add this note if it exists already or we would
904
// introduce duplicates
905
if( _contacts_cache.hasAssociatedNote( id, note ) )
909
_backend.addContactNote( id, note );
911
// and add this note to the cache to prevent a addition of duplicate
912
// date from another file
913
_contacts_cache.addAssociatedNote( id, note );
917
private void importContactBirthday( Long id, String birthday )
918
throws ContactCreationException
920
// we don't want to import this birthday if it already exists
921
if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
925
_backend.addContactBirthday( id, birthday );
927
// and update the cache
928
_contacts_cache.addAssociatedBirthday( id, birthday );
846
931
synchronized protected void checkAbort() throws AbortImportException