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

  • Committer: edam
  • Date: 2011-05-30 19:20:17 UTC
  • Revision ID: edam@waxworlds.org-20110530192017-5c09k4kgpov02gja
- added checks for Doit.this == null when handling dialog buttons (I managed to abort an import as a duplicate contacts dialog was shown, but can't reproduce it now)
- added line no.s to vcard parsing errors
- update progress bar after a contact is imported, not before
- fixed bug introduced in last commit where a contacts were imported after finaliseVcard()ing failed
- don't show unknown encoding errors for vcard fields that we don't care about (which ignores base64 encoded photos, for example)

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