4
* Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
6
6
* This file is part of the Import Contacts program (hereafter referred
7
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/import-contacts
8
* http://ed.am/dev/android/import-contacts
10
10
* This program is free software: you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License as published by
38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
41
import java.util.Locale;
41
42
import java.util.NoSuchElementException;
42
43
import java.util.Set;
43
44
import java.util.Vector;
44
45
import java.util.regex.Matcher;
45
46
import java.util.regex.Pattern;
48
import android.annotation.SuppressLint;
47
49
import android.content.SharedPreferences;
48
import android.provider.Contacts;
49
import android.provider.Contacts.PhonesColumns;
51
51
public class VcardImporter extends Importer
83
84
class VCardFilter implements FilenameFilter {
84
85
public boolean accept( File dir, String name ) {
85
return name.toLowerCase().endsWith( ".vcf" );
86
return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
88
89
files = file.listFiles( new VCardFilter() );
569
570
// determine whether we care about this entry
570
571
final HashSet< String > interesting_fields =
571
new HashSet< String >( Arrays.asList( new String[]
572
{ "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
572
new HashSet< String >( Arrays.asList( new String[] { "N",
573
"FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
574
575
boolean is_interesting_field =
575
576
interesting_fields.contains( name_param_parts[ 0 ] );
577
578
// parse encoding parameter
578
579
String encoding = checkParam( name_param_parts, "ENCODING" );
579
if( encoding != null ) encoding = encoding.toUpperCase();
580
if( encoding != null )
581
encoding = encoding.toUpperCase( Locale.US );
580
582
if( is_interesting_field && encoding != null &&
581
583
!encoding.equals( "8BIT" ) &&
582
584
!encoding.equals( "QUOTED-PRINTABLE" ) )
588
590
// parse charset parameter
589
591
String charset = checkParam( name_param_parts, "CHARSET" );
590
if( charset != null ) charset = charset.toUpperCase();
592
if( charset != null )
593
charset = charset.toUpperCase( Locale.US );
591
594
if( charset != null &&
592
595
!charset.equals( "US-ASCII" ) &&
593
596
!charset.equals( "ASCII" ) &&
676
679
parseEMAIL( name_param_parts, complete_value );
677
680
else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
681
parseADR( name_param_parts, complete_value );
682
else if( name_param_parts[ 0 ].equals( "LABEL" ) )
683
parseLABEL( name_param_parts, complete_value );
684
else if( name_param_parts[ 0 ].equals( "NOTE" ) )
685
parseNOTE( name_param_parts, complete_value );
878
891
if( types.contains( "FAX" ) )
879
892
if( types.contains( "HOME" ) )
880
type = PhonesColumns.TYPE_FAX_HOME;
893
type = TYPE_FAX_HOME;
882
type = PhonesColumns.TYPE_FAX_WORK;
895
type = TYPE_FAX_WORK;
883
896
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
884
type = PhonesColumns.TYPE_MOBILE;
885
898
else if( types.contains( "PAGER" ) )
886
type = PhonesColumns.TYPE_PAGER;
887
900
else if( types.contains( "WORK" ) )
888
type = PhonesColumns.TYPE_WORK;
890
type = PhonesColumns.TYPE_HOME;
892
905
// add phone number
893
906
addNumber( value, type, is_preferred );
904
917
boolean is_preferred = types.contains( "PREF" );
906
919
if( types.contains( "WORK" ) )
907
type = Contacts.ContactMethods.TYPE_WORK;
909
type = Contacts.ContactMethods.TYPE_HOME;
911
924
addEmail( unescapeValue( value ), type, is_preferred );
935
948
Set< String > types = extractTypes( params, Arrays.asList(
936
"PREF", "WORK", "HOME", "INTERNET" ) );
940
if( types.contains( "WORK" ) )
941
type = Contacts.ContactMethods.TYPE_WORK;
943
type = Contacts.ContactMethods.TYPE_HOME;
945
addAddress( unescapeValue( value ), type );
949
"PREF", "WORK", "HOME" ) );
953
if( types.contains( "WORK" ) )
958
addAddress( unescapeValue( value ), type );
961
private void parseLABEL( String[] params, String value )
963
Set< String > types = extractTypes( params, Arrays.asList(
964
"PREF", "WORK", "HOME" ) );
968
if( types.contains( "WORK" ) )
973
addAddress( unescapeValue( value ), type );
976
private void parseNOTE( String[] params, String value )
978
addNote( unescapeValue( value ) );
948
981
public void finaliseVcard()
993
* Amongst the params, find the value of the first, only, of any with
997
* @return a value, or null
959
999
private String checkParam( String[] params, String name )
1001
String[] res = checkParams( params, name );
1002
return res.length > 0? res[ 0 ] : null;
1006
* Amongst the params, find the values of any with the specified name
1009
* @return an array of values, or null
1011
private String[] checkParams( String[] params, String name )
1013
HashSet< String > ret = new HashSet< String >();
961
1015
Pattern p = Pattern.compile(
962
1016
"^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
963
1017
for( int i = 0; i < params.length; i++ ) {
964
1018
Matcher m = p.matcher( params[ i ] );
965
1019
if( m.matches() )
1020
ret.add( m.group( 2 ) );
1023
return (String[]) ret.toArray( new String[ ret.size() ] );
1027
* Amongst the params, return any type values present. For v2.1 vCards,
1028
* those types are just parameters. For v3.0, they are prefixed with
1029
* "TYPE=". There may also be multiple type parameters.
1031
* @param a list of type values to look for
1032
* @return a set of present type values
971
1034
private Set< String > extractTypes( String[] params,
972
1035
List< String > valid_types )
974
1037
HashSet< String > types = new HashSet< String >();
976
1039
// get 3.0-style TYPE= param
978
if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
979
String[] parts = type_param.split( "," );
1040
String type_params[] = checkParams( params, "TYPE" );
1041
for( int a = 0; a < type_params.length; a++ )
1043
// check for a comma-separated list of types (why? this isn't in
1045
String[] parts = type_params[ a ].split( "," );
980
1046
for( int i = 0; i < parts.length; i++ )
981
1047
if( valid_types.contains( parts[ i ] ) )
982
1048
types.add( parts[ i ] );