21
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package am.ed.importcontacts;
24
package org.waxworlds.edam.importcontacts;
26
import java.util.Arrays;
27
26
import java.util.HashMap;
28
import java.util.HashSet;
29
27
import java.util.Iterator;
30
28
import java.util.Set;
31
29
import java.util.regex.Matcher;
32
30
import java.util.regex.Pattern;
32
import android.content.ContentUris;
33
import android.content.ContentValues;
34
34
import android.content.SharedPreferences;
35
import android.net.Uri;
35
36
import android.os.Message;
36
import android.provider.Contacts.PhonesColumns;
37
import android.provider.Contacts;
38
40
public class Importer extends Thread
119
@SuppressWarnings("serial")
120
protected class ContactNotIdentifiableException extends Exception
124
125
protected String _name = null;
125
126
protected String _primary_organisation = null;
126
protected boolean _primary_organisation_is_preferred;
127
protected boolean _primary_organisation_is_preferred = false;
127
128
protected String _primary_number = null;
128
protected int _primary_number_type;
129
protected boolean _primary_number_is_preferred;
129
protected boolean _primary_number_is_preferred = false;
130
130
protected String _primary_email = null;
131
protected boolean _primary_email_is_preferred;
131
protected boolean _primary_email_is_preferred = false;
132
132
protected HashMap< String, ExtraDetail > _organisations = null;
133
133
protected HashMap< String, PreferredDetail > _numbers = null;
134
134
protected HashMap< String, PreferredDetail > _emails = null;
135
135
protected HashMap< String, TypeDetail > _addresses = null;
137
private ContactsCache.CacheIdentifier _cache_identifier = null;
139
137
protected void setName( String name )
223
221
_numbers.put( number,
224
222
new PreferredDetail( type, false ) );
226
final Set< Integer > non_voice_types = new HashSet< Integer >(
227
Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
228
PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
230
224
// if this is the first number added, or it's a preferred number
231
// and the current primary number isn't, or this number is on equal
232
// standing with the primary number in terms of preference and it is
233
// a voice number and the primary number isn't, then record this as
234
// the primary number.
225
// and a previous number wasn't, then remember that this is the
235
227
if( _primary_number == null ||
236
( is_preferred && !_primary_number_is_preferred ) ||
237
( is_preferred == _primary_number_is_preferred &&
238
!non_voice_types.contains( type ) &&
239
non_voice_types.contains( _primary_number_type ) ) )
228
( is_preferred && !_primary_number_is_preferred ) )
241
230
_primary_number = number;
242
_primary_number_type = type;
243
231
_primary_number_is_preferred = is_preferred;
552
522
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
555
protected void setProgressMax( int max_progress )
525
protected void setProgressMax( int maxProgress )
556
526
throws AbortImportException
559
529
_doit._handler.sendMessage( Message.obtain(
560
530
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
561
new Integer( max_progress ) ) );
531
new Integer( maxProgress ) ) );
564
protected void setTmpProgress( int tmp_progress )
565
throws AbortImportException
534
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
568
537
_doit._handler.sendMessage( Message.obtain(
569
538
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
570
new Integer( tmp_progress ) ) );
539
new Integer( tmpProgress ) ) );
573
542
protected void setProgress( int progress ) throws AbortImportException
599
568
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
571
protected boolean isImportRequired( ContactData contact )
572
throws AbortImportException, ContactNeedsMoreInfoException
575
return isImportRequired( contact, _merge_setting );
578
synchronized private boolean isImportRequired(
579
ContactData contact, int merge_setting )
580
throws AbortImportException, ContactNeedsMoreInfoException
606
582
_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)
584
// create a cache identifier which we can use to detect if this contact
585
// is valid for importing
586
ContactsCache.CacheIdentifier identifier =
587
ContactsCache.createIdentifier( contact );
588
if( identifier == null )
589
throw new ContactNeedsMoreInfoException();
612
591
// handle special cases
613
592
switch( merge_setting )
615
594
case Doit.ACTION_KEEP:
616
595
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
596
return !_contactsCache.canLookup( identifier );
619
598
case Doit.ACTION_PROMPT:
620
599
// if we are prompting on duplicate, we better check for one and if
621
600
// the contact doesn'te exist, we want to import it
622
if( !_contacts_cache.canLookup( cache_identifier ) )
601
if( !_contactsCache.canLookup( identifier ) )
625
604
// ok, it exists, so do prompt
626
605
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
606
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
660
// It is expected that we use contact.getCacheIdentifier() here. The
661
// contact we are passed should have been successfully finalise()d,
662
// which includes generating a valid cache identifier.
663
ContactsCache.CacheIdentifier cache_identifier =
664
contact.getCacheIdentifier();
666
// check to see if this contact is a duplicate and should be skipped
667
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
672
639
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
640
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
676
boolean ui_informed = false;
678
// attempt to lookup the id of an existing contact in the cache with
679
// this contact data's cache identifier
680
Long id = (Long)_contacts_cache.lookup( cache_identifier );
642
ContentValues values = new ContentValues();
643
boolean uiInformed = false;
646
// give the contact a chance to finalise it's data
649
// create something, from the contact data, that we can use to identify
650
// a cache entry and attempt to lookup the id of an existing contact in
652
ContactsCache.CacheIdentifier identifier =
653
ContactsCache.createIdentifier( contact );
654
if( identifier != null ) id = (Long)_contactsCache.lookup( identifier );
682
656
// does contact exist already?
685
659
// should we skip this import altogether?
686
660
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
663
Uri contactUri = ContentUris.withAppendedId(
664
Contacts.People.CONTENT_URI, id );
688
666
// should we destroy the existing contact before importing?
689
667
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
691
669
// remove from device
692
_backend.deleteContact( id );
670
_doit.getContentResolver().delete( contactUri, null, null );
695
_contacts_cache.removeLookup( cache_identifier );
696
_contacts_cache.removeAssociatedData( id );
673
_contactsCache.removeLookup( identifier );
674
_contactsCache.removeAssociatedData( id );
698
676
// show that we're overwriting a contact
699
677
_doit._handler.sendEmptyMessage(
700
678
Doit.MESSAGE_CONTACTOVERWRITTEN );
703
681
// discard the contact id
708
// if we don't have a contact id yet (or we did, but we destroyed it
686
// if we don't have a contact id yet (or if we did, but we destroyed it
709
687
// when we deleted the contact), we'll have to create a new contact
712
690
// create a new contact
713
id = _backend.addContact( contact._name );
691
values.put( Contacts.People.NAME, contact._name );
692
Uri contactUri = _doit.getContentResolver().insert(
693
Contacts.People.CONTENT_URI, values );
694
id = ContentUris.parseId( contactUri );
695
if( id == null || id <= 0 )
715
696
showError( R.string.error_unabletoaddcontact );
698
// try to add them to the "My Contacts" group
700
Contacts.People.addToMyContactsGroup(
701
_doit.getContentResolver(), id );
703
catch( IllegalStateException e ) {
704
// ignore any failure
718
_contacts_cache.addLookup( cache_identifier, id );
708
_contactsCache.addLookup(
709
ContactsCache.createIdentifier( contact ), id );
720
711
// if we haven't already shown that we're overwriting a contact,
721
712
// show that we're creating a new contact
723
714
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
728
719
// if we haven't already shown that we're overwriting or creating a
729
// contact, show that we're merging a contact
720
// contact show that we're merging a contact
731
722
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
733
724
// import contact parts
758
754
// if the number exists at all, it doesn't need importing. Because
759
755
// of this, we also can't update the cache (which we don't need to
760
756
// anyway, so it's not a problem).
761
if( _contacts_cache.hasAssociatedNumber( id, number ) )
757
if( _contactsCache.hasAssociatedNumber( id, number ) )
764
760
// add phone number
765
_backend.addContactPhone( id, number, data );
761
ContentValues values = new ContentValues();
762
values.put( Contacts.Phones.TYPE, data.getType() );
763
values.put( Contacts.Phones.NUMBER, number );
764
if( data.isPreferred() )
765
values.put( Contacts.Phones.ISPRIMARY, 1 );
766
_doit.getContentResolver().insert( contactPhonesUri, values );
767
768
// and add this address to the cache to prevent a addition of
768
769
// duplicate date from another file
769
_contacts_cache.addAssociatedNumber( id, number );
770
_contactsCache.addAssociatedNumber( id, number );
773
774
private void importContactEmails( Long id,
774
775
HashMap< String, ContactData.PreferredDetail > datas )
777
// get URI to contact's contact methods
778
Uri contactContactMethodsUri = Uri.withAppendedPath(
779
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
780
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
781
Set< String > datasKeys = datas.keySet();
776
783
// add email addresses
777
Set< String > datas_keys = datas.keySet();
778
Iterator< String > i = datas_keys.iterator();
784
Iterator< String > i = datasKeys.iterator();
779
785
while( i.hasNext() ) {
780
786
String email = i.next();
781
787
ContactData.PreferredDetail data = datas.get( email );
783
789
// we don't want to add this email address if it exists already or
784
790
// we would introduce duplicates.
785
if( _contacts_cache.hasAssociatedEmail( id, email ) )
791
if( _contactsCache.hasAssociatedEmail( id, email ) )
788
794
// add phone number
789
_backend.addContactEmail( id, email, data );
795
ContentValues values = new ContentValues();
796
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
797
values.put( Contacts.ContactMethods.DATA, email );
798
values.put( Contacts.ContactMethods.TYPE, data.getType() );
799
if( data.isPreferred() )
800
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
801
_doit.getContentResolver().insert( contactContactMethodsUri,
791
804
// and add this address to the cache to prevent a addition of
792
805
// duplicate date from another file
793
_contacts_cache.addAssociatedEmail( id, email );
806
_contactsCache.addAssociatedEmail( id, email );
797
810
private void importContactAddresses( Long id,
798
811
HashMap< String, ContactData.TypeDetail > datas )
813
// get URI to contact's contact methods
814
Uri contactContactMethodsUri = Uri.withAppendedPath(
815
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
816
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
801
Set< String > datas_keys = datas.keySet();
802
Iterator< String > i = datas_keys.iterator();
819
Set< String > datasKeys = datas.keySet();
820
Iterator< String > i = datasKeys.iterator();
803
821
while( i.hasNext() ) {
804
822
String address = i.next();
805
823
ContactData.TypeDetail data = datas.get( address );
807
825
// we don't want to add this address if it exists already or we
808
826
// would introduce duplicates
809
if( _contacts_cache.hasAssociatedAddress( id, address ) )
827
if( _contactsCache.hasAssociatedAddress( id, address ) )
812
830
// add postal address
813
_backend.addContactAddresses( id, address, data );
831
ContentValues values = new ContentValues();
832
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
833
values.put( Contacts.ContactMethods.DATA, address );
834
values.put( Contacts.ContactMethods.TYPE, data.getType() );
835
_doit.getContentResolver().insert( contactContactMethodsUri,
815
838
// and add this address to the cache to prevent a addition of
816
839
// duplicate date from another file
817
_contacts_cache.addAssociatedAddress( id, address );
840
_contactsCache.addAssociatedAddress( id, address );
822
845
HashMap< String, ContactData.ExtraDetail > datas )
825
Set< String > datas_keys = datas.keySet();
826
Iterator< String > i = datas_keys.iterator();
848
Set< String > datasKeys = datas.keySet();
849
Iterator< String > i = datasKeys.iterator();
827
850
while( i.hasNext() ) {
828
851
String organisation = i.next();
829
852
ContactData.ExtraDetail data = datas.get( organisation );
831
854
// we don't want to add this address if it exists already or we
832
855
// would introduce duplicates
833
if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
856
if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
836
859
// add organisation address
837
_backend.addContactOrganisation( id, organisation, data );
860
ContentValues values = new ContentValues();
861
values.put( Contacts.Organizations.PERSON_ID, id );
862
values.put( Contacts.Organizations.COMPANY, organisation );
863
values.put( Contacts.ContactMethods.TYPE,
864
Contacts.OrganizationColumns.TYPE_WORK );
865
if( data.getExtra() != null )
866
values.put( Contacts.Organizations.TITLE, data.getExtra() );
867
_doit.getContentResolver().insert(
868
Contacts.Organizations.CONTENT_URI, values );
839
870
// and add this address to the cache to prevent a addition of
840
871
// duplicate date from another file
841
_contacts_cache.addAssociatedOrganisation( id, organisation );
872
_contactsCache.addAssociatedOrganisation( id, organisation );