4
* Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
4
* Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
6
6
* This file is part of the Import Contacts program (hereafter referred
7
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/import-contacts
8
* http://www.waxworlds.org/edam/software/android/import-contacts
10
10
* This program is free software: you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License as published by
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
54
56
private boolean _abort = false;
55
57
private boolean _is_finished = false;
56
58
private ContactsCache _contacts_cache = null;
57
private Backend _backend = null;
60
@SuppressWarnings("serial")
61
protected class ContactNeedsMoreInfoException extends Exception
60
66
* Data about a contact
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 )
175
173
new ExtraDetail( 0, false, title ) );
177
175
// if this is the first organisation added, or it's a preferred
178
// organisation and the current primary organisation isn't, then
179
// record this as the primary organisation.
176
// organisation and a previous organisation wasn't, then remember
177
// that this is the "primary organisation".
180
178
if( _primary_organisation == null ||
181
179
( is_preferred && !_primary_organisation_is_preferred ) )
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;
281
269
if( !_emails.containsKey( email ) )
282
270
_emails.put( email, new PreferredDetail( type, false ) );
284
// if this is the first email added, or it's a preferred email and
285
// the current primary organisation isn't, then record this as the
272
// if this is the first email added, or it's a preferred email
273
// and a previous email wasn't, then remember that this is the
287
275
if( _primary_email == null ||
288
276
( is_preferred && !_primary_email_is_preferred ) )
340
328
protected void finalise()
341
throws ContactNotIdentifiableException
343
330
// ensure that if there is a primary number, it is preferred so
344
331
// that there is always one preferred number. Android will assign
362
349
_organisations.put( _primary_organisation,
363
350
new ExtraDetail( 0, true, data.getExtra() ) );
366
// create a cache identifier from this contact data, which can be
367
// used to look-up an existing contact
368
_cache_identifier = ContactsCache.createIdentifier( this );
369
if( _cache_identifier == null )
370
throw new ContactNotIdentifiableException();
373
public ContactsCache.CacheIdentifier getCacheIdentifier()
375
return _cache_identifier;
378
354
private String sanitisePhoneNumber( String number )
418
394
setProgressMessage( R.string.doit_caching );
420
// create the appropriate backend
421
// if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
422
// _backend = new ContactsContractBackend( _doit );
424
_backend = new ContactsBackend( _doit );
426
// create a cache of existing contacts and populate it
396
// build a cache of existing contacts
427
397
_contacts_cache = new ContactsCache();
428
_backend.populateCache( _contacts_cache );
398
_contacts_cache.buildCache( _doit );
599
569
return _doit.getText( res );
602
synchronized private boolean checkForDuplicate(
603
ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
throws AbortImportException
572
protected boolean isImportRequired( ContactData contact )
573
throws AbortImportException, ContactNeedsMoreInfoException
576
return isImportRequired( contact, _merge_setting );
579
synchronized private boolean isImportRequired(
580
ContactData contact, int merge_setting )
581
throws AbortImportException, ContactNeedsMoreInfoException
606
583
_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)
585
// create a cache identifier which we can use to detect if this contact
586
// is valid for importing
587
ContactsCache.CacheIdentifier identifier =
588
ContactsCache.createIdentifier( contact );
589
if( identifier == null )
590
throw new ContactNeedsMoreInfoException();
612
592
// handle special cases
613
593
switch( merge_setting )
615
595
case Doit.ACTION_KEEP:
616
596
// if we keep contacts on duplicate, we better check for one
617
return !_contacts_cache.canLookup( cache_identifier );
597
return !_contacts_cache.canLookup( identifier );
619
599
case Doit.ACTION_PROMPT:
620
600
// if we are prompting on duplicate, we better check for one and if
621
601
// the contact doesn'te exist, we want to import it
622
if( !_contacts_cache.canLookup( cache_identifier ) )
602
if( !_contacts_cache.canLookup( identifier ) )
625
605
// ok, it exists, so do prompt
626
606
_doit._handler.sendMessage( Message.obtain( _doit._handler,
627
Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
607
Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
638
618
_merge_setting = _response;
640
620
// recurse, with our new merge setting
641
return checkForDuplicate( cache_identifier, _response );
621
return isImportRequired( contact, _response );
644
624
// for all other cases (either overwriting or merging) we will need the
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
640
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
641
// finish( ACTION_ABORT );
675
// keep track of whether we've informed the UI of what we're doing
643
ContentValues values = new ContentValues();
676
644
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 );
647
// give the contact a chance to finalise it's data
650
// create something, from the contact data, that we can use to identify
651
// a cache entry and attempt to lookup the id of an existing contact in
653
ContactsCache.CacheIdentifier identifier =
654
ContactsCache.createIdentifier( contact );
655
if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
682
657
// does contact exist already?
685
660
// should we skip this import altogether?
686
661
if( _last_merge_decision == Doit.ACTION_KEEP ) return;
664
Uri contact_uri = ContentUris.withAppendedId(
665
Contacts.People.CONTENT_URI, id );
688
667
// should we destroy the existing contact before importing?
689
668
if( _last_merge_decision == Doit.ACTION_OVERWRITE )
691
670
// remove from device
692
_backend.deleteContact( id );
671
_doit.getContentResolver().delete( contact_uri, null, null );
695
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
674
_contacts_cache.removeLookup( identifier );
696
675
_contacts_cache.removeAssociatedData( id );
698
677
// show that we're overwriting a contact
708
// if we don't have a contact id yet (or we did, but we destroyed it
687
// if we don't have a contact id yet (or if we did, but we destroyed it
709
688
// when we deleted the contact), we'll have to create a new contact
712
691
// create a new contact
713
id = _backend.addContact( contact._name );
692
values.put( Contacts.People.NAME, contact._name );
693
Uri contact_uri = _doit.getContentResolver().insert(
694
Contacts.People.CONTENT_URI, values );
695
id = ContentUris.parseId( contact_uri );
696
if( id == null || id <= 0 )
715
697
showError( R.string.error_unabletoaddcontact );
699
// try to add them to the "My Contacts" group
701
Contacts.People.addToMyContactsGroup(
702
_doit.getContentResolver(), id );
704
catch( IllegalStateException e ) {
705
// ignore any failure
718
709
_contacts_cache.addLookup(
719
710
ContactsCache.createIdentifier( contact ), id );
729
720
// if we haven't already shown that we're overwriting or creating a
730
// contact, show that we're merging a contact
721
// contact show that we're merging a contact
731
722
if( !ui_informed )
732
723
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
745
736
private void importContactPhones( Long id,
746
737
HashMap< String, ContactData.PreferredDetail > datas )
739
// get URI to contact's phones
740
Uri contact_phones_uri = Uri.withAppendedPath(
741
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
742
Contacts.People.Phones.CONTENT_DIRECTORY );
743
Set< String > datas_keys = datas.keySet();
748
745
// add phone numbers
749
Set< String > datas_keys = datas.keySet();
750
746
Iterator< String > i = datas_keys.iterator();
751
747
while( i.hasNext() ) {
752
748
String number = i.next();
765
761
// add phone number
766
_backend.addContactPhone( id, number, data );
762
ContentValues values = new ContentValues();
763
values.put( Contacts.Phones.TYPE, data.getType() );
764
values.put( Contacts.Phones.NUMBER, number );
765
if( data.isPreferred() )
766
values.put( Contacts.Phones.ISPRIMARY, 1 );
767
_doit.getContentResolver().insert( contact_phones_uri, values );
768
769
// and add this address to the cache to prevent a addition of
769
770
// duplicate date from another file
774
775
private void importContactEmails( Long id,
775
776
HashMap< String, ContactData.PreferredDetail > datas )
778
// get URI to contact's contact methods
779
Uri contact_contact_methods_uri = Uri.withAppendedPath(
780
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
781
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
782
Set< String > datas_keys = datas.keySet();
777
784
// add email addresses
778
Set< String > datas_keys = datas.keySet();
779
785
Iterator< String > i = datas_keys.iterator();
780
786
while( i.hasNext() ) {
781
787
String email = i.next();
789
795
// add phone number
790
_backend.addContactEmail( id, email, data );
796
ContentValues values = new ContentValues();
797
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
798
values.put( Contacts.ContactMethods.DATA, email );
799
values.put( Contacts.ContactMethods.TYPE, data.getType() );
800
if( data.isPreferred() )
801
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
802
_doit.getContentResolver().insert( contact_contact_methods_uri,
792
805
// and add this address to the cache to prevent a addition of
793
806
// duplicate date from another file
798
811
private void importContactAddresses( Long id,
799
812
HashMap< String, ContactData.TypeDetail > datas )
814
// get URI to contact's contact methods
815
Uri contact_contact_methods_uri = Uri.withAppendedPath(
816
ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
817
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
802
820
Set< String > datas_keys = datas.keySet();
803
821
Iterator< String > i = datas_keys.iterator();
813
831
// add postal address
814
_backend.addContactAddresses( id, address, data );
832
ContentValues values = new ContentValues();
833
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
834
values.put( Contacts.ContactMethods.DATA, address );
835
values.put( Contacts.ContactMethods.TYPE, data.getType() );
836
_doit.getContentResolver().insert( contact_contact_methods_uri,
816
839
// and add this address to the cache to prevent a addition of
817
840
// duplicate date from another file
837
860
// add organisation address
838
_backend.addContactOrganisation( id, organisation, data );
861
ContentValues values = new ContentValues();
862
values.put( Contacts.Organizations.PERSON_ID, id );
863
values.put( Contacts.Organizations.COMPANY, organisation );
864
values.put( Contacts.ContactMethods.TYPE,
865
Contacts.OrganizationColumns.TYPE_WORK );
866
if( data.getExtra() != null )
867
values.put( Contacts.Organizations.TITLE, data.getExtra() );
868
_doit.getContentResolver().insert(
869
Contacts.Organizations.CONTENT_URI, values );
840
871
// and add this address to the cache to prevent a addition of
841
872
// duplicate date from another file