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 );
502
544
finish( ACTION_ABORT );
505
protected void showFatalError( int res ) throws AbortImportException
507
showFatalError( _doit.getText( res ).toString() );
510
synchronized protected void showFatalError( String message )
511
throws AbortImportException
514
_doit._handler.sendMessage( Message.obtain(
515
_doit._handler, Doit.MESSAGE_ERROR, message ) );
519
catch( InterruptedException e ) { }
521
// no need to check if an abortion happened during the wait, we are
522
// about to finish anyway!
523
finish( ACTION_ABORT );
526
protected boolean showContinue( int res ) throws AbortImportException
528
return showContinue( _doit.getText( res ).toString() );
531
synchronized protected boolean showContinue( String message )
547
protected void showContinueOrAbort( int res ) throws AbortImportException
549
showContinueOrAbort( _doit.getText( res ).toString() );
552
synchronized protected void showContinueOrAbort( String message )
532
553
throws AbortImportException
599
622
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
626
* Should we skip a contact, given whether it exists or not and the current
627
* merge setting? This routine handles throwing up a prompt, if required.
629
* @param contact_detail the display name of the contact
630
* @param exists true if this contact matches one in the cache
631
* @param merge_setting the merge setting to use
632
* @return true if the contact should be skipped outright
633
* @throws AbortImportException
635
synchronized private boolean shouldWeSkipContact( String contact_detail,
636
boolean exists, int merge_setting ) throws AbortImportException
606
638
_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
640
// handle special cases
613
641
switch( merge_setting )
615
643
case Doit.ACTION_KEEP:
616
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
644
// if we are skipping on a duplicate, check for one
619
647
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 ) )
648
// if we are prompting on duplicate, then we can say that we won't
649
// skip if there isn't one
650
if( !exists ) return false;
625
// ok, it exists, so do prompt
652
// ok, duplicate exists, so do prompt
626
653
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
654
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
660
// It is expected that we use contact.getCacheIdentifier() here. The
688
// It is expected that we use contact.getCacheIdentifier() here. The
661
689
// contact we are passed should have been successfully finalise()d,
662
690
// which includes generating a valid cache identifier.
663
691
ContactsCache.CacheIdentifier cache_identifier =
664
692
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
672
694
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
695
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
678
697
// attempt to lookup the id of an existing contact in the cache with
679
698
// this contact data's cache identifier
680
699
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 )
701
// check to see if this contact should be skipped
702
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
705
// show that we're skipping a contact
706
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
710
// if a contact exists, and we're overwriting, destroy the existing
711
// contact before importing
712
boolean contact_deleted = false;
713
if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
715
contact_deleted = true;
717
// remove from device
718
_backend.deleteContact( id );
721
_contacts_cache.removeLookup( cache_identifier );
722
_contacts_cache.removeAssociatedData( id );
724
// show that we're overwriting a contact
725
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
727
// discard the contact id
732
// if we don't have a contact id yet (or we did, but we destroyed it
733
// when we deleted the contact), we'll have to create a new contact
691
// remove from device
692
_backend.deleteContact( id );
736
// create a new contact
737
id = _backend.addContact( contact._name );
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
740
_contacts_cache.addLookup( cache_identifier, id );
742
// if we haven't already shown that we're overwriting a contact,
743
// show that we're creating a new contact
744
if( !contact_deleted )
745
_doit._handler.sendEmptyMessage(
746
Doit.MESSAGE_CONTACTCREATED );
749
// show that we're merging with an existing contact
750
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
752
// import contact parts
753
if( contact.hasNumbers() )
754
importContactPhones( id, contact.getNumbers() );
755
if( contact.hasEmails() )
756
importContactEmails( id, contact.getEmails() );
757
if( contact.hasAddresses() )
758
importContactAddresses( id, contact.getAddresses() );
759
if( contact.hasOrganisations() )
760
importContactOrganisations( id, contact.getOrganisations() );
761
if( contact.hasNotes() )
762
importContactNotes( id, contact.getNotes() );
763
if( contact.hasBirthday() )
764
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
766
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( 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 );
768
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() );
744
772
private void importContactPhones( Long id,
745
HashMap< String, ContactData.PreferredDetail > datas )
773
HashMap< String, ContactData.PreferredDetail > datas )
774
throws ContactCreationException
747
776
// add phone numbers
748
777
Set< String > datas_keys = datas.keySet();
751
780
String number = i.next();
752
781
ContactData.PreferredDetail data = datas.get( number );
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).
783
// We don't want to add this number if it's crap, or it already
784
// exists (which would cause a duplicate to be created). We don't
785
// take in to account the type when checking for duplicates. This
786
// is intentional: types aren't really very reliable. We assume
787
// that if the number exists at all, it doesn't need importing.
788
// Because of this, we also can't update the cache (which we don't
789
// need to anyway, so it's not a problem).
761
790
if( _contacts_cache.hasAssociatedNumber( id, number ) )
877
private void importContactNotes( Long id, HashSet< String > datas )
878
throws ContactCreationException
881
Iterator< String > i = datas.iterator();
882
while( i.hasNext() ) {
883
String note = i.next();
885
// we don't want to add this note if it exists already or we would
886
// introduce duplicates
887
if( _contacts_cache.hasAssociatedNote( id, note ) )
891
_backend.addContactNote( id, note );
893
// and add this note to the cache to prevent a addition of duplicate
894
// date from another file
895
_contacts_cache.addAssociatedNote( id, note );
899
private void importContactBirthday( Long id, String birthday )
900
throws ContactCreationException
902
// we don't want to import this birthday if it already exists
903
if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
907
_backend.addContactBirthday( id, birthday );
909
// and update the cache
910
_contacts_cache.addAssociatedBirthday( id, birthday );
845
913
synchronized protected void checkAbort() throws AbortImportException