/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-11 12:37:02 UTC
  • Revision ID: edam@waxworlds.org-20110611123702-h9ldpe7freq1rbqf
- modified 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 <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[] { "N",
 
572
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
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" ) )
627
676
                                        parseEMAIL( name_param_parts, complete_value );
628
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
629
678
                                        parseADR( name_param_parts, complete_value );
 
679
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
680
                                        parseLABEL( name_param_parts, complete_value );
630
681
                        }
631
682
                }
632
683
 
645
696
                        return ( count & 1 ) == 1;
646
697
                }
647
698
 
648
 
                private String[] splitValueBySemicolon( String value )
 
699
                private String[] splitValueByCharacter( String value, char character )
649
700
                {
650
 
                        // split string in to parts by semicolon
 
701
                        // split string in to parts by specified character
651
702
                        ArrayList< String > parts = new ArrayList< String >(
652
 
                                Arrays.asList( value.split(  ";" ) ) );
 
703
                                Arrays.asList( value.split( "" + character ) ) );
653
704
 
654
705
                        // go through parts
655
706
                        for( int a = 0; a < parts.size(); a++ )
663
714
                                if( a < parts.size() - 1 &&
664
715
                                        doesStringEndInAnEscapeChar( str ) )
665
716
                                {
666
 
                                        // join the next part to this part and remove the next part
 
717
                                        // append the escaped character, join the next part to this
 
718
                                        // part and remove the next part
667
719
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
668
 
                                                ';' + parts.get( a + 1 ) );
 
720
                                                character + parts.get( a + 1 ) );
669
721
                                        parts.remove( a + 1 );
670
722
 
671
723
                                        // re-visit this part
682
734
                        return parts.toArray( ret );
683
735
                }
684
736
 
 
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
 
685
780
                private void parseN( String[] params, String value )
686
 
                        throws ParseException, SkipContactException,
687
 
                        AbortImportException
688
781
                {
689
782
                        // already got a better name?
690
783
                        if( _name_level >= NAMELEVEL_N ) return;
691
784
 
692
785
                        // get name parts
693
 
                        String[] name_parts = splitValueBySemicolon( value );
 
786
                        String[] name_parts = splitValueByCharacter( value, ';' );
694
787
 
695
788
                        // build name
696
789
                        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 ];
 
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
                                }
701
805
 
702
806
                        // set name
703
 
                        setName( value );
 
807
                        setName( unescapeValue( value ) );
704
808
                        _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
809
                }
711
810
 
712
811
                private void parseFN( String[] params, String value )
713
 
                        throws ParseException, SkipContactException
714
812
                {
715
813
                        // already got a better name?
716
814
                        if( _name_level >= NAMELEVEL_FN ) return;
717
815
 
718
816
                        // set name
719
 
                        setName( value );
 
817
                        setName( unescapeValue( value ) );
720
818
                        _name_level = NAMELEVEL_FN;
721
819
                }
722
820
 
723
821
                private void parseORG( String[] params, String value )
724
 
                        throws ParseException, SkipContactException
725
822
                {
726
 
                        // already got a better name?
727
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
728
 
 
729
823
                        // 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;
 
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;
743
867
                }
744
868
 
745
869
                private void parseTEL( String[] params, String value )
746
 
                        throws ParseException
747
870
                {
748
871
                        if( value.length() == 0 ) return;
749
872
 
752
875
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
753
876
 
754
877
                        // 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;
 
878
                        boolean is_preferred = types.contains( "PREF" );
 
879
                        int type;
764
880
                        if( types.contains( "FAX" ) )
765
881
                                if( types.contains( "HOME" ) )
766
882
                                        type = PhonesColumns.TYPE_FAX_HOME;
767
883
                                else
768
884
                                        type = PhonesColumns.TYPE_FAX_WORK;
769
 
                        if( types.contains( "PAGER" ) )
 
885
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
886
                                type = PhonesColumns.TYPE_MOBILE;
 
887
                        else if( types.contains( "PAGER" ) )
770
888
                                type = PhonesColumns.TYPE_PAGER;
 
889
                        else if( types.contains( "WORK" ) )
 
890
                                type = PhonesColumns.TYPE_WORK;
 
891
                        else
 
892
                                type = PhonesColumns.TYPE_HOME;
771
893
 
772
894
                        // add phone number
773
 
                        addPhone( value, type, preferred );
 
895
                        addNumber( value, type, is_preferred );
774
896
                }
775
897
 
776
898
                public void parseEMAIL( String[] params, String value )
777
 
                        throws ParseException
778
899
                {
779
900
                        if( value.length() == 0 ) return;
780
901
 
782
903
                                "PREF", "WORK", "HOME", "INTERNET" ) );
783
904
 
784
905
                        // add email address
785
 
                        boolean preferred = types.contains( "PREF" );
 
906
                        boolean is_preferred = types.contains( "PREF" );
 
907
                        int type;
786
908
                        if( types.contains( "WORK" ) )
787
 
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
 
909
                                type = Contacts.ContactMethods.TYPE_WORK;
788
910
                        else
789
 
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
 
911
                                type = Contacts.ContactMethods.TYPE_HOME;
 
912
 
 
913
                        addEmail( unescapeValue( value ), type, is_preferred );
790
914
                }
791
915
 
792
916
                private void parseADR( String[] params, String value )
793
 
                        throws ParseException, SkipContactException
794
917
                {
795
918
                        // get address parts
796
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
919
                        String[] adr_parts = splitValueByCharacter( value, ';' );
797
920
 
798
921
                        // build address
799
922
                        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
 
                        }
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
 
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
818
967
                {
819
968
                        // missing version (and data is present)
820
969
                        if( _version == null && _buffers != null )
821
970
                                throw new ParseException( R.string.error_vcf_malformed );
822
971
 
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();
 
972
                        // finalise the parent class
 
973
                        finalise();
832
974
                }
833
975
 
 
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
                 */
834
983
                private String checkParam( String[] params, String name )
835
984
                {
 
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
 
836
999
                        Pattern p = Pattern.compile(
837
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
838
1001
                        for( int i = 0; i < params.length; i++ ) {
839
1002
                                Matcher m = p.matcher( params[ i ] );
840
1003
                                if( m.matches() )
841
 
                                        return m.group( 2 );
 
1004
                                        ret.add( m.group( 2 ) );
842
1005
                        }
843
 
                        return null;
 
1006
 
 
1007
                        return (String[]) ret.toArray( new String[ ret.size() ] );
844
1008
                }
845
1009
 
 
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
                 */
846
1018
                private Set< String > extractTypes( String[] params,
847
1019
                        List< String > valid_types )
848
1020
                {
849
1021
                        HashSet< String > types = new HashSet< String >();
850
1022
 
851
1023
                        // get 3.0-style TYPE= param
852
 
                        String type_param;
853
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
854
 
                                String[] parts = type_param.split( "," );
 
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( "," );
855
1030
                                for( int i = 0; i < parts.length; i++ )
856
1031
                                        if( valid_types.contains( parts[ i ] ) )
857
1032
                                                types.add( parts[ i ] );