/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-12-19 17:41:29 UTC
  • Revision ID: tim@ed.am-20121219174129-41i7vrz0jviideqi
fix intro strings

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 2012 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;
38
 
 
 
36
import android.provider.Contacts.PhonesColumns;
39
37
 
40
38
public class Importer extends Thread
41
39
{
56
54
        private boolean _abort = false;
57
55
        private boolean _is_finished = false;
58
56
        private ContactsCache _contacts_cache = null;
 
57
        private Backend _backend = null;
59
58
 
60
59
        /**
61
60
         * Data about a contact
124
123
 
125
124
                protected String _name = null;
126
125
                protected String _primary_organisation = null;
127
 
                protected boolean _primary_organisation_is_preferred = false;
 
126
                protected boolean _primary_organisation_is_preferred;
128
127
                protected String _primary_number = null;
129
 
                protected boolean _primary_number_is_preferred = false;
 
128
                protected int _primary_number_type;
 
129
                protected boolean _primary_number_is_preferred;
130
130
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred = false;
 
131
                protected boolean _primary_email_is_preferred;
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 a previous organisation wasn't, then remember
179
 
                        // that this is the "primary organisation".
 
178
                        // organisation and the current primary organisation isn't, then
 
179
                        // record this as 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
 
226
230
                        // 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".
 
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.
229
235
                        if( _primary_number == null ||
230
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
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 ) ) )
231
240
                        {
232
241
                                _primary_number = number;
 
242
                                _primary_number_type = type;
233
243
                                _primary_number_is_preferred = is_preferred;
234
244
                        }
235
245
                }
260
270
                        email = sanitisesEmailAddress( email );
261
271
                        if( email == null )
262
272
                        {
263
 
                                // TODO: warn that an imported email addtrss is being ignored
 
273
                                // TODO: warn that an imported email address is being ignored
264
274
                                return;
265
275
                        }
266
276
 
271
281
                        if( !_emails.containsKey( email ) )
272
282
                                _emails.put( email, new PreferredDetail( type, false ) );
273
283
 
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".
 
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.
277
287
                        if( _primary_email == null ||
278
288
                                ( is_preferred && !_primary_email_is_preferred ) )
279
289
                        {
407
417
                        // update UI
408
418
                        setProgressMessage( R.string.doit_caching );
409
419
 
410
 
                        // build a cache of existing contacts
 
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
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
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
 
695
                                _contacts_cache.removeLookup( cache_identifier );
684
696
                                _contacts_cache.removeAssociatedData( id );
685
697
 
686
698
                                // show that we're overwriting a contact
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 );
706
 
                        if( id == null || id <= 0 )
 
713
                        id = _backend.addContact( contact._name );
 
714
                        if( id == null )
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
 
                        _contacts_cache.addLookup(
720
 
                                ContactsCache.createIdentifier( contact ), id );
 
718
                        _contacts_cache.addLookup( cache_identifier, id );
721
719
 
722
720
                        // if we haven't already shown that we're overwriting a contact,
723
721
                        // show that we're creating a new contact
746
744
        private void importContactPhones( Long id,
747
745
                        HashMap< String, ContactData.PreferredDetail > datas )
748
746
        {
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
747
                // add phone numbers
 
748
                Set< String > datas_keys = datas.keySet();
756
749
                Iterator< String > i = datas_keys.iterator();
757
750
                while( i.hasNext() ) {
758
751
                        String number = i.next();
769
762
                                continue;
770
763
 
771
764
                        // 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 );
 
765
                        _backend.addContactPhone( id, number, data );
778
766
 
779
767
                        // and add this address to the cache to prevent a addition of
780
768
                        // duplicate date from another file
785
773
        private void importContactEmails( Long id,
786
774
                        HashMap< String, ContactData.PreferredDetail > datas )
787
775
        {
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
776
                // add email addresses
 
777
                Set< String > datas_keys = datas.keySet();
795
778
                Iterator< String > i = datas_keys.iterator();
796
779
                while( i.hasNext() ) {
797
780
                        String email = i.next();
803
786
                                continue;
804
787
 
805
788
                        // 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 );
 
789
                        _backend.addContactEmail( id, email, data );
814
790
 
815
791
                        // and add this address to the cache to prevent a addition of
816
792
                        // duplicate date from another file
821
797
        private void importContactAddresses( Long id,
822
798
                HashMap< String, ContactData.TypeDetail > datas )
823
799
        {
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
800
                // add addresses
830
801
                Set< String > datas_keys = datas.keySet();
831
802
                Iterator< String > i = datas_keys.iterator();
839
810
                                continue;
840
811
 
841
812
                        // 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 );
 
813
                        _backend.addContactAddresses( id, address, data );
848
814
 
849
815
                        // and add this address to the cache to prevent a addition of
850
816
                        // duplicate date from another file
868
834
                                continue;
869
835
 
870
836
                        // 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 );
 
837
                        _backend.addContactOrganisation( id, organisation, data );
880
838
 
881
839
                        // and add this address to the cache to prevent a addition of
882
840
                        // duplicate date from another file