/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 16:52:28 UTC
  • Revision ID: edam@waxworlds.org-20110604165228-jam230oo29u1m06u
- properly handle multiple TYPE= params in one entry in a v3.0 vCard
- when deciding which phone number to use as the pimary number, a voice number takes precedence over a non-voice (fax or pager) number of the same standing in terms of being preferred or not.

Show diffs side-by-side

added added

removed removed

956
956
                        finalise();
957
957
                }
958
958
 
 
959
                /**
 
960
                 * Amongst the params, find the value of the first, only, of any with
 
961
                 * the specified name
 
962
                 * @param params
 
963
                 * @param name
 
964
                 * @return a value, or null
 
965
                 */
959
966
                private String checkParam( String[] params, String name )
960
967
                {
 
968
                        String[] res = checkParams( params, name );
 
969
                        return res.length > 0? res[ 0 ] : null;
 
970
                }
 
971
 
 
972
                /**
 
973
                 * Amongst the params, find the values of any with the specified name
 
974
                 * @param params
 
975
                 * @param name
 
976
                 * @return an array of values, or null
 
977
                 */
 
978
                private String[] checkParams( String[] params, String name )
 
979
                {
 
980
                        HashSet< String > ret = new HashSet< String >();
 
981
 
961
982
                        Pattern p = Pattern.compile(
962
983
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
963
984
                        for( int i = 0; i < params.length; i++ ) {
964
985
                                Matcher m = p.matcher( params[ i ] );
965
986
                                if( m.matches() )
966
 
                                        return m.group( 2 );
 
987
                                        ret.add( m.group( 2 ) );
967
988
                        }
968
 
                        return null;
 
989
 
 
990
                        return (String[]) ret.toArray( new String[ ret.size() ] );
969
991
                }
970
992
 
 
993
                /**
 
994
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
995
                 * those types are just parameters. For v3.0, they are prefixed with
 
996
                 * "TYPE=". There may also be multiple type parameters.
 
997
                 * @param params
 
998
                 * @param a list of type values to look for
 
999
                 * @return a set of present type values
 
1000
                 */
971
1001
                private Set< String > extractTypes( String[] params,
972
1002
                        List< String > valid_types )
973
1003
                {
974
1004
                        HashSet< String > types = new HashSet< String >();
975
1005
 
976
1006
                        // get 3.0-style TYPE= param
977
 
                        String type_param;
978
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
979
 
                                String[] parts = type_param.split( "," );
 
1007
                        String type_params[] = checkParams( params, "TYPE" );
 
1008
                        for( int a = 0; a < type_params.length; a++ )
 
1009
                        {
 
1010
                                // check for a comma-separated list of types (why? this isn't in
 
1011
                                // the specs!)
 
1012
                                String[] parts = type_params[ a ].split( "," );
980
1013
                                for( int i = 0; i < parts.length; i++ )
981
1014
                                        if( valid_types.contains( parts[ i ] ) )
982
1015
                                                types.add( parts[ i ] );