/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: 2013-07-19 15:57:39 UTC
  • Revision ID: tim@ed.am-20130719155739-50w182nof760psos
Tags: 1.3.3
bump version no. to 1.3.3

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 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
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
7
 * to as "this program").  For more information, see
 
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
 
public class VCFImporter extends Importer
 
51
public class VcardImporter extends Importer
52
52
{
53
53
        private int _vcard_count = 0;
54
54
        private int _progress = 0;
55
55
 
56
 
        public VCFImporter( Doit doit )
 
56
        public VcardImporter( Doit doit )
57
57
        {
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() );
116
117
                setProgress( 0 );
117
118
                for( int i = 0; i < files.length; i++ )
118
119
                        importVCardFile( files[ i ] );
 
120
                setProgress( _vcard_count );
119
121
        }
120
122
 
121
123
        private void countVCardFile( File file ) throws AbortImportException
131
133
                        boolean in_vcard = false;
132
134
                        while( ( line = reader.readLine() ) != null )
133
135
                        {
134
 
                                if( !in_vcard ) {
 
136
                                if( !in_vcard )
 
137
                                {
135
138
                                        // look for vcard beginning
136
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
139
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
137
140
                                                in_vcard = true;
138
141
                                                _vcard_count++;
139
142
                                        }
 
143
                                        // check for vMsg files
 
144
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
 
145
                                                showError( getText( R.string.error_vcf_vmsgfile )
 
146
                                                        + file.getName() );
 
147
                                        }
140
148
                                }
141
 
                                else if( line.matches( "^END:VCARD" ) )
 
149
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
142
150
                                        in_vcard = false;
143
151
                        }
144
152
 
168
176
                        FileInputStream istream = new FileInputStream( file );
169
177
                        byte[] content = new byte[ (int)file.length() ];
170
178
                        istream.read( content );
 
179
                        istream = null;
171
180
 
172
181
                        // import
173
182
                        importVCardFileContent( content, file.getName() );
174
183
                }
 
184
                catch( OutOfMemoryError e ) {
 
185
                        showError( R.string.error_outofmemory );
 
186
                }
175
187
                catch( FileNotFoundException e ) {
176
188
                        showError( getText( R.string.error_filenotfound ) +
177
189
                                file.getName() );
185
197
                throws AbortImportException
186
198
        {
187
199
                // go through lines
188
 
                VCard vcard = null;
 
200
                Vcard vcard = null;
 
201
                int vcard_start_line = 0;
189
202
                ContentLineIterator cli = new ContentLineIterator( content );
190
203
                while( cli.hasNext() )
191
204
                {
192
 
                        ByteBuffer buffer = cli.next();
 
205
                        ContentLine content_line = cli.next();
193
206
 
194
 
                        // get a US-ASCII version of the line for processing
195
 
                        String line;
196
 
                        try {
197
 
                                line = new String( buffer.array(), buffer.position(),
198
 
                                        buffer.limit() - buffer.position(), "US-ASCII" );
199
 
                        }
200
 
                        catch( UnsupportedEncodingException e ) {
201
 
                                // we know US-ASCII is supported, so appease the compiler...
202
 
                                line = "";
203
 
                        }
 
207
                        // get a US-ASCII version of the string, for processing
 
208
                        String line = content_line.getUsAsciiLine();
204
209
 
205
210
                        if( vcard == null ) {
206
211
                                // look for vcard beginning
207
 
                                if( line.matches( "^BEGIN:VCARD" ) ) {
208
 
                                        setProgress( ++_progress );
209
 
                                        vcard = new VCard();
 
212
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
 
213
                                        setProgress( _progress++ );
 
214
                                        vcard = new Vcard();
 
215
                                        vcard_start_line = cli.getLineNumber();
210
216
                                }
211
217
                        }
212
218
                        else {
213
219
                                // look for vcard content or ending
214
 
                                if( line.matches( "^END:VCARD" ) )
 
220
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
215
221
                                {
216
 
                                        // store vcard and do away with it
 
222
                                        // finalise the vcard/contact
217
223
                                        try {
218
 
                                                vcard.finaliseParsing();
 
224
                                                vcard.finaliseVcard();
 
225
 
 
226
                                                // pass the finalised contact to the importer
219
227
                                                importContact( vcard );
220
228
                                        }
221
 
                                        catch( VCard.ParseException e ) {
222
 
                                                skipContact();
223
 
                                                if( !showContinue(
224
 
                                                        getText( R.string.error_vcf_parse ).toString()
225
 
                                                        + fileName + "\n" + e.getMessage() ) )
226
 
                                                {
227
 
                                                        finish( ACTION_ABORT );
228
 
                                                }
229
 
                                        }
230
 
                                        catch( VCard.SkipContactException e ) {
231
 
                                                skipContact();
232
 
                                                // do nothing
233
 
                                        }
 
229
                                        catch( Vcard.ParseException e ) {
 
230
                                                if( !showContinue(
 
231
                                                        getText( R.string.error_vcf_parse ).toString()
 
232
                                                        + fileName +
 
233
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
234
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
235
                                                {
 
236
                                                        finish( ACTION_ABORT );
 
237
                                                }
 
238
                                                else
 
239
                                                        skipContact();
 
240
                                        }
 
241
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
242
                                                if( !showContinue(
 
243
                                                        getText( R.string.error_vcf_parse ).toString()
 
244
                                                        + fileName +
 
245
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
246
                                                        + vcard_start_line + ":\n" + getText(
 
247
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
248
                                                ) )
 
249
                                                {
 
250
                                                        finish( ACTION_ABORT );
 
251
                                                }
 
252
                                                else
 
253
                                                        skipContact();
 
254
                                        }
 
255
 
 
256
                                        // discard this vcard
234
257
                                        vcard = null;
235
258
                                }
236
259
                                else
237
260
                                {
238
261
                                        // try giving the line to the vcard
239
262
                                        try {
240
 
                                                vcard.parseLine( buffer, line,
241
 
                                                        cli.doesNextLineLookFolded() );
 
263
                                                vcard.parseLine( content_line );
242
264
                                        }
243
 
                                        catch( VCard.ParseException e ) {
 
265
                                        catch( Vcard.ParseException e ) {
244
266
                                                skipContact();
245
267
                                                if( !showContinue(
246
268
                                                        getText( R.string.error_vcf_parse ).toString()
247
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
269
                                                        + fileName +
 
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
248
272
                                                {
249
273
                                                        finish( ACTION_ABORT );
250
274
                                                }
251
275
 
252
 
                                                // although we're continuing, we still need to abort
253
 
                                                // 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
254
278
                                                // get to another BEGIN:VCARD line.
255
279
                                                vcard = null;
256
280
                                        }
257
 
                                        catch( VCard.SkipContactException e ) {
 
281
                                        catch( Vcard.SkipImportException e ) {
258
282
                                                skipContact();
259
 
                                                // abort this vCard. Further lines will be ignored until
 
283
                                                // Abort this vCard.  Further lines will be ignored until
260
284
                                                // we get to another BEGIN:VCARD line.
261
285
                                                vcard = null;
262
286
                                        }
265
289
                }
266
290
        }
267
291
 
268
 
        class ContentLineIterator implements Iterator< ByteBuffer >
 
292
        class ContentLine
 
293
        {
 
294
                private ByteBuffer _buffer;
 
295
                private boolean _folded_next;
 
296
                private String _line;
 
297
 
 
298
                public ContentLine( ByteBuffer buffer, boolean folded_next )
 
299
                {
 
300
                        _buffer = buffer;
 
301
                        _folded_next = folded_next;
 
302
                        _line = null;
 
303
                }
 
304
 
 
305
                public ByteBuffer getBuffer()
 
306
                {
 
307
                        return _buffer;
 
308
                }
 
309
 
 
310
                public boolean doesNextLineLookFolded()
 
311
                {
 
312
                        return _folded_next;
 
313
                }
 
314
 
 
315
                public String getUsAsciiLine()
 
316
                {
 
317
                        // generated line and cache it
 
318
                        if( _line == null ) {
 
319
                                try {
 
320
                                        _line = new String( _buffer.array(), _buffer.position(),
 
321
                                                _buffer.limit() - _buffer.position(), "US-ASCII" );
 
322
                                }
 
323
                                catch( UnsupportedEncodingException e ) {
 
324
                                        // we know US-ASCII *is* supported, so appease the
 
325
                                        // compiler...
 
326
                                }
 
327
                        }
 
328
 
 
329
                        // return cached line
 
330
                        return _line;
 
331
                }
 
332
        }
 
333
 
 
334
        class ContentLineIterator implements Iterator< ContentLine >
269
335
        {
270
336
                protected byte[] _content = null;
271
337
                protected int _pos = 0;
 
338
                protected int _line = 0;
272
339
 
273
340
                public ContentLineIterator( byte[] content )
274
341
                {
282
349
                }
283
350
 
284
351
                @Override
285
 
                public ByteBuffer next()
 
352
                public ContentLine next()
286
353
                {
287
354
                        int initial_pos = _pos;
288
355
 
294
361
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
295
362
                                                _pos > initial_pos )? _pos - 1 : _pos;
296
363
                                        _pos++;
297
 
                                        return ByteBuffer.wrap( _content, initial_pos,
298
 
                                                to - initial_pos );
 
364
                                        _line++;
 
365
                                        return new ContentLine(
 
366
                                                ByteBuffer.wrap( _content, initial_pos,
 
367
                                                        to - initial_pos ),
 
368
                                                doesNextLineLookFolded() );
299
369
                                }
300
370
 
301
371
                        // we didn't find one, but were there bytes left?
302
372
                        if( _pos != initial_pos ) {
303
373
                                int to = _pos;
304
374
                                _pos++;
305
 
                                return ByteBuffer.wrap( _content, initial_pos,
306
 
                                        to - initial_pos );
 
375
                                _line++;
 
376
                                return new ContentLine(
 
377
                                        ByteBuffer.wrap( _content, initial_pos,
 
378
                                                to - initial_pos ),
 
379
                                        doesNextLineLookFolded() );
307
380
                        }
308
381
 
309
382
                        // no bytes left
321
394
                 * onto the end of this one?
322
395
                 * @return
323
396
                 */
324
 
                public boolean doesNextLineLookFolded()
 
397
                private boolean doesNextLineLookFolded()
325
398
                {
326
399
                        return _pos > 0 && _pos < _content.length &&
327
 
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
 
400
                                _content[ _pos - 1 ] == '\n' &&
 
401
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
 
402
                }
 
403
 
 
404
                public int getLineNumber()
 
405
                {
 
406
                        return _line;
328
407
                }
329
408
        }
330
409
 
331
 
        private class VCard extends ContactData
 
410
        private class Vcard extends ContactData
332
411
        {
333
412
                private final static int NAMELEVEL_NONE = 0;
334
 
                private final static int NAMELEVEL_FN = 1;
335
 
                private final static int NAMELEVEL_N = 2;
 
413
                private final static int NAMELEVEL_N = 1;
 
414
                private final static int NAMELEVEL_FN = 2;
336
415
 
337
416
                private final static int MULTILINE_NONE = 0;
338
417
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
339
418
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
340
 
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
 
419
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
341
420
 
342
421
                private String _version = null;
343
 
                private Vector< ByteBuffer > _buffers = null;
 
422
                private Vector< ContentLine > _content_lines = null;
344
423
                private int _name_level = NAMELEVEL_NONE;
345
424
                private int _parser_multiline_state = MULTILINE_NONE;
346
425
                private String _parser_current_name_and_params = null;
382
461
 
383
462
                        public ParseException( int res )
384
463
                        {
385
 
                                super( VCFImporter.this.getText( res ).toString() );
 
464
                                super( VcardImporter.this.getText( res ).toString() );
386
465
                        }
387
466
                }
388
467
 
389
468
                @SuppressWarnings("serial")
390
 
                protected class SkipContactException extends Exception { }
 
469
                protected class SkipImportException extends Exception { }
391
470
 
392
 
                private String extractCollonPartFromLine( ByteBuffer buffer,
393
 
                        String line, boolean former )
 
471
                private String extractCollonPartFromLine( ContentLine content_line,
 
472
                        boolean former )
394
473
                {
395
474
                        String ret = null;
396
475
 
397
 
                        // get a US-ASCII version of the line for processing, unless we were
398
 
                        // supplied with one
399
 
                        if( line == null ) {
400
 
                                try {
401
 
                                        line = new String( buffer.array(), buffer.position(),
402
 
                                                buffer.limit() - buffer.position(), "US-ASCII" );
403
 
                                }
404
 
                                catch( UnsupportedEncodingException e ) {
405
 
                                        // we know US-ASCII is supported, so appease the compiler...
406
 
                                        line = "";
407
 
                                }
408
 
                        }
409
 
 
410
476
                        // split line into name and value parts and check to make sure we
411
477
                        // only got 2 parts and that the first part is not zero in length
412
 
                        String[] parts = line.split( ":", 2 );
 
478
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
413
479
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
414
480
                                ret = parts[ former? 0 : 1 ];
415
481
 
416
482
                        return ret;
417
483
                }
418
484
 
419
 
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
420
 
                        String line )
421
 
                {
422
 
                        return extractCollonPartFromLine( buffer, line, true );
423
 
                }
424
 
 
425
 
                private String extractValueFromLine( ByteBuffer buffer, String line )
426
 
                {
427
 
                        return extractCollonPartFromLine( buffer, line, false );
428
 
                }
429
 
 
430
 
                public void parseLine( ByteBuffer buffer, String line,
431
 
                        boolean next_line_looks_folded )
432
 
                        throws ParseException, SkipContactException,
 
485
                private String extractNameAndParamsFromLine( ContentLine content_line )
 
486
                {
 
487
                        return extractCollonPartFromLine( content_line, true ).trim();
 
488
                }
 
489
 
 
490
                private String extractValueFromLine( ContentLine content_line )
 
491
                {
 
492
                        return extractCollonPartFromLine( content_line, false );
 
493
                }
 
494
 
 
495
                public void parseLine( ContentLine content_line )
 
496
                        throws ParseException, SkipImportException,
433
497
                        AbortImportException
434
498
                {
435
499
                        // do we have a version yet?
437
501
                        {
438
502
                                // tentatively get name and params from line
439
503
                                String name_and_params =
440
 
                                        extractNameAndParamsFromLine( buffer, line );
 
504
                                        extractNameAndParamsFromLine( content_line );
441
505
 
442
506
                                // is it a version line?
443
507
                                if( name_and_params != null &&
444
 
                                        name_and_params.equals( "VERSION" ) )
 
508
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
445
509
                                {
446
510
                                        // yes, get it!
447
 
                                        String value = extractValueFromLine( buffer, line );
 
511
                                        String value = extractValueFromLine( content_line ).trim();
448
512
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
449
513
                                                throw new ParseException( R.string.error_vcf_version );
450
514
                                        _version = value;
451
515
 
452
516
                                        // parse any buffers we've been accumulating while we waited
453
517
                                        // for a version
454
 
                                        if( _buffers != null )
455
 
                                                for( int i = 0; i < _buffers.size(); i++ )
456
 
                                                        parseLine( _buffers.get( i ), null,
457
 
                                                                i + 1 < _buffers.size() &&
458
 
                                                                _buffers.get( i + 1 ).hasRemaining() &&
459
 
                                                                _buffers.get( i + 1 ).get(
460
 
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
461
 
                                        _buffers = null;
 
518
                                        if( _content_lines != null )
 
519
                                                for( int i = 0; i < _content_lines.size(); i++ )
 
520
                                                        parseLine( _content_lines.get( i ) );
 
521
                                        _content_lines = null;
462
522
                                }
463
523
                                else
464
524
                                {
465
525
                                        // no, so stash this line till we get a version
466
 
                                        if( _buffers == null )
467
 
                                                _buffers = new Vector< ByteBuffer >();
468
 
                                        _buffers.add( buffer );
 
526
                                        if( _content_lines == null )
 
527
                                                _content_lines = new Vector< ContentLine >();
 
528
                                        _content_lines.add( content_line );
469
529
                                }
470
530
                        }
471
531
                        else
472
532
                        {
473
533
                                // name and params and the position in the buffer where the
474
 
                                // "value" part of the line start
 
534
                                // "value" part of the line starts
475
535
                                String name_and_params;
476
536
                                int pos;
477
537
 
483
543
 
484
544
                                        // skip some initial line characters, depending on the type
485
545
                                        // of multi-line we're handling
486
 
                                        pos = buffer.position();
 
546
                                        pos = content_line.getBuffer().position();
487
547
                                        switch( _parser_multiline_state )
488
548
                                        {
489
549
                                        case MULTILINE_FOLDED:
490
550
                                                pos++;
491
551
                                                break;
492
552
                                        case MULTILINE_ENCODED:
493
 
                                                while( pos < buffer.limit() && (
494
 
                                                        buffer.get( pos ) == ' ' ||
495
 
                                                        buffer.get( pos ) == '\t' ) )
 
553
                                                while( pos < content_line.getBuffer().limit() && (
 
554
                                                        content_line.getBuffer().get( pos ) == ' ' ||
 
555
                                                        content_line.getBuffer().get( pos ) == '\t' ) )
496
556
                                                {
497
557
                                                        pos++;
498
558
                                                }
507
567
                                }
508
568
                                else
509
569
                                {
 
570
                                        // skip empty lines
 
571
                                        if( content_line.getUsAsciiLine().trim().length() == 0 )
 
572
                                                return;
 
573
 
510
574
                                        // get name and params from line, and since we're not
511
575
                                        // parsing a subsequent line in a multi-line, this should
512
576
                                        // not fail, or it's an error
513
577
                                        name_and_params =
514
 
                                                extractNameAndParamsFromLine( buffer, line );
 
578
                                                extractNameAndParamsFromLine( content_line );
515
579
                                        if( name_and_params == null )
516
580
                                                throw new ParseException(
517
581
                                                        R.string.error_vcf_malformed );
518
582
 
519
583
                                        // calculate how many chars to skip from beginning of line
520
584
                                        // so we skip the property "name:" part
521
 
                                        pos = buffer.position() + name_and_params.length() + 1;
 
585
                                        pos = content_line.getBuffer().position() +
 
586
                                                name_and_params.length() + 1;
522
587
 
523
588
                                        // reset the saved multi-line state
524
589
                                        _parser_current_name_and_params = name_and_params;
527
592
 
528
593
                                // get value from buffer, as raw bytes
529
594
                                ByteBuffer value;
530
 
                                value = ByteBuffer.wrap( buffer.array(), pos,
531
 
                                        buffer.limit() - pos );
 
595
                                value = ByteBuffer.wrap( content_line.getBuffer().array(), pos,
 
596
                                        content_line.getBuffer().limit() - pos );
532
597
 
533
598
                                // get parameter parts
534
599
                                String[] name_param_parts = name_and_params.split( ";", -1 );
535
600
                                for( int i = 0; i < name_param_parts.length; i++ )
536
601
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
537
602
 
 
603
                                // determine whether we care about this entry
 
604
                                final HashSet< String > interesting_fields =
 
605
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
606
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
607
                                ) );
 
608
                                boolean is_interesting_field =
 
609
                                        interesting_fields.contains(
 
610
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
611
 
538
612
                                // parse encoding parameter
539
613
                                String encoding = checkParam( name_param_parts, "ENCODING" );
540
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
541
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
542
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
543
 
                                        //&& !encoding.equals( "BASE64" ) )
 
614
                                if( encoding != null )
 
615
                                        encoding = encoding.toUpperCase( Locale.US );
 
616
                                if( is_interesting_field && encoding != null &&
 
617
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
 
618
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
619
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
544
620
                                {
545
621
                                        throw new ParseException( R.string.error_vcf_encoding );
546
622
                                }
547
623
 
548
624
                                // parse charset parameter
549
625
                                String charset = checkParam( name_param_parts, "CHARSET" );
550
 
                                if( charset != null ) charset = charset.toUpperCase();
551
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
552
 
                                        !charset.equals( "ASCII" ) &&
553
 
                                        !charset.equals( "UTF-8" ) )
 
626
                                if( charset != null )
 
627
                                        charset = charset.toUpperCase( Locale.US );
 
628
                                if( charset != null &&
 
629
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
 
630
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
 
631
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
554
632
                                {
555
633
                                        throw new ParseException( R.string.error_vcf_charset );
556
634
                                }
558
636
                                // do unencoding (or default to a fake unencoding result with
559
637
                                // the raw string)
560
638
                                UnencodeResult unencoding_result = null;
561
 
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
639
                                if( encoding != null &&
 
640
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
641
                                {
562
642
                                        unencoding_result = unencodeQuotedPrintable( value );
563
 
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
 
643
                                }
 
644
//                              else if( encoding != null &&
 
645
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
 
646
//                              {
564
647
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
 
648
//                              }
565
649
                                if( unencoding_result != null ) {
566
650
                                        value = unencoding_result.getBuffer();
567
651
                                        if( unencoding_result.isAnotherLineRequired() )
568
652
                                                _parser_multiline_state = MULTILINE_ENCODED;
569
653
                                }
570
654
 
571
 
                                // convert 8-bit ASCII charset to US-ASCII
572
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
655
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
656
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
657
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
658
                                        ( charset != null && (
 
659
                                                charset.equalsIgnoreCase( "ASCII" ) ||
 
660
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
 
661
                                {
573
662
                                        value = transcodeAsciiToUtf8( value );
574
 
                                        charset = "UTF-8";
575
663
                                }
576
664
 
577
 
                                // process charset
 
665
                                // process charset (value is now in UTF-8)
578
666
                                String string_value;
579
667
                                try {
580
668
                                        string_value = new String( value.array(), value.position(),
581
 
                                                value.limit() - value.position(), charset );
 
669
                                                value.limit() - value.position(), "UTF-8" );
582
670
                                } catch( UnsupportedEncodingException e ) {
583
671
                                        throw new ParseException( R.string.error_vcf_charset );
584
672
                                }
586
674
                                // for some entries that have semicolon-separated value parts,
587
675
                                // check to see if the value ends in an escape character, which
588
676
                                // indicates that we have a multi-line value
589
 
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
590
 
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
591
 
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
 
677
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
 
678
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
 
679
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
592
680
                                        doesStringEndInAnEscapeChar( string_value ) )
593
681
                                {
594
682
                                        _parser_multiline_state = MULTILINE_ESCAPED;
596
684
                                                string_value.length() - 1 );
597
685
                                }
598
686
 
599
 
                                // now we know whether we're in an encoding multi-line,
600
 
                                // determine if we're in a v3 folded multi-line or not
 
687
                                // if we know we're not in an encoding-based multi-line, check
 
688
                                // to see if we're in a folded multi-line
601
689
                                if( _parser_multiline_state == MULTILINE_NONE &&
602
 
                                        _version.equals( "3.0" ) && next_line_looks_folded )
 
690
                                        content_line.doesNextLineLookFolded() )
603
691
                                {
604
692
                                        _parser_multiline_state = MULTILINE_FOLDED;
605
693
                                }
617
705
                                if( complete_value.length() < 1 ) return;
618
706
 
619
707
                                // parse some properties
620
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
 
708
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
621
709
                                        parseN( name_param_parts, complete_value );
622
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
 
710
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
623
711
                                        parseFN( name_param_parts, complete_value );
624
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
 
712
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
625
713
                                        parseORG( name_param_parts, complete_value );
626
 
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
714
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
627
715
                                        parseTITLE( name_param_parts, complete_value );
628
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
 
716
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
629
717
                                        parseTEL( name_param_parts, complete_value );
630
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
 
718
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
631
719
                                        parseEMAIL( name_param_parts, complete_value );
632
 
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
 
720
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
633
721
                                        parseADR( name_param_parts, complete_value );
 
722
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
 
723
                                        parseLABEL( name_param_parts, complete_value );
 
724
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
 
725
                                        parseNOTE( name_param_parts, complete_value );
 
726
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
727
                                        parseBDAY( name_param_parts, complete_value );
634
728
                        }
635
729
                }
636
730
 
649
743
                        return ( count & 1 ) == 1;
650
744
                }
651
745
 
652
 
                private String[] splitValueBySemicolon( String value )
 
746
                private String[] splitValueByCharacter( String value, char character )
653
747
                {
654
 
                        // split string in to parts by semicolon
 
748
                        // split string in to parts by specified character
655
749
                        ArrayList< String > parts = new ArrayList< String >(
656
 
                                Arrays.asList( value.split(  ";" ) ) );
 
750
                                Arrays.asList( value.split( "" + character ) ) );
657
751
 
658
752
                        // go through parts
659
753
                        for( int a = 0; a < parts.size(); a++ )
660
754
                        {
661
755
                                String str = parts.get( a );
662
756
 
663
 
                                // look for parts that end in an escape character, but ignore
664
 
                                // the final part. We've already detected escape chars at the
 
757
                                // Look for parts that end in an escape character, but ignore
 
758
                                // the final part.  We've already detected escape chars at the
665
759
                                // end of the final part in parseLine() and handled multi-lines
666
760
                                // accordingly.
667
761
                                if( a < parts.size() - 1 &&
668
762
                                        doesStringEndInAnEscapeChar( str ) )
669
763
                                {
670
 
                                        // join the next part to this part and remove the next part
 
764
                                        // append the escaped character, join the next part to this
 
765
                                        // part and remove the next part
671
766
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
672
 
                                                ';' + parts.get( a + 1 ) );
 
767
                                                character + parts.get( a + 1 ) );
673
768
                                        parts.remove( a + 1 );
674
769
 
675
770
                                        // re-visit this part
686
781
                        return parts.toArray( ret );
687
782
                }
688
783
 
 
784
                private String unescapeValue( String value )
 
785
                {
 
786
                        StringBuilder ret = new StringBuilder( value.length() );
 
787
                        boolean in_escape = false;
 
788
                        for( int a = 0; a < value.length(); a++ )
 
789
                        {
 
790
                                int c = value.codePointAt( a );
 
791
 
 
792
                                // process a normal character
 
793
                                if( !in_escape ) {
 
794
                                        if( c == '\\' )
 
795
                                                in_escape = true;
 
796
                                        else
 
797
                                                ret.append( Character.toChars( c ) );
 
798
                                        continue;
 
799
                                }
 
800
 
 
801
                                // process an escape sequence
 
802
                                in_escape = false;
 
803
                                switch( c )
 
804
                                {
 
805
                                case 'T':
 
806
                                case 't':
 
807
                                        // add tab (invalid/non-standard, but accepted)
 
808
                                        ret.append( '\t' );
 
809
                                        break;
 
810
                                case 'N':
 
811
                                case 'n':
 
812
                                        // add newline
 
813
                                        ret.append( '\n' );
 
814
                                        break;
 
815
                                case '\\':
 
816
                                case ',':
 
817
                                case ';':
 
818
                                        // add escaped character
 
819
                                        ret.append( Character.toChars( c ) );
 
820
                                        break;
 
821
                                default:
 
822
                                        // unknown escape sequence, so add it unescaped
 
823
                                        // (invalid/non-standard, but accepted)
 
824
                                        ret.append( "\\" );
 
825
                                        ret.append( Character.toChars( c ) );
 
826
                                        break;
 
827
                                }
 
828
                        }
 
829
 
 
830
                        return ret.toString();
 
831
                }
 
832
 
689
833
                private void parseN( String[] params, String value )
690
834
                {
691
835
                        // already got a better name?
692
836
                        if( _name_level >= NAMELEVEL_N ) return;
693
837
 
694
838
                        // get name parts
695
 
                        String[] name_parts = splitValueBySemicolon( value );
 
839
                        String[] name_parts = splitValueByCharacter( value, ';' );
696
840
 
697
841
                        // build name
698
842
                        value = "";
699
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
700
 
                                value += name_parts[ 1 ];
701
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
702
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
843
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
844
                        for( int a = 0; a < part_order.length; a++ )
 
845
                                if( name_parts.length > part_order[ a ] &&
 
846
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
847
                                {
 
848
                                        // split this part in to it's comma-separated bits
 
849
                                        String[] name_part_parts = splitValueByCharacter(
 
850
                                                name_parts[ part_order[ a ] ], ',' );
 
851
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
852
                                                if( name_part_parts[ b ].length() > 0 )
 
853
                                                {
 
854
                                                        if( value.length() > 0 ) value += " ";
 
855
                                                        value += name_part_parts[ b ];
 
856
                                                }
 
857
                                }
703
858
 
704
859
                        // set name
705
 
                        setName( value );
 
860
                        setName( unescapeValue( value ) );
706
861
                        _name_level = NAMELEVEL_N;
707
862
                }
708
863
 
712
867
                        if( _name_level >= NAMELEVEL_FN ) return;
713
868
 
714
869
                        // set name
715
 
                        setName( value );
 
870
                        setName( unescapeValue( value ) );
716
871
                        _name_level = NAMELEVEL_FN;
717
872
                }
718
873
 
719
874
                private void parseORG( String[] params, String value )
720
875
                {
721
876
                        // get org parts
722
 
                        String[] org_parts = splitValueBySemicolon( value );
 
877
                        String[] org_parts = splitValueByCharacter( value, ';' );
723
878
                        if( org_parts == null || org_parts.length < 1 ) return;
724
879
 
725
880
                        // build organisation name
727
882
                                String.valueOf( org_parts[ 0 ] ) );
728
883
                        for( int a = 1; a < org_parts.length; a++ )
729
884
                                builder.append( ", " ).append( org_parts[ a ] );
730
 
                        String organisation = builder.toString();
 
885
                        String organisation = unescapeValue( builder.toString() );
731
886
 
732
887
                        // set organisation name (using a title we've previously found)
733
888
                        addOrganisation( organisation, _cached_title, true );
744
899
 
745
900
                private void parseTITLE( String[] params, String value )
746
901
                {
 
902
                        value = unescapeValue( value );
 
903
 
747
904
                        // if we previously had an organisation, look it up and append this
748
905
                        // title to it
749
906
                        if( _cached_organisation != null && hasOrganisations() ) {
775
932
                        int type;
776
933
                        if( types.contains( "FAX" ) )
777
934
                                if( types.contains( "HOME" ) )
778
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
935
                                        type = TYPE_FAX_HOME;
779
936
                                else
780
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
937
                                        type = TYPE_FAX_WORK;
781
938
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
782
 
                                type = PhonesColumns.TYPE_MOBILE;
 
939
                                type = TYPE_MOBILE;
783
940
                        else if( types.contains( "PAGER" ) )
784
 
                                type = PhonesColumns.TYPE_PAGER;
 
941
                                type = TYPE_PAGER;
785
942
                        else if( types.contains( "WORK" ) )
786
 
                                type = PhonesColumns.TYPE_WORK;
 
943
                                type = TYPE_WORK;
787
944
                        else
788
 
                                type = PhonesColumns.TYPE_HOME;
 
945
                                type = TYPE_HOME;
789
946
 
790
947
                        // add phone number
791
948
                        addNumber( value, type, is_preferred );
802
959
                        boolean is_preferred = types.contains( "PREF" );
803
960
                        int type;
804
961
                        if( types.contains( "WORK" ) )
805
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
962
                                type = TYPE_WORK;
806
963
                        else
807
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
964
                                type = TYPE_HOME;
808
965
 
809
 
                        addEmail( value, type, is_preferred );
 
966
                        addEmail( unescapeValue( value ), type, is_preferred );
810
967
                }
811
968
 
812
969
                private void parseADR( String[] params, String value )
813
970
                {
814
971
                        // get address parts
815
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
972
                        String[] adr_parts = splitValueByCharacter( value, ';' );
816
973
 
817
974
                        // build address
818
975
                        value = "";
819
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
820
 
                                if( value.length() > 0 ) value += "\n";
821
 
                                value += adr_parts[ a ].trim();
822
 
                        }
823
 
 
824
 
                        Set< String > types = extractTypes( params, Arrays.asList(
825
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
826
 
 
827
 
                        // add address
828
 
                        int type;
829
 
                        if( types.contains( "WORK" ) )
830
 
                                type = Contacts.ContactMethods.TYPE_WORK;
831
 
                        else
832
 
                                type = Contacts.ContactMethods.TYPE_HOME;
833
 
 
834
 
                        addAddress( value, type );
835
 
                }
836
 
 
837
 
                public void finaliseParsing()
838
 
                        throws ParseException, SkipContactException,
839
 
                        AbortImportException
 
976
                        for( int a = 0; a < adr_parts.length; a++ )
 
977
                                if( adr_parts[ a ].length() > 0 )
 
978
                                {
 
979
                                        // version 3.0 vCards allow further splitting by comma
 
980
                                        if( _version.equals( "3.0" ) )
 
981
                                        {
 
982
                                                // split this part in to it's comma-separated bits and
 
983
                                                // add them on individual lines
 
984
                                                String[] adr_part_parts =
 
985
                                                        splitValueByCharacter( adr_parts[ a ], ',' );
 
986
                                                for( int b = 0; b < adr_part_parts.length; b++ )
 
987
                                                        if( adr_part_parts[ b ].length() > 0 )
 
988
                                                        {
 
989
                                                                if( value.length() > 0 ) value += "\n";
 
990
                                                                value += adr_part_parts[ b ];
 
991
                                                        }
 
992
                                        }
 
993
                                        else
 
994
                                        {
 
995
                                                // add this part on an individual line
 
996
                                                if( value.length() > 0 ) value += "\n";
 
997
                                                value += adr_parts[ a ];
 
998
                                        }
 
999
                                }
 
1000
 
 
1001
                        Set< String > types = extractTypes( params, Arrays.asList(
 
1002
                                "PREF", "WORK", "HOME" ) );
 
1003
 
 
1004
                        // add address
 
1005
                        int type;
 
1006
                        if( types.contains( "WORK" ) )
 
1007
                                type = TYPE_WORK;
 
1008
                        else
 
1009
                                type = TYPE_HOME;
 
1010
 
 
1011
                        addAddress( unescapeValue( value ), type );
 
1012
                }
 
1013
 
 
1014
                private void parseLABEL( String[] params, String value )
 
1015
                {
 
1016
                        Set< String > types = extractTypes( params, Arrays.asList(
 
1017
                                "PREF", "WORK", "HOME" ) );
 
1018
 
 
1019
                        // add address
 
1020
                        int type;
 
1021
                        if( types.contains( "WORK" ) )
 
1022
                                type = TYPE_WORK;
 
1023
                        else
 
1024
                                type = TYPE_HOME;
 
1025
 
 
1026
                        addAddress( unescapeValue( value ), type );
 
1027
                }
 
1028
 
 
1029
                private void parseNOTE( String[] params, String value )
 
1030
                {
 
1031
                        addNote( unescapeValue( value ) );
 
1032
                }
 
1033
 
 
1034
                private void parseBDAY( String[] params, String value )
 
1035
                {
 
1036
                        setBirthday( value );
 
1037
                }
 
1038
 
 
1039
                public void finaliseVcard()
 
1040
                        throws ParseException, ContactNotIdentifiableException
840
1041
                {
841
1042
                        // missing version (and data is present)
842
 
                        if( _version == null && _buffers != null )
 
1043
                        if( _version == null && _content_lines != null )
843
1044
                                throw new ParseException( R.string.error_vcf_malformed );
844
1045
 
845
 
                        // check if we should import this contact
846
 
                        try {
847
 
                                if( !isImportRequired( this ) )
848
 
                                        throw new SkipContactException();
849
 
                        }
850
 
                        catch( ContactNeedsMoreInfoException e ) {
851
 
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
852
 
                        }
 
1046
                        // finalise the parent class
 
1047
                        finalise();
853
1048
                }
854
1049
 
 
1050
                /**
 
1051
                 * Amongst the params, find the value of the first, only, of any with
 
1052
                 * the specified name.
 
1053
                 *
 
1054
                 * @param params
 
1055
                 * @param name
 
1056
                 * @return a value, or null
 
1057
                 */
855
1058
                private String checkParam( String[] params, String name )
856
1059
                {
 
1060
                        String[] res = checkParams( params, name );
 
1061
                        return res.length > 0? res[ 0 ] : null;
 
1062
                }
 
1063
 
 
1064
                /**
 
1065
                 * Amongst the params, find the values of any with the specified name.
 
1066
                 *
 
1067
                 * @param params
 
1068
                 * @param name
 
1069
                 * @return an array of values, or null
 
1070
                 */
 
1071
                private String[] checkParams( String[] params, String name )
 
1072
                {
 
1073
                        HashSet< String > ret = new HashSet< String >();
 
1074
 
857
1075
                        Pattern p = Pattern.compile(
858
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
 
1076
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
 
1077
                                Pattern.CASE_INSENSITIVE );
859
1078
                        for( int i = 0; i < params.length; i++ ) {
860
1079
                                Matcher m = p.matcher( params[ i ] );
861
1080
                                if( m.matches() )
862
 
                                        return m.group( 2 );
 
1081
                                        ret.add( m.group( 2 ) );
863
1082
                        }
864
 
                        return null;
 
1083
 
 
1084
                        return (String[]) ret.toArray( new String[ ret.size() ] );
865
1085
                }
866
1086
 
 
1087
                /**
 
1088
                 * Amongst the params, return any type values present.  For v2.1 vCards,
 
1089
                 * those types are just parameters.  For v3.0, they are prefixed with
 
1090
                 * "TYPE=".  There may also be multiple type parameters.
 
1091
                 *
 
1092
                 * @param params an array of params to look for types in
 
1093
                 * @param valid_types an list of upper-case type values to look for
 
1094
                 * @return a set of present type values
 
1095
                 */
867
1096
                private Set< String > extractTypes( String[] params,
868
1097
                        List< String > valid_types )
869
1098
                {
870
1099
                        HashSet< String > types = new HashSet< String >();
871
1100
 
872
1101
                        // get 3.0-style TYPE= param
873
 
                        String type_param;
874
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
875
 
                                String[] parts = type_param.split( "," );
876
 
                                for( int i = 0; i < parts.length; i++ )
877
 
                                        if( valid_types.contains( parts[ i ] ) )
878
 
                                                types.add( parts[ i ] );
 
1102
                        String type_params[] = checkParams( params, "TYPE" );
 
1103
                        for( int a = 0; a < type_params.length; a++ )
 
1104
                        {
 
1105
                                // check for a comma-separated list of types (why? I don't think
 
1106
                                // this is in the specs!)
 
1107
                                String[] parts = type_params[ a ].split( "," );
 
1108
                                for( int i = 0; i < parts.length; i++ ) {
 
1109
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1110
                                        if( valid_types.contains( ucpart ) )
 
1111
                                                types.add( ucpart );
 
1112
                                }
879
1113
                        }
880
1114
 
881
1115
                        // get 2.1-style type param
882
1116
                        if( _version.equals( "2.1" ) ) {
883
 
                                for( int i = 1; i < params.length; i++ )
884
 
                                        if( valid_types.contains( params[ i ] ) )
885
 
                                                types.add( params[ i ] );
 
1117
                                for( int i = 1; i < params.length; i++ ) {
 
1118
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1119
                                        if( valid_types.contains( ucparam ) )
 
1120
                                                types.add( ucparam );
 
1121
                                }
886
1122
                        }
887
1123
 
888
1124
                        return types;
910
1146
                                else if( ch == '=' && i == in.limit() - 1 )
911
1147
                                {
912
1148
                                        // we found a '=' at the end of a line signifying a multi-
913
 
                                        // line string, so we don't add it.
 
1149
                                        // line string, so we don't add it
914
1150
                                        another = true;
915
1151
                                        continue;
916
1152
                                }