/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: 2014-03-02 10:58:35 UTC
  • Revision ID: tim@ed.am-20140302105835-szgzmrvr7pa5kljw
updated 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
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
 
30
import java.util.Locale;
28
31
import java.util.Set;
29
32
import java.util.regex.Matcher;
30
33
import java.util.regex.Pattern;
31
34
 
32
 
import android.content.ContentUris;
33
 
import android.content.ContentValues;
 
35
import am.ed.importcontacts.Backend.ContactCreationException;
34
36
import android.content.SharedPreferences;
35
 
import android.net.Uri;
36
37
import android.os.Message;
37
 
import android.provider.Contacts;
38
 
 
39
38
 
40
39
public class Importer extends Thread
41
40
{
56
55
        private boolean _abort = false;
57
56
        private boolean _is_finished = false;
58
57
        private ContactsCache _contacts_cache = null;
59
 
 
60
 
        @SuppressWarnings("serial")
61
 
        protected class ContactNeedsMoreInfoException extends Exception
62
 
        {
63
 
        }
 
58
        private Backend _backend = null;
64
59
 
65
60
        /**
66
61
         * Data about a contact
67
62
         */
68
63
        public class ContactData
69
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
 
70
72
                class TypeDetail
71
73
                {
72
74
                        protected int _type;
122
124
                        }
123
125
                }
124
126
 
 
127
                @SuppressWarnings("serial")
 
128
                protected class ContactNotIdentifiableException extends Exception
 
129
                {
 
130
                }
 
131
 
125
132
                protected String _name = null;
126
133
                protected String _primary_organisation = null;
127
 
                protected boolean _primary_organisation_is_preferred = false;
 
134
                protected boolean _primary_organisation_is_preferred;
128
135
                protected String _primary_number = null;
129
 
                protected boolean _primary_number_is_preferred = false;
 
136
                protected int _primary_number_type;
 
137
                protected boolean _primary_number_is_preferred;
130
138
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred = false;
 
139
                protected boolean _primary_email_is_preferred;
132
140
                protected HashMap< String, ExtraDetail > _organisations = null;
133
141
                protected HashMap< String, PreferredDetail > _numbers = null;
134
142
                protected HashMap< String, PreferredDetail > _emails = null;
135
143
                protected HashMap< String, TypeDetail > _addresses = null;
 
144
                protected HashSet< String > _notes = null;
 
145
                protected String _birthday = null;
 
146
 
 
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
136
148
 
137
149
                protected void setName( String name )
138
150
                {
173
185
                                        new ExtraDetail( 0, false, title ) );
174
186
 
175
187
                        // if this is the first organisation added, or it's a preferred
176
 
                        // organisation and a previous organisation wasn't, then remember
177
 
                        // that this is the "primary organisation".
 
188
                        // organisation and the current primary organisation isn't, then
 
189
                        // record this as the primary organisation
178
190
                        if( _primary_organisation == null ||
179
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
180
192
                        {
221
233
                                _numbers.put( number,
222
234
                                        new PreferredDetail( type, false ) );
223
235
 
 
236
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
 
237
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
238
 
224
239
                        // if this is the first number added, or it's a preferred number
225
 
                        // and a previous number wasn't, then remember that this is the
226
 
                        // "primary 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
244
                        if( _primary_number == null ||
228
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
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 ) ) )
229
249
                        {
230
250
                                _primary_number = number;
 
251
                                _primary_number_type = type;
231
252
                                _primary_number_is_preferred = is_preferred;
232
253
                        }
233
254
                }
258
279
                        email = sanitisesEmailAddress( email );
259
280
                        if( email == null )
260
281
                        {
261
 
                                // TODO: warn that an imported email addtrss is being ignored
 
282
                                // TODO: warn that an imported email address is being ignored
262
283
                                return;
263
284
                        }
264
285
 
269
290
                        if( !_emails.containsKey( email ) )
270
291
                                _emails.put( email, new PreferredDetail( type, false ) );
271
292
 
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".
 
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
275
296
                        if( _primary_email == null ||
276
297
                                ( is_preferred && !_primary_email_is_preferred ) )
277
298
                        {
325
346
                        return _addresses;
326
347
                }
327
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
 
328
381
                protected void finalise()
 
382
                        throws ContactNotIdentifiableException
329
383
                {
330
 
                        // ensure that if there is a primary number, it is preferred so
331
 
                        // 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
332
386
                        // preference to one anyway so we might as well decide one sensibly.
333
387
                        if( _primary_number != null ) {
334
388
                                PreferredDetail data = _numbers.get( _primary_number );
349
403
                                _organisations.put( _primary_organisation,
350
404
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
405
                        }
 
406
 
 
407
                        // create a cache identifier from this contact data, which can be
 
408
                        // used to look-up an existing contact
 
409
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
410
                        if( _cache_identifier == null )
 
411
                                throw new ContactNotIdentifiableException();
 
412
                }
 
413
 
 
414
                public ContactsCache.CacheIdentifier getCacheIdentifier()
 
415
                {
 
416
                        return _cache_identifier;
352
417
                }
353
418
 
354
419
                private String sanitisePhoneNumber( String number )
368
433
                        Matcher m = p.matcher( email );
369
434
                        if( m.matches() ) {
370
435
                                String[] bits = email.split( "@" );
371
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
436
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
372
437
                        }
373
438
                        return null;
374
439
                }
393
458
                        // update UI
394
459
                        setProgressMessage( R.string.doit_caching );
395
460
 
396
 
                        // build a cache of existing contacts
 
461
                        // create the appropriate backend
 
462
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
463
                                _backend = new ContactsContractBackend( _doit );
 
464
                        else
 
465
                                _backend = new ContactsBackend( _doit );
 
466
 
 
467
                        // create a cache of existing contacts and populate it
397
468
                        _contacts_cache = new ContactsCache();
398
 
                        _contacts_cache.buildCache( _doit );
 
469
                        _backend.populateCache( _contacts_cache );
399
470
 
400
471
                        // do the import
401
472
                        onImport();
528
599
                checkAbort();
529
600
                _doit._handler.sendMessage( Message.obtain(
530
601
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
 
                        new Integer( max_progress ) ) );
 
602
                        Integer.valueOf( max_progress ) ) );
532
603
        }
533
604
 
534
605
        protected void setTmpProgress( int tmp_progress )
537
608
                checkAbort();
538
609
                _doit._handler.sendMessage( Message.obtain(
539
610
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
540
 
                        new Integer( tmp_progress ) ) );
 
611
                        Integer.valueOf( tmp_progress ) ) );
541
612
        }
542
613
 
543
614
        protected void setProgress( int progress ) throws AbortImportException
545
616
                checkAbort();
546
617
                _doit._handler.sendMessage( Message.obtain(
547
618
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
548
 
                        new Integer( progress ) ) );
 
619
                        Integer.valueOf( progress ) ) );
549
620
        }
550
621
 
551
622
        protected void finish( int action ) throws AbortImportException
569
640
                return _doit.getText( res );
570
641
        }
571
642
 
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
 
643
        /**
 
644
         * Should we skip a contact, given whether it exists or not and the current
 
645
         * merge setting?  This routine handles throwing up a prompt, if required.
 
646
         *
 
647
         * @param contact_detail the display name of the contact
 
648
         * @param exists true if this contact matches one in the cache
 
649
         * @param merge_setting the merge setting to use
 
650
         * @return true if the contact should be skipped outright
 
651
         * @throws AbortImportException
 
652
         */
 
653
        synchronized private boolean shouldWeSkipContact( String contact_detail,
 
654
                boolean exists, int merge_setting ) throws AbortImportException
582
655
        {
583
656
                _last_merge_decision = merge_setting;
584
657
 
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
 
 
592
658
                // handle special cases
593
659
                switch( merge_setting )
594
660
                {
595
661
                case Doit.ACTION_KEEP:
596
 
                        // if we keep contacts on duplicate, we better check for one
597
 
                        return !_contacts_cache.canLookup( identifier );
 
662
                        // if we are skipping on a duplicate, check for one
 
663
                        return exists;
598
664
 
599
665
                case Doit.ACTION_PROMPT:
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;
 
666
                        // if we are prompting on duplicate, then we can say that we won't
 
667
                        // skip if there isn't one
 
668
                        if( !exists ) return false;
604
669
 
605
 
                        // ok, it exists, so do prompt
 
670
                        // ok, duplicate exists, so do prompt
606
671
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
607
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
672
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
608
673
                        try {
609
674
                                wait();
610
675
                        }
618
683
                                _merge_setting = _response;
619
684
 
620
685
                        // recurse, with our new merge setting
621
 
                        return isImportRequired( contact, _response );
 
686
                        return shouldWeSkipContact( contact_detail, exists, _response );
622
687
                }
623
688
 
624
 
                // for all other cases (either overwriting or merging) we will need the
625
 
                // imported data
626
 
                return true;
 
689
                // for all other cases (either overwriting or merging) we don't skip
 
690
                return false;
627
691
        }
628
692
 
629
693
        protected void skipContact() throws AbortImportException
630
694
        {
631
695
                checkAbort();
 
696
 
 
697
                // show that we're skipping a new contact
632
698
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
633
699
        }
634
700
 
637
703
        {
638
704
                checkAbort();
639
705
 
 
706
                // It is expected that we use contact.getCacheIdentifier() here.  The
 
707
                // contact we are passed should have been successfully finalise()d,
 
708
                // which includes generating a valid cache identifier.
 
709
                ContactsCache.CacheIdentifier cache_identifier =
 
710
                        contact.getCacheIdentifier();
 
711
 
640
712
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
713
//                      finish( ACTION_ABORT );
642
714
 
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 )
659
 
                {
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 )
 
715
                // attempt to lookup the id of an existing contact in the cache with
 
716
                // this contact data's cache identifier
 
717
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
 
718
 
 
719
                // check to see if this contact should be skipped
 
720
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
 
721
                        _merge_setting ) )
 
722
                {
 
723
                        // show that we're skipping a contact
 
724
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
725
                        return;
 
726
                }
 
727
 
 
728
                // if a contact exists, and we're overwriting, destroy the existing
 
729
                // contact before importing
 
730
                boolean contact_deleted = false;
 
731
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
732
                {
 
733
                        contact_deleted = true;
 
734
 
 
735
                        // remove from device
 
736
                        _backend.deleteContact( id );
 
737
 
 
738
                        // update cache
 
739
                        _contacts_cache.removeLookup( cache_identifier );
 
740
                        _contacts_cache.removeAssociatedData( id );
 
741
 
 
742
                        // show that we're overwriting a contact
 
743
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
744
 
 
745
                        // discard the contact id
 
746
                        id = null;
 
747
                }
 
748
 
 
749
                try {
 
750
                        // if we don't have a contact id yet (or we did, but we destroyed it
 
751
                        // when we deleted the contact), we'll have to create a new contact
 
752
                        if( id == null )
669
753
                        {
670
 
                                // remove from device
671
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
754
                                // create a new contact
 
755
                                id = _backend.addContact( contact._name );
672
756
 
673
757
                                // 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;
 
758
                                _contacts_cache.addLookup( cache_identifier, id );
 
759
 
 
760
                                // if we haven't already shown that we're overwriting a contact,
 
761
                                // show that we're creating a new contact
 
762
                                if( !contact_deleted )
 
763
                                        _doit._handler.sendEmptyMessage(
 
764
                                                Doit.MESSAGE_CONTACTCREATED );
684
765
                        }
 
766
                        else
 
767
                                // show that we're merging with an existing contact
 
768
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
769
 
 
770
                        // import contact parts
 
771
                        if( contact.hasNumbers() )
 
772
                                importContactPhones( id, contact.getNumbers() );
 
773
                        if( contact.hasEmails() )
 
774
                                importContactEmails( id, contact.getEmails() );
 
775
                        if( contact.hasAddresses() )
 
776
                                importContactAddresses( id, contact.getAddresses() );
 
777
                        if( contact.hasOrganisations() )
 
778
                                importContactOrganisations( id, contact.getOrganisations() );
 
779
                        if( contact.hasNotes() )
 
780
                                importContactNotes( id, contact.getNotes() );
 
781
                        if( contact.hasBirthday() )
 
782
                                importContactBirthday( id, contact.getBirthday() );
685
783
                }
686
 
 
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 )
 
784
                catch( Backend.ContactCreationException e )
690
785
                {
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 );
698
 
 
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
 
                        }
707
 
 
708
 
                        // update cache
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;
717
 
                        }
 
786
                        showError( R.string.error_unabletoaddcontact );
718
787
                }
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() );
734
788
        }
735
789
 
736
790
        private void importContactPhones( Long id,
737
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
791
                HashMap< String, ContactData.PreferredDetail > datas )
 
792
                throws ContactCreationException
738
793
        {
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
 
 
745
794
                // add phone numbers
 
795
                Set< String > datas_keys = datas.keySet();
746
796
                Iterator< String > i = datas_keys.iterator();
747
797
                while( i.hasNext() ) {
748
798
                        String number = i.next();
749
799
                        ContactData.PreferredDetail data = datas.get( number );
750
800
 
751
 
                        // we don't want to add this number if it's crap, or it already
752
 
                        // exists (which would cause a duplicate to be created). We don't
753
 
                        // take in to account the type when checking for duplicates. This is
754
 
                        // intentional: types aren't really very reliable. We assume that
755
 
                        // if the number exists at all, it doesn't need importing. Because
756
 
                        // of this, we also can't update the cache (which we don't need to
757
 
                        // anyway, so it's not a problem).
 
801
                        // We don't want to add this number if it's crap, or it already
 
802
                        // exists (which would cause a duplicate to be created).  We don't
 
803
                        // take in to account the type when checking for duplicates.  This
 
804
                        // is intentional: types aren't really very reliable.  We assume
 
805
                        // that if the number exists at all, it doesn't need importing.
 
806
                        // Because of this, we also can't update the cache (which we don't
 
807
                        // need to anyway, so it's not a problem).
758
808
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
759
809
                                continue;
760
810
 
761
811
                        // add phone number
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 );
 
812
                        _backend.addContactPhone( id, number, data );
768
813
 
769
814
                        // and add this address to the cache to prevent a addition of
770
815
                        // duplicate date from another file
773
818
        }
774
819
 
775
820
        private void importContactEmails( Long id,
776
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
821
                HashMap< String, ContactData.PreferredDetail > datas )
 
822
                throws ContactCreationException
777
823
        {
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
 
 
784
824
                // add email addresses
 
825
                Set< String > datas_keys = datas.keySet();
785
826
                Iterator< String > i = datas_keys.iterator();
786
827
                while( i.hasNext() ) {
787
828
                        String email = i.next();
788
829
                        ContactData.PreferredDetail data = datas.get( email );
789
830
 
790
831
                        // we don't want to add this email address if it exists already or
791
 
                        // we would introduce duplicates.
 
832
                        // we would introduce duplicates
792
833
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
793
834
                                continue;
794
835
 
795
836
                        // add phone number
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 );
 
837
                        _backend.addContactEmail( id, email, data );
804
838
 
805
839
                        // and add this address to the cache to prevent a addition of
806
840
                        // duplicate date from another file
810
844
 
811
845
        private void importContactAddresses( Long id,
812
846
                HashMap< String, ContactData.TypeDetail > datas )
 
847
                throws ContactCreationException
813
848
        {
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
 
 
819
849
                // add addresses
820
850
                Set< String > datas_keys = datas.keySet();
821
851
                Iterator< String > i = datas_keys.iterator();
829
859
                                continue;
830
860
 
831
861
                        // add postal address
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 );
 
862
                        _backend.addContactAddresses( id, address, data );
838
863
 
839
864
                        // and add this address to the cache to prevent a addition of
840
865
                        // duplicate date from another file
844
869
 
845
870
        private void importContactOrganisations( Long id,
846
871
                HashMap< String, ContactData.ExtraDetail > datas )
 
872
                throws ContactCreationException
847
873
        {
848
874
                // add addresses
849
875
                Set< String > datas_keys = datas.keySet();
858
884
                                continue;
859
885
 
860
886
                        // add organisation address
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 );
 
887
                        _backend.addContactOrganisation( id, organisation, data );
870
888
 
871
889
                        // and add this address to the cache to prevent a addition of
872
890
                        // duplicate date from another file
874
892
                }
875
893
        }
876
894
 
 
895
        private void importContactNotes( Long id, HashSet< String > datas )
 
896
                throws ContactCreationException
 
897
        {
 
898
                // add notes
 
899
                Iterator< String > i = datas.iterator();
 
900
                while( i.hasNext() ) {
 
901
                        String note = i.next();
 
902
 
 
903
                        // we don't want to add this note if it exists already or we would
 
904
                        // introduce duplicates
 
905
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
 
906
                                continue;
 
907
 
 
908
                        // add note
 
909
                        _backend.addContactNote( id, note );
 
910
 
 
911
                        // and add this note to the cache to prevent a addition of duplicate
 
912
                        // date from another file
 
913
                        _contacts_cache.addAssociatedNote( id, note );
 
914
                }
 
915
        }
 
916
 
 
917
        private void importContactBirthday( Long id, String birthday )
 
918
                throws ContactCreationException
 
919
        {
 
920
                // we don't want to import this birthday if it already exists
 
921
                if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
 
922
                        return;
 
923
 
 
924
                // add birthday
 
925
                _backend.addContactBirthday( id, birthday );
 
926
 
 
927
                // and update the cache
 
928
                _contacts_cache.addAssociatedBirthday( id, birthday );
 
929
        }
 
930
 
877
931
        synchronized protected void checkAbort() throws AbortImportException
878
932
        {
879
933
                if( _abort ) {