/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: Tim Marston
  • Date: 2016-03-28 18:13:50 UTC
  • Revision ID: tim@ed.am-20160328181350-ytxvox60fxj1tevm
fixed typo in strings

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 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
 
7
 * to as "this program").  For more information, see
8
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
45
45
import java.util.regex.Matcher;
46
46
import java.util.regex.Pattern;
47
47
 
48
 
import android.annotation.SuppressLint;
49
48
import android.content.SharedPreferences;
 
49
import android.os.Environment;
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
        {
71
70
                File[] files = null;
72
71
                try
73
72
                {
 
73
                        // check SD card is mounted
 
74
                        String state = Environment.getExternalStorageState();
 
75
                        if( !Environment.MEDIA_MOUNTED.equals( state ) &&
 
76
                                !Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) )
 
77
                        {
 
78
                                showError( R.string.error_nosdcard );
 
79
                        }
 
80
 
74
81
                        // open directory
75
 
                        String path = "/sdcard" + prefs.getString( "location", "/" );
76
 
                        File file = new File( path );
 
82
                        File file = new File( Environment.getExternalStorageDirectory(),
 
83
                                prefs.getString( "location", "/" ) );
77
84
                        if( !file.exists() )
78
85
                                showError( R.string.error_locationnotfound );
79
86
 
83
90
                                // get files
84
91
                                class VCardFilter implements FilenameFilter {
85
92
                                        public boolean accept( File dir, String name ) {
86
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
93
                                                return name.toLowerCase( Locale.ENGLISH )
 
94
                                                        .endsWith( ".vcf" );
87
95
                                        }
88
96
                                }
89
97
                                files = file.listFiles( new VCardFilter() );
133
141
                        boolean in_vcard = false;
134
142
                        while( ( line = reader.readLine() ) != null )
135
143
                        {
136
 
                                if( !in_vcard ) {
 
144
                                if( !in_vcard )
 
145
                                {
137
146
                                        // look for vcard beginning
138
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
147
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
139
148
                                                in_vcard = true;
140
149
                                                _vcard_count++;
141
150
                                        }
 
151
                                        // check for vMsg files
 
152
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
 
153
                                                showError( getText( R.string.error_vcf_vmsgfile )
 
154
                                                        + file.getName() );
 
155
                                        }
142
156
                                }
143
 
                                else if( line.matches( "^END:VCARD" ) )
 
157
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
144
158
                                        in_vcard = false;
145
159
                        }
 
160
                        reader.close();
146
161
 
147
162
                }
148
163
                catch( FileNotFoundException e ) {
170
185
                        FileInputStream istream = new FileInputStream( file );
171
186
                        byte[] content = new byte[ (int)file.length() ];
172
187
                        istream.read( content );
173
 
                        istream = null;
 
188
                        istream.close();
174
189
 
175
190
                        // import
176
191
                        importVCardFileContent( content, file.getName() );
203
218
 
204
219
                        if( vcard == null ) {
205
220
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
 
221
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
207
222
                                        setProgress( _progress++ );
208
223
                                        vcard = new Vcard();
209
224
                                        vcard_start_line = cli.getLineNumber();
211
226
                        }
212
227
                        else {
213
228
                                // look for vcard content or ending
214
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
 
229
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
215
230
                                {
216
231
                                        // finalise the vcard/contact
217
232
                                        try {
221
236
                                                importContact( vcard );
222
237
                                        }
223
238
                                        catch( Vcard.ParseException e ) {
224
 
                                                if( !showContinue(
 
239
                                                showContinueOrAbort(
225
240
                                                        getText( R.string.error_vcf_parse ).toString()
226
241
                                                        + fileName +
227
242
                                                        getText( R.string.error_vcf_parse_line ).toString()
228
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
229
 
                                                {
230
 
                                                        finish( ACTION_ABORT );
231
 
                                                }
232
 
                                                else
233
 
                                                        skipContact();
 
243
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
 
244
                                                skipContact();
234
245
                                        }
235
246
                                        catch( ContactData.ContactNotIdentifiableException e ) {
236
 
                                                if( !showContinue(
 
247
                                                showContinueOrAbort(
237
248
                                                        getText( R.string.error_vcf_parse ).toString()
238
249
                                                        + fileName +
239
250
                                                        getText( R.string.error_vcf_parse_line ).toString()
240
251
                                                        + vcard_start_line + ":\n" + getText(
241
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
242
 
                                                ) )
243
 
                                                {
244
 
                                                        finish( ACTION_ABORT );
245
 
                                                }
246
 
                                                else
247
 
                                                        skipContact();
 
252
                                                                R.string.error_vcf_notenoughinfo ).toString() );
 
253
                                                skipContact();
248
254
                                        }
249
255
 
250
256
                                        // discard this vcard
258
264
                                        }
259
265
                                        catch( Vcard.ParseException e ) {
260
266
                                                skipContact();
261
 
                                                if( !showContinue(
 
267
                                                showContinueOrAbort(
262
268
                                                        getText( R.string.error_vcf_parse ).toString()
263
269
                                                        + fileName +
264
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
265
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
266
 
                                                {
267
 
                                                        finish( ACTION_ABORT );
268
 
                                                }
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
269
272
 
270
 
                                                // although we're continuing, we still need to abort
271
 
                                                // this vCard. Further lines will be ignored until we
 
273
                                                // Although we're continuing, we still need to abort
 
274
                                                // this vCard.  Further lines will be ignored until we
272
275
                                                // get to another BEGIN:VCARD line.
273
276
                                                vcard = null;
274
277
                                        }
275
278
                                        catch( Vcard.SkipImportException e ) {
276
279
                                                skipContact();
277
 
                                                // abort this vCard. Further lines will be ignored until
 
280
                                                // Abort this vCard.  Further lines will be ignored until
278
281
                                                // we get to another BEGIN:VCARD line.
279
282
                                                vcard = null;
280
283
                                        }
465
468
                private String extractCollonPartFromLine( ContentLine content_line,
466
469
                        boolean former )
467
470
                {
468
 
                        String ret = null;
469
 
 
470
471
                        // split line into name and value parts and check to make sure we
471
472
                        // only got 2 parts and that the first part is not zero in length
472
473
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
473
474
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
474
 
                                ret = parts[ former? 0 : 1 ];
 
475
                                return parts[ former? 0 : 1 ].trim();
475
476
 
476
 
                        return ret;
 
477
                        return null;
477
478
                }
478
479
 
479
480
                private String extractNameAndParamsFromLine( ContentLine content_line )
503
504
                                {
504
505
                                        // yes, get it!
505
506
                                        String value = extractValueFromLine( content_line );
506
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
507
                                        if( value == null || (
 
508
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
509
                                        {
507
510
                                                throw new ParseException( R.string.error_vcf_version );
 
511
                                        }
508
512
                                        _version = value;
509
513
 
510
514
                                        // parse any buffers we've been accumulating while we waited
601
605
                                ) );
602
606
                                boolean is_interesting_field =
603
607
                                        interesting_fields.contains(
604
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
608
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
605
609
 
606
610
                                // parse encoding parameter
607
611
                                String encoding = checkParam( name_param_parts, "ENCODING" );
608
612
                                if( encoding != null )
609
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
613
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
610
614
                                if( is_interesting_field && encoding != null &&
611
615
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
612
616
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
618
622
                                // parse charset parameter
619
623
                                String charset = checkParam( name_param_parts, "CHARSET" );
620
624
                                if( charset != null )
621
 
                                        charset = charset.toUpperCase( Locale.US );
 
625
                                        charset = charset.toUpperCase( Locale.ENGLISH );
622
626
                                if( charset != null &&
623
627
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
624
628
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
717
721
                                        parseLABEL( name_param_parts, complete_value );
718
722
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
719
723
                                        parseNOTE( name_param_parts, complete_value );
 
724
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
725
                                        parseBDAY( name_param_parts, complete_value );
720
726
                        }
721
727
                }
722
728
 
746
752
                        {
747
753
                                String str = parts.get( a );
748
754
 
749
 
                                // look for parts that end in an escape character, but ignore
750
 
                                // the final part. We've already detected escape chars at the
 
755
                                // Look for parts that end in an escape character, but ignore
 
756
                                // the final part.  We've already detected escape chars at the
751
757
                                // end of the final part in parseLine() and handled multi-lines
752
758
                                // accordingly.
753
759
                                if( a < parts.size() - 1 &&
1023
1029
                        addNote( unescapeValue( value ) );
1024
1030
                }
1025
1031
 
 
1032
                private void parseBDAY( String[] params, String value )
 
1033
                {
 
1034
                        setBirthday( value );
 
1035
                }
 
1036
 
1026
1037
                public void finaliseVcard()
1027
1038
                        throws ParseException, ContactNotIdentifiableException
1028
1039
                {
1036
1047
 
1037
1048
                /**
1038
1049
                 * Amongst the params, find the value of the first, only, of any with
1039
 
                 * the specified name
 
1050
                 * the specified name.
 
1051
                 *
1040
1052
                 * @param params
1041
1053
                 * @param name
1042
1054
                 * @return a value, or null
1048
1060
                }
1049
1061
 
1050
1062
                /**
1051
 
                 * Amongst the params, find the values of any with the specified name
 
1063
                 * Amongst the params, find the values of any with the specified name.
 
1064
                 *
1052
1065
                 * @param params
1053
1066
                 * @param name
1054
1067
                 * @return an array of values, or null
1070
1083
                }
1071
1084
 
1072
1085
                /**
1073
 
                 * Amongst the params, return any type values present. For v2.1 vCards,
1074
 
                 * those types are just parameters. For v3.0, they are prefixed with
1075
 
                 * "TYPE=". There may also be multiple type parameters.
 
1086
                 * Amongst the params, return any type values present.  For v2.1 vCards,
 
1087
                 * those types are just parameters.  For v3.0, they are prefixed with
 
1088
                 * "TYPE=".  There may also be multiple type parameters.
 
1089
                 *
1076
1090
                 * @param params an array of params to look for types in
1077
1091
                 * @param valid_types an list of upper-case type values to look for
1078
1092
                 * @return a set of present type values
1090
1104
                                // this is in the specs!)
1091
1105
                                String[] parts = type_params[ a ].split( "," );
1092
1106
                                for( int i = 0; i < parts.length; i++ ) {
1093
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1107
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1094
1108
                                        if( valid_types.contains( ucpart ) )
1095
1109
                                                types.add( ucpart );
1096
1110
                                }
1099
1113
                        // get 2.1-style type param
1100
1114
                        if( _version.equals( "2.1" ) ) {
1101
1115
                                for( int i = 1; i < params.length; i++ ) {
1102
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1103
1117
                                        if( valid_types.contains( ucparam ) )
1104
1118
                                                types.add( ucparam );
1105
1119
                                }
1130
1144
                                else if( ch == '=' && i == in.limit() - 1 )
1131
1145
                                {
1132
1146
                                        // we found a '=' at the end of a line signifying a multi-
1133
 
                                        // line string, so we don't add it.
 
1147
                                        // line string, so we don't add it
1134
1148
                                        another = true;
1135
1149
                                        continue;
1136
1150
                                }