/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() );
133
141
                        boolean in_vcard = false;
134
142
                        while( ( line = reader.readLine() ) != null )
135
143
                        {
136
 
                                if( !in_vcard ) {
 
144
                                if( !in_vcard )
 
145
                                {
137
146
                                        // look for vcard beginning
138
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
147
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
139
148
                                                in_vcard = true;
140
149
                                                _vcard_count++;
141
150
                                        }
 
151
                                        // check for vMsg files
 
152
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
 
153
                                                showError( getText( R.string.error_vcf_vmsgfile )
 
154
                                                        + file.getName() );
 
155
                                        }
142
156
                                }
143
 
                                else if( line.matches( "^END:VCARD" ) )
 
157
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
144
158
                                        in_vcard = false;
145
159
                        }
 
160
                        reader.close();
146
161
 
147
162
                }
148
163
                catch( FileNotFoundException e ) {
170
185
                        FileInputStream istream = new FileInputStream( file );
171
186
                        byte[] content = new byte[ (int)file.length() ];
172
187
                        istream.read( content );
173
 
                        istream = null;
 
188
                        istream.close();
174
189
 
175
190
                        // import
176
191
                        importVCardFileContent( content, file.getName() );
203
218
 
204
219
                        if( vcard == null ) {
205
220
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
 
221
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
207
222
                                        setProgress( _progress++ );
208
223
                                        vcard = new Vcard();
209
224
                                        vcard_start_line = cli.getLineNumber();
211
226
                        }
212
227
                        else {
213
228
                                // look for vcard content or ending
214
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
 
229
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
215
230
                                {
216
231
                                        // finalise the vcard/contact
217
232
                                        try {
221
236
                                                importContact( vcard );
222
237
                                        }
223
238
                                        catch( Vcard.ParseException e ) {
224
 
                                                if( !showContinue(
 
239
                                                showContinueOrAbort(
225
240
                                                        getText( R.string.error_vcf_parse ).toString()
226
241
                                                        + fileName +
227
242
                                                        getText( R.string.error_vcf_parse_line ).toString()
228
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
229
 
                                                {
230
 
                                                        finish( ACTION_ABORT );
231
 
                                                }
232
 
                                                else
233
 
                                                        skipContact();
 
243
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
 
244
                                                skipContact();
234
245
                                        }
235
246
                                        catch( ContactData.ContactNotIdentifiableException e ) {
236
 
                                                if( !showContinue(
 
247
                                                showContinueOrAbort(
237
248
                                                        getText( R.string.error_vcf_parse ).toString()
238
249
                                                        + fileName +
239
250
                                                        getText( R.string.error_vcf_parse_line ).toString()
240
251
                                                        + vcard_start_line + ":\n" + getText(
241
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
242
 
                                                ) )
243
 
                                                {
244
 
                                                        finish( ACTION_ABORT );
245
 
                                                }
246
 
                                                else
247
 
                                                        skipContact();
 
252
                                                                R.string.error_vcf_notenoughinfo ).toString() );
 
253
                                                skipContact();
 
254
                                        }
 
255
                                        catch( Vcard.SkipImportException e ) {
 
256
                                                skipContact();
248
257
                                        }
249
258
 
250
259
                                        // discard this vcard
258
267
                                        }
259
268
                                        catch( Vcard.ParseException e ) {
260
269
                                                skipContact();
261
 
                                                if( !showContinue(
 
270
                                                showContinueOrAbort(
262
271
                                                        getText( R.string.error_vcf_parse ).toString()
263
272
                                                        + fileName +
264
273
                                                        getText( R.string.error_vcf_parse_line ).toString()
265
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
266
 
                                                {
267
 
                                                        finish( ACTION_ABORT );
268
 
                                                }
 
274
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
269
275
 
270
 
                                                // although we're continuing, we still need to abort
271
 
                                                // 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
272
278
                                                // get to another BEGIN:VCARD line.
273
279
                                                vcard = null;
274
280
                                        }
275
281
                                        catch( Vcard.SkipImportException e ) {
276
282
                                                skipContact();
277
 
                                                // abort this vCard. Further lines will be ignored until
 
283
                                                // Abort this vCard.  Further lines will be ignored until
278
284
                                                // we get to another BEGIN:VCARD line.
279
285
                                                vcard = null;
280
286
                                        }
465
471
                private String extractCollonPartFromLine( ContentLine content_line,
466
472
                        boolean former )
467
473
                {
468
 
                        String ret = null;
469
 
 
470
474
                        // split line into name and value parts and check to make sure we
471
475
                        // only got 2 parts and that the first part is not zero in length
472
476
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
473
477
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
474
 
                                ret = parts[ former? 0 : 1 ];
 
478
                                return parts[ former? 0 : 1 ].trim();
475
479
 
476
 
                        return ret;
 
480
                        return null;
477
481
                }
478
482
 
479
483
                private String extractNameAndParamsFromLine( ContentLine content_line )
503
507
                                {
504
508
                                        // yes, get it!
505
509
                                        String value = extractValueFromLine( content_line );
506
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
510
                                        if( value == null || (
 
511
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
512
                                        {
507
513
                                                throw new ParseException( R.string.error_vcf_version );
 
514
                                        }
508
515
                                        _version = value;
509
516
 
510
517
                                        // parse any buffers we've been accumulating while we waited
601
608
                                ) );
602
609
                                boolean is_interesting_field =
603
610
                                        interesting_fields.contains(
604
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
611
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
605
612
 
606
613
                                // parse encoding parameter
607
614
                                String encoding = checkParam( name_param_parts, "ENCODING" );
608
615
                                if( encoding != null )
609
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
616
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
610
617
                                if( is_interesting_field && encoding != null &&
611
618
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
612
619
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
618
625
                                // parse charset parameter
619
626
                                String charset = checkParam( name_param_parts, "CHARSET" );
620
627
                                if( charset != null )
621
 
                                        charset = charset.toUpperCase( Locale.US );
 
628
                                        charset = charset.toUpperCase( Locale.ENGLISH );
622
629
                                if( charset != null &&
623
630
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
624
631
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
717
724
                                        parseLABEL( name_param_parts, complete_value );
718
725
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
719
726
                                        parseNOTE( name_param_parts, complete_value );
 
727
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
728
                                        parseBDAY( name_param_parts, complete_value );
720
729
                        }
721
730
                }
722
731
 
746
755
                        {
747
756
                                String str = parts.get( a );
748
757
 
749
 
                                // look for parts that end in an escape character, but ignore
750
 
                                // 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
751
760
                                // end of the final part in parseLine() and handled multi-lines
752
761
                                // accordingly.
753
762
                                if( a < parts.size() - 1 &&
1023
1032
                        addNote( unescapeValue( value ) );
1024
1033
                }
1025
1034
 
 
1035
                private void parseBDAY( String[] params, String value )
 
1036
                {
 
1037
                        setBirthday( value );
 
1038
                }
 
1039
 
1026
1040
                public void finaliseVcard()
1027
 
                        throws ParseException, ContactNotIdentifiableException
 
1041
                        throws ParseException, ContactNotIdentifiableException,
 
1042
                                SkipImportException, AbortImportException
1028
1043
                {
1029
 
                        // missing version (and data is present)
1030
 
                        if( _version == null && _content_lines != null )
1031
 
                                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
                        }
1032
1052
 
1033
1053
                        // finalise the parent class
1034
1054
                        finalise();
1036
1056
 
1037
1057
                /**
1038
1058
                 * Amongst the params, find the value of the first, only, of any with
1039
 
                 * the specified name
 
1059
                 * the specified name.
 
1060
                 *
1040
1061
                 * @param params
1041
1062
                 * @param name
1042
1063
                 * @return a value, or null
1048
1069
                }
1049
1070
 
1050
1071
                /**
1051
 
                 * 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
                 *
1052
1074
                 * @param params
1053
1075
                 * @param name
1054
1076
                 * @return an array of values, or null
1070
1092
                }
1071
1093
 
1072
1094
                /**
1073
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1074
 
                 * those types are just parameters. For v3.0, they are prefixed with
1075
 
                 * "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
                 *
1076
1099
                 * @param params an array of params to look for types in
1077
1100
                 * @param valid_types an list of upper-case type values to look for
1078
1101
                 * @return a set of present type values
1090
1113
                                // this is in the specs!)
1091
1114
                                String[] parts = type_params[ a ].split( "," );
1092
1115
                                for( int i = 0; i < parts.length; i++ ) {
1093
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1094
1117
                                        if( valid_types.contains( ucpart ) )
1095
1118
                                                types.add( ucpart );
1096
1119
                                }
1099
1122
                        // get 2.1-style type param
1100
1123
                        if( _version.equals( "2.1" ) ) {
1101
1124
                                for( int i = 1; i < params.length; i++ ) {
1102
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1125
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1103
1126
                                        if( valid_types.contains( ucparam ) )
1104
1127
                                                types.add( ucparam );
1105
1128
                                }
1130
1153
                                else if( ch == '=' && i == in.limit() - 1 )
1131
1154
                                {
1132
1155
                                        // we found a '=' at the end of a line signifying a multi-
1133
 
                                        // line string, so we don't add it.
 
1156
                                        // line string, so we don't add it
1134
1157
                                        another = true;
1135
1158
                                        continue;
1136
1159
                                }