/android/import-contacts

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

« back to all changes in this revision

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

  • Committer: edam
  • Date: 2012-12-19 17:51:35 UTC
  • Revision ID: tim@ed.am-20121219175135-1cpuafp76jg1ib1p
added preliminary (buggy) ContactsContract backend

Show diffs side-by-side

added added

removed removed

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