/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 {
216
220
                                        // finalise the vcard/contact
217
221
                                        try {
218
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
 
225
                                                importContact( vcard );
219
226
                                        }
220
227
                                        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
 
 
230
 
                                        // pass the finalised contact to the importer
231
 
                                        importContact( vcard );
232
 
 
233
 
                                        // and discard it
 
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
                                                }
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
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
                                {
835
876
                }
836
877
 
837
878
                public void finaliseVcard()
838
 
                        throws ParseException
 
879
                        throws ParseException, ContactNotIdentifiableException
839
880
                {
840
881
                        // missing version (and data is present)
841
882
                        if( _version == null && _buffers != null )
842
883
                                throw new ParseException( R.string.error_vcf_malformed );
843
884
 
844
885
                        // finalise the parent class
845
 
                        try {
846
 
                                finalise();
847
 
                        }
848
 
                        catch( ContactNotIdentifiableException e ) {
849
 
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
850
 
                        }
 
886
                        finalise();
851
887
                }
852
888
 
853
889
                private String checkParam( String[] params, String name )