/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/Importer.java

  • Committer: edam
  • Date: 2012-12-19 17:51:35 UTC
  • Revision ID: tim@ed.am-20121219175135-1cpuafp76jg1ib1p
added preliminary (buggy) ContactsContract backend

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
 
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
{
55
54
        private int _last_merge_decision;
56
55
        private boolean _abort = false;
57
56
        private boolean _is_finished = false;
58
 
        private ContactsCache _contactsCache = null;
59
 
 
60
 
        @SuppressWarnings("serial")
61
 
        protected class ContactNeedsMoreInfoException extends Exception
62
 
        {
63
 
        }
 
57
        private ContactsCache _contacts_cache = null;
 
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;
136
144
 
 
145
                private ContactsCache.CacheIdentifier _cache_identifier = null;
 
146
 
137
147
                protected void setName( String name )
138
148
                {
139
149
                        _name = name;
173
183
                                        new ExtraDetail( 0, false, title ) );
174
184
 
175
185
                        // 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".
 
186
                        // organisation and the current primary organisation isn't, then
 
187
                        // record this as the primary organisation.
178
188
                        if( _primary_organisation == null ||
179
189
                                ( is_preferred && !_primary_organisation_is_preferred ) )
180
190
                        {
221
231
                                _numbers.put( number,
222
232
                                        new PreferredDetail( type, false ) );
223
233
 
 
234
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
 
235
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
236
 
224
237
                        // 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".
 
238
                        // and the current primary number isn't, or this number is on equal
 
239
                        // standing with the primary number in terms of preference and it is
 
240
                        // a voice number and the primary number isn't, then record this as
 
241
                        // the primary number.
227
242
                        if( _primary_number == null ||
228
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
243
                                ( is_preferred && !_primary_number_is_preferred ) ||
 
244
                                ( is_preferred == _primary_number_is_preferred &&
 
245
                                        !non_voice_types.contains( type ) &&
 
246
                                        non_voice_types.contains( _primary_number_type ) ) )
229
247
                        {
230
248
                                _primary_number = number;
 
249
                                _primary_number_type = type;
231
250
                                _primary_number_is_preferred = is_preferred;
232
251
                        }
233
252
                }
258
277
                        email = sanitisesEmailAddress( email );
259
278
                        if( email == null )
260
279
                        {
261
 
                                // TODO: warn that an imported email addtrss is being ignored
 
280
                                // TODO: warn that an imported email address is being ignored
262
281
                                return;
263
282
                        }
264
283
 
269
288
                        if( !_emails.containsKey( email ) )
270
289
                                _emails.put( email, new PreferredDetail( type, false ) );
271
290
 
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".
 
291
                        // if this is the first email added, or it's a preferred email and
 
292
                        // the current primary organisation isn't, then record this as the
 
293
                        // primary email.
275
294
                        if( _primary_email == null ||
276
295
                                ( is_preferred && !_primary_email_is_preferred ) )
277
296
                        {
326
345
                }
327
346
 
328
347
                protected void finalise()
 
348
                        throws ContactNotIdentifiableException
329
349
                {
330
350
                        // ensure that if there is a primary number, it is preferred so
331
351
                        // that there is always one preferred number. Android will assign
349
369
                                _organisations.put( _primary_organisation,
350
370
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
371
                        }
 
372
 
 
373
                        // create a cache identifier from this contact data, which can be
 
374
                        // used to look-up an existing contact
 
375
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
376
                        if( _cache_identifier == null )
 
377
                                throw new ContactNotIdentifiableException();
 
378
                }
 
379
 
 
380
                public ContactsCache.CacheIdentifier getCacheIdentifier()
 
381
                {
 
382
                        return _cache_identifier;
352
383
                }
353
384
 
354
385
                private String sanitisePhoneNumber( String number )
368
399
                        Matcher m = p.matcher( email );
369
400
                        if( m.matches() ) {
370
401
                                String[] bits = email.split( "@" );
371
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
402
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
372
403
                        }
373
404
                        return null;
374
405
                }
393
424
                        // update UI
394
425
                        setProgressMessage( R.string.doit_caching );
395
426
 
396
 
                        // build a cache of existing contacts
397
 
                        _contactsCache = new ContactsCache();
398
 
                        _contactsCache.buildCache( _doit );
 
427
                        // create the appropriate backend
 
428
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
429
                                _backend = new ContactsContractBackend( _doit );
 
430
                        else
 
431
                                _backend = new ContactsBackend( _doit );
 
432
 
 
433
                        // create a cache of existing contacts and populate it
 
434
                        _contacts_cache = new ContactsCache();
 
435
                        _backend.populateCache( _contacts_cache );
399
436
 
400
437
                        // do the import
401
438
                        onImport();
522
559
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
523
560
        }
524
561
 
525
 
        protected void setProgressMax( int maxProgress )
 
562
        protected void setProgressMax( int max_progress )
526
563
                        throws AbortImportException
527
564
        {
528
565
                checkAbort();
529
566
                _doit._handler.sendMessage( Message.obtain(
530
567
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
 
                        new Integer( maxProgress ) ) );
 
568
                        Integer.valueOf( max_progress ) ) );
532
569
        }
533
570
 
534
 
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
 
571
        protected void setTmpProgress( int tmp_progress )
 
572
                throws AbortImportException
535
573
        {
536
574
                checkAbort();
537
575
                _doit._handler.sendMessage( Message.obtain(
538
576
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
539
 
                        new Integer( tmpProgress ) ) );
 
577
                        Integer.valueOf( tmp_progress ) ) );
540
578
        }
541
579
 
542
580
        protected void setProgress( int progress ) throws AbortImportException
544
582
                checkAbort();
545
583
                _doit._handler.sendMessage( Message.obtain(
546
584
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
547
 
                        new Integer( progress ) ) );
 
585
                        Integer.valueOf( progress ) ) );
548
586
        }
549
587
 
550
588
        protected void finish( int action ) throws AbortImportException
568
606
                return _doit.getText( res );
569
607
        }
570
608
 
571
 
        protected boolean isImportRequired( ContactData contact )
572
 
                        throws AbortImportException, ContactNeedsMoreInfoException
573
 
        {
574
 
                checkAbort();
575
 
                return isImportRequired( contact, _merge_setting );
576
 
        }
577
 
 
578
 
        synchronized private boolean isImportRequired(
579
 
                ContactData contact, int merge_setting )
580
 
                throws AbortImportException, ContactNeedsMoreInfoException
 
609
        /**
 
610
         * Should we skip a contact, given whether it exists or not and the current
 
611
         * merge setting?  This routine handles throwing up a prompt, if required.
 
612
         * @param contact_detail the display name of the contact
 
613
         * @param exists true if this contact matches one in the cache
 
614
         * @param merge_setting the merge setting to use
 
615
         * @return true if the contact should be skipped outright
 
616
         * @throws AbortImportException
 
617
         */
 
618
        synchronized private boolean shouldWeSkipContact( String contact_detail,
 
619
                boolean exists, int merge_setting ) throws AbortImportException
581
620
        {
582
621
                _last_merge_decision = merge_setting;
583
622
 
584
 
                // create a cache identifier which we can use to detect if this contact
585
 
                // is valid for importing
586
 
                ContactsCache.CacheIdentifier identifier =
587
 
                        ContactsCache.createIdentifier( contact );
588
 
                if( identifier == null )
589
 
                        throw new ContactNeedsMoreInfoException();
590
 
 
591
623
                // handle special cases
592
624
                switch( merge_setting )
593
625
                {
594
626
                case Doit.ACTION_KEEP:
595
 
                        // if we keep contacts on duplicate, we better check for one
596
 
                        return !_contactsCache.canLookup( identifier );
 
627
                        // if we are skipping on a duplicate, check for one
 
628
                        return exists;
597
629
 
598
630
                case Doit.ACTION_PROMPT:
599
 
                        // if we are prompting on duplicate, we better check for one and if
600
 
                        // the contact doesn'te exist, we want to import it
601
 
                        if( !_contactsCache.canLookup( identifier ) )
602
 
                                return true;
 
631
                        // if we are prompting on duplicate, then we can say that we won't
 
632
                        // skip if there isn't one
 
633
                        if( !exists ) return false;
603
634
 
604
 
                        // ok, it exists, so do prompt
 
635
                        // ok, duplicate exists, so do prompt
605
636
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
606
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
637
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
607
638
                        try {
608
639
                                wait();
609
640
                        }
617
648
                                _merge_setting = _response;
618
649
 
619
650
                        // recurse, with our new merge setting
620
 
                        return isImportRequired( contact, _response );
 
651
                        return shouldWeSkipContact( contact_detail, exists, _response );
621
652
                }
622
653
 
623
 
                // for all other cases (either overwriting or merging) we will need the
624
 
                // imported data
625
 
                return true;
 
654
                // for all other cases (either overwriting or merging) we don't skip
 
655
                return false;
626
656
        }
627
657
 
628
658
        protected void skipContact() throws AbortImportException
629
659
        {
630
660
                checkAbort();
 
661
 
 
662
                // show that we're skipping a new contact
631
663
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
632
664
        }
633
665
 
636
668
        {
637
669
                checkAbort();
638
670
 
 
671
                // It is expected that we use contact.getCacheIdentifier() here. The
 
672
                // contact we are passed should have been successfully finalise()d,
 
673
                // which includes generating a valid cache identifier.
 
674
                ContactsCache.CacheIdentifier cache_identifier =
 
675
                        contact.getCacheIdentifier();
 
676
 
639
677
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
640
678
//                      finish( ACTION_ABORT );
641
679
 
642
 
                ContentValues values = new ContentValues();
643
 
                boolean uiInformed = false;
644
 
                Long id = null;
645
 
 
646
 
                // give the contact a chance to finalise it's data
647
 
                contact.finalise();
648
 
 
649
 
                // create something, from the contact data, that we can use to identify
650
 
                // a cache entry and attempt to lookup the id of an existing contact in
651
 
                // the cache with it
652
 
                ContactsCache.CacheIdentifier identifier =
653
 
                        ContactsCache.createIdentifier( contact );
654
 
                if( identifier != null ) id = (Long)_contactsCache.lookup( identifier );
655
 
 
656
 
                // does contact exist already?
657
 
                if( id != null )
658
 
                {
659
 
                        // should we skip this import altogether?
660
 
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
661
 
 
662
 
                        // get contact's URI
663
 
                        Uri contactUri = ContentUris.withAppendedId(
664
 
                                Contacts.People.CONTENT_URI, id );
665
 
 
666
 
                        // should we destroy the existing contact before importing?
667
 
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
680
                // attempt to lookup the id of an existing contact in the cache with
 
681
                // this contact data's cache identifier
 
682
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
 
683
 
 
684
                // check to see if this contact should be skipped
 
685
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
 
686
                        _merge_setting ) )
 
687
                {
 
688
                        // show that we're skipping a contact
 
689
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
690
                        return;
 
691
                }
 
692
 
 
693
                // if a contact exists, and we're overwriting, destroy the existing
 
694
                // contact before importing
 
695
                boolean contact_deleted = false;
 
696
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
697
                {
 
698
                        contact_deleted = true;
 
699
 
 
700
                        // remove from device
 
701
                        _backend.deleteContact( id );
 
702
 
 
703
                        // update cache
 
704
                        _contacts_cache.removeLookup( cache_identifier );
 
705
                        _contacts_cache.removeAssociatedData( id );
 
706
 
 
707
                        // show that we're overwriting a contact
 
708
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
709
 
 
710
                        // discard the contact id
 
711
                        id = null;
 
712
                }
 
713
 
 
714
                try {
 
715
                        // if we don't have a contact id yet (or we did, but we destroyed it
 
716
                        // when we deleted the contact), we'll have to create a new contact
 
717
                        if( id == null )
668
718
                        {
669
 
                                // remove from device
670
 
                                _doit.getContentResolver().delete( contactUri, null, null );
 
719
                                // create a new contact
 
720
                                id = _backend.addContact( contact._name );
671
721
 
672
722
                                // update cache
673
 
                                _contactsCache.removeLookup( identifier );
674
 
                                _contactsCache.removeAssociatedData( id );
675
 
 
676
 
                                // show that we're overwriting a contact
677
 
                                _doit._handler.sendEmptyMessage(
678
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
679
 
                                uiInformed = true;
680
 
 
681
 
                                // discard the contact id
682
 
                                id = null;
 
723
                                _contacts_cache.addLookup( cache_identifier, id );
 
724
 
 
725
                                // if we haven't already shown that we're overwriting a contact,
 
726
                                // show that we're creating a new contact
 
727
                                if( !contact_deleted )
 
728
                                        _doit._handler.sendEmptyMessage(
 
729
                                                Doit.MESSAGE_CONTACTCREATED );
683
730
                        }
 
731
                        else
 
732
                                // show that we're merging with an existing contact
 
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() );
684
744
                }
685
 
 
686
 
                // if we don't have a contact id yet (or if we did, but we destroyed it
687
 
                // when we deleted the contact), we'll have to create a new contact
688
 
                if( id == null )
 
745
                catch( Backend.ContactCreationException e )
689
746
                {
690
 
                        // create a new contact
691
 
                        values.put( Contacts.People.NAME, contact._name );
692
 
                        Uri contactUri = _doit.getContentResolver().insert(
693
 
                                Contacts.People.CONTENT_URI, values );
694
 
                        id = ContentUris.parseId( contactUri );
695
 
                        if( id == null || id <= 0 )
696
 
                                showError( R.string.error_unabletoaddcontact );
697
 
 
698
 
                        // try to add them to the "My Contacts" group
699
 
                        try {
700
 
                                Contacts.People.addToMyContactsGroup(
701
 
                                        _doit.getContentResolver(), id );
702
 
                        }
703
 
                        catch( IllegalStateException e ) {
704
 
                                // ignore any failure
705
 
                        }
706
 
 
707
 
                        // update cache
708
 
                        _contactsCache.addLookup(
709
 
                                ContactsCache.createIdentifier( contact ), id );
710
 
 
711
 
                        // if we haven't already shown that we're overwriting a contact,
712
 
                        // show that we're creating a new contact
713
 
                        if( !uiInformed ) {
714
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
715
 
                                uiInformed = true;
716
 
                        }
 
747
                        showError( R.string.error_unabletoaddcontact );
717
748
                }
718
 
 
719
 
                // if we haven't already shown that we're overwriting or creating a
720
 
                // contact show that we're merging a contact
721
 
                if( !uiInformed )
722
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
723
 
 
724
 
                // import contact parts
725
 
                if( contact.hasNumbers() )
726
 
                        importContactPhones( id, contact.getNumbers() );
727
 
                if( contact.hasEmails() )
728
 
                        importContactEmails( id, contact.getEmails() );
729
 
                if( contact.hasAddresses() )
730
 
                        importContactAddresses( id, contact.getAddresses() );
731
 
                if( contact.hasOrganisations() )
732
 
                        importContactOrganisations( id, contact.getOrganisations() );
733
749
        }
734
750
 
735
751
        private void importContactPhones( Long id,
736
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
752
                HashMap< String, ContactData.PreferredDetail > datas )
 
753
                throws ContactCreationException
737
754
        {
738
 
                // get URI to contact's phones
739
 
                Uri contactPhonesUri = Uri.withAppendedPath(
740
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
741
 
                        Contacts.People.Phones.CONTENT_DIRECTORY );
742
 
                Set< String > datasKeys = datas.keySet();
743
 
 
744
755
                // add phone numbers
745
 
                Iterator< String > i = datasKeys.iterator();
 
756
                Set< String > datas_keys = datas.keySet();
 
757
                Iterator< String > i = datas_keys.iterator();
746
758
                while( i.hasNext() ) {
747
759
                        String number = i.next();
748
760
                        ContactData.PreferredDetail data = datas.get( number );
754
766
                        // if the number exists at all, it doesn't need importing. Because
755
767
                        // of this, we also can't update the cache (which we don't need to
756
768
                        // anyway, so it's not a problem).
757
 
                        if( _contactsCache.hasAssociatedNumber( id, number ) )
 
769
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
758
770
                                continue;
759
771
 
760
772
                        // add phone number
761
 
                        ContentValues values = new ContentValues();
762
 
                        values.put( Contacts.Phones.TYPE, data.getType() );
763
 
                        values.put( Contacts.Phones.NUMBER, number );
764
 
                        if( data.isPreferred() )
765
 
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
766
 
                        _doit.getContentResolver().insert( contactPhonesUri, values );
 
773
                        _backend.addContactPhone( id, number, data );
767
774
 
768
775
                        // and add this address to the cache to prevent a addition of
769
776
                        // duplicate date from another file
770
 
                        _contactsCache.addAssociatedNumber( id, number );
 
777
                        _contacts_cache.addAssociatedNumber( id, number );
771
778
                }
772
779
        }
773
780
 
774
781
        private void importContactEmails( Long id,
775
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
782
                HashMap< String, ContactData.PreferredDetail > datas )
 
783
                throws ContactCreationException
776
784
        {
777
 
                // get URI to contact's contact methods
778
 
                Uri contactContactMethodsUri = Uri.withAppendedPath(
779
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
780
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
781
 
                Set< String > datasKeys = datas.keySet();
782
 
 
783
785
                // add email addresses
784
 
                Iterator< String > i = datasKeys.iterator();
 
786
                Set< String > datas_keys = datas.keySet();
 
787
                Iterator< String > i = datas_keys.iterator();
785
788
                while( i.hasNext() ) {
786
789
                        String email = i.next();
787
790
                        ContactData.PreferredDetail data = datas.get( email );
788
791
 
789
792
                        // we don't want to add this email address if it exists already or
790
793
                        // we would introduce duplicates.
791
 
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
 
794
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
792
795
                                continue;
793
796
 
794
797
                        // add phone number
795
 
                        ContentValues values = new ContentValues();
796
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
797
 
                        values.put( Contacts.ContactMethods.DATA, email );
798
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
799
 
                        if( data.isPreferred() )
800
 
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
801
 
                        _doit.getContentResolver().insert( contactContactMethodsUri,
802
 
                                values );
 
798
                        _backend.addContactEmail( id, email, data );
803
799
 
804
800
                        // and add this address to the cache to prevent a addition of
805
801
                        // duplicate date from another file
806
 
                        _contactsCache.addAssociatedEmail( id, email );
 
802
                        _contacts_cache.addAssociatedEmail( id, email );
807
803
                }
808
804
        }
809
805
 
810
806
        private void importContactAddresses( Long id,
811
807
                HashMap< String, ContactData.TypeDetail > datas )
 
808
                throws ContactCreationException
812
809
        {
813
 
                // get URI to contact's contact methods
814
 
                Uri contactContactMethodsUri = Uri.withAppendedPath(
815
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
816
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
817
 
 
818
810
                // add addresses
819
 
                Set< String > datasKeys = datas.keySet();
820
 
                Iterator< String > i = datasKeys.iterator();
 
811
                Set< String > datas_keys = datas.keySet();
 
812
                Iterator< String > i = datas_keys.iterator();
821
813
                while( i.hasNext() ) {
822
814
                        String address = i.next();
823
815
                        ContactData.TypeDetail data = datas.get( address );
824
816
 
825
817
                        // we don't want to add this address if it exists already or we
826
818
                        // would introduce duplicates
827
 
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
 
819
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
828
820
                                continue;
829
821
 
830
822
                        // add postal address
831
 
                        ContentValues values = new ContentValues();
832
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
833
 
                        values.put( Contacts.ContactMethods.DATA, address );
834
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
835
 
                        _doit.getContentResolver().insert( contactContactMethodsUri,
836
 
                                values );
 
823
                        _backend.addContactAddresses( id, address, data );
837
824
 
838
825
                        // and add this address to the cache to prevent a addition of
839
826
                        // duplicate date from another file
840
 
                        _contactsCache.addAssociatedAddress( id, address );
 
827
                        _contacts_cache.addAssociatedAddress( id, address );
841
828
                }
842
829
        }
843
830
 
844
831
        private void importContactOrganisations( Long id,
845
832
                HashMap< String, ContactData.ExtraDetail > datas )
 
833
                throws ContactCreationException
846
834
        {
847
835
                // add addresses
848
 
                Set< String > datasKeys = datas.keySet();
849
 
                Iterator< String > i = datasKeys.iterator();
 
836
                Set< String > datas_keys = datas.keySet();
 
837
                Iterator< String > i = datas_keys.iterator();
850
838
                while( i.hasNext() ) {
851
839
                        String organisation = i.next();
852
840
                        ContactData.ExtraDetail data = datas.get( organisation );
853
841
 
854
842
                        // we don't want to add this address if it exists already or we
855
843
                        // would introduce duplicates
856
 
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
 
844
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
857
845
                                continue;
858
846
 
859
847
                        // add organisation address
860
 
                        ContentValues values = new ContentValues();
861
 
                        values.put( Contacts.Organizations.PERSON_ID, id );
862
 
                        values.put( Contacts.Organizations.COMPANY, organisation );
863
 
                        values.put( Contacts.ContactMethods.TYPE,
864
 
                                Contacts.OrganizationColumns.TYPE_WORK );
865
 
                        if( data.getExtra() != null )
866
 
                                values.put( Contacts.Organizations.TITLE, data.getExtra() );
867
 
                        _doit.getContentResolver().insert(
868
 
                                Contacts.Organizations.CONTENT_URI, values );
 
848
                        _backend.addContactOrganisation( id, organisation, data );
869
849
 
870
850
                        // and add this address to the cache to prevent a addition of
871
851
                        // duplicate date from another file
872
 
                        _contactsCache.addAssociatedOrganisation( id, organisation );
 
852
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
873
853
                }
874
854
        }
875
855