/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-06-04 13:16:20 UTC
  • Revision ID: edam@waxworlds.org-20110604131620-hean9j66r3f5i72h
- fix UTF-8 unencoding by default on v3.0 vCards
- proper unescaping of strings in v3.0 vCards (also works for v2.1, although the 2.1 specs don't mention it)

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2013 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
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
7
 * to as "this program"). For more information, see
 
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
59
 
60
60
        /**
61
61
         * Data about a contact
62
62
         */
63
63
        public class ContactData
64
64
        {
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
65
                class TypeDetail
73
66
                {
74
67
                        protected int _type;
131
124
 
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
 
                protected String _birthday = null;
146
136
 
147
137
                private ContactsCache.CacheIdentifier _cache_identifier = null;
148
138
 
185
175
                                        new ExtraDetail( 0, false, title ) );
186
176
 
187
177
                        // if this is the first organisation added, or it's a preferred
188
 
                        // organisation and the current primary organisation isn't, then
189
 
                        // record this as the primary organisation
 
178
                        // organisation and a previous organisation wasn't, then remember
 
179
                        // that this is the "primary organisation".
190
180
                        if( _primary_organisation == null ||
191
181
                                ( is_preferred && !_primary_organisation_is_preferred ) )
192
182
                        {
233
223
                                _numbers.put( number,
234
224
                                        new PreferredDetail( type, false ) );
235
225
 
236
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
237
 
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
238
 
 
239
226
                        // if this is the first number added, or it's a preferred number
240
 
                        // and the current primary number isn't, or this number is on equal
241
 
                        // standing with the primary number in terms of preference and it is
242
 
                        // a voice number and the primary number isn't, then record this as
243
 
                        // the primary number
 
227
                        // and a previous number wasn't, then remember that this is the
 
228
                        // "primary number".
244
229
                        if( _primary_number == null ||
245
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
246
 
                                ( is_preferred == _primary_number_is_preferred &&
247
 
                                        !non_voice_types.contains( type ) &&
248
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
230
                                ( is_preferred && !_primary_number_is_preferred ) )
249
231
                        {
250
232
                                _primary_number = number;
251
 
                                _primary_number_type = type;
252
233
                                _primary_number_is_preferred = is_preferred;
253
234
                        }
254
235
                }
279
260
                        email = sanitisesEmailAddress( email );
280
261
                        if( email == null )
281
262
                        {
282
 
                                // TODO: warn that an imported email address is being ignored
 
263
                                // TODO: warn that an imported email addtrss is being ignored
283
264
                                return;
284
265
                        }
285
266
 
290
271
                        if( !_emails.containsKey( email ) )
291
272
                                _emails.put( email, new PreferredDetail( type, false ) );
292
273
 
293
 
                        // if this is the first email added, or it's a preferred email and
294
 
                        // the current primary organisation isn't, then record this as the
295
 
                        // 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".
296
277
                        if( _primary_email == null ||
297
278
                                ( is_preferred && !_primary_email_is_preferred ) )
298
279
                        {
346
327
                        return _addresses;
347
328
                }
348
329
 
349
 
                protected void addNote( String note )
350
 
                {
351
 
                        if( _notes == null ) _notes = new HashSet< String >();
352
 
                        if( !_notes.contains( note ) )
353
 
                                _notes.add( note );
354
 
                }
355
 
 
356
 
                public boolean hasNotes()
357
 
                {
358
 
                        return _notes != null && _notes.size() > 0;
359
 
                }
360
 
 
361
 
                public HashSet< String > getNotes()
362
 
                {
363
 
                        return _notes;
364
 
                }
365
 
 
366
 
                public void setBirthday( String birthday )
367
 
                {
368
 
                        _birthday = birthday;
369
 
                }
370
 
 
371
 
                public boolean hasBirthday()
372
 
                {
373
 
                        return _birthday != null;
374
 
                }
375
 
 
376
 
                public String getBirthday()
377
 
                {
378
 
                        return _birthday;
379
 
                }
380
 
 
381
330
                protected void finalise()
382
331
                        throws ContactNotIdentifiableException
383
332
                {
384
 
                        // Ensure that if there is a primary number, it is preferred so
385
 
                        // that there is always one preferred number.  Android will assign
 
333
                        // ensure that if there is a primary number, it is preferred so
 
334
                        // that there is always one preferred number. Android will assign
386
335
                        // preference to one anyway so we might as well decide one sensibly.
387
336
                        if( _primary_number != null ) {
388
337
                                PreferredDetail data = _numbers.get( _primary_number );
406
355
 
407
356
                        // create a cache identifier from this contact data, which can be
408
357
                        // used to look-up an existing contact
409
 
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
358
                        _cache_identifier = ContactsCache.createIdentifier( this );
410
359
                        if( _cache_identifier == null )
411
360
                                throw new ContactNotIdentifiableException();
412
361
                }
433
382
                        Matcher m = p.matcher( email );
434
383
                        if( m.matches() ) {
435
384
                                String[] bits = email.split( "@" );
436
 
                                return bits[ 0 ] + "@" +
437
 
                                        bits[ 1 ].toLowerCase( Locale.ENGLISH );
 
385
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
438
386
                        }
439
387
                        return null;
440
388
                }
459
407
                        // update UI
460
408
                        setProgressMessage( R.string.doit_caching );
461
409
 
462
 
                        // create the appropriate backend
463
 
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
464
 
                                _backend = new ContactsContractBackend( _doit );
465
 
                        else
466
 
                                _backend = new ContactsBackend( _doit );
467
 
 
468
 
                        // create a cache of existing contacts and populate it
 
410
                        // build a cache of existing contacts
469
411
                        _contacts_cache = new ContactsCache();
470
 
                        _backend.populateCache( _contacts_cache );
 
412
                        _contacts_cache.buildCache( _doit );
471
413
 
472
414
                        // do the import
473
415
                        onImport();
544
486
                finish( ACTION_ABORT );
545
487
        }
546
488
 
547
 
        protected void showContinueOrAbort( int res ) throws AbortImportException
548
 
        {
549
 
                showContinueOrAbort( _doit.getText( res ).toString() );
550
 
        }
551
 
 
552
 
        synchronized protected void showContinueOrAbort( String message )
 
489
        protected void showFatalError( int res ) throws AbortImportException
 
490
        {
 
491
                showFatalError( _doit.getText( res ).toString() );
 
492
        }
 
493
 
 
494
        synchronized protected void showFatalError( String message )
 
495
                        throws AbortImportException
 
496
        {
 
497
                checkAbort();
 
498
                _doit._handler.sendMessage( Message.obtain(
 
499
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
500
                try {
 
501
                        wait();
 
502
                }
 
503
                catch( InterruptedException e ) { }
 
504
 
 
505
                // no need to check if an abortion happened during the wait, we are
 
506
                // about to finish anyway!
 
507
                finish( ACTION_ABORT );
 
508
        }
 
509
 
 
510
        protected boolean showContinue( int res ) throws AbortImportException
 
511
        {
 
512
                return showContinue( _doit.getText( res ).toString() );
 
513
        }
 
514
 
 
515
        synchronized protected boolean showContinue( String message )
553
516
                        throws AbortImportException
554
517
        {
555
518
                checkAbort();
560
523
                }
561
524
                catch( InterruptedException e ) { }
562
525
 
563
 
                // if we're aborting, there's no need to check if an abortion happened
564
 
                // during the wait
565
 
                if( _response == RESPONSE_NEGATIVE )
566
 
                        finish( ACTION_ABORT );
567
 
                else
568
 
                        checkAbort();
 
526
                // check if an abortion happened during the wait
 
527
                checkAbort();
 
528
 
 
529
                return _response == RESPONSE_POSITIVE;
569
530
        }
570
531
 
571
532
        protected void setProgressMessage( int res ) throws AbortImportException
581
542
                checkAbort();
582
543
                _doit._handler.sendMessage( Message.obtain(
583
544
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
584
 
                        Integer.valueOf( max_progress ) ) );
 
545
                        new Integer( max_progress ) ) );
585
546
        }
586
547
 
587
548
        protected void setTmpProgress( int tmp_progress )
590
551
                checkAbort();
591
552
                _doit._handler.sendMessage( Message.obtain(
592
553
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
593
 
                        Integer.valueOf( tmp_progress ) ) );
 
554
                        new Integer( tmp_progress ) ) );
594
555
        }
595
556
 
596
557
        protected void setProgress( int progress ) throws AbortImportException
598
559
                checkAbort();
599
560
                _doit._handler.sendMessage( Message.obtain(
600
561
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
601
 
                        Integer.valueOf( progress ) ) );
 
562
                        new Integer( progress ) ) );
602
563
        }
603
564
 
604
565
        protected void finish( int action ) throws AbortImportException
622
583
                return _doit.getText( res );
623
584
        }
624
585
 
625
 
        /**
626
 
         * Should we skip a contact, given whether it exists or not and the current
627
 
         * merge setting?  This routine handles throwing up a prompt, if required.
628
 
         *
629
 
         * @param contact_detail the display name of the contact
630
 
         * @param exists true if this contact matches one in the cache
631
 
         * @param merge_setting the merge setting to use
632
 
         * @return true if the contact should be skipped outright
633
 
         * @throws AbortImportException
634
 
         */
635
 
        synchronized private boolean shouldWeSkipContact( String contact_detail,
636
 
                boolean exists, int merge_setting ) throws AbortImportException
 
586
        synchronized private boolean checkForDuplicate(
 
587
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
 
588
                throws AbortImportException
637
589
        {
638
590
                _last_merge_decision = merge_setting;
639
591
 
 
592
                // it is ok to use contact.getCacheIdentifier(). The contact has already
 
593
                // been finalised, which means a valid cache identifier will have been
 
594
                // created for it (or it would have been skipped)
 
595
 
640
596
                // handle special cases
641
597
                switch( merge_setting )
642
598
                {
643
599
                case Doit.ACTION_KEEP:
644
 
                        // if we are skipping on a duplicate, check for one
645
 
                        return exists;
 
600
                        // if we keep contacts on duplicate, we better check for one
 
601
                        return !_contacts_cache.canLookup( cache_identifier );
646
602
 
647
603
                case Doit.ACTION_PROMPT:
648
 
                        // if we are prompting on duplicate, then we can say that we won't
649
 
                        // skip if there isn't one
650
 
                        if( !exists ) return false;
 
604
                        // if we are prompting on duplicate, we better check for one and if
 
605
                        // the contact doesn'te exist, we want to import it
 
606
                        if( !_contacts_cache.canLookup( cache_identifier ) )
 
607
                                return true;
651
608
 
652
 
                        // ok, duplicate exists, so do prompt
 
609
                        // ok, it exists, so do prompt
653
610
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
654
 
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
 
611
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
655
612
                        try {
656
613
                                wait();
657
614
                        }
665
622
                                _merge_setting = _response;
666
623
 
667
624
                        // recurse, with our new merge setting
668
 
                        return shouldWeSkipContact( contact_detail, exists, _response );
 
625
                        return checkForDuplicate( cache_identifier, _response );
669
626
                }
670
627
 
671
 
                // for all other cases (either overwriting or merging) we don't skip
672
 
                return false;
 
628
                // for all other cases (either overwriting or merging) we will need the
 
629
                // imported data
 
630
                return true;
673
631
        }
674
632
 
675
633
        protected void skipContact() throws AbortImportException
676
634
        {
677
635
                checkAbort();
678
 
 
679
 
                // show that we're skipping a new contact
680
636
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
681
637
        }
682
638
 
685
641
        {
686
642
                checkAbort();
687
643
 
688
 
                // It is expected that we use contact.getCacheIdentifier() here.  The
 
644
                // It is expected that we use contact.getCacheIdentifier() here. The
689
645
                // contact we are passed should have been successfully finalise()d,
690
646
                // which includes generating a valid cache identifier.
691
647
                ContactsCache.CacheIdentifier cache_identifier =
692
648
                        contact.getCacheIdentifier();
693
649
 
 
650
                // check to see if this contact is a duplicate and should be skipped
 
651
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
 
652
                        skipContact();
 
653
                        return;
 
654
                }
 
655
 
694
656
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
695
657
//                      finish( ACTION_ABORT );
696
658
 
 
659
                // keep track of whether we've informed the UI of what we're doing
 
660
                boolean ui_informed = false;
 
661
 
697
662
                // attempt to lookup the id of an existing contact in the cache with
698
663
                // this contact data's cache identifier
699
664
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
700
665
 
701
 
                // check to see if this contact should be skipped
702
 
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
703
 
                        _merge_setting ) )
 
666
                // does contact exist already?
 
667
                if( id != null )
704
668
                {
705
 
                        // show that we're skipping a contact
706
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
707
 
                        return;
 
669
                        // should we skip this import altogether?
 
670
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
 
671
 
 
672
                        // get contact's URI
 
673
                        Uri contact_uri = ContentUris.withAppendedId(
 
674
                                Contacts.People.CONTENT_URI, id );
 
675
 
 
676
                        // should we destroy the existing contact before importing?
 
677
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
678
                        {
 
679
                                // remove from device
 
680
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
681
 
 
682
                                // update cache
 
683
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
 
684
                                _contacts_cache.removeAssociatedData( id );
 
685
 
 
686
                                // show that we're overwriting a contact
 
687
                                _doit._handler.sendEmptyMessage(
 
688
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
689
                                ui_informed = true;
 
690
 
 
691
                                // discard the contact id
 
692
                                id = null;
 
693
                        }
708
694
                }
709
695
 
710
 
                // if a contact exists, and we're overwriting, destroy the existing
711
 
                // contact before importing
712
 
                boolean contact_deleted = false;
713
 
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
696
                // if we don't have a contact id yet (or if we did, but we destroyed it
 
697
                // when we deleted the contact), we'll have to create a new contact
 
698
                if( id == null )
714
699
                {
715
 
                        contact_deleted = true;
 
700
                        // 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 )
 
707
                                showError( R.string.error_unabletoaddcontact );
716
708
 
717
 
                        // remove from device
718
 
                        _backend.deleteContact( id );
 
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
                        }
719
717
 
720
718
                        // update cache
721
 
                        _contacts_cache.removeLookup( cache_identifier );
722
 
                        _contacts_cache.removeAssociatedData( id );
723
 
 
724
 
                        // show that we're overwriting a contact
725
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
726
 
 
727
 
                        // discard the contact id
728
 
                        id = null;
729
 
                }
730
 
 
731
 
                try {
732
 
                        // if we don't have a contact id yet (or we did, but we destroyed it
733
 
                        // when we deleted the contact), we'll have to create a new contact
734
 
                        if( id == null )
735
 
                        {
736
 
                                // create a new contact
737
 
                                id = _backend.addContact( contact._name );
738
 
 
739
 
                                // update cache
740
 
                                _contacts_cache.addLookup( cache_identifier, id );
741
 
 
742
 
                                // if we haven't already shown that we're overwriting a contact,
743
 
                                // show that we're creating a new contact
744
 
                                if( !contact_deleted )
745
 
                                        _doit._handler.sendEmptyMessage(
746
 
                                                Doit.MESSAGE_CONTACTCREATED );
 
719
                        _contacts_cache.addLookup(
 
720
                                ContactsCache.createIdentifier( contact ), id );
 
721
 
 
722
                        // if we haven't already shown that we're overwriting a contact,
 
723
                        // show that we're creating a new contact
 
724
                        if( !ui_informed ) {
 
725
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
 
726
                                ui_informed = true;
747
727
                        }
748
 
                        else
749
 
                                // show that we're merging with an existing contact
750
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
751
 
 
752
 
                        // import contact parts
753
 
                        if( contact.hasNumbers() )
754
 
                                importContactPhones( id, contact.getNumbers() );
755
 
                        if( contact.hasEmails() )
756
 
                                importContactEmails( id, contact.getEmails() );
757
 
                        if( contact.hasAddresses() )
758
 
                                importContactAddresses( id, contact.getAddresses() );
759
 
                        if( contact.hasOrganisations() )
760
 
                                importContactOrganisations( id, contact.getOrganisations() );
761
 
                        if( contact.hasNotes() )
762
 
                                importContactNotes( id, contact.getNotes() );
763
 
                        if( contact.hasBirthday() )
764
 
                                importContactBirthday( id, contact.getBirthday() );
765
 
                }
766
 
                catch( Backend.ContactCreationException e )
767
 
                {
768
 
                        showError( R.string.error_unabletoaddcontact );
769
 
                }
 
728
                }
 
729
 
 
730
                // if we haven't already shown that we're overwriting or creating a
 
731
                // contact, show that we're merging a contact
 
732
                if( !ui_informed )
 
733
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
734
 
 
735
                // import contact parts
 
736
                if( contact.hasNumbers() )
 
737
                        importContactPhones( id, contact.getNumbers() );
 
738
                if( contact.hasEmails() )
 
739
                        importContactEmails( id, contact.getEmails() );
 
740
                if( contact.hasAddresses() )
 
741
                        importContactAddresses( id, contact.getAddresses() );
 
742
                if( contact.hasOrganisations() )
 
743
                        importContactOrganisations( id, contact.getOrganisations() );
770
744
        }
771
745
 
772
746
        private void importContactPhones( Long id,
773
 
                HashMap< String, ContactData.PreferredDetail > datas )
774
 
                throws ContactCreationException
 
747
                        HashMap< String, ContactData.PreferredDetail > datas )
775
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
 
776
755
                // add phone numbers
777
 
                Set< String > datas_keys = datas.keySet();
778
756
                Iterator< String > i = datas_keys.iterator();
779
757
                while( i.hasNext() ) {
780
758
                        String number = i.next();
781
759
                        ContactData.PreferredDetail data = datas.get( number );
782
760
 
783
 
                        // We don't want to add this number if it's crap, or it already
784
 
                        // exists (which would cause a duplicate to be created).  We don't
785
 
                        // take in to account the type when checking for duplicates.  This
786
 
                        // is intentional: types aren't really very reliable.  We assume
787
 
                        // that if the number exists at all, it doesn't need importing.
788
 
                        // Because of this, we also can't update the cache (which we don't
789
 
                        // need to anyway, so it's not a problem).
 
761
                        // we don't want to add this number if it's crap, or it already
 
762
                        // exists (which would cause a duplicate to be created). We don't
 
763
                        // take in to account the type when checking for duplicates. This is
 
764
                        // intentional: types aren't really very reliable. We assume that
 
765
                        // if the number exists at all, it doesn't need importing. Because
 
766
                        // of this, we also can't update the cache (which we don't need to
 
767
                        // anyway, so it's not a problem).
790
768
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
791
769
                                continue;
792
770
 
793
771
                        // add phone number
794
 
                        _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 );
795
778
 
796
779
                        // and add this address to the cache to prevent a addition of
797
780
                        // duplicate date from another file
800
783
        }
801
784
 
802
785
        private void importContactEmails( Long id,
803
 
                HashMap< String, ContactData.PreferredDetail > datas )
804
 
                throws ContactCreationException
 
786
                        HashMap< String, ContactData.PreferredDetail > datas )
805
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
 
806
794
                // add email addresses
807
 
                Set< String > datas_keys = datas.keySet();
808
795
                Iterator< String > i = datas_keys.iterator();
809
796
                while( i.hasNext() ) {
810
797
                        String email = i.next();
811
798
                        ContactData.PreferredDetail data = datas.get( email );
812
799
 
813
800
                        // we don't want to add this email address if it exists already or
814
 
                        // we would introduce duplicates
 
801
                        // we would introduce duplicates.
815
802
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
816
803
                                continue;
817
804
 
818
805
                        // add phone number
819
 
                        _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 );
820
814
 
821
815
                        // and add this address to the cache to prevent a addition of
822
816
                        // duplicate date from another file
826
820
 
827
821
        private void importContactAddresses( Long id,
828
822
                HashMap< String, ContactData.TypeDetail > datas )
829
 
                throws ContactCreationException
830
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
 
831
829
                // add addresses
832
830
                Set< String > datas_keys = datas.keySet();
833
831
                Iterator< String > i = datas_keys.iterator();
841
839
                                continue;
842
840
 
843
841
                        // add postal address
844
 
                        _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 );
845
848
 
846
849
                        // and add this address to the cache to prevent a addition of
847
850
                        // duplicate date from another file
851
854
 
852
855
        private void importContactOrganisations( Long id,
853
856
                HashMap< String, ContactData.ExtraDetail > datas )
854
 
                throws ContactCreationException
855
857
        {
856
858
                // add addresses
857
859
                Set< String > datas_keys = datas.keySet();
866
868
                                continue;
867
869
 
868
870
                        // add organisation address
869
 
                        _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 );
870
880
 
871
881
                        // and add this address to the cache to prevent a addition of
872
882
                        // duplicate date from another file
874
884
                }
875
885
        }
876
886
 
877
 
        private void importContactNotes( Long id, 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
 
        private void importContactBirthday( Long id, String birthday )
900
 
                throws ContactCreationException
901
 
        {
902
 
                // we don't want to import this birthday if it already exists
903
 
                if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
904
 
                        return;
905
 
 
906
 
                // add birthday
907
 
                _backend.addContactBirthday( id, birthday );
908
 
 
909
 
                // and update the cache
910
 
                _contacts_cache.addAssociatedBirthday( id, birthday );
911
 
        }
912
 
 
913
887
        synchronized protected void checkAbort() throws AbortImportException
914
888
        {
915
889
                if( _abort ) {