/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/importcontacts/Importer.java

  • Committer: edam
  • Date: 2011-05-30 14:04:51 UTC
  • Revision ID: edam@waxworlds.org-20110530140451-d99fy3zoi6zq1jf2
- renamed VCFImporter to VcardImporter and VCard to Vcard

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
5
5
 *
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
9
9
 *
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/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
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;
33
31
 
 
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
 
37
39
 
38
40
public class Importer extends Thread
39
41
{
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;
 
59
 
 
60
        @SuppressWarnings("serial")
 
61
        protected class ContactNeedsMoreInfoException extends Exception
 
62
        {
 
63
        }
58
64
 
59
65
        /**
60
66
         * Data about a contact
116
122
                        }
117
123
                }
118
124
 
119
 
                @SuppressWarnings("serial")
120
 
                protected class ContactNotIdentifiableException extends Exception
121
 
                {
122
 
                }
123
 
 
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;
136
136
 
137
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
138
 
 
139
137
                protected void setName( String name )
140
138
                {
141
139
                        _name = name;
175
173
                                        new ExtraDetail( 0, false, title ) );
176
174
 
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 ) )
182
180
                        {
223
221
                                _numbers.put( number,
224
222
                                        new PreferredDetail( type, false ) );
225
223
 
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 ) );
229
 
 
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
 
226
                        // "primary number".
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 ) )
240
229
                        {
241
230
                                _primary_number = number;
242
 
                                _primary_number_type = type;
243
231
                                _primary_number_is_preferred = is_preferred;
244
232
                        }
245
233
                }
270
258
                        email = sanitisesEmailAddress( email );
271
259
                        if( email == null )
272
260
                        {
273
 
                                // TODO: warn that an imported email address is being ignored
 
261
                                // TODO: warn that an imported email addtrss is being ignored
274
262
                                return;
275
263
                        }
276
264
 
281
269
                        if( !_emails.containsKey( email ) )
282
270
                                _emails.put( email, new PreferredDetail( type, false ) );
283
271
 
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
286
 
                        // primary email.
 
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
 
274
                        // "primary email".
287
275
                        if( _primary_email == null ||
288
276
                                ( is_preferred && !_primary_email_is_preferred ) )
289
277
                        {
338
326
                }
339
327
 
340
328
                protected void finalise()
341
 
                        throws ContactNotIdentifiableException
342
329
                {
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() ) );
364
351
                        }
365
 
 
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();
371
 
                }
372
 
 
373
 
                public ContactsCache.CacheIdentifier getCacheIdentifier()
374
 
                {
375
 
                        return _cache_identifier;
376
352
                }
377
353
 
378
354
                private String sanitisePhoneNumber( String number )
417
393
                        // update UI
418
394
                        setProgressMessage( R.string.doit_caching );
419
395
 
420
 
                        // create the appropriate backend
421
 
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
422
 
//                              _backend = new ContactsContractBackend( _doit );
423
 
//                      else
424
 
                                _backend = new ContactsBackend( _doit );
425
 
 
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 );
429
399
 
430
400
                        // do the import
431
401
                        onImport();
599
569
                return _doit.getText( res );
600
570
        }
601
571
 
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
 
574
        {
 
575
                checkAbort();
 
576
                return isImportRequired( contact, _merge_setting );
 
577
        }
 
578
 
 
579
        synchronized private boolean isImportRequired(
 
580
                ContactData contact, int merge_setting )
 
581
                throws AbortImportException, ContactNeedsMoreInfoException
605
582
        {
606
583
                _last_merge_decision = merge_setting;
607
584
 
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();
611
591
 
612
592
                // handle special cases
613
593
                switch( merge_setting )
614
594
                {
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 );
618
598
 
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 ) )
623
603
                                return true;
624
604
 
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() ) );
628
608
                        try {
629
609
                                wait();
630
610
                        }
638
618
                                _merge_setting = _response;
639
619
 
640
620
                        // recurse, with our new merge setting
641
 
                        return checkForDuplicate( cache_identifier, _response );
 
621
                        return isImportRequired( contact, _response );
642
622
                }
643
623
 
644
624
                // for all other cases (either overwriting or merging) we will need the
657
637
        {
658
638
                checkAbort();
659
639
 
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();
665
 
 
666
 
                // check to see if this contact is a duplicate and should be skipped
667
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
668
 
                        skipContact();
669
 
                        return;
670
 
                }
671
 
 
672
640
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
641
//                      finish( ACTION_ABORT );
674
642
 
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;
677
 
 
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 );
 
645
                Long id = null;
 
646
 
 
647
                // give the contact a chance to finalise it's data
 
648
                contact.finalise();
 
649
 
 
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
 
652
                // the cache with it
 
653
                ContactsCache.CacheIdentifier identifier =
 
654
                        ContactsCache.createIdentifier( contact );
 
655
                if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
681
656
 
682
657
                // does contact exist already?
683
658
                if( id != null )
685
660
                        // should we skip this import altogether?
686
661
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
662
 
 
663
                        // get contact's URI
 
664
                        Uri contact_uri = ContentUris.withAppendedId(
 
665
                                Contacts.People.CONTENT_URI, id );
 
666
 
688
667
                        // should we destroy the existing contact before importing?
689
668
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
690
669
                        {
691
670
                                // remove from device
692
 
                                _backend.deleteContact( id );
 
671
                                _doit.getContentResolver().delete( contact_uri, null, null );
693
672
 
694
673
                                // update cache
695
 
                                _contacts_cache.removeLookup( cache_identifier );
 
674
                                _contacts_cache.removeLookup( identifier );
696
675
                                _contacts_cache.removeAssociatedData( id );
697
676
 
698
677
                                // show that we're overwriting a contact
705
684
                        }
706
685
                }
707
686
 
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
710
689
                if( id == null )
711
690
                {
712
691
                        // create a new contact
713
 
                        id = _backend.addContact( contact._name );
714
 
                        if( id == null )
 
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 );
716
698
 
 
699
                        // try to add them to the "My Contacts" group
 
700
                        try {
 
701
                                Contacts.People.addToMyContactsGroup(
 
702
                                        _doit.getContentResolver(), id );
 
703
                        }
 
704
                        catch( IllegalStateException e ) {
 
705
                                // ignore any failure
 
706
                        }
 
707
 
717
708
                        // update cache
718
 
                        _contacts_cache.addLookup( cache_identifier, id );
 
709
                        _contacts_cache.addLookup(
 
710
                                ContactsCache.createIdentifier( contact ), id );
719
711
 
720
712
                        // if we haven't already shown that we're overwriting a contact,
721
713
                        // show that we're creating a new contact
726
718
                }
727
719
 
728
720
                // if we haven't already shown that we're overwriting or creating a
729
 
                // contact, show that we're merging a contact
 
721
                // contact show that we're merging a contact
730
722
                if( !ui_informed )
731
723
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
732
724
 
744
736
        private void importContactPhones( Long id,
745
737
                        HashMap< String, ContactData.PreferredDetail > datas )
746
738
        {
 
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();
 
744
 
747
745
                // add phone numbers
748
 
                Set< String > datas_keys = datas.keySet();
749
746
                Iterator< String > i = datas_keys.iterator();
750
747
                while( i.hasNext() ) {
751
748
                        String number = i.next();
762
759
                                continue;
763
760
 
764
761
                        // add phone number
765
 
                        _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 );
766
768
 
767
769
                        // and add this address to the cache to prevent a addition of
768
770
                        // duplicate date from another file
773
775
        private void importContactEmails( Long id,
774
776
                        HashMap< String, ContactData.PreferredDetail > datas )
775
777
        {
 
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();
 
783
 
776
784
                // add email addresses
777
 
                Set< String > datas_keys = datas.keySet();
778
785
                Iterator< String > i = datas_keys.iterator();
779
786
                while( i.hasNext() ) {
780
787
                        String email = i.next();
786
793
                                continue;
787
794
 
788
795
                        // add phone number
789
 
                        _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,
 
803
                                values );
790
804
 
791
805
                        // and add this address to the cache to prevent a addition of
792
806
                        // duplicate date from another file
797
811
        private void importContactAddresses( Long id,
798
812
                HashMap< String, ContactData.TypeDetail > datas )
799
813
        {
 
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 );
 
818
 
800
819
                // add addresses
801
820
                Set< String > datas_keys = datas.keySet();
802
821
                Iterator< String > i = datas_keys.iterator();
810
829
                                continue;
811
830
 
812
831
                        // add postal address
813
 
                        _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,
 
837
                                values );
814
838
 
815
839
                        // and add this address to the cache to prevent a addition of
816
840
                        // duplicate date from another file
834
858
                                continue;
835
859
 
836
860
                        // add organisation address
837
 
                        _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 );
838
870
 
839
871
                        // and add this address to the cache to prevent a addition of
840
872
                        // duplicate date from another file