/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 17:16:08 UTC
  • Revision ID: tim@ed.am-20121220171608-vx41zykf4krel9xf
slightly improved the efficiency of the cache identifier factory function

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
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() );
362
363
        private class Vcard extends ContactData
363
364
        {
364
365
                private final static int NAMELEVEL_NONE = 0;
365
 
                private final static int NAMELEVEL_FN = 1;
366
 
                private final static int NAMELEVEL_N = 2;
 
366
                private final static int NAMELEVEL_N = 1;
 
367
                private final static int NAMELEVEL_FN = 2;
367
368
 
368
369
                private final static int MULTILINE_NONE = 0;
369
370
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
568
569
 
569
570
                                // determine whether we care about this entry
570
571
                                final HashSet< String > interesting_fields =
571
 
                                        new HashSet< String >( Arrays.asList( new String[]
572
 
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
 
572
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
573
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
573
574
                                ) );
574
575
                                boolean is_interesting_field =
575
576
                                        interesting_fields.contains( name_param_parts[ 0 ] );
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" ) &&
609
612
                                                _parser_multiline_state = MULTILINE_ENCODED;
610
613
                                }
611
614
 
612
 
                                // convert 8-bit ASCII charset to US-ASCII
613
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
615
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
616
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
617
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
618
                                        ( charset != null && (
 
619
                                                charset.equals( "ASCII" ) ||
 
620
                                                charset.equals( "US-ASCII" ) ) ) )
 
621
                                {
614
622
                                        value = transcodeAsciiToUtf8( value );
615
 
                                        charset = "UTF-8";
616
623
                                }
617
624
 
618
 
                                // process charset
 
625
                                // process charset (value is now in UTF-8)
619
626
                                String string_value;
620
627
                                try {
621
628
                                        string_value = new String( value.array(), value.position(),
622
 
                                                value.limit() - value.position(), charset );
 
629
                                                value.limit() - value.position(), "UTF-8" );
623
630
                                } catch( UnsupportedEncodingException e ) {
624
631
                                        throw new ParseException( R.string.error_vcf_charset );
625
632
                                }
672
679
                                        parseEMAIL( name_param_parts, complete_value );
673
680
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
674
681
                                        parseADR( name_param_parts, complete_value );
 
682
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
683
                                        parseLABEL( name_param_parts, complete_value );
 
684
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
 
685
                                        parseNOTE( name_param_parts, complete_value );
675
686
                        }
676
687
                }
677
688
 
690
701
                        return ( count & 1 ) == 1;
691
702
                }
692
703
 
693
 
                private String[] splitValueBySemicolon( String value )
 
704
                private String[] splitValueByCharacter( String value, char character )
694
705
                {
695
 
                        // split string in to parts by semicolon
 
706
                        // split string in to parts by specified character
696
707
                        ArrayList< String > parts = new ArrayList< String >(
697
 
                                Arrays.asList( value.split(  ";" ) ) );
 
708
                                Arrays.asList( value.split( "" + character ) ) );
698
709
 
699
710
                        // go through parts
700
711
                        for( int a = 0; a < parts.size(); a++ )
708
719
                                if( a < parts.size() - 1 &&
709
720
                                        doesStringEndInAnEscapeChar( str ) )
710
721
                                {
711
 
                                        // join the next part to this part and remove the next part
 
722
                                        // append the escaped character, join the next part to this
 
723
                                        // part and remove the next part
712
724
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
713
 
                                                ';' + parts.get( a + 1 ) );
 
725
                                                character + parts.get( a + 1 ) );
714
726
                                        parts.remove( a + 1 );
715
727
 
716
728
                                        // re-visit this part
727
739
                        return parts.toArray( ret );
728
740
                }
729
741
 
 
742
                private String unescapeValue( String value )
 
743
                {
 
744
                        StringBuilder ret = new StringBuilder( value.length() );
 
745
                        boolean in_escape = false;
 
746
                        for( int a = 0; a < value.length(); a++ )
 
747
                        {
 
748
                                int c = value.codePointAt( a );
 
749
 
 
750
                                // process a normal character
 
751
                                if( !in_escape ) {
 
752
                                        if( c == '\\' )
 
753
                                                in_escape = true;
 
754
                                        else
 
755
                                                ret.append( Character.toChars( c ) );
 
756
                                        continue;
 
757
                                }
 
758
 
 
759
                                // process an escape sequence
 
760
                                in_escape = false;
 
761
                                switch( c )
 
762
                                {
 
763
                                case 'T':
 
764
                                case 't':
 
765
                                        // add tab (invalid/non-standard, but accepted)
 
766
                                        ret.append( '\t' );
 
767
                                        break;
 
768
                                case 'N':
 
769
                                case 'n':
 
770
                                        // add newline
 
771
                                        ret.append( '\n' );
 
772
                                        break;
 
773
                                case '\\':
 
774
                                case ',':
 
775
                                case ';':
 
776
                                        // add escaped character
 
777
                                        ret.append( Character.toChars( c ) );
 
778
                                        break;
 
779
                                default:
 
780
                                        // unknown escape sequence, so add it unescaped
 
781
                                        // (invalid/non-standard, but accepted)
 
782
                                        ret.append( "\\" );
 
783
                                        ret.append( Character.toChars( c ) );
 
784
                                        break;
 
785
                                }
 
786
                        }
 
787
 
 
788
                        return ret.toString();
 
789
                }
 
790
 
730
791
                private void parseN( String[] params, String value )
731
792
                {
732
793
                        // already got a better name?
733
794
                        if( _name_level >= NAMELEVEL_N ) return;
734
795
 
735
796
                        // get name parts
736
 
                        String[] name_parts = splitValueBySemicolon( value );
 
797
                        String[] name_parts = splitValueByCharacter( value, ';' );
737
798
 
738
799
                        // build name
739
800
                        value = "";
740
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
741
 
                                value += name_parts[ 1 ];
742
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
743
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
801
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
802
                        for( int a = 0; a < part_order.length; a++ )
 
803
                                if( name_parts.length > part_order[ a ] &&
 
804
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
805
                                {
 
806
                                        // split this part in to it's comma-separated bits
 
807
                                        String[] name_part_parts = splitValueByCharacter(
 
808
                                                name_parts[ part_order[ a ] ], ',' );
 
809
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
810
                                                if( name_part_parts[ b ].length() > 0 )
 
811
                                                {
 
812
                                                        if( value.length() == 0 ) value += " ";
 
813
                                                        value += name_part_parts[ b ];
 
814
                                                }
 
815
                                }
744
816
 
745
817
                        // set name
746
 
                        setName( value );
 
818
                        setName( unescapeValue( value ) );
747
819
                        _name_level = NAMELEVEL_N;
748
820
                }
749
821
 
753
825
                        if( _name_level >= NAMELEVEL_FN ) return;
754
826
 
755
827
                        // set name
756
 
                        setName( value );
 
828
                        setName( unescapeValue( value ) );
757
829
                        _name_level = NAMELEVEL_FN;
758
830
                }
759
831
 
760
832
                private void parseORG( String[] params, String value )
761
833
                {
762
834
                        // get org parts
763
 
                        String[] org_parts = splitValueBySemicolon( value );
 
835
                        String[] org_parts = splitValueByCharacter( value, ';' );
764
836
                        if( org_parts == null || org_parts.length < 1 ) return;
765
837
 
766
838
                        // build organisation name
768
840
                                String.valueOf( org_parts[ 0 ] ) );
769
841
                        for( int a = 1; a < org_parts.length; a++ )
770
842
                                builder.append( ", " ).append( org_parts[ a ] );
771
 
                        String organisation = builder.toString();
 
843
                        String organisation = unescapeValue( builder.toString() );
772
844
 
773
845
                        // set organisation name (using a title we've previously found)
774
846
                        addOrganisation( organisation, _cached_title, true );
785
857
 
786
858
                private void parseTITLE( String[] params, String value )
787
859
                {
 
860
                        value = unescapeValue( value );
 
861
 
788
862
                        // if we previously had an organisation, look it up and append this
789
863
                        // title to it
790
864
                        if( _cached_organisation != null && hasOrganisations() ) {
816
890
                        int type;
817
891
                        if( types.contains( "FAX" ) )
818
892
                                if( types.contains( "HOME" ) )
819
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
893
                                        type = TYPE_FAX_HOME;
820
894
                                else
821
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
895
                                        type = TYPE_FAX_WORK;
822
896
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
823
 
                                type = PhonesColumns.TYPE_MOBILE;
 
897
                                type = TYPE_MOBILE;
824
898
                        else if( types.contains( "PAGER" ) )
825
 
                                type = PhonesColumns.TYPE_PAGER;
 
899
                                type = TYPE_PAGER;
826
900
                        else if( types.contains( "WORK" ) )
827
 
                                type = PhonesColumns.TYPE_WORK;
 
901
                                type = TYPE_WORK;
828
902
                        else
829
 
                                type = PhonesColumns.TYPE_HOME;
 
903
                                type = TYPE_HOME;
830
904
 
831
905
                        // add phone number
832
906
                        addNumber( value, type, is_preferred );
843
917
                        boolean is_preferred = types.contains( "PREF" );
844
918
                        int type;
845
919
                        if( types.contains( "WORK" ) )
846
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
920
                                type = TYPE_WORK;
847
921
                        else
848
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
922
                                type = TYPE_HOME;
849
923
 
850
 
                        addEmail( value, type, is_preferred );
 
924
                        addEmail( unescapeValue( value ), type, is_preferred );
851
925
                }
852
926
 
853
927
                private void parseADR( String[] params, String value )
854
928
                {
855
929
                        // get address parts
856
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
930
                        String[] adr_parts = splitValueByCharacter( value, ';' );
857
931
 
858
932
                        // build address
859
933
                        value = "";
860
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
861
 
                                if( value.length() > 0 ) value += "\n";
862
 
                                value += adr_parts[ a ].trim();
863
 
                        }
864
 
 
865
 
                        Set< String > types = extractTypes( params, Arrays.asList(
866
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
867
 
 
868
 
                        // add address
869
 
                        int type;
870
 
                        if( types.contains( "WORK" ) )
871
 
                                type = Contacts.ContactMethods.TYPE_WORK;
872
 
                        else
873
 
                                type = Contacts.ContactMethods.TYPE_HOME;
874
 
 
875
 
                        addAddress( value, type );
 
934
                        for( int a = 0; a < adr_parts.length; a++ )
 
935
                                if( adr_parts[ a ].length() > 0 )
 
936
                                {
 
937
                                        // split this part in to it's comma-separated bits
 
938
                                        String[] adr_part_parts =
 
939
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
940
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
941
                                                if( adr_part_parts[ b ].length() > 0 )
 
942
                                                {
 
943
                                                        if( value.length() > 0 ) value += "\n";
 
944
                                                        value += adr_part_parts[ b ];
 
945
                                                }
 
946
                                }
 
947
 
 
948
                        Set< String > types = extractTypes( params, Arrays.asList(
 
949
                                "PREF", "WORK", "HOME" ) );
 
950
 
 
951
                        // add address
 
952
                        int type;
 
953
                        if( types.contains( "WORK" ) )
 
954
                                type = TYPE_WORK;
 
955
                        else
 
956
                                type = TYPE_HOME;
 
957
 
 
958
                        addAddress( unescapeValue( value ), type );
 
959
                }
 
960
 
 
961
                private void parseLABEL( String[] params, String value )
 
962
                {
 
963
                        Set< String > types = extractTypes( params, Arrays.asList(
 
964
                                "PREF", "WORK", "HOME" ) );
 
965
 
 
966
                        // add address
 
967
                        int type;
 
968
                        if( types.contains( "WORK" ) )
 
969
                                type = TYPE_WORK;
 
970
                        else
 
971
                                type = TYPE_HOME;
 
972
 
 
973
                        addAddress( unescapeValue( value ), type );
 
974
                }
 
975
 
 
976
                private void parseNOTE( String[] params, String value )
 
977
                {
 
978
                        addNote( unescapeValue( value ) );
876
979
                }
877
980
 
878
981
                public void finaliseVcard()
886
989
                        finalise();
887
990
                }
888
991
 
 
992
                /**
 
993
                 * Amongst the params, find the value of the first, only, of any with
 
994
                 * the specified name
 
995
                 * @param params
 
996
                 * @param name
 
997
                 * @return a value, or null
 
998
                 */
889
999
                private String checkParam( String[] params, String name )
890
1000
                {
 
1001
                        String[] res = checkParams( params, name );
 
1002
                        return res.length > 0? res[ 0 ] : null;
 
1003
                }
 
1004
 
 
1005
                /**
 
1006
                 * Amongst the params, find the values of any with the specified name
 
1007
                 * @param params
 
1008
                 * @param name
 
1009
                 * @return an array of values, or null
 
1010
                 */
 
1011
                private String[] checkParams( String[] params, String name )
 
1012
                {
 
1013
                        HashSet< String > ret = new HashSet< String >();
 
1014
 
891
1015
                        Pattern p = Pattern.compile(
892
1016
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
893
1017
                        for( int i = 0; i < params.length; i++ ) {
894
1018
                                Matcher m = p.matcher( params[ i ] );
895
1019
                                if( m.matches() )
896
 
                                        return m.group( 2 );
 
1020
                                        ret.add( m.group( 2 ) );
897
1021
                        }
898
 
                        return null;
 
1022
 
 
1023
                        return (String[]) ret.toArray( new String[ ret.size() ] );
899
1024
                }
900
1025
 
 
1026
                /**
 
1027
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1028
                 * those types are just parameters. For v3.0, they are prefixed with
 
1029
                 * "TYPE=". There may also be multiple type parameters.
 
1030
                 * @param params
 
1031
                 * @param a list of type values to look for
 
1032
                 * @return a set of present type values
 
1033
                 */
901
1034
                private Set< String > extractTypes( String[] params,
902
1035
                        List< String > valid_types )
903
1036
                {
904
1037
                        HashSet< String > types = new HashSet< String >();
905
1038
 
906
1039
                        // get 3.0-style TYPE= param
907
 
                        String type_param;
908
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
909
 
                                String[] parts = type_param.split( "," );
 
1040
                        String type_params[] = checkParams( params, "TYPE" );
 
1041
                        for( int a = 0; a < type_params.length; a++ )
 
1042
                        {
 
1043
                                // check for a comma-separated list of types (why? this isn't in
 
1044
                                // the specs!)
 
1045
                                String[] parts = type_params[ a ].split( "," );
910
1046
                                for( int i = 0; i < parts.length; i++ )
911
1047
                                        if( valid_types.contains( parts[ i ] ) )
912
1048
                                                types.add( parts[ i ] );