/android/import-contacts

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

« back to all changes in this revision

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

  • Committer: Tim Marston
  • Date: 2014-03-02 11:25:27 UTC
  • Revision ID: tim@ed.am-20140302112527-0ygh8puoq8cxwmnz
Tags: 1.3.4
updated NEWS

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
7
 * to as "this program").  For more information, see
 
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
 
30
import java.util.Locale;
28
31
import java.util.Set;
29
32
import java.util.regex.Matcher;
30
33
import java.util.regex.Pattern;
31
34
 
32
 
import android.content.ContentUris;
33
 
import android.content.ContentValues;
 
35
import am.ed.importcontacts.Backend.ContactCreationException;
34
36
import android.content.SharedPreferences;
35
 
import android.net.Uri;
36
37
import android.os.Message;
37
 
import android.provider.Contacts;
38
 
 
39
38
 
40
39
public class Importer extends Thread
41
40
{
56
55
        private boolean _abort = false;
57
56
        private boolean _is_finished = false;
58
57
        private ContactsCache _contacts_cache = null;
59
 
 
60
 
        @SuppressWarnings("serial")
61
 
        protected class ContactNeedsMoreInfoException extends Exception
62
 
        {
63
 
        }
 
58
        private Backend _backend = null;
64
59
 
65
60
        /**
66
61
         * Data about a contact
67
62
         */
68
63
        public class ContactData
69
64
        {
 
65
                public final static int TYPE_HOME = 0;
 
66
                public final static int TYPE_WORK = 1;
 
67
                public final static int TYPE_MOBILE = 2;        // only used with phones
 
68
                public final static int TYPE_FAX_HOME = 3;      // only used with phones
 
69
                public final static int TYPE_FAX_WORK = 4;      // only used with phones
 
70
                public final static int TYPE_PAGER = 5;         // only used with phones
 
71
 
70
72
                class TypeDetail
71
73
                {
72
74
                        protected int _type;
122
124
                        }
123
125
                }
124
126
 
 
127
                @SuppressWarnings("serial")
 
128
                protected class ContactNotIdentifiableException extends Exception
 
129
                {
 
130
                }
 
131
 
125
132
                protected String _name = null;
126
133
                protected String _primary_organisation = null;
127
 
                protected boolean _primary_organisation_is_preferred = false;
 
134
                protected boolean _primary_organisation_is_preferred;
128
135
                protected String _primary_number = null;
129
 
                protected boolean _primary_number_is_preferred = false;
 
136
                protected int _primary_number_type;
 
137
                protected boolean _primary_number_is_preferred;
130
138
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred = false;
 
139
                protected boolean _primary_email_is_preferred;
132
140
                protected HashMap< String, ExtraDetail > _organisations = null;
133
141
                protected HashMap< String, PreferredDetail > _numbers = null;
134
142
                protected HashMap< String, PreferredDetail > _emails = null;
135
143
                protected HashMap< String, TypeDetail > _addresses = null;
 
144
                protected HashSet< String > _notes = null;
 
145
                protected String _birthday = null;
 
146
 
 
147
                private ContactsCache.CacheIdentifier _cache_identifier = null;
136
148
 
137
149
                protected void setName( String name )
138
150
                {
173
185
                                        new ExtraDetail( 0, false, title ) );
174
186
 
175
187
                        // if this is the first organisation added, or it's a preferred
176
 
                        // organisation and a previous organisation wasn't, then remember
177
 
                        // that this is the "primary organisation".
 
188
                        // organisation and the current primary organisation isn't, then
 
189
                        // record this as the primary organisation
178
190
                        if( _primary_organisation == null ||
179
191
                                ( is_preferred && !_primary_organisation_is_preferred ) )
180
192
                        {
221
233
                                _numbers.put( number,
222
234
                                        new PreferredDetail( type, false ) );
223
235
 
 
236
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
 
237
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
238
 
224
239
                        // if this is the first number added, or it's a preferred number
225
 
                        // and a previous number wasn't, then remember that this is the
226
 
                        // "primary number".
 
240
                        // and the current primary number isn't, or this number is on equal
 
241
                        // standing with the primary number in terms of preference and it is
 
242
                        // a voice number and the primary number isn't, then record this as
 
243
                        // the primary number
227
244
                        if( _primary_number == null ||
228
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
245
                                ( is_preferred && !_primary_number_is_preferred ) ||
 
246
                                ( is_preferred == _primary_number_is_preferred &&
 
247
                                        !non_voice_types.contains( type ) &&
 
248
                                        non_voice_types.contains( _primary_number_type ) ) )
229
249
                        {
230
250
                                _primary_number = number;
 
251
                                _primary_number_type = type;
231
252
                                _primary_number_is_preferred = is_preferred;
232
253
                        }
233
254
                }
258
279
                        email = sanitisesEmailAddress( email );
259
280
                        if( email == null )
260
281
                        {
261
 
                                // TODO: warn that an imported email addtrss is being ignored
 
282
                                // TODO: warn that an imported email address is being ignored
262
283
                                return;
263
284
                        }
264
285
 
269
290
                        if( !_emails.containsKey( email ) )
270
291
                                _emails.put( email, new PreferredDetail( type, false ) );
271
292
 
272
 
                        // if this is the first email added, or it's a preferred email
273
 
                        // and a previous email wasn't, then remember that this is the
274
 
                        // "primary email".
 
293
                        // if this is the first email added, or it's a preferred email and
 
294
                        // the current primary organisation isn't, then record this as the
 
295
                        // primary email
275
296
                        if( _primary_email == null ||
276
297
                                ( is_preferred && !_primary_email_is_preferred ) )
277
298
                        {
325
346
                        return _addresses;
326
347
                }
327
348
 
 
349
                protected void addNote( String note )
 
350
                {
 
351
                        if( _notes == null ) _notes = new HashSet< String >();
 
352
                        if( !_notes.contains( note ) )
 
353
                                _notes.add( note );
 
354
                }
 
355
 
 
356
                public boolean hasNotes()
 
357
                {
 
358
                        return _notes != null && _notes.size() > 0;
 
359
                }
 
360
 
 
361
                public HashSet< String > getNotes()
 
362
                {
 
363
                        return _notes;
 
364
                }
 
365
 
 
366
                public void setBirthday( String birthday )
 
367
                {
 
368
                        _birthday = birthday;
 
369
                }
 
370
 
 
371
                public boolean hasBirthday()
 
372
                {
 
373
                        return _birthday != null;
 
374
                }
 
375
 
 
376
                public String getBirthday()
 
377
                {
 
378
                        return _birthday;
 
379
                }
 
380
 
328
381
                protected void finalise()
 
382
                        throws ContactNotIdentifiableException
329
383
                {
330
 
                        // ensure that if there is a primary number, it is preferred so
331
 
                        // that there is always one preferred number. Android will assign
 
384
                        // Ensure that if there is a primary number, it is preferred so
 
385
                        // that there is always one preferred number.  Android will assign
332
386
                        // preference to one anyway so we might as well decide one sensibly.
333
387
                        if( _primary_number != null ) {
334
388
                                PreferredDetail data = _numbers.get( _primary_number );
349
403
                                _organisations.put( _primary_organisation,
350
404
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
405
                        }
 
406
 
 
407
                        // create a cache identifier from this contact data, which can be
 
408
                        // used to look-up an existing contact
 
409
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
410
                        if( _cache_identifier == null )
 
411
                                throw new ContactNotIdentifiableException();
 
412
                }
 
413
 
 
414
                public ContactsCache.CacheIdentifier getCacheIdentifier()
 
415
                {
 
416
                        return _cache_identifier;
352
417
                }
353
418
 
354
419
                private String sanitisePhoneNumber( String number )
368
433
                        Matcher m = p.matcher( email );
369
434
                        if( m.matches() ) {
370
435
                                String[] bits = email.split( "@" );
371
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
436
                                return bits[ 0 ] + "@" +
 
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
 
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
397
469
                        _contacts_cache = new ContactsCache();
398
 
                        _contacts_cache.buildCache( _doit );
 
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
528
581
                checkAbort();
529
582
                _doit._handler.sendMessage( Message.obtain(
530
583
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
 
                        new Integer( max_progress ) ) );
 
584
                        Integer.valueOf( max_progress ) ) );
532
585
        }
533
586
 
534
587
        protected void setTmpProgress( int tmp_progress )
537
590
                checkAbort();
538
591
                _doit._handler.sendMessage( Message.obtain(
539
592
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
540
 
                        new Integer( tmp_progress ) ) );
 
593
                        Integer.valueOf( tmp_progress ) ) );
541
594
        }
542
595
 
543
596
        protected void setProgress( int progress ) throws AbortImportException
545
598
                checkAbort();
546
599
                _doit._handler.sendMessage( Message.obtain(
547
600
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
548
 
                        new Integer( progress ) ) );
 
601
                        Integer.valueOf( progress ) ) );
549
602
        }
550
603
 
551
604
        protected void finish( int action ) throws AbortImportException
569
622
                return _doit.getText( res );
570
623
        }
571
624
 
572
 
        protected boolean isImportRequired( ContactData contact )
573
 
                        throws AbortImportException, ContactNeedsMoreInfoException
574
 
        {
575
 
                checkAbort();
576
 
                return isImportRequired( contact, _merge_setting );
577
 
        }
578
 
 
579
 
        synchronized private boolean isImportRequired(
580
 
                ContactData contact, int merge_setting )
581
 
                throws AbortImportException, ContactNeedsMoreInfoException
 
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
582
637
        {
583
638
                _last_merge_decision = merge_setting;
584
639
 
585
 
                // create a cache identifier which we can use to detect if this contact
586
 
                // is valid for importing
587
 
                ContactsCache.CacheIdentifier identifier =
588
 
                        ContactsCache.createIdentifier( contact );
589
 
                if( identifier == null )
590
 
                        throw new ContactNeedsMoreInfoException();
591
 
 
592
640
                // handle special cases
593
641
                switch( merge_setting )
594
642
                {
595
643
                case Doit.ACTION_KEEP:
596
 
                        // if we keep contacts on duplicate, we better check for one
597
 
                        return !_contacts_cache.canLookup( identifier );
 
644
                        // if we are skipping on a duplicate, check for one
 
645
                        return exists;
598
646
 
599
647
                case Doit.ACTION_PROMPT:
600
 
                        // if we are prompting on duplicate, we better check for one and if
601
 
                        // the contact doesn'te exist, we want to import it
602
 
                        if( !_contacts_cache.canLookup( identifier ) )
603
 
                                return true;
 
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;
604
651
 
605
 
                        // ok, it exists, so do prompt
 
652
                        // ok, duplicate exists, so do prompt
606
653
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
607
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
654
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
608
655
                        try {
609
656
                                wait();
610
657
                        }
618
665
                                _merge_setting = _response;
619
666
 
620
667
                        // recurse, with our new merge setting
621
 
                        return isImportRequired( contact, _response );
 
668
                        return shouldWeSkipContact( contact_detail, exists, _response );
622
669
                }
623
670
 
624
 
                // for all other cases (either overwriting or merging) we will need the
625
 
                // imported data
626
 
                return true;
 
671
                // for all other cases (either overwriting or merging) we don't skip
 
672
                return false;
627
673
        }
628
674
 
629
675
        protected void skipContact() throws AbortImportException
630
676
        {
631
677
                checkAbort();
 
678
 
 
679
                // show that we're skipping a new contact
632
680
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
633
681
        }
634
682
 
637
685
        {
638
686
                checkAbort();
639
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
 
640
694
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
695
//                      finish( ACTION_ABORT );
642
696
 
643
 
                ContentValues values = new ContentValues();
644
 
                boolean ui_informed = false;
645
 
                Long id = null;
646
 
 
647
 
                // give the contact a chance to finalise it's data
648
 
                contact.finalise();
649
 
 
650
 
                // create something, from the contact data, that we can use to identify
651
 
                // a cache entry and attempt to lookup the id of an existing contact in
652
 
                // the cache with it
653
 
                ContactsCache.CacheIdentifier identifier =
654
 
                        ContactsCache.createIdentifier( contact );
655
 
                if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
656
 
 
657
 
                // does contact exist already?
658
 
                if( id != null )
659
 
                {
660
 
                        // should we skip this import altogether?
661
 
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
662
 
 
663
 
                        // get contact's URI
664
 
                        Uri contact_uri = ContentUris.withAppendedId(
665
 
                                Contacts.People.CONTENT_URI, id );
666
 
 
667
 
                        // should we destroy the existing contact before importing?
668
 
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
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 )
669
735
                        {
670
 
                                // remove from device
671
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
736
                                // create a new contact
 
737
                                id = _backend.addContact( contact._name );
672
738
 
673
739
                                // update cache
674
 
                                _contacts_cache.removeLookup( identifier );
675
 
                                _contacts_cache.removeAssociatedData( id );
676
 
 
677
 
                                // show that we're overwriting a contact
678
 
                                _doit._handler.sendEmptyMessage(
679
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
680
 
                                ui_informed = true;
681
 
 
682
 
                                // discard the contact id
683
 
                                id = null;
 
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 );
684
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() );
685
765
                }
686
 
 
687
 
                // if we don't have a contact id yet (or if we did, but we destroyed it
688
 
                // when we deleted the contact), we'll have to create a new contact
689
 
                if( id == null )
 
766
                catch( Backend.ContactCreationException e )
690
767
                {
691
 
                        // create a new contact
692
 
                        values.put( Contacts.People.NAME, contact._name );
693
 
                        Uri contact_uri = _doit.getContentResolver().insert(
694
 
                                Contacts.People.CONTENT_URI, values );
695
 
                        id = ContentUris.parseId( contact_uri );
696
 
                        if( id == null || id <= 0 )
697
 
                                showError( R.string.error_unabletoaddcontact );
698
 
 
699
 
                        // try to add them to the "My Contacts" group
700
 
                        try {
701
 
                                Contacts.People.addToMyContactsGroup(
702
 
                                        _doit.getContentResolver(), id );
703
 
                        }
704
 
                        catch( IllegalStateException e ) {
705
 
                                // ignore any failure
706
 
                        }
707
 
 
708
 
                        // update cache
709
 
                        _contacts_cache.addLookup(
710
 
                                ContactsCache.createIdentifier( contact ), id );
711
 
 
712
 
                        // if we haven't already shown that we're overwriting a contact,
713
 
                        // show that we're creating a new contact
714
 
                        if( !ui_informed ) {
715
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
716
 
                                ui_informed = true;
717
 
                        }
 
768
                        showError( R.string.error_unabletoaddcontact );
718
769
                }
719
 
 
720
 
                // if we haven't already shown that we're overwriting or creating a
721
 
                // contact show that we're merging a contact
722
 
                if( !ui_informed )
723
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
724
 
 
725
 
                // import contact parts
726
 
                if( contact.hasNumbers() )
727
 
                        importContactPhones( id, contact.getNumbers() );
728
 
                if( contact.hasEmails() )
729
 
                        importContactEmails( id, contact.getEmails() );
730
 
                if( contact.hasAddresses() )
731
 
                        importContactAddresses( id, contact.getAddresses() );
732
 
                if( contact.hasOrganisations() )
733
 
                        importContactOrganisations( id, contact.getOrganisations() );
734
770
        }
735
771
 
736
772
        private void importContactPhones( Long id,
737
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
773
                HashMap< String, ContactData.PreferredDetail > datas )
 
774
                throws ContactCreationException
738
775
        {
739
 
                // get URI to contact's phones
740
 
                Uri contact_phones_uri = Uri.withAppendedPath(
741
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
742
 
                        Contacts.People.Phones.CONTENT_DIRECTORY );
743
 
                Set< String > datas_keys = datas.keySet();
744
 
 
745
776
                // add phone numbers
 
777
                Set< String > datas_keys = datas.keySet();
746
778
                Iterator< String > i = datas_keys.iterator();
747
779
                while( i.hasNext() ) {
748
780
                        String number = i.next();
749
781
                        ContactData.PreferredDetail data = datas.get( number );
750
782
 
751
 
                        // we don't want to add this number if it's crap, or it already
752
 
                        // exists (which would cause a duplicate to be created). We don't
753
 
                        // take in to account the type when checking for duplicates. This is
754
 
                        // intentional: types aren't really very reliable. We assume that
755
 
                        // if the number exists at all, it doesn't need importing. Because
756
 
                        // of this, we also can't update the cache (which we don't need to
757
 
                        // anyway, so it's not a problem).
 
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).
758
790
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
759
791
                                continue;
760
792
 
761
793
                        // add phone number
762
 
                        ContentValues values = new ContentValues();
763
 
                        values.put( Contacts.Phones.TYPE, data.getType() );
764
 
                        values.put( Contacts.Phones.NUMBER, number );
765
 
                        if( data.isPreferred() )
766
 
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
767
 
                        _doit.getContentResolver().insert( contact_phones_uri, values );
 
794
                        _backend.addContactPhone( id, number, data );
768
795
 
769
796
                        // and add this address to the cache to prevent a addition of
770
797
                        // duplicate date from another file
773
800
        }
774
801
 
775
802
        private void importContactEmails( Long id,
776
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
803
                HashMap< String, ContactData.PreferredDetail > datas )
 
804
                throws ContactCreationException
777
805
        {
778
 
                // get URI to contact's contact methods
779
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
780
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
781
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
782
 
                Set< String > datas_keys = datas.keySet();
783
 
 
784
806
                // add email addresses
 
807
                Set< String > datas_keys = datas.keySet();
785
808
                Iterator< String > i = datas_keys.iterator();
786
809
                while( i.hasNext() ) {
787
810
                        String email = i.next();
788
811
                        ContactData.PreferredDetail data = datas.get( email );
789
812
 
790
813
                        // we don't want to add this email address if it exists already or
791
 
                        // we would introduce duplicates.
 
814
                        // we would introduce duplicates
792
815
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
793
816
                                continue;
794
817
 
795
818
                        // add phone number
796
 
                        ContentValues values = new ContentValues();
797
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
798
 
                        values.put( Contacts.ContactMethods.DATA, email );
799
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
800
 
                        if( data.isPreferred() )
801
 
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
802
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
803
 
                                values );
 
819
                        _backend.addContactEmail( id, email, data );
804
820
 
805
821
                        // and add this address to the cache to prevent a addition of
806
822
                        // duplicate date from another file
810
826
 
811
827
        private void importContactAddresses( Long id,
812
828
                HashMap< String, ContactData.TypeDetail > datas )
 
829
                throws ContactCreationException
813
830
        {
814
 
                // get URI to contact's contact methods
815
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
816
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
817
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
818
 
 
819
831
                // add addresses
820
832
                Set< String > datas_keys = datas.keySet();
821
833
                Iterator< String > i = datas_keys.iterator();
829
841
                                continue;
830
842
 
831
843
                        // add postal address
832
 
                        ContentValues values = new ContentValues();
833
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
834
 
                        values.put( Contacts.ContactMethods.DATA, address );
835
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
836
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
837
 
                                values );
 
844
                        _backend.addContactAddresses( id, address, data );
838
845
 
839
846
                        // and add this address to the cache to prevent a addition of
840
847
                        // duplicate date from another file
844
851
 
845
852
        private void importContactOrganisations( Long id,
846
853
                HashMap< String, ContactData.ExtraDetail > datas )
 
854
                throws ContactCreationException
847
855
        {
848
856
                // add addresses
849
857
                Set< String > datas_keys = datas.keySet();
858
866
                                continue;
859
867
 
860
868
                        // add organisation address
861
 
                        ContentValues values = new ContentValues();
862
 
                        values.put( Contacts.Organizations.PERSON_ID, id );
863
 
                        values.put( Contacts.Organizations.COMPANY, organisation );
864
 
                        values.put( Contacts.ContactMethods.TYPE,
865
 
                                Contacts.OrganizationColumns.TYPE_WORK );
866
 
                        if( data.getExtra() != null )
867
 
                                values.put( Contacts.Organizations.TITLE, data.getExtra() );
868
 
                        _doit.getContentResolver().insert(
869
 
                                Contacts.Organizations.CONTENT_URI, values );
 
869
                        _backend.addContactOrganisation( id, organisation, data );
870
870
 
871
871
                        // and add this address to the cache to prevent a addition of
872
872
                        // duplicate date from another file
874
874
                }
875
875
        }
876
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 );
 
911
        }
 
912
 
877
913
        synchronized protected void checkAbort() throws AbortImportException
878
914
        {
879
915
                if( _abort ) {