32
32
import java.io.IOException;
33
33
import java.io.UnsupportedEncodingException;
34
34
import java.nio.ByteBuffer;
35
import java.util.ArrayList;
36
35
import java.util.Arrays;
37
import java.util.HashMap;
38
36
import java.util.HashSet;
39
37
import java.util.Iterator;
40
38
import java.util.List;
41
import java.util.NoSuchElementException;
42
39
import java.util.Set;
43
40
import java.util.Vector;
44
41
import java.util.regex.Matcher;
45
42
import java.util.regex.Pattern;
43
import java.util.NoSuchElementException;
44
import java.lang.UnsupportedOperationException;
47
46
import android.content.SharedPreferences;
48
47
import android.provider.Contacts;
49
48
import android.provider.Contacts.PhonesColumns;
51
public class VcardImporter extends Importer
50
public class VCFImporter extends Importer
53
private int _vcard_count = 0;
52
private int _vCardCount = 0;
54
53
private int _progress = 0;
56
public VcardImporter( Doit doit )
55
public VCFImporter( Doit doit )
227
226
finish( ACTION_ABORT );
230
catch( Vcard.SkipContactException e ) {
229
catch( VCard.SkipContactException e ) {
238
237
// try giving the line to the vcard
240
vcard.parseLine( buffer, line,
239
vCard.parseLine( buffer, line,
241
240
cli.doesNextLineLookFolded() );
243
catch( Vcard.ParseException e ) {
242
catch( VCard.ParseException e ) {
245
244
if( !showContinue(
246
245
getText( R.string.error_vcf_parse ).toString()
331
private class Vcard extends ContactData
330
private class VCard extends ContactData
333
332
private final static int NAMELEVEL_NONE = 0;
334
private final static int NAMELEVEL_FN = 1;
335
private final static int NAMELEVEL_N = 2;
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
333
private final static int NAMELEVEL_ORG = 1;
334
private final static int NAMELEVEL_FN = 2;
335
private final static int NAMELEVEL_N = 3;
342
337
private String _version = null;
343
338
private Vector< ByteBuffer > _buffers = null;
344
339
private int _name_level = NAMELEVEL_NONE;
345
private int _parser_multiline_state = MULTILINE_NONE;
340
private boolean _parser_in_encoded_multiline = false;
341
private boolean _parser_in_folded_multiline = false;
346
342
private String _parser_current_name_and_params = null;
347
343
private String _parser_buffered_value_so_far = "";
348
private String _cached_organisation = null;
349
private String _cached_title = null;
351
345
protected class UnencodeResult
475
469
String name_and_params;
478
if( _parser_multiline_state != MULTILINE_NONE )
472
if( _parser_in_encoded_multiline ||
473
_parser_in_folded_multiline )
480
475
// if we're currently in a multi-line value, use the stored
481
476
// property name and parameters
482
477
name_and_params = _parser_current_name_and_params;
484
// skip some initial line characters, depending on the type
485
// of multi-line we're handling
486
479
pos = buffer.position();
487
switch( _parser_multiline_state )
489
case MULTILINE_FOLDED:
481
// for folded multi-lines, skip the single space at the
482
// start of the next line
483
if( _parser_in_folded_multiline )
492
case MULTILINE_ENCODED:
486
// else, this must be an encoded multi-line, so skip any
487
// whitespace we find at the start of the next line
493
489
while( pos < buffer.limit() && (
494
490
buffer.get( pos ) == ' ' ||
495
491
buffer.get( pos ) == '\t' ) )
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;
583
571
throw new ParseException( R.string.error_vcf_charset );
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 ) )
594
_parser_multiline_state = MULTILINE_ESCAPED;
595
string_value = string_value.substring( 0,
596
string_value.length() - 1 );
599
574
// now we know whether we're in an encoding multi-line,
600
575
// determine if we're in a v3 folded multi-line or not
601
if( _parser_multiline_state == MULTILINE_NONE &&
602
_version.equals( "3.0" ) && next_line_looks_folded )
576
_parser_in_folded_multiline = !_parser_in_encoded_multiline &&
577
_version.equals( "3.0" ) && next_line_looks_folded;
579
// handle multi-line requests
580
if( _parser_in_encoded_multiline ||
581
_parser_in_folded_multiline )
604
_parser_multiline_state = MULTILINE_FOLDED;
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 ) {
610
583
_parser_buffered_value_so_far += string_value;
587
// add on buffered multi-line content
613
588
String complete_value =
614
( _parser_buffered_value_so_far + string_value ).trim();
589
_parser_buffered_value_so_far + string_value;
616
591
// ignore empty values
617
592
if( complete_value.length() < 1 ) return;
623
598
parseFN( name_param_parts, complete_value );
624
599
else if( name_param_parts[ 0 ].equals( "ORG" ) )
625
600
parseORG( name_param_parts, complete_value );
626
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
627
parseTITLE( name_param_parts, complete_value );
628
601
else if( name_param_parts[ 0 ].equals( "TEL" ) )
629
602
parseTEL( name_param_parts, complete_value );
630
603
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
631
604
parseEMAIL( name_param_parts, complete_value );
632
else if( name_param_parts[ 0 ].equals( "ADR" ) )
633
parseADR( name_param_parts, complete_value );
637
private boolean doesStringEndInAnEscapeChar( String string )
639
// count the number of backslashes at the end of the string
641
for( int a = string.length() - 1; a >= 0; a-- )
642
if( string.charAt( a ) == '\\' )
647
// if there are an even number of backslashes then the final one
649
return ( count & 1 ) == 1;
652
private String[] splitValueBySemicolon( String value )
654
// split string in to parts by semicolon
655
ArrayList< String > parts = new ArrayList< String >(
656
Arrays.asList( value.split( ";" ) ) );
659
for( int a = 0; a < parts.size(); a++ )
661
String str = parts.get( a );
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
667
if( a < parts.size() - 1 &&
668
doesStringEndInAnEscapeChar( str ) )
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 );
675
// re-visit this part
680
// trim and replace string
685
String[] ret = new String[ parts.size() ];
686
return parts.toArray( ret );
689
608
private void parseN( String[] params, String value )
609
throws ParseException, SkipContactException,
691
612
// already got a better name?
692
613
if( _name_level >= NAMELEVEL_N ) return;
694
615
// get name parts
695
String[] name_parts = splitValueBySemicolon( value );
616
String[] name_parts = value.split( ";" );
617
for( int i = 0; i < name_parts.length; i++ )
618
name_parts[ i ] = name_parts[ i ].trim();
705
628
setName( value );
706
629
_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();
709
637
private void parseFN( String[] params, String value )
638
throws ParseException, SkipContactException
711
640
// already got a better name?
712
641
if( _name_level >= NAMELEVEL_FN ) return;
719
648
private void parseORG( String[] params, String value )
649
throws ParseException, SkipContactException
651
// already got a better name?
652
if( _name_level >= NAMELEVEL_ORG ) return;
722
String[] org_parts = splitValueBySemicolon( value );
723
if( org_parts == null || org_parts.length < 1 ) return;
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();
732
// set organisation name (using a title we've previously found)
733
addOrganisation( organisation, _cached_title, true );
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;
742
_cached_title = null;
745
private void parseTITLE( String[] params, String value )
747
// if we previously had an organisation, look it up and append this
749
if( _cached_organisation != null && hasOrganisations() ) {
750
HashMap< String, ExtraDetail > datas = getOrganisations();
751
ExtraDetail detail = datas.get( _cached_organisation );
753
detail.setExtra( value );
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;
762
_cached_organisation = null;
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;
765
670
private void parseTEL( String[] params, String value )
671
throws ParseException
767
673
if( value.length() == 0 ) return;
771
677
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
773
679
// here's the logic...
774
boolean is_preferred = types.contains( "PREF" );
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;
686
type = PhonesColumns.TYPE_HOME;
687
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
688
type = PhonesColumns.TYPE_MOBILE;
776
689
if( types.contains( "FAX" ) )
777
690
if( types.contains( "HOME" ) )
778
691
type = PhonesColumns.TYPE_FAX_HOME;
780
693
type = PhonesColumns.TYPE_FAX_WORK;
781
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
782
type = PhonesColumns.TYPE_MOBILE;
783
else if( types.contains( "PAGER" ) )
694
if( types.contains( "PAGER" ) )
784
695
type = PhonesColumns.TYPE_PAGER;
785
else if( types.contains( "WORK" ) )
786
type = PhonesColumns.TYPE_WORK;
788
type = PhonesColumns.TYPE_HOME;
790
697
// add phone number
791
addNumber( value, type, is_preferred );
698
addPhone( value, type, preferred );
794
701
public void parseEMAIL( String[] params, String value )
702
throws ParseException
796
704
if( value.length() == 0 ) return;
798
706
Set< String > types = extractTypes( params, Arrays.asList(
799
707
"PREF", "WORK", "HOME", "INTERNET" ) );
802
boolean is_preferred = types.contains( "PREF" );
804
if( types.contains( "WORK" ) )
805
type = Contacts.ContactMethods.TYPE_WORK;
807
type = Contacts.ContactMethods.TYPE_HOME;
809
addEmail( value, type, is_preferred );
812
private void parseADR( String[] params, String value )
815
String[] adr_parts = splitValueBySemicolon( value );
819
for( int a = 0; a < adr_parts.length; a++ ) {
820
if( value.length() > 0 ) value += "\n";
821
value += adr_parts[ a ].trim();
824
Set< String > types = extractTypes( params, Arrays.asList(
825
"PREF", "WORK", "HOME", "INTERNET" ) );
829
if( types.contains( "WORK" ) )
830
type = Contacts.ContactMethods.TYPE_WORK;
832
type = Contacts.ContactMethods.TYPE_HOME;
834
addAddress( value, type );
709
// here's the logic...
710
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 );
837
717
public void finaliseParsing()
842
722
if( _version == null && _buffers != null )
843
723
throw new ParseException( R.string.error_vcf_malformed );
845
// check if we should import this contact
847
if( !isImportRequired( this ) )
848
throw new SkipContactException();
850
catch( ContactNeedsMoreInfoException e ) {
851
throw new ParseException( R.string.error_vcf_notenoughinfo );
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();
855
736
private String checkParam( String[] params, String name )