/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/VcardImporter.java

  • Committer: edam
  • Date: 2012-12-20 16:49:39 UTC
  • Revision ID: tim@ed.am-20121220164939-j9mg98v0uofws7kw
added support for notes; rewrote backends so that all normalising of data is now done within the contacts cache; made the vCard unescape routine slightly more acceptant of non-standard escaped characters

Show diffs side-by-side

added added

removed removed

38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
 
41
import java.util.Locale;
41
42
import java.util.NoSuchElementException;
42
43
import java.util.Set;
43
44
import java.util.Vector;
44
45
import java.util.regex.Matcher;
45
46
import java.util.regex.Pattern;
46
47
 
 
48
import android.annotation.SuppressLint;
47
49
import android.content.SharedPreferences;
48
 
import android.provider.Contacts;
49
 
import android.provider.Contacts.PhonesColumns;
50
50
 
51
51
public class VcardImporter extends Importer
52
52
{
58
58
                super( doit );
59
59
        }
60
60
 
 
61
        @SuppressLint( "SdCardPath" )
61
62
        @Override
62
63
        protected void onImport() throws AbortImportException
63
64
        {
82
83
                                // get files
83
84
                                class VCardFilter implements FilenameFilter {
84
85
                                        public boolean accept( File dir, String name ) {
85
 
                                                return name.toLowerCase().endsWith( ".vcf" );
 
86
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
86
87
                                        }
87
88
                                }
88
89
                                files = file.listFiles( new VCardFilter() );
576
577
 
577
578
                                // parse encoding parameter
578
579
                                String encoding = checkParam( name_param_parts, "ENCODING" );
579
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
 
580
                                if( encoding != null )
 
581
                                        encoding = encoding.toUpperCase( Locale.US );
580
582
                                if( is_interesting_field && encoding != null &&
581
583
                                        !encoding.equals( "8BIT" ) &&
582
584
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
587
589
 
588
590
                                // parse charset parameter
589
591
                                String charset = checkParam( name_param_parts, "CHARSET" );
590
 
                                if( charset != null ) charset = charset.toUpperCase();
 
592
                                if( charset != null )
 
593
                                        charset = charset.toUpperCase( Locale.US );
591
594
                                if( charset != null &&
592
595
                                        !charset.equals( "US-ASCII" ) &&
593
596
                                        !charset.equals( "ASCII" ) &&
678
681
                                        parseADR( name_param_parts, complete_value );
679
682
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
683
                                        parseLABEL( name_param_parts, complete_value );
 
684
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
 
685
                                        parseNOTE( name_param_parts, complete_value );
681
686
                        }
682
687
                }
683
688
 
755
760
                                in_escape = false;
756
761
                                switch( c )
757
762
                                {
 
763
                                case 'T':
 
764
                                case 't':
 
765
                                        // add tab (invalid/non-standard, but accepted)
 
766
                                        ret.append( '\t' );
 
767
                                        break;
758
768
                                case 'N':
759
769
                                case 'n':
760
770
                                        // add newline
768
778
                                        break;
769
779
                                default:
770
780
                                        // unknown escape sequence, so add it unescaped
 
781
                                        // (invalid/non-standard, but accepted)
771
782
                                        ret.append( "\\" );
772
783
                                        ret.append( Character.toChars( c ) );
773
784
                                        break;
879
890
                        int type;
880
891
                        if( types.contains( "FAX" ) )
881
892
                                if( types.contains( "HOME" ) )
882
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
893
                                        type = TYPE_FAX_HOME;
883
894
                                else
884
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
895
                                        type = TYPE_FAX_WORK;
885
896
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
886
 
                                type = PhonesColumns.TYPE_MOBILE;
 
897
                                type = TYPE_MOBILE;
887
898
                        else if( types.contains( "PAGER" ) )
888
 
                                type = PhonesColumns.TYPE_PAGER;
 
899
                                type = TYPE_PAGER;
889
900
                        else if( types.contains( "WORK" ) )
890
 
                                type = PhonesColumns.TYPE_WORK;
 
901
                                type = TYPE_WORK;
891
902
                        else
892
 
                                type = PhonesColumns.TYPE_HOME;
 
903
                                type = TYPE_HOME;
893
904
 
894
905
                        // add phone number
895
906
                        addNumber( value, type, is_preferred );
906
917
                        boolean is_preferred = types.contains( "PREF" );
907
918
                        int type;
908
919
                        if( types.contains( "WORK" ) )
909
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
920
                                type = TYPE_WORK;
910
921
                        else
911
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
922
                                type = TYPE_HOME;
912
923
 
913
924
                        addEmail( unescapeValue( value ), type, is_preferred );
914
925
                }
940
951
                        // add address
941
952
                        int type;
942
953
                        if( types.contains( "WORK" ) )
943
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
954
                                type = TYPE_WORK;
944
955
                        else
945
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
956
                                type = TYPE_HOME;
946
957
 
947
958
                        addAddress( unescapeValue( value ), type );
948
959
                }
955
966
                        // add address
956
967
                        int type;
957
968
                        if( types.contains( "WORK" ) )
958
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
969
                                type = TYPE_WORK;
959
970
                        else
960
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
971
                                type = TYPE_HOME;
961
972
 
962
973
                        addAddress( unescapeValue( value ), type );
963
974
                }
964
975
 
 
976
                private void parseNOTE( String[] params, String value )
 
977
                {
 
978
                        addNote( unescapeValue( value ) );
 
979
                }
 
980
 
965
981
                public void finaliseVcard()
966
982
                        throws ParseException, ContactNotIdentifiableException
967
983
                {