/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 19:20:17 UTC
  • Revision ID: edam@waxworlds.org-20110530192017-5c09k4kgpov02gja
- added checks for Doit.this == null when handling dialog buttons (I managed to abort an import as a duplicate contacts dialog was shown, but can't reproduce it now)
- added line no.s to vcard parsing errors
- update progress bar after a contact is imported, not before
- fixed bug introduced in last commit where a contacts were imported after finaliseVcard()ing failed
- don't show unknown encoding errors for vcard fields that we don't care about (which ignores base64 encoded photos, for example)

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;
58
59
 
59
60
        /**
60
61
         * Data about a contact
123
124
 
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;
175
175
                                        new ExtraDetail( 0, false, title ) );
176
176
 
177
177
                        // 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.
 
178
                        // organisation and a previous organisation wasn't, then remember
 
179
                        // that this is the "primary organisation".
180
180
                        if( _primary_organisation == null ||
181
181
                                ( is_preferred && !_primary_organisation_is_preferred ) )
182
182
                        {
223
223
                                _numbers.put( number,
224
224
                                        new PreferredDetail( type, false ) );
225
225
 
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
226
                        // 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.
 
227
                        // and a previous number wasn't, then remember that this is the
 
228
                        // "primary number".
235
229
                        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 ) ) )
 
230
                                ( is_preferred && !_primary_number_is_preferred ) )
240
231
                        {
241
232
                                _primary_number = number;
242
 
                                _primary_number_type = type;
243
233
                                _primary_number_is_preferred = is_preferred;
244
234
                        }
245
235
                }
270
260
                        email = sanitisesEmailAddress( email );
271
261
                        if( email == null )
272
262
                        {
273
 
                                // TODO: warn that an imported email address is being ignored
 
263
                                // TODO: warn that an imported email addtrss is being ignored
274
264
                                return;
275
265
                        }
276
266
 
281
271
                        if( !_emails.containsKey( email ) )
282
272
                                _emails.put( email, new PreferredDetail( type, false ) );
283
273
 
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.
 
274
                        // if this is the first email added, or it's a preferred email
 
275
                        // and a previous email wasn't, then remember that this is the
 
276
                        // "primary email".
287
277
                        if( _primary_email == null ||
288
278
                                ( is_preferred && !_primary_email_is_preferred ) )
289
279
                        {
417
407
                        // update UI
418
408
                        setProgressMessage( R.string.doit_caching );
419
409
 
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
 
410
                        // build a cache of existing contacts
427
411
                        _contacts_cache = new ContactsCache();
428
 
                        _backend.populateCache( _contacts_cache );
 
412
                        _contacts_cache.buildCache( _doit );
429
413
 
430
414
                        // do the import
431
415
                        onImport();
685
669
                        // should we skip this import altogether?
686
670
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
671
 
 
672
                        // get contact's URI
 
673
                        Uri contact_uri = ContentUris.withAppendedId(
 
674
                                Contacts.People.CONTENT_URI, id );
 
675
 
688
676
                        // should we destroy the existing contact before importing?
689
677
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
690
678
                        {
691
679
                                // remove from device
692
 
                                _backend.deleteContact( id );
 
680
                                _doit.getContentResolver().delete( contact_uri, null, null );
693
681
 
694
682
                                // update cache
695
 
                                _contacts_cache.removeLookup( cache_identifier );
 
683
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
696
684
                                _contacts_cache.removeAssociatedData( id );
697
685
 
698
686
                                // show that we're overwriting a contact
705
693
                        }
706
694
                }
707
695
 
708
 
                // if we don't have a contact id yet (or we did, but we destroyed it
 
696
                // if we don't have a contact id yet (or if we did, but we destroyed it
709
697
                // when we deleted the contact), we'll have to create a new contact
710
698
                if( id == null )
711
699
                {
712
700
                        // create a new contact
713
 
                        id = _backend.addContact( contact._name );
714
 
                        if( id == null )
 
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 )
715
707
                                showError( R.string.error_unabletoaddcontact );
716
708
 
 
709
                        // try to add them to the "My Contacts" group
 
710
                        try {
 
711
                                Contacts.People.addToMyContactsGroup(
 
712
                                        _doit.getContentResolver(), id );
 
713
                        }
 
714
                        catch( IllegalStateException e ) {
 
715
                                // ignore any failure
 
716
                        }
 
717
 
717
718
                        // update cache
718
 
                        _contacts_cache.addLookup( cache_identifier, id );
 
719
                        _contacts_cache.addLookup(
 
720
                                ContactsCache.createIdentifier( contact ), id );
719
721
 
720
722
                        // if we haven't already shown that we're overwriting a contact,
721
723
                        // show that we're creating a new contact
744
746
        private void importContactPhones( Long id,
745
747
                        HashMap< String, ContactData.PreferredDetail > datas )
746
748
        {
 
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();
 
754
 
747
755
                // add phone numbers
748
 
                Set< String > datas_keys = datas.keySet();
749
756
                Iterator< String > i = datas_keys.iterator();
750
757
                while( i.hasNext() ) {
751
758
                        String number = i.next();
762
769
                                continue;
763
770
 
764
771
                        // add phone number
765
 
                        _backend.addContactPhone( id, number, data );
 
772
                        ContentValues values = new ContentValues();
 
773
                        values.put( Contacts.Phones.TYPE, data.getType() );
 
774
                        values.put( Contacts.Phones.NUMBER, number );
 
775
                        if( data.isPreferred() )
 
776
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
 
777
                        _doit.getContentResolver().insert( contact_phones_uri, values );
766
778
 
767
779
                        // and add this address to the cache to prevent a addition of
768
780
                        // duplicate date from another file
773
785
        private void importContactEmails( Long id,
774
786
                        HashMap< String, ContactData.PreferredDetail > datas )
775
787
        {
 
788
                // get URI to contact's contact methods
 
789
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
790
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
791
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
792
                Set< String > datas_keys = datas.keySet();
 
793
 
776
794
                // add email addresses
777
 
                Set< String > datas_keys = datas.keySet();
778
795
                Iterator< String > i = datas_keys.iterator();
779
796
                while( i.hasNext() ) {
780
797
                        String email = i.next();
786
803
                                continue;
787
804
 
788
805
                        // add phone number
789
 
                        _backend.addContactEmail( id, email, data );
 
806
                        ContentValues values = new ContentValues();
 
807
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
 
808
                        values.put( Contacts.ContactMethods.DATA, email );
 
809
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
810
                        if( data.isPreferred() )
 
811
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
 
812
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
 
813
                                values );
790
814
 
791
815
                        // and add this address to the cache to prevent a addition of
792
816
                        // duplicate date from another file
797
821
        private void importContactAddresses( Long id,
798
822
                HashMap< String, ContactData.TypeDetail > datas )
799
823
        {
 
824
                // get URI to contact's contact methods
 
825
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
826
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
 
827
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
828
 
800
829
                // add addresses
801
830
                Set< String > datas_keys = datas.keySet();
802
831
                Iterator< String > i = datas_keys.iterator();
810
839
                                continue;
811
840
 
812
841
                        // add postal address
813
 
                        _backend.addContactAddresses( id, address, data );
 
842
                        ContentValues values = new ContentValues();
 
843
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
 
844
                        values.put( Contacts.ContactMethods.DATA, address );
 
845
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
 
846
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
 
847
                                values );
814
848
 
815
849
                        // and add this address to the cache to prevent a addition of
816
850
                        // duplicate date from another file
834
868
                                continue;
835
869
 
836
870
                        // add organisation address
837
 
                        _backend.addContactOrganisation( id, organisation, data );
 
871
                        ContentValues values = new ContentValues();
 
872
                        values.put( Contacts.Organizations.PERSON_ID, id );
 
873
                        values.put( Contacts.Organizations.COMPANY, organisation );
 
874
                        values.put( Contacts.ContactMethods.TYPE,
 
875
                                Contacts.OrganizationColumns.TYPE_WORK );
 
876
                        if( data.getExtra() != null )
 
877
                                values.put( Contacts.Organizations.TITLE, data.getExtra() );
 
878
                        _doit.getContentResolver().insert(
 
879
                                Contacts.Organizations.CONTENT_URI, values );
838
880
 
839
881
                        // and add this address to the cache to prevent a addition of
840
882
                        // duplicate date from another file