/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

44
44
import java.util.regex.Matcher;
45
45
import java.util.regex.Pattern;
46
46
 
47
 
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
48
 
 
49
47
import android.content.SharedPreferences;
50
48
import android.provider.Contacts;
51
49
import android.provider.Contacts.PhonesColumns;
52
50
 
53
 
public class VCFImporter extends Importer
 
51
public class VcardImporter extends Importer
54
52
{
55
 
        private int _vCardCount = 0;
 
53
        private int _vcard_count = 0;
56
54
        private int _progress = 0;
57
55
 
58
 
        public VCFImporter( Doit doit )
 
56
        public VcardImporter( Doit doit )
59
57
        {
60
58
                super( doit );
61
59
        }
112
110
                        countVCardFile( files[ i ] );
113
111
                        setTmpProgress( i );
114
112
                }
115
 
                setProgressMax( _vCardCount );  // will also update tmp progress
 
113
                setProgressMax( _vcard_count ); // will also update tmp progress
116
114
 
117
115
                // import them
118
116
                setProgress( 0 );
119
117
                for( int i = 0; i < files.length; i++ )
120
118
                        importVCardFile( files[ i ] );
 
119
                setProgress( _vcard_count );
121
120
        }
122
121
 
123
122
        private void countVCardFile( File file ) throws AbortImportException
130
129
 
131
130
                        // read
132
131
                        String line;
133
 
                        boolean inVCard = false;
 
132
                        boolean in_vcard = false;
134
133
                        while( ( line = reader.readLine() ) != null )
135
134
                        {
136
 
                                if( !inVCard ) {
 
135
                                if( !in_vcard ) {
137
136
                                        // look for vcard beginning
138
137
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
139
 
                                                inVCard = true;
140
 
                                                _vCardCount++;
 
138
                                                in_vcard = true;
 
139
                                                _vcard_count++;
141
140
                                        }
142
141
                                }
143
142
                                else if( line.matches( "^END:VCARD" ) )
144
 
                                        inVCard = false;
 
143
                                        in_vcard = false;
145
144
                        }
146
145
 
147
146
                }
170
169
                        FileInputStream istream = new FileInputStream( file );
171
170
                        byte[] content = new byte[ (int)file.length() ];
172
171
                        istream.read( content );
 
172
                        istream = null;
173
173
 
174
174
                        // import
175
175
                        importVCardFileContent( content, file.getName() );
187
187
                throws AbortImportException
188
188
        {
189
189
                // go through lines
190
 
                VCard vCard = null;
 
190
                Vcard vcard = null;
 
191
                int vcard_start_line = 0;
191
192
                ContentLineIterator cli = new ContentLineIterator( content );
192
193
                while( cli.hasNext() )
193
194
                {
204
205
                                line = "";
205
206
                        }
206
207
 
207
 
                        if( vCard == null ) {
 
208
                        if( vcard == null ) {
208
209
                                // look for vcard beginning
209
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
210
 
                                        setProgress( ++_progress );
211
 
                                        vCard = new VCard();
 
211
                                        setProgress( _progress++ );
 
212
                                        vcard = new Vcard();
 
213
                                        vcard_start_line = cli.getLineNumber();
212
214
                                }
213
215
                        }
214
216
                        else {
215
217
                                // look for vcard content or ending
216
218
                                if( line.matches( "^END:VCARD" ) )
217
219
                                {
218
 
                                        // store vcard and do away with it
 
220
                                        // finalise the vcard/contact
219
221
                                        try {
220
 
                                                vCard.finaliseParsing();
221
 
                                                importContact( vCard );
222
 
                                        }
223
 
                                        catch( VCard.ParseException e ) {
224
 
                                                skipContact();
225
 
                                                if( !showContinue(
226
 
                                                        getText( R.string.error_vcf_parse ).toString()
227
 
                                                        + fileName + "\n" + e.getMessage() ) )
228
 
                                                {
229
 
                                                        finish( ACTION_ABORT );
230
 
                                                }
231
 
                                        }
232
 
                                        catch( VCard.SkipContactException e ) {
233
 
                                                skipContact();
234
 
                                                // do nothing
235
 
                                        }
236
 
                                        vCard = null;
 
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
 
225
                                                importContact( vcard );
 
226
                                        }
 
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
 
255
                                        vcard = null;
237
256
                                }
238
257
                                else
239
258
                                {
240
259
                                        // try giving the line to the vcard
241
260
                                        try {
242
 
                                                vCard.parseLine( buffer, line,
 
261
                                                vcard.parseLine( buffer, line,
243
262
                                                        cli.doesNextLineLookFolded() );
244
263
                                        }
245
 
                                        catch( VCard.ParseException e ) {
 
264
                                        catch( Vcard.ParseException e ) {
246
265
                                                skipContact();
247
266
                                                if( !showContinue(
248
267
                                                        getText( R.string.error_vcf_parse ).toString()
249
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
268
                                                        + fileName +
 
269
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
270
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
250
271
                                                {
251
272
                                                        finish( ACTION_ABORT );
252
273
                                                }
254
275
                                                // although we're continuing, we still need to abort
255
276
                                                // this vCard. Further lines will be ignored until we
256
277
                                                // get to another BEGIN:VCARD line.
257
 
                                                vCard = null;
 
278
                                                vcard = null;
258
279
                                        }
259
 
                                        catch( VCard.SkipContactException e ) {
 
280
                                        catch( Vcard.SkipImportException e ) {
260
281
                                                skipContact();
261
282
                                                // abort this vCard. Further lines will be ignored until
262
283
                                                // we get to another BEGIN:VCARD line.
263
 
                                                vCard = null;
 
284
                                                vcard = null;
264
285
                                        }
265
286
                                }
266
287
                        }
271
292
        {
272
293
                protected byte[] _content = null;
273
294
                protected int _pos = 0;
 
295
                protected int _line = 0;
274
296
 
275
297
                public ContentLineIterator( byte[] content )
276
298
                {
296
318
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
297
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
298
320
                                        _pos++;
 
321
                                        _line++;
299
322
                                        return ByteBuffer.wrap( _content, initial_pos,
300
323
                                                to - initial_pos );
301
324
                                }
304
327
                        if( _pos != initial_pos ) {
305
328
                                int to = _pos;
306
329
                                _pos++;
 
330
                                _line++;
307
331
                                return ByteBuffer.wrap( _content, initial_pos,
308
332
                                        to - initial_pos );
309
333
                        }
328
352
                        return _pos > 0 && _pos < _content.length &&
329
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
330
354
                }
 
355
 
 
356
                public int getLineNumber()
 
357
                {
 
358
                        return _line;
 
359
                }
331
360
        }
332
361
 
333
 
        private class VCard extends ContactData
 
362
        private class Vcard extends ContactData
334
363
        {
335
364
                private final static int NAMELEVEL_NONE = 0;
336
365
                private final static int NAMELEVEL_FN = 1;
384
413
 
385
414
                        public ParseException( int res )
386
415
                        {
387
 
                                super( VCFImporter.this.getText( res ).toString() );
 
416
                                super( VcardImporter.this.getText( res ).toString() );
388
417
                        }
389
418
                }
390
419
 
391
420
                @SuppressWarnings("serial")
392
 
                protected class SkipContactException extends Exception { }
 
421
                protected class SkipImportException extends Exception { }
393
422
 
394
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
395
424
                        String line, boolean former )
431
460
 
432
461
                public void parseLine( ByteBuffer buffer, String line,
433
462
                        boolean next_line_looks_folded )
434
 
                        throws ParseException, SkipContactException,
 
463
                        throws ParseException, SkipImportException,
435
464
                        AbortImportException
436
465
                {
437
466
                        // do we have a version yet?
537
566
                                for( int i = 0; i < name_param_parts.length; i++ )
538
567
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
539
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
 
540
577
                                // parse encoding parameter
541
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
542
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
543
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
580
                                if( is_interesting_field && encoding != null &&
 
581
                                        !encoding.equals( "8BIT" ) &&
544
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
545
583
                                        //&& !encoding.equals( "BASE64" ) )
546
584
                                {
550
588
                                // parse charset parameter
551
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
552
590
                                if( charset != null ) charset = charset.toUpperCase();
553
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
591
                                if( charset != null &&
 
592
                                        !charset.equals( "US-ASCII" ) &&
554
593
                                        !charset.equals( "ASCII" ) &&
555
594
                                        !charset.equals( "UTF-8" ) )
556
595
                                {
773
812
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
774
813
 
775
814
                        // here's the logic...
776
 
                        boolean preferred = types.contains( "PREF" );
 
815
                        boolean is_preferred = types.contains( "PREF" );
777
816
                        int type;
778
817
                        if( types.contains( "FAX" ) )
779
818
                                if( types.contains( "HOME" ) )
790
829
                                type = PhonesColumns.TYPE_HOME;
791
830
 
792
831
                        // add phone number
793
 
                        addNumber( value, type, preferred );
 
832
                        addNumber( value, type, is_preferred );
794
833
                }
795
834
 
796
835
                public void parseEMAIL( String[] params, String value )
801
840
                                "PREF", "WORK", "HOME", "INTERNET" ) );
802
841
 
803
842
                        // add email address
804
 
                        boolean preferred = types.contains( "PREF" );
 
843
                        boolean is_preferred = types.contains( "PREF" );
805
844
                        int type;
806
845
                        if( types.contains( "WORK" ) )
807
846
                                type = Contacts.ContactMethods.TYPE_WORK;
808
847
                        else
809
848
                                type = Contacts.ContactMethods.TYPE_HOME;
810
849
 
811
 
                        addEmail( value, type, preferred );
 
850
                        addEmail( value, type, is_preferred );
812
851
                }
813
852
 
814
853
                private void parseADR( String[] params, String value )
836
875
                        addAddress( value, type );
837
876
                }
838
877
 
839
 
                public void finaliseParsing()
840
 
                        throws ParseException, SkipContactException,
841
 
                        AbortImportException
 
878
                public void finaliseVcard()
 
879
                        throws ParseException, ContactNotIdentifiableException
842
880
                {
843
881
                        // missing version (and data is present)
844
882
                        if( _version == null && _buffers != null )
845
883
                                throw new ParseException( R.string.error_vcf_malformed );
846
884
 
847
 
                        // check if we should import this contact
848
 
                        try {
849
 
                                if( !isImportRequired( this ) )
850
 
                                        throw new SkipContactException();
851
 
                        }
852
 
                        catch( ContactNeedsMoreInfoException e ) {
853
 
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
854
 
                        }
 
885
                        // finalise the parent class
 
886
                        finalise();
855
887
                }
856
888
 
857
889
                private String checkParam( String[] params, String name )