223
232
_numbers.put( number,
224
233
new PreferredDetail( type, false ) );
235
final Set< Integer > non_voice_types = new HashSet< Integer >(
236
Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
226
238
// if this is the first number added, or it's a preferred number
227
// and a previous number wasn't, then remember that this is the
239
// and the current primary number isn't, or this number is on equal
240
// standing with the primary number in terms of preference and it is
241
// a voice number and the primary number isn't, then record this as
242
// the primary number.
229
243
if( _primary_number == null ||
230
( is_preferred && !_primary_number_is_preferred ) )
244
( is_preferred && !_primary_number_is_preferred ) ||
245
( is_preferred == _primary_number_is_preferred &&
246
!non_voice_types.contains( type ) &&
247
non_voice_types.contains( _primary_number_type ) ) )
232
249
_primary_number = number;
250
_primary_number_type = type;
233
251
_primary_number_is_preferred = is_preferred;
583
624
return _doit.getText( res );
586
synchronized private boolean checkForDuplicate(
587
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
throws AbortImportException
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
590
639
_last_merge_decision = merge_setting;
592
// it is ok to use contact.getCacheIdentifier(). The contact has already
593
// been finalised, which means a valid cache identifier will have been
594
// created for it (or it would have been skipped)
596
641
// handle special cases
597
642
switch( merge_setting )
599
644
case Doit.ACTION_KEEP:
600
// if we keep contacts on duplicate, we better check for one
601
return !_contacts_cache.canLookup( cache_identifier );
645
// if we are skipping on a duplicate, check for one
603
648
case Doit.ACTION_PROMPT:
604
// if we are prompting on duplicate, we better check for one and if
605
// the contact doesn'te exist, we want to import it
606
if( !_contacts_cache.canLookup( cache_identifier ) )
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;
609
// ok, it exists, so do prompt
653
// ok, duplicate exists, so do prompt
610
654
_doit._handler.sendMessage( Message.obtain( _doit._handler,
611
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
655
Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
647
692
ContactsCache.CacheIdentifier cache_identifier =
648
693
contact.getCacheIdentifier();
650
// check to see if this contact is a duplicate and should be skipped
651
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
656
695
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
657
696
// finish( ACTION_ABORT );
659
// keep track of whether we've informed the UI of what we're doing
660
boolean ui_informed = false;
662
698
// attempt to lookup the id of an existing contact in the cache with
663
699
// this contact data's cache identifier
664
700
Long id = (Long)_contacts_cache.lookup( cache_identifier );
666
// does contact exist already?
669
// should we skip this import altogether?
670
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
673
Uri contact_uri = ContentUris.withAppendedId(
674
Contacts.People.CONTENT_URI, id );
676
// should we destroy the existing contact before importing?
677
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
702
// check to see if this contact should be skipped
703
if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
706
// show that we're skipping a contact
707
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
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 )
716
contact_deleted = true;
718
// remove from device
719
_backend.deleteContact( id );
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
679
// remove from device
680
_doit.getContentResolver().delete( contact_uri, null, null );
737
// create a new contact
738
id = _backend.addContact( contact._name );
683
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
684
_contacts_cache.removeAssociatedData( id );
686
// show that we're overwriting a contact
687
_doit._handler.sendEmptyMessage(
688
Doit.MESSAGE_CONTACTOVERWRITTEN );
691
// discard the contact id
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 );
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() );
696
// if we don't have a contact id yet (or if we did, but we destroyed it
697
// when we deleted the contact), we'll have to create a new contact
765
catch( Backend.ContactCreationException e )
700
// create a new contact
701
ContentValues values = new ContentValues();
702
values.put( Contacts.People.NAME, contact._name );
703
Uri contact_uri = _doit.getContentResolver().insert(
704
Contacts.People.CONTENT_URI, values );
705
id = ContentUris.parseId( contact_uri );
706
if( id == null || id <= 0 )
707
showError( R.string.error_unabletoaddcontact );
709
// try to add them to the "My Contacts" group
711
Contacts.People.addToMyContactsGroup(
712
_doit.getContentResolver(), id );
714
catch( IllegalStateException e ) {
715
// ignore any failure
719
_contacts_cache.addLookup(
720
ContactsCache.createIdentifier( contact ), id );
722
// if we haven't already shown that we're overwriting a contact,
723
// show that we're creating a new contact
725
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
767
showError( R.string.error_unabletoaddcontact );
730
// if we haven't already shown that we're overwriting or creating a
731
// contact, show that we're merging a 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() );
746
771
private void importContactPhones( Long id,
747
HashMap< String, ContactData.PreferredDetail > datas )
772
HashMap< String, ContactData.PreferredDetail > datas )
773
throws ContactCreationException
749
// get URI to contact's phones
750
Uri contact_phones_uri = Uri.withAppendedPath(
751
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
752
Contacts.People.Phones.CONTENT_DIRECTORY );
753
Set< String > datas_keys = datas.keySet();
755
775
// add phone numbers
776
Set< String > datas_keys = datas.keySet();
756
777
Iterator< String > i = datas_keys.iterator();
757
778
while( i.hasNext() ) {
758
779
String number = i.next();