/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: Tim Marston
  • Date: 2015-02-25 00:19:29 UTC
  • Revision ID: tim@ed.am-20150225001929-cp6ji1c0dwf9zs1d
fixed date in NEWS

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 2013 Tim Marston <tim@ed.am>
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://www.waxworlds.org/edam/software/android/import-contacts
 
7
 * to as "this program").  For more information, see
 
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
26
import java.util.Arrays;
27
27
import java.util.HashMap;
28
28
import java.util.HashSet;
29
29
import java.util.Iterator;
 
30
import java.util.Locale;
30
31
import java.util.Set;
31
32
import java.util.regex.Matcher;
32
33
import java.util.regex.Pattern;
33
34
 
34
 
import android.content.ContentUris;
35
 
import android.content.ContentValues;
 
35
import am.ed.importcontacts.Backend.ContactCreationException;
36
36
import android.content.SharedPreferences;
37
 
import android.net.Uri;
38
37
import android.os.Message;
39
 
import android.provider.Contacts;
40
 
import android.provider.Contacts.PhonesColumns;
41
 
 
42
38
 
43
39
public class Importer extends Thread
44
40
{
59
55
        private boolean _abort = false;
60
56
        private boolean _is_finished = false;
61
57
        private ContactsCache _contacts_cache = null;
 
58
        private Backend _backend = null;
62
59
 
63
60
        /**
64
61
         * Data about a contact
65
62
         */
66
63
        public class ContactData
67
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
 
68
72
                class TypeDetail
69
73
                {
70
74
                        protected int _type;
137
141
                protected HashMap< String, PreferredDetail > _numbers = null;
138
142
                protected HashMap< String, PreferredDetail > _emails = null;
139
143
                protected HashMap< String, TypeDetail > _addresses = null;
 
144
                protected HashSet< String > _notes = null;
 
145
                protected String _birthday = null;
140
146
 
141
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
142
148
 
180
186
 
181
187
                        // if this is the first organisation added, or it's a preferred
182
188
                        // organisation and the current primary organisation isn't, then
183
 
                        // record this as the primary organisation.
 
189
                        // record this as the primary organisation
184
190
                        if( _primary_organisation == null ||
185
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
186
192
                        {
228
234
                                        new PreferredDetail( type, false ) );
229
235
 
230
236
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
231
 
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
232
 
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
 
237
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
233
238
 
234
239
                        // if this is the first number added, or it's a preferred number
235
240
                        // and the current primary number isn't, or this number is on equal
236
241
                        // standing with the primary number in terms of preference and it is
237
242
                        // a voice number and the primary number isn't, then record this as
238
 
                        // the primary number.
 
243
                        // the primary number
239
244
                        if( _primary_number == null ||
240
245
                                ( is_preferred && !_primary_number_is_preferred ) ||
241
246
                                ( is_preferred == _primary_number_is_preferred &&
274
279
                        email = sanitisesEmailAddress( email );
275
280
                        if( email == null )
276
281
                        {
277
 
                                // TODO: warn that an imported email addtrss is being ignored
 
282
                                // TODO: warn that an imported email address is being ignored
278
283
                                return;
279
284
                        }
280
285
 
287
292
 
288
293
                        // if this is the first email added, or it's a preferred email and
289
294
                        // the current primary organisation isn't, then record this as the
290
 
                        // primary email.
 
295
                        // primary email
291
296
                        if( _primary_email == null ||
292
297
                                ( is_preferred && !_primary_email_is_preferred ) )
293
298
                        {
341
346
                        return _addresses;
342
347
                }
343
348
 
 
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
 
344
381
                protected void finalise()
345
382
                        throws ContactNotIdentifiableException
346
383
                {
347
 
                        // ensure that if there is a primary number, it is preferred so
348
 
                        // that there is always one preferred number. Android will assign
 
384
                        // Ensure that if there is a primary number, it is preferred so
 
385
                        // that there is always one preferred number.  Android will assign
349
386
                        // preference to one anyway so we might as well decide one sensibly.
350
387
                        if( _primary_number != null ) {
351
388
                                PreferredDetail data = _numbers.get( _primary_number );
369
406
 
370
407
                        // create a cache identifier from this contact data, which can be
371
408
                        // used to look-up an existing contact
372
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
409
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
373
410
                        if( _cache_identifier == null )
374
411
                                throw new ContactNotIdentifiableException();
375
412
                }
396
433
                        Matcher m = p.matcher( email );
397
434
                        if( m.matches() ) {
398
435
                                String[] bits = email.split( "@" );
399
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
436
                                return bits[ 0 ] + "@" +
 
437
                                        bits[ 1 ].toLowerCase( Locale.ENGLISH );
400
438
                        }
401
439
                        return null;
402
440
                }
421
459
                        // update UI
422
460
                        setProgressMessage( R.string.doit_caching );
423
461
 
424
 
                        // build a cache of existing contacts
 
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
425
469
                        _contacts_cache = new ContactsCache();
426
 
                        _contacts_cache.buildCache( _doit );
 
470
                        _backend.populateCache( _contacts_cache );
427
471
 
428
472
                        // do the import
429
473
                        onImport();
500
544
                finish( ACTION_ABORT );
501
545
        }
502
546
 
503
 
        protected void showFatalError( int res ) throws AbortImportException
504
 
        {
505
 
                showFatalError( _doit.getText( res ).toString() );
506
 
        }
507
 
 
508
 
        synchronized protected void showFatalError( String message )
509
 
                        throws AbortImportException
510
 
        {
511
 
                checkAbort();
512
 
                _doit._handler.sendMessage( Message.obtain(
513
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
514
 
                try {
515
 
                        wait();
516
 
                }
517
 
                catch( InterruptedException e ) { }
518
 
 
519
 
                // no need to check if an abortion happened during the wait, we are
520
 
                // about to finish anyway!
521
 
                finish( ACTION_ABORT );
522
 
        }
523
 
 
524
 
        protected boolean showContinue( int res ) throws AbortImportException
525
 
        {
526
 
                return showContinue( _doit.getText( res ).toString() );
527
 
        }
528
 
 
529
 
        synchronized protected boolean showContinue( String message )
 
547
        protected void showContinueOrAbort( int res ) throws AbortImportException
 
548
        {
 
549
                showContinueOrAbort( _doit.getText( res ).toString() );
 
550
        }
 
551
 
 
552
        synchronized protected void showContinueOrAbort( String message )
530
553
                        throws AbortImportException
531
554
        {
532
555
                checkAbort();
537
560
                }
538
561
                catch( InterruptedException e ) { }
539
562
 
540
 
                // check if an abortion happened during the wait
541
 
                checkAbort();
542
 
 
543
 
                return _response == RESPONSE_POSITIVE;
 
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();
544
569
        }
545
570
 
546
571
        protected void setProgressMessage( int res ) throws AbortImportException
556
581
                checkAbort();
557
582
                _doit._handler.sendMessage( Message.obtain(
558
583
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
559
 
                        new Integer( max_progress ) ) );
 
584
                        Integer.valueOf( max_progress ) ) );
560
585
        }
561
586
 
562
587
        protected void setTmpProgress( int tmp_progress )
565
590
                checkAbort();
566
591
                _doit._handler.sendMessage( Message.obtain(
567
592
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
568
 
                        new Integer( tmp_progress ) ) );
 
593
                        Integer.valueOf( tmp_progress ) ) );
569
594
        }
570
595
 
571
596
        protected void setProgress( int progress ) throws AbortImportException
573
598
                checkAbort();
574
599
                _doit._handler.sendMessage( Message.obtain(
575
600
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
576
 
                        new Integer( progress ) ) );
 
601
                        Integer.valueOf( progress ) ) );
577
602
        }
578
603
 
579
604
        protected void finish( int action ) throws AbortImportException
597
622
                return _doit.getText( res );
598
623
        }
599
624
 
600
 
        synchronized private boolean checkForDuplicate(
601
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
602
 
                throws AbortImportException
 
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
603
637
        {
604
638
                _last_merge_decision = merge_setting;
605
639
 
606
 
                // it is ok to use contact.getCacheIdentifier(). The contact has already
607
 
                // been finalised, which means a valid cache identifier will have been
608
 
                // created for it (or it would have been skipped)
609
 
 
610
640
                // handle special cases
611
641
                switch( merge_setting )
612
642
                {
613
643
                case Doit.ACTION_KEEP:
614
 
                        // if we keep contacts on duplicate, we better check for one
615
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
644
                        // if we are skipping on a duplicate, check for one
 
645
                        return exists;
616
646
 
617
647
                case Doit.ACTION_PROMPT:
618
 
                        // if we are prompting on duplicate, we better check for one and if
619
 
                        // the contact doesn'te exist, we want to import it
620
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
621
 
                                return true;
 
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;
622
651
 
623
 
                        // ok, it exists, so do prompt
 
652
                        // ok, duplicate exists, so do prompt
624
653
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
625
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
654
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
626
655
                        try {
627
656
                                wait();
628
657
                        }
636
665
                                _merge_setting = _response;
637
666
 
638
667
                        // recurse, with our new merge setting
639
 
                        return checkForDuplicate( cache_identifier, _response );
 
668
                        return shouldWeSkipContact( contact_detail, exists, _response );
640
669
                }
641
670
 
642
 
                // for all other cases (either overwriting or merging) we will need the
643
 
                // imported data
644
 
                return true;
 
671
                // for all other cases (either overwriting or merging) we don't skip
 
672
                return false;
645
673
        }
646
674
 
647
675
        protected void skipContact() throws AbortImportException
648
676
        {
649
677
                checkAbort();
 
678
 
 
679
                // show that we're skipping a new contact
650
680
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
651
681
        }
652
682
 
655
685
        {
656
686
                checkAbort();
657
687
 
658
 
                // It is expected that we use contact.getCacheIdentifier() here. The
 
688
                // It is expected that we use contact.getCacheIdentifier() here.  The
659
689
                // contact we are passed should have been successfully finalise()d,
660
690
                // which includes generating a valid cache identifier.
661
691
                ContactsCache.CacheIdentifier cache_identifier =
662
692
                        contact.getCacheIdentifier();
663
693
 
664
 
                // check to see if this contact is a duplicate and should be skipped
665
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
666
 
                        skipContact();
667
 
                        return;
668
 
                }
669
 
 
670
694
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
671
695
//                      finish( ACTION_ABORT );
672
696
 
673
 
                // keep track of whether we've informed the UI of what we're doing
674
 
                boolean ui_informed = false;
675
 
 
676
697
                // attempt to lookup the id of an existing contact in the cache with
677
698
                // this contact data's cache identifier
678
699
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
679
700
 
680
 
                // does contact exist already?
681
 
                if( id != null )
682
 
                {
683
 
                        // should we skip this import altogether?
684
 
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
685
 
 
686
 
                        // get contact's URI
687
 
                        Uri contact_uri = ContentUris.withAppendedId(
688
 
                                Contacts.People.CONTENT_URI, id );
689
 
 
690
 
                        // should we destroy the existing contact before importing?
691
 
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
701
                // check to see if this contact should be skipped
 
702
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
 
703
                        _merge_setting ) )
 
704
                {
 
705
                        // show that we're skipping a contact
 
706
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
707
                        return;
 
708
                }
 
709
 
 
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 )
 
714
                {
 
715
                        contact_deleted = true;
 
716
 
 
717
                        // remove from device
 
718
                        _backend.deleteContact( id );
 
719
 
 
720
                        // 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 )
692
735
                        {
693
 
                                // remove from device
694
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
736
                                // create a new contact
 
737
                                id = _backend.addContact( contact._name );
695
738
 
696
739
                                // update cache
697
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
698
 
                                _contacts_cache.removeAssociatedData( id );
699
 
 
700
 
                                // show that we're overwriting a contact
701
 
                                _doit._handler.sendEmptyMessage(
702
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
703
 
                                ui_informed = true;
704
 
 
705
 
                                // discard the contact id
706
 
                                id = null;
 
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 );
707
747
                        }
 
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() );
708
765
                }
709
 
 
710
 
                // if we don't have a contact id yet (or if we did, but we destroyed it
711
 
                // when we deleted the contact), we'll have to create a new contact
712
 
                if( id == null )
 
766
                catch( Backend.ContactCreationException e )
713
767
                {
714
 
                        // create a new contact
715
 
                        ContentValues values = new ContentValues();
716
 
                        values.put( Contacts.People.NAME, contact._name );
717
 
                        Uri contact_uri = _doit.getContentResolver().insert(
718
 
                                Contacts.People.CONTENT_URI, values );
719
 
                        id = ContentUris.parseId( contact_uri );
720
 
                        if( id == null || id <= 0 )
721
 
                                showError( R.string.error_unabletoaddcontact );
722
 
 
723
 
                        // try to add them to the "My Contacts" group
724
 
                        try {
725
 
                                Contacts.People.addToMyContactsGroup(
726
 
                                        _doit.getContentResolver(), id );
727
 
                        }
728
 
                        catch( IllegalStateException e ) {
729
 
                                // ignore any failure
730
 
                        }
731
 
 
732
 
                        // update cache
733
 
                        _contacts_cache.addLookup(
734
 
                                ContactsCache.createIdentifier( contact ), id );
735
 
 
736
 
                        // if we haven't already shown that we're overwriting a contact,
737
 
                        // show that we're creating a new contact
738
 
                        if( !ui_informed ) {
739
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
740
 
                                ui_informed = true;
741
 
                        }
 
768
                        showError( R.string.error_unabletoaddcontact );
742
769
                }
743
 
 
744
 
                // if we haven't already shown that we're overwriting or creating a
745
 
                // contact, show that we're merging a contact
746
 
                if( !ui_informed )
747
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
748
 
 
749
 
                // import contact parts
750
 
                if( contact.hasNumbers() )
751
 
                        importContactPhones( id, contact.getNumbers() );
752
 
                if( contact.hasEmails() )
753
 
                        importContactEmails( id, contact.getEmails() );
754
 
                if( contact.hasAddresses() )
755
 
                        importContactAddresses( id, contact.getAddresses() );
756
 
                if( contact.hasOrganisations() )
757
 
                        importContactOrganisations( id, contact.getOrganisations() );
758
770
        }
759
771
 
760
772
        private void importContactPhones( Long id,
761
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
773
                HashMap< String, ContactData.PreferredDetail > datas )
 
774
                throws ContactCreationException
762
775
        {
763
 
                // get URI to contact's phones
764
 
                Uri contact_phones_uri = Uri.withAppendedPath(
765
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
766
 
                        Contacts.People.Phones.CONTENT_DIRECTORY );
767
 
                Set< String > datas_keys = datas.keySet();
768
 
 
769
776
                // add phone numbers
 
777
                Set< String > datas_keys = datas.keySet();
770
778
                Iterator< String > i = datas_keys.iterator();
771
779
                while( i.hasNext() ) {
772
780
                        String number = i.next();
773
781
                        ContactData.PreferredDetail data = datas.get( number );
774
782
 
775
 
                        // we don't want to add this number if it's crap, or it already
776
 
                        // exists (which would cause a duplicate to be created). We don't
777
 
                        // take in to account the type when checking for duplicates. This is
778
 
                        // intentional: types aren't really very reliable. We assume that
779
 
                        // if the number exists at all, it doesn't need importing. Because
780
 
                        // of this, we also can't update the cache (which we don't need to
781
 
                        // anyway, so it's not a problem).
 
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).
782
790
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
783
791
                                continue;
784
792
 
785
793
                        // add phone number
786
 
                        ContentValues values = new ContentValues();
787
 
                        values.put( Contacts.Phones.TYPE, data.getType() );
788
 
                        values.put( Contacts.Phones.NUMBER, number );
789
 
                        if( data.isPreferred() )
790
 
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
791
 
                        _doit.getContentResolver().insert( contact_phones_uri, values );
 
794
                        _backend.addContactPhone( id, number, data );
792
795
 
793
796
                        // and add this address to the cache to prevent a addition of
794
797
                        // duplicate date from another file
797
800
        }
798
801
 
799
802
        private void importContactEmails( Long id,
800
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
803
                HashMap< String, ContactData.PreferredDetail > datas )
 
804
                throws ContactCreationException
801
805
        {
802
 
                // get URI to contact's contact methods
803
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
804
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
805
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
806
 
                Set< String > datas_keys = datas.keySet();
807
 
 
808
806
                // add email addresses
 
807
                Set< String > datas_keys = datas.keySet();
809
808
                Iterator< String > i = datas_keys.iterator();
810
809
                while( i.hasNext() ) {
811
810
                        String email = i.next();
812
811
                        ContactData.PreferredDetail data = datas.get( email );
813
812
 
814
813
                        // we don't want to add this email address if it exists already or
815
 
                        // we would introduce duplicates.
 
814
                        // we would introduce duplicates
816
815
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
817
816
                                continue;
818
817
 
819
818
                        // add phone number
820
 
                        ContentValues values = new ContentValues();
821
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
822
 
                        values.put( Contacts.ContactMethods.DATA, email );
823
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
824
 
                        if( data.isPreferred() )
825
 
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
826
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
827
 
                                values );
 
819
                        _backend.addContactEmail( id, email, data );
828
820
 
829
821
                        // and add this address to the cache to prevent a addition of
830
822
                        // duplicate date from another file
834
826
 
835
827
        private void importContactAddresses( Long id,
836
828
                HashMap< String, ContactData.TypeDetail > datas )
 
829
                throws ContactCreationException
837
830
        {
838
 
                // get URI to contact's contact methods
839
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
840
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
841
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
842
 
 
843
831
                // add addresses
844
832
                Set< String > datas_keys = datas.keySet();
845
833
                Iterator< String > i = datas_keys.iterator();
853
841
                                continue;
854
842
 
855
843
                        // add postal address
856
 
                        ContentValues values = new ContentValues();
857
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
858
 
                        values.put( Contacts.ContactMethods.DATA, address );
859
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
860
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
861
 
                                values );
 
844
                        _backend.addContactAddresses( id, address, data );
862
845
 
863
846
                        // and add this address to the cache to prevent a addition of
864
847
                        // duplicate date from another file
868
851
 
869
852
        private void importContactOrganisations( Long id,
870
853
                HashMap< String, ContactData.ExtraDetail > datas )
 
854
                throws ContactCreationException
871
855
        {
872
856
                // add addresses
873
857
                Set< String > datas_keys = datas.keySet();
882
866
                                continue;
883
867
 
884
868
                        // add organisation address
885
 
                        ContentValues values = new ContentValues();
886
 
                        values.put( Contacts.Organizations.PERSON_ID, id );
887
 
                        values.put( Contacts.Organizations.COMPANY, organisation );
888
 
                        values.put( Contacts.ContactMethods.TYPE,
889
 
                                Contacts.OrganizationColumns.TYPE_WORK );
890
 
                        if( data.getExtra() != null )
891
 
                                values.put( Contacts.Organizations.TITLE, data.getExtra() );
892
 
                        _doit.getContentResolver().insert(
893
 
                                Contacts.Organizations.CONTENT_URI, values );
 
869
                        _backend.addContactOrganisation( id, organisation, data );
894
870
 
895
871
                        // and add this address to the cache to prevent a addition of
896
872
                        // duplicate date from another file
898
874
                }
899
875
        }
900
876
 
 
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
 
901
913
        synchronized protected void checkAbort() throws AbortImportException
902
914
        {
903
915
                if( _abort ) {