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

  • Committer: edam
  • Date: 2011-05-05 21:49:43 UTC
  • Revision ID: edam@waxworlds.org-20110505214943-bg0cn6qz0gr49dlk
- updated TODO
- made varibale names consistent (camelCaseVariables now_use_underscores)

Show diffs side-by-side

added added

removed removed

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