/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-02 18:28:24 UTC
  • Revision ID: edam@waxworlds.org-20110502182824-acgdi3qfxfzqgely
- fixed logic for vcard field types (home, work, cell, etc) so it works
- updated NEWS and TODO
- rewrote most of ContactsCache, including a new ContactIdentifier class to identify contacts in the cache and new cache building code
- contacts now identified in the same way that Andoid displays them (by name, or organisation, or number, or email, in that order)
- propper handling and support for organisations and titles
- validation of imported contact now done by Importer, not VcfImporter
- separated sanitisation and normalisation (for cache lookups)
- generacised PhoneData, EmailData and AddressData classes
- ContactData is now aware of primary numbers, emails and organisations (defaults to the first prefrred one seen, or the first one seen where none is preferred)

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