/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 2011 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;
37
38
 
38
39
 
39
40
public class Importer extends Thread
55
56
        private boolean _abort = false;
56
57
        private boolean _is_finished = false;
57
58
        private ContactsCache _contacts_cache = null;
58
 
        private Backend _backend = null;
59
59
 
60
60
        /**
61
61
         * Data about a contact
124
124
 
125
125
                protected String _name = null;
126
126
                protected String _primary_organisation = null;
127
 
                protected boolean _primary_organisation_is_preferred;
 
127
                protected boolean _primary_organisation_is_preferred = false;
128
128
                protected String _primary_number = null;
129
 
                protected int _primary_number_type;
130
 
                protected boolean _primary_number_is_preferred;
 
129
                protected boolean _primary_number_is_preferred = false;
131
130
                protected String _primary_email = null;
132
 
                protected boolean _primary_email_is_preferred;
 
131
                protected boolean _primary_email_is_preferred = false;
133
132
                protected HashMap< String, ExtraDetail > _organisations = null;
134
133
                protected HashMap< String, PreferredDetail > _numbers = null;
135
134
                protected HashMap< String, PreferredDetail > _emails = null;
176
175
                                        new ExtraDetail( 0, false, title ) );
177
176
 
178
177
                        // if this is the first organisation added, or it's a preferred
179
 
                        // organisation and the current primary organisation isn't, then
180
 
                        // record this as the primary organisation.
 
178
                        // organisation and a previous organisation wasn't, then remember
 
179
                        // that this is the "primary organisation".
181
180
                        if( _primary_organisation == null ||
182
181
                                ( is_preferred && !_primary_organisation_is_preferred ) )
183
182
                        {
224
223
                                _numbers.put( number,
225
224
                                        new PreferredDetail( type, false ) );
226
225
 
227
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
228
 
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
229
 
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
230
 
 
231
226
                        // if this is the first number added, or it's a preferred number
232
 
                        // and the current primary number isn't, or this number is on equal
233
 
                        // standing with the primary number in terms of preference and it is
234
 
                        // a voice number and the primary number isn't, then record this as
235
 
                        // the primary number.
 
227
                        // and a previous number wasn't, then remember that this is the
 
228
                        // "primary number".
236
229
                        if( _primary_number == null ||
237
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
238
 
                                ( is_preferred == _primary_number_is_preferred &&
239
 
                                        !non_voice_types.contains( type ) &&
240
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
230
                                ( is_preferred && !_primary_number_is_preferred ) )
241
231
                        {
242
232
                                _primary_number = number;
243
 
                                _primary_number_type = type;
244
233
                                _primary_number_is_preferred = is_preferred;
245
234
                        }
246
235
                }
282
271
                        if( !_emails.containsKey( email ) )
283
272
                                _emails.put( email, new PreferredDetail( type, false ) );
284
273
 
285
 
                        // if this is the first email added, or it's a preferred email and
286
 
                        // the current primary organisation isn't, then record this as the
287
 
                        // 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".
288
277
                        if( _primary_email == null ||
289
278
                                ( is_preferred && !_primary_email_is_preferred ) )
290
279
                        {
418
407
                        // update UI
419
408
                        setProgressMessage( R.string.doit_caching );
420
409
 
421
 
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
422
 
//                              _backend = new ContactsContractBackend();
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
683
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
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 );
 
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 );
714
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
719
                        _contacts_cache.addLookup(
719
720
                                ContactsCache.createIdentifier( contact ), id );
745
746
        private void importContactPhones( Long id,
746
747
                        HashMap< String, ContactData.PreferredDetail > datas )
747
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
 
748
755
                // add phone numbers
749
 
                Set< String > datas_keys = datas.keySet();
750
756
                Iterator< String > i = datas_keys.iterator();
751
757
                while( i.hasNext() ) {
752
758
                        String number = i.next();
763
769
                                continue;
764
770
 
765
771
                        // add phone number
766
 
                        _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 );
767
778
 
768
779
                        // and add this address to the cache to prevent a addition of
769
780
                        // duplicate date from another file
774
785
        private void importContactEmails( Long id,
775
786
                        HashMap< String, ContactData.PreferredDetail > datas )
776
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
 
777
794
                // add email addresses
778
 
                Set< String > datas_keys = datas.keySet();
779
795
                Iterator< String > i = datas_keys.iterator();
780
796
                while( i.hasNext() ) {
781
797
                        String email = i.next();
787
803
                                continue;
788
804
 
789
805
                        // add phone number
790
 
                        _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 );
791
814
 
792
815
                        // and add this address to the cache to prevent a addition of
793
816
                        // duplicate date from another file
798
821
        private void importContactAddresses( Long id,
799
822
                HashMap< String, ContactData.TypeDetail > datas )
800
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
 
801
829
                // add addresses
802
830
                Set< String > datas_keys = datas.keySet();
803
831
                Iterator< String > i = datas_keys.iterator();
811
839
                                continue;
812
840
 
813
841
                        // add postal address
814
 
                        _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 );
815
848
 
816
849
                        // and add this address to the cache to prevent a addition of
817
850
                        // duplicate date from another file
835
868
                                continue;
836
869
 
837
870
                        // add organisation address
838
 
                        _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 );
839
880
 
840
881
                        // and add this address to the cache to prevent a addition of
841
882
                        // duplicate date from another file