346
337
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()
381
340
protected void finalise()
382
341
throws ContactNotIdentifiableException
384
// Ensure that if there is a primary number, it is preferred so
385
// that there is always one preferred number. Android will assign
343
// ensure that if there is a primary number, it is preferred so
344
// that there is always one preferred number. Android will assign
386
345
// preference to one anyway so we might as well decide one sensibly.
387
346
if( _primary_number != null ) {
388
347
PreferredDetail data = _numbers.get( _primary_number );
640
599
return _doit.getText( res );
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
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
656
606
_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)
658
612
// handle special cases
659
613
switch( merge_setting )
661
615
case Doit.ACTION_KEEP:
662
// if we are skipping on a duplicate, check for one
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
665
619
case Doit.ACTION_PROMPT:
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;
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 ) )
670
// ok, duplicate exists, so do prompt
625
// ok, it exists, so do prompt
671
626
_doit._handler.sendMessage( Message.obtain( _doit._handler,
672
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
706
// It is expected that we use contact.getCacheIdentifier() here. The
660
// It is expected that we use contact.getCacheIdentifier() here. The
707
661
// contact we are passed should have been successfully finalise()d,
708
662
// which includes generating a valid cache identifier.
709
663
ContactsCache.CacheIdentifier cache_identifier =
710
664
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
712
672
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
713
673
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
715
678
// attempt to lookup the id of an existing contact in the cache with
716
679
// this contact data's cache identifier
717
680
Long id = (Long)_contacts_cache.lookup( cache_identifier );
719
// check to see if this contact should be skipped
720
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
682
// does contact exist already?
723
// show that we're skipping a contact
724
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
691
// remove from device
692
_backend.deleteContact( id );
695
_contacts_cache.removeLookup( cache_identifier );
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
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 )
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
733
contact_deleted = true;
735
// remove from device
736
_backend.deleteContact( id );
712
// create a new contact
713
id = _backend.addContact( contact._name );
715
showError( R.string.error_unabletoaddcontact );
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
754
// create a new contact
755
id = _backend.addContact( contact._name );
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 );
718
_contacts_cache.addLookup( cache_identifier, id );
720
// if we haven't already shown that we're overwriting a contact,
721
// show that we're creating a new contact
723
_doit._handler.sendEmptyMessage( 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() );
784
catch( Backend.ContactCreationException e )
786
showError( R.string.error_unabletoaddcontact );
728
// if we haven't already shown that we're overwriting or creating a
729
// contact, show that we're merging a contact
731
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
733
// import contact parts
734
if( contact.hasNumbers() )
735
importContactPhones( id, contact.getNumbers() );
736
if( contact.hasEmails() )
737
importContactEmails( id, contact.getEmails() );
738
if( contact.hasAddresses() )
739
importContactAddresses( id, contact.getAddresses() );
740
if( contact.hasOrganisations() )
741
importContactOrganisations( id, contact.getOrganisations() );
790
744
private void importContactPhones( Long id,
791
HashMap< String, ContactData.PreferredDetail > datas )
792
throws ContactCreationException
745
HashMap< String, ContactData.PreferredDetail > datas )
794
747
// add phone numbers
795
748
Set< String > datas_keys = datas.keySet();
798
751
String number = i.next();
799
752
ContactData.PreferredDetail data = datas.get( number );
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).
754
// we don't want to add this number if it's crap, or it already
755
// exists (which would cause a duplicate to be created). We don't
756
// take in to account the type when checking for duplicates. This is
757
// intentional: types aren't really very reliable. We assume that
758
// if the number exists at all, it doesn't need importing. Because
759
// of this, we also can't update the cache (which we don't need to
760
// anyway, so it's not a problem).
808
761
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 );
931
845
synchronized protected void checkAbort() throws AbortImportException