/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-12-20 17:16:08 UTC
  • Revision ID: tim@ed.am-20121220171608-vx41zykf4krel9xf
slightly improved the efficiency of the cache identifier factory function

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;
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;
46
47
 
 
48
import android.annotation.SuppressLint;
47
49
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" )
61
62
        @Override
62
63
        protected void onImport() throws AbortImportException
63
64
        {
82
83
                                // get files
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" );
86
87
                                        }
87
88
                                }
88
89
                                files = file.listFiles( new VCardFilter() );
568
569
 
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" }
573
574
                                ) );
574
575
                                boolean is_interesting_field =
575
576
                                        interesting_fields.contains( name_param_parts[ 0 ] );
576
577
 
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" ) )
587
589
 
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 );
679
686
                        }
680
687
                }
681
688
 
753
760
                                in_escape = false;
754
761
                                switch( c )
755
762
                                {
 
763
                                case 'T':
 
764
                                case 't':
 
765
                                        // add tab (invalid/non-standard, but accepted)
 
766
                                        ret.append( '\t' );
 
767
                                        break;
756
768
                                case 'N':
757
769
                                case 'n':
758
770
                                        // add newline
766
778
                                        break;
767
779
                                default:
768
780
                                        // unknown escape sequence, so add it unescaped
 
781
                                        // (invalid/non-standard, but accepted)
769
782
                                        ret.append( "\\" );
770
783
                                        ret.append( Character.toChars( c ) );
771
784
                                        break;
877
890
                        int type;
878
891
                        if( types.contains( "FAX" ) )
879
892
                                if( types.contains( "HOME" ) )
880
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
893
                                        type = TYPE_FAX_HOME;
881
894
                                else
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;
 
897
                                type = TYPE_MOBILE;
885
898
                        else if( types.contains( "PAGER" ) )
886
 
                                type = PhonesColumns.TYPE_PAGER;
 
899
                                type = TYPE_PAGER;
887
900
                        else if( types.contains( "WORK" ) )
888
 
                                type = PhonesColumns.TYPE_WORK;
 
901
                                type = TYPE_WORK;
889
902
                        else
890
 
                                type = PhonesColumns.TYPE_HOME;
 
903
                                type = TYPE_HOME;
891
904
 
892
905
                        // add phone number
893
906
                        addNumber( value, type, is_preferred );
904
917
                        boolean is_preferred = types.contains( "PREF" );
905
918
                        int type;
906
919
                        if( types.contains( "WORK" ) )
907
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
920
                                type = TYPE_WORK;
908
921
                        else
909
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
922
                                type = TYPE_HOME;
910
923
 
911
924
                        addEmail( unescapeValue( value ), type, is_preferred );
912
925
                }
933
946
                                }
934
947
 
935
948
                        Set< String > types = extractTypes( params, Arrays.asList(
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;
944
 
 
945
 
                        addAddress( unescapeValue( value ), type );
 
949
                                "PREF", "WORK", "HOME" ) );
 
950
 
 
951
                        // add address
 
952
                        int type;
 
953
                        if( types.contains( "WORK" ) )
 
954
                                type = TYPE_WORK;
 
955
                        else
 
956
                                type = TYPE_HOME;
 
957
 
 
958
                        addAddress( unescapeValue( value ), type );
 
959
                }
 
960
 
 
961
                private void parseLABEL( String[] params, String value )
 
962
                {
 
963
                        Set< String > types = extractTypes( params, Arrays.asList(
 
964
                                "PREF", "WORK", "HOME" ) );
 
965
 
 
966
                        // add address
 
967
                        int type;
 
968
                        if( types.contains( "WORK" ) )
 
969
                                type = TYPE_WORK;
 
970
                        else
 
971
                                type = TYPE_HOME;
 
972
 
 
973
                        addAddress( unescapeValue( value ), type );
 
974
                }
 
975
 
 
976
                private void parseNOTE( String[] params, String value )
 
977
                {
 
978
                        addNote( unescapeValue( value ) );
946
979
                }
947
980
 
948
981
                public void finaliseVcard()
956
989
                        finalise();
957
990
                }
958
991
 
 
992
                /**
 
993
                 * Amongst the params, find the value of the first, only, of any with
 
994
                 * the specified name
 
995
                 * @param params
 
996
                 * @param name
 
997
                 * @return a value, or null
 
998
                 */
959
999
                private String checkParam( String[] params, String name )
960
1000
                {
 
1001
                        String[] res = checkParams( params, name );
 
1002
                        return res.length > 0? res[ 0 ] : null;
 
1003
                }
 
1004
 
 
1005
                /**
 
1006
                 * Amongst the params, find the values of any with the specified name
 
1007
                 * @param params
 
1008
                 * @param name
 
1009
                 * @return an array of values, or null
 
1010
                 */
 
1011
                private String[] checkParams( String[] params, String name )
 
1012
                {
 
1013
                        HashSet< String > ret = new HashSet< String >();
 
1014
 
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() )
966
 
                                        return m.group( 2 );
 
1020
                                        ret.add( m.group( 2 ) );
967
1021
                        }
968
 
                        return null;
 
1022
 
 
1023
                        return (String[]) ret.toArray( new String[ ret.size() ] );
969
1024
                }
970
1025
 
 
1026
                /**
 
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.
 
1030
                 * @param params
 
1031
                 * @param a list of type values to look for
 
1032
                 * @return a set of present type values
 
1033
                 */
971
1034
                private Set< String > extractTypes( String[] params,
972
1035
                        List< String > valid_types )
973
1036
                {
974
1037
                        HashSet< String > types = new HashSet< String >();
975
1038
 
976
1039
                        // get 3.0-style TYPE= param
977
 
                        String 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++ )
 
1042
                        {
 
1043
                                // check for a comma-separated list of types (why? this isn't in
 
1044
                                // the specs!)
 
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 ] );