/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
 
import java.util.Locale;
31
28
import java.util.Set;
32
29
import java.util.regex.Matcher;
33
30
import java.util.regex.Pattern;
34
31
 
35
 
import am.ed.importcontacts.Backend.ContactCreationException;
 
32
import android.content.ContentUris;
 
33
import android.content.ContentValues;
36
34
import android.content.SharedPreferences;
 
35
import android.net.Uri;
37
36
import android.os.Message;
 
37
import android.provider.Contacts;
 
38
 
38
39
 
39
40
public class Importer extends Thread
40
41
{
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
 
 
60
        @SuppressWarnings("serial")
 
61
        protected class ContactNeedsMoreInfoException extends Exception
 
62
        {
 
63
        }
59
64
 
60
65
        /**
61
66
         * Data about a contact
62
67
         */
63
68
        public class ContactData
64
69
        {
65
 
                public final static int TYPE_HOME = 0;
66
 
                public final static int TYPE_WORK = 1;
67
 
                public final static int TYPE_MOBILE = 2;        // only used with phones
68
 
                public final static int TYPE_FAX_HOME = 3;      // only used with phones
69
 
                public final static int TYPE_FAX_WORK = 4;      // only used with phones
70
 
                public final static int TYPE_PAGER = 5;         // only used with phones
71
 
 
72
70
                class TypeDetail
73
71
                {
74
72
                        protected int _type;
124
122
                        }
125
123
                }
126
124
 
127
 
                @SuppressWarnings("serial")
128
 
                protected class ContactNotIdentifiableException extends Exception
129
 
                {
130
 
                }
131
 
 
132
125
                protected String _name = null;
133
126
                protected String _primary_organisation = null;
134
 
                protected boolean _primary_organisation_is_preferred;
 
127
                protected boolean _primary_organisation_is_preferred = false;
135
128
                protected String _primary_number = null;
136
 
                protected int _primary_number_type;
137
 
                protected boolean _primary_number_is_preferred;
 
129
                protected boolean _primary_number_is_preferred = false;
138
130
                protected String _primary_email = null;
139
 
                protected boolean _primary_email_is_preferred;
 
131
                protected boolean _primary_email_is_preferred = false;
140
132
                protected HashMap< String, ExtraDetail > _organisations = null;
141
133
                protected HashMap< String, PreferredDetail > _numbers = null;
142
134
                protected HashMap< String, PreferredDetail > _emails = null;
143
135
                protected HashMap< String, TypeDetail > _addresses = null;
144
 
                protected HashSet< String > _notes = null;
145
 
 
146
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
147
136
 
148
137
                protected void setName( String name )
149
138
                {
184
173
                                        new ExtraDetail( 0, false, title ) );
185
174
 
186
175
                        // if this is the first organisation added, or it's a preferred
187
 
                        // organisation and the current primary organisation isn't, then
188
 
                        // record this as the primary organisation.
 
176
                        // organisation and a previous organisation wasn't, then remember
 
177
                        // that this is the "primary organisation".
189
178
                        if( _primary_organisation == null ||
190
179
                                ( is_preferred && !_primary_organisation_is_preferred ) )
191
180
                        {
232
221
                                _numbers.put( number,
233
222
                                        new PreferredDetail( type, false ) );
234
223
 
235
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
236
 
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
237
 
 
238
224
                        // if this is the first number added, or it's a preferred number
239
 
                        // and the current primary number isn't, or this number is on equal
240
 
                        // standing with the primary number in terms of preference and it is
241
 
                        // a voice number and the primary number isn't, then record this as
242
 
                        // the primary number.
 
225
                        // and a previous number wasn't, then remember that this is the
 
226
                        // "primary number".
243
227
                        if( _primary_number == null ||
244
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
245
 
                                ( is_preferred == _primary_number_is_preferred &&
246
 
                                        !non_voice_types.contains( type ) &&
247
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
228
                                ( is_preferred && !_primary_number_is_preferred ) )
248
229
                        {
249
230
                                _primary_number = number;
250
 
                                _primary_number_type = type;
251
231
                                _primary_number_is_preferred = is_preferred;
252
232
                        }
253
233
                }
278
258
                        email = sanitisesEmailAddress( email );
279
259
                        if( email == null )
280
260
                        {
281
 
                                // TODO: warn that an imported email address is being ignored
 
261
                                // TODO: warn that an imported email addtrss is being ignored
282
262
                                return;
283
263
                        }
284
264
 
289
269
                        if( !_emails.containsKey( email ) )
290
270
                                _emails.put( email, new PreferredDetail( type, false ) );
291
271
 
292
 
                        // if this is the first email added, or it's a preferred email and
293
 
                        // the current primary organisation isn't, then record this as the
294
 
                        // 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".
295
275
                        if( _primary_email == null ||
296
276
                                ( is_preferred && !_primary_email_is_preferred ) )
297
277
                        {
345
325
                        return _addresses;
346
326
                }
347
327
 
348
 
                protected void addNote( String note )
349
 
                {
350
 
                        if( _notes == null ) _notes = new HashSet< String >();
351
 
                        if( !_notes.contains( note ) )
352
 
                                _notes.add( note );
353
 
                }
354
 
 
355
 
                public boolean hasNotes()
356
 
                {
357
 
                        return _notes != null && _notes.size() > 0;
358
 
                }
359
 
 
360
 
                public HashSet< String > getNotes()
361
 
                {
362
 
                        return _notes;
363
 
                }
364
 
 
365
328
                protected void finalise()
366
 
                        throws ContactNotIdentifiableException
367
329
                {
368
330
                        // ensure that if there is a primary number, it is preferred so
369
331
                        // that there is always one preferred number. Android will assign
387
349
                                _organisations.put( _primary_organisation,
388
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
389
351
                        }
390
 
 
391
 
                        // create a cache identifier from this contact data, which can be
392
 
                        // used to look-up an existing contact
393
 
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
394
 
                        if( _cache_identifier == null )
395
 
                                throw new ContactNotIdentifiableException();
396
 
                }
397
 
 
398
 
                public ContactsCache.CacheIdentifier getCacheIdentifier()
399
 
                {
400
 
                        return _cache_identifier;
401
352
                }
402
353
 
403
354
                private String sanitisePhoneNumber( String number )
417
368
                        Matcher m = p.matcher( email );
418
369
                        if( m.matches() ) {
419
370
                                String[] bits = email.split( "@" );
420
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
 
371
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
421
372
                        }
422
373
                        return null;
423
374
                }
442
393
                        // update UI
443
394
                        setProgressMessage( R.string.doit_caching );
444
395
 
445
 
                        // create the appropriate backend
446
 
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
447
 
                                _backend = new ContactsContractBackend( _doit );
448
 
                        else
449
 
                                _backend = new ContactsBackend( _doit );
450
 
 
451
 
                        // create a cache of existing contacts and populate it
 
396
                        // build a cache of existing contacts
452
397
                        _contacts_cache = new ContactsCache();
453
 
                        _backend.populateCache( _contacts_cache );
 
398
                        _contacts_cache.buildCache( _doit );
454
399
 
455
400
                        // do the import
456
401
                        onImport();
583
528
                checkAbort();
584
529
                _doit._handler.sendMessage( Message.obtain(
585
530
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
586
 
                        Integer.valueOf( max_progress ) ) );
 
531
                        new Integer( max_progress ) ) );
587
532
        }
588
533
 
589
534
        protected void setTmpProgress( int tmp_progress )
592
537
                checkAbort();
593
538
                _doit._handler.sendMessage( Message.obtain(
594
539
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
595
 
                        Integer.valueOf( tmp_progress ) ) );
 
540
                        new Integer( tmp_progress ) ) );
596
541
        }
597
542
 
598
543
        protected void setProgress( int progress ) throws AbortImportException
600
545
                checkAbort();
601
546
                _doit._handler.sendMessage( Message.obtain(
602
547
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
603
 
                        Integer.valueOf( progress ) ) );
 
548
                        new Integer( progress ) ) );
604
549
        }
605
550
 
606
551
        protected void finish( int action ) throws AbortImportException
624
569
                return _doit.getText( res );
625
570
        }
626
571
 
627
 
        /**
628
 
         * Should we skip a contact, given whether it exists or not and the current
629
 
         * merge setting?  This routine handles throwing up a prompt, if required.
630
 
         * @param contact_detail the display name of the contact
631
 
         * @param exists true if this contact matches one in the cache
632
 
         * @param merge_setting the merge setting to use
633
 
         * @return true if the contact should be skipped outright
634
 
         * @throws AbortImportException
635
 
         */
636
 
        synchronized private boolean shouldWeSkipContact( String contact_detail,
637
 
                boolean exists, int merge_setting ) 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
638
582
        {
639
583
                _last_merge_decision = merge_setting;
640
584
 
 
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();
 
591
 
641
592
                // handle special cases
642
593
                switch( merge_setting )
643
594
                {
644
595
                case Doit.ACTION_KEEP:
645
 
                        // if we are skipping on a duplicate, check for one
646
 
                        return exists;
 
596
                        // if we keep contacts on duplicate, we better check for one
 
597
                        return !_contacts_cache.canLookup( identifier );
647
598
 
648
599
                case Doit.ACTION_PROMPT:
649
 
                        // if we are prompting on duplicate, then we can say that we won't
650
 
                        // skip if there isn't one
651
 
                        if( !exists ) return false;
 
600
                        // if we are prompting on duplicate, we better check for one and if
 
601
                        // the contact doesn'te exist, we want to import it
 
602
                        if( !_contacts_cache.canLookup( identifier ) )
 
603
                                return true;
652
604
 
653
 
                        // ok, duplicate exists, so do prompt
 
605
                        // ok, it exists, so do prompt
654
606
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
655
 
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
 
607
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
656
608
                        try {
657
609
                                wait();
658
610
                        }
666
618
                                _merge_setting = _response;
667
619
 
668
620
                        // recurse, with our new merge setting
669
 
                        return shouldWeSkipContact( contact_detail, exists, _response );
 
621
                        return isImportRequired( contact, _response );
670
622
                }
671
623
 
672
 
                // for all other cases (either overwriting or merging) we don't skip
673
 
                return false;
 
624
                // for all other cases (either overwriting or merging) we will need the
 
625
                // imported data
 
626
                return true;
674
627
        }
675
628
 
676
629
        protected void skipContact() throws AbortImportException
677
630
        {
678
631
                checkAbort();
679
 
 
680
 
                // show that we're skipping a new contact
681
632
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
682
633
        }
683
634
 
686
637
        {
687
638
                checkAbort();
688
639
 
689
 
                // It is expected that we use contact.getCacheIdentifier() here. The
690
 
                // contact we are passed should have been successfully finalise()d,
691
 
                // which includes generating a valid cache identifier.
692
 
                ContactsCache.CacheIdentifier cache_identifier =
693
 
                        contact.getCacheIdentifier();
694
 
 
695
640
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
696
641
//                      finish( ACTION_ABORT );
697
642
 
698
 
                // attempt to lookup the id of an existing contact in the cache with
699
 
                // this contact data's cache identifier
700
 
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
701
 
 
702
 
                // check to see if this contact should be skipped
703
 
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
704
 
                        _merge_setting ) )
 
643
                ContentValues values = new ContentValues();
 
644
                boolean ui_informed = false;
 
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 );
 
656
 
 
657
                // does contact exist already?
 
658
                if( id != null )
705
659
                {
706
 
                        // show that we're skipping a contact
707
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
708
 
                        return;
 
660
                        // should we skip this import altogether?
 
661
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
 
662
 
 
663
                        // get contact's URI
 
664
                        Uri contact_uri = ContentUris.withAppendedId(
 
665
                                Contacts.People.CONTENT_URI, id );
 
666
 
 
667
                        // should we destroy the existing contact before importing?
 
668
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
669
                        {
 
670
                                // remove from device
 
671
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
672
 
 
673
                                // update cache
 
674
                                _contacts_cache.removeLookup( identifier );
 
675
                                _contacts_cache.removeAssociatedData( id );
 
676
 
 
677
                                // show that we're overwriting a contact
 
678
                                _doit._handler.sendEmptyMessage(
 
679
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
680
                                ui_informed = true;
 
681
 
 
682
                                // discard the contact id
 
683
                                id = null;
 
684
                        }
709
685
                }
710
686
 
711
 
                // if a contact exists, and we're overwriting, destroy the existing
712
 
                // contact before importing
713
 
                boolean contact_deleted = false;
714
 
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
687
                // if we don't have a contact id yet (or if we did, but we destroyed it
 
688
                // when we deleted the contact), we'll have to create a new contact
 
689
                if( id == null )
715
690
                {
716
 
                        contact_deleted = true;
 
691
                        // create a new contact
 
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 )
 
697
                                showError( R.string.error_unabletoaddcontact );
717
698
 
718
 
                        // remove from device
719
 
                        _backend.deleteContact( id );
 
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
                        }
720
707
 
721
708
                        // update cache
722
 
                        _contacts_cache.removeLookup( cache_identifier );
723
 
                        _contacts_cache.removeAssociatedData( id );
724
 
 
725
 
                        // show that we're overwriting a contact
726
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
727
 
 
728
 
                        // discard the contact id
729
 
                        id = null;
730
 
                }
731
 
 
732
 
                try {
733
 
                        // if we don't have a contact id yet (or we did, but we destroyed it
734
 
                        // when we deleted the contact), we'll have to create a new contact
735
 
                        if( id == null )
736
 
                        {
737
 
                                // create a new contact
738
 
                                id = _backend.addContact( contact._name );
739
 
 
740
 
                                // update cache
741
 
                                _contacts_cache.addLookup( cache_identifier, id );
742
 
 
743
 
                                // if we haven't already shown that we're overwriting a contact,
744
 
                                // show that we're creating a new contact
745
 
                                if( !contact_deleted )
746
 
                                        _doit._handler.sendEmptyMessage(
747
 
                                                Doit.MESSAGE_CONTACTCREATED );
 
709
                        _contacts_cache.addLookup(
 
710
                                ContactsCache.createIdentifier( contact ), id );
 
711
 
 
712
                        // if we haven't already shown that we're overwriting a contact,
 
713
                        // show that we're creating a new contact
 
714
                        if( !ui_informed ) {
 
715
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
 
716
                                ui_informed = true;
748
717
                        }
749
 
                        else
750
 
                                // show that we're merging with an existing contact
751
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
752
 
 
753
 
                        // import contact parts
754
 
                        if( contact.hasNumbers() )
755
 
                                importContactPhones( id, contact.getNumbers() );
756
 
                        if( contact.hasEmails() )
757
 
                                importContactEmails( id, contact.getEmails() );
758
 
                        if( contact.hasAddresses() )
759
 
                                importContactAddresses( id, contact.getAddresses() );
760
 
                        if( contact.hasOrganisations() )
761
 
                                importContactOrganisations( id, contact.getOrganisations() );
762
 
                        if( contact.hasNotes() )
763
 
                                importContactNotes( id, contact.getNotes() );
764
 
                }
765
 
                catch( Backend.ContactCreationException e )
766
 
                {
767
 
                        showError( R.string.error_unabletoaddcontact );
768
 
                }
 
718
                }
 
719
 
 
720
                // if we haven't already shown that we're overwriting or creating a
 
721
                // contact show that we're merging a contact
 
722
                if( !ui_informed )
 
723
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
724
 
 
725
                // import contact parts
 
726
                if( contact.hasNumbers() )
 
727
                        importContactPhones( id, contact.getNumbers() );
 
728
                if( contact.hasEmails() )
 
729
                        importContactEmails( id, contact.getEmails() );
 
730
                if( contact.hasAddresses() )
 
731
                        importContactAddresses( id, contact.getAddresses() );
 
732
                if( contact.hasOrganisations() )
 
733
                        importContactOrganisations( id, contact.getOrganisations() );
769
734
        }
770
735
 
771
736
        private void importContactPhones( Long id,
772
 
                HashMap< String, ContactData.PreferredDetail > datas )
773
 
                throws ContactCreationException
 
737
                        HashMap< String, ContactData.PreferredDetail > datas )
774
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
 
775
745
                // add phone numbers
776
 
                Set< String > datas_keys = datas.keySet();
777
746
                Iterator< String > i = datas_keys.iterator();
778
747
                while( i.hasNext() ) {
779
748
                        String number = i.next();
790
759
                                continue;
791
760
 
792
761
                        // add phone number
793
 
                        _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 );
794
768
 
795
769
                        // and add this address to the cache to prevent a addition of
796
770
                        // duplicate date from another file
799
773
        }
800
774
 
801
775
        private void importContactEmails( Long id,
802
 
                HashMap< String, ContactData.PreferredDetail > datas )
803
 
                throws ContactCreationException
 
776
                        HashMap< String, ContactData.PreferredDetail > datas )
804
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
 
805
784
                // add email addresses
806
 
                Set< String > datas_keys = datas.keySet();
807
785
                Iterator< String > i = datas_keys.iterator();
808
786
                while( i.hasNext() ) {
809
787
                        String email = i.next();
815
793
                                continue;
816
794
 
817
795
                        // add phone number
818
 
                        _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 );
819
804
 
820
805
                        // and add this address to the cache to prevent a addition of
821
806
                        // duplicate date from another file
825
810
 
826
811
        private void importContactAddresses( Long id,
827
812
                HashMap< String, ContactData.TypeDetail > datas )
828
 
                throws ContactCreationException
829
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
 
830
819
                // add addresses
831
820
                Set< String > datas_keys = datas.keySet();
832
821
                Iterator< String > i = datas_keys.iterator();
840
829
                                continue;
841
830
 
842
831
                        // add postal address
843
 
                        _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 );
844
838
 
845
839
                        // and add this address to the cache to prevent a addition of
846
840
                        // duplicate date from another file
850
844
 
851
845
        private void importContactOrganisations( Long id,
852
846
                HashMap< String, ContactData.ExtraDetail > datas )
853
 
                throws ContactCreationException
854
847
        {
855
848
                // add addresses
856
849
                Set< String > datas_keys = datas.keySet();
865
858
                                continue;
866
859
 
867
860
                        // add organisation address
868
 
                        _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 );
869
870
 
870
871
                        // and add this address to the cache to prevent a addition of
871
872
                        // duplicate date from another file
873
874
                }
874
875
        }
875
876
 
876
 
        private void importContactNotes( Long id,
877
 
                HashSet< String > datas )
878
 
                throws ContactCreationException
879
 
        {
880
 
                // add notes
881
 
                Iterator< String > i = datas.iterator();
882
 
                while( i.hasNext() ) {
883
 
                        String note = i.next();
884
 
 
885
 
                        // we don't want to add this note if it exists already or we would
886
 
                        // introduce duplicates
887
 
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
888
 
                                continue;
889
 
 
890
 
                        // add note
891
 
                        _backend.addContactNote( id, note );
892
 
 
893
 
                        // and add this note to the cache to prevent a addition of duplicate
894
 
                        // date from another file
895
 
                        _contacts_cache.addAssociatedNote( id, note );
896
 
                }
897
 
 
898
 
        }
899
 
 
900
877
        synchronized protected void checkAbort() throws AbortImportException
901
878
        {
902
879
                if( _abort ) {