/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: edam
  • Date: 2012-05-08 15:09:13 UTC
  • Revision ID: tim@ed.am-20120508150913-t9qzbbs1kld691dy
abstracted the android contacts API in to an interface, ready to be switched to
another depending on the platform version.

Show diffs side-by-side

added added

removed removed

31
31
import java.util.regex.Matcher;
32
32
import java.util.regex.Pattern;
33
33
 
34
 
import android.content.ContentUris;
35
 
import android.content.ContentValues;
36
34
import android.content.SharedPreferences;
37
 
import android.net.Uri;
38
35
import android.os.Message;
39
 
import android.provider.Contacts;
40
36
import android.provider.Contacts.PhonesColumns;
41
37
 
42
38
 
59
55
        private boolean _abort = false;
60
56
        private boolean _is_finished = false;
61
57
        private ContactsCache _contacts_cache = null;
 
58
        private Backend _backend = null;
62
59
 
63
60
        /**
64
61
         * Data about a contact
421
418
                        // update UI
422
419
                        setProgressMessage( R.string.doit_caching );
423
420
 
424
 
                        // build a cache of existing contacts
 
421
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
422
//                              _backend = new ContactsContractBackend();
 
423
//                      else
 
424
                                _backend = new ContactsBackend( _doit );
 
425
 
 
426
                        // create a cache of existing contacts and populate it
425
427
                        _contacts_cache = new ContactsCache();
426
 
                        _contacts_cache.buildCache( _doit );
 
428
                        _backend.populateCache( _contacts_cache );
427
429
 
428
430
                        // do the import
429
431
                        onImport();
683
685
                        // should we skip this import altogether?
684
686
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
685
687
 
686
 
                        // get contact's URI
687
 
                        Uri contact_uri = ContentUris.withAppendedId(
688
 
                                Contacts.People.CONTENT_URI, id );
689
 
 
690
688
                        // should we destroy the existing contact before importing?
691
689
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
692
690
                        {
693
691
                                // remove from device
694
 
                                _doit.getContentResolver().delete( contact_uri, null, null );
 
692
                                _backend.deleteContact( id );
695
693
 
696
694
                                // update cache
697
695
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
707
705
                        }
708
706
                }
709
707
 
710
 
                // if we don't have a contact id yet (or if we did, but we destroyed it
 
708
                // if we don't have a contact id yet (or we did, but we destroyed it
711
709
                // when we deleted the contact), we'll have to create a new contact
712
710
                if( id == null )
713
711
                {
714
712
                        // create a new contact
715
 
                        ContentValues values = new ContentValues();
716
 
                        values.put( Contacts.People.NAME, contact._name );
717
 
                        Uri contact_uri = _doit.getContentResolver().insert(
718
 
                                Contacts.People.CONTENT_URI, values );
719
 
                        id = ContentUris.parseId( contact_uri );
 
713
                        id = _backend.addContact( contact._name );
720
714
                        if( id == null || id <= 0 )
721
715
                                showError( R.string.error_unabletoaddcontact );
722
716
 
723
 
                        // try to add them to the "My Contacts" group
724
 
                        try {
725
 
                                Contacts.People.addToMyContactsGroup(
726
 
                                        _doit.getContentResolver(), id );
727
 
                        }
728
 
                        catch( IllegalStateException e ) {
729
 
                                // ignore any failure
730
 
                        }
731
 
 
732
717
                        // update cache
733
718
                        _contacts_cache.addLookup(
734
719
                                ContactsCache.createIdentifier( contact ), id );
760
745
        private void importContactPhones( Long id,
761
746
                        HashMap< String, ContactData.PreferredDetail > datas )
762
747
        {
763
 
                // get URI to contact's phones
764
 
                Uri contact_phones_uri = Uri.withAppendedPath(
765
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
766
 
                        Contacts.People.Phones.CONTENT_DIRECTORY );
767
 
                Set< String > datas_keys = datas.keySet();
768
 
 
769
748
                // add phone numbers
 
749
                Set< String > datas_keys = datas.keySet();
770
750
                Iterator< String > i = datas_keys.iterator();
771
751
                while( i.hasNext() ) {
772
752
                        String number = i.next();
783
763
                                continue;
784
764
 
785
765
                        // add phone number
786
 
                        ContentValues values = new ContentValues();
787
 
                        values.put( Contacts.Phones.TYPE, data.getType() );
788
 
                        values.put( Contacts.Phones.NUMBER, number );
789
 
                        if( data.isPreferred() )
790
 
                                values.put( Contacts.Phones.ISPRIMARY, 1 );
791
 
                        _doit.getContentResolver().insert( contact_phones_uri, values );
 
766
                        _backend.addContactPhone( id, number, data );
792
767
 
793
768
                        // and add this address to the cache to prevent a addition of
794
769
                        // duplicate date from another file
799
774
        private void importContactEmails( Long id,
800
775
                        HashMap< String, ContactData.PreferredDetail > datas )
801
776
        {
802
 
                // get URI to contact's contact methods
803
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
804
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
805
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
806
 
                Set< String > datas_keys = datas.keySet();
807
 
 
808
777
                // add email addresses
 
778
                Set< String > datas_keys = datas.keySet();
809
779
                Iterator< String > i = datas_keys.iterator();
810
780
                while( i.hasNext() ) {
811
781
                        String email = i.next();
817
787
                                continue;
818
788
 
819
789
                        // add phone number
820
 
                        ContentValues values = new ContentValues();
821
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL );
822
 
                        values.put( Contacts.ContactMethods.DATA, email );
823
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
824
 
                        if( data.isPreferred() )
825
 
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
826
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
827
 
                                values );
 
790
                        _backend.addContactEmail( id, email, data );
828
791
 
829
792
                        // and add this address to the cache to prevent a addition of
830
793
                        // duplicate date from another file
835
798
        private void importContactAddresses( Long id,
836
799
                HashMap< String, ContactData.TypeDetail > datas )
837
800
        {
838
 
                // get URI to contact's contact methods
839
 
                Uri contact_contact_methods_uri = Uri.withAppendedPath(
840
 
                        ContentUris.withAppendedId( Contacts.People.CONTENT_URI, id ),
841
 
                        Contacts.People.ContactMethods.CONTENT_DIRECTORY );
842
 
 
843
801
                // add addresses
844
802
                Set< String > datas_keys = datas.keySet();
845
803
                Iterator< String > i = datas_keys.iterator();
853
811
                                continue;
854
812
 
855
813
                        // add postal address
856
 
                        ContentValues values = new ContentValues();
857
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
858
 
                        values.put( Contacts.ContactMethods.DATA, address );
859
 
                        values.put( Contacts.ContactMethods.TYPE, data.getType() );
860
 
                        _doit.getContentResolver().insert( contact_contact_methods_uri,
861
 
                                values );
 
814
                        _backend.addContactAddresses( id, address, data );
862
815
 
863
816
                        // and add this address to the cache to prevent a addition of
864
817
                        // duplicate date from another file
882
835
                                continue;
883
836
 
884
837
                        // add organisation address
885
 
                        ContentValues values = new ContentValues();
886
 
                        values.put( Contacts.Organizations.PERSON_ID, id );
887
 
                        values.put( Contacts.Organizations.COMPANY, organisation );
888
 
                        values.put( Contacts.ContactMethods.TYPE,
889
 
                                Contacts.OrganizationColumns.TYPE_WORK );
890
 
                        if( data.getExtra() != null )
891
 
                                values.put( Contacts.Organizations.TITLE, data.getExtra() );
892
 
                        _doit.getContentResolver().insert(
893
 
                                Contacts.Organizations.CONTENT_URI, values );
 
838
                        _backend.addContactOrganisation( id, organisation, data );
894
839
 
895
840
                        // and add this address to the cache to prevent a addition of
896
841
                        // duplicate date from another file