/android/import-contacts

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

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2011-05-30 19:20:17 UTC
  • Revision ID: edam@waxworlds.org-20110530192017-5c09k4kgpov02gja
- added checks for Doit.this == null when handling dialog buttons (I managed to abort an import as a duplicate contacts dialog was shown, but can't reproduce it now)
- added line no.s to vcard parsing errors
- update progress bar after a contact is imported, not before
- fixed bug introduced in last commit where a contacts were imported after finaliseVcard()ing failed
- don't show unknown encoding errors for vcard fields that we don't care about (which ignores base64 encoded photos, for example)

Show diffs side-by-side

added added

removed removed

116
116
                setProgress( 0 );
117
117
                for( int i = 0; i < files.length; i++ )
118
118
                        importVCardFile( files[ i ] );
 
119
                setProgress( _vcard_count );
119
120
        }
120
121
 
121
122
        private void countVCardFile( File file ) throws AbortImportException
168
169
                        FileInputStream istream = new FileInputStream( file );
169
170
                        byte[] content = new byte[ (int)file.length() ];
170
171
                        istream.read( content );
 
172
                        istream = null;
171
173
 
172
174
                        // import
173
175
                        importVCardFileContent( content, file.getName() );
186
188
        {
187
189
                // go through lines
188
190
                Vcard vcard = null;
 
191
                int vcard_start_line = 0;
189
192
                ContentLineIterator cli = new ContentLineIterator( content );
190
193
                while( cli.hasNext() )
191
194
                {
205
208
                        if( vcard == null ) {
206
209
                                // look for vcard beginning
207
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
208
 
                                        setProgress( ++_progress );
 
211
                                        setProgress( _progress++ );
209
212
                                        vcard = new Vcard();
 
213
                                        vcard_start_line = cli.getLineNumber();
210
214
                                }
211
215
                        }
212
216
                        else {
213
217
                                // look for vcard content or ending
214
218
                                if( line.matches( "^END:VCARD" ) )
215
219
                                {
216
 
                                        // store vcard and do away with it
 
220
                                        // finalise the vcard/contact
217
221
                                        try {
218
 
                                                vcard.finaliseParsing();
 
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
219
225
                                                importContact( vcard );
220
226
                                        }
221
227
                                        catch( Vcard.ParseException e ) {
222
 
                                                skipContact();
223
 
                                                if( !showContinue(
224
 
                                                        getText( R.string.error_vcf_parse ).toString()
225
 
                                                        + fileName + "\n" + e.getMessage() ) )
226
 
                                                {
227
 
                                                        finish( ACTION_ABORT );
228
 
                                                }
229
 
                                        }
230
 
                                        catch( Vcard.SkipContactException e ) {
231
 
                                                skipContact();
232
 
                                                // do nothing
233
 
                                        }
 
228
                                                if( !showContinue(
 
229
                                                        getText( R.string.error_vcf_parse ).toString()
 
230
                                                        + fileName +
 
231
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
232
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
233
                                                {
 
234
                                                        finish( ACTION_ABORT );
 
235
                                                }
 
236
                                                else
 
237
                                                        skipContact();
 
238
                                        }
 
239
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
240
                                                if( !showContinue(
 
241
                                                        getText( R.string.error_vcf_parse ).toString()
 
242
                                                        + fileName +
 
243
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
244
                                                        + vcard_start_line + ":\n" + getText(
 
245
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
246
                                                ) )
 
247
                                                {
 
248
                                                        finish( ACTION_ABORT );
 
249
                                                }
 
250
                                                else
 
251
                                                        skipContact();
 
252
                                        }
 
253
 
 
254
                                        // discard this vcard
234
255
                                        vcard = null;
235
256
                                }
236
257
                                else
244
265
                                                skipContact();
245
266
                                                if( !showContinue(
246
267
                                                        getText( R.string.error_vcf_parse ).toString()
247
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
268
                                                        + fileName +
 
269
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
270
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
248
271
                                                {
249
272
                                                        finish( ACTION_ABORT );
250
273
                                                }
254
277
                                                // get to another BEGIN:VCARD line.
255
278
                                                vcard = null;
256
279
                                        }
257
 
                                        catch( Vcard.SkipContactException e ) {
 
280
                                        catch( Vcard.SkipImportException e ) {
258
281
                                                skipContact();
259
282
                                                // abort this vCard. Further lines will be ignored until
260
283
                                                // we get to another BEGIN:VCARD line.
269
292
        {
270
293
                protected byte[] _content = null;
271
294
                protected int _pos = 0;
 
295
                protected int _line = 0;
272
296
 
273
297
                public ContentLineIterator( byte[] content )
274
298
                {
294
318
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
295
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
296
320
                                        _pos++;
 
321
                                        _line++;
297
322
                                        return ByteBuffer.wrap( _content, initial_pos,
298
323
                                                to - initial_pos );
299
324
                                }
302
327
                        if( _pos != initial_pos ) {
303
328
                                int to = _pos;
304
329
                                _pos++;
 
330
                                _line++;
305
331
                                return ByteBuffer.wrap( _content, initial_pos,
306
332
                                        to - initial_pos );
307
333
                        }
326
352
                        return _pos > 0 && _pos < _content.length &&
327
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
328
354
                }
 
355
 
 
356
                public int getLineNumber()
 
357
                {
 
358
                        return _line;
 
359
                }
329
360
        }
330
361
 
331
362
        private class Vcard extends ContactData
387
418
                }
388
419
 
389
420
                @SuppressWarnings("serial")
390
 
                protected class SkipContactException extends Exception { }
 
421
                protected class SkipImportException extends Exception { }
391
422
 
392
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
393
424
                        String line, boolean former )
429
460
 
430
461
                public void parseLine( ByteBuffer buffer, String line,
431
462
                        boolean next_line_looks_folded )
432
 
                        throws ParseException, SkipContactException,
 
463
                        throws ParseException, SkipImportException,
433
464
                        AbortImportException
434
465
                {
435
466
                        // do we have a version yet?
535
566
                                for( int i = 0; i < name_param_parts.length; i++ )
536
567
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
537
568
 
 
569
                                // determine whether we care about this entry
 
570
                                final HashSet< String > interesting_fields =
 
571
                                        new HashSet< String >( Arrays.asList( new String[]
 
572
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
 
573
                                ) );
 
574
                                boolean is_interesting_field =
 
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
 
576
 
538
577
                                // parse encoding parameter
539
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
540
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
541
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
580
                                if( is_interesting_field && encoding != null &&
 
581
                                        !encoding.equals( "8BIT" ) &&
542
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
543
583
                                        //&& !encoding.equals( "BASE64" ) )
544
584
                                {
548
588
                                // parse charset parameter
549
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
550
590
                                if( charset != null ) charset = charset.toUpperCase();
551
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
591
                                if( charset != null &&
 
592
                                        !charset.equals( "US-ASCII" ) &&
552
593
                                        !charset.equals( "ASCII" ) &&
553
594
                                        !charset.equals( "UTF-8" ) )
554
595
                                {
834
875
                        addAddress( value, type );
835
876
                }
836
877
 
837
 
                public void finaliseParsing()
838
 
                        throws ParseException, SkipContactException,
839
 
                        AbortImportException
 
878
                public void finaliseVcard()
 
879
                        throws ParseException, ContactNotIdentifiableException
840
880
                {
841
881
                        // missing version (and data is present)
842
882
                        if( _version == null && _buffers != null )
843
883
                                throw new ParseException( R.string.error_vcf_malformed );
844
884
 
845
 
                        // check if we should import this contact
846
 
                        try {
847
 
                                if( !isImportRequired( this ) )
848
 
                                        throw new SkipContactException();
849
 
                        }
850
 
                        catch( ContactNeedsMoreInfoException e ) {
851
 
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
852
 
                        }
 
885
                        // finalise the parent class
 
886
                        finalise();
853
887
                }
854
888
 
855
889
                private String checkParam( String[] params, String name )