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;
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
46
49
import android.content.SharedPreferences;
47
50
import android.provider.Contacts;
330
333
private class VCard extends ContactData
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;
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
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;
345
353
protected class UnencodeResult
469
477
String name_and_params;
472
if( _parser_in_encoded_multiline ||
473
_parser_in_folded_multiline )
480
if( _parser_multiline_state != MULTILINE_NONE )
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;
486
// skip some initial line characters, depending on the type
487
// of multi-line we're handling
479
488
pos = buffer.position();
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 )
491
case MULTILINE_FOLDED:
486
// else, this must be an encoded multi-line, so skip any
487
// whitespace we find at the start of the next line
494
case MULTILINE_ENCODED:
489
495
while( pos < buffer.limit() && (
490
496
buffer.get( pos ) == ' ' ||
491
497
buffer.get( pos ) == '\t' ) )
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;
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;
559
573
// convert 8-bit ASCII charset to US-ASCII
571
585
throw new ParseException( R.string.error_vcf_charset );
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 ) )
596
_parser_multiline_state = MULTILINE_ESCAPED;
597
string_value = string_value.substring( 0,
598
string_value.length() - 1 );
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 )
606
_parser_multiline_state = MULTILINE_FOLDED;
579
// handle multi-line requests
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
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;
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();
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 );
634
else if( name_param_parts[ 0 ].equals( "ADR" ) )
635
parseADR( name_param_parts, complete_value );
639
private boolean doesStringEndInAnEscapeChar( String string )
641
// count the number of backslashes at the end of the string
643
for( int a = string.length() - 1; a >= 0; a-- )
644
if( string.charAt( a ) == '\\' )
649
// if there are an even number of backslashes then the final one
651
return ( count & 1 ) == 1;
654
private String[] splitValueBySemicolon( String value )
656
// split string in to parts by semicolon
657
ArrayList< String > parts = new ArrayList< String >(
658
Arrays.asList( value.split( ";" ) ) );
661
for( int a = 0; a < parts.size(); a++ )
663
String str = parts.get( a );
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
669
if( a < parts.size() - 1 &&
670
doesStringEndInAnEscapeChar( str ) )
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 );
677
// re-visit this part
682
// trim and replace string
687
String[] ret = new String[ parts.size() ];
688
return parts.toArray( ret );
608
691
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
612
693
// already got a better name?
613
694
if( _name_level >= NAMELEVEL_N ) return;
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 );
628
707
setName( value );
629
708
_name_level = NAMELEVEL_N;
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();
637
711
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
640
713
// already got a better name?
641
714
if( _name_level >= NAMELEVEL_FN ) return;
648
721
private void parseORG( String[] params, String value )
649
throws ParseException, SkipContactException
651
// already got a better name?
652
if( _name_level >= NAMELEVEL_ORG ) return;
655
String[] org_parts = value.split( ";" );
656
for( int i = 0; i < org_parts.length; i++ )
657
org_parts[ i ] = org_parts[ i ].trim();
660
if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
661
value = org_parts[ 1 ];
663
value = org_parts[ 0 ];
667
_name_level = NAMELEVEL_ORG;
724
String[] org_parts = splitValueBySemicolon( value );
725
if( org_parts == null || org_parts.length < 1 ) return;
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();
734
// set organisation name (using a title we've previously found)
735
addOrganisation( organisation, _cached_title, true );
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;
744
_cached_title = null;
747
private void parseTITLE( String[] params, String value )
749
// if we previously had an organisation, look it up and append this
751
if( _cached_organisation != null && hasOrganisations() ) {
752
HashMap< String, ExtraDetail > datas = getOrganisations();
753
ExtraDetail detail = datas.get( _cached_organisation );
755
detail.setExtra( value );
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;
764
_cached_organisation = null;
670
767
private void parseTEL( String[] params, String value )
671
throws ParseException
673
769
if( value.length() == 0 ) return;
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;
686
type = PhonesColumns.TYPE_HOME;
687
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
688
type = PhonesColumns.TYPE_MOBILE;
689
778
if( types.contains( "FAX" ) )
690
779
if( types.contains( "HOME" ) )
691
780
type = PhonesColumns.TYPE_FAX_HOME;
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;
790
type = PhonesColumns.TYPE_HOME;
697
792
// add phone number
698
addPhone( value, type, preferred );
793
addNumber( value, type, preferred );
701
796
public void parseEMAIL( String[] params, String value )
702
throws ParseException
704
798
if( value.length() == 0 ) return;
706
800
Set< String > types = extractTypes( params, Arrays.asList(
707
801
"PREF", "WORK", "HOME", "INTERNET" ) );
709
// here's the logic...
710
804
boolean preferred = types.contains( "PREF" );
711
if( types.contains( "WORK" ) )
712
addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
714
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
806
if( types.contains( "WORK" ) )
807
type = Contacts.ContactMethods.TYPE_WORK;
809
type = Contacts.ContactMethods.TYPE_HOME;
811
addEmail( value, type, preferred );
814
private void parseADR( String[] params, String value )
817
String[] adr_parts = splitValueBySemicolon( value );
821
for( int a = 0; a < adr_parts.length; a++ ) {
822
if( value.length() > 0 ) value += "\n";
823
value += adr_parts[ a ].trim();
826
Set< String > types = extractTypes( params, Arrays.asList(
827
"PREF", "WORK", "HOME", "INTERNET" ) );
831
if( types.contains( "WORK" ) )
832
type = Contacts.ContactMethods.TYPE_WORK;
834
type = Contacts.ContactMethods.TYPE_HOME;
836
addAddress( value, type );
717
839
public void finaliseParsing()
722
844
if( _version == null && _buffers != null )
723
845
throw new ParseException( R.string.error_vcf_malformed );
725
// missing name properties?
726
if( _name_level == NAMELEVEL_NONE )
727
throw new ParseException( R.string.error_vcf_noname );
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
849
if( !isImportRequired( this ) )
850
throw new SkipContactException();
852
catch( ContactNeedsMoreInfoException e ) {
853
throw new ParseException( R.string.error_vcf_notenoughinfo );
736
857
private String checkParam( String[] params, String name )