/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

55
55
        private int _last_merge_decision;
56
56
        private boolean _abort = false;
57
57
        private boolean _is_finished = false;
58
 
        private ContactsCache _contacts_cache = 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
117
122
                        }
118
123
                }
119
124
 
120
 
                @SuppressWarnings("serial")
121
 
                protected class ContactNotIdentifiableException extends Exception
122
 
                {
123
 
                }
124
 
 
125
125
                protected String _name = null;
126
126
                protected String _primary_organisation = null;
127
127
                protected boolean _primary_organisation_is_preferred = false;
134
134
                protected HashMap< String, PreferredDetail > _emails = null;
135
135
                protected HashMap< String, TypeDetail > _addresses = null;
136
136
 
137
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
138
 
 
139
137
                protected void setName( String name )
140
138
                {
141
139
                        _name = name;
328
326
                }
329
327
 
330
328
                protected void finalise()
331
 
                        throws ContactNotIdentifiableException
332
329
                {
333
330
                        // ensure that if there is a primary number, it is preferred so
334
331
                        // that there is always one preferred number. Android will assign
352
349
                                _organisations.put( _primary_organisation,
353
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
354
351
                        }
355
 
 
356
 
                        // create a cache identifier from this contact data, which can be
357
 
                        // used to look-up an existing contact
358
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
359
 
                        if( _cache_identifier == null )
360
 
                                throw new ContactNotIdentifiableException();
361
 
                }
362
 
 
363
 
                public ContactsCache.CacheIdentifier getCacheIdentifier()
364
 
                {
365
 
                        return _cache_identifier;
366
352
                }
367
353
 
368
354
                private String sanitisePhoneNumber( String number )
408
394
                        setProgressMessage( R.string.doit_caching );
409
395
 
410
396
                        // build a cache of existing contacts
411
 
                        _contacts_cache = new ContactsCache();
412
 
                        _contacts_cache.buildCache( _doit );
 
397
                        _contactsCache = new ContactsCache();
 
398
                        _contactsCache.buildCache( _doit );
413
399
 
414
400
                        // do the import
415
401
                        onImport();
536
522
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
537
523
        }
538
524
 
539
 
        protected void setProgressMax( int max_progress )
 
525
        protected void setProgressMax( int maxProgress )
540
526
                        throws AbortImportException
541
527
        {
542
528
                checkAbort();
543
529
                _doit._handler.sendMessage( Message.obtain(
544
530
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
545
 
                        new Integer( max_progress ) ) );
 
531
                        new Integer( maxProgress ) ) );
546
532
        }
547
533
 
548
 
        protected void setTmpProgress( int tmp_progress )
549
 
                throws AbortImportException
 
534
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
550
535
        {
551
536
                checkAbort();
552
537
                _doit._handler.sendMessage( Message.obtain(
553
538
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
554
 
                        new Integer( tmp_progress ) ) );
 
539
                        new Integer( tmpProgress ) ) );
555
540
        }
556
541
 
557
542
        protected void setProgress( int progress ) throws AbortImportException
583
568
                return _doit.getText( res );
584
569
        }
585
570
 
586
 
        synchronized private boolean checkForDuplicate(
587
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
 
                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
589
581
        {
590
582
                _last_merge_decision = merge_setting;
591
583
 
592
 
                // it is ok to use contact.getCacheIdentifier(). The contact has already
593
 
                // been finalised, which means a valid cache identifier will have been
594
 
                // created for it (or it would have been skipped)
 
584
                // create a cache identifier which we can use to detect if this contact
 
585
                // is valid for importing
 
586
                ContactsCache.CacheIdentifier identifier =
 
587
                        ContactsCache.createIdentifier( contact );
 
588
                if( identifier == null )
 
589
                        throw new ContactNeedsMoreInfoException();
595
590
 
596
591
                // handle special cases
597
592
                switch( merge_setting )
598
593
                {
599
594
                case Doit.ACTION_KEEP:
600
595
                        // if we keep contacts on duplicate, we better check for one
601
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
596
                        return !_contactsCache.canLookup( identifier );
602
597
 
603
598
                case Doit.ACTION_PROMPT:
604
599
                        // if we are prompting on duplicate, we better check for one and if
605
600
                        // the contact doesn'te exist, we want to import it
606
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
 
601
                        if( !_contactsCache.canLookup( identifier ) )
607
602
                                return true;
608
603
 
609
604
                        // ok, it exists, so do prompt
610
605
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
611
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
606
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
612
607
                        try {
613
608
                                wait();
614
609
                        }
622
617
                                _merge_setting = _response;
623
618
 
624
619
                        // recurse, with our new merge setting
625
 
                        return checkForDuplicate( cache_identifier, _response );
 
620
                        return isImportRequired( contact, _response );
626
621
                }
627
622
 
628
623
                // for all other cases (either overwriting or merging) we will need the
641
636
        {
642
637
                checkAbort();
643
638
 
644
 
                // It is expected that we use contact.getCacheIdentifier() here. The
645
 
                // contact we are passed should have been successfully finalise()d,
646
 
                // which includes generating a valid cache identifier.
647
 
                ContactsCache.CacheIdentifier cache_identifier =
648
 
                        contact.getCacheIdentifier();
649
 
 
650
 
                // check to see if this contact is a duplicate and should be skipped
651
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
652
 
                        skipContact();
653
 
                        return;
654
 
                }
655
 
 
656
639
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
657
640
//                      finish( ACTION_ABORT );
658
641
 
659
 
                // keep track of whether we've informed the UI of what we're doing
660
 
                boolean ui_informed = false;
661
 
 
662
 
                // attempt to lookup the id of an existing contact in the cache with
663
 
                // this contact data's cache identifier
664
 
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
 
642
                ContentValues values = new ContentValues();
 
643
                boolean uiInformed = false;
 
644
                Long id = null;
 
645
 
 
646
                // give the contact a chance to finalise it's data
 
647
                contact.finalise();
 
648
 
 
649
                // create something, from the contact data, that we can use to identify
 
650
                // a cache entry and attempt to lookup the id of an existing contact in
 
651
                // the cache with it
 
652
                ContactsCache.CacheIdentifier identifier =
 
653
                        ContactsCache.createIdentifier( contact );
 
654
                if( identifier != null ) id = (Long)_contactsCache.lookup( identifier );
665
655
 
666
656
                // does contact exist already?
667
657
                if( id != null )
670
660
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
671
661
 
672
662
                        // get contact's URI
673
 
                        Uri contact_uri = ContentUris.withAppendedId(
 
663
                        Uri contactUri = ContentUris.withAppendedId(
674
664
                                Contacts.People.CONTENT_URI, id );
675
665
 
676
666
                        // should we destroy the existing contact before importing?
677
667
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
678
668
                        {
679
669
                                // remove from device
680
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
670
                                _doit.getContentResolver().delete( contactUri, null, null );
681
671
 
682
672
                                // update cache
683
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
684
 
                                _contacts_cache.removeAssociatedData( id );
 
673
                                _contactsCache.removeLookup( identifier );
 
674
                                _contactsCache.removeAssociatedData( id );
685
675
 
686
676
                                // show that we're overwriting a contact
687
677
                                _doit._handler.sendEmptyMessage(
688
678
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
689
 
                                ui_informed = true;
 
679
                                uiInformed = true;
690
680
 
691
681
                                // discard the contact id
692
682
                                id = null;
698
688
                if( id == null )
699
689
                {
700
690
                        // create a new contact
701
 
                        ContentValues values = new ContentValues();
702
691
                        values.put( Contacts.People.NAME, contact._name );
703
 
                        Uri contact_uri = _doit.getContentResolver().insert(
 
692
                        Uri contactUri = _doit.getContentResolver().insert(
704
693
                                Contacts.People.CONTENT_URI, values );
705
 
                        id = ContentUris.parseId( contact_uri );
 
694
                        id = ContentUris.parseId( contactUri );
706
695
                        if( id == null || id <= 0 )
707
696
                                showError( R.string.error_unabletoaddcontact );
708
697
 
716
705
                        }
717
706
 
718
707
                        // update cache
719
 
                        _contacts_cache.addLookup(
 
708
                        _contactsCache.addLookup(
720
709
                                ContactsCache.createIdentifier( contact ), id );
721
710
 
722
711
                        // if we haven't already shown that we're overwriting a contact,
723
712
                        // show that we're creating a new contact
724
 
                        if( !ui_informed ) {
 
713
                        if( !uiInformed ) {
725
714
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
726
 
                                ui_informed = true;
 
715
                                uiInformed = true;
727
716
                        }
728
717
                }
729
718
 
730
719
                // if we haven't already shown that we're overwriting or creating a
731
 
                // contact, show that we're merging a contact
732
 
                if( !ui_informed )
 
720
                // contact show that we're merging a contact
 
721
                if( !uiInformed )
733
722
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
734
723
 
735
724
                // import contact parts
747
736
                        HashMap< String, ContactData.PreferredDetail > datas )
748
737
        {
749
738
                // get URI to contact's phones
750
 
                Uri contact_phones_uri = Uri.withAppendedPath(
 
739
                Uri contactPhonesUri = Uri.withAppendedPath(
751
740
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
752
741
                        Contacts.People.Phones.CONTENT_DIRECTORY );
753
 
                Set< String > datas_keys = datas.keySet();
 
742
                Set< String > datasKeys = datas.keySet();
754
743
 
755
744
                // add phone numbers
756
 
                Iterator< String > i = datas_keys.iterator();
 
745
                Iterator< String > i = datasKeys.iterator();
757
746
                while( i.hasNext() ) {
758
747
                        String number = i.next();
759
748
                        ContactData.PreferredDetail data = datas.get( number );
765
754
                        // if the number exists at all, it doesn't need importing. Because
766
755
                        // of this, we also can't update the cache (which we don't need to
767
756
                        // anyway, so it's not a problem).
768
 
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
 
757
                        if( _contactsCache.hasAssociatedNumber( id, number ) )
769
758
                                continue;
770
759
 
771
760
                        // add phone number
774
763
                        values.put( Contacts.Phones.NUMBER, number );
775
764
                        if( data.isPreferred() )
776
765
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
777
 
                        _doit.getContentResolver().insert( contact_phones_uri, values );
 
766
                        _doit.getContentResolver().insert( contactPhonesUri, values );
778
767
 
779
768
                        // and add this address to the cache to prevent a addition of
780
769
                        // duplicate date from another file
781
 
                        _contacts_cache.addAssociatedNumber( id, number );
 
770
                        _contactsCache.addAssociatedNumber( id, number );
782
771
                }
783
772
        }
784
773
 
786
775
                        HashMap< String, ContactData.PreferredDetail > datas )
787
776
        {
788
777
                // get URI to contact's contact methods
789
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
778
                Uri contactContactMethodsUri = Uri.withAppendedPath(
790
779
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
791
780
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
792
 
                Set< String > datas_keys = datas.keySet();
 
781
                Set< String > datasKeys = datas.keySet();
793
782
 
794
783
                // add email addresses
795
 
                Iterator< String > i = datas_keys.iterator();
 
784
                Iterator< String > i = datasKeys.iterator();
796
785
                while( i.hasNext() ) {
797
786
                        String email = i.next();
798
787
                        ContactData.PreferredDetail data = datas.get( email );
799
788
 
800
789
                        // we don't want to add this email address if it exists already or
801
790
                        // we would introduce duplicates.
802
 
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
 
791
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
803
792
                                continue;
804
793
 
805
794
                        // add phone number
809
798
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
810
799
                        if( data.isPreferred() )
811
800
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
812
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
 
801
                        _doit.getContentResolver().insert( contactContactMethodsUri,
813
802
                                values );
814
803
 
815
804
                        // and add this address to the cache to prevent a addition of
816
805
                        // duplicate date from another file
817
 
                        _contacts_cache.addAssociatedEmail( id, email );
 
806
                        _contactsCache.addAssociatedEmail( id, email );
818
807
                }
819
808
        }
820
809
 
822
811
                HashMap< String, ContactData.TypeDetail > datas )
823
812
        {
824
813
                // get URI to contact's contact methods
825
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
 
814
                Uri contactContactMethodsUri = Uri.withAppendedPath(
826
815
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
827
816
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
828
817
 
829
818
                // add addresses
830
 
                Set< String > datas_keys = datas.keySet();
831
 
                Iterator< String > i = datas_keys.iterator();
 
819
                Set< String > datasKeys = datas.keySet();
 
820
                Iterator< String > i = datasKeys.iterator();
832
821
                while( i.hasNext() ) {
833
822
                        String address = i.next();
834
823
                        ContactData.TypeDetail data = datas.get( address );
835
824
 
836
825
                        // we don't want to add this address if it exists already or we
837
826
                        // would introduce duplicates
838
 
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
 
827
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
839
828
                                continue;
840
829
 
841
830
                        // add postal address
843
832
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
844
833
                        values.put( Contacts.ContactMethods.DATA, address );
845
834
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
846
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
 
835
                        _doit.getContentResolver().insert( contactContactMethodsUri,
847
836
                                values );
848
837
 
849
838
                        // and add this address to the cache to prevent a addition of
850
839
                        // duplicate date from another file
851
 
                        _contacts_cache.addAssociatedAddress( id, address );
 
840
                        _contactsCache.addAssociatedAddress( id, address );
852
841
                }
853
842
        }
854
843
 
856
845
                HashMap< String, ContactData.ExtraDetail > datas )
857
846
        {
858
847
                // add addresses
859
 
                Set< String > datas_keys = datas.keySet();
860
 
                Iterator< String > i = datas_keys.iterator();
 
848
                Set< String > datasKeys = datas.keySet();
 
849
                Iterator< String > i = datasKeys.iterator();
861
850
                while( i.hasNext() ) {
862
851
                        String organisation = i.next();
863
852
                        ContactData.ExtraDetail data = datas.get( organisation );
864
853
 
865
854
                        // we don't want to add this address if it exists already or we
866
855
                        // would introduce duplicates
867
 
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
 
856
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
868
857
                                continue;
869
858
 
870
859
                        // add organisation address
880
869
 
881
870
                        // and add this address to the cache to prevent a addition of
882
871
                        // duplicate date from another file
883
 
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
 
872
                        _contactsCache.addAssociatedOrganisation( id, organisation );
884
873
                }
885
874
        }
886
875