/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-04-24 11:46:29 UTC
  • Revision ID: tim@ed.am-20120424114629-0ibja9f6vyrz7iwn
fixed bad error_ok string and removed some redundant directories

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;
34
36
import android.content.SharedPreferences;
 
37
import android.net.Uri;
35
38
import android.os.Message;
 
39
import android.provider.Contacts;
36
40
import android.provider.Contacts.PhonesColumns;
37
41
 
38
42
 
55
59
        private boolean _abort = false;
56
60
        private boolean _is_finished = false;
57
61
        private ContactsCache _contacts_cache = null;
58
 
        private Backend _backend = null;
59
62
 
60
63
        /**
61
64
         * Data about a contact
418
421
                        // update UI
419
422
                        setProgressMessage( R.string.doit_caching );
420
423
 
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
 
424
                        // build a cache of existing contacts
427
425
                        _contacts_cache = new ContactsCache();
428
 
                        _backend.populateCache( _contacts_cache );
 
426
                        _contacts_cache.buildCache( _doit );
429
427
 
430
428
                        // do the import
431
429
                        onImport();
685
683
                        // should we skip this import altogether?
686
684
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
685
 
 
686
                        // get contact's URI
 
687
                        Uri contact_uri = ContentUris.withAppendedId(
 
688
                                Contacts.People.CONTENT_URI, id );
 
689
 
688
690
                        // should we destroy the existing contact before importing?
689
691
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
690
692
                        {
691
693
                                // remove from device
692
 
                                _backend.deleteContact( id );
 
694
                                _doit.getContentResolver().delete( contact_uri, null, null );
693
695
 
694
696
                                // update cache
695
697
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
705
707
                        }
706
708
                }
707
709
 
708
 
                // if we don't have a contact id yet (or we did, but we destroyed it
 
710
                // if we don't have a contact id yet (or if we did, but we destroyed it
709
711
                // when we deleted the contact), we'll have to create a new contact
710
712
                if( id == null )
711
713
                {
712
714
                        // create a new contact
713
 
                        id = _backend.addContact( contact._name );
 
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 );
714
720
                        if( id == null || id <= 0 )
715
721
                                showError( R.string.error_unabletoaddcontact );
716
722
 
 
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
 
717
732
                        // update cache
718
733
                        _contacts_cache.addLookup(
719
734
                                ContactsCache.createIdentifier( contact ), id );
745
760
        private void importContactPhones( Long id,
746
761
                        HashMap< String, ContactData.PreferredDetail > datas )
747
762
        {
 
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
 
748
769
                // add phone numbers
749
 
                Set< String > datas_keys = datas.keySet();
750
770
                Iterator< String > i = datas_keys.iterator();
751
771
                while( i.hasNext() ) {
752
772
                        String number = i.next();
763
783
                                continue;
764
784
 
765
785
                        // add phone number
766
 
                        _backend.addContactPhone( id, number, data );
 
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 );
767
792
 
768
793
                        // and add this address to the cache to prevent a addition of
769
794
                        // duplicate date from another file
774
799
        private void importContactEmails( Long id,
775
800
                        HashMap< String, ContactData.PreferredDetail > datas )
776
801
        {
 
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
 
777
808
                // add email addresses
778
 
                Set< String > datas_keys = datas.keySet();
779
809
                Iterator< String > i = datas_keys.iterator();
780
810
                while( i.hasNext() ) {
781
811
                        String email = i.next();
787
817
                                continue;
788
818
 
789
819
                        // add phone number
790
 
                        _backend.addContactEmail( id, email, data );
 
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 );
791
828
 
792
829
                        // and add this address to the cache to prevent a addition of
793
830
                        // duplicate date from another file
798
835
        private void importContactAddresses( Long id,
799
836
                HashMap< String, ContactData.TypeDetail > datas )
800
837
        {
 
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
 
801
843
                // add addresses
802
844
                Set< String > datas_keys = datas.keySet();
803
845
                Iterator< String > i = datas_keys.iterator();
811
853
                                continue;
812
854
 
813
855
                        // add postal address
814
 
                        _backend.addContactAddresses( id, address, data );
 
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 );
815
862
 
816
863
                        // and add this address to the cache to prevent a addition of
817
864
                        // duplicate date from another file
835
882
                                continue;
836
883
 
837
884
                        // add organisation address
838
 
                        _backend.addContactOrganisation( id, organisation, data );
 
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 );
839
894
 
840
895
                        // and add this address to the cache to prevent a addition of
841
896
                        // duplicate date from another file