/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

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
 
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
685
 
                                        parseNOTE( name_param_parts, complete_value );
686
679
                        }
687
680
                }
688
681
 
760
753
                                in_escape = false;
761
754
                                switch( c )
762
755
                                {
763
 
                                case 'T':
764
 
                                case 't':
765
 
                                        // add tab (invalid/non-standard, but accepted)
766
 
                                        ret.append( '\t' );
767
 
                                        break;
768
756
                                case 'N':
769
757
                                case 'n':
770
758
                                        // add newline
778
766
                                        break;
779
767
                                default:
780
768
                                        // unknown escape sequence, so add it unescaped
781
 
                                        // (invalid/non-standard, but accepted)
782
769
                                        ret.append( "\\" );
783
770
                                        ret.append( Character.toChars( c ) );
784
771
                                        break;
890
877
                        int type;
891
878
                        if( types.contains( "FAX" ) )
892
879
                                if( types.contains( "HOME" ) )
893
 
                                        type = TYPE_FAX_HOME;
 
880
                                        type = PhonesColumns.TYPE_FAX_HOME;
894
881
                                else
895
 
                                        type = TYPE_FAX_WORK;
 
882
                                        type = PhonesColumns.TYPE_FAX_WORK;
896
883
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
897
 
                                type = TYPE_MOBILE;
 
884
                                type = PhonesColumns.TYPE_MOBILE;
898
885
                        else if( types.contains( "PAGER" ) )
899
 
                                type = TYPE_PAGER;
 
886
                                type = PhonesColumns.TYPE_PAGER;
900
887
                        else if( types.contains( "WORK" ) )
901
 
                                type = TYPE_WORK;
 
888
                                type = PhonesColumns.TYPE_WORK;
902
889
                        else
903
 
                                type = TYPE_HOME;
 
890
                                type = PhonesColumns.TYPE_HOME;
904
891
 
905
892
                        // add phone number
906
893
                        addNumber( value, type, is_preferred );
917
904
                        boolean is_preferred = types.contains( "PREF" );
918
905
                        int type;
919
906
                        if( types.contains( "WORK" ) )
920
 
                                type = TYPE_WORK;
 
907
                                type = Contacts.ContactMethods.TYPE_WORK;
921
908
                        else
922
 
                                type = TYPE_HOME;
 
909
                                type = Contacts.ContactMethods.TYPE_HOME;
923
910
 
924
911
                        addEmail( unescapeValue( value ), type, is_preferred );
925
912
                }
946
933
                                }
947
934
 
948
935
                        Set< String > types = extractTypes( params, Arrays.asList(
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 ) );
 
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 );
979
946
                }
980
947
 
981
948
                public void finaliseVcard()
1013
980
                        HashSet< String > ret = new HashSet< String >();
1014
981
 
1015
982
                        Pattern p = Pattern.compile(
1016
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
1017
 
                                Pattern.CASE_INSENSITIVE );
 
983
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1018
984
                        for( int i = 0; i < params.length; i++ ) {
1019
985
                                Matcher m = p.matcher( params[ i ] );
1020
986
                                if( m.matches() )
1028
994
                 * Amongst the params, return any type values present. For v2.1 vCards,
1029
995
                 * those types are just parameters. For v3.0, they are prefixed with
1030
996
                 * "TYPE=". There may also be multiple type parameters.
1031
 
                 * @param params an array of params to look for types in
1032
 
                 * @param valid_types an list of upper-case type values to look for
 
997
                 * @param params
 
998
                 * @param a list of type values to look for
1033
999
                 * @return a set of present type values
1034
1000
                 */
1035
1001
                private Set< String > extractTypes( String[] params,
1045
1011
                                // the specs!)
1046
1012
                                String[] parts = type_params[ a ].split( "," );
1047
1013
                                for( int i = 0; i < parts.length; i++ )
1048
 
                                        parts[ i ] = parts[ i ].toUpperCase( Locale.US );
1049
 
                                for( int i = 0; i < parts.length; i++ )
1050
1014
                                        if( valid_types.contains( parts[ i ] ) )
1051
1015
                                                types.add( parts[ i ] );
1052
1016
                        }