/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-05 21:49:43 UTC
  • Revision ID: edam@waxworlds.org-20110505214943-bg0cn6qz0gr49dlk
- updated TODO
- made varibale names consistent (camelCaseVariables now_use_underscores)

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;
 
58
        private ContactsCache _contacts_cache = null;
59
59
 
60
60
        @SuppressWarnings("serial")
61
61
        protected class ContactNeedsMoreInfoException extends Exception
394
394
                        setProgressMessage( R.string.doit_caching );
395
395
 
396
396
                        // build a cache of existing contacts
397
 
                        _contactsCache = new ContactsCache();
398
 
                        _contactsCache.buildCache( _doit );
 
397
                        _contacts_cache = new ContactsCache();
 
398
                        _contacts_cache.buildCache( _doit );
399
399
 
400
400
                        // do the import
401
401
                        onImport();
522
522
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
523
523
        }
524
524
 
525
 
        protected void setProgressMax( int maxProgress )
 
525
        protected void setProgressMax( int max_progress )
526
526
                        throws AbortImportException
527
527
        {
528
528
                checkAbort();
529
529
                _doit._handler.sendMessage( Message.obtain(
530
530
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
531
 
                        new Integer( maxProgress ) ) );
 
531
                        new Integer( max_progress ) ) );
532
532
        }
533
533
 
534
 
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
 
534
        protected void setTmpProgress( int tmp_progress )
 
535
                throws AbortImportException
535
536
        {
536
537
                checkAbort();
537
538
                _doit._handler.sendMessage( Message.obtain(
538
539
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
539
 
                        new Integer( tmpProgress ) ) );
 
540
                        new Integer( tmp_progress ) ) );
540
541
        }
541
542
 
542
543
        protected void setProgress( int progress ) throws AbortImportException
593
594
                {
594
595
                case Doit.ACTION_KEEP:
595
596
                        // if we keep contacts on duplicate, we better check for one
596
 
                        return !_contactsCache.canLookup( identifier );
 
597
                        return !_contacts_cache.canLookup( identifier );
597
598
 
598
599
                case Doit.ACTION_PROMPT:
599
600
                        // if we are prompting on duplicate, we better check for one and if
600
601
                        // the contact doesn'te exist, we want to import it
601
 
                        if( !_contactsCache.canLookup( identifier ) )
 
602
                        if( !_contacts_cache.canLookup( identifier ) )
602
603
                                return true;
603
604
 
604
605
                        // ok, it exists, so do prompt
640
641
//                      finish( ACTION_ABORT );
641
642
 
642
643
                ContentValues values = new ContentValues();
643
 
                boolean uiInformed = false;
 
644
                boolean ui_informed = false;
644
645
                Long id = null;
645
646
 
646
647
                // give the contact a chance to finalise it's data
651
652
                // the cache with it
652
653
                ContactsCache.CacheIdentifier identifier =
653
654
                        ContactsCache.createIdentifier( contact );
654
 
                if( identifier != null ) id = (Long)_contactsCache.lookup( identifier );
 
655
                if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
655
656
 
656
657
                // does contact exist already?
657
658
                if( id != null )
660
661
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
661
662
 
662
663
                        // get contact's URI
663
 
                        Uri contactUri = ContentUris.withAppendedId(
 
664
                        Uri contact_uri = ContentUris.withAppendedId(
664
665
                                Contacts.People.CONTENT_URI, id );
665
666
 
666
667
                        // should we destroy the existing contact before importing?
667
668
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
668
669
                        {
669
670
                                // remove from device
670
 
                                _doit.getContentResolver().delete( contactUri, null, null );
 
671
                                _doit.getContentResolver().delete( contact_uri, null, null );
671
672
 
672
673
                                // update cache
673
 
                                _contactsCache.removeLookup( identifier );
674
 
                                _contactsCache.removeAssociatedData( id );
 
674
                                _contacts_cache.removeLookup( identifier );
 
675
                                _contacts_cache.removeAssociatedData( id );
675
676
 
676
677
                                // show that we're overwriting a contact
677
678
                                _doit._handler.sendEmptyMessage(
678
679
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
679
 
                                uiInformed = true;
 
680
                                ui_informed = true;
680
681
 
681
682
                                // discard the contact id
682
683
                                id = null;
689
690
                {
690
691
                        // create a new contact
691
692
                        values.put( Contacts.People.NAME, contact._name );
692
 
                        Uri contactUri = _doit.getContentResolver().insert(
 
693
                        Uri contact_uri = _doit.getContentResolver().insert(
693
694
                                Contacts.People.CONTENT_URI, values );
694
 
                        id = ContentUris.parseId( contactUri );
 
695
                        id = ContentUris.parseId( contact_uri );
695
696
                        if( id == null || id <= 0 )
696
697
                                showError( R.string.error_unabletoaddcontact );
697
698
 
705
706
                        }
706
707
 
707
708
                        // update cache
708
 
                        _contactsCache.addLookup(
 
709
                        _contacts_cache.addLookup(
709
710
                                ContactsCache.createIdentifier( contact ), id );
710
711
 
711
712
                        // if we haven't already shown that we're overwriting a contact,
712
713
                        // show that we're creating a new contact
713
 
                        if( !uiInformed ) {
 
714
                        if( !ui_informed ) {
714
715
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
715
 
                                uiInformed = true;
 
716
                                ui_informed = true;
716
717
                        }
717
718
                }
718
719
 
719
720
                // if we haven't already shown that we're overwriting or creating a
720
721
                // contact show that we're merging a contact
721
 
                if( !uiInformed )
 
722
                if( !ui_informed )
722
723
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
723
724
 
724
725
                // import contact parts
736
737
                        HashMap< String, ContactData.PreferredDetail > datas )
737
738
        {
738
739
                // get URI to contact's phones
739
 
                Uri contactPhonesUri = Uri.withAppendedPath(
 
740
                Uri contact_phones_uri = Uri.withAppendedPath(
740
741
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
741
742
                        Contacts.People.Phones.CONTENT_DIRECTORY );
742
 
                Set< String > datasKeys = datas.keySet();
 
743
                Set< String > datas_keys = datas.keySet();
743
744
 
744
745
                // add phone numbers
745
 
                Iterator< String > i = datasKeys.iterator();
 
746
                Iterator< String > i = datas_keys.iterator();
746
747
                while( i.hasNext() ) {
747
748
                        String number = i.next();
748
749
                        ContactData.PreferredDetail data = datas.get( number );
754
755
                        // if the number exists at all, it doesn't need importing. Because
755
756
                        // of this, we also can't update the cache (which we don't need to
756
757
                        // anyway, so it's not a problem).
757
 
                        if( _contactsCache.hasAssociatedNumber( id, number ) )
 
758
                        if( _contacts_cache.hasAssociatedNumber( id, number ) )
758
759
                                continue;
759
760
 
760
761
                        // add phone number
763
764
                        values.put( Contacts.Phones.NUMBER, number );
764
765
                        if( data.isPreferred() )
765
766
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
766
 
                        _doit.getContentResolver().insert( contactPhonesUri, values );
 
767
                        _doit.getContentResolver().insert( contact_phones_uri, values );
767
768
 
768
769
                        // and add this address to the cache to prevent a addition of
769
770
                        // duplicate date from another file
770
 
                        _contactsCache.addAssociatedNumber( id, number );
 
771
                        _contacts_cache.addAssociatedNumber( id, number );
771
772
                }
772
773
        }
773
774
 
775
776
                        HashMap< String, ContactData.PreferredDetail > datas )
776
777
        {
777
778
                // get URI to contact's contact methods
778
 
                Uri contactContactMethodsUri = Uri.withAppendedPath(
 
779
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
779
780
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
780
781
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
781
 
                Set< String > datasKeys = datas.keySet();
 
782
                Set< String > datas_keys = datas.keySet();
782
783
 
783
784
                // add email addresses
784
 
                Iterator< String > i = datasKeys.iterator();
 
785
                Iterator< String > i = datas_keys.iterator();
785
786
                while( i.hasNext() ) {
786
787
                        String email = i.next();
787
788
                        ContactData.PreferredDetail data = datas.get( email );
788
789
 
789
790
                        // we don't want to add this email address if it exists already or
790
791
                        // we would introduce duplicates.
791
 
                        if( _contactsCache.hasAssociatedEmail( id, email ) )
 
792
                        if( _contacts_cache.hasAssociatedEmail( id, email ) )
792
793
                                continue;
793
794
 
794
795
                        // add phone number
798
799
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
799
800
                        if( data.isPreferred() )
800
801
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
801
 
                        _doit.getContentResolver().insert( contactContactMethodsUri,
 
802
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
802
803
                                values );
803
804
 
804
805
                        // and add this address to the cache to prevent a addition of
805
806
                        // duplicate date from another file
806
 
                        _contactsCache.addAssociatedEmail( id, email );
 
807
                        _contacts_cache.addAssociatedEmail( id, email );
807
808
                }
808
809
        }
809
810
 
811
812
                HashMap< String, ContactData.TypeDetail > datas )
812
813
        {
813
814
                // get URI to contact's contact methods
814
 
                Uri contactContactMethodsUri = Uri.withAppendedPath(
 
815
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
815
816
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
816
817
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
817
818
 
818
819
                // add addresses
819
 
                Set< String > datasKeys = datas.keySet();
820
 
                Iterator< String > i = datasKeys.iterator();
 
820
                Set< String > datas_keys = datas.keySet();
 
821
                Iterator< String > i = datas_keys.iterator();
821
822
                while( i.hasNext() ) {
822
823
                        String address = i.next();
823
824
                        ContactData.TypeDetail data = datas.get( address );
824
825
 
825
826
                        // we don't want to add this address if it exists already or we
826
827
                        // would introduce duplicates
827
 
                        if( _contactsCache.hasAssociatedAddress( id, address ) )
 
828
                        if( _contacts_cache.hasAssociatedAddress( id, address ) )
828
829
                                continue;
829
830
 
830
831
                        // add postal address
832
833
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
833
834
                        values.put( Contacts.ContactMethods.DATA, address );
834
835
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
835
 
                        _doit.getContentResolver().insert( contactContactMethodsUri,
 
836
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
836
837
                                values );
837
838
 
838
839
                        // and add this address to the cache to prevent a addition of
839
840
                        // duplicate date from another file
840
 
                        _contactsCache.addAssociatedAddress( id, address );
 
841
                        _contacts_cache.addAssociatedAddress( id, address );
841
842
                }
842
843
        }
843
844
 
845
846
                HashMap< String, ContactData.ExtraDetail > datas )
846
847
        {
847
848
                // add addresses
848
 
                Set< String > datasKeys = datas.keySet();
849
 
                Iterator< String > i = datasKeys.iterator();
 
849
                Set< String > datas_keys = datas.keySet();
 
850
                Iterator< String > i = datas_keys.iterator();
850
851
                while( i.hasNext() ) {
851
852
                        String organisation = i.next();
852
853
                        ContactData.ExtraDetail data = datas.get( organisation );
853
854
 
854
855
                        // we don't want to add this address if it exists already or we
855
856
                        // would introduce duplicates
856
 
                        if( _contactsCache.hasAssociatedOrganisation( id, organisation ) )
 
857
                        if( _contacts_cache.hasAssociatedOrganisation( id, organisation ) )
857
858
                                continue;
858
859
 
859
860
                        // add organisation address
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
                        _contacts_cache.addAssociatedOrganisation( id, organisation );
873
874
                }
874
875
        }
875
876