/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

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 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
32
32
import java.io.IOException;
33
33
import java.io.UnsupportedEncodingException;
34
34
import java.nio.ByteBuffer;
 
35
import java.util.ArrayList;
35
36
import java.util.Arrays;
 
37
import java.util.HashMap;
36
38
import java.util.HashSet;
37
39
import java.util.Iterator;
38
40
import java.util.List;
 
41
import java.util.NoSuchElementException;
39
42
import java.util.Set;
40
43
import java.util.Vector;
41
44
import java.util.regex.Matcher;
42
45
import java.util.regex.Pattern;
43
 
import java.util.NoSuchElementException;
44
 
import java.lang.UnsupportedOperationException;
45
46
 
46
47
import android.content.SharedPreferences;
47
48
import android.provider.Contacts;
49
50
 
50
51
public class VCFImporter extends Importer
51
52
{
52
 
        private int _vCardCount = 0;
 
53
        private int _vcard_count = 0;
53
54
        private int _progress = 0;
54
55
 
55
56
        public VCFImporter( Doit doit )
109
110
                        countVCardFile( files[ i ] );
110
111
                        setTmpProgress( i );
111
112
                }
112
 
                setProgressMax( _vCardCount );  // will also update tmp progress
 
113
                setProgressMax( _vcard_count ); // will also update tmp progress
113
114
 
114
115
                // import them
115
116
                setProgress( 0 );
127
128
 
128
129
                        // read
129
130
                        String line;
130
 
                        boolean inVCard = false;
 
131
                        boolean in_vcard = false;
131
132
                        while( ( line = reader.readLine() ) != null )
132
133
                        {
133
 
                                if( !inVCard ) {
 
134
                                if( !in_vcard ) {
134
135
                                        // look for vcard beginning
135
136
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
136
 
                                                inVCard = true;
137
 
                                                _vCardCount++;
 
137
                                                in_vcard = true;
 
138
                                                _vcard_count++;
138
139
                                        }
139
140
                                }
140
141
                                else if( line.matches( "^END:VCARD" ) )
141
 
                                        inVCard = false;
 
142
                                        in_vcard = false;
142
143
                        }
143
144
 
144
145
                }
184
185
                throws AbortImportException
185
186
        {
186
187
                // go through lines
187
 
                VCard vCard = null;
 
188
                VCard vcard = null;
188
189
                ContentLineIterator cli = new ContentLineIterator( content );
189
190
                while( cli.hasNext() )
190
191
                {
201
202
                                line = "";
202
203
                        }
203
204
 
204
 
                        if( vCard == null ) {
 
205
                        if( vcard == null ) {
205
206
                                // look for vcard beginning
206
207
                                if( line.matches( "^BEGIN:VCARD" ) ) {
207
208
                                        setProgress( ++_progress );
208
 
                                        vCard = new VCard();
 
209
                                        vcard = new VCard();
209
210
                                }
210
211
                        }
211
212
                        else {
214
215
                                {
215
216
                                        // store vcard and do away with it
216
217
                                        try {
217
 
                                                vCard.finaliseParsing();
218
 
                                                importContact( vCard );
 
218
                                                vcard.finaliseParsing();
 
219
                                                importContact( vcard );
219
220
                                        }
220
221
                                        catch( VCard.ParseException e ) {
221
222
                                                skipContact();
230
231
                                                skipContact();
231
232
                                                // do nothing
232
233
                                        }
233
 
                                        vCard = null;
 
234
                                        vcard = null;
234
235
                                }
235
236
                                else
236
237
                                {
237
238
                                        // try giving the line to the vcard
238
239
                                        try {
239
 
                                                vCard.parseLine( buffer, line,
 
240
                                                vcard.parseLine( buffer, line,
240
241
                                                        cli.doesNextLineLookFolded() );
241
242
                                        }
242
243
                                        catch( VCard.ParseException e ) {
251
252
                                                // although we're continuing, we still need to abort
252
253
                                                // this vCard. Further lines will be ignored until we
253
254
                                                // get to another BEGIN:VCARD line.
254
 
                                                vCard = null;
 
255
                                                vcard = null;
255
256
                                        }
256
257
                                        catch( VCard.SkipContactException e ) {
257
258
                                                skipContact();
258
259
                                                // abort this vCard. Further lines will be ignored until
259
260
                                                // we get to another BEGIN:VCARD line.
260
 
                                                vCard = null;
 
261
                                                vcard = null;
261
262
                                        }
262
263
                                }
263
264
                        }
330
331
        private class VCard extends ContactData
331
332
        {
332
333
                private final static int NAMELEVEL_NONE = 0;
333
 
                private final static int NAMELEVEL_ORG = 1;
334
 
                private final static int NAMELEVEL_FN = 2;
335
 
                private final static int NAMELEVEL_N = 3;
 
334
                private final static int NAMELEVEL_FN = 1;
 
335
                private final static int NAMELEVEL_N = 2;
 
336
 
 
337
                private final static int MULTILINE_NONE = 0;
 
338
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
 
339
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
 
340
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
336
341
 
337
342
                private String _version = null;
338
343
                private Vector< ByteBuffer > _buffers = null;
339
344
                private int _name_level = NAMELEVEL_NONE;
340
 
                private boolean _parser_in_encoded_multiline = false;
341
 
                private boolean _parser_in_folded_multiline = false;
 
345
                private int _parser_multiline_state = MULTILINE_NONE;
342
346
                private String _parser_current_name_and_params = null;
343
347
                private String _parser_buffered_value_so_far = "";
 
348
                private String _cached_organisation = null;
 
349
                private String _cached_title = null;
344
350
 
345
351
                protected class UnencodeResult
346
352
                {
469
475
                                String name_and_params;
470
476
                                int pos;
471
477
 
472
 
                                if( _parser_in_encoded_multiline ||
473
 
                                        _parser_in_folded_multiline )
 
478
                                if( _parser_multiline_state != MULTILINE_NONE )
474
479
                                {
475
480
                                        // if we're currently in a multi-line value, use the stored
476
481
                                        // property name and parameters
477
482
                                        name_and_params = _parser_current_name_and_params;
478
483
 
 
484
                                        // skip some initial line characters, depending on the type
 
485
                                        // of multi-line we're handling
479
486
                                        pos = buffer.position();
480
 
 
481
 
                                        // for folded multi-lines, skip the single space at the
482
 
                                        // start of the next line
483
 
                                        if( _parser_in_folded_multiline )
 
487
                                        switch( _parser_multiline_state )
 
488
                                        {
 
489
                                        case MULTILINE_FOLDED:
484
490
                                                pos++;
485
 
 
486
 
                                        // else, this must be an encoded multi-line, so skip any
487
 
                                        // whitespace we find at the start of the next line
488
 
                                        else
 
491
                                                break;
 
492
                                        case MULTILINE_ENCODED:
489
493
                                                while( pos < buffer.limit() && (
490
494
                                                        buffer.get( pos ) == ' ' ||
491
495
                                                        buffer.get( pos ) == '\t' ) )
492
496
                                                {
493
497
                                                        pos++;
494
498
                                                }
 
499
                                                break;
 
500
                                        default:
 
501
                                                // do nothing
 
502
                                        }
 
503
 
 
504
                                        // take us out of multi-line so that we can re-detect that
 
505
                                        // this line is a multi-line or not
 
506
                                        _parser_multiline_state = MULTILINE_NONE;
495
507
                                }
496
508
                                else
497
509
                                {
552
564
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
553
565
                                if( unencoding_result != null ) {
554
566
                                        value = unencoding_result.getBuffer();
555
 
                                        _parser_in_encoded_multiline =
556
 
                                                unencoding_result.isAnotherLineRequired();
 
567
                                        if( unencoding_result.isAnotherLineRequired() )
 
568
                                                _parser_multiline_state = MULTILINE_ENCODED;
557
569
                                }
558
570
 
559
571
                                // convert 8-bit ASCII charset to US-ASCII
571
583
                                        throw new ParseException( R.string.error_vcf_charset );
572
584
                                }
573
585
 
 
586
                                // for some entries that have semicolon-separated value parts,
 
587
                                // check to see if the value ends in an escape character, which
 
588
                                // indicates that we have a multi-line value
 
589
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
 
590
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
 
591
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
 
592
                                        doesStringEndInAnEscapeChar( string_value ) )
 
593
                                {
 
594
                                        _parser_multiline_state = MULTILINE_ESCAPED;
 
595
                                        string_value = string_value.substring( 0,
 
596
                                                string_value.length() - 1 );
 
597
                                }
 
598
 
574
599
                                // now we know whether we're in an encoding multi-line,
575
600
                                // determine if we're in a v3 folded multi-line or not
576
 
                                _parser_in_folded_multiline = !_parser_in_encoded_multiline &&
577
 
                                        _version.equals( "3.0" ) && next_line_looks_folded;
 
601
                                if( _parser_multiline_state == MULTILINE_NONE &&
 
602
                                        _version.equals( "3.0" ) && next_line_looks_folded )
 
603
                                {
 
604
                                        _parser_multiline_state = MULTILINE_FOLDED;
 
605
                                }
578
606
 
579
 
                                // handle multi-line requests
580
 
                                if( _parser_in_encoded_multiline ||
581
 
                                        _parser_in_folded_multiline )
582
 
                                {
 
607
                                // handle multi-lines by buffering them and parsing them when we
 
608
                                // are processing the last line in a multi-line sequence
 
609
                                if( _parser_multiline_state != MULTILINE_NONE ) {
583
610
                                        _parser_buffered_value_so_far += string_value;
584
611
                                        return;
585
612
                                }
586
 
 
587
 
                                // add on buffered multi-line content
588
613
                                String complete_value =
589
 
                                        _parser_buffered_value_so_far + string_value;
 
614
                                        ( _parser_buffered_value_so_far + string_value ).trim();
590
615
 
591
616
                                // ignore empty values
592
617
                                if( complete_value.length() < 1 ) return;
598
623
                                        parseFN( name_param_parts, complete_value );
599
624
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
600
625
                                        parseORG( name_param_parts, complete_value );
 
626
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
627
                                        parseTITLE( name_param_parts, complete_value );
601
628
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
602
629
                                        parseTEL( name_param_parts, complete_value );
603
630
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
604
631
                                        parseEMAIL( name_param_parts, complete_value );
605
 
                        }
 
632
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
 
633
                                        parseADR( name_param_parts, complete_value );
 
634
                        }
 
635
                }
 
636
 
 
637
                private boolean doesStringEndInAnEscapeChar( String string )
 
638
                {
 
639
                        // count the number of backslashes at the end of the string
 
640
                        int count = 0;
 
641
                        for( int a = string.length() - 1; a >= 0; a-- )
 
642
                                if( string.charAt( a ) == '\\' )
 
643
                                        count++;
 
644
                                else
 
645
                                        break;
 
646
 
 
647
                        // if there are an even number of backslashes then the final one
 
648
                        // doesn't count
 
649
                        return ( count & 1 ) == 1;
 
650
                }
 
651
 
 
652
                private String[] splitValueBySemicolon( String value )
 
653
                {
 
654
                        // split string in to parts by semicolon
 
655
                        ArrayList< String > parts = new ArrayList< String >(
 
656
                                Arrays.asList( value.split(  ";" ) ) );
 
657
 
 
658
                        // go through parts
 
659
                        for( int a = 0; a < parts.size(); a++ )
 
660
                        {
 
661
                                String str = parts.get( a );
 
662
 
 
663
                                // look for parts that end in an escape character, but ignore
 
664
                                // the final part. We've already detected escape chars at the
 
665
                                // end of the final part in parseLine() and handled multi-lines
 
666
                                // accordingly.
 
667
                                if( a < parts.size() - 1 &&
 
668
                                        doesStringEndInAnEscapeChar( str ) )
 
669
                                {
 
670
                                        // join the next part to this part and remove the next part
 
671
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
 
672
                                                ';' + parts.get( a + 1 ) );
 
673
                                        parts.remove( a + 1 );
 
674
 
 
675
                                        // re-visit this part
 
676
                                        a--;
 
677
                                        continue;
 
678
                                }
 
679
 
 
680
                                // trim and replace string
 
681
                                str = str.trim();
 
682
                                parts.set( a, str );
 
683
                        }
 
684
 
 
685
                        String[] ret = new String[ parts.size() ];
 
686
                        return parts.toArray( ret );
606
687
                }
607
688
 
608
689
                private void parseN( String[] params, String value )
609
 
                        throws ParseException, SkipContactException,
610
 
                        AbortImportException
611
690
                {
612
691
                        // already got a better name?
613
692
                        if( _name_level >= NAMELEVEL_N ) return;
614
693
 
615
694
                        // get name parts
616
 
                        String[] name_parts = value.split( ";" );
617
 
                        for( int i = 0; i < name_parts.length; i++ )
618
 
                                name_parts[ i ] = name_parts[ i ].trim();
 
695
                        String[] name_parts = splitValueBySemicolon( value );
619
696
 
620
697
                        // build name
621
698
                        value = "";
627
704
                        // set name
628
705
                        setName( value );
629
706
                        _name_level = NAMELEVEL_N;
630
 
 
631
 
                        // check now to see if we need to import this contact (to avoid
632
 
                        // parsing the rest of the vCard unnecessarily)
633
 
                        if( !isImportRequired( getName() ) )
634
 
                                throw new SkipContactException();
635
707
                }
636
708
 
637
709
                private void parseFN( String[] params, String value )
638
 
                        throws ParseException, SkipContactException
639
710
                {
640
711
                        // already got a better name?
641
712
                        if( _name_level >= NAMELEVEL_FN ) return;
646
717
                }
647
718
 
648
719
                private void parseORG( String[] params, String value )
649
 
                        throws ParseException, SkipContactException
650
720
                {
651
 
                        // already got a better name?
652
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
653
 
 
654
721
                        // get org parts
655
 
                        String[] org_parts = value.split( ";" );
656
 
                        for( int i = 0; i < org_parts.length; i++ )
657
 
                                org_parts[ i ] = org_parts[ i ].trim();
658
 
 
659
 
                        // build name
660
 
                        if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
661
 
                                value = org_parts[ 1 ];
662
 
                        else
663
 
                                value = org_parts[ 0 ];
664
 
 
665
 
                        // set name
666
 
                        setName( value );
667
 
                        _name_level = NAMELEVEL_ORG;
 
722
                        String[] org_parts = splitValueBySemicolon( value );
 
723
                        if( org_parts == null || org_parts.length < 1 ) return;
 
724
 
 
725
                        // build organisation name
 
726
                        StringBuilder builder = new StringBuilder(
 
727
                                String.valueOf( org_parts[ 0 ] ) );
 
728
                        for( int a = 1; a < org_parts.length; a++ )
 
729
                                builder.append( ", " ).append( org_parts[ a ] );
 
730
                        String organisation = builder.toString();
 
731
 
 
732
                        // set organisation name (using a title we've previously found)
 
733
                        addOrganisation( organisation, _cached_title, true );
 
734
 
 
735
                        // if we've not previously found a title, store this organisation
 
736
                        // name (we'll need it when we find a title to update the
 
737
                        // organisation, by name), else if we *have* previously found a
 
738
                        // title, clear it (since we just used it)
 
739
                        if( _cached_title == null )
 
740
                                _cached_organisation = organisation;
 
741
                        else
 
742
                                _cached_title = null;
 
743
                }
 
744
 
 
745
                private void parseTITLE( String[] params, String value )
 
746
                {
 
747
                        // if we previously had an organisation, look it up and append this
 
748
                        // title to it
 
749
                        if( _cached_organisation != null && hasOrganisations() ) {
 
750
                                HashMap< String, ExtraDetail > datas = getOrganisations();
 
751
                                ExtraDetail detail = datas.get( _cached_organisation );
 
752
                                if( detail != null )
 
753
                                        detail.setExtra( value );
 
754
                        }
 
755
 
 
756
                        // same as when handling organisation, if we've not previously found
 
757
                        // an organisation we store this title, else we clear it (since we
 
758
                        // just appended this title to it)
 
759
                        if( _cached_organisation == null )
 
760
                                _cached_title = value;
 
761
                        else
 
762
                                _cached_organisation = null;
668
763
                }
669
764
 
670
765
                private void parseTEL( String[] params, String value )
671
 
                        throws ParseException
672
766
                {
673
767
                        if( value.length() == 0 ) return;
674
768
 
677
771
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
678
772
 
679
773
                        // here's the logic...
680
 
                        boolean preferred = types.contains( "PREF" );
681
 
                        int type = PhonesColumns.TYPE_MOBILE;
682
 
                        if( types.contains( "VOICE" ) )
683
 
                                if( types.contains( "WORK" ) )
684
 
                                        type = PhonesColumns.TYPE_WORK;
685
 
                                else
686
 
                                        type = PhonesColumns.TYPE_HOME;
687
 
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
688
 
                                type = PhonesColumns.TYPE_MOBILE;
 
774
                        boolean is_preferred = types.contains( "PREF" );
 
775
                        int type;
689
776
                        if( types.contains( "FAX" ) )
690
777
                                if( types.contains( "HOME" ) )
691
778
                                        type = PhonesColumns.TYPE_FAX_HOME;
692
779
                                else
693
780
                                        type = PhonesColumns.TYPE_FAX_WORK;
694
 
                        if( types.contains( "PAGER" ) )
 
781
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
782
                                type = PhonesColumns.TYPE_MOBILE;
 
783
                        else if( types.contains( "PAGER" ) )
695
784
                                type = PhonesColumns.TYPE_PAGER;
 
785
                        else if( types.contains( "WORK" ) )
 
786
                                type = PhonesColumns.TYPE_WORK;
 
787
                        else
 
788
                                type = PhonesColumns.TYPE_HOME;
696
789
 
697
790
                        // add phone number
698
 
                        addPhone( value, type, preferred );
 
791
                        addNumber( value, type, is_preferred );
699
792
                }
700
793
 
701
794
                public void parseEMAIL( String[] params, String value )
702
 
                        throws ParseException
703
795
                {
704
796
                        if( value.length() == 0 ) return;
705
797
 
706
798
                        Set< String > types = extractTypes( params, Arrays.asList(
707
799
                                "PREF", "WORK", "HOME", "INTERNET" ) );
708
800
 
709
 
                        // here's the logic...
710
 
                        boolean preferred = types.contains( "PREF" );
711
 
                        if( types.contains( "WORK" ) )
712
 
                                addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
713
 
                        else
714
 
                                addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
 
801
                        // add email address
 
802
                        boolean is_preferred = types.contains( "PREF" );
 
803
                        int type;
 
804
                        if( types.contains( "WORK" ) )
 
805
                                type = Contacts.ContactMethods.TYPE_WORK;
 
806
                        else
 
807
                                type = Contacts.ContactMethods.TYPE_HOME;
 
808
 
 
809
                        addEmail( value, type, is_preferred );
 
810
                }
 
811
 
 
812
                private void parseADR( String[] params, String value )
 
813
                {
 
814
                        // get address parts
 
815
                        String[] adr_parts = splitValueBySemicolon( value );
 
816
 
 
817
                        // build address
 
818
                        value = "";
 
819
                        for( int a = 0; a < adr_parts.length; a++ ) {
 
820
                                if( value.length() > 0 ) value += "\n";
 
821
                                value += adr_parts[ a ].trim();
 
822
                        }
 
823
 
 
824
                        Set< String > types = extractTypes( params, Arrays.asList(
 
825
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
826
 
 
827
                        // add address
 
828
                        int type;
 
829
                        if( types.contains( "WORK" ) )
 
830
                                type = Contacts.ContactMethods.TYPE_WORK;
 
831
                        else
 
832
                                type = Contacts.ContactMethods.TYPE_HOME;
 
833
 
 
834
                        addAddress( value, type );
715
835
                }
716
836
 
717
837
                public void finaliseParsing()
722
842
                        if( _version == null && _buffers != null )
723
843
                                throw new ParseException( R.string.error_vcf_malformed );
724
844
 
725
 
                        //  missing name properties?
726
 
                        if( _name_level == NAMELEVEL_NONE )
727
 
                                throw new ParseException( R.string.error_vcf_noname );
728
 
 
729
 
                        // check if we should import this one? If we've already got an 'N'-
730
 
                        // type name, this will already have been done by parseN() so we
731
 
                        // mustn't do this here (or it could prompt twice!)
732
 
                        if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
733
 
                                throw new SkipContactException();
 
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
                        }
734
853
                }
735
854
 
736
855
                private String checkParam( String[] params, String name )