/android/import-contacts

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

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/importcontacts/Importer.java

  • Committer: edam
  • Date: 2011-05-02 18:28:24 UTC
  • Revision ID: edam@waxworlds.org-20110502182824-acgdi3qfxfzqgely
- fixed logic for vcard field types (home, work, cell, etc) so it works
- updated NEWS and TODO
- rewrote most of ContactsCache, including a new ContactIdentifier class to identify contacts in the cache and new cache building code
- contacts now identified in the same way that Andoid displays them (by name, or organisation, or number, or email, in that order)
- propper handling and support for organisations and titles
- validation of imported contact now done by Importer, not VcfImporter
- separated sanitisation and normalisation (for cache lookups)
- generacised PhoneData, EmailData and AddressData classes
- ContactData is now aware of primary numbers, emails and organisations (defaults to the first prefrred one seen, or the first one seen where none is preferred)

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
 
import java.util.Arrays;
27
26
import java.util.HashMap;
28
 
import java.util.HashSet;
29
27
import java.util.Iterator;
30
28
import java.util.Set;
31
29
import java.util.regex.Matcher;
32
30
import java.util.regex.Pattern;
33
31
 
 
32
import android.content.ContentUris;
 
33
import android.content.ContentValues;
34
34
import android.content.SharedPreferences;
 
35
import android.net.Uri;
35
36
import android.os.Message;
36
 
import android.provider.Contacts.PhonesColumns;
 
37
import android.provider.Contacts;
 
38
 
37
39
 
38
40
public class Importer extends Thread
39
41
{
53
55
        private int _last_merge_decision;
54
56
        private boolean _abort = false;
55
57
        private boolean _is_finished = false;
56
 
        private ContactsCache _contacts_cache = null;
57
 
        private Backend _backend = null;
 
58
        private ContactsCache _contactsCache = null;
 
59
 
 
60
        @SuppressWarnings("serial")
 
61
        protected class ContactNeedsMoreInfoException extends Exception
 
62
        {
 
63
        }
58
64
 
59
65
        /**
60
66
         * Data about a contact
116
122
                        }
117
123
                }
118
124
 
119
 
                @SuppressWarnings("serial")
120
 
                protected class ContactNotIdentifiableException extends Exception
121
 
                {
122
 
                }
123
 
 
124
125
                protected String _name = null;
125
126
                protected String _primary_organisation = null;
126
 
                protected boolean _primary_organisation_is_preferred;
 
127
                protected boolean _primary_organisation_is_preferred = false;
127
128
                protected String _primary_number = null;
128
 
                protected int _primary_number_type;
129
 
                protected boolean _primary_number_is_preferred;
 
129
                protected boolean _primary_number_is_preferred = false;
130
130
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred;
 
131
                protected boolean _primary_email_is_preferred = false;
132
132
                protected HashMap< String, ExtraDetail > _organisations = null;
133
133
                protected HashMap< String, PreferredDetail > _numbers = null;
134
134
                protected HashMap< String, PreferredDetail > _emails = null;
135
135
                protected HashMap< String, TypeDetail > _addresses = null;
136
136
 
137
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
138
 
 
139
137
                protected void setName( String name )
140
138
                {
141
139
                        _name = name;
175
173
                                        new ExtraDetail( 0, false, title ) );
176
174
 
177
175
                        // if this is the first organisation added, or it's a preferred
178
 
                        // organisation and the current primary organisation isn't, then
179
 
                        // record this as the primary organisation.
 
176
                        // organisation and a previous organisation wasn't, then remember
 
177
                        // that this is the "primary organisation".
180
178
                        if( _primary_organisation == null ||
181
179
                                ( is_preferred && !_primary_organisation_is_preferred ) )
182
180
                        {
223
221
                                _numbers.put( number,
224
222
                                        new PreferredDetail( type, false ) );
225
223
 
226
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
227
 
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
228
 
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
229
 
 
230
224
                        // if this is the first number added, or it's a preferred number
231
 
                        // and the current primary number isn't, or this number is on equal
232
 
                        // standing with the primary number in terms of preference and it is
233
 
                        // a voice number and the primary number isn't, then record this as
234
 
                        // the primary number.
 
225
                        // and a previous number wasn't, then remember that this is the
 
226
                        // "primary number".
235
227
                        if( _primary_number == null ||
236
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
237
 
                                ( is_preferred == _primary_number_is_preferred &&
238
 
                                        !non_voice_types.contains( type ) &&
239
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
228
                                ( is_preferred && !_primary_number_is_preferred ) )
240
229
                        {
241
230
                                _primary_number = number;
242
 
                                _primary_number_type = type;
243
231
                                _primary_number_is_preferred = is_preferred;
244
232
                        }
245
233
                }
270
258
                        email = sanitisesEmailAddress( email );
271
259
                        if( email == null )
272
260
                        {
273
 
                                // TODO: warn that an imported email address is being ignored
 
261
                                // TODO: warn that an imported email addtrss is being ignored
274
262
                                return;
275
263
                        }
276
264
 
281
269
                        if( !_emails.containsKey( email ) )
282
270
                                _emails.put( email, new PreferredDetail( type, false ) );
283
271
 
284
 
                        // if this is the first email added, or it's a preferred email and
285
 
                        // the current primary organisation isn't, then record this as the
286
 
                        // primary email.
 
272
                        // if this is the first email added, or it's a preferred email
 
273
                        // and a previous email wasn't, then remember that this is the
 
274
                        // "primary email".
287
275
                        if( _primary_email == null ||
288
276
                                ( is_preferred && !_primary_email_is_preferred ) )
289
277
                        {
338
326
                }
339
327
 
340
328
                protected void finalise()
341
 
                        throws ContactNotIdentifiableException
342
329
                {
343
330
                        // ensure that if there is a primary number, it is preferred so
344
331
                        // that there is always one preferred number. Android will assign
362
349
                                _organisations.put( _primary_organisation,
363
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
364
351
                        }
365
 
 
366
 
                        // create a cache identifier from this contact data, which can be
367
 
                        // used to look-up an existing contact
368
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
369
 
                        if( _cache_identifier == null )
370
 
                                throw new ContactNotIdentifiableException();
371
 
                }
372
 
 
373
 
                public ContactsCache.CacheIdentifier getCacheIdentifier()
374
 
                {
375
 
                        return _cache_identifier;
376
352
                }
377
353
 
378
354
                private String sanitisePhoneNumber( String number )
417
393
                        // update UI
418
394
                        setProgressMessage( R.string.doit_caching );
419
395
 
420
 
                        // create the appropriate backend
421
 
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
422
 
//                              _backend = new ContactsContractBackend( _doit );
423
 
//                      else
424
 
                                _backend = new ContactsBackend( _doit );
425
 
 
426
 
                        // create a cache of existing contacts and populate it
427
 
                        _contacts_cache = new ContactsCache();
428
 
                        _backend.populateCache( _contacts_cache );
 
396
                        // build a cache of existing contacts
 
397
                        _contactsCache = new ContactsCache();
 
398
                        _contactsCache.buildCache( _doit );
429
399
 
430
400
                        // do the import
431
401
                        onImport();
552
522
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
553
523
        }
554
524
 
555
 
        protected void setProgressMax( int max_progress )
 
525
        protected void setProgressMax( int maxProgress )
556
526
                        throws AbortImportException
557
527
        {
558
528
                checkAbort();
559
529
                _doit._handler.sendMessage( Message.obtain(
560
530
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
561
 
                        new Integer( max_progress ) ) );
 
531
                        new Integer( maxProgress ) ) );
562
532
        }
563
533
 
564
 
        protected void setTmpProgress( int tmp_progress )
565
 
                throws AbortImportException
 
534
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
566
535
        {
567
536
                checkAbort();
568
537
                _doit._handler.sendMessage( Message.obtain(
569
538
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
570
 
                        new Integer( tmp_progress ) ) );
 
539
                        new Integer( tmpProgress ) ) );
571
540
        }
572
541
 
573
542
        protected void setProgress( int progress ) throws AbortImportException
599
568
                return _doit.getText( res );
600
569
        }
601
570
 
602
 
        synchronized private boolean checkForDuplicate(
603
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
 
                throws AbortImportException
 
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
605
581
        {
606
582
                _last_merge_decision = merge_setting;
607
583
 
608
 
                // it is ok to use contact.getCacheIdentifier(). The contact has already
609
 
                // been finalised, which means a valid cache identifier will have been
610
 
                // created for it (or it would have been skipped)
 
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();
611
590
 
612
591
                // handle special cases
613
592
                switch( merge_setting )
614
593
                {
615
594
                case Doit.ACTION_KEEP:
616
595
                        // if we keep contacts on duplicate, we better check for one
617
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
596
                        return !_contactsCache.canLookup( identifier );
618
597
 
619
598
                case Doit.ACTION_PROMPT:
620
599
                        // if we are prompting on duplicate, we better check for one and if
621
600
                        // the contact doesn'te exist, we want to import it
622
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
 
601
                        if( !_contactsCache.canLookup( identifier ) )
623
602
                                return true;
624
603
 
625
604
                        // ok, it exists, so do prompt
626
605
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
627
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
606
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
628
607
                        try {
629
608
                                wait();
630
609
                        }
638
617
                                _merge_setting = _response;
639
618
 
640
619
                        // recurse, with our new merge setting
641
 
                        return checkForDuplicate( cache_identifier, _response );
 
620
                        return isImportRequired( contact, _response );
642
621
                }
643
622
 
644
623
                // for all other cases (either overwriting or merging) we will need the
657
636
        {
658
637
                checkAbort();
659
638
 
660
 
                // It is expected that we use contact.getCacheIdentifier() here. The
661
 
                // contact we are passed should have been successfully finalise()d,
662
 
                // which includes generating a valid cache identifier.
663
 
                ContactsCache.CacheIdentifier cache_identifier =
664
 
                        contact.getCacheIdentifier();
665
 
 
666
 
                // check to see if this contact is a duplicate and should be skipped
667
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
668
 
                        skipContact();
669
 
                        return;
670
 
                }
671
 
 
672
639
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
640
//                      finish( ACTION_ABORT );
674
641
 
675
 
                // keep track of whether we've informed the UI of what we're doing
676
 
                boolean ui_informed = false;
677
 
 
678
 
                // attempt to lookup the id of an existing contact in the cache with
679
 
                // this contact data's cache identifier
680
 
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
 
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 );
681
655
 
682
656
                // does contact exist already?
683
657
                if( id != null )
685
659
                        // should we skip this import altogether?
686
660
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
661
 
 
662
                        // get contact's URI
 
663
                        Uri contactUri = ContentUris.withAppendedId(
 
664
                                Contacts.People.CONTENT_URI, id );
 
665
 
688
666
                        // should we destroy the existing contact before importing?
689
667
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
690
668
                        {
691
669
                                // remove from device
692
 
                                _backend.deleteContact( id );
 
670
                                _doit.getContentResolver().delete( contactUri, null, null );
693
671
 
694
672
                                // update cache
695
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
696
 
                                _contacts_cache.removeAssociatedData( id );
 
673
                                _contactsCache.removeLookup( identifier );
 
674
                                _contactsCache.removeAssociatedData( id );
697
675
 
698
676
                                // show that we're overwriting a contact
699
677
                                _doit._handler.sendEmptyMessage(
700
678
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
701
 
                                ui_informed = true;
 
679
                                uiInformed = true;
702
680
 
703
681
                                // discard the contact id
704
682
                                id = null;
705
683
                        }
706
684
                }
707
685
 
708
 
                // if we don't have a contact id yet (or we did, but we destroyed it
 
686
                // if we don't have a contact id yet (or if we did, but we destroyed it
709
687
                // when we deleted the contact), we'll have to create a new contact
710
688
                if( id == null )
711
689
                {
712
690
                        // create a new contact
713
 
                        id = _backend.addContact( contact._name );
714
 
                        if( id == null )
 
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 )
715
696
                                showError( R.string.error_unabletoaddcontact );
716
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
 
717
707
                        // update cache
718
 
                        _contacts_cache.addLookup(
 
708
                        _contactsCache.addLookup(
719
709
                                ContactsCache.createIdentifier( contact ), id );
720
710
 
721
711
                        // if we haven't already shown that we're overwriting a contact,
722
712
                        // show that we're creating a new contact
723
 
                        if( !ui_informed ) {
 
713
                        if( !uiInformed ) {
724
714
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
725
 
                                ui_informed = true;
 
715
                                uiInformed = true;
726
716
                        }
727
717
                }
728
718
 
729
719
                // if we haven't already shown that we're overwriting or creating a
730
 
                // contact, show that we're merging a contact
731
 
                if( !ui_informed )
 
720
                // contact show that we're merging a contact
 
721
                if( !uiInformed )
732
722
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
733
723
 
734
724
                // import contact parts
745
735
        private void importContactPhones( Long id,
746
736
                        HashMap< String, ContactData.PreferredDetail > datas )
747
737
        {
 
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
 
748
744
                // add phone numbers
749
 
                Set< String > datas_keys = datas.keySet();
750
 
                Iterator< String > i = datas_keys.iterator();
 
745
                Iterator< String > i = datasKeys.iterator();
751
746
                while( i.hasNext() ) {
752
747
                        String number = i.next();
753
748
                        ContactData.PreferredDetail data = datas.get( number );
759
754
                        // if the number exists at all, it doesn't need importing. Because
760
755
                        // of this, we also can't update the cache (which we don't need to
761
756
                        // anyway, so it's not a problem).
762
 
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
 
757
                        if( _contactsCache.hasAssociatedNumber( id, number ) )
763
758
                                continue;
764
759
 
765
760
                        // add phone number
766
 
                        _backend.addContactPhone( id, number, data );
 
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 );
767
767
 
768
768
                        // and add this address to the cache to prevent a addition of
769
769
                        // duplicate date from another file
770
 
                        _contacts_cache.addAssociatedNumber( id, number );
 
770
                        _contactsCache.addAssociatedNumber( id, number );
771
771
                }
772
772
        }
773
773
 
774
774
        private void importContactEmails( Long id,
775
775
                        HashMap< String, ContactData.PreferredDetail > datas )
776
776
        {
 
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
 
777
783
                // add email addresses
778
 
                Set< String > datas_keys = datas.keySet();
779
 
                Iterator< String > i = datas_keys.iterator();
 
784
                Iterator< String > i = datasKeys.iterator();
780
785
                while( i.hasNext() ) {
781
786
                        String email = i.next();
782
787
                        ContactData.PreferredDetail data = datas.get( email );
783
788
 
784
789
                        // we don't want to add this email address if it exists already or
785
790
                        // we would introduce duplicates.
786
 
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
 
791
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
787
792
                                continue;
788
793
 
789
794
                        // add phone number
790
 
                        _backend.addContactEmail( id, email, data );
 
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 );
791
803
 
792
804
                        // and add this address to the cache to prevent a addition of
793
805
                        // duplicate date from another file
794
 
                        _contacts_cache.addAssociatedEmail( id, email );
 
806
                        _contactsCache.addAssociatedEmail( id, email );
795
807
                }
796
808
        }
797
809
 
798
810
        private void importContactAddresses( Long id,
799
811
                HashMap< String, ContactData.TypeDetail > datas )
800
812
        {
 
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
 
801
818
                // add addresses
802
 
                Set< String > datas_keys = datas.keySet();
803
 
                Iterator< String > i = datas_keys.iterator();
 
819
                Set< String > datasKeys = datas.keySet();
 
820
                Iterator< String > i = datasKeys.iterator();
804
821
                while( i.hasNext() ) {
805
822
                        String address = i.next();
806
823
                        ContactData.TypeDetail data = datas.get( address );
807
824
 
808
825
                        // we don't want to add this address if it exists already or we
809
826
                        // would introduce duplicates
810
 
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
 
827
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
811
828
                                continue;
812
829
 
813
830
                        // add postal address
814
 
                        _backend.addContactAddresses( id, address, data );
 
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 );
815
837
 
816
838
                        // and add this address to the cache to prevent a addition of
817
839
                        // duplicate date from another file
818
 
                        _contacts_cache.addAssociatedAddress( id, address );
 
840
                        _contactsCache.addAssociatedAddress( id, address );
819
841
                }
820
842
        }
821
843
 
823
845
                HashMap< String, ContactData.ExtraDetail > datas )
824
846
        {
825
847
                // add addresses
826
 
                Set< String > datas_keys = datas.keySet();
827
 
                Iterator< String > i = datas_keys.iterator();
 
848
                Set< String > datasKeys = datas.keySet();
 
849
                Iterator< String > i = datasKeys.iterator();
828
850
                while( i.hasNext() ) {
829
851
                        String organisation = i.next();
830
852
                        ContactData.ExtraDetail data = datas.get( organisation );
831
853
 
832
854
                        // we don't want to add this address if it exists already or we
833
855
                        // would introduce duplicates
834
 
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
 
856
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
835
857
                                continue;
836
858
 
837
859
                        // add organisation address
838
 
                        _backend.addContactOrganisation( id, organisation, data );
 
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 );
839
869
 
840
870
                        // and add this address to the cache to prevent a addition of
841
871
                        // duplicate date from another file
842
 
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
 
872
                        _contactsCache.addAssociatedOrganisation( id, organisation );
843
873
                }
844
874
        }
845
875