/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/VCFImporter.java

  • Committer: edam
  • Date: 2011-04-22 15:36:06 UTC
  • Revision ID: edam@waxworlds.org-20110422153606-9x9l0nbmvx6oxfxu
- pulled contacts cache out in to seperate class

Show diffs side-by-side

added added

removed removed

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