/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/am/ed/importcontacts/Importer.java

  • Committer: edam
  • Date: 2012-05-08 15:09:13 UTC
  • Revision ID: tim@ed.am-20120508150913-t9qzbbs1kld691dy
abstracted the android contacts API in to an interface, ready to be switched to
another depending on the platform version.

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 <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
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://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/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 org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
28
30
import java.util.Set;
29
31
import java.util.regex.Matcher;
30
32
import java.util.regex.Pattern;
31
33
 
32
 
import android.content.ContentUris;
33
 
import android.content.ContentValues;
34
34
import android.content.SharedPreferences;
35
 
import android.net.Uri;
36
35
import android.os.Message;
37
 
import android.provider.Contacts;
 
36
import android.provider.Contacts.PhonesColumns;
38
37
 
39
38
 
40
39
public class Importer extends Thread
56
55
        private boolean _abort = false;
57
56
        private boolean _is_finished = false;
58
57
        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 = false;
 
127
                protected boolean _primary_organisation_is_preferred;
128
128
                protected String _primary_number = null;
129
 
                protected boolean _primary_number_is_preferred = false;
 
129
                protected int _primary_number_type;
 
130
                protected boolean _primary_number_is_preferred;
130
131
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred = false;
 
132
                protected boolean _primary_email_is_preferred;
132
133
                protected HashMap< String, ExtraDetail > _organisations = null;
133
134
                protected HashMap< String, PreferredDetail > _numbers = null;
134
135
                protected HashMap< String, PreferredDetail > _emails = null;
175
176
                                        new ExtraDetail( 0, false, title ) );
176
177
 
177
178
                        // if this is the first organisation added, or it's a preferred
178
 
                        // organisation and a previous organisation wasn't, then remember
179
 
                        // that this is the "primary organisation".
 
179
                        // organisation and the current primary organisation isn't, then
 
180
                        // record this as the primary organisation.
180
181
                        if( _primary_organisation == null ||
181
182
                                ( is_preferred && !_primary_organisation_is_preferred ) )
182
183
                        {
223
224
                                _numbers.put( number,
224
225
                                        new PreferredDetail( type, false ) );
225
226
 
 
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
 
226
231
                        // if this is the first number added, or it's a preferred number
227
 
                        // and a previous number wasn't, then remember that this is the
228
 
                        // "primary 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.
229
236
                        if( _primary_number == null ||
230
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
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 ) ) )
231
241
                        {
232
242
                                _primary_number = number;
 
243
                                _primary_number_type = type;
233
244
                                _primary_number_is_preferred = is_preferred;
234
245
                        }
235
246
                }
271
282
                        if( !_emails.containsKey( email ) )
272
283
                                _emails.put( email, new PreferredDetail( type, false ) );
273
284
 
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".
 
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.
277
288
                        if( _primary_email == null ||
278
289
                                ( is_preferred && !_primary_email_is_preferred ) )
279
290
                        {
407
418
                        // update UI
408
419
                        setProgressMessage( R.string.doit_caching );
409
420
 
410
 
                        // build a cache of existing contacts
 
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
411
427
                        _contacts_cache = new ContactsCache();
412
 
                        _contacts_cache.buildCache( _doit );
 
428
                        _backend.populateCache( _contacts_cache );
413
429
 
414
430
                        // do the import
415
431
                        onImport();
669
685
                        // should we skip this import altogether?
670
686
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
671
687
 
672
 
                        // get contact's URI
673
 
                        Uri contact_uri = ContentUris.withAppendedId(
674
 
                                Contacts.People.CONTENT_URI, id );
675
 
 
676
688
                        // should we destroy the existing contact before importing?
677
689
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
678
690
                        {
679
691
                                // remove from device
680
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
692
                                _backend.deleteContact( id );
681
693
 
682
694
                                // update cache
683
695
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
693
705
                        }
694
706
                }
695
707
 
696
 
                // if we don't have a contact id yet (or if we did, but we destroyed it
 
708
                // if we don't have a contact id yet (or we did, but we destroyed it
697
709
                // when we deleted the contact), we'll have to create a new contact
698
710
                if( id == null )
699
711
                {
700
712
                        // create a new contact
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 );
 
713
                        id = _backend.addContact( contact._name );
706
714
                        if( id == null || id <= 0 )
707
715
                                showError( R.string.error_unabletoaddcontact );
708
716
 
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
 
 
718
717
                        // update cache
719
718
                        _contacts_cache.addLookup(
720
719
                                ContactsCache.createIdentifier( contact ), id );
746
745
        private void importContactPhones( Long id,
747
746
                        HashMap< String, ContactData.PreferredDetail > datas )
748
747
        {
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
 
 
755
748
                // add phone numbers
 
749
                Set< String > datas_keys = datas.keySet();
756
750
                Iterator< String > i = datas_keys.iterator();
757
751
                while( i.hasNext() ) {
758
752
                        String number = i.next();
769
763
                                continue;
770
764
 
771
765
                        // add phone number
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
                        _backend.addContactPhone( id, number, data );
778
767
 
779
768
                        // and add this address to the cache to prevent a addition of
780
769
                        // duplicate date from another file
785
774
        private void importContactEmails( Long id,
786
775
                        HashMap< String, ContactData.PreferredDetail > datas )
787
776
        {
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
 
 
794
777
                // add email addresses
 
778
                Set< String > datas_keys = datas.keySet();
795
779
                Iterator< String > i = datas_keys.iterator();
796
780
                while( i.hasNext() ) {
797
781
                        String email = i.next();
803
787
                                continue;
804
788
 
805
789
                        // add phone number
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
                        _backend.addContactEmail( id, email, data );
814
791
 
815
792
                        // and add this address to the cache to prevent a addition of
816
793
                        // duplicate date from another file
821
798
        private void importContactAddresses( Long id,
822
799
                HashMap< String, ContactData.TypeDetail > datas )
823
800
        {
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
 
 
829
801
                // add addresses
830
802
                Set< String > datas_keys = datas.keySet();
831
803
                Iterator< String > i = datas_keys.iterator();
839
811
                                continue;
840
812
 
841
813
                        // add postal address
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
                        _backend.addContactAddresses( id, address, data );
848
815
 
849
816
                        // and add this address to the cache to prevent a addition of
850
817
                        // duplicate date from another file
868
835
                                continue;
869
836
 
870
837
                        // add organisation address
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
                        _backend.addContactOrganisation( id, organisation, data );
880
839
 
881
840
                        // and add this address to the cache to prevent a addition of
882
841
                        // duplicate date from another file