/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/org/waxworlds/edam/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2011-06-04 13:16:20 UTC
  • Revision ID: edam@waxworlds.org-20110604131620-hean9j66r3f5i72h
- fix UTF-8 unencoding by default on v3.0 vCards
- proper unescaping of strings in v3.0 vCards (also works for v2.1, although the 2.1 specs don't mention it)

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 <edam@waxworlds.org>
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
34
34
import java.nio.ByteBuffer;
35
35
import java.util.ArrayList;
36
36
import java.util.Arrays;
 
37
import java.util.HashMap;
37
38
import java.util.HashSet;
38
39
import java.util.Iterator;
39
40
import java.util.List;
47
48
import android.provider.Contacts;
48
49
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
        }
109
110
                        countVCardFile( files[ i ] );
110
111
                        setTmpProgress( i );
111
112
                }
112
 
                setProgressMax( _vCardCount );  // will also update tmp progress
 
113
                setProgressMax( _vcard_count ); // will also update tmp progress
113
114
 
114
115
                // import them
115
116
                setProgress( 0 );
116
117
                for( int i = 0; i < files.length; i++ )
117
118
                        importVCardFile( files[ i ] );
 
119
                setProgress( _vcard_count );
118
120
        }
119
121
 
120
122
        private void countVCardFile( File file ) throws AbortImportException
127
129
 
128
130
                        // read
129
131
                        String line;
130
 
                        boolean inVCard = false;
 
132
                        boolean in_vcard = false;
131
133
                        while( ( line = reader.readLine() ) != null )
132
134
                        {
133
 
                                if( !inVCard ) {
 
135
                                if( !in_vcard ) {
134
136
                                        // look for vcard beginning
135
137
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
136
 
                                                inVCard = true;
137
 
                                                _vCardCount++;
 
138
                                                in_vcard = true;
 
139
                                                _vcard_count++;
138
140
                                        }
139
141
                                }
140
142
                                else if( line.matches( "^END:VCARD" ) )
141
 
                                        inVCard = false;
 
143
                                        in_vcard = false;
142
144
                        }
143
145
 
144
146
                }
167
169
                        FileInputStream istream = new FileInputStream( file );
168
170
                        byte[] content = new byte[ (int)file.length() ];
169
171
                        istream.read( content );
 
172
                        istream = null;
170
173
 
171
174
                        // import
172
175
                        importVCardFileContent( content, file.getName() );
184
187
                throws AbortImportException
185
188
        {
186
189
                // go through lines
187
 
                VCard vCard = null;
 
190
                Vcard vcard = null;
 
191
                int vcard_start_line = 0;
188
192
                ContentLineIterator cli = new ContentLineIterator( content );
189
193
                while( cli.hasNext() )
190
194
                {
201
205
                                line = "";
202
206
                        }
203
207
 
204
 
                        if( vCard == null ) {
 
208
                        if( vcard == null ) {
205
209
                                // look for vcard beginning
206
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
207
 
                                        setProgress( ++_progress );
208
 
                                        vCard = new VCard();
 
211
                                        setProgress( _progress++ );
 
212
                                        vcard = new Vcard();
 
213
                                        vcard_start_line = cli.getLineNumber();
209
214
                                }
210
215
                        }
211
216
                        else {
212
217
                                // look for vcard content or ending
213
218
                                if( line.matches( "^END:VCARD" ) )
214
219
                                {
215
 
                                        // store vcard and do away with it
 
220
                                        // finalise the vcard/contact
216
221
                                        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;
 
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
 
225
                                                importContact( vcard );
 
226
                                        }
 
227
                                        catch( Vcard.ParseException e ) {
 
228
                                                if( !showContinue(
 
229
                                                        getText( R.string.error_vcf_parse ).toString()
 
230
                                                        + fileName +
 
231
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
232
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
233
                                                {
 
234
                                                        finish( ACTION_ABORT );
 
235
                                                }
 
236
                                                else
 
237
                                                        skipContact();
 
238
                                        }
 
239
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
240
                                                if( !showContinue(
 
241
                                                        getText( R.string.error_vcf_parse ).toString()
 
242
                                                        + fileName +
 
243
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
244
                                                        + vcard_start_line + ":\n" + getText(
 
245
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
246
                                                ) )
 
247
                                                {
 
248
                                                        finish( ACTION_ABORT );
 
249
                                                }
 
250
                                                else
 
251
                                                        skipContact();
 
252
                                        }
 
253
 
 
254
                                        // discard this vcard
 
255
                                        vcard = null;
234
256
                                }
235
257
                                else
236
258
                                {
237
259
                                        // try giving the line to the vcard
238
260
                                        try {
239
 
                                                vCard.parseLine( buffer, line,
 
261
                                                vcard.parseLine( buffer, line,
240
262
                                                        cli.doesNextLineLookFolded() );
241
263
                                        }
242
 
                                        catch( VCard.ParseException e ) {
 
264
                                        catch( Vcard.ParseException e ) {
243
265
                                                skipContact();
244
266
                                                if( !showContinue(
245
267
                                                        getText( R.string.error_vcf_parse ).toString()
246
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
268
                                                        + fileName +
 
269
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
270
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
247
271
                                                {
248
272
                                                        finish( ACTION_ABORT );
249
273
                                                }
251
275
                                                // although we're continuing, we still need to abort
252
276
                                                // this vCard. Further lines will be ignored until we
253
277
                                                // get to another BEGIN:VCARD line.
254
 
                                                vCard = null;
 
278
                                                vcard = null;
255
279
                                        }
256
 
                                        catch( VCard.SkipContactException e ) {
 
280
                                        catch( Vcard.SkipImportException e ) {
257
281
                                                skipContact();
258
282
                                                // abort this vCard. Further lines will be ignored until
259
283
                                                // we get to another BEGIN:VCARD line.
260
 
                                                vCard = null;
 
284
                                                vcard = null;
261
285
                                        }
262
286
                                }
263
287
                        }
268
292
        {
269
293
                protected byte[] _content = null;
270
294
                protected int _pos = 0;
 
295
                protected int _line = 0;
271
296
 
272
297
                public ContentLineIterator( byte[] content )
273
298
                {
293
318
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
294
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
295
320
                                        _pos++;
 
321
                                        _line++;
296
322
                                        return ByteBuffer.wrap( _content, initial_pos,
297
323
                                                to - initial_pos );
298
324
                                }
301
327
                        if( _pos != initial_pos ) {
302
328
                                int to = _pos;
303
329
                                _pos++;
 
330
                                _line++;
304
331
                                return ByteBuffer.wrap( _content, initial_pos,
305
332
                                        to - initial_pos );
306
333
                        }
325
352
                        return _pos > 0 && _pos < _content.length &&
326
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
327
354
                }
 
355
 
 
356
                public int getLineNumber()
 
357
                {
 
358
                        return _line;
 
359
                }
328
360
        }
329
361
 
330
 
        private class VCard extends ContactData
 
362
        private class Vcard extends ContactData
331
363
        {
332
364
                private final static int NAMELEVEL_NONE = 0;
333
 
                private final static int NAMELEVEL_ORG = 1;
 
365
                private final static int NAMELEVEL_N = 1;
334
366
                private final static int NAMELEVEL_FN = 2;
335
 
                private final static int NAMELEVEL_N = 3;
336
367
 
337
368
                private final static int MULTILINE_NONE = 0;
338
369
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
345
376
                private int _parser_multiline_state = MULTILINE_NONE;
346
377
                private String _parser_current_name_and_params = null;
347
378
                private String _parser_buffered_value_so_far = "";
 
379
                private String _cached_organisation = null;
 
380
                private String _cached_title = null;
348
381
 
349
382
                protected class UnencodeResult
350
383
                {
380
413
 
381
414
                        public ParseException( int res )
382
415
                        {
383
 
                                super( VCFImporter.this.getText( res ).toString() );
 
416
                                super( VcardImporter.this.getText( res ).toString() );
384
417
                        }
385
418
                }
386
419
 
387
420
                @SuppressWarnings("serial")
388
 
                protected class SkipContactException extends Exception { }
 
421
                protected class SkipImportException extends Exception { }
389
422
 
390
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
391
424
                        String line, boolean former )
427
460
 
428
461
                public void parseLine( ByteBuffer buffer, String line,
429
462
                        boolean next_line_looks_folded )
430
 
                        throws ParseException, SkipContactException,
 
463
                        throws ParseException, SkipImportException,
431
464
                        AbortImportException
432
465
                {
433
466
                        // do we have a version yet?
533
566
                                for( int i = 0; i < name_param_parts.length; i++ )
534
567
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
535
568
 
 
569
                                // determine whether we care about this entry
 
570
                                final HashSet< String > interesting_fields =
 
571
                                        new HashSet< String >( Arrays.asList( new String[]
 
572
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
 
573
                                ) );
 
574
                                boolean is_interesting_field =
 
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
 
576
 
536
577
                                // parse encoding parameter
537
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
538
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
539
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
580
                                if( is_interesting_field && encoding != null &&
 
581
                                        !encoding.equals( "8BIT" ) &&
540
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
541
583
                                        //&& !encoding.equals( "BASE64" ) )
542
584
                                {
546
588
                                // parse charset parameter
547
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
548
590
                                if( charset != null ) charset = charset.toUpperCase();
549
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
591
                                if( charset != null &&
 
592
                                        !charset.equals( "US-ASCII" ) &&
550
593
                                        !charset.equals( "ASCII" ) &&
551
594
                                        !charset.equals( "UTF-8" ) )
552
595
                                {
566
609
                                                _parser_multiline_state = MULTILINE_ENCODED;
567
610
                                }
568
611
 
569
 
                                // convert 8-bit ASCII charset to US-ASCII
570
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
612
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
613
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
614
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
615
                                        ( charset != null && (
 
616
                                                charset.equals( "ASCII" ) ||
 
617
                                                charset.equals( "US-ASCII" ) ) ) )
 
618
                                {
571
619
                                        value = transcodeAsciiToUtf8( value );
572
 
                                        charset = "UTF-8";
573
620
                                }
574
621
 
575
 
                                // process charset
 
622
                                // process charset (value is now in UTF-8)
576
623
                                String string_value;
577
624
                                try {
578
625
                                        string_value = new String( value.array(), value.position(),
579
 
                                                value.limit() - value.position(), charset );
 
626
                                                value.limit() - value.position(), "UTF-8" );
580
627
                                } catch( UnsupportedEncodingException e ) {
581
628
                                        throw new ParseException( R.string.error_vcf_charset );
582
629
                                }
621
668
                                        parseFN( name_param_parts, complete_value );
622
669
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
623
670
                                        parseORG( name_param_parts, complete_value );
 
671
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
672
                                        parseTITLE( name_param_parts, complete_value );
624
673
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
625
674
                                        parseTEL( name_param_parts, complete_value );
626
675
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
645
694
                        return ( count & 1 ) == 1;
646
695
                }
647
696
 
648
 
                private String[] splitValueBySemicolon( String value )
 
697
                private String[] splitValueByCharacter( String value, char character )
649
698
                {
650
 
                        // split string in to parts by semicolon
 
699
                        // split string in to parts by specified character
651
700
                        ArrayList< String > parts = new ArrayList< String >(
652
 
                                Arrays.asList( value.split(  ";" ) ) );
 
701
                                Arrays.asList( value.split( "" + character ) ) );
653
702
 
654
703
                        // go through parts
655
704
                        for( int a = 0; a < parts.size(); a++ )
663
712
                                if( a < parts.size() - 1 &&
664
713
                                        doesStringEndInAnEscapeChar( str ) )
665
714
                                {
666
 
                                        // join the next part to this part and remove the next part
 
715
                                        // append the escaped character, join the next part to this
 
716
                                        // part and remove the next part
667
717
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
668
 
                                                ';' + parts.get( a + 1 ) );
 
718
                                                character + parts.get( a + 1 ) );
669
719
                                        parts.remove( a + 1 );
670
720
 
671
721
                                        // re-visit this part
682
732
                        return parts.toArray( ret );
683
733
                }
684
734
 
 
735
                private String unescapeValue( String value )
 
736
                {
 
737
                        StringBuilder ret = new StringBuilder( value.length() );
 
738
                        boolean in_escape = false;
 
739
                        for( int a = 0; a < value.length(); a++ )
 
740
                        {
 
741
                                int c = value.codePointAt( a );
 
742
 
 
743
                                // process a normal character
 
744
                                if( !in_escape ) {
 
745
                                        if( c == '\\' )
 
746
                                                in_escape = true;
 
747
                                        else
 
748
                                                ret.append( Character.toChars( c ) );
 
749
                                        continue;
 
750
                                }
 
751
 
 
752
                                // process an escape sequence
 
753
                                in_escape = false;
 
754
                                switch( c )
 
755
                                {
 
756
                                case 'N':
 
757
                                case 'n':
 
758
                                        // add newline
 
759
                                        ret.append( '\n' );
 
760
                                        break;
 
761
                                case '\\':
 
762
                                case ',':
 
763
                                case ';':
 
764
                                        // add escaped character
 
765
                                        ret.append( Character.toChars( c ) );
 
766
                                        break;
 
767
                                default:
 
768
                                        // unknown escape sequence, so add it unescaped
 
769
                                        ret.append( "\\" );
 
770
                                        ret.append( Character.toChars( c ) );
 
771
                                        break;
 
772
                                }
 
773
                        }
 
774
 
 
775
                        return ret.toString();
 
776
                }
 
777
 
685
778
                private void parseN( String[] params, String value )
686
 
                        throws ParseException, SkipContactException,
687
 
                        AbortImportException
688
779
                {
689
780
                        // already got a better name?
690
781
                        if( _name_level >= NAMELEVEL_N ) return;
691
782
 
692
783
                        // get name parts
693
 
                        String[] name_parts = splitValueBySemicolon( value );
 
784
                        String[] name_parts = splitValueByCharacter( value, ';' );
694
785
 
695
786
                        // build name
696
787
                        value = "";
697
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
698
 
                                value += name_parts[ 1 ];
699
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
700
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
788
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
789
                        for( int a = 0; a < part_order.length; a++ )
 
790
                                if( name_parts.length > part_order[ a ] &&
 
791
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
792
                                {
 
793
                                        // split this part in to it's comma-separated bits
 
794
                                        String[] name_part_parts = splitValueByCharacter(
 
795
                                                name_parts[ part_order[ a ] ], ',' );
 
796
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
797
                                                if( name_part_parts[ b ].length() > 0 )
 
798
                                                {
 
799
                                                        if( value.length() == 0 ) value += " ";
 
800
                                                        value += name_part_parts[ b ];
 
801
                                                }
 
802
                                }
701
803
 
702
804
                        // set name
703
 
                        setName( value );
 
805
                        setName( unescapeValue( value ) );
704
806
                        _name_level = NAMELEVEL_N;
705
 
 
706
 
                        // check now to see if we need to import this contact (to avoid
707
 
                        // parsing the rest of the vCard unnecessarily)
708
 
                        if( !isImportRequired( getName() ) )
709
 
                                throw new SkipContactException();
710
807
                }
711
808
 
712
809
                private void parseFN( String[] params, String value )
713
 
                        throws ParseException, SkipContactException
714
810
                {
715
811
                        // already got a better name?
716
812
                        if( _name_level >= NAMELEVEL_FN ) return;
717
813
 
718
814
                        // set name
719
 
                        setName( value );
 
815
                        setName( unescapeValue( value ) );
720
816
                        _name_level = NAMELEVEL_FN;
721
817
                }
722
818
 
723
819
                private void parseORG( String[] params, String value )
724
 
                        throws ParseException, SkipContactException
725
820
                {
726
 
                        // already got a better name?
727
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
728
 
 
729
821
                        // get org parts
730
 
                        String[] org_parts = splitValueBySemicolon( value );
731
 
 
732
 
                        // build name
733
 
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
734
 
                                value = org_parts[ 1 ];
735
 
                        else if( org_parts.length > 1 && org_parts[ 1 ].length() > 0 )
736
 
                                value = org_parts[ 0 ] + ", " + org_parts[ 1 ];
737
 
                        else
738
 
                                value = org_parts[ 0 ];
739
 
 
740
 
                        // set name
741
 
                        setName( value );
742
 
                        _name_level = NAMELEVEL_ORG;
 
822
                        String[] org_parts = splitValueByCharacter( value, ';' );
 
823
                        if( org_parts == null || org_parts.length < 1 ) return;
 
824
 
 
825
                        // build organisation name
 
826
                        StringBuilder builder = new StringBuilder(
 
827
                                String.valueOf( org_parts[ 0 ] ) );
 
828
                        for( int a = 1; a < org_parts.length; a++ )
 
829
                                builder.append( ", " ).append( org_parts[ a ] );
 
830
                        String organisation = unescapeValue( builder.toString() );
 
831
 
 
832
                        // set organisation name (using a title we've previously found)
 
833
                        addOrganisation( organisation, _cached_title, true );
 
834
 
 
835
                        // if we've not previously found a title, store this organisation
 
836
                        // name (we'll need it when we find a title to update the
 
837
                        // organisation, by name), else if we *have* previously found a
 
838
                        // title, clear it (since we just used it)
 
839
                        if( _cached_title == null )
 
840
                                _cached_organisation = organisation;
 
841
                        else
 
842
                                _cached_title = null;
 
843
                }
 
844
 
 
845
                private void parseTITLE( String[] params, String value )
 
846
                {
 
847
                        value = unescapeValue( value );
 
848
 
 
849
                        // if we previously had an organisation, look it up and append this
 
850
                        // title to it
 
851
                        if( _cached_organisation != null && hasOrganisations() ) {
 
852
                                HashMap< String, ExtraDetail > datas = getOrganisations();
 
853
                                ExtraDetail detail = datas.get( _cached_organisation );
 
854
                                if( detail != null )
 
855
                                        detail.setExtra( value );
 
856
                        }
 
857
 
 
858
                        // same as when handling organisation, if we've not previously found
 
859
                        // an organisation we store this title, else we clear it (since we
 
860
                        // just appended this title to it)
 
861
                        if( _cached_organisation == null )
 
862
                                _cached_title = value;
 
863
                        else
 
864
                                _cached_organisation = null;
743
865
                }
744
866
 
745
867
                private void parseTEL( String[] params, String value )
746
 
                        throws ParseException
747
868
                {
748
869
                        if( value.length() == 0 ) return;
749
870
 
752
873
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
753
874
 
754
875
                        // here's the logic...
755
 
                        boolean preferred = types.contains( "PREF" );
756
 
                        int type = PhonesColumns.TYPE_MOBILE;
757
 
                        if( types.contains( "VOICE" ) )
758
 
                                if( types.contains( "WORK" ) )
759
 
                                        type = PhonesColumns.TYPE_WORK;
760
 
                                else
761
 
                                        type = PhonesColumns.TYPE_HOME;
762
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
763
 
                                type = PhonesColumns.TYPE_MOBILE;
 
876
                        boolean is_preferred = types.contains( "PREF" );
 
877
                        int type;
764
878
                        if( types.contains( "FAX" ) )
765
879
                                if( types.contains( "HOME" ) )
766
880
                                        type = PhonesColumns.TYPE_FAX_HOME;
767
881
                                else
768
882
                                        type = PhonesColumns.TYPE_FAX_WORK;
769
 
                        if( types.contains( "PAGER" ) )
 
883
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
884
                                type = PhonesColumns.TYPE_MOBILE;
 
885
                        else if( types.contains( "PAGER" ) )
770
886
                                type = PhonesColumns.TYPE_PAGER;
 
887
                        else if( types.contains( "WORK" ) )
 
888
                                type = PhonesColumns.TYPE_WORK;
 
889
                        else
 
890
                                type = PhonesColumns.TYPE_HOME;
771
891
 
772
892
                        // add phone number
773
 
                        addPhone( value, type, preferred );
 
893
                        addNumber( value, type, is_preferred );
774
894
                }
775
895
 
776
896
                public void parseEMAIL( String[] params, String value )
777
 
                        throws ParseException
778
897
                {
779
898
                        if( value.length() == 0 ) return;
780
899
 
782
901
                                "PREF", "WORK", "HOME", "INTERNET" ) );
783
902
 
784
903
                        // add email address
785
 
                        boolean preferred = types.contains( "PREF" );
 
904
                        boolean is_preferred = types.contains( "PREF" );
 
905
                        int type;
786
906
                        if( types.contains( "WORK" ) )
787
 
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
 
907
                                type = Contacts.ContactMethods.TYPE_WORK;
788
908
                        else
789
 
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
 
909
                                type = Contacts.ContactMethods.TYPE_HOME;
 
910
 
 
911
                        addEmail( unescapeValue( value ), type, is_preferred );
790
912
                }
791
913
 
792
914
                private void parseADR( String[] params, String value )
793
 
                        throws ParseException, SkipContactException
794
915
                {
795
916
                        // get address parts
796
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
917
                        String[] adr_parts = splitValueByCharacter( value, ';' );
797
918
 
798
919
                        // build address
799
920
                        value = "";
800
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
801
 
                                if( value.length() > 0 ) value += "\n";
802
 
                                value += adr_parts[ a ].trim();
803
 
                        }
 
921
                        for( int a = 0; a < adr_parts.length; a++ )
 
922
                                if( adr_parts[ a ].length() > 0 )
 
923
                                {
 
924
                                        // split this part in to it's comma-separated bits
 
925
                                        String[] adr_part_parts =
 
926
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
927
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
928
                                                if( adr_part_parts[ b ].length() > 0 )
 
929
                                                {
 
930
                                                        if( value.length() > 0 ) value += "\n";
 
931
                                                        value += adr_part_parts[ b ];
 
932
                                                }
 
933
                                }
804
934
 
805
935
                        Set< String > types = extractTypes( params, Arrays.asList(
806
936
                                "PREF", "WORK", "HOME", "INTERNET" ) );
807
937
 
808
938
                        // add address
 
939
                        int type;
809
940
                        if( types.contains( "WORK" ) )
810
 
                                addAddress( value, Contacts.ContactMethods.TYPE_WORK );
 
941
                                type = Contacts.ContactMethods.TYPE_WORK;
811
942
                        else
812
 
                                addAddress( value, Contacts.ContactMethods.TYPE_HOME);
 
943
                                type = Contacts.ContactMethods.TYPE_HOME;
 
944
 
 
945
                        addAddress( unescapeValue( value ), type );
813
946
                }
814
947
 
815
 
                public void finaliseParsing()
816
 
                        throws ParseException, SkipContactException,
817
 
                        AbortImportException
 
948
                public void finaliseVcard()
 
949
                        throws ParseException, ContactNotIdentifiableException
818
950
                {
819
951
                        // missing version (and data is present)
820
952
                        if( _version == null && _buffers != null )
821
953
                                throw new ParseException( R.string.error_vcf_malformed );
822
954
 
823
 
                        //  missing name properties?
824
 
                        if( _name_level == NAMELEVEL_NONE )
825
 
                                throw new ParseException( R.string.error_vcf_noname );
826
 
 
827
 
                        // check if we should import this one? If we've already got an 'N'-
828
 
                        // type name, this will already have been done by parseN() so we
829
 
                        // mustn't do this here (or it could prompt twice!)
830
 
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
831
 
                                throw new SkipContactException();
 
955
                        // finalise the parent class
 
956
                        finalise();
832
957
                }
833
958
 
834
959
                private String checkParam( String[] params, String name )