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

  • Committer: edam
  • Date: 2009-02-02 07:07:52 UTC
  • Revision ID: edam@waxworlds.org-20090202070752-2lp8igdsdjyu9fic
- bugfix: add contacts to the "my contacts" group didn't actually work on a real device. So we're doing it a different way.
- updated todo list

Show diffs side-by-side

added added

removed removed

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 org.waxworlds.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
28
 
import java.io.FileInputStream;
29
28
import java.io.FileNotFoundException;
30
29
import java.io.FileReader;
31
30
import java.io.FilenameFilter;
32
31
import java.io.IOException;
33
32
import java.io.UnsupportedEncodingException;
34
 
import java.nio.ByteBuffer;
35
 
import java.util.ArrayList;
36
33
import java.util.Arrays;
37
34
import java.util.HashSet;
38
 
import java.util.Iterator;
39
35
import java.util.List;
40
 
import java.util.NoSuchElementException;
41
36
import java.util.Set;
42
37
import java.util.Vector;
43
38
import java.util.regex.Matcher;
44
39
import java.util.regex.Pattern;
45
40
 
 
41
import org.waxworlds.importcontacts.Importer.AbortImportException;
 
42
 
46
43
import android.content.SharedPreferences;
47
44
import android.provider.Contacts;
48
45
import android.provider.Contacts.PhonesColumns;
70
67
                try
71
68
                {
72
69
                        // open directory
73
 
                        String path = "/sdcard" + prefs.getString( "location", "/" );
74
 
                        File file = new File( path );
75
 
                        if( !file.exists() )
 
70
                        String location = prefs.getString( "location", "" );
 
71
                        File dir = new File( location );
 
72
                        if( !dir.exists() || !dir.isDirectory() )
76
73
                                showError( R.string.error_locationnotfound );
77
74
 
78
 
                        // directory, or file?
79
 
                        if( file.isDirectory() )
80
 
                        {
81
 
                                // get files
82
 
                                class VCardFilter implements FilenameFilter {
83
 
                                        public boolean accept( File dir, String name ) {
84
 
                                                return name.toLowerCase().endsWith( ".vcf" );
85
 
                                        }
86
 
                                }
87
 
                                files = file.listFiles( new VCardFilter() );
88
 
                        }
89
 
                        else
90
 
                        {
91
 
                                // use just this file
92
 
                                files = new File[ 1 ];
93
 
                                files[ 0 ] = file;
94
 
                        }
 
75
                        // get files
 
76
                        class VCardFilter implements FilenameFilter {
 
77
                            public boolean accept( File dir, String name ) {
 
78
                                return name.toLowerCase().endsWith( ".vcf" );
 
79
                            }
 
80
                        }
 
81
                        files = dir.listFiles( new VCardFilter() );
95
82
                }
96
83
                catch( SecurityException e ) {
97
84
                        showError( R.string.error_locationpermissions );
123
110
                {
124
111
                        // open file
125
112
                        BufferedReader reader = new BufferedReader(
126
 
                                new FileReader( file ) );
 
113
                                        new FileReader( file ) );
127
114
 
128
115
                        // read
129
116
                        String line;
132
119
                        {
133
120
                                if( !inVCard ) {
134
121
                                        // look for vcard beginning
135
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
122
                                        if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
136
123
                                                inVCard = true;
137
124
                                                _vCardCount++;
138
125
                                        }
139
126
                                }
140
 
                                else if( line.matches( "^END:VCARD" ) )
 
127
                                else if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
141
128
                                        inVCard = false;
142
129
                        }
143
130
 
144
131
                }
145
132
                catch( FileNotFoundException e ) {
146
 
                        showError( getText( R.string.error_filenotfound ) +
147
 
                                file.getName() );
 
133
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
148
134
                }
149
135
                catch( IOException e ) {
150
136
                        showError( getText( R.string.error_ioerror ) + file.getName() );
153
139
 
154
140
        private void importVCardFile( File file ) throws AbortImportException
155
141
        {
156
 
                // check file is good
157
 
                if( !file.exists() )
158
 
                        showError( getText( R.string.error_filenotfound ) +
159
 
                                file.getName() );
160
 
                if( file.length() == 0 )
161
 
                        showError( getText( R.string.error_fileisempty ) +
162
 
                                file.getName() );
163
 
 
164
142
                try
165
143
                {
166
 
                        // open/read file
167
 
                        FileInputStream istream = new FileInputStream( file );
168
 
                        byte[] content = new byte[ (int)file.length() ];
169
 
                        istream.read( content );
170
 
 
171
 
                        // import
172
 
                        importVCardFileContent( content, file.getName() );
 
144
                        // open file
 
145
                        BufferedReader reader = new BufferedReader(
 
146
                                        new FileReader( file ) );
 
147
 
 
148
                        // read
 
149
                        StringBuffer content = new StringBuffer();
 
150
                        String line;
 
151
                        while( ( line = reader.readLine() ) != null )
 
152
                                content.append( line ).append( "\n" );
 
153
 
 
154
                        importVCardFileContent( content.toString(), file.getName() );
173
155
                }
174
156
                catch( FileNotFoundException e ) {
175
 
                        showError( getText( R.string.error_filenotfound ) +
176
 
                                file.getName() );
 
157
                        showError( getText( R.string.error_filenotfound ) + file.getName() );
177
158
                }
178
159
                catch( IOException e ) {
179
160
                        showError( getText( R.string.error_ioerror ) + file.getName() );
180
161
                }
181
162
        }
182
163
 
183
 
        private void importVCardFileContent( byte[] content, String fileName )
184
 
                throws AbortImportException
 
164
        private void importVCardFileContent( String content, String fileName )
 
165
                        throws AbortImportException
185
166
        {
186
 
                // go through lines
 
167
                // unfold RFC2425 section 5.8.1 folded lines, except that we must also
 
168
                // handle embedded Quoted-Printable encodings that have a trailing '='.
 
169
                // So we remove these first before doing RFC2425 unfolding.
 
170
                content = content.replaceAll( "=\n[ \\t]", "" )
 
171
                                .replaceAll( "\n[ \\t]", "" );
 
172
 
 
173
                // get lines and parse them
 
174
                String[] lines = content.split( "\n" );
187
175
                VCard vCard = null;
188
 
                ContentLineIterator cli = new ContentLineIterator( content );
189
 
                while( cli.hasNext() )
 
176
                for( int i = 0; i < lines.length; i++ )
190
177
                {
191
 
                        ByteBuffer buffer = cli.next();
192
 
 
193
 
                        // get a US-ASCII version of the line for processing
194
 
                        String line;
195
 
                        try {
196
 
                                line = new String( buffer.array(), buffer.position(),
197
 
                                        buffer.limit() - buffer.position(), "US-ASCII" );
198
 
                        }
199
 
                        catch( UnsupportedEncodingException e ) {
200
 
                                // we know US-ASCII is supported, so appease the compiler...
201
 
                                line = "";
202
 
                        }
 
178
                        String line = lines[ i ];
203
179
 
204
180
                        if( vCard == null ) {
205
181
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN:VCARD" ) ) {
 
182
                                if( line.matches( "^BEGIN[ \\t]*:[ \\t]*VCARD" ) ) {
207
183
                                        setProgress( ++_progress );
208
184
                                        vCard = new VCard();
209
185
                                }
210
186
                        }
211
187
                        else {
212
188
                                // look for vcard content or ending
213
 
                                if( line.matches( "^END:VCARD" ) )
 
189
                                if( line.matches( "^END[ \\t]*:[ \\t]*VCARD" ) )
214
190
                                {
215
191
                                        // store vcard and do away with it
216
192
                                        try {
220
196
                                        catch( VCard.ParseException e ) {
221
197
                                                skipContact();
222
198
                                                if( !showContinue(
223
 
                                                        getText( R.string.error_vcf_parse ).toString()
224
 
                                                        + fileName + "\n" + e.getMessage() ) )
225
 
                                                {
 
199
                                                                getText( R.string.error_vcf_parse ).toString()
 
200
                                                                + fileName + "\n" + e.getMessage() ) )
226
201
                                                        finish( ACTION_ABORT );
227
 
                                                }
228
202
                                        }
229
203
                                        catch( VCard.SkipContactException e ) {
230
204
                                                skipContact();
236
210
                                {
237
211
                                        // try giving the line to the vcard
238
212
                                        try {
239
 
                                                vCard.parseLine( buffer, line,
240
 
                                                        cli.doesNextLineLookFolded() );
 
213
                                                vCard.parseLine( line );
241
214
                                        }
242
215
                                        catch( VCard.ParseException e ) {
243
216
                                                skipContact();
244
217
                                                if( !showContinue(
245
 
                                                        getText( R.string.error_vcf_parse ).toString()
246
 
                                                        + fileName + "\n" + e.getMessage() ) )
247
 
                                                {
 
218
                                                                getText( R.string.error_vcf_parse ).toString()
 
219
                                                                + fileName + "\n" + e.getMessage() ) )
248
220
                                                        finish( ACTION_ABORT );
249
 
                                                }
250
221
 
251
222
                                                // although we're continuing, we still need to abort
252
223
                                                // this vCard. Further lines will be ignored until we
264
235
                }
265
236
        }
266
237
 
267
 
        class ContentLineIterator implements Iterator< ByteBuffer >
268
 
        {
269
 
                protected byte[] _content = null;
270
 
                protected int _pos = 0;
271
 
 
272
 
                public ContentLineIterator( byte[] content )
273
 
                {
274
 
                        _content = content;
275
 
                }
276
 
 
277
 
                @Override
278
 
                public boolean hasNext()
279
 
                {
280
 
                        return _pos < _content.length;
281
 
                }
282
 
 
283
 
                @Override
284
 
                public ByteBuffer next()
285
 
                {
286
 
                        int initial_pos = _pos;
287
 
 
288
 
                        // find newline
289
 
                        for( ; _pos < _content.length; _pos++ )
290
 
                                if( _content[ _pos ] == '\n' )
291
 
                                {
292
 
                                        // adjust for a \r preceding the \n
293
 
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
294
 
                                                _pos > initial_pos )? _pos - 1 : _pos;
295
 
                                        _pos++;
296
 
                                        return ByteBuffer.wrap( _content, initial_pos,
297
 
                                                to - initial_pos );
298
 
                                }
299
 
 
300
 
                        // we didn't find one, but were there bytes left?
301
 
                        if( _pos != initial_pos ) {
302
 
                                int to = _pos;
303
 
                                _pos++;
304
 
                                return ByteBuffer.wrap( _content, initial_pos,
305
 
                                        to - initial_pos );
306
 
                        }
307
 
 
308
 
                        // no bytes left
309
 
                        throw new NoSuchElementException();
310
 
                }
311
 
 
312
 
                @Override
313
 
                public void remove()
314
 
                {
315
 
                        throw new UnsupportedOperationException();
316
 
                }
317
 
 
318
 
                /**
319
 
                 * Does the next line, if there is one, look like it should be folded
320
 
                 * onto the end of this one?
321
 
                 * @return
322
 
                 */
323
 
                public boolean doesNextLineLookFolded()
324
 
                {
325
 
                        return _pos > 0 && _pos < _content.length &&
326
 
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
327
 
                }
328
 
        }
329
 
 
330
238
        private class VCard extends ContactData
331
239
        {
332
240
                private final static int NAMELEVEL_NONE = 0;
334
242
                private final static int NAMELEVEL_FN = 2;
335
243
                private final static int NAMELEVEL_N = 3;
336
244
 
337
 
                private final static int MULTILINE_NONE = 0;
338
 
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
339
 
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
340
 
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
341
 
 
342
245
                private String _version = null;
343
 
                private Vector< ByteBuffer > _buffers = null;
344
 
                private int _name_level = NAMELEVEL_NONE;
345
 
                private int _parser_multiline_state = MULTILINE_NONE;
346
 
                private String _parser_current_name_and_params = null;
347
 
                private String _parser_buffered_value_so_far = "";
348
 
 
349
 
                protected class UnencodeResult
350
 
                {
351
 
                        private boolean _another_line_required;
352
 
                        private ByteBuffer _buffer;
353
 
 
354
 
                        public UnencodeResult( boolean another_line_required,
355
 
                                ByteBuffer buffer )
356
 
                        {
357
 
                                _another_line_required = another_line_required;
358
 
                                _buffer = buffer;
359
 
                        }
360
 
 
361
 
                        public boolean isAnotherLineRequired()
362
 
                        {
363
 
                                return _another_line_required;
364
 
                        }
365
 
 
366
 
                        public ByteBuffer getBuffer()
367
 
                        {
368
 
                                return _buffer;
369
 
                        }
370
 
                }
371
 
 
372
 
                @SuppressWarnings("serial")
 
246
                private Vector< String > _lines = null;
 
247
                private int _nameLevel = NAMELEVEL_NONE;
 
248
 
373
249
                protected class ParseException extends Exception
374
250
                {
375
 
                        @SuppressWarnings("unused")
376
251
                        public ParseException( String error )
377
252
                        {
378
253
                                super( error );
384
259
                        }
385
260
                }
386
261
 
387
 
                @SuppressWarnings("serial")
388
262
                protected class SkipContactException extends Exception { }
389
263
 
390
 
                private String extractCollonPartFromLine( ByteBuffer buffer,
391
 
                        String line, boolean former )
392
 
                {
393
 
                        String ret = null;
394
 
 
395
 
                        // get a US-ASCII version of the line for processing, unless we were
396
 
                        // supplied with one
397
 
                        if( line == null ) {
398
 
                                try {
399
 
                                        line = new String( buffer.array(), buffer.position(),
400
 
                                                buffer.limit() - buffer.position(), "US-ASCII" );
401
 
                                }
402
 
                                catch( UnsupportedEncodingException e ) {
403
 
                                        // we know US-ASCII is supported, so appease the compiler...
404
 
                                        line = "";
405
 
                                }
406
 
                        }
407
 
 
408
 
                        // split line into name and value parts and check to make sure we
409
 
                        // only got 2 parts and that the first part is not zero in length
410
 
                        String[] parts = line.split( ":", 2 );
411
 
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
412
 
                                ret = parts[ former? 0 : 1 ];
413
 
 
414
 
                        return ret;
415
 
                }
416
 
 
417
 
                private String extractNameAndParamsFromLine( ByteBuffer buffer,
418
 
                        String line )
419
 
                {
420
 
                        return extractCollonPartFromLine( buffer, line, true );
421
 
                }
422
 
 
423
 
                private String extractValueFromLine( ByteBuffer buffer, String line )
424
 
                {
425
 
                        return extractCollonPartFromLine( buffer, line, false );
426
 
                }
427
 
 
428
 
                public void parseLine( ByteBuffer buffer, String line,
429
 
                        boolean next_line_looks_folded )
430
 
                        throws ParseException, SkipContactException,
431
 
                        AbortImportException
432
 
                {
433
 
                        // do we have a version yet?
 
264
                public void parseLine( String line )
 
265
                                throws ParseException, SkipContactException,
 
266
                                AbortImportException
 
267
                {
 
268
                        // get property halves
 
269
                        String[] props = line.split( ":" );
 
270
                        for( int i = 0; i < props.length; i++ )
 
271
                                props[ i ] = props[ i ].trim();
 
272
                        if( props.length < 2 ||
 
273
                                        props[ 0 ].length() < 1 || props[ 1 ].length() < 1 )
 
274
                                throw new ParseException( R.string.error_vcf_malformed );
 
275
 
434
276
                        if( _version == null )
435
277
                        {
436
 
                                // tentatively get name and params from line
437
 
                                String name_and_params =
438
 
                                        extractNameAndParamsFromLine( buffer, line );
439
 
 
440
 
                                // is it a version line?
441
 
                                if( name_and_params != null &&
442
 
                                        name_and_params.equals( "VERSION" ) )
 
278
                                if( props[ 0 ].equals( "VERSION" ) )
443
279
                                {
444
 
                                        // yes, get it!
445
 
                                        String value = extractValueFromLine( buffer, line );
446
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
280
                                        // get version
 
281
                                        if( !props[ 1 ].equals( "2.1" ) &&
 
282
                                                        !props[ 1 ].equals( "3.0" ) )
447
283
                                                throw new ParseException( R.string.error_vcf_version );
448
 
                                        _version = value;
 
284
                                        _version = props[ 1 ];
449
285
 
450
 
                                        // parse any buffers we've been accumulating while we waited
451
 
                                        // for a version
452
 
                                        if( _buffers != null )
453
 
                                                for( int i = 0; i < _buffers.size(); i++ )
454
 
                                                        parseLine( _buffers.get( i ), null,
455
 
                                                                i + 1 < _buffers.size() &&
456
 
                                                                _buffers.get( i + 1 ).hasRemaining() &&
457
 
                                                                _buffers.get( i + 1 ).get(
458
 
                                                                        _buffers.get( i + 1 ).position() ) == ' ' );
459
 
                                        _buffers = null;
 
286
                                        // parse any other lines we've accumulated so far
 
287
                                        if( _lines != null )
 
288
                                                for( int i = 0; i < _lines.size(); i++ )
 
289
                                                        parseLine( _lines.get( i ) );
 
290
                                        _lines = null;
460
291
                                }
461
292
                                else
462
293
                                {
463
 
                                        // no, so stash this line till we get a version
464
 
                                        if( _buffers == null )
465
 
                                                _buffers = new Vector< ByteBuffer >();
466
 
                                        _buffers.add( buffer );
 
294
                                        // stash this line till we have a version
 
295
                                        if( _lines == null )
 
296
                                                _lines = new Vector< String >();
 
297
                                        _lines.add( line );
467
298
                                }
468
299
                        }
469
300
                        else
470
301
                        {
471
 
                                // name and params and the position in the buffer where the
472
 
                                // "value" part of the line start
473
 
                                String name_and_params;
474
 
                                int pos;
475
 
 
476
 
                                if( _parser_multiline_state != MULTILINE_NONE )
477
 
                                {
478
 
                                        // if we're currently in a multi-line value, use the stored
479
 
                                        // property name and parameters
480
 
                                        name_and_params = _parser_current_name_and_params;
481
 
 
482
 
                                        // skip some initial line characters, depending on the type
483
 
                                        // of multi-line we're handling
484
 
                                        pos = buffer.position();
485
 
                                        switch( _parser_multiline_state )
486
 
                                        {
487
 
                                        case MULTILINE_FOLDED:
488
 
                                                pos++;
489
 
                                                break;
490
 
                                        case MULTILINE_ENCODED:
491
 
                                                while( pos < buffer.limit() && (
492
 
                                                        buffer.get( pos ) == ' ' ||
493
 
                                                        buffer.get( pos ) == '\t' ) )
494
 
                                                {
495
 
                                                        pos++;
496
 
                                                }
497
 
                                                break;
498
 
                                        default:
499
 
                                                // do nothing
500
 
                                        }
501
 
 
502
 
                                        // take us out of multi-line so that we can re-detect that
503
 
                                        // this line is a multi-line or not
504
 
                                        _parser_multiline_state = MULTILINE_NONE;
505
 
                                }
506
 
                                else
507
 
                                {
508
 
                                        // get name and params from line, and since we're not
509
 
                                        // parsing a subsequent line in a multi-line, this should
510
 
                                        // not fail, or it's an error
511
 
                                        name_and_params =
512
 
                                                extractNameAndParamsFromLine( buffer, line );
513
 
                                        if( name_and_params == null )
514
 
                                                throw new ParseException(
515
 
                                                        R.string.error_vcf_malformed );
516
 
 
517
 
                                        // calculate how many chars to skip from beginning of line
518
 
                                        // so we skip the property "name:" part
519
 
                                        pos = buffer.position() + name_and_params.length() + 1;
520
 
 
521
 
                                        // reset the saved multi-line state
522
 
                                        _parser_current_name_and_params = name_and_params;
523
 
                                        _parser_buffered_value_so_far = "";
524
 
                                }
525
 
 
526
 
                                // get value from buffer, as raw bytes
527
 
                                ByteBuffer value;
528
 
                                value = ByteBuffer.wrap( buffer.array(), pos,
529
 
                                        buffer.limit() - pos );
530
 
 
531
302
                                // get parameter parts
532
 
                                String[] name_param_parts = name_and_params.split( ";", -1 );
533
 
                                for( int i = 0; i < name_param_parts.length; i++ )
534
 
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
535
 
 
536
 
                                // parse encoding parameter
537
 
                                String encoding = checkParam( name_param_parts, "ENCODING" );
538
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
539
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
540
 
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
541
 
                                        //&& !encoding.equals( "BASE64" ) )
542
 
                                {
543
 
                                        throw new ParseException( R.string.error_vcf_encoding );
544
 
                                }
545
 
 
546
 
                                // parse charset parameter
547
 
                                String charset = checkParam( name_param_parts, "CHARSET" );
548
 
                                if( charset != null ) charset = charset.toUpperCase();
549
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
550
 
                                        !charset.equals( "ASCII" ) &&
551
 
                                        !charset.equals( "UTF-8" ) )
552
 
                                {
553
 
                                        throw new ParseException( R.string.error_vcf_charset );
554
 
                                }
555
 
 
556
 
                                // do unencoding (or default to a fake unencoding result with
557
 
                                // the raw string)
558
 
                                UnencodeResult unencoding_result = null;
559
 
                                if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
560
 
                                        unencoding_result = unencodeQuotedPrintable( value );
561
 
//                              else if( encoding != null && encoding.equals( "BASE64" ) )
562
 
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
563
 
                                if( unencoding_result != null ) {
564
 
                                        value = unencoding_result.getBuffer();
565
 
                                        if( unencoding_result.isAnotherLineRequired() )
566
 
                                                _parser_multiline_state = MULTILINE_ENCODED;
567
 
                                }
568
 
 
569
 
                                // convert 8-bit ASCII charset to US-ASCII
570
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
571
 
                                        value = transcodeAsciiToUtf8( value );
572
 
                                        charset = "UTF-8";
573
 
                                }
574
 
 
575
 
                                // process charset
576
 
                                String string_value;
577
 
                                try {
578
 
                                        string_value = new String( value.array(), value.position(),
579
 
                                                value.limit() - value.position(), charset );
580
 
                                } catch( UnsupportedEncodingException e ) {
581
 
                                        throw new ParseException( R.string.error_vcf_charset );
582
 
                                }
583
 
 
584
 
                                // for some entries that have semicolon-separated value parts,
585
 
                                // check to see if the value ends in an escape character, which
586
 
                                // indicates that we have a multi-line value
587
 
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
588
 
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
589
 
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
590
 
                                        doesStringEndInAnEscapeChar( string_value ) )
591
 
                                {
592
 
                                        _parser_multiline_state = MULTILINE_ESCAPED;
593
 
                                        string_value = string_value.substring( 0,
594
 
                                                string_value.length() - 1 );
595
 
                                }
596
 
 
597
 
                                // now we know whether we're in an encoding multi-line,
598
 
                                // determine if we're in a v3 folded multi-line or not
599
 
                                if( _parser_multiline_state == MULTILINE_NONE &&
600
 
                                        _version.equals( "3.0" ) && next_line_looks_folded )
601
 
                                {
602
 
                                        _parser_multiline_state = MULTILINE_FOLDED;
603
 
                                }
604
 
 
605
 
                                // handle multi-lines by buffering them and parsing them when we
606
 
                                // are processing the last line in a multi-line sequence
607
 
                                if( _parser_multiline_state != MULTILINE_NONE ) {
608
 
                                        _parser_buffered_value_so_far += string_value;
609
 
                                        return;
610
 
                                }
611
 
                                String complete_value =
612
 
                                        ( _parser_buffered_value_so_far + string_value ).trim();
613
 
 
614
 
                                // ignore empty values
615
 
                                if( complete_value.length() < 1 ) return;
 
303
                                String[] params = props[ 0 ].split( ";" );
 
304
                                for( int i = 0; i < params.length; i++ )
 
305
                                        params[ i ] = params[ i ].trim();
616
306
 
617
307
                                // parse some properties
618
 
                                if( name_param_parts[ 0 ].equals( "N" ) )
619
 
                                        parseN( name_param_parts, complete_value );
620
 
                                else if( name_param_parts[ 0 ].equals( "FN" ) )
621
 
                                        parseFN( name_param_parts, complete_value );
622
 
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
623
 
                                        parseORG( name_param_parts, complete_value );
624
 
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
625
 
                                        parseTEL( name_param_parts, complete_value );
626
 
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
627
 
                                        parseEMAIL( name_param_parts, complete_value );
628
 
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
629
 
                                        parseADR( name_param_parts, complete_value );
630
 
                        }
631
 
                }
632
 
 
633
 
                private boolean doesStringEndInAnEscapeChar( String string )
634
 
                {
635
 
                        // count the number of backslashes at the end of the string
636
 
                        int count = 0;
637
 
                        for( int a = string.length() - 1; a >= 0; a-- )
638
 
                                if( string.charAt( a ) == '\\' )
639
 
                                        count++;
640
 
                                else
641
 
                                        break;
642
 
 
643
 
                        // if there are an even number of backslashes then the final one
644
 
                        // doesn't count
645
 
                        return ( count & 1 ) == 1;
646
 
                }
647
 
 
648
 
                private String[] splitValueBySemicolon( String value )
649
 
                {
650
 
                        // split string in to parts by semicolon
651
 
                        ArrayList< String > parts = new ArrayList< String >(
652
 
                                Arrays.asList( value.split(  ";" ) ) );
653
 
 
654
 
                        // go through parts
655
 
                        for( int a = 0; a < parts.size(); a++ )
656
 
                        {
657
 
                                String str = parts.get( a );
658
 
 
659
 
                                // look for parts that end in an escape character, but ignore
660
 
                                // the final part. We've already detected escape chars at the
661
 
                                // end of the final part in parseLine() and handled multi-lines
662
 
                                // accordingly.
663
 
                                if( a < parts.size() - 1 &&
664
 
                                        doesStringEndInAnEscapeChar( str ) )
665
 
                                {
666
 
                                        // join the next part to this part and remove the next part
667
 
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
668
 
                                                ';' + parts.get( a + 1 ) );
669
 
                                        parts.remove( a + 1 );
670
 
 
671
 
                                        // re-visit this part
672
 
                                        a--;
673
 
                                        continue;
674
 
                                }
675
 
 
676
 
                                // trim and replace string
677
 
                                str = str.trim();
678
 
                                parts.set( a, str );
679
 
                        }
680
 
 
681
 
                        String[] ret = new String[ parts.size() ];
682
 
                        return parts.toArray( ret );
 
308
                                if( params[ 0 ].equals( "N" ) )
 
309
                                        parseN( params, props[ 1 ] );
 
310
                                else if( params[ 0 ].equals( "FN" ) )
 
311
                                        parseFN( params, props[ 1 ] );
 
312
                                else if( params[ 0 ].equals( "ORG" ) )
 
313
                                        parseORG( params, props[ 1 ] );
 
314
                                else if( params[ 0 ].equals( "TEL" ) )
 
315
                                        parseTEL( params, props[ 1 ] );
 
316
                                else if( params[ 0 ].equals( "EMAIL" ) )
 
317
                                        parseEMAIL( params, props[ 1 ] );
 
318
                        }
683
319
                }
684
320
 
685
321
                private void parseN( String[] params, String value )
686
 
                        throws ParseException, SkipContactException,
687
 
                        AbortImportException
 
322
                                throws ParseException, SkipContactException,
 
323
                                AbortImportException
688
324
                {
689
325
                        // already got a better name?
690
 
                        if( _name_level >= NAMELEVEL_N ) return;
 
326
                        if( _nameLevel >= NAMELEVEL_N ) return;
691
327
 
692
328
                        // get name parts
693
 
                        String[] name_parts = splitValueBySemicolon( value );
 
329
                        String[] nameparts = value.split( ";" );
 
330
                        for( int i = 0; i < nameparts.length; i++ )
 
331
                                nameparts[ i ] = nameparts[ i ].trim();
694
332
 
695
333
                        // build name
696
334
                        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 ];
 
335
                        if( nameparts.length > 1 && nameparts[ 1 ].length() > 0 )
 
336
                                value += nameparts[ 1 ];
 
337
                        if( nameparts[ 0 ].length() > 0 )
 
338
                                value += ( value.length() == 0? "" : " " ) + nameparts[ 0 ];
701
339
 
702
340
                        // set name
703
 
                        setName( value );
704
 
                        _name_level = NAMELEVEL_N;
 
341
                        setName( undoCharsetAndEncoding( params, value ) );
 
342
                        _nameLevel = NAMELEVEL_N;
705
343
 
706
344
                        // check now to see if we need to import this contact (to avoid
707
345
                        // parsing the rest of the vCard unnecessarily)
710
348
                }
711
349
 
712
350
                private void parseFN( String[] params, String value )
713
 
                        throws ParseException, SkipContactException
 
351
                                throws ParseException, SkipContactException
714
352
                {
715
353
                        // already got a better name?
716
 
                        if( _name_level >= NAMELEVEL_FN ) return;
 
354
                        if( _nameLevel >= NAMELEVEL_FN ) return;
717
355
 
718
356
                        // set name
719
 
                        setName( value );
720
 
                        _name_level = NAMELEVEL_FN;
 
357
                        setName( undoCharsetAndEncoding( params, value ) );
 
358
                        _nameLevel = NAMELEVEL_FN;
721
359
                }
722
360
 
723
361
                private void parseORG( String[] params, String value )
724
 
                        throws ParseException, SkipContactException
 
362
                                throws ParseException, SkipContactException
725
363
                {
726
364
                        // already got a better name?
727
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
 
365
                        if( _nameLevel >= NAMELEVEL_ORG ) return;
728
366
 
729
367
                        // get org parts
730
 
                        String[] org_parts = splitValueBySemicolon( value );
 
368
                        String[] orgparts = value.split( ";" );
 
369
                        for( int i = 0; i < orgparts.length; i++ )
 
370
                                orgparts[ i ] = orgparts[ i ].trim();
731
371
 
732
372
                        // 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 ];
 
373
                        if( orgparts[ 0 ].length() == 0 && orgparts.length > 1 )
 
374
                                value = orgparts[ 1 ];
737
375
                        else
738
 
                                value = org_parts[ 0 ];
 
376
                                value = orgparts[ 0 ];
739
377
 
740
378
                        // set name
741
 
                        setName( value );
742
 
                        _name_level = NAMELEVEL_ORG;
 
379
                        setName( undoCharsetAndEncoding( params, value ) );
 
380
                        _nameLevel = NAMELEVEL_ORG;
743
381
                }
744
382
 
745
383
                private void parseTEL( String[] params, String value )
746
 
                        throws ParseException
 
384
                                throws ParseException
747
385
                {
748
386
                        if( value.length() == 0 ) return;
749
387
 
750
388
                        Set< String > types = extractTypes( params, Arrays.asList(
751
 
                                "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
752
 
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
 
389
                                        "PREF", "HOME", "WORK", "VOICE", "FAX", "MSG", "CELL",
 
390
                                        "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
753
391
 
754
392
                        // here's the logic...
755
393
                        boolean preferred = types.contains( "PREF" );
756
 
                        int type = PhonesColumns.TYPE_MOBILE;
757
394
                        if( types.contains( "VOICE" ) )
758
395
                                if( types.contains( "WORK" ) )
759
 
                                        type = PhonesColumns.TYPE_WORK;
 
396
                                        addPhone( value, PhonesColumns.TYPE_WORK, preferred );
760
397
                                else
761
 
                                        type = PhonesColumns.TYPE_HOME;
 
398
                                        addPhone( value, PhonesColumns.TYPE_HOME, preferred );
762
399
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
763
 
                                type = PhonesColumns.TYPE_MOBILE;
 
400
                                addPhone( value, PhonesColumns.TYPE_MOBILE, preferred );
764
401
                        if( types.contains( "FAX" ) )
765
402
                                if( types.contains( "HOME" ) )
766
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
403
                                        addPhone( value, PhonesColumns.TYPE_FAX_HOME, preferred );
767
404
                                else
768
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
405
                                        addPhone( value, PhonesColumns.TYPE_FAX_WORK, preferred );
769
406
                        if( types.contains( "PAGER" ) )
770
 
                                type = PhonesColumns.TYPE_PAGER;
771
 
 
772
 
                        // add phone number
773
 
                        addPhone( value, type, preferred );
 
407
                                addPhone( value, PhonesColumns.TYPE_PAGER, preferred );
774
408
                }
775
409
 
776
410
                public void parseEMAIL( String[] params, String value )
777
 
                        throws ParseException
778
411
                {
779
412
                        if( value.length() == 0 ) return;
780
413
 
781
414
                        Set< String > types = extractTypes( params, Arrays.asList(
782
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
415
                                        "PREF", "WORK", "HOME", "INTERNET" ) );
783
416
 
784
 
                        // add email address
 
417
                        // here's the logic...
785
418
                        boolean preferred = types.contains( "PREF" );
786
419
                        if( types.contains( "WORK" ) )
787
420
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
789
422
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
790
423
                }
791
424
 
792
 
                private void parseADR( String[] params, String value )
793
 
                        throws ParseException, SkipContactException
794
 
                {
795
 
                        // get address parts
796
 
                        String[] adr_parts = splitValueBySemicolon( value );
797
 
 
798
 
                        // build address
799
 
                        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
425
                public void finaliseParsing()
816
 
                        throws ParseException, SkipContactException,
817
 
                        AbortImportException
 
426
                                throws ParseException, SkipContactException,
 
427
                                AbortImportException
818
428
                {
819
429
                        // missing version (and data is present)
820
 
                        if( _version == null && _buffers != null )
 
430
                        if( _version == null && _lines != null )
821
431
                                throw new ParseException( R.string.error_vcf_malformed );
822
432
 
823
433
                        //  missing name properties?
824
 
                        if( _name_level == NAMELEVEL_NONE )
 
434
                        if( _nameLevel == NAMELEVEL_NONE )
825
435
                                throw new ParseException( R.string.error_vcf_noname );
826
436
 
827
437
                        // check if we should import this one? If we've already got an 'N'-
828
438
                        // type name, this will already have been done by parseN() so we
829
439
                        // mustn't do this here (or it could prompt twice!)
830
 
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
 
440
                        if( _nameLevel < NAMELEVEL_N && !isImportRequired( getName() ) )
831
441
                                throw new SkipContactException();
832
442
                }
833
443
 
 
444
                private String undoCharsetAndEncoding( String[] params, String value )
 
445
                                throws ParseException
 
446
                {
 
447
                        // check encoding/charset
 
448
                        String charset, encoding;
 
449
                        if( ( charset = checkParam( params, "CHARSET" ) ) != null &&
 
450
                                        !charset.equals( "UTF-8" ) && !charset.equals( "UTF-16" ) )
 
451
                                throw new ParseException( R.string.error_vcf_charset );
 
452
                        if( ( encoding = checkParam( params, "ENCODING" ) ) != null &&
 
453
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
 
454
                                throw new ParseException( R.string.error_vcf_encoding );
 
455
 
 
456
                        // do decoding?
 
457
                        if( encoding != null && encoding.equals( "QUOTED-PRINTABLE" ) )
 
458
                                return unencodeQuotedPrintable( value, charset );
 
459
 
 
460
                        // nothing to do!
 
461
                        return value;
 
462
                }
 
463
 
834
464
                private String checkParam( String[] params, String name )
835
465
                {
836
 
                        Pattern p = Pattern.compile(
837
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
 
466
                        Pattern p = Pattern.compile( "^" + name + "[ \\t]*=[ \\t]*(.*)$" );
838
467
                        for( int i = 0; i < params.length; i++ ) {
839
468
                                Matcher m = p.matcher( params[ i ] );
840
469
                                if( m.matches() )
841
 
                                        return m.group( 2 );
 
470
                                        return m.group( 1 );
842
471
                        }
843
472
                        return null;
844
473
                }
845
474
 
846
475
                private Set< String > extractTypes( String[] params,
847
 
                        List< String > valid_types )
 
476
                                List< String > validTypes )
848
477
                {
849
478
                        HashSet< String > types = new HashSet< String >();
850
479
 
851
480
                        // get 3.0-style TYPE= param
852
 
                        String type_param;
853
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
854
 
                                String[] parts = type_param.split( "," );
855
 
                                for( int i = 0; i < parts.length; i++ )
856
 
                                        if( valid_types.contains( parts[ i ] ) )
857
 
                                                types.add( parts[ i ] );
 
481
                        String typeParam;
 
482
                        if( ( typeParam = checkParam( params, "TYPE" ) ) != null ) {
 
483
                                String[] bits = typeParam.split( "," );
 
484
                                for( int i = 0; i < bits.length; i++ )
 
485
                                        if( validTypes.contains( bits[ i ] ) )
 
486
                                                types.add( bits[ i ] );
858
487
                        }
859
488
 
860
489
                        // get 2.1-style type param
861
490
                        if( _version.equals( "2.1" ) ) {
862
491
                                for( int i = 1; i < params.length; i++ )
863
 
                                        if( valid_types.contains( params[ i ] ) )
 
492
                                        if( validTypes.contains( params[ i ] ) )
864
493
                                                types.add( params[ i ] );
865
494
                        }
866
495
 
867
496
                        return types;
868
497
                }
869
498
 
870
 
                private UnencodeResult unencodeQuotedPrintable( ByteBuffer in )
 
499
                private String unencodeQuotedPrintable( String str, String charset )
871
500
                {
872
 
                        boolean another = false;
 
501
                        // default encoding scheme
 
502
                        if( charset == null ) charset = "UTF-8";
873
503
 
874
 
                        // unencode quoted-printable encoding, as per RFC1521 section 5.1
875
 
                        byte[] out = new byte[ in.limit() - in.position() ];
 
504
                        // unencode quoted-pritable encoding, as per RFC1521 section 5.1
 
505
                        byte[] bytes = new byte[ str.length() ];
876
506
                        int j = 0;
877
 
                        for( int i = in.position(); i < in.limit(); i++ )
878
 
                        {
879
 
                                // get next char and process...
880
 
                                byte ch = in.array()[ i ];
881
 
                                if( ch == '=' && i < in.limit() - 2 )
882
 
                                {
883
 
                                        // we found a =XX format byte, add it
884
 
                                        out[ j ] = (byte)(
885
 
                                                        Character.digit( in.array()[ i + 1 ], 16 ) * 16 +
886
 
                                                        Character.digit( in.array()[ i + 2 ], 16 ) );
 
507
                        for( int i = 0; i < str.length(); i++, j++ ) {
 
508
                                char ch = str.charAt( i );
 
509
                                if( ch == '=' && i < str.length() - 2 ) {
 
510
                                        bytes[ j ] = (byte)(
 
511
                                                        Character.digit( str.charAt( i + 1 ), 16 ) * 16 +
 
512
                                                        Character.digit( str.charAt( i + 2 ), 16 ) );
887
513
                                        i += 2;
888
514
                                }
889
 
                                else if( ch == '=' && i == in.limit() - 1 )
890
 
                                {
891
 
                                        // we found a '=' at the end of a line signifying a multi-
892
 
                                        // line string, so we don't add it.
893
 
                                        another = true;
894
 
                                        continue;
895
 
                                }
896
515
                                else
897
 
                                        // just a normal char...
898
 
                                        out[ j ] = (byte)ch;
899
 
                                j++;
900
 
                        }
901
 
 
902
 
                        return new UnencodeResult( another, ByteBuffer.wrap( out, 0, j ) );
903
 
                }
904
 
 
905
 
                private ByteBuffer transcodeAsciiToUtf8( ByteBuffer in )
906
 
                {
907
 
                        // transcode
908
 
                        byte[] out = new byte[ ( in.limit() - in.position() ) * 2 ];
909
 
                        int j = 0;
910
 
                        for( int a = in.position(); a < in.limit(); a++ )
911
 
                        {
912
 
                                // if char is < 127, keep it as-is
913
 
                                if( in.array()[ a ] >= 0 )
914
 
                                        out[ j++ ] = in.array()[ a ];
915
 
 
916
 
                                // else, convert it to UTF-8
917
 
                                else {
918
 
                                        int b = 0xff & (int)in.array()[ a ];
919
 
                                        out[ j++ ] = (byte)( 0xc0 | ( b >> 6 ) );
920
 
                                        out[ j++ ] = (byte)( 0x80 | ( b & 0x3f ) );
921
 
                                }
922
 
                        }
923
 
 
924
 
                        return ByteBuffer.wrap( out, 0, j );
 
516
                                        bytes[ j ] = (byte)ch;
 
517
                        }
 
518
                        try {
 
519
                                return new String( bytes, 0, j, charset );
 
520
                        } catch( UnsupportedEncodingException e ) { }
 
521
                        return null;
925
522
                }
926
523
        }
927
524
}