/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-21 14:47:57 UTC
  • Revision ID: tim@ed.am-20121221144757-5cb1lgsp7fdt7p2n
updated TODO

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
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
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
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;
32
32
import java.io.IOException;
33
33
import java.io.UnsupportedEncodingException;
34
34
import java.nio.ByteBuffer;
 
35
import java.util.ArrayList;
35
36
import java.util.Arrays;
 
37
import java.util.HashMap;
36
38
import java.util.HashSet;
37
39
import java.util.Iterator;
38
40
import java.util.List;
 
41
import java.util.Locale;
 
42
import java.util.NoSuchElementException;
39
43
import java.util.Set;
40
44
import java.util.Vector;
41
45
import java.util.regex.Matcher;
42
46
import java.util.regex.Pattern;
43
 
import java.util.NoSuchElementException;
44
 
import java.lang.UnsupportedOperationException;
45
47
 
 
48
import android.annotation.SuppressLint;
46
49
import android.content.SharedPreferences;
47
 
import android.provider.Contacts;
48
 
import android.provider.Contacts.PhonesColumns;
49
50
 
50
 
public class VCFImporter extends Importer
 
51
public class VcardImporter extends Importer
51
52
{
52
 
        private int _vCardCount = 0;
 
53
        private int _vcard_count = 0;
53
54
        private int _progress = 0;
54
55
 
55
 
        public VCFImporter( Doit doit )
 
56
        public VcardImporter( Doit doit )
56
57
        {
57
58
                super( doit );
58
59
        }
59
60
 
 
61
        @SuppressLint( "SdCardPath" )
60
62
        @Override
61
63
        protected void onImport() throws AbortImportException
62
64
        {
81
83
                                // get files
82
84
                                class VCardFilter implements FilenameFilter {
83
85
                                        public boolean accept( File dir, String name ) {
84
 
                                                return name.toLowerCase().endsWith( ".vcf" );
 
86
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
85
87
                                        }
86
88
                                }
87
89
                                files = file.listFiles( new VCardFilter() );
109
111
                        countVCardFile( files[ i ] );
110
112
                        setTmpProgress( i );
111
113
                }
112
 
                setProgressMax( _vCardCount );  // will also update tmp progress
 
114
                setProgressMax( _vcard_count ); // will also update tmp progress
113
115
 
114
116
                // import them
115
117
                setProgress( 0 );
116
118
                for( int i = 0; i < files.length; i++ )
117
119
                        importVCardFile( files[ i ] );
 
120
                setProgress( _vcard_count );
118
121
        }
119
122
 
120
123
        private void countVCardFile( File file ) throws AbortImportException
127
130
 
128
131
                        // read
129
132
                        String line;
130
 
                        boolean inVCard = false;
 
133
                        boolean in_vcard = false;
131
134
                        while( ( line = reader.readLine() ) != null )
132
135
                        {
133
 
                                if( !inVCard ) {
 
136
                                if( !in_vcard ) {
134
137
                                        // look for vcard beginning
135
138
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
136
 
                                                inVCard = true;
137
 
                                                _vCardCount++;
 
139
                                                in_vcard = true;
 
140
                                                _vcard_count++;
138
141
                                        }
139
142
                                }
140
143
                                else if( line.matches( "^END:VCARD" ) )
141
 
                                        inVCard = false;
 
144
                                        in_vcard = false;
142
145
                        }
143
146
 
144
147
                }
167
170
                        FileInputStream istream = new FileInputStream( file );
168
171
                        byte[] content = new byte[ (int)file.length() ];
169
172
                        istream.read( content );
 
173
                        istream = null;
170
174
 
171
175
                        // import
172
176
                        importVCardFileContent( content, file.getName() );
184
188
                throws AbortImportException
185
189
        {
186
190
                // go through lines
187
 
                VCard vCard = null;
 
191
                Vcard vcard = null;
 
192
                int vcard_start_line = 0;
188
193
                ContentLineIterator cli = new ContentLineIterator( content );
189
194
                while( cli.hasNext() )
190
195
                {
197
202
                                        buffer.limit() - buffer.position(), "US-ASCII" );
198
203
                        }
199
204
                        catch( UnsupportedEncodingException e ) {
200
 
                                // we know US-ASCII is supported, so appease the compiler...
 
205
                                // we know US-ASCII *is* supported, so appease the compiler...
201
206
                                line = "";
202
207
                        }
203
208
 
204
 
                        if( vCard == null ) {
 
209
                        if( vcard == null ) {
205
210
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN:VCARD" ) ) {
207
 
                                        setProgress( ++_progress );
208
 
                                        vCard = new VCard();
 
211
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
 
212
                                        setProgress( _progress++ );
 
213
                                        vcard = new Vcard();
 
214
                                        vcard_start_line = cli.getLineNumber();
209
215
                                }
210
216
                        }
211
217
                        else {
212
218
                                // look for vcard content or ending
213
 
                                if( line.matches( "^END:VCARD" ) )
 
219
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
214
220
                                {
215
 
                                        // store vcard and do away with it
 
221
                                        // finalise the vcard/contact
216
222
                                        try {
217
 
                                                vCard.finaliseParsing();
218
 
                                                importContact( vCard );
219
 
                                        }
220
 
                                        catch( VCard.ParseException e ) {
221
 
                                                skipContact();
222
 
                                                if( !showContinue(
223
 
                                                        getText( R.string.error_vcf_parse ).toString()
224
 
                                                        + fileName + "\n" + e.getMessage() ) )
225
 
                                                {
226
 
                                                        finish( ACTION_ABORT );
227
 
                                                }
228
 
                                        }
229
 
                                        catch( VCard.SkipContactException e ) {
230
 
                                                skipContact();
231
 
                                                // do nothing
232
 
                                        }
233
 
                                        vCard = null;
 
223
                                                vcard.finaliseVcard();
 
224
 
 
225
                                                // pass the finalised contact to the importer
 
226
                                                importContact( vcard );
 
227
                                        }
 
228
                                        catch( Vcard.ParseException e ) {
 
229
                                                if( !showContinue(
 
230
                                                        getText( R.string.error_vcf_parse ).toString()
 
231
                                                        + fileName +
 
232
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
233
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
234
                                                {
 
235
                                                        finish( ACTION_ABORT );
 
236
                                                }
 
237
                                                else
 
238
                                                        skipContact();
 
239
                                        }
 
240
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
241
                                                if( !showContinue(
 
242
                                                        getText( R.string.error_vcf_parse ).toString()
 
243
                                                        + fileName +
 
244
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
245
                                                        + vcard_start_line + ":\n" + getText(
 
246
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
247
                                                ) )
 
248
                                                {
 
249
                                                        finish( ACTION_ABORT );
 
250
                                                }
 
251
                                                else
 
252
                                                        skipContact();
 
253
                                        }
 
254
 
 
255
                                        // discard this vcard
 
256
                                        vcard = null;
234
257
                                }
235
258
                                else
236
259
                                {
237
260
                                        // try giving the line to the vcard
238
261
                                        try {
239
 
                                                vCard.parseLine( buffer, line,
 
262
                                                vcard.parseLine( buffer, line,
240
263
                                                        cli.doesNextLineLookFolded() );
241
264
                                        }
242
 
                                        catch( VCard.ParseException e ) {
 
265
                                        catch( Vcard.ParseException e ) {
243
266
                                                skipContact();
244
267
                                                if( !showContinue(
245
268
                                                        getText( R.string.error_vcf_parse ).toString()
246
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
269
                                                        + fileName +
 
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
247
272
                                                {
248
273
                                                        finish( ACTION_ABORT );
249
274
                                                }
251
276
                                                // although we're continuing, we still need to abort
252
277
                                                // this vCard. Further lines will be ignored until we
253
278
                                                // get to another BEGIN:VCARD line.
254
 
                                                vCard = null;
 
279
                                                vcard = null;
255
280
                                        }
256
 
                                        catch( VCard.SkipContactException e ) {
 
281
                                        catch( Vcard.SkipImportException e ) {
257
282
                                                skipContact();
258
283
                                                // abort this vCard. Further lines will be ignored until
259
284
                                                // we get to another BEGIN:VCARD line.
260
 
                                                vCard = null;
 
285
                                                vcard = null;
261
286
                                        }
262
287
                                }
263
288
                        }
268
293
        {
269
294
                protected byte[] _content = null;
270
295
                protected int _pos = 0;
 
296
                protected int _line = 0;
271
297
 
272
298
                public ContentLineIterator( byte[] content )
273
299
                {
293
319
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
294
320
                                                _pos > initial_pos )? _pos - 1 : _pos;
295
321
                                        _pos++;
 
322
                                        _line++;
296
323
                                        return ByteBuffer.wrap( _content, initial_pos,
297
324
                                                to - initial_pos );
298
325
                                }
301
328
                        if( _pos != initial_pos ) {
302
329
                                int to = _pos;
303
330
                                _pos++;
 
331
                                _line++;
304
332
                                return ByteBuffer.wrap( _content, initial_pos,
305
333
                                        to - initial_pos );
306
334
                        }
323
351
                public boolean doesNextLineLookFolded()
324
352
                {
325
353
                        return _pos > 0 && _pos < _content.length &&
326
 
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
 
354
                                _content[ _pos - 1 ] == '\n' &&
 
355
                                ( _content[ _pos ] == ' ' || _content[ _pos ] == '\t' );
 
356
                }
 
357
 
 
358
                public int getLineNumber()
 
359
                {
 
360
                        return _line;
327
361
                }
328
362
        }
329
363
 
330
 
        private class VCard extends ContactData
 
364
        private class Vcard extends ContactData
331
365
        {
332
366
                private final static int NAMELEVEL_NONE = 0;
333
 
                private final static int NAMELEVEL_ORG = 1;
 
367
                private final static int NAMELEVEL_N = 1;
334
368
                private final static int NAMELEVEL_FN = 2;
335
 
                private final static int NAMELEVEL_N = 3;
 
369
 
 
370
                private final static int MULTILINE_NONE = 0;
 
371
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
 
372
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
 
373
                private final static int MULTILINE_FOLDED = 3;  // MIME-DIR folding
336
374
 
337
375
                private String _version = null;
338
376
                private Vector< ByteBuffer > _buffers = null;
339
377
                private int _name_level = NAMELEVEL_NONE;
340
 
                private boolean _parser_in_encoded_multiline = false;
341
 
                private boolean _parser_in_folded_multiline = false;
 
378
                private int _parser_multiline_state = MULTILINE_NONE;
342
379
                private String _parser_current_name_and_params = null;
343
380
                private String _parser_buffered_value_so_far = "";
 
381
                private String _cached_organisation = null;
 
382
                private String _cached_title = null;
344
383
 
345
384
                protected class UnencodeResult
346
385
                {
376
415
 
377
416
                        public ParseException( int res )
378
417
                        {
379
 
                                super( VCFImporter.this.getText( res ).toString() );
 
418
                                super( VcardImporter.this.getText( res ).toString() );
380
419
                        }
381
420
                }
382
421
 
383
422
                @SuppressWarnings("serial")
384
 
                protected class SkipContactException extends Exception { }
 
423
                protected class SkipImportException extends Exception { }
385
424
 
386
425
                private String extractCollonPartFromLine( ByteBuffer buffer,
387
426
                        String line, boolean former )
423
462
 
424
463
                public void parseLine( ByteBuffer buffer, String line,
425
464
                        boolean next_line_looks_folded )
426
 
                        throws ParseException, SkipContactException,
 
465
                        throws ParseException, SkipImportException,
427
466
                        AbortImportException
428
467
                {
429
468
                        // do we have a version yet?
435
474
 
436
475
                                // is it a version line?
437
476
                                if( name_and_params != null &&
438
 
                                        name_and_params.equals( "VERSION" ) )
 
477
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
439
478
                                {
440
479
                                        // yes, get it!
441
480
                                        String value = extractValueFromLine( buffer, line );
465
504
                        else
466
505
                        {
467
506
                                // name and params and the position in the buffer where the
468
 
                                // "value" part of the line start
 
507
                                // "value" part of the line starts
469
508
                                String name_and_params;
470
509
                                int pos;
471
510
 
472
 
                                if( _parser_in_encoded_multiline ||
473
 
                                        _parser_in_folded_multiline )
 
511
                                if( _parser_multiline_state != MULTILINE_NONE )
474
512
                                {
475
513
                                        // if we're currently in a multi-line value, use the stored
476
514
                                        // property name and parameters
477
515
                                        name_and_params = _parser_current_name_and_params;
478
516
 
 
517
                                        // skip some initial line characters, depending on the type
 
518
                                        // of multi-line we're handling
479
519
                                        pos = buffer.position();
480
 
 
481
 
                                        // for folded multi-lines, skip the single space at the
482
 
                                        // start of the next line
483
 
                                        if( _parser_in_folded_multiline )
 
520
                                        switch( _parser_multiline_state )
 
521
                                        {
 
522
                                        case MULTILINE_FOLDED:
484
523
                                                pos++;
485
 
 
486
 
                                        // else, this must be an encoded multi-line, so skip any
487
 
                                        // whitespace we find at the start of the next line
488
 
                                        else
 
524
                                                break;
 
525
                                        case MULTILINE_ENCODED:
489
526
                                                while( pos < buffer.limit() && (
490
527
                                                        buffer.get( pos ) == ' ' ||
491
528
                                                        buffer.get( pos ) == '\t' ) )
492
529
                                                {
493
530
                                                        pos++;
494
531
                                                }
 
532
                                                break;
 
533
                                        default:
 
534
                                                // do nothing
 
535
                                        }
 
536
 
 
537
                                        // take us out of multi-line so that we can re-detect that
 
538
                                        // this line is a multi-line or not
 
539
                                        _parser_multiline_state = MULTILINE_NONE;
495
540
                                }
496
541
                                else
497
542
                                {
 
543
                                        // skip empty lines
 
544
                                        if( line.trim().length() == 0 ) return;
 
545
 
498
546
                                        // get name and params from line, and since we're not
499
547
                                        // parsing a subsequent line in a multi-line, this should
500
548
                                        // not fail, or it's an error
523
571
                                for( int i = 0; i < name_param_parts.length; i++ )
524
572
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
525
573
 
 
574
                                // determine whether we care about this entry
 
575
                                final HashSet< String > interesting_fields =
 
576
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
577
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
578
                                ) );
 
579
                                boolean is_interesting_field =
 
580
                                        interesting_fields.contains(
 
581
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
582
 
526
583
                                // parse encoding parameter
527
584
                                String encoding = checkParam( name_param_parts, "ENCODING" );
528
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
529
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
530
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
531
 
                                        //&& !encoding.equals( "BASE64" ) )
 
585
                                if( encoding != null )
 
586
                                        encoding = encoding.toUpperCase( Locale.US );
 
587
                                if( is_interesting_field && encoding != null &&
 
588
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
 
589
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
590
                                        //&& !encoding.equalsIgnoreCase( "BASE64" ) )
532
591
                                {
533
592
                                        throw new ParseException( R.string.error_vcf_encoding );
534
593
                                }
535
594
 
536
595
                                // parse charset parameter
537
596
                                String charset = checkParam( name_param_parts, "CHARSET" );
538
 
                                if( charset != null ) charset = charset.toUpperCase();
539
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
540
 
                                        !charset.equals( "ASCII" ) &&
541
 
                                        !charset.equals( "UTF-8" ) )
 
597
                                if( charset != null )
 
598
                                        charset = charset.toUpperCase( Locale.US );
 
599
                                if( charset != null &&
 
600
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
 
601
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
 
602
                                        !charset.equalsIgnoreCase( "UTF-8" ) )
542
603
                                {
543
604
                                        throw new ParseException( R.string.error_vcf_charset );
544
605
                                }
546
607
                                // do unencoding (or default to a fake unencoding result with
547
608
                                // the raw string)
548
609
                                UnencodeResult unencoding_result = null;
549
 
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
610
                                if( encoding != null &&
 
611
                                        encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
 
612
                                {
550
613
                                        unencoding_result = unencodeQuotedPrintable( value );
551
 
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
 
614
                                }
 
615
//                              else if( encoding != null &&
 
616
//                                      encoding.equalsIgnoreCase( "BASE64" ) )
 
617
//                              {
552
618
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
 
619
//                              }
553
620
                                if( unencoding_result != null ) {
554
621
                                        value = unencoding_result.getBuffer();
555
 
                                        _parser_in_encoded_multiline =
556
 
                                                unencoding_result.isAnotherLineRequired();
 
622
                                        if( unencoding_result.isAnotherLineRequired() )
 
623
                                                _parser_multiline_state = MULTILINE_ENCODED;
557
624
                                }
558
625
 
559
 
                                // convert 8-bit ASCII charset to US-ASCII
560
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
626
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
627
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
628
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
629
                                        ( charset != null && (
 
630
                                                charset.equalsIgnoreCase( "ASCII" ) ||
 
631
                                                charset.equalsIgnoreCase( "US-ASCII" ) ) ) )
 
632
                                {
561
633
                                        value = transcodeAsciiToUtf8( value );
562
 
                                        charset = "UTF-8";
563
634
                                }
564
635
 
565
 
                                // process charset
 
636
                                // process charset (value is now in UTF-8)
566
637
                                String string_value;
567
638
                                try {
568
639
                                        string_value = new String( value.array(), value.position(),
569
 
                                                value.limit() - value.position(), charset );
 
640
                                                value.limit() - value.position(), "UTF-8" );
570
641
                                } catch( UnsupportedEncodingException e ) {
571
642
                                        throw new ParseException( R.string.error_vcf_charset );
572
643
                                }
573
644
 
574
 
                                // now we know whether we're in an encoding multi-line,
575
 
                                // determine if we're in a v3 folded multi-line or not
576
 
                                _parser_in_folded_multiline = !_parser_in_encoded_multiline &&
577
 
                                        _version.equals( "3.0" ) && next_line_looks_folded;
578
 
 
579
 
                                // handle multi-line requests
580
 
                                if( _parser_in_encoded_multiline ||
581
 
                                        _parser_in_folded_multiline )
582
 
                                {
 
645
                                // for some entries that have semicolon-separated value parts,
 
646
                                // check to see if the value ends in an escape character, which
 
647
                                // indicates that we have a multi-line value
 
648
                                if( ( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) ||
 
649
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) ||
 
650
                                        name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) ) &&
 
651
                                        doesStringEndInAnEscapeChar( string_value ) )
 
652
                                {
 
653
                                        _parser_multiline_state = MULTILINE_ESCAPED;
 
654
                                        string_value = string_value.substring( 0,
 
655
                                                string_value.length() - 1 );
 
656
                                }
 
657
 
 
658
                                // if we know we're not in an encoding-based multi-line, check
 
659
                                // to see if we're in a folded multi-line
 
660
                                if( _parser_multiline_state == MULTILINE_NONE &&
 
661
                                        next_line_looks_folded )
 
662
                                {
 
663
                                        _parser_multiline_state = MULTILINE_FOLDED;
 
664
                                }
 
665
 
 
666
                                // handle multi-lines by buffering them and parsing them when we
 
667
                                // are processing the last line in a multi-line sequence
 
668
                                if( _parser_multiline_state != MULTILINE_NONE ) {
583
669
                                        _parser_buffered_value_so_far += string_value;
584
670
                                        return;
585
671
                                }
586
 
 
587
 
                                // add on buffered multi-line content
588
672
                                String complete_value =
589
 
                                        _parser_buffered_value_so_far + string_value;
 
673
                                        ( _parser_buffered_value_so_far + string_value ).trim();
590
674
 
591
675
                                // ignore empty values
592
676
                                if( complete_value.length() < 1 ) return;
593
677
 
594
678
                                // parse some properties
595
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
 
679
                                if( name_param_parts[ 0 ].equalsIgnoreCase( "N" ) )
596
680
                                        parseN( name_param_parts, complete_value );
597
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
 
681
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "FN" ) )
598
682
                                        parseFN( name_param_parts, complete_value );
599
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
 
683
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ORG" ) )
600
684
                                        parseORG( name_param_parts, complete_value );
601
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
 
685
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TITLE" ) )
 
686
                                        parseTITLE( name_param_parts, complete_value );
 
687
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "TEL" ) )
602
688
                                        parseTEL( name_param_parts, complete_value );
603
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
 
689
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "EMAIL" ) )
604
690
                                        parseEMAIL( name_param_parts, complete_value );
605
 
                        }
 
691
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "ADR" ) )
 
692
                                        parseADR( name_param_parts, complete_value );
 
693
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "LABEL" ) )
 
694
                                        parseLABEL( name_param_parts, complete_value );
 
695
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
 
696
                                        parseNOTE( name_param_parts, complete_value );
 
697
                        }
 
698
                }
 
699
 
 
700
                private boolean doesStringEndInAnEscapeChar( String string )
 
701
                {
 
702
                        // count the number of backslashes at the end of the string
 
703
                        int count = 0;
 
704
                        for( int a = string.length() - 1; a >= 0; a-- )
 
705
                                if( string.charAt( a ) == '\\' )
 
706
                                        count++;
 
707
                                else
 
708
                                        break;
 
709
 
 
710
                        // if there are an even number of backslashes then the final one
 
711
                        // doesn't count
 
712
                        return ( count & 1 ) == 1;
 
713
                }
 
714
 
 
715
                private String[] splitValueByCharacter( String value, char character )
 
716
                {
 
717
                        // split string in to parts by specified character
 
718
                        ArrayList< String > parts = new ArrayList< String >(
 
719
                                Arrays.asList( value.split( "" + character ) ) );
 
720
 
 
721
                        // go through parts
 
722
                        for( int a = 0; a < parts.size(); a++ )
 
723
                        {
 
724
                                String str = parts.get( a );
 
725
 
 
726
                                // look for parts that end in an escape character, but ignore
 
727
                                // the final part. We've already detected escape chars at the
 
728
                                // end of the final part in parseLine() and handled multi-lines
 
729
                                // accordingly.
 
730
                                if( a < parts.size() - 1 &&
 
731
                                        doesStringEndInAnEscapeChar( str ) )
 
732
                                {
 
733
                                        // append the escaped character, join the next part to this
 
734
                                        // part and remove the next part
 
735
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
 
736
                                                character + parts.get( a + 1 ) );
 
737
                                        parts.remove( a + 1 );
 
738
 
 
739
                                        // re-visit this part
 
740
                                        a--;
 
741
                                        continue;
 
742
                                }
 
743
 
 
744
                                // trim and replace string
 
745
                                str = str.trim();
 
746
                                parts.set( a, str );
 
747
                        }
 
748
 
 
749
                        String[] ret = new String[ parts.size() ];
 
750
                        return parts.toArray( ret );
 
751
                }
 
752
 
 
753
                private String unescapeValue( String value )
 
754
                {
 
755
                        StringBuilder ret = new StringBuilder( value.length() );
 
756
                        boolean in_escape = false;
 
757
                        for( int a = 0; a < value.length(); a++ )
 
758
                        {
 
759
                                int c = value.codePointAt( a );
 
760
 
 
761
                                // process a normal character
 
762
                                if( !in_escape ) {
 
763
                                        if( c == '\\' )
 
764
                                                in_escape = true;
 
765
                                        else
 
766
                                                ret.append( Character.toChars( c ) );
 
767
                                        continue;
 
768
                                }
 
769
 
 
770
                                // process an escape sequence
 
771
                                in_escape = false;
 
772
                                switch( c )
 
773
                                {
 
774
                                case 'T':
 
775
                                case 't':
 
776
                                        // add tab (invalid/non-standard, but accepted)
 
777
                                        ret.append( '\t' );
 
778
                                        break;
 
779
                                case 'N':
 
780
                                case 'n':
 
781
                                        // add newline
 
782
                                        ret.append( '\n' );
 
783
                                        break;
 
784
                                case '\\':
 
785
                                case ',':
 
786
                                case ';':
 
787
                                        // add escaped character
 
788
                                        ret.append( Character.toChars( c ) );
 
789
                                        break;
 
790
                                default:
 
791
                                        // unknown escape sequence, so add it unescaped
 
792
                                        // (invalid/non-standard, but accepted)
 
793
                                        ret.append( "\\" );
 
794
                                        ret.append( Character.toChars( c ) );
 
795
                                        break;
 
796
                                }
 
797
                        }
 
798
 
 
799
                        return ret.toString();
606
800
                }
607
801
 
608
802
                private void parseN( String[] params, String value )
609
 
                        throws ParseException, SkipContactException,
610
 
                        AbortImportException
611
803
                {
612
804
                        // already got a better name?
613
805
                        if( _name_level >= NAMELEVEL_N ) return;
614
806
 
615
807
                        // get name parts
616
 
                        String[] name_parts = value.split( ";" );
617
 
                        for( int i = 0; i < name_parts.length; i++ )
618
 
                                name_parts[ i ] = name_parts[ i ].trim();
 
808
                        String[] name_parts = splitValueByCharacter( value, ';' );
619
809
 
620
810
                        // build name
621
811
                        value = "";
622
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
623
 
                                value += name_parts[ 1 ];
624
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
625
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
812
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
813
                        for( int a = 0; a < part_order.length; a++ )
 
814
                                if( name_parts.length > part_order[ a ] &&
 
815
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
816
                                {
 
817
                                        // split this part in to it's comma-separated bits
 
818
                                        String[] name_part_parts = splitValueByCharacter(
 
819
                                                name_parts[ part_order[ a ] ], ',' );
 
820
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
821
                                                if( name_part_parts[ b ].length() > 0 )
 
822
                                                {
 
823
                                                        if( value.length() == 0 ) value += " ";
 
824
                                                        value += name_part_parts[ b ];
 
825
                                                }
 
826
                                }
626
827
 
627
828
                        // set name
628
 
                        setName( value );
 
829
                        setName( unescapeValue( value ) );
629
830
                        _name_level = NAMELEVEL_N;
630
 
 
631
 
                        // check now to see if we need to import this contact (to avoid
632
 
                        // parsing the rest of the vCard unnecessarily)
633
 
                        if( !isImportRequired( getName() ) )
634
 
                                throw new SkipContactException();
635
831
                }
636
832
 
637
833
                private void parseFN( String[] params, String value )
638
 
                        throws ParseException, SkipContactException
639
834
                {
640
835
                        // already got a better name?
641
836
                        if( _name_level >= NAMELEVEL_FN ) return;
642
837
 
643
838
                        // set name
644
 
                        setName( value );
 
839
                        setName( unescapeValue( value ) );
645
840
                        _name_level = NAMELEVEL_FN;
646
841
                }
647
842
 
648
843
                private void parseORG( String[] params, String value )
649
 
                        throws ParseException, SkipContactException
650
844
                {
651
 
                        // already got a better name?
652
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
653
 
 
654
845
                        // get org parts
655
 
                        String[] org_parts = value.split( ";" );
656
 
                        for( int i = 0; i < org_parts.length; i++ )
657
 
                                org_parts[ i ] = org_parts[ i ].trim();
658
 
 
659
 
                        // build name
660
 
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
661
 
                                value = org_parts[ 1 ];
662
 
                        else
663
 
                                value = org_parts[ 0 ];
664
 
 
665
 
                        // set name
666
 
                        setName( value );
667
 
                        _name_level = NAMELEVEL_ORG;
 
846
                        String[] org_parts = splitValueByCharacter( value, ';' );
 
847
                        if( org_parts == null || org_parts.length < 1 ) return;
 
848
 
 
849
                        // build organisation name
 
850
                        StringBuilder builder = new StringBuilder(
 
851
                                String.valueOf( org_parts[ 0 ] ) );
 
852
                        for( int a = 1; a < org_parts.length; a++ )
 
853
                                builder.append( ", " ).append( org_parts[ a ] );
 
854
                        String organisation = unescapeValue( builder.toString() );
 
855
 
 
856
                        // set organisation name (using a title we've previously found)
 
857
                        addOrganisation( organisation, _cached_title, true );
 
858
 
 
859
                        // if we've not previously found a title, store this organisation
 
860
                        // name (we'll need it when we find a title to update the
 
861
                        // organisation, by name), else if we *have* previously found a
 
862
                        // title, clear it (since we just used it)
 
863
                        if( _cached_title == null )
 
864
                                _cached_organisation = organisation;
 
865
                        else
 
866
                                _cached_title = null;
 
867
                }
 
868
 
 
869
                private void parseTITLE( String[] params, String value )
 
870
                {
 
871
                        value = unescapeValue( value );
 
872
 
 
873
                        // if we previously had an organisation, look it up and append this
 
874
                        // title to it
 
875
                        if( _cached_organisation != null && hasOrganisations() ) {
 
876
                                HashMap< String, ExtraDetail > datas = getOrganisations();
 
877
                                ExtraDetail detail = datas.get( _cached_organisation );
 
878
                                if( detail != null )
 
879
                                        detail.setExtra( value );
 
880
                        }
 
881
 
 
882
                        // same as when handling organisation, if we've not previously found
 
883
                        // an organisation we store this title, else we clear it (since we
 
884
                        // just appended this title to it)
 
885
                        if( _cached_organisation == null )
 
886
                                _cached_title = value;
 
887
                        else
 
888
                                _cached_organisation = null;
668
889
                }
669
890
 
670
891
                private void parseTEL( String[] params, String value )
671
 
                        throws ParseException
672
892
                {
673
893
                        if( value.length() == 0 ) return;
674
894
 
677
897
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
678
898
 
679
899
                        // here's the logic...
680
 
                        boolean preferred = types.contains( "PREF" );
681
 
                        int type = PhonesColumns.TYPE_MOBILE;
682
 
                        if( types.contains( "VOICE" ) )
683
 
                                if( types.contains( "WORK" ) )
684
 
                                        type = PhonesColumns.TYPE_WORK;
685
 
                                else
686
 
                                        type = PhonesColumns.TYPE_HOME;
687
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
688
 
                                type = PhonesColumns.TYPE_MOBILE;
 
900
                        boolean is_preferred = types.contains( "PREF" );
 
901
                        int type;
689
902
                        if( types.contains( "FAX" ) )
690
903
                                if( types.contains( "HOME" ) )
691
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
904
                                        type = TYPE_FAX_HOME;
692
905
                                else
693
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
694
 
                        if( types.contains( "PAGER" ) )
695
 
                                type = PhonesColumns.TYPE_PAGER;
 
906
                                        type = TYPE_FAX_WORK;
 
907
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
908
                                type = TYPE_MOBILE;
 
909
                        else if( types.contains( "PAGER" ) )
 
910
                                type = TYPE_PAGER;
 
911
                        else if( types.contains( "WORK" ) )
 
912
                                type = TYPE_WORK;
 
913
                        else
 
914
                                type = TYPE_HOME;
696
915
 
697
916
                        // add phone number
698
 
                        addPhone( value, type, preferred );
 
917
                        addNumber( value, type, is_preferred );
699
918
                }
700
919
 
701
920
                public void parseEMAIL( String[] params, String value )
702
 
                        throws ParseException
703
921
                {
704
922
                        if( value.length() == 0 ) return;
705
923
 
706
924
                        Set< String > types = extractTypes( params, Arrays.asList(
707
925
                                "PREF", "WORK", "HOME", "INTERNET" ) );
708
926
 
709
 
                        // here's the logic...
710
 
                        boolean preferred = types.contains( "PREF" );
711
 
                        if( types.contains( "WORK" ) )
712
 
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
713
 
                        else
714
 
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
715
 
                }
716
 
 
717
 
                public void finaliseParsing()
718
 
                        throws ParseException, SkipContactException,
719
 
                        AbortImportException
 
927
                        // add email address
 
928
                        boolean is_preferred = types.contains( "PREF" );
 
929
                        int type;
 
930
                        if( types.contains( "WORK" ) )
 
931
                                type = TYPE_WORK;
 
932
                        else
 
933
                                type = TYPE_HOME;
 
934
 
 
935
                        addEmail( unescapeValue( value ), type, is_preferred );
 
936
                }
 
937
 
 
938
                private void parseADR( String[] params, String value )
 
939
                {
 
940
                        // get address parts
 
941
                        String[] adr_parts = splitValueByCharacter( value, ';' );
 
942
 
 
943
                        // build address
 
944
                        value = "";
 
945
                        for( int a = 0; a < adr_parts.length; a++ )
 
946
                                if( adr_parts[ a ].length() > 0 )
 
947
                                {
 
948
                                        // version 3.0 vCards allow further splitting by comma
 
949
                                        if( _version.equals( "3.0" ) )
 
950
                                        {
 
951
                                                // split this part in to it's comma-separated bits and
 
952
                                                // add them on individual lines
 
953
                                                String[] adr_part_parts =
 
954
                                                        splitValueByCharacter( adr_parts[ a ], ',' );
 
955
                                                for( int b = 0; b < adr_part_parts.length; b++ )
 
956
                                                        if( adr_part_parts[ b ].length() > 0 )
 
957
                                                        {
 
958
                                                                if( value.length() > 0 ) value += "\n";
 
959
                                                                value += adr_part_parts[ b ];
 
960
                                                        }
 
961
                                        }
 
962
                                        else
 
963
                                        {
 
964
                                                // add this part on an individual line
 
965
                                                if( value.length() > 0 ) value += "\n";
 
966
                                                value += adr_parts[ a ];
 
967
                                        }
 
968
                                }
 
969
 
 
970
                        Set< String > types = extractTypes( params, Arrays.asList(
 
971
                                "PREF", "WORK", "HOME" ) );
 
972
 
 
973
                        // add address
 
974
                        int type;
 
975
                        if( types.contains( "WORK" ) )
 
976
                                type = TYPE_WORK;
 
977
                        else
 
978
                                type = TYPE_HOME;
 
979
 
 
980
                        addAddress( unescapeValue( value ), type );
 
981
                }
 
982
 
 
983
                private void parseLABEL( String[] params, String value )
 
984
                {
 
985
                        Set< String > types = extractTypes( params, Arrays.asList(
 
986
                                "PREF", "WORK", "HOME" ) );
 
987
 
 
988
                        // add address
 
989
                        int type;
 
990
                        if( types.contains( "WORK" ) )
 
991
                                type = TYPE_WORK;
 
992
                        else
 
993
                                type = TYPE_HOME;
 
994
 
 
995
                        addAddress( unescapeValue( value ), type );
 
996
                }
 
997
 
 
998
                private void parseNOTE( String[] params, String value )
 
999
                {
 
1000
                        addNote( unescapeValue( value ) );
 
1001
                }
 
1002
 
 
1003
                public void finaliseVcard()
 
1004
                        throws ParseException, ContactNotIdentifiableException
720
1005
                {
721
1006
                        // missing version (and data is present)
722
1007
                        if( _version == null && _buffers != null )
723
1008
                                throw new ParseException( R.string.error_vcf_malformed );
724
1009
 
725
 
                        //  missing name properties?
726
 
                        if( _name_level == NAMELEVEL_NONE )
727
 
                                throw new ParseException( R.string.error_vcf_noname );
728
 
 
729
 
                        // check if we should import this one? If we've already got an 'N'-
730
 
                        // type name, this will already have been done by parseN() so we
731
 
                        // mustn't do this here (or it could prompt twice!)
732
 
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
733
 
                                throw new SkipContactException();
 
1010
                        // finalise the parent class
 
1011
                        finalise();
734
1012
                }
735
1013
 
 
1014
                /**
 
1015
                 * Amongst the params, find the value of the first, only, of any with
 
1016
                 * the specified name
 
1017
                 * @param params
 
1018
                 * @param name
 
1019
                 * @return a value, or null
 
1020
                 */
736
1021
                private String checkParam( String[] params, String name )
737
1022
                {
 
1023
                        String[] res = checkParams( params, name );
 
1024
                        return res.length > 0? res[ 0 ] : null;
 
1025
                }
 
1026
 
 
1027
                /**
 
1028
                 * Amongst the params, find the values of any with the specified name
 
1029
                 * @param params
 
1030
                 * @param name
 
1031
                 * @return an array of values, or null
 
1032
                 */
 
1033
                private String[] checkParams( String[] params, String name )
 
1034
                {
 
1035
                        HashSet< String > ret = new HashSet< String >();
 
1036
 
738
1037
                        Pattern p = Pattern.compile(
739
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
 
1038
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
 
1039
                                Pattern.CASE_INSENSITIVE );
740
1040
                        for( int i = 0; i < params.length; i++ ) {
741
1041
                                Matcher m = p.matcher( params[ i ] );
742
1042
                                if( m.matches() )
743
 
                                        return m.group( 2 );
 
1043
                                        ret.add( m.group( 2 ) );
744
1044
                        }
745
 
                        return null;
 
1045
 
 
1046
                        return (String[]) ret.toArray( new String[ ret.size() ] );
746
1047
                }
747
1048
 
 
1049
                /**
 
1050
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1051
                 * those types are just parameters. For v3.0, they are prefixed with
 
1052
                 * "TYPE=". There may also be multiple type parameters.
 
1053
                 * @param params an array of params to look for types in
 
1054
                 * @param valid_types an list of upper-case type values to look for
 
1055
                 * @return a set of present type values
 
1056
                 */
748
1057
                private Set< String > extractTypes( String[] params,
749
1058
                        List< String > valid_types )
750
1059
                {
751
1060
                        HashSet< String > types = new HashSet< String >();
752
1061
 
753
1062
                        // get 3.0-style TYPE= param
754
 
                        String type_param;
755
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
756
 
                                String[] parts = type_param.split( "," );
757
 
                                for( int i = 0; i < parts.length; i++ )
758
 
                                        if( valid_types.contains( parts[ i ] ) )
759
 
                                                types.add( parts[ i ] );
 
1063
                        String type_params[] = checkParams( params, "TYPE" );
 
1064
                        for( int a = 0; a < type_params.length; a++ )
 
1065
                        {
 
1066
                                // check for a comma-separated list of types (why? I don't think
 
1067
                                // this is in the specs!)
 
1068
                                String[] parts = type_params[ a ].split( "," );
 
1069
                                for( int i = 0; i < parts.length; i++ ) {
 
1070
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1071
                                        if( valid_types.contains( ucpart ) )
 
1072
                                                types.add( ucpart );
 
1073
                                }
760
1074
                        }
761
1075
 
762
1076
                        // get 2.1-style type param
763
1077
                        if( _version.equals( "2.1" ) ) {
764
 
                                for( int i = 1; i < params.length; i++ )
765
 
                                        if( valid_types.contains( params[ i ] ) )
766
 
                                                types.add( params[ i ] );
 
1078
                                for( int i = 1; i < params.length; i++ ) {
 
1079
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1080
                                        if( valid_types.contains( ucparam ) )
 
1081
                                                types.add( ucparam );
 
1082
                                }
767
1083
                        }
768
1084
 
769
1085
                        return types;