/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: Tim Marston
  • Date: 2016-03-28 18:18:11 UTC
  • Revision ID: tim@ed.am-20160328181811-as61lllmvnv8e5os
handle vcards with missing vesion lines (treat as v2.1)

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 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
 
7
 * to as "this program").  For more information, see
8
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
45
45
import java.util.regex.Matcher;
46
46
import java.util.regex.Pattern;
47
47
 
48
 
import android.annotation.SuppressLint;
49
48
import android.content.SharedPreferences;
 
49
import android.os.Environment;
50
50
 
51
51
public class VcardImporter extends Importer
52
52
{
58
58
                super( doit );
59
59
        }
60
60
 
61
 
        @SuppressLint( "SdCardPath" )
62
61
        @Override
63
62
        protected void onImport() throws AbortImportException
64
63
        {
71
70
                File[] files = null;
72
71
                try
73
72
                {
 
73
                        // check SD card is mounted
 
74
                        String state = Environment.getExternalStorageState();
 
75
                        if( !Environment.MEDIA_MOUNTED.equals( state ) &&
 
76
                                !Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) )
 
77
                        {
 
78
                                showError( R.string.error_nosdcard );
 
79
                        }
 
80
 
74
81
                        // open directory
75
 
                        String path = "/sdcard" + prefs.getString( "location", "/" );
76
 
                        File file = new File( path );
 
82
                        File file = new File( Environment.getExternalStorageDirectory(),
 
83
                                prefs.getString( "location", "/" ) );
77
84
                        if( !file.exists() )
78
85
                                showError( R.string.error_locationnotfound );
79
86
 
83
90
                                // get files
84
91
                                class VCardFilter implements FilenameFilter {
85
92
                                        public boolean accept( File dir, String name ) {
86
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
93
                                                return name.toLowerCase( Locale.ENGLISH )
 
94
                                                        .endsWith( ".vcf" );
87
95
                                        }
88
96
                                }
89
97
                                files = file.listFiles( new VCardFilter() );
136
144
                                if( !in_vcard )
137
145
                                {
138
146
                                        // look for vcard beginning
139
 
                                        if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
147
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
140
148
                                                in_vcard = true;
141
149
                                                _vcard_count++;
142
150
                                        }
143
151
                                        // check for vMsg files
144
 
                                        else if( line.matches( "^BEGIN[ \t]*:[ \t]*VMSG.*$" ) ) {
 
152
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
145
153
                                                showError( getText( R.string.error_vcf_vmsgfile )
146
154
                                                        + file.getName() );
147
155
                                        }
148
156
                                }
149
 
                                else if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
157
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
150
158
                                        in_vcard = false;
151
159
                        }
 
160
                        reader.close();
152
161
 
153
162
                }
154
163
                catch( FileNotFoundException e ) {
176
185
                        FileInputStream istream = new FileInputStream( file );
177
186
                        byte[] content = new byte[ (int)file.length() ];
178
187
                        istream.read( content );
179
 
                        istream = null;
 
188
                        istream.close();
180
189
 
181
190
                        // import
182
191
                        importVCardFileContent( content, file.getName() );
209
218
 
210
219
                        if( vcard == null ) {
211
220
                                // look for vcard beginning
212
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
221
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
213
222
                                        setProgress( _progress++ );
214
223
                                        vcard = new Vcard();
215
224
                                        vcard_start_line = cli.getLineNumber();
217
226
                        }
218
227
                        else {
219
228
                                // look for vcard content or ending
220
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
229
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
221
230
                                {
222
231
                                        // finalise the vcard/contact
223
232
                                        try {
227
236
                                                importContact( vcard );
228
237
                                        }
229
238
                                        catch( Vcard.ParseException e ) {
230
 
                                                if( !showContinue(
 
239
                                                showContinueOrAbort(
231
240
                                                        getText( R.string.error_vcf_parse ).toString()
232
241
                                                        + fileName +
233
242
                                                        getText( R.string.error_vcf_parse_line ).toString()
234
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
235
 
                                                {
236
 
                                                        finish( ACTION_ABORT );
237
 
                                                }
238
 
                                                else
239
 
                                                        skipContact();
 
243
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
 
244
                                                skipContact();
240
245
                                        }
241
246
                                        catch( ContactData.ContactNotIdentifiableException e ) {
242
 
                                                if( !showContinue(
 
247
                                                showContinueOrAbort(
243
248
                                                        getText( R.string.error_vcf_parse ).toString()
244
249
                                                        + fileName +
245
250
                                                        getText( R.string.error_vcf_parse_line ).toString()
246
251
                                                        + vcard_start_line + ":\n" + getText(
247
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
248
 
                                                ) )
249
 
                                                {
250
 
                                                        finish( ACTION_ABORT );
251
 
                                                }
252
 
                                                else
253
 
                                                        skipContact();
 
252
                                                                R.string.error_vcf_notenoughinfo ).toString() );
 
253
                                                skipContact();
 
254
                                        }
 
255
                                        catch( Vcard.SkipImportException e ) {
 
256
                                                skipContact();
254
257
                                        }
255
258
 
256
259
                                        // discard this vcard
264
267
                                        }
265
268
                                        catch( Vcard.ParseException e ) {
266
269
                                                skipContact();
267
 
                                                if( !showContinue(
 
270
                                                showContinueOrAbort(
268
271
                                                        getText( R.string.error_vcf_parse ).toString()
269
272
                                                        + fileName +
270
273
                                                        getText( R.string.error_vcf_parse_line ).toString()
271
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
272
 
                                                {
273
 
                                                        finish( ACTION_ABORT );
274
 
                                                }
 
274
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
275
275
 
276
 
                                                // although we're continuing, we still need to abort
277
 
                                                // this vCard. Further lines will be ignored until we
 
276
                                                // Although we're continuing, we still need to abort
 
277
                                                // this vCard.  Further lines will be ignored until we
278
278
                                                // get to another BEGIN:VCARD line.
279
279
                                                vcard = null;
280
280
                                        }
281
281
                                        catch( Vcard.SkipImportException e ) {
282
282
                                                skipContact();
283
 
                                                // abort this vCard. Further lines will be ignored until
 
283
                                                // Abort this vCard.  Further lines will be ignored until
284
284
                                                // we get to another BEGIN:VCARD line.
285
285
                                                vcard = null;
286
286
                                        }
471
471
                private String extractCollonPartFromLine( ContentLine content_line,
472
472
                        boolean former )
473
473
                {
474
 
                        String ret = null;
475
 
 
476
474
                        // split line into name and value parts and check to make sure we
477
475
                        // only got 2 parts and that the first part is not zero in length
478
476
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
479
477
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
480
 
                                ret = parts[ former? 0 : 1 ];
 
478
                                return parts[ former? 0 : 1 ].trim();
481
479
 
482
 
                        return ret;
 
480
                        return null;
483
481
                }
484
482
 
485
483
                private String extractNameAndParamsFromLine( ContentLine content_line )
486
484
                {
487
 
                        return extractCollonPartFromLine( content_line, true ).trim();
 
485
                        return extractCollonPartFromLine( content_line, true );
488
486
                }
489
487
 
490
488
                private String extractValueFromLine( ContentLine content_line )
508
506
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
509
507
                                {
510
508
                                        // yes, get it!
511
 
                                        String value = extractValueFromLine( content_line ).trim();
512
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
509
                                        String value = extractValueFromLine( content_line );
 
510
                                        if( value == null || (
 
511
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
512
                                        {
513
513
                                                throw new ParseException( R.string.error_vcf_version );
 
514
                                        }
514
515
                                        _version = value;
515
516
 
516
517
                                        // parse any buffers we've been accumulating while we waited
607
608
                                ) );
608
609
                                boolean is_interesting_field =
609
610
                                        interesting_fields.contains(
610
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
611
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
611
612
 
612
613
                                // parse encoding parameter
613
614
                                String encoding = checkParam( name_param_parts, "ENCODING" );
614
615
                                if( encoding != null )
615
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
616
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
616
617
                                if( is_interesting_field && encoding != null &&
617
618
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
618
619
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
624
625
                                // parse charset parameter
625
626
                                String charset = checkParam( name_param_parts, "CHARSET" );
626
627
                                if( charset != null )
627
 
                                        charset = charset.toUpperCase( Locale.US );
 
628
                                        charset = charset.toUpperCase( Locale.ENGLISH );
628
629
                                if( charset != null &&
629
630
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
630
631
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
723
724
                                        parseLABEL( name_param_parts, complete_value );
724
725
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
725
726
                                        parseNOTE( name_param_parts, complete_value );
 
727
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
728
                                        parseBDAY( name_param_parts, complete_value );
726
729
                        }
727
730
                }
728
731
 
752
755
                        {
753
756
                                String str = parts.get( a );
754
757
 
755
 
                                // look for parts that end in an escape character, but ignore
756
 
                                // the final part. We've already detected escape chars at the
 
758
                                // Look for parts that end in an escape character, but ignore
 
759
                                // the final part.  We've already detected escape chars at the
757
760
                                // end of the final part in parseLine() and handled multi-lines
758
761
                                // accordingly.
759
762
                                if( a < parts.size() - 1 &&
1029
1032
                        addNote( unescapeValue( value ) );
1030
1033
                }
1031
1034
 
 
1035
                private void parseBDAY( String[] params, String value )
 
1036
                {
 
1037
                        setBirthday( value );
 
1038
                }
 
1039
 
1032
1040
                public void finaliseVcard()
1033
 
                        throws ParseException, ContactNotIdentifiableException
 
1041
                        throws ParseException, ContactNotIdentifiableException,
 
1042
                                SkipImportException, AbortImportException
1034
1043
                {
1035
 
                        // missing version (and data is present)
1036
 
                        if( _version == null && _content_lines != null )
1037
 
                                throw new ParseException( R.string.error_vcf_malformed );
 
1044
                        // if there was content present, but no version line, then it must
 
1045
                        // be a version 2.1 vCard; process that content now
 
1046
                        if( _version == null && _content_lines != null ) {
 
1047
                                _version = "2.1";
 
1048
                                for( int i = 0; i < _content_lines.size(); i++ )
 
1049
                                        parseLine( _content_lines.get( i ) );
 
1050
                                _content_lines = null;
 
1051
                        }
1038
1052
 
1039
1053
                        // finalise the parent class
1040
1054
                        finalise();
1042
1056
 
1043
1057
                /**
1044
1058
                 * Amongst the params, find the value of the first, only, of any with
1045
 
                 * the specified name
 
1059
                 * the specified name.
 
1060
                 *
1046
1061
                 * @param params
1047
1062
                 * @param name
1048
1063
                 * @return a value, or null
1054
1069
                }
1055
1070
 
1056
1071
                /**
1057
 
                 * Amongst the params, find the values of any with the specified name
 
1072
                 * Amongst the params, find the values of any with the specified name.
 
1073
                 *
1058
1074
                 * @param params
1059
1075
                 * @param name
1060
1076
                 * @return an array of values, or null
1076
1092
                }
1077
1093
 
1078
1094
                /**
1079
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1080
 
                 * those types are just parameters. For v3.0, they are prefixed with
1081
 
                 * "TYPE=". There may also be multiple type parameters.
 
1095
                 * Amongst the params, return any type values present.  For v2.1 vCards,
 
1096
                 * those types are just parameters.  For v3.0, they are prefixed with
 
1097
                 * "TYPE=".  There may also be multiple type parameters.
 
1098
                 *
1082
1099
                 * @param params an array of params to look for types in
1083
1100
                 * @param valid_types an list of upper-case type values to look for
1084
1101
                 * @return a set of present type values
1096
1113
                                // this is in the specs!)
1097
1114
                                String[] parts = type_params[ a ].split( "," );
1098
1115
                                for( int i = 0; i < parts.length; i++ ) {
1099
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1100
1117
                                        if( valid_types.contains( ucpart ) )
1101
1118
                                                types.add( ucpart );
1102
1119
                                }
1105
1122
                        // get 2.1-style type param
1106
1123
                        if( _version.equals( "2.1" ) ) {
1107
1124
                                for( int i = 1; i < params.length; i++ ) {
1108
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1125
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1109
1126
                                        if( valid_types.contains( ucparam ) )
1110
1127
                                                types.add( ucparam );
1111
1128
                                }
1136
1153
                                else if( ch == '=' && i == in.limit() - 1 )
1137
1154
                                {
1138
1155
                                        // we found a '=' at the end of a line signifying a multi-
1139
 
                                        // line string, so we don't add it.
 
1156
                                        // line string, so we don't add it
1140
1157
                                        another = true;
1141
1158
                                        continue;
1142
1159
                                }