/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2012-12-19 17:41:29 UTC
  • Revision ID: tim@ed.am-20121219174129-41i7vrz0jviideqi
fix intro strings

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * 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
38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
41
 
import java.util.Locale;
42
41
import java.util.NoSuchElementException;
43
42
import java.util.Set;
44
43
import java.util.Vector;
46
45
import java.util.regex.Pattern;
47
46
 
48
47
import android.content.SharedPreferences;
49
 
import android.os.Environment;
 
48
import android.provider.Contacts;
 
49
import android.provider.Contacts.PhonesColumns;
50
50
 
51
51
public class VcardImporter extends Importer
52
52
{
70
70
                File[] files = null;
71
71
                try
72
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
 
 
81
73
                        // open directory
82
 
                        File file = new File( Environment.getExternalStorageDirectory(),
83
 
                                prefs.getString( "location", "/" ) );
 
74
                        String path = "/sdcard" + prefs.getString( "location", "/" );
 
75
                        File file = new File( path );
84
76
                        if( !file.exists() )
85
77
                                showError( R.string.error_locationnotfound );
86
78
 
90
82
                                // get files
91
83
                                class VCardFilter implements FilenameFilter {
92
84
                                        public boolean accept( File dir, String name ) {
93
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
85
                                                return name.toLowerCase().endsWith( ".vcf" );
94
86
                                        }
95
87
                                }
96
88
                                files = file.listFiles( new VCardFilter() );
140
132
                        boolean in_vcard = false;
141
133
                        while( ( line = reader.readLine() ) != null )
142
134
                        {
143
 
                                if( !in_vcard )
144
 
                                {
 
135
                                if( !in_vcard ) {
145
136
                                        // look for vcard beginning
146
 
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
 
137
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
147
138
                                                in_vcard = true;
148
139
                                                _vcard_count++;
149
140
                                        }
150
 
                                        // check for vMsg files
151
 
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
152
 
                                                showError( getText( R.string.error_vcf_vmsgfile )
153
 
                                                        + file.getName() );
154
 
                                        }
155
141
                                }
156
 
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
 
142
                                else if( line.matches( "^END:VCARD" ) )
157
143
                                        in_vcard = false;
158
144
                        }
159
 
                        reader.close();
160
145
 
161
146
                }
162
147
                catch( FileNotFoundException e ) {
184
169
                        FileInputStream istream = new FileInputStream( file );
185
170
                        byte[] content = new byte[ (int)file.length() ];
186
171
                        istream.read( content );
187
 
                        istream.close();
 
172
                        istream = null;
188
173
 
189
174
                        // import
190
175
                        importVCardFileContent( content, file.getName() );
191
176
                }
192
 
                catch( OutOfMemoryError e ) {
193
 
                        showError( R.string.error_outofmemory );
194
 
                }
195
177
                catch( FileNotFoundException e ) {
196
178
                        showError( getText( R.string.error_filenotfound ) +
197
179
                                file.getName() );
210
192
                ContentLineIterator cli = new ContentLineIterator( content );
211
193
                while( cli.hasNext() )
212
194
                {
213
 
                        ContentLine content_line = cli.next();
 
195
                        ByteBuffer buffer = cli.next();
214
196
 
215
 
                        // get a US-ASCII version of the string, for processing
216
 
                        String line = content_line.getUsAsciiLine();
 
197
                        // get a US-ASCII version of the line for processing
 
198
                        String line;
 
199
                        try {
 
200
                                line = new String( buffer.array(), buffer.position(),
 
201
                                        buffer.limit() - buffer.position(), "US-ASCII" );
 
202
                        }
 
203
                        catch( UnsupportedEncodingException e ) {
 
204
                                // we know US-ASCII is supported, so appease the compiler...
 
205
                                line = "";
 
206
                        }
217
207
 
218
208
                        if( vcard == null ) {
219
209
                                // look for vcard beginning
220
 
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
 
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
221
211
                                        setProgress( _progress++ );
222
212
                                        vcard = new Vcard();
223
213
                                        vcard_start_line = cli.getLineNumber();
225
215
                        }
226
216
                        else {
227
217
                                // look for vcard content or ending
228
 
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
 
218
                                if( line.matches( "^END:VCARD" ) )
229
219
                                {
230
220
                                        // finalise the vcard/contact
231
221
                                        try {
268
258
                                {
269
259
                                        // try giving the line to the vcard
270
260
                                        try {
271
 
                                                vcard.parseLine( content_line );
 
261
                                                vcard.parseLine( buffer, line,
 
262
                                                        cli.doesNextLineLookFolded() );
272
263
                                        }
273
264
                                        catch( Vcard.ParseException e ) {
274
265
                                                skipContact();
281
272
                                                        finish( ACTION_ABORT );
282
273
                                                }
283
274
 
284
 
                                                // Although we're continuing, we still need to abort
285
 
                                                // this vCard.  Further lines will be ignored until we
 
275
                                                // although we're continuing, we still need to abort
 
276
                                                // this vCard. Further lines will be ignored until we
286
277
                                                // get to another BEGIN:VCARD line.
287
278
                                                vcard = null;
288
279
                                        }
289
280
                                        catch( Vcard.SkipImportException e ) {
290
281
                                                skipContact();
291
 
                                                // Abort this vCard.  Further lines will be ignored until
 
282
                                                // abort this vCard. Further lines will be ignored until
292
283
                                                // we get to another BEGIN:VCARD line.
293
284
                                                vcard = null;
294
285
                                        }
297
288
                }
298
289
        }
299
290
 
300
 
        class ContentLine
301
 
        {
302
 
                private ByteBuffer _buffer;
303
 
                private boolean _folded_next;
304
 
                private String _line;
305
 
 
306
 
                public ContentLine( ByteBuffer buffer, boolean folded_next )
307
 
                {
308
 
                        _buffer = buffer;
309
 
                        _folded_next = folded_next;
310
 
                        _line = null;
311
 
                }
312
 
 
313
 
                public ByteBuffer getBuffer()
314
 
                {
315
 
                        return _buffer;
316
 
                }
317
 
 
318
 
                public boolean doesNextLineLookFolded()
319
 
                {
320
 
                        return _folded_next;
321
 
                }
322
 
 
323
 
                public String getUsAsciiLine()
324
 
                {
325
 
                        // generated line and cache it
326
 
                        if( _line == null ) {
327
 
                                try {
328
 
                                        _line = new String( _buffer.array(), _buffer.position(),
329
 
                                                _buffer.limit() - _buffer.position(), "US-ASCII" );
330
 
                                }
331
 
                                catch( UnsupportedEncodingException e ) {
332
 
                                        // we know US-ASCII *is* supported, so appease the
333
 
                                        // compiler...
334
 
                                }
335
 
                        }
336
 
 
337
 
                        // return cached line
338
 
                        return _line;
339
 
                }
340
 
        }
341
 
 
342
 
        class ContentLineIterator implements Iterator< ContentLine >
 
291
        class ContentLineIterator implements Iterator< ByteBuffer >
343
292
        {
344
293
                protected byte[] _content = null;
345
294
                protected int _pos = 0;
357
306
                }
358
307
 
359
308
                @Override
360
 
                public ContentLine next()
 
309
                public ByteBuffer next()
361
310
                {
362
311
                        int initial_pos = _pos;
363
312
 
370
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
371
320
                                        _pos++;
372
321
                                        _line++;
373
 
                                        return new ContentLine(
374
 
                                                ByteBuffer.wrap( _content, initial_pos,
375
 
                                                        to - initial_pos ),
376
 
                                                doesNextLineLookFolded() );
 
322
                                        return ByteBuffer.wrap( _content, initial_pos,
 
323
                                                to - initial_pos );
377
324
                                }
378
325
 
379
326
                        // we didn't find one, but were there bytes left?
381
328
                                int to = _pos;
382
329
                                _pos++;
383
330
                                _line++;
384
 
                                return new ContentLine(
385
 
                                        ByteBuffer.wrap( _content, initial_pos,
386
 
                                                to - initial_pos ),
387
 
                                        doesNextLineLookFolded() );
 
331
                                return ByteBuffer.wrap( _content, initial_pos,
 
332
                                        to - initial_pos );
388
333
                        }
389
334
 
390
335
                        // no bytes left
402
347
                 * onto the end of this one?
403
348
                 * @return
404
349
                 */
405
 
                private boolean doesNextLineLookFolded()
 
350
                public boolean doesNextLineLookFolded()
406
351
                {
407
352
                        return _pos > 0 && _pos < _content.length &&
408
 
                                _content[ _pos - 1 ] == '\n' &&
409
 
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
 
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
410
354
                }
411
355
 
412
356
                public int getLineNumber()
424
368
                private final static int MULTILINE_NONE = 0;
425
369
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
426
370
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
427
 
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
 
371
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
428
372
 
429
373
                private String _version = null;
430
 
                private Vector< ContentLine > _content_lines = null;
 
374
                private Vector< ByteBuffer > _buffers = null;
431
375
                private int _name_level = NAMELEVEL_NONE;
432
376
                private int _parser_multiline_state = MULTILINE_NONE;
433
377
                private String _parser_current_name_and_params = null;
476
420
                @SuppressWarnings("serial")
477
421
                protected class SkipImportException extends Exception { }
478
422
 
479
 
                private String extractCollonPartFromLine( ContentLine content_line,
480
 
                        boolean former )
 
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
 
424
                        String line, boolean former )
481
425
                {
 
426
                        String ret = null;
 
427
 
 
428
                        // get a US-ASCII version of the line for processing, unless we were
 
429
                        // supplied with one
 
430
                        if( line == null ) {
 
431
                                try {
 
432
                                        line = new String( buffer.array(), buffer.position(),
 
433
                                                buffer.limit() - buffer.position(), "US-ASCII" );
 
434
                                }
 
435
                                catch( UnsupportedEncodingException e ) {
 
436
                                        // we know US-ASCII is supported, so appease the compiler...
 
437
                                        line = "";
 
438
                                }
 
439
                        }
 
440
 
482
441
                        // split line into name and value parts and check to make sure we
483
442
                        // only got 2 parts and that the first part is not zero in length
484
 
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
 
443
                        String[] parts = line.split( ":", 2 );
485
444
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
486
 
                                return parts[ former? 0 : 1 ].trim();
487
 
 
488
 
                        return null;
489
 
                }
490
 
 
491
 
                private String extractNameAndParamsFromLine( ContentLine content_line )
492
 
                {
493
 
                        return extractCollonPartFromLine( content_line, true );
494
 
                }
495
 
 
496
 
                private String extractValueFromLine( ContentLine content_line )
497
 
                {
498
 
                        return extractCollonPartFromLine( content_line, false );
499
 
                }
500
 
 
501
 
                public void parseLine( ContentLine content_line )
 
445
                                ret = parts[ former? 0 : 1 ];
 
446
 
 
447
                        return ret;
 
448
                }
 
449
 
 
450
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
 
451
                        String line )
 
452
                {
 
453
                        return extractCollonPartFromLine( buffer, line, true );
 
454
                }
 
455
 
 
456
                private String extractValueFromLine( ByteBuffer buffer, String line )
 
457
                {
 
458
                        return extractCollonPartFromLine( buffer, line, false );
 
459
                }
 
460
 
 
461
                public void parseLine( ByteBuffer buffer, String line,
 
462
                        boolean next_line_looks_folded )
502
463
                        throws ParseException, SkipImportException,
503
464
                        AbortImportException
504
465
                {
507
468
                        {
508
469
                                // tentatively get name and params from line
509
470
                                String name_and_params =
510
 
                                        extractNameAndParamsFromLine( content_line );
 
471
                                        extractNameAndParamsFromLine( buffer, line );
511
472
 
512
473
                                // is it a version line?
513
474
                                if( name_and_params != null &&
514
 
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
 
475
                                        name_and_params.equals( "VERSION" ) )
515
476
                                {
516
477
                                        // yes, get it!
517
 
                                        String value = extractValueFromLine( content_line );
518
 
                                        if( value == null || (
519
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
520
 
                                        {
 
478
                                        String value = extractValueFromLine( buffer, line );
 
479
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
521
480
                                                throw new ParseException( R.string.error_vcf_version );
522
 
                                        }
523
481
                                        _version = value;
524
482
 
525
483
                                        // parse any buffers we've been accumulating while we waited
526
484
                                        // for a version
527
 
                                        if( _content_lines != null )
528
 
                                                for( int i = 0; i < _content_lines.size(); i++ )
529
 
                                                        parseLine( _content_lines.get( i ) );
530
 
                                        _content_lines = null;
 
485
                                        if( _buffers != null )
 
486
                                                for( int i = 0; i < _buffers.size(); i++ )
 
487
                                                        parseLine( _buffers.get( i ), null,
 
488
                                                                i + 1 < _buffers.size() &&
 
489
                                                                _buffers.get( i + 1 ).hasRemaining() &&
 
490
                                                                _buffers.get( i + 1 ).get(
 
491
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
 
492
                                        _buffers = null;
531
493
                                }
532
494
                                else
533
495
                                {
534
496
                                        // no, so stash this line till we get a version
535
 
                                        if( _content_lines == null )
536
 
                                                _content_lines = new Vector< ContentLine >();
537
 
                                        _content_lines.add( content_line );
 
497
                                        if( _buffers == null )
 
498
                                                _buffers = new Vector< ByteBuffer >();
 
499
                                        _buffers.add( buffer );
538
500
                                }
539
501
                        }
540
502
                        else
541
503
                        {
542
504
                                // name and params and the position in the buffer where the
543
 
                                // "value" part of the line starts
 
505
                                // "value" part of the line start
544
506
                                String name_and_params;
545
507
                                int pos;
546
508
 
552
514
 
553
515
                                        // skip some initial line characters, depending on the type
554
516
                                        // of multi-line we're handling
555
 
                                        pos = content_line.getBuffer().position();
 
517
                                        pos = buffer.position();
556
518
                                        switch( _parser_multiline_state )
557
519
                                        {
558
520
                                        case MULTILINE_FOLDED:
559
521
                                                pos++;
560
522
                                                break;
561
523
                                        case MULTILINE_ENCODED:
562
 
                                                while( pos < content_line.getBuffer().limit() && (
563
 
                                                        content_line.getBuffer().get( pos ) == ' ' ||
564
 
                                                        content_line.getBuffer().get( pos ) == '\t' ) )
 
524
                                                while( pos < buffer.limit() && (
 
525
                                                        buffer.get( pos ) == ' ' ||
 
526
                                                        buffer.get( pos ) == '\t' ) )
565
527
                                                {
566
528
                                                        pos++;
567
529
                                                }
576
538
                                }
577
539
                                else
578
540
                                {
579
 
                                        // skip empty lines
580
 
                                        if( content_line.getUsAsciiLine().trim().length() == 0 )
581
 
                                                return;
582
 
 
583
541
                                        // get name and params from line, and since we're not
584
542
                                        // parsing a subsequent line in a multi-line, this should
585
543
                                        // not fail, or it's an error
586
544
                                        name_and_params =
587
 
                                                extractNameAndParamsFromLine( content_line );
 
545
                                                extractNameAndParamsFromLine( buffer, line );
588
546
                                        if( name_and_params == null )
589
547
                                                throw new ParseException(
590
548
                                                        R.string.error_vcf_malformed );
591
549
 
592
550
                                        // calculate how many chars to skip from beginning of line
593
551
                                        // so we skip the property "name:" part
594
 
                                        pos = content_line.getBuffer().position() +
595
 
                                                name_and_params.length() + 1;
 
552
                                        pos = buffer.position() + name_and_params.length() + 1;
596
553
 
597
554
                                        // reset the saved multi-line state
598
555
                                        _parser_current_name_and_params = name_and_params;
601
558
 
602
559
                                // get value from buffer, as raw bytes
603
560
                                ByteBuffer value;
604
 
                                value = ByteBuffer.wrap( content_line.getBuffer().array(), pos,
605
 
                                        content_line.getBuffer().limit() - pos );
 
561
                                value = ByteBuffer.wrap( buffer.array(), pos,
 
562
                                        buffer.limit() - pos );
606
563
 
607
564
                                // get parameter parts
608
565
                                String[] name_param_parts = name_and_params.split( ";", -1 );
615
572
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
616
573
                                ) );
617
574
                                boolean is_interesting_field =
618
 
                                        interesting_fields.contains(
619
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
620
576
 
621
577
                                // parse encoding parameter
622
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
623
 
                                if( encoding != null )
624
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
625
580
                                if( is_interesting_field && encoding != null &&
626
 
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
627
 
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
628
 
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
 
581
                                        !encoding.equals( "8BIT" ) &&
 
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
583
                                        //&& !encoding.equals( "BASE64" ) )
629
584
                                {
630
585
                                        throw new ParseException( R.string.error_vcf_encoding );
631
586
                                }
632
587
 
633
588
                                // parse charset parameter
634
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
635
 
                                if( charset != null )
636
 
                                        charset = charset.toUpperCase( Locale.US );
 
590
                                if( charset != null ) charset = charset.toUpperCase();
637
591
                                if( charset != null &&
638
 
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
639
 
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
640
 
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
 
592
                                        !charset.equals( "US-ASCII" ) &&
 
593
                                        !charset.equals( "ASCII" ) &&
 
594
                                        !charset.equals( "UTF-8" ) )
641
595
                                {
642
596
                                        throw new ParseException( R.string.error_vcf_charset );
643
597
                                }
645
599
                                // do unencoding (or default to a fake unencoding result with
646
600
                                // the raw string)
647
601
                                UnencodeResult unencoding_result = null;
648
 
                                if( encoding != null &&
649
 
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
650
 
                                {
 
602
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
651
603
                                        unencoding_result = unencodeQuotedPrintable( value );
652
 
                                }
653
 
//                              else if( encoding != null &&
654
 
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
655
 
//                              {
 
604
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
656
605
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
657
 
//                              }
658
606
                                if( unencoding_result != null ) {
659
607
                                        value = unencoding_result.getBuffer();
660
608
                                        if( unencoding_result.isAnotherLineRequired() )
665
613
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
666
614
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
667
615
                                        ( charset != null && (
668
 
                                                charset.equalsIgnoreCase( "ASCII" ) ||
669
 
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
 
616
                                                charset.equals( "ASCII" ) ||
 
617
                                                charset.equals( "US-ASCII" ) ) ) )
670
618
                                {
671
619
                                        value = transcodeAsciiToUtf8( value );
672
620
                                }
683
631
                                // for some entries that have semicolon-separated value parts,
684
632
                                // check to see if the value ends in an escape character, which
685
633
                                // indicates that we have a multi-line value
686
 
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
687
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
688
 
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
 
634
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
 
635
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
 
636
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
689
637
                                        doesStringEndInAnEscapeChar( string_value ) )
690
638
                                {
691
639
                                        _parser_multiline_state = MULTILINE_ESCAPED;
693
641
                                                string_value.length() - 1 );
694
642
                                }
695
643
 
696
 
                                // if we know we're not in an encoding-based multi-line, check
697
 
                                // to see if we're in a folded multi-line
 
644
                                // now we know whether we're in an encoding multi-line,
 
645
                                // determine if we're in a v3 folded multi-line or not
698
646
                                if( _parser_multiline_state == MULTILINE_NONE &&
699
 
                                        content_line.doesNextLineLookFolded() )
 
647
                                        _version.equals( "3.0" ) && next_line_looks_folded )
700
648
                                {
701
649
                                        _parser_multiline_state = MULTILINE_FOLDED;
702
650
                                }
714
662
                                if( complete_value.length() < 1 ) return;
715
663
 
716
664
                                // parse some properties
717
 
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
 
665
                                if( name_param_parts[ 0 ].equals( "N" ) )
718
666
                                        parseN( name_param_parts, complete_value );
719
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
 
667
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
720
668
                                        parseFN( name_param_parts, complete_value );
721
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
 
669
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
722
670
                                        parseORG( name_param_parts, complete_value );
723
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
 
671
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
724
672
                                        parseTITLE( name_param_parts, complete_value );
725
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
 
673
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
726
674
                                        parseTEL( name_param_parts, complete_value );
727
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
 
675
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
728
676
                                        parseEMAIL( name_param_parts, complete_value );
729
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
 
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
730
678
                                        parseADR( name_param_parts, complete_value );
731
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
 
679
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
732
680
                                        parseLABEL( name_param_parts, complete_value );
733
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
734
 
                                        parseNOTE( name_param_parts, complete_value );
735
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
736
 
                                        parseBDAY( name_param_parts, complete_value );
737
681
                        }
738
682
                }
739
683
 
763
707
                        {
764
708
                                String str = parts.get( a );
765
709
 
766
 
                                // Look for parts that end in an escape character, but ignore
767
 
                                // the final part.  We've already detected escape chars at the
 
710
                                // look for parts that end in an escape character, but ignore
 
711
                                // the final part. We've already detected escape chars at the
768
712
                                // end of the final part in parseLine() and handled multi-lines
769
713
                                // accordingly.
770
714
                                if( a < parts.size() - 1 &&
811
755
                                in_escape = false;
812
756
                                switch( c )
813
757
                                {
814
 
                                case 'T':
815
 
                                case 't':
816
 
                                        // add tab (invalid/non-standard, but accepted)
817
 
                                        ret.append( '\t' );
818
 
                                        break;
819
758
                                case 'N':
820
759
                                case 'n':
821
760
                                        // add newline
829
768
                                        break;
830
769
                                default:
831
770
                                        // unknown escape sequence, so add it unescaped
832
 
                                        // (invalid/non-standard, but accepted)
833
771
                                        ret.append( "\\" );
834
772
                                        ret.append( Character.toChars( c ) );
835
773
                                        break;
860
798
                                        for( int b = 0; b < name_part_parts.length; b++ )
861
799
                                                if( name_part_parts[ b ].length() > 0 )
862
800
                                                {
863
 
                                                        if( value.length() > 0 ) value += " ";
 
801
                                                        if( value.length() == 0 ) value += " ";
864
802
                                                        value += name_part_parts[ b ];
865
803
                                                }
866
804
                                }
941
879
                        int type;
942
880
                        if( types.contains( "FAX" ) )
943
881
                                if( types.contains( "HOME" ) )
944
 
                                        type = TYPE_FAX_HOME;
 
882
                                        type = PhonesColumns.TYPE_FAX_HOME;
945
883
                                else
946
 
                                        type = TYPE_FAX_WORK;
 
884
                                        type = PhonesColumns.TYPE_FAX_WORK;
947
885
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
948
 
                                type = TYPE_MOBILE;
 
886
                                type = PhonesColumns.TYPE_MOBILE;
949
887
                        else if( types.contains( "PAGER" ) )
950
 
                                type = TYPE_PAGER;
 
888
                                type = PhonesColumns.TYPE_PAGER;
951
889
                        else if( types.contains( "WORK" ) )
952
 
                                type = TYPE_WORK;
 
890
                                type = PhonesColumns.TYPE_WORK;
953
891
                        else
954
 
                                type = TYPE_HOME;
 
892
                                type = PhonesColumns.TYPE_HOME;
955
893
 
956
894
                        // add phone number
957
895
                        addNumber( value, type, is_preferred );
968
906
                        boolean is_preferred = types.contains( "PREF" );
969
907
                        int type;
970
908
                        if( types.contains( "WORK" ) )
971
 
                                type = TYPE_WORK;
 
909
                                type = Contacts.ContactMethods.TYPE_WORK;
972
910
                        else
973
 
                                type = TYPE_HOME;
 
911
                                type = Contacts.ContactMethods.TYPE_HOME;
974
912
 
975
913
                        addEmail( unescapeValue( value ), type, is_preferred );
976
914
                }
985
923
                        for( int a = 0; a < adr_parts.length; a++ )
986
924
                                if( adr_parts[ a ].length() > 0 )
987
925
                                {
988
 
                                        // version 3.0 vCards allow further splitting by comma
989
 
                                        if( _version.equals( "3.0" ) )
990
 
                                        {
991
 
                                                // split this part in to it's comma-separated bits and
992
 
                                                // add them on individual lines
993
 
                                                String[] adr_part_parts =
994
 
                                                        splitValueByCharacter( adr_parts[ a ], ',' );
995
 
                                                for( int b = 0; b < adr_part_parts.length; b++ )
996
 
                                                        if( adr_part_parts[ b ].length() > 0 )
997
 
                                                        {
998
 
                                                                if( value.length() > 0 ) value += "\n";
999
 
                                                                value += adr_part_parts[ b ];
1000
 
                                                        }
1001
 
                                        }
1002
 
                                        else
1003
 
                                        {
1004
 
                                                // add this part on an individual line
1005
 
                                                if( value.length() > 0 ) value += "\n";
1006
 
                                                value += adr_parts[ a ];
1007
 
                                        }
 
926
                                        // split this part in to it's comma-separated bits
 
927
                                        String[] adr_part_parts =
 
928
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
929
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
930
                                                if( adr_part_parts[ b ].length() > 0 )
 
931
                                                {
 
932
                                                        if( value.length() > 0 ) value += "\n";
 
933
                                                        value += adr_part_parts[ b ];
 
934
                                                }
1008
935
                                }
1009
936
 
1010
937
                        Set< String > types = extractTypes( params, Arrays.asList(
1013
940
                        // add address
1014
941
                        int type;
1015
942
                        if( types.contains( "WORK" ) )
1016
 
                                type = TYPE_WORK;
 
943
                                type = Contacts.ContactMethods.TYPE_WORK;
1017
944
                        else
1018
 
                                type = TYPE_HOME;
 
945
                                type = Contacts.ContactMethods.TYPE_HOME;
1019
946
 
1020
947
                        addAddress( unescapeValue( value ), type );
1021
948
                }
1028
955
                        // add address
1029
956
                        int type;
1030
957
                        if( types.contains( "WORK" ) )
1031
 
                                type = TYPE_WORK;
 
958
                                type = Contacts.ContactMethods.TYPE_WORK;
1032
959
                        else
1033
 
                                type = TYPE_HOME;
 
960
                                type = Contacts.ContactMethods.TYPE_HOME;
1034
961
 
1035
962
                        addAddress( unescapeValue( value ), type );
1036
963
                }
1037
964
 
1038
 
                private void parseNOTE( String[] params, String value )
1039
 
                {
1040
 
                        addNote( unescapeValue( value ) );
1041
 
                }
1042
 
 
1043
 
                private void parseBDAY( String[] params, String value )
1044
 
                {
1045
 
                        setBirthday( value );
1046
 
                }
1047
 
 
1048
965
                public void finaliseVcard()
1049
966
                        throws ParseException, ContactNotIdentifiableException
1050
967
                {
1051
968
                        // missing version (and data is present)
1052
 
                        if( _version == null && _content_lines != null )
 
969
                        if( _version == null && _buffers != null )
1053
970
                                throw new ParseException( R.string.error_vcf_malformed );
1054
971
 
1055
972
                        // finalise the parent class
1058
975
 
1059
976
                /**
1060
977
                 * Amongst the params, find the value of the first, only, of any with
1061
 
                 * the specified name.
1062
 
                 *
 
978
                 * the specified name
1063
979
                 * @param params
1064
980
                 * @param name
1065
981
                 * @return a value, or null
1071
987
                }
1072
988
 
1073
989
                /**
1074
 
                 * Amongst the params, find the values of any with the specified name.
1075
 
                 *
 
990
                 * Amongst the params, find the values of any with the specified name
1076
991
                 * @param params
1077
992
                 * @param name
1078
993
                 * @return an array of values, or null
1082
997
                        HashSet< String > ret = new HashSet< String >();
1083
998
 
1084
999
                        Pattern p = Pattern.compile(
1085
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
1086
 
                                Pattern.CASE_INSENSITIVE );
 
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1087
1001
                        for( int i = 0; i < params.length; i++ ) {
1088
1002
                                Matcher m = p.matcher( params[ i ] );
1089
1003
                                if( m.matches() )
1094
1008
                }
1095
1009
 
1096
1010
                /**
1097
 
                 * Amongst the params, return any type values present.  For v2.1 vCards,
1098
 
                 * those types are just parameters.  For v3.0, they are prefixed with
1099
 
                 * "TYPE=".  There may also be multiple type parameters.
1100
 
                 *
1101
 
                 * @param params an array of params to look for types in
1102
 
                 * @param valid_types an list of upper-case type values to look for
 
1011
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1012
                 * those types are just parameters. For v3.0, they are prefixed with
 
1013
                 * "TYPE=". There may also be multiple type parameters.
 
1014
                 * @param params
 
1015
                 * @param a list of type values to look for
1103
1016
                 * @return a set of present type values
1104
1017
                 */
1105
1018
                private Set< String > extractTypes( String[] params,
1111
1024
                        String type_params[] = checkParams( params, "TYPE" );
1112
1025
                        for( int a = 0; a < type_params.length; a++ )
1113
1026
                        {
1114
 
                                // check for a comma-separated list of types (why? I don't think
1115
 
                                // this is in the specs!)
 
1027
                                // check for a comma-separated list of types (why? this isn't in
 
1028
                                // the specs!)
1116
1029
                                String[] parts = type_params[ a ].split( "," );
1117
 
                                for( int i = 0; i < parts.length; i++ ) {
1118
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
1119
 
                                        if( valid_types.contains( ucpart ) )
1120
 
                                                types.add( ucpart );
1121
 
                                }
 
1030
                                for( int i = 0; i < parts.length; i++ )
 
1031
                                        if( valid_types.contains( parts[ i ] ) )
 
1032
                                                types.add( parts[ i ] );
1122
1033
                        }
1123
1034
 
1124
1035
                        // get 2.1-style type param
1125
1036
                        if( _version.equals( "2.1" ) ) {
1126
 
                                for( int i = 1; i < params.length; i++ ) {
1127
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
1128
 
                                        if( valid_types.contains( ucparam ) )
1129
 
                                                types.add( ucparam );
1130
 
                                }
 
1037
                                for( int i = 1; i < params.length; i++ )
 
1038
                                        if( valid_types.contains( params[ i ] ) )
 
1039
                                                types.add( params[ i ] );
1131
1040
                        }
1132
1041
 
1133
1042
                        return types;
1155
1064
                                else if( ch == '=' && i == in.limit() - 1 )
1156
1065
                                {
1157
1066
                                        // we found a '=' at the end of a line signifying a multi-
1158
 
                                        // line string, so we don't add it
 
1067
                                        // line string, so we don't add it.
1159
1068
                                        another = true;
1160
1069
                                        continue;
1161
1070
                                }