/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

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;
 
46
 
 
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
45
48
 
46
49
import android.content.SharedPreferences;
47
50
import android.provider.Contacts;
330
333
        private class VCard extends ContactData
331
334
        {
332
335
                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;
 
336
                private final static int NAMELEVEL_FN = 1;
 
337
                private final static int NAMELEVEL_N = 2;
 
338
 
 
339
                private final static int MULTILINE_NONE = 0;
 
340
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
 
341
                private final static int MULTILINE_ESCAPED = 2; // v2.1 \\CRLF
 
342
                private final static int MULTILINE_FOLDED = 3;  // v3.0 folding
336
343
 
337
344
                private String _version = null;
338
345
                private Vector< ByteBuffer > _buffers = null;
339
346
                private int _name_level = NAMELEVEL_NONE;
340
 
                private boolean _parser_in_encoded_multiline = false;
341
 
                private boolean _parser_in_folded_multiline = false;
 
347
                private int _parser_multiline_state = MULTILINE_NONE;
342
348
                private String _parser_current_name_and_params = null;
343
349
                private String _parser_buffered_value_so_far = "";
 
350
                private String _cached_organisation = null;
 
351
                private String _cached_title = null;
344
352
 
345
353
                protected class UnencodeResult
346
354
                {
469
477
                                String name_and_params;
470
478
                                int pos;
471
479
 
472
 
                                if( _parser_in_encoded_multiline ||
473
 
                                        _parser_in_folded_multiline )
 
480
                                if( _parser_multiline_state != MULTILINE_NONE )
474
481
                                {
475
482
                                        // if we're currently in a multi-line value, use the stored
476
483
                                        // property name and parameters
477
484
                                        name_and_params = _parser_current_name_and_params;
478
485
 
 
486
                                        // skip some initial line characters, depending on the type
 
487
                                        // of multi-line we're handling
479
488
                                        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 )
 
489
                                        switch( _parser_multiline_state )
 
490
                                        {
 
491
                                        case MULTILINE_FOLDED:
484
492
                                                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
 
493
                                                break;
 
494
                                        case MULTILINE_ENCODED:
489
495
                                                while( pos < buffer.limit() && (
490
496
                                                        buffer.get( pos ) == ' ' ||
491
497
                                                        buffer.get( pos ) == '\t' ) )
492
498
                                                {
493
499
                                                        pos++;
494
500
                                                }
 
501
                                                break;
 
502
                                        default:
 
503
                                                // do nothing
 
504
                                        }
 
505
 
 
506
                                        // take us out of multi-line so that we can re-detect that
 
507
                                        // this line is a multi-line or not
 
508
                                        _parser_multiline_state = MULTILINE_NONE;
495
509
                                }
496
510
                                else
497
511
                                {
552
566
//                                      unencoding_result = unencodeBase64( props[ 1 ], charset );
553
567
                                if( unencoding_result != null ) {
554
568
                                        value = unencoding_result.getBuffer();
555
 
                                        _parser_in_encoded_multiline =
556
 
                                                unencoding_result.isAnotherLineRequired();
 
569
                                        if( unencoding_result.isAnotherLineRequired() )
 
570
                                                _parser_multiline_state = MULTILINE_ENCODED;
557
571
                                }
558
572
 
559
573
                                // convert 8-bit ASCII charset to US-ASCII
571
585
                                        throw new ParseException( R.string.error_vcf_charset );
572
586
                                }
573
587
 
 
588
                                // for some entries that have semicolon-separated value parts,
 
589
                                // check to see if the value ends in an escape character, which
 
590
                                // indicates that we have a multi-line value
 
591
                                if( ( name_param_parts[ 0 ].equals( "N" ) ||
 
592
                                        name_param_parts[ 0 ].equals( "ORG" ) ||
 
593
                                        name_param_parts[ 0 ].equals( "ADR" ) ) &&
 
594
                                        doesStringEndInAnEscapeChar( string_value ) )
 
595
                                {
 
596
                                        _parser_multiline_state = MULTILINE_ESCAPED;
 
597
                                        string_value = string_value.substring( 0,
 
598
                                                string_value.length() - 1 );
 
599
                                }
 
600
 
574
601
                                // now we know whether we're in an encoding multi-line,
575
602
                                // 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;
 
603
                                if( _parser_multiline_state == MULTILINE_NONE &&
 
604
                                        _version.equals( "3.0" ) && next_line_looks_folded )
 
605
                                {
 
606
                                        _parser_multiline_state = MULTILINE_FOLDED;
 
607
                                }
578
608
 
579
 
                                // handle multi-line requests
580
 
                                if( _parser_in_encoded_multiline ||
581
 
                                        _parser_in_folded_multiline )
582
 
                                {
 
609
                                // handle multi-lines by buffering them and parsing them when we
 
610
                                // are processing the last line in a multi-line sequence
 
611
                                if( _parser_multiline_state != MULTILINE_NONE ) {
583
612
                                        _parser_buffered_value_so_far += string_value;
584
613
                                        return;
585
614
                                }
586
 
 
587
 
                                // add on buffered multi-line content
588
615
                                String complete_value =
589
 
                                        _parser_buffered_value_so_far + string_value;
 
616
                                        ( _parser_buffered_value_so_far + string_value ).trim();
590
617
 
591
618
                                // ignore empty values
592
619
                                if( complete_value.length() < 1 ) return;
598
625
                                        parseFN( name_param_parts, complete_value );
599
626
                                else if( name_param_parts[ 0 ].equals( "ORG" ) )
600
627
                                        parseORG( name_param_parts, complete_value );
 
628
                                else if( name_param_parts[ 0 ].equals( "TITLE" ) )
 
629
                                        parseTITLE( name_param_parts, complete_value );
601
630
                                else if( name_param_parts[ 0 ].equals( "TEL" ) )
602
631
                                        parseTEL( name_param_parts, complete_value );
603
632
                                else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
604
633
                                        parseEMAIL( name_param_parts, complete_value );
605
 
                        }
 
634
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
 
635
                                        parseADR( name_param_parts, complete_value );
 
636
                        }
 
637
                }
 
638
 
 
639
                private boolean doesStringEndInAnEscapeChar( String string )
 
640
                {
 
641
                        // count the number of backslashes at the end of the string
 
642
                        int count = 0;
 
643
                        for( int a = string.length() - 1; a >= 0; a-- )
 
644
                                if( string.charAt( a ) == '\\' )
 
645
                                        count++;
 
646
                                else
 
647
                                        break;
 
648
 
 
649
                        // if there are an even number of backslashes then the final one
 
650
                        // doesn't count
 
651
                        return ( count & 1 ) == 1;
 
652
                }
 
653
 
 
654
                private String[] splitValueBySemicolon( String value )
 
655
                {
 
656
                        // split string in to parts by semicolon
 
657
                        ArrayList< String > parts = new ArrayList< String >(
 
658
                                Arrays.asList( value.split(  ";" ) ) );
 
659
 
 
660
                        // go through parts
 
661
                        for( int a = 0; a < parts.size(); a++ )
 
662
                        {
 
663
                                String str = parts.get( a );
 
664
 
 
665
                                // look for parts that end in an escape character, but ignore
 
666
                                // the final part. We've already detected escape chars at the
 
667
                                // end of the final part in parseLine() and handled multi-lines
 
668
                                // accordingly.
 
669
                                if( a < parts.size() - 1 &&
 
670
                                        doesStringEndInAnEscapeChar( str ) )
 
671
                                {
 
672
                                        // join the next part to this part and remove the next part
 
673
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
 
674
                                                ';' + parts.get( a + 1 ) );
 
675
                                        parts.remove( a + 1 );
 
676
 
 
677
                                        // re-visit this part
 
678
                                        a--;
 
679
                                        continue;
 
680
                                }
 
681
 
 
682
                                // trim and replace string
 
683
                                str = str.trim();
 
684
                                parts.set( a, str );
 
685
                        }
 
686
 
 
687
                        String[] ret = new String[ parts.size() ];
 
688
                        return parts.toArray( ret );
606
689
                }
607
690
 
608
691
                private void parseN( String[] params, String value )
609
 
                        throws ParseException, SkipContactException,
610
 
                        AbortImportException
611
692
                {
612
693
                        // already got a better name?
613
694
                        if( _name_level >= NAMELEVEL_N ) return;
614
695
 
615
696
                        // 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();
 
697
                        String[] name_parts = splitValueBySemicolon( value );
619
698
 
620
699
                        // build name
621
700
                        value = "";
627
706
                        // set name
628
707
                        setName( value );
629
708
                        _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
709
                }
636
710
 
637
711
                private void parseFN( String[] params, String value )
638
 
                        throws ParseException, SkipContactException
639
712
                {
640
713
                        // already got a better name?
641
714
                        if( _name_level >= NAMELEVEL_FN ) return;
646
719
                }
647
720
 
648
721
                private void parseORG( String[] params, String value )
649
 
                        throws ParseException, SkipContactException
650
722
                {
651
 
                        // already got a better name?
652
 
                        if( _name_level >= NAMELEVEL_ORG ) return;
653
 
 
654
723
                        // 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;
 
724
                        String[] org_parts = splitValueBySemicolon( value );
 
725
                        if( org_parts == null || org_parts.length < 1 ) return;
 
726
 
 
727
                        // build organisation name
 
728
                        StringBuilder builder = new StringBuilder(
 
729
                                String.valueOf( org_parts[ 0 ] ) );
 
730
                        for( int a = 1; a < org_parts.length; a++ )
 
731
                                builder.append( ", " ).append( org_parts[ a ] );
 
732
                        String organisation = builder.toString();
 
733
 
 
734
                        // set organisation name (using a title we've previously found)
 
735
                        addOrganisation( organisation, _cached_title, true );
 
736
 
 
737
                        // if we've not previously found a title, store this organisation
 
738
                        // name (we'll need it when we find a title to update the
 
739
                        // organisation, by name), else if we *have* previously found a
 
740
                        // title, clear it (since we just used it)
 
741
                        if( _cached_title == null )
 
742
                                _cached_organisation = organisation;
 
743
                        else
 
744
                                _cached_title = null;
 
745
                }
 
746
 
 
747
                private void parseTITLE( String[] params, String value )
 
748
                {
 
749
                        // if we previously had an organisation, look it up and append this
 
750
                        // title to it
 
751
                        if( _cached_organisation != null && hasOrganisations() ) {
 
752
                                HashMap< String, ExtraDetail > datas = getOrganisations();
 
753
                                ExtraDetail detail = datas.get( _cached_organisation );
 
754
                                if( detail != null )
 
755
                                        detail.setExtra( value );
 
756
                        }
 
757
 
 
758
                        // same as when handling organisation, if we've not previously found
 
759
                        // an organisation we store this title, else we clear it (since we
 
760
                        // just appended this title to it)
 
761
                        if( _cached_organisation == null )
 
762
                                _cached_title = value;
 
763
                        else
 
764
                                _cached_organisation = null;
668
765
                }
669
766
 
670
767
                private void parseTEL( String[] params, String value )
671
 
                        throws ParseException
672
768
                {
673
769
                        if( value.length() == 0 ) return;
674
770
 
678
774
 
679
775
                        // here's the logic...
680
776
                        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;
 
777
                        int type;
689
778
                        if( types.contains( "FAX" ) )
690
779
                                if( types.contains( "HOME" ) )
691
780
                                        type = PhonesColumns.TYPE_FAX_HOME;
692
781
                                else
693
782
                                        type = PhonesColumns.TYPE_FAX_WORK;
694
 
                        if( types.contains( "PAGER" ) )
 
783
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
 
784
                                type = PhonesColumns.TYPE_MOBILE;
 
785
                        else if( types.contains( "PAGER" ) )
695
786
                                type = PhonesColumns.TYPE_PAGER;
 
787
                        else if( types.contains( "WORK" ) )
 
788
                                type = PhonesColumns.TYPE_WORK;
 
789
                        else
 
790
                                type = PhonesColumns.TYPE_HOME;
696
791
 
697
792
                        // add phone number
698
 
                        addPhone( value, type, preferred );
 
793
                        addNumber( value, type, preferred );
699
794
                }
700
795
 
701
796
                public void parseEMAIL( String[] params, String value )
702
 
                        throws ParseException
703
797
                {
704
798
                        if( value.length() == 0 ) return;
705
799
 
706
800
                        Set< String > types = extractTypes( params, Arrays.asList(
707
801
                                "PREF", "WORK", "HOME", "INTERNET" ) );
708
802
 
709
 
                        // here's the logic...
 
803
                        // add email address
710
804
                        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 );
 
805
                        int type;
 
806
                        if( types.contains( "WORK" ) )
 
807
                                type = Contacts.ContactMethods.TYPE_WORK;
 
808
                        else
 
809
                                type = Contacts.ContactMethods.TYPE_HOME;
 
810
 
 
811
                        addEmail( value, type, preferred );
 
812
                }
 
813
 
 
814
                private void parseADR( String[] params, String value )
 
815
                {
 
816
                        // get address parts
 
817
                        String[] adr_parts = splitValueBySemicolon( value );
 
818
 
 
819
                        // build address
 
820
                        value = "";
 
821
                        for( int a = 0; a < adr_parts.length; a++ ) {
 
822
                                if( value.length() > 0 ) value += "\n";
 
823
                                value += adr_parts[ a ].trim();
 
824
                        }
 
825
 
 
826
                        Set< String > types = extractTypes( params, Arrays.asList(
 
827
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
828
 
 
829
                        // add address
 
830
                        int type;
 
831
                        if( types.contains( "WORK" ) )
 
832
                                type = Contacts.ContactMethods.TYPE_WORK;
 
833
                        else
 
834
                                type = Contacts.ContactMethods.TYPE_HOME;
 
835
 
 
836
                        addAddress( value, type );
715
837
                }
716
838
 
717
839
                public void finaliseParsing()
722
844
                        if( _version == null && _buffers != null )
723
845
                                throw new ParseException( R.string.error_vcf_malformed );
724
846
 
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();
 
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
                        }
734
855
                }
735
856
 
736
857
                private String checkParam( String[] params, String name )