/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-30 15:33:01 UTC
  • Revision ID: edam@waxworlds.org-20110530153301-oor6ci9b3hf9clul
- refactored some code to do with how contacts are imported
- Vcards (and ContactData) instances now generate a CacheIdentifier when they are finalised so that ContactData instances that do not have enough information to identify them can be discovered then
- importContact() now calls the private method checkForDuplicate(), renamed from isImportRequired(), and return if it is not
- importContact() and checkForDuplicate() now use the ContactData's generated CacheIdentifier

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