/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

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;
568
568
 
569
569
                                // determine whether we care about this entry
570
570
                                final HashSet< String > interesting_fields =
571
 
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
572
 
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
571
                                        new HashSet< String >( Arrays.asList( new String[]
 
572
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
573
573
                                ) );
574
574
                                boolean is_interesting_field =
575
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
676
676
                                        parseEMAIL( name_param_parts, complete_value );
677
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
678
                                        parseADR( name_param_parts, complete_value );
679
 
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
680
 
                                        parseLABEL( name_param_parts, complete_value );
681
679
                        }
682
680
                }
683
681
 
935
933
                                }
936
934
 
937
935
                        Set< String > types = extractTypes( params, Arrays.asList(
938
 
                                "PREF", "WORK", "HOME" ) );
939
 
 
940
 
                        // add address
941
 
                        int type;
942
 
                        if( types.contains( "WORK" ) )
943
 
                                type = Contacts.ContactMethods.TYPE_WORK;
944
 
                        else
945
 
                                type = Contacts.ContactMethods.TYPE_HOME;
946
 
 
947
 
                        addAddress( unescapeValue( value ), type );
948
 
                }
949
 
 
950
 
                private void parseLABEL( String[] params, String value )
951
 
                {
952
 
                        Set< String > types = extractTypes( params, Arrays.asList(
953
 
                                "PREF", "WORK", "HOME" ) );
 
936
                                "PREF", "WORK", "HOME", "INTERNET" ) );
954
937
 
955
938
                        // add address
956
939
                        int type;
973
956
                        finalise();
974
957
                }
975
958
 
976
 
                /**
977
 
                 * Amongst the params, find the value of the first, only, of any with
978
 
                 * the specified name
979
 
                 * @param params
980
 
                 * @param name
981
 
                 * @return a value, or null
982
 
                 */
983
959
                private String checkParam( String[] params, String name )
984
960
                {
985
 
                        String[] res = checkParams( params, name );
986
 
                        return res.length > 0? res[ 0 ] : null;
987
 
                }
988
 
 
989
 
                /**
990
 
                 * Amongst the params, find the values of any with the specified name
991
 
                 * @param params
992
 
                 * @param name
993
 
                 * @return an array of values, or null
994
 
                 */
995
 
                private String[] checkParams( String[] params, String name )
996
 
                {
997
 
                        HashSet< String > ret = new HashSet< String >();
998
 
 
999
961
                        Pattern p = Pattern.compile(
1000
962
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1001
963
                        for( int i = 0; i < params.length; i++ ) {
1002
964
                                Matcher m = p.matcher( params[ i ] );
1003
965
                                if( m.matches() )
1004
 
                                        ret.add( m.group( 2 ) );
 
966
                                        return m.group( 2 );
1005
967
                        }
1006
 
 
1007
 
                        return (String[]) ret.toArray( new String[ ret.size() ] );
 
968
                        return null;
1008
969
                }
1009
970
 
1010
 
                /**
1011
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1012
 
                 * those types are just parameters. For v3.0, they are prefixed with
1013
 
                 * "TYPE=". There may also be multiple type parameters.
1014
 
                 * @param params
1015
 
                 * @param a list of type values to look for
1016
 
                 * @return a set of present type values
1017
 
                 */
1018
971
                private Set< String > extractTypes( String[] params,
1019
972
                        List< String > valid_types )
1020
973
                {
1021
974
                        HashSet< String > types = new HashSet< String >();
1022
975
 
1023
976
                        // get 3.0-style TYPE= param
1024
 
                        String type_params[] = checkParams( params, "TYPE" );
1025
 
                        for( int a = 0; a < type_params.length; a++ )
1026
 
                        {
1027
 
                                // check for a comma-separated list of types (why? this isn't in
1028
 
                                // the specs!)
1029
 
                                String[] parts = type_params[ a ].split( "," );
 
977
                        String type_param;
 
978
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
 
979
                                String[] parts = type_param.split( "," );
1030
980
                                for( int i = 0; i < parts.length; i++ )
1031
981
                                        if( valid_types.contains( parts[ i ] ) )
1032
982
                                                types.add( parts[ i ] );