/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/VcardImporter.java

  • Committer: edam
  • Date: 2011-06-04 13:16:20 UTC
  • Revision ID: edam@waxworlds.org-20110604131620-hean9j66r3f5i72h
- fix UTF-8 unencoding by default on v3.0 vCards
- proper unescaping of strings in v3.0 vCards (also works for v2.1, although the 2.1 specs don't mention it)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
 
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
8
 
 * http://ed.am/dev/android/import-contacts
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
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
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
41
 
import java.util.Locale;
42
41
import java.util.NoSuchElementException;
43
42
import java.util.Set;
44
43
import java.util.Vector;
45
44
import java.util.regex.Matcher;
46
45
import java.util.regex.Pattern;
47
46
 
48
 
import android.annotation.SuppressLint;
49
47
import android.content.SharedPreferences;
 
48
import android.provider.Contacts;
 
49
import android.provider.Contacts.PhonesColumns;
50
50
 
51
51
public class VcardImporter extends Importer
52
52
{
58
58
                super( doit );
59
59
        }
60
60
 
61
 
        @SuppressLint( "SdCardPath" )
62
61
        @Override
63
62
        protected void onImport() throws AbortImportException
64
63
        {
83
82
                                // get files
84
83
                                class VCardFilter implements FilenameFilter {
85
84
                                        public boolean accept( File dir, String name ) {
86
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
85
                                                return name.toLowerCase().endsWith( ".vcf" );
87
86
                                        }
88
87
                                }
89
88
                                files = file.listFiles( new VCardFilter() );
569
568
 
570
569
                                // determine whether we care about this entry
571
570
                                final HashSet< String > interesting_fields =
572
 
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
573
 
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
571
                                        new HashSet< String >( Arrays.asList( new String[]
 
572
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
574
573
                                ) );
575
574
                                boolean is_interesting_field =
576
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
577
576
 
578
577
                                // parse encoding parameter
579
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
580
 
                                if( encoding != null )
581
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
582
580
                                if( is_interesting_field && encoding != null &&
583
581
                                        !encoding.equals( "8BIT" ) &&
584
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
589
587
 
590
588
                                // parse charset parameter
591
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
592
 
                                if( charset != null )
593
 
                                        charset = charset.toUpperCase( Locale.US );
 
590
                                if( charset != null ) charset = charset.toUpperCase();
594
591
                                if( charset != null &&
595
592
                                        !charset.equals( "US-ASCII" ) &&
596
593
                                        !charset.equals( "ASCII" ) &&
679
676
                                        parseEMAIL( name_param_parts, complete_value );
680
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
681
678
                                        parseADR( name_param_parts, complete_value );
682
 
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
683
 
                                        parseLABEL( name_param_parts, complete_value );
684
679
                        }
685
680
                }
686
681
 
882
877
                        int type;
883
878
                        if( types.contains( "FAX" ) )
884
879
                                if( types.contains( "HOME" ) )
885
 
                                        type = TYPE_FAX_HOME;
 
880
                                        type = PhonesColumns.TYPE_FAX_HOME;
886
881
                                else
887
 
                                        type = TYPE_FAX_WORK;
 
882
                                        type = PhonesColumns.TYPE_FAX_WORK;
888
883
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
889
 
                                type = TYPE_MOBILE;
 
884
                                type = PhonesColumns.TYPE_MOBILE;
890
885
                        else if( types.contains( "PAGER" ) )
891
 
                                type = TYPE_PAGER;
 
886
                                type = PhonesColumns.TYPE_PAGER;
892
887
                        else if( types.contains( "WORK" ) )
893
 
                                type = TYPE_WORK;
 
888
                                type = PhonesColumns.TYPE_WORK;
894
889
                        else
895
 
                                type = TYPE_HOME;
 
890
                                type = PhonesColumns.TYPE_HOME;
896
891
 
897
892
                        // add phone number
898
893
                        addNumber( value, type, is_preferred );
909
904
                        boolean is_preferred = types.contains( "PREF" );
910
905
                        int type;
911
906
                        if( types.contains( "WORK" ) )
912
 
                                type = TYPE_WORK;
 
907
                                type = Contacts.ContactMethods.TYPE_WORK;
913
908
                        else
914
 
                                type = TYPE_HOME;
 
909
                                type = Contacts.ContactMethods.TYPE_HOME;
915
910
 
916
911
                        addEmail( unescapeValue( value ), type, is_preferred );
917
912
                }
938
933
                                }
939
934
 
940
935
                        Set< String > types = extractTypes( params, Arrays.asList(
941
 
                                "PREF", "WORK", "HOME" ) );
942
 
 
943
 
                        // add address
944
 
                        int type;
945
 
                        if( types.contains( "WORK" ) )
946
 
                                type = TYPE_WORK;
947
 
                        else
948
 
                                type = TYPE_HOME;
949
 
 
950
 
                        addAddress( unescapeValue( value ), type );
951
 
                }
952
 
 
953
 
                private void parseLABEL( String[] params, String value )
954
 
                {
955
 
                        Set< String > types = extractTypes( params, Arrays.asList(
956
 
                                "PREF", "WORK", "HOME" ) );
957
 
 
958
 
                        // add address
959
 
                        int type;
960
 
                        if( types.contains( "WORK" ) )
961
 
                                type = TYPE_WORK;
962
 
                        else
963
 
                                type = TYPE_HOME;
 
936
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
937
 
 
938
                        // add address
 
939
                        int type;
 
940
                        if( types.contains( "WORK" ) )
 
941
                                type = Contacts.ContactMethods.TYPE_WORK;
 
942
                        else
 
943
                                type = Contacts.ContactMethods.TYPE_HOME;
964
944
 
965
945
                        addAddress( unescapeValue( value ), type );
966
946
                }
976
956
                        finalise();
977
957
                }
978
958
 
979
 
                /**
980
 
                 * Amongst the params, find the value of the first, only, of any with
981
 
                 * the specified name
982
 
                 * @param params
983
 
                 * @param name
984
 
                 * @return a value, or null
985
 
                 */
986
959
                private String checkParam( String[] params, String name )
987
960
                {
988
 
                        String[] res = checkParams( params, name );
989
 
                        return res.length > 0? res[ 0 ] : null;
990
 
                }
991
 
 
992
 
                /**
993
 
                 * Amongst the params, find the values of any with the specified name
994
 
                 * @param params
995
 
                 * @param name
996
 
                 * @return an array of values, or null
997
 
                 */
998
 
                private String[] checkParams( String[] params, String name )
999
 
                {
1000
 
                        HashSet< String > ret = new HashSet< String >();
1001
 
 
1002
961
                        Pattern p = Pattern.compile(
1003
962
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1004
963
                        for( int i = 0; i < params.length; i++ ) {
1005
964
                                Matcher m = p.matcher( params[ i ] );
1006
965
                                if( m.matches() )
1007
 
                                        ret.add( m.group( 2 ) );
 
966
                                        return m.group( 2 );
1008
967
                        }
1009
 
 
1010
 
                        return (String[]) ret.toArray( new String[ ret.size() ] );
 
968
                        return null;
1011
969
                }
1012
970
 
1013
 
                /**
1014
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1015
 
                 * those types are just parameters. For v3.0, they are prefixed with
1016
 
                 * "TYPE=". There may also be multiple type parameters.
1017
 
                 * @param params
1018
 
                 * @param a list of type values to look for
1019
 
                 * @return a set of present type values
1020
 
                 */
1021
971
                private Set< String > extractTypes( String[] params,
1022
972
                        List< String > valid_types )
1023
973
                {
1024
974
                        HashSet< String > types = new HashSet< String >();
1025
975
 
1026
976
                        // get 3.0-style TYPE= param
1027
 
                        String type_params[] = checkParams( params, "TYPE" );
1028
 
                        for( int a = 0; a < type_params.length; a++ )
1029
 
                        {
1030
 
                                // check for a comma-separated list of types (why? this isn't in
1031
 
                                // the specs!)
1032
 
                                String[] parts = type_params[ a ].split( "," );
 
977
                        String type_param;
 
978
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
979
                                String[] parts = type_param.split( "," );
1033
980
                                for( int i = 0; i < parts.length; i++ )
1034
981
                                        if( valid_types.contains( parts[ i ] ) )
1035
982
                                                types.add( parts[ i ] );