/android/import-contacts

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

« back to all changes in this revision

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

  • Committer: Tim Marston
  • Date: 2016-03-28 18:13:50 UTC
  • Revision ID: tim@ed.am-20160328181350-ytxvox60fxj1tevm
fixed typo in strings

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
7
 * to as "this program").  For more information, see
 
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
 
30
import java.util.Locale;
28
31
import java.util.Set;
29
32
import java.util.regex.Matcher;
30
33
import java.util.regex.Pattern;
31
34
 
32
 
import android.content.ContentUris;
33
 
import android.content.ContentValues;
 
35
import am.ed.importcontacts.Backend.ContactCreationException;
34
36
import android.content.SharedPreferences;
35
 
import android.net.Uri;
36
37
import android.os.Message;
37
 
import android.provider.Contacts;
38
 
 
39
38
 
40
39
public class Importer extends Thread
41
40
{
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;
 
144
                protected HashSet< String > _notes = null;
 
145
                protected String _birthday = null;
 
146
 
 
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
136
148
 
137
149
                protected void setName( String name )
138
150
                {
173
185
                                        new ExtraDetail( 0, false, title ) );
174
186
 
175
187
                        // if this is the first organisation added, or it's a preferred
176
 
                        // organisation and a previous organisation wasn't, then remember
177
 
                        // that this is the "primary organisation".
 
188
                        // organisation and the current primary organisation isn't, then
 
189
                        // record this as the primary organisation
178
190
                        if( _primary_organisation == null ||
179
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
180
192
                        {
221
233
                                _numbers.put( number,
222
234
                                        new PreferredDetail( type, false ) );
223
235
 
 
236
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
 
237
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
238
 
224
239
                        // if this is the first number added, or it's a preferred number
225
 
                        // and a previous number wasn't, then remember that this is the
226
 
                        // "primary number".
 
240
                        // and the current primary number isn't, or this number is on equal
 
241
                        // standing with the primary number in terms of preference and it is
 
242
                        // a voice number and the primary number isn't, then record this as
 
243
                        // the primary number
227
244
                        if( _primary_number == null ||
228
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
245
                                ( is_preferred && !_primary_number_is_preferred ) ||
 
246
                                ( is_preferred == _primary_number_is_preferred &&
 
247
                                        !non_voice_types.contains( type ) &&
 
248
                                        non_voice_types.contains( _primary_number_type ) ) )
229
249
                        {
230
250
                                _primary_number = number;
 
251
                                _primary_number_type = type;
231
252
                                _primary_number_is_preferred = is_preferred;
232
253
                        }
233
254
                }
258
279
                        email = sanitisesEmailAddress( email );
259
280
                        if( email == null )
260
281
                        {
261
 
                                // TODO: warn that an imported email addtrss is being ignored
 
282
                                // TODO: warn that an imported email address is being ignored
262
283
                                return;
263
284
                        }
264
285
 
269
290
                        if( !_emails.containsKey( email ) )
270
291
                                _emails.put( email, new PreferredDetail( type, false ) );
271
292
 
272
 
                        // if this is the first email added, or it's a preferred email
273
 
                        // and a previous email wasn't, then remember that this is the
274
 
                        // "primary email".
 
293
                        // if this is the first email added, or it's a preferred email and
 
294
                        // the current primary organisation isn't, then record this as the
 
295
                        // primary email
275
296
                        if( _primary_email == null ||
276
297
                                ( is_preferred && !_primary_email_is_preferred ) )
277
298
                        {
325
346
                        return _addresses;
326
347
                }
327
348
 
 
349
                protected void addNote( String note )
 
350
                {
 
351
                        if( _notes == null ) _notes = new HashSet< String >();
 
352
                        if( !_notes.contains( note ) )
 
353
                                _notes.add( note );
 
354
                }
 
355
 
 
356
                public boolean hasNotes()
 
357
                {
 
358
                        return _notes != null && _notes.size() > 0;
 
359
                }
 
360
 
 
361
                public HashSet< String > getNotes()
 
362
                {
 
363
                        return _notes;
 
364
                }
 
365
 
 
366
                public void setBirthday( String birthday )
 
367
                {
 
368
                        _birthday = birthday;
 
369
                }
 
370
 
 
371
                public boolean hasBirthday()
 
372
                {
 
373
                        return _birthday != null;
 
374
                }
 
375
 
 
376
                public String getBirthday()
 
377
                {
 
378
                        return _birthday;
 
379
                }
 
380
 
328
381
                protected void finalise()
 
382
                        throws ContactNotIdentifiableException
329
383
                {
330
 
                        // ensure that if there is a primary number, it is preferred so
331
 
                        // that there is always one preferred number. Android will assign
 
384
                        // Ensure that if there is a primary number, it is preferred so
 
385
                        // that there is always one preferred number.  Android will assign
332
386
                        // preference to one anyway so we might as well decide one sensibly.
333
387
                        if( _primary_number != null ) {
334
388
                                PreferredDetail data = _numbers.get( _primary_number );
349
403
                                _organisations.put( _primary_organisation,
350
404
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
405
                        }
 
406
 
 
407
                        // create a cache identifier from this contact data, which can be
 
408
                        // used to look-up an existing contact
 
409
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
410
                        if( _cache_identifier == null )
 
411
                                throw new ContactNotIdentifiableException();
 
412
                }
 
413
 
 
414
                public ContactsCache.CacheIdentifier getCacheIdentifier()
 
415
                {
 
416
                        return _cache_identifier;
352
417
                }
353
418
 
354
419
                private String sanitisePhoneNumber( String number )
368
433
                        Matcher m = p.matcher( email );
369
434
                        if( m.matches() ) {
370
435
                                String[] bits = email.split( "@" );
371
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
436
                                return bits[ 0 ] + "@" +
 
437
                                        bits[ 1 ].toLowerCase( Locale.ENGLISH );
372
438
                        }
373
439
                        return null;
374
440
                }
393
459
                        // update UI
394
460
                        setProgressMessage( R.string.doit_caching );
395
461
 
396
 
                        // build a cache of existing contacts
397
 
                        _contactsCache = new ContactsCache();
398
 
                        _contactsCache.buildCache( _doit );
 
462
                        // create the appropriate backend
 
463
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
464
                                _backend = new ContactsContractBackend( _doit );
 
465
                        else
 
466
                                _backend = new ContactsBackend( _doit );
 
467
 
 
468
                        // create a cache of existing contacts and populate it
 
469
                        _contacts_cache = new ContactsCache();
 
470
                        _backend.populateCache( _contacts_cache );
399
471
 
400
472
                        // do the import
401
473
                        onImport();
472
544
                finish( ACTION_ABORT );
473
545
        }
474
546
 
475
 
        protected void showFatalError( int res ) throws AbortImportException
476
 
        {
477
 
                showFatalError( _doit.getText( res ).toString() );
478
 
        }
479
 
 
480
 
        synchronized protected void showFatalError( String message )
481
 
                        throws AbortImportException
482
 
        {
483
 
                checkAbort();
484
 
                _doit._handler.sendMessage( Message.obtain(
485
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
486
 
                try {
487
 
                        wait();
488
 
                }
489
 
                catch( InterruptedException e ) { }
490
 
 
491
 
                // no need to check if an abortion happened during the wait, we are
492
 
                // about to finish anyway!
493
 
                finish( ACTION_ABORT );
494
 
        }
495
 
 
496
 
        protected boolean showContinue( int res ) throws AbortImportException
497
 
        {
498
 
                return showContinue( _doit.getText( res ).toString() );
499
 
        }
500
 
 
501
 
        synchronized protected boolean showContinue( String message )
 
547
        protected void showContinueOrAbort( int res ) throws AbortImportException
 
548
        {
 
549
                showContinueOrAbort( _doit.getText( res ).toString() );
 
550
        }
 
551
 
 
552
        synchronized protected void showContinueOrAbort( String message )
502
553
                        throws AbortImportException
503
554
        {
504
555
                checkAbort();
509
560
                }
510
561
                catch( InterruptedException e ) { }
511
562
 
512
 
                // check if an abortion happened during the wait
513
 
                checkAbort();
514
 
 
515
 
                return _response == RESPONSE_POSITIVE;
 
563
                // if we're aborting, there's no need to check if an abortion happened
 
564
                // during the wait
 
565
                if( _response == RESPONSE_NEGATIVE )
 
566
                        finish( ACTION_ABORT );
 
567
                else
 
568
                        checkAbort();
516
569
        }
517
570
 
518
571
        protected void setProgressMessage( int res ) throws AbortImportException
522
575
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
523
576
        }
524
577
 
525
 
        protected void setProgressMax( int maxProgress )
 
578
        protected void setProgressMax( int max_progress )
526
579
                        throws AbortImportException
527
580
        {
528
581
                checkAbort();
529
582
                _doit._handler.sendMessage( Message.obtain(
530
583
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
 
                        new Integer( maxProgress ) ) );
 
584
                        Integer.valueOf( max_progress ) ) );
532
585
        }
533
586
 
534
 
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
 
587
        protected void setTmpProgress( int tmp_progress )
 
588
                throws AbortImportException
535
589
        {
536
590
                checkAbort();
537
591
                _doit._handler.sendMessage( Message.obtain(
538
592
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
539
 
                        new Integer( tmpProgress ) ) );
 
593
                        Integer.valueOf( tmp_progress ) ) );
540
594
        }
541
595
 
542
596
        protected void setProgress( int progress ) throws AbortImportException
544
598
                checkAbort();
545
599
                _doit._handler.sendMessage( Message.obtain(
546
600
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
547
 
                        new Integer( progress ) ) );
 
601
                        Integer.valueOf( progress ) ) );
548
602
        }
549
603
 
550
604
        protected void finish( int action ) throws AbortImportException
568
622
                return _doit.getText( res );
569
623
        }
570
624
 
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
 
625
        /**
 
626
         * Should we skip a contact, given whether it exists or not and the current
 
627
         * merge setting?  This routine handles throwing up a prompt, if required.
 
628
         *
 
629
         * @param contact_detail the display name of the contact
 
630
         * @param exists true if this contact matches one in the cache
 
631
         * @param merge_setting the merge setting to use
 
632
         * @return true if the contact should be skipped outright
 
633
         * @throws AbortImportException
 
634
         */
 
635
        synchronized private boolean shouldWeSkipContact( String contact_detail,
 
636
                boolean exists, int merge_setting ) throws AbortImportException
581
637
        {
582
638
                _last_merge_decision = merge_setting;
583
639
 
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
640
                // handle special cases
592
641
                switch( merge_setting )
593
642
                {
594
643
                case Doit.ACTION_KEEP:
595
 
                        // if we keep contacts on duplicate, we better check for one
596
 
                        return !_contactsCache.canLookup( identifier );
 
644
                        // if we are skipping on a duplicate, check for one
 
645
                        return exists;
597
646
 
598
647
                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;
 
648
                        // if we are prompting on duplicate, then we can say that we won't
 
649
                        // skip if there isn't one
 
650
                        if( !exists ) return false;
603
651
 
604
 
                        // ok, it exists, so do prompt
 
652
                        // ok, duplicate exists, so do prompt
605
653
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
606
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
654
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
607
655
                        try {
608
656
                                wait();
609
657
                        }
617
665
                                _merge_setting = _response;
618
666
 
619
667
                        // recurse, with our new merge setting
620
 
                        return isImportRequired( contact, _response );
 
668
                        return shouldWeSkipContact( contact_detail, exists, _response );
621
669
                }
622
670
 
623
 
                // for all other cases (either overwriting or merging) we will need the
624
 
                // imported data
625
 
                return true;
 
671
                // for all other cases (either overwriting or merging) we don't skip
 
672
                return false;
626
673
        }
627
674
 
628
675
        protected void skipContact() throws AbortImportException
629
676
        {
630
677
                checkAbort();
 
678
 
 
679
                // show that we're skipping a new contact
631
680
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
632
681
        }
633
682
 
636
685
        {
637
686
                checkAbort();
638
687
 
 
688
                // It is expected that we use contact.getCacheIdentifier() here.  The
 
689
                // contact we are passed should have been successfully finalise()d,
 
690
                // which includes generating a valid cache identifier.
 
691
                ContactsCache.CacheIdentifier cache_identifier =
 
692
                        contact.getCacheIdentifier();
 
693
 
639
694
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
640
695
//                      finish( ACTION_ABORT );
641
696
 
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 )
 
697
                // attempt to lookup the id of an existing contact in the cache with
 
698
                // this contact data's cache identifier
 
699
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
 
700
 
 
701
                // check to see if this contact should be skipped
 
702
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
 
703
                        _merge_setting ) )
 
704
                {
 
705
                        // show that we're skipping a contact
 
706
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
707
                        return;
 
708
                }
 
709
 
 
710
                // if a contact exists, and we're overwriting, destroy the existing
 
711
                // contact before importing
 
712
                boolean contact_deleted = false;
 
713
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
714
                {
 
715
                        contact_deleted = true;
 
716
 
 
717
                        // remove from device
 
718
                        _backend.deleteContact( id );
 
719
 
 
720
                        // update cache
 
721
                        _contacts_cache.removeLookup( cache_identifier );
 
722
                        _contacts_cache.removeAssociatedData( id );
 
723
 
 
724
                        // show that we're overwriting a contact
 
725
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
726
 
 
727
                        // discard the contact id
 
728
                        id = null;
 
729
                }
 
730
 
 
731
                try {
 
732
                        // if we don't have a contact id yet (or we did, but we destroyed it
 
733
                        // when we deleted the contact), we'll have to create a new contact
 
734
                        if( id == null )
668
735
                        {
669
 
                                // remove from device
670
 
                                _doit.getContentResolver().delete( contactUri, null, null );
 
736
                                // create a new contact
 
737
                                id = _backend.addContact( contact._name );
671
738
 
672
739
                                // 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;
 
740
                                _contacts_cache.addLookup( cache_identifier, id );
 
741
 
 
742
                                // if we haven't already shown that we're overwriting a contact,
 
743
                                // show that we're creating a new contact
 
744
                                if( !contact_deleted )
 
745
                                        _doit._handler.sendEmptyMessage(
 
746
                                                Doit.MESSAGE_CONTACTCREATED );
683
747
                        }
 
748
                        else
 
749
                                // show that we're merging with an existing contact
 
750
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
751
 
 
752
                        // import contact parts
 
753
                        if( contact.hasNumbers() )
 
754
                                importContactPhones( id, contact.getNumbers() );
 
755
                        if( contact.hasEmails() )
 
756
                                importContactEmails( id, contact.getEmails() );
 
757
                        if( contact.hasAddresses() )
 
758
                                importContactAddresses( id, contact.getAddresses() );
 
759
                        if( contact.hasOrganisations() )
 
760
                                importContactOrganisations( id, contact.getOrganisations() );
 
761
                        if( contact.hasNotes() )
 
762
                                importContactNotes( id, contact.getNotes() );
 
763
                        if( contact.hasBirthday() )
 
764
                                importContactBirthday( id, contact.getBirthday() );
684
765
                }
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 )
 
766
                catch( Backend.ContactCreationException e )
689
767
                {
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
 
                        }
 
768
                        showError( R.string.error_unabletoaddcontact );
717
769
                }
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
770
        }
734
771
 
735
772
        private void importContactPhones( Long id,
736
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
773
                HashMap< String, ContactData.PreferredDetail > datas )
 
774
                throws ContactCreationException
737
775
        {
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
776
                // add phone numbers
745
 
                Iterator< String > i = datasKeys.iterator();
 
777
                Set< String > datas_keys = datas.keySet();
 
778
                Iterator< String > i = datas_keys.iterator();
746
779
                while( i.hasNext() ) {
747
780
                        String number = i.next();
748
781
                        ContactData.PreferredDetail data = datas.get( number );
749
782
 
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 ) )
 
783
                        // We don't want to add this number if it's crap, or it already
 
784
                        // exists (which would cause a duplicate to be created).  We don't
 
785
                        // take in to account the type when checking for duplicates.  This
 
786
                        // is intentional: types aren't really very reliable.  We assume
 
787
                        // that if the number exists at all, it doesn't need importing.
 
788
                        // Because of this, we also can't update the cache (which we don't
 
789
                        // need to anyway, so it's not a problem).
 
790
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
758
791
                                continue;
759
792
 
760
793
                        // 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 );
 
794
                        _backend.addContactPhone( id, number, data );
767
795
 
768
796
                        // and add this address to the cache to prevent a addition of
769
797
                        // duplicate date from another file
770
 
                        _contactsCache.addAssociatedNumber( id, number );
 
798
                        _contacts_cache.addAssociatedNumber( id, number );
771
799
                }
772
800
        }
773
801
 
774
802
        private void importContactEmails( Long id,
775
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
803
                HashMap< String, ContactData.PreferredDetail > datas )
 
804
                throws ContactCreationException
776
805
        {
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
806
                // add email addresses
784
 
                Iterator< String > i = datasKeys.iterator();
 
807
                Set< String > datas_keys = datas.keySet();
 
808
                Iterator< String > i = datas_keys.iterator();
785
809
                while( i.hasNext() ) {
786
810
                        String email = i.next();
787
811
                        ContactData.PreferredDetail data = datas.get( email );
788
812
 
789
813
                        // we don't want to add this email address if it exists already or
790
 
                        // we would introduce duplicates.
791
 
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
 
814
                        // we would introduce duplicates
 
815
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
792
816
                                continue;
793
817
 
794
818
                        // 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 );
 
819
                        _backend.addContactEmail( id, email, data );
803
820
 
804
821
                        // and add this address to the cache to prevent a addition of
805
822
                        // duplicate date from another file
806
 
                        _contactsCache.addAssociatedEmail( id, email );
 
823
                        _contacts_cache.addAssociatedEmail( id, email );
807
824
                }
808
825
        }
809
826
 
810
827
        private void importContactAddresses( Long id,
811
828
                HashMap< String, ContactData.TypeDetail > datas )
 
829
                throws ContactCreationException
812
830
        {
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
831
                // add addresses
819
 
                Set< String > datasKeys = datas.keySet();
820
 
                Iterator< String > i = datasKeys.iterator();
 
832
                Set< String > datas_keys = datas.keySet();
 
833
                Iterator< String > i = datas_keys.iterator();
821
834
                while( i.hasNext() ) {
822
835
                        String address = i.next();
823
836
                        ContactData.TypeDetail data = datas.get( address );
824
837
 
825
838
                        // we don't want to add this address if it exists already or we
826
839
                        // would introduce duplicates
827
 
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
 
840
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
828
841
                                continue;
829
842
 
830
843
                        // 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 );
 
844
                        _backend.addContactAddresses( id, address, data );
837
845
 
838
846
                        // and add this address to the cache to prevent a addition of
839
847
                        // duplicate date from another file
840
 
                        _contactsCache.addAssociatedAddress( id, address );
 
848
                        _contacts_cache.addAssociatedAddress( id, address );
841
849
                }
842
850
        }
843
851
 
844
852
        private void importContactOrganisations( Long id,
845
853
                HashMap< String, ContactData.ExtraDetail > datas )
 
854
                throws ContactCreationException
846
855
        {
847
856
                // add addresses
848
 
                Set< String > datasKeys = datas.keySet();
849
 
                Iterator< String > i = datasKeys.iterator();
 
857
                Set< String > datas_keys = datas.keySet();
 
858
                Iterator< String > i = datas_keys.iterator();
850
859
                while( i.hasNext() ) {
851
860
                        String organisation = i.next();
852
861
                        ContactData.ExtraDetail data = datas.get( organisation );
853
862
 
854
863
                        // we don't want to add this address if it exists already or we
855
864
                        // would introduce duplicates
856
 
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
 
865
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
857
866
                                continue;
858
867
 
859
868
                        // 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 );
 
869
                        _backend.addContactOrganisation( id, organisation, data );
869
870
 
870
871
                        // and add this address to the cache to prevent a addition of
871
872
                        // duplicate date from another file
872
 
                        _contactsCache.addAssociatedOrganisation( id, organisation );
873
 
                }
 
873
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
 
874
                }
 
875
        }
 
876
 
 
877
        private void importContactNotes( Long id, HashSet< String > datas )
 
878
                throws ContactCreationException
 
879
        {
 
880
                // add notes
 
881
                Iterator< String > i = datas.iterator();
 
882
                while( i.hasNext() ) {
 
883
                        String note = i.next();
 
884
 
 
885
                        // we don't want to add this note if it exists already or we would
 
886
                        // introduce duplicates
 
887
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
 
888
                                continue;
 
889
 
 
890
                        // add note
 
891
                        _backend.addContactNote( id, note );
 
892
 
 
893
                        // and add this note to the cache to prevent a addition of duplicate
 
894
                        // date from another file
 
895
                        _contacts_cache.addAssociatedNote( id, note );
 
896
                }
 
897
        }
 
898
 
 
899
        private void importContactBirthday( Long id, String birthday )
 
900
                throws ContactCreationException
 
901
        {
 
902
                // we don't want to import this birthday if it already exists
 
903
                if( _contacts_cache.hasAssociatedBirthday( id, birthday ) )
 
904
                        return;
 
905
 
 
906
                // add birthday
 
907
                _backend.addContactBirthday( id, birthday );
 
908
 
 
909
                // and update the cache
 
910
                _contacts_cache.addAssociatedBirthday( id, birthday );
874
911
        }
875
912
 
876
913
        synchronized protected void checkAbort() throws AbortImportException