/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 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
 
import java.util.Arrays;
27
26
import java.util.HashMap;
28
 
import java.util.HashSet;
29
27
import java.util.Iterator;
30
 
import java.util.Locale;
31
28
import java.util.Set;
32
29
import java.util.regex.Matcher;
33
30
import java.util.regex.Pattern;
34
31
 
35
 
import am.ed.importcontacts.Backend.ContactCreationException;
 
32
import android.content.ContentUris;
 
33
import android.content.ContentValues;
36
34
import android.content.SharedPreferences;
 
35
import android.net.Uri;
37
36
import android.os.Message;
 
37
import android.provider.Contacts;
 
38
 
38
39
 
39
40
public class Importer extends Thread
40
41
{
54
55
        private int _last_merge_decision;
55
56
        private boolean _abort = false;
56
57
        private boolean _is_finished = false;
57
 
        private ContactsCache _contacts_cache = null;
58
 
        private Backend _backend = null;
 
58
        private ContactsCache _contactsCache = null;
 
59
 
 
60
        @SuppressWarnings("serial")
 
61
        protected class ContactNeedsMoreInfoException extends Exception
 
62
        {
 
63
        }
59
64
 
60
65
        /**
61
66
         * Data about a contact
62
67
         */
63
68
        public class ContactData
64
69
        {
65
 
                public final static int TYPE_HOME = 0;
66
 
                public final static int TYPE_WORK = 1;
67
 
                public final static int TYPE_MOBILE = 2;        // only used with phones
68
 
                public final static int TYPE_FAX_HOME = 3;      // only used with phones
69
 
                public final static int TYPE_FAX_WORK = 4;      // only used with phones
70
 
                public final static int TYPE_PAGER = 5;         // only used with phones
71
 
 
72
70
                class TypeDetail
73
71
                {
74
72
                        protected int _type;
124
122
                        }
125
123
                }
126
124
 
127
 
                @SuppressWarnings("serial")
128
 
                protected class ContactNotIdentifiableException extends Exception
129
 
                {
130
 
                }
131
 
 
132
125
                protected String _name = null;
133
126
                protected String _primary_organisation = null;
134
 
                protected boolean _primary_organisation_is_preferred;
 
127
                protected boolean _primary_organisation_is_preferred = false;
135
128
                protected String _primary_number = null;
136
 
                protected int _primary_number_type;
137
 
                protected boolean _primary_number_is_preferred;
 
129
                protected boolean _primary_number_is_preferred = false;
138
130
                protected String _primary_email = null;
139
 
                protected boolean _primary_email_is_preferred;
 
131
                protected boolean _primary_email_is_preferred = false;
140
132
                protected HashMap< String, ExtraDetail > _organisations = null;
141
133
                protected HashMap< String, PreferredDetail > _numbers = null;
142
134
                protected HashMap< String, PreferredDetail > _emails = null;
143
135
                protected HashMap< String, TypeDetail > _addresses = null;
144
 
                protected HashSet< String > _notes = null;
145
 
                protected String _birthday = null;
146
 
 
147
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
148
136
 
149
137
                protected void setName( String name )
150
138
                {
185
173
                                        new ExtraDetail( 0, false, title ) );
186
174
 
187
175
                        // if this is the first organisation added, or it's a preferred
188
 
                        // organisation and the current primary organisation isn't, then
189
 
                        // record this as the primary organisation
 
176
                        // organisation and a previous organisation wasn't, then remember
 
177
                        // that this is the "primary organisation".
190
178
                        if( _primary_organisation == null ||
191
179
                                ( is_preferred && !_primary_organisation_is_preferred ) )
192
180
                        {
233
221
                                _numbers.put( number,
234
222
                                        new PreferredDetail( type, false ) );
235
223
 
236
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
237
 
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
238
 
 
239
224
                        // if this is the first number added, or it's a preferred number
240
 
                        // and the current primary number isn't, or this number is on equal
241
 
                        // standing with the primary number in terms of preference and it is
242
 
                        // a voice number and the primary number isn't, then record this as
243
 
                        // the primary number
 
225
                        // and a previous number wasn't, then remember that this is the
 
226
                        // "primary number".
244
227
                        if( _primary_number == null ||
245
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
246
 
                                ( is_preferred == _primary_number_is_preferred &&
247
 
                                        !non_voice_types.contains( type ) &&
248
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
228
                                ( is_preferred && !_primary_number_is_preferred ) )
249
229
                        {
250
230
                                _primary_number = number;
251
 
                                _primary_number_type = type;
252
231
                                _primary_number_is_preferred = is_preferred;
253
232
                        }
254
233
                }
279
258
                        email = sanitisesEmailAddress( email );
280
259
                        if( email == null )
281
260
                        {
282
 
                                // TODO: warn that an imported email address is being ignored
 
261
                                // TODO: warn that an imported email addtrss is being ignored
283
262
                                return;
284
263
                        }
285
264
 
290
269
                        if( !_emails.containsKey( email ) )
291
270
                                _emails.put( email, new PreferredDetail( type, false ) );
292
271
 
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
 
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".
296
275
                        if( _primary_email == null ||
297
276
                                ( is_preferred && !_primary_email_is_preferred ) )
298
277
                        {
346
325
                        return _addresses;
347
326
                }
348
327
 
349
 
                protected void addNote( String note )
350
 
                {
351
 
                        if( _notes == null ) _notes = new HashSet< String >();
352
 
                        if( !_notes.contains( note ) )
353
 
                                _notes.add( note );
354
 
                }
355
 
 
356
 
                public boolean hasNotes()
357
 
                {
358
 
                        return _notes != null && _notes.size() > 0;
359
 
                }
360
 
 
361
 
                public HashSet< String > getNotes()
362
 
                {
363
 
                        return _notes;
364
 
                }
365
 
 
366
 
                public void setBirthday( String birthday )
367
 
                {
368
 
                        _birthday = birthday;
369
 
                }
370
 
 
371
 
                public boolean hasBirthday()
372
 
                {
373
 
                        return _birthday != null;
374
 
                }
375
 
 
376
 
                public String getBirthday()
377
 
                {
378
 
                        return _birthday;
379
 
                }
380
 
 
381
328
                protected void finalise()
382
 
                        throws ContactNotIdentifiableException
383
329
                {
384
 
                        // Ensure that if there is a primary number, it is preferred so
385
 
                        // that there is always one preferred number.  Android will assign
 
330
                        // ensure that if there is a primary number, it is preferred so
 
331
                        // that there is always one preferred number. Android will assign
386
332
                        // preference to one anyway so we might as well decide one sensibly.
387
333
                        if( _primary_number != null ) {
388
334
                                PreferredDetail data = _numbers.get( _primary_number );
403
349
                                _organisations.put( _primary_organisation,
404
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
405
351
                        }
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;
417
352
                }
418
353
 
419
354
                private String sanitisePhoneNumber( String number )
433
368
                        Matcher m = p.matcher( email );
434
369
                        if( m.matches() ) {
435
370
                                String[] bits = email.split( "@" );
436
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
 
371
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
437
372
                        }
438
373
                        return null;
439
374
                }
458
393
                        // update UI
459
394
                        setProgressMessage( R.string.doit_caching );
460
395
 
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
468
 
                        _contacts_cache = new ContactsCache();
469
 
                        _backend.populateCache( _contacts_cache );
 
396
                        // build a cache of existing contacts
 
397
                        _contactsCache = new ContactsCache();
 
398
                        _contactsCache.buildCache( _doit );
470
399
 
471
400
                        // do the import
472
401
                        onImport();
593
522
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
594
523
        }
595
524
 
596
 
        protected void setProgressMax( int max_progress )
 
525
        protected void setProgressMax( int maxProgress )
597
526
                        throws AbortImportException
598
527
        {
599
528
                checkAbort();
600
529
                _doit._handler.sendMessage( Message.obtain(
601
530
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
602
 
                        Integer.valueOf( max_progress ) ) );
 
531
                        new Integer( maxProgress ) ) );
603
532
        }
604
533
 
605
 
        protected void setTmpProgress( int tmp_progress )
606
 
                throws AbortImportException
 
534
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
607
535
        {
608
536
                checkAbort();
609
537
                _doit._handler.sendMessage( Message.obtain(
610
538
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
611
 
                        Integer.valueOf( tmp_progress ) ) );
 
539
                        new Integer( tmpProgress ) ) );
612
540
        }
613
541
 
614
542
        protected void setProgress( int progress ) throws AbortImportException
616
544
                checkAbort();
617
545
                _doit._handler.sendMessage( Message.obtain(
618
546
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
619
 
                        Integer.valueOf( progress ) ) );
 
547
                        new Integer( progress ) ) );
620
548
        }
621
549
 
622
550
        protected void finish( int action ) throws AbortImportException
640
568
                return _doit.getText( res );
641
569
        }
642
570
 
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
 
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
655
581
        {
656
582
                _last_merge_decision = merge_setting;
657
583
 
 
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
 
658
591
                // handle special cases
659
592
                switch( merge_setting )
660
593
                {
661
594
                case Doit.ACTION_KEEP:
662
 
                        // if we are skipping on a duplicate, check for one
663
 
                        return exists;
 
595
                        // if we keep contacts on duplicate, we better check for one
 
596
                        return !_contactsCache.canLookup( identifier );
664
597
 
665
598
                case Doit.ACTION_PROMPT:
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;
 
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;
669
603
 
670
 
                        // ok, duplicate exists, so do prompt
 
604
                        // ok, it exists, so do prompt
671
605
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
672
 
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
 
606
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
673
607
                        try {
674
608
                                wait();
675
609
                        }
683
617
                                _merge_setting = _response;
684
618
 
685
619
                        // recurse, with our new merge setting
686
 
                        return shouldWeSkipContact( contact_detail, exists, _response );
 
620
                        return isImportRequired( contact, _response );
687
621
                }
688
622
 
689
 
                // for all other cases (either overwriting or merging) we don't skip
690
 
                return false;
 
623
                // for all other cases (either overwriting or merging) we will need the
 
624
                // imported data
 
625
                return true;
691
626
        }
692
627
 
693
628
        protected void skipContact() throws AbortImportException
694
629
        {
695
630
                checkAbort();
696
 
 
697
 
                // show that we're skipping a new contact
698
631
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
699
632
        }
700
633
 
703
636
        {
704
637
                checkAbort();
705
638
 
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
 
 
712
639
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
713
640
//                      finish( ACTION_ABORT );
714
641
 
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 ) )
 
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 )
722
658
                {
723
 
                        // show that we're skipping a contact
724
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
725
 
                        return;
 
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 )
 
668
                        {
 
669
                                // remove from device
 
670
                                _doit.getContentResolver().delete( contactUri, null, null );
 
671
 
 
672
                                // 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;
 
683
                        }
726
684
                }
727
685
 
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 )
 
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 )
732
689
                {
733
 
                        contact_deleted = true;
 
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 );
734
697
 
735
 
                        // remove from device
736
 
                        _backend.deleteContact( id );
 
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
                        }
737
706
 
738
707
                        // 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 )
753
 
                        {
754
 
                                // create a new contact
755
 
                                id = _backend.addContact( contact._name );
756
 
 
757
 
                                // update cache
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 );
 
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;
765
716
                        }
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() );
783
 
                }
784
 
                catch( Backend.ContactCreationException e )
785
 
                {
786
 
                        showError( R.string.error_unabletoaddcontact );
787
 
                }
 
717
                }
 
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() );
788
733
        }
789
734
 
790
735
        private void importContactPhones( Long id,
791
 
                HashMap< String, ContactData.PreferredDetail > datas )
792
 
                throws ContactCreationException
 
736
                        HashMap< String, ContactData.PreferredDetail > datas )
793
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
 
794
744
                // add phone numbers
795
 
                Set< String > datas_keys = datas.keySet();
796
 
                Iterator< String > i = datas_keys.iterator();
 
745
                Iterator< String > i = datasKeys.iterator();
797
746
                while( i.hasNext() ) {
798
747
                        String number = i.next();
799
748
                        ContactData.PreferredDetail data = datas.get( number );
800
749
 
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).
808
 
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
 
750
                        // we don't want to add this number if it's crap, or it already
 
751
                        // exists (which would cause a duplicate to be created). We don't
 
752
                        // take in to account the type when checking for duplicates. This is
 
753
                        // intentional: types aren't really very reliable. We assume that
 
754
                        // if the number exists at all, it doesn't need importing. Because
 
755
                        // of this, we also can't update the cache (which we don't need to
 
756
                        // anyway, so it's not a problem).
 
757
                        if( _contactsCache.hasAssociatedNumber( id, number ) )
809
758
                                continue;
810
759
 
811
760
                        // add phone number
812
 
                        _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 );
813
767
 
814
768
                        // and add this address to the cache to prevent a addition of
815
769
                        // duplicate date from another file
816
 
                        _contacts_cache.addAssociatedNumber( id, number );
 
770
                        _contactsCache.addAssociatedNumber( id, number );
817
771
                }
818
772
        }
819
773
 
820
774
        private void importContactEmails( Long id,
821
 
                HashMap< String, ContactData.PreferredDetail > datas )
822
 
                throws ContactCreationException
 
775
                        HashMap< String, ContactData.PreferredDetail > datas )
823
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
 
824
783
                // add email addresses
825
 
                Set< String > datas_keys = datas.keySet();
826
 
                Iterator< String > i = datas_keys.iterator();
 
784
                Iterator< String > i = datasKeys.iterator();
827
785
                while( i.hasNext() ) {
828
786
                        String email = i.next();
829
787
                        ContactData.PreferredDetail data = datas.get( email );
830
788
 
831
789
                        // we don't want to add this email address if it exists already or
832
 
                        // we would introduce duplicates
833
 
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
 
790
                        // we would introduce duplicates.
 
791
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
834
792
                                continue;
835
793
 
836
794
                        // add phone number
837
 
                        _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 );
838
803
 
839
804
                        // and add this address to the cache to prevent a addition of
840
805
                        // duplicate date from another file
841
 
                        _contacts_cache.addAssociatedEmail( id, email );
 
806
                        _contactsCache.addAssociatedEmail( id, email );
842
807
                }
843
808
        }
844
809
 
845
810
        private void importContactAddresses( Long id,
846
811
                HashMap< String, ContactData.TypeDetail > datas )
847
 
                throws ContactCreationException
848
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
 
849
818
                // add addresses
850
 
                Set< String > datas_keys = datas.keySet();
851
 
                Iterator< String > i = datas_keys.iterator();
 
819
                Set< String > datasKeys = datas.keySet();
 
820
                Iterator< String > i = datasKeys.iterator();
852
821
                while( i.hasNext() ) {
853
822
                        String address = i.next();
854
823
                        ContactData.TypeDetail data = datas.get( address );
855
824
 
856
825
                        // we don't want to add this address if it exists already or we
857
826
                        // would introduce duplicates
858
 
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
 
827
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
859
828
                                continue;
860
829
 
861
830
                        // add postal address
862
 
                        _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 );
863
837
 
864
838
                        // and add this address to the cache to prevent a addition of
865
839
                        // duplicate date from another file
866
 
                        _contacts_cache.addAssociatedAddress( id, address );
 
840
                        _contactsCache.addAssociatedAddress( id, address );
867
841
                }
868
842
        }
869
843
 
870
844
        private void importContactOrganisations( Long id,
871
845
                HashMap< String, ContactData.ExtraDetail > datas )
872
 
                throws ContactCreationException
873
846
        {
874
847
                // add addresses
875
 
                Set< String > datas_keys = datas.keySet();
876
 
                Iterator< String > i = datas_keys.iterator();
 
848
                Set< String > datasKeys = datas.keySet();
 
849
                Iterator< String > i = datasKeys.iterator();
877
850
                while( i.hasNext() ) {
878
851
                        String organisation = i.next();
879
852
                        ContactData.ExtraDetail data = datas.get( organisation );
880
853
 
881
854
                        // we don't want to add this address if it exists already or we
882
855
                        // would introduce duplicates
883
 
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
 
856
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
884
857
                                continue;
885
858
 
886
859
                        // add organisation address
887
 
                        _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 );
888
869
 
889
870
                        // and add this address to the cache to prevent a addition of
890
871
                        // duplicate date from another file
891
 
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
892
 
                }
893
 
        }
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 );
 
872
                        _contactsCache.addAssociatedOrganisation( id, organisation );
 
873
                }
929
874
        }
930
875
 
931
876
        synchronized protected void checkAbort() throws AbortImportException