/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-04-22 15:36:06 UTC
  • Revision ID: edam@waxworlds.org-20110422153606-9x9l0nbmvx6oxfxu
- pulled contacts cache out in to seperate class

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
34
34
import java.nio.ByteBuffer;
35
35
import java.util.ArrayList;
36
36
import java.util.Arrays;
37
 
import java.util.HashMap;
38
37
import java.util.HashSet;
39
38
import java.util.Iterator;
40
39
import java.util.List;
48
47
import android.provider.Contacts;
49
48
import android.provider.Contacts.PhonesColumns;
50
49
 
51
 
public class VcardImporter extends Importer
 
50
public class VCFImporter extends Importer
52
51
{
53
 
        private int _vcard_count = 0;
 
52
        private int _vCardCount = 0;
54
53
        private int _progress = 0;
55
54
 
56
 
        public VcardImporter( Doit doit )
 
55
        public VCFImporter( Doit doit )
57
56
        {
58
57
                super( doit );
59
58
        }
110
109
                        countVCardFile( files[ i ] );
111
110
                        setTmpProgress( i );
112
111
                }
113
 
                setProgressMax( _vcard_count ); // will also update tmp progress
 
112
                setProgressMax( _vCardCount );  // will also update tmp progress
114
113
 
115
114
                // import them
116
115
                setProgress( 0 );
117
116
                for( int i = 0; i < files.length; i++ )
118
117
                        importVCardFile( files[ i ] );
119
 
                setProgress( _vcard_count );
120
118
        }
121
119
 
122
120
        private void countVCardFile( File file ) throws AbortImportException
129
127
 
130
128
                        // read
131
129
                        String line;
132
 
                        boolean in_vcard = false;
 
130
                        boolean inVCard = false;
133
131
                        while( ( line = reader.readLine() ) != null )
134
132
                        {
135
 
                                if( !in_vcard ) {
 
133
                                if( !inVCard ) {
136
134
                                        // look for vcard beginning
137
135
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
138
 
                                                in_vcard = true;
139
 
                                                _vcard_count++;
 
136
                                                inVCard = true;
 
137
                                                _vCardCount++;
140
138
                                        }
141
139
                                }
142
140
                                else if( line.matches( "^END:VCARD" ) )
143
 
                                        in_vcard = false;
 
141
                                        inVCard = false;
144
142
                        }
145
143
 
146
144
                }
169
167
                        FileInputStream istream = new FileInputStream( file );
170
168
                        byte[] content = new byte[ (int)file.length() ];
171
169
                        istream.read( content );
172
 
                        istream = null;
173
170
 
174
171
                        // import
175
172
                        importVCardFileContent( content, file.getName() );
187
184
                throws AbortImportException
188
185
        {
189
186
                // go through lines
190
 
                Vcard vcard = null;
191
 
                int vcard_start_line = 0;
 
187
                VCard vCard = null;
192
188
                ContentLineIterator cli = new ContentLineIterator( content );
193
189
                while( cli.hasNext() )
194
190
                {
205
201
                                line = "";
206
202
                        }
207
203
 
208
 
                        if( vcard == null ) {
 
204
                        if( vCard == null ) {
209
205
                                // look for vcard beginning
210
206
                                if( line.matches( "^BEGIN:VCARD" ) ) {
211
 
                                        setProgress( _progress++ );
212
 
                                        vcard = new Vcard();
213
 
                                        vcard_start_line = cli.getLineNumber();
 
207
                                        setProgress( ++_progress );
 
208
                                        vCard = new VCard();
214
209
                                }
215
210
                        }
216
211
                        else {
217
212
                                // look for vcard content or ending
218
213
                                if( line.matches( "^END:VCARD" ) )
219
214
                                {
220
 
                                        // finalise the vcard/contact
 
215
                                        // store vcard and do away with it
221
216
                                        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;
 
217
                                                vCard.finaliseParsing();
 
218
                                                importContact( vCard );
 
219
                                        }
 
220
                                        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
                                        catch( VCard.SkipContactException e ) {
 
230
                                                skipContact();
 
231
                                                // do nothing
 
232
                                        }
 
233
                                        vCard = null;
256
234
                                }
257
235
                                else
258
236
                                {
259
237
                                        // try giving the line to the vcard
260
238
                                        try {
261
 
                                                vcard.parseLine( buffer, line,
 
239
                                                vCard.parseLine( buffer, line,
262
240
                                                        cli.doesNextLineLookFolded() );
263
241
                                        }
264
 
                                        catch( Vcard.ParseException e ) {
 
242
                                        catch( VCard.ParseException e ) {
265
243
                                                skipContact();
266
244
                                                if( !showContinue(
267
245
                                                        getText( R.string.error_vcf_parse ).toString()
268
 
                                                        + fileName +
269
 
                                                        getText( R.string.error_vcf_parse_line ).toString()
270
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
246
                                                        + fileName + "\n" + e.getMessage() ) )
271
247
                                                {
272
248
                                                        finish( ACTION_ABORT );
273
249
                                                }
275
251
                                                // although we're continuing, we still need to abort
276
252
                                                // this vCard. Further lines will be ignored until we
277
253
                                                // get to another BEGIN:VCARD line.
278
 
                                                vcard = null;
 
254
                                                vCard = null;
279
255
                                        }
280
 
                                        catch( Vcard.SkipImportException e ) {
 
256
                                        catch( VCard.SkipContactException e ) {
281
257
                                                skipContact();
282
258
                                                // abort this vCard. Further lines will be ignored until
283
259
                                                // we get to another BEGIN:VCARD line.
284
 
                                                vcard = null;
 
260
                                                vCard = null;
285
261
                                        }
286
262
                                }
287
263
                        }
292
268
        {
293
269
                protected byte[] _content = null;
294
270
                protected int _pos = 0;
295
 
                protected int _line = 0;
296
271
 
297
272
                public ContentLineIterator( byte[] content )
298
273
                {
318
293
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
319
294
                                                _pos > initial_pos )? _pos - 1 : _pos;
320
295
                                        _pos++;
321
 
                                        _line++;
322
296
                                        return ByteBuffer.wrap( _content, initial_pos,
323
297
                                                to - initial_pos );
324
298
                                }
327
301
                        if( _pos != initial_pos ) {
328
302
                                int to = _pos;
329
303
                                _pos++;
330
 
                                _line++;
331
304
                                return ByteBuffer.wrap( _content, initial_pos,
332
305
                                        to - initial_pos );
333
306
                        }
352
325
                        return _pos > 0 && _pos < _content.length &&
353
326
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
354
327
                }
355
 
 
356
 
                public int getLineNumber()
357
 
                {
358
 
                        return _line;
359
 
                }
360
328
        }
361
329
 
362
 
        private class Vcard extends ContactData
 
330
        private class VCard extends ContactData
363
331
        {
364
332
                private final static int NAMELEVEL_NONE = 0;
365
 
                private final static int NAMELEVEL_FN = 1;
366
 
                private final static int NAMELEVEL_N = 2;
 
333
                private final static int NAMELEVEL_ORG = 1;
 
334
                private final static int NAMELEVEL_FN = 2;
 
335
                private final static int NAMELEVEL_N = 3;
367
336
 
368
337
                private final static int MULTILINE_NONE = 0;
369
338
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
376
345
                private int _parser_multiline_state = MULTILINE_NONE;
377
346
                private String _parser_current_name_and_params = null;
378
347
                private String _parser_buffered_value_so_far = "";
379
 
                private String _cached_organisation = null;
380
 
                private String _cached_title = null;
381
348
 
382
349
                protected class UnencodeResult
383
350
                {
413
380
 
414
381
                        public ParseException( int res )
415
382
                        {
416
 
                                super( VcardImporter.this.getText( res ).toString() );
 
383
                                super( VCFImporter.this.getText( res ).toString() );
417
384
                        }
418
385
                }
419
386
 
420
387
                @SuppressWarnings("serial")
421
 
                protected class SkipImportException extends Exception { }
 
388
                protected class SkipContactException extends Exception { }
422
389
 
423
390
                private String extractCollonPartFromLine( ByteBuffer buffer,
424
391
                        String line, boolean former )
460
427
 
461
428
                public void parseLine( ByteBuffer buffer, String line,
462
429
                        boolean next_line_looks_folded )
463
 
                        throws ParseException, SkipImportException,
 
430
                        throws ParseException, SkipContactException,
464
431
                        AbortImportException
465
432
                {
466
433
                        // do we have a version yet?
566
533
                                for( int i = 0; i < name_param_parts.length; i++ )
567
534
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
568
535
 
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
536
                                // parse encoding parameter
578
537
                                String encoding = checkParam( name_param_parts, "ENCODING" );
579
538
                                if( encoding != null ) encoding = encoding.toUpperCase();
580
 
                                if( is_interesting_field && encoding != null &&
581
 
                                        !encoding.equals( "8BIT" ) &&
 
539
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
582
540
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
583
541
                                        //&& !encoding.equals( "BASE64" ) )
584
542
                                {
588
546
                                // parse charset parameter
589
547
                                String charset = checkParam( name_param_parts, "CHARSET" );
590
548
                                if( charset != null ) charset = charset.toUpperCase();
591
 
                                if( charset != null &&
592
 
                                        !charset.equals( "US-ASCII" ) &&
 
549
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
593
550
                                        !charset.equals( "ASCII" ) &&
594
551
                                        !charset.equals( "UTF-8" ) )
595
552
                                {
664
621
                                        parseFN( name_param_parts, complete_value );
665
622
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
666
623
                                        parseORG( name_param_parts, complete_value );
667
 
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
668
 
                                        parseTITLE( name_param_parts, complete_value );
669
624
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
670
625
                                        parseTEL( name_param_parts, complete_value );
671
626
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
728
683
                }
729
684
 
730
685
                private void parseN( String[] params, String value )
 
686
                        throws ParseException, SkipContactException,
 
687
                        AbortImportException
731
688
                {
732
689
                        // already got a better name?
733
690
                        if( _name_level >= NAMELEVEL_N ) return;
745
702
                        // set name
746
703
                        setName( value );
747
704
                        _name_level = NAMELEVEL_N;
 
705
 
 
706
                        // check now to see if we need to import this contact (to avoid
 
707
                        // parsing the rest of the vCard unnecessarily)
 
708
                        if( !isImportRequired( getName() ) )
 
709
                                throw new SkipContactException();
748
710
                }
749
711
 
750
712
                private void parseFN( String[] params, String value )
 
713
                        throws ParseException, SkipContactException
751
714
                {
752
715
                        // already got a better name?
753
716
                        if( _name_level >= NAMELEVEL_FN ) return;
758
721
                }
759
722
 
760
723
                private void parseORG( String[] params, String value )
 
724
                        throws ParseException, SkipContactException
761
725
                {
 
726
                        // already got a better name?
 
727
                        if( _name_level >= NAMELEVEL_ORG ) return;
 
728
 
762
729
                        // get org parts
763
730
                        String[] org_parts = splitValueBySemicolon( value );
764
 
                        if( org_parts == null || org_parts.length < 1 ) return;
765
 
 
766
 
                        // build organisation name
767
 
                        StringBuilder builder = new StringBuilder(
768
 
                                String.valueOf( org_parts[ 0 ] ) );
769
 
                        for( int a = 1; a < org_parts.length; a++ )
770
 
                                builder.append( ", " ).append( org_parts[ a ] );
771
 
                        String organisation = builder.toString();
772
 
 
773
 
                        // set organisation name (using a title we've previously found)
774
 
                        addOrganisation( organisation, _cached_title, true );
775
 
 
776
 
                        // if we've not previously found a title, store this organisation
777
 
                        // name (we'll need it when we find a title to update the
778
 
                        // organisation, by name), else if we *have* previously found a
779
 
                        // title, clear it (since we just used it)
780
 
                        if( _cached_title == null )
781
 
                                _cached_organisation = organisation;
782
 
                        else
783
 
                                _cached_title = null;
784
 
                }
785
 
 
786
 
                private void parseTITLE( String[] params, String value )
787
 
                {
788
 
                        // if we previously had an organisation, look it up and append this
789
 
                        // title to it
790
 
                        if( _cached_organisation != null && hasOrganisations() ) {
791
 
                                HashMap< String, ExtraDetail > datas = getOrganisations();
792
 
                                ExtraDetail detail = datas.get( _cached_organisation );
793
 
                                if( detail != null )
794
 
                                        detail.setExtra( value );
795
 
                        }
796
 
 
797
 
                        // same as when handling organisation, if we've not previously found
798
 
                        // an organisation we store this title, else we clear it (since we
799
 
                        // just appended this title to it)
800
 
                        if( _cached_organisation == null )
801
 
                                _cached_title = value;
802
 
                        else
803
 
                                _cached_organisation = null;
 
731
 
 
732
                        // build name
 
733
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
 
734
                                value = org_parts[ 1 ];
 
735
                        else if( org_parts.length > 1 && org_parts[ 1 ].length() > 0 )
 
736
                                value = org_parts[ 0 ] + ", " + org_parts[ 1 ];
 
737
                        else
 
738
                                value = org_parts[ 0 ];
 
739
 
 
740
                        // set name
 
741
                        setName( value );
 
742
                        _name_level = NAMELEVEL_ORG;
804
743
                }
805
744
 
806
745
                private void parseTEL( String[] params, String value )
 
746
                        throws ParseException
807
747
                {
808
748
                        if( value.length() == 0 ) return;
809
749
 
812
752
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
813
753
 
814
754
                        // here's the logic...
815
 
                        boolean is_preferred = types.contains( "PREF" );
816
 
                        int type;
 
755
                        boolean preferred = types.contains( "PREF" );
 
756
                        int type = PhonesColumns.TYPE_MOBILE;
 
757
                        if( types.contains( "VOICE" ) )
 
758
                                if( types.contains( "WORK" ) )
 
759
                                        type = PhonesColumns.TYPE_WORK;
 
760
                                else
 
761
                                        type = PhonesColumns.TYPE_HOME;
 
762
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
763
                                type = PhonesColumns.TYPE_MOBILE;
817
764
                        if( types.contains( "FAX" ) )
818
765
                                if( types.contains( "HOME" ) )
819
766
                                        type = PhonesColumns.TYPE_FAX_HOME;
820
767
                                else
821
768
                                        type = PhonesColumns.TYPE_FAX_WORK;
822
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
823
 
                                type = PhonesColumns.TYPE_MOBILE;
824
 
                        else if( types.contains( "PAGER" ) )
 
769
                        if( types.contains( "PAGER" ) )
825
770
                                type = PhonesColumns.TYPE_PAGER;
826
 
                        else if( types.contains( "WORK" ) )
827
 
                                type = PhonesColumns.TYPE_WORK;
828
 
                        else
829
 
                                type = PhonesColumns.TYPE_HOME;
830
771
 
831
772
                        // add phone number
832
 
                        addNumber( value, type, is_preferred );
 
773
                        addPhone( value, type, preferred );
833
774
                }
834
775
 
835
776
                public void parseEMAIL( String[] params, String value )
 
777
                        throws ParseException
836
778
                {
837
779
                        if( value.length() == 0 ) return;
838
780
 
840
782
                                "PREF", "WORK", "HOME", "INTERNET" ) );
841
783
 
842
784
                        // add email address
843
 
                        boolean is_preferred = types.contains( "PREF" );
844
 
                        int type;
 
785
                        boolean preferred = types.contains( "PREF" );
845
786
                        if( types.contains( "WORK" ) )
846
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
787
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
847
788
                        else
848
 
                                type = Contacts.ContactMethods.TYPE_HOME;
849
 
 
850
 
                        addEmail( value, type, is_preferred );
 
789
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
851
790
                }
852
791
 
853
792
                private void parseADR( String[] params, String value )
 
793
                        throws ParseException, SkipContactException
854
794
                {
855
795
                        // get address parts
856
796
                        String[] adr_parts = splitValueBySemicolon( value );
866
806
                                "PREF", "WORK", "HOME", "INTERNET" ) );
867
807
 
868
808
                        // add address
869
 
                        int type;
870
809
                        if( types.contains( "WORK" ) )
871
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
810
                                addAddress( value, Contacts.ContactMethods.TYPE_WORK );
872
811
                        else
873
 
                                type = Contacts.ContactMethods.TYPE_HOME;
874
 
 
875
 
                        addAddress( value, type );
 
812
                                addAddress( value, Contacts.ContactMethods.TYPE_HOME);
876
813
                }
877
814
 
878
 
                public void finaliseVcard()
879
 
                        throws ParseException, ContactNotIdentifiableException
 
815
                public void finaliseParsing()
 
816
                        throws ParseException, SkipContactException,
 
817
                        AbortImportException
880
818
                {
881
819
                        // missing version (and data is present)
882
820
                        if( _version == null && _buffers != null )
883
821
                                throw new ParseException( R.string.error_vcf_malformed );
884
822
 
885
 
                        // finalise the parent class
886
 
                        finalise();
 
823
                        //  missing name properties?
 
824
                        if( _name_level == NAMELEVEL_NONE )
 
825
                                throw new ParseException( R.string.error_vcf_noname );
 
826
 
 
827
                        // check if we should import this one? If we've already got an 'N'-
 
828
                        // type name, this will already have been done by parseN() so we
 
829
                        // mustn't do this here (or it could prompt twice!)
 
830
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
 
831
                                throw new SkipContactException();
887
832
                }
888
833
 
889
834
                private String checkParam( String[] params, String name )