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

  • Committer: edam
  • Date: 2012-04-24 09:56:17 UTC
  • Revision ID: tim@ed.am-20120424095617-lsfkkpkrlqk9xthn
updated all URLs, email addresses and package names to ed.am

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 <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
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://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/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 org.waxworlds.edam.importcontacts;
 
24
package am.ed.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[]
572
 
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
 
571
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
572
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
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 );
679
681
                        }
680
682
                }
681
683
 
933
935
                                }
934
936
 
935
937
                        Set< String > types = extractTypes( params, Arrays.asList(
936
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
 
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" ) );
937
954
 
938
955
                        // add address
939
956
                        int type;
956
973
                        finalise();
957
974
                }
958
975
 
 
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
                 */
959
983
                private String checkParam( String[] params, String name )
960
984
                {
 
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
 
961
999
                        Pattern p = Pattern.compile(
962
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
963
1001
                        for( int i = 0; i < params.length; i++ ) {
964
1002
                                Matcher m = p.matcher( params[ i ] );
965
1003
                                if( m.matches() )
966
 
                                        return m.group( 2 );
 
1004
                                        ret.add( m.group( 2 ) );
967
1005
                        }
968
 
                        return null;
 
1006
 
 
1007
                        return (String[]) ret.toArray( new String[ ret.size() ] );
969
1008
                }
970
1009
 
 
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
                 */
971
1018
                private Set< String > extractTypes( String[] params,
972
1019
                        List< String > valid_types )
973
1020
                {
974
1021
                        HashSet< String > types = new HashSet< String >();
975
1022
 
976
1023
                        // get 3.0-style TYPE= param
977
 
                        String type_param;
978
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
979
 
                                String[] parts = type_param.split( "," );
 
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( "," );
980
1030
                                for( int i = 0; i < parts.length; i++ )
981
1031
                                        if( valid_types.contains( parts[ i ] ) )
982
1032
                                                types.add( parts[ i ] );