/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

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() );
149
157
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
150
158
                                        in_vcard = false;
151
159
                        }
 
160
                        reader.close();
152
161
 
153
162
                }
154
163
                catch( FileNotFoundException e ) {
176
185
                        FileInputStream istream = new FileInputStream( file );
177
186
                        byte[] content = new byte[ (int)file.length() ];
178
187
                        istream.read( content );
179
 
                        istream = null;
 
188
                        istream.close();
180
189
 
181
190
                        // import
182
191
                        importVCardFileContent( content, file.getName() );
227
236
                                                importContact( vcard );
228
237
                                        }
229
238
                                        catch( Vcard.ParseException e ) {
230
 
                                                if( !showContinue(
 
239
                                                showContinueOrAbort(
231
240
                                                        getText( R.string.error_vcf_parse ).toString()
232
241
                                                        + fileName +
233
242
                                                        getText( R.string.error_vcf_parse_line ).toString()
234
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
235
 
                                                {
236
 
                                                        finish( ACTION_ABORT );
237
 
                                                }
238
 
                                                else
239
 
                                                        skipContact();
 
243
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
 
244
                                                skipContact();
240
245
                                        }
241
246
                                        catch( ContactData.ContactNotIdentifiableException e ) {
242
 
                                                if( !showContinue(
 
247
                                                showContinueOrAbort(
243
248
                                                        getText( R.string.error_vcf_parse ).toString()
244
249
                                                        + fileName +
245
250
                                                        getText( R.string.error_vcf_parse_line ).toString()
246
251
                                                        + vcard_start_line + ":\n" + getText(
247
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
248
 
                                                ) )
249
 
                                                {
250
 
                                                        finish( ACTION_ABORT );
251
 
                                                }
252
 
                                                else
253
 
                                                        skipContact();
 
252
                                                                R.string.error_vcf_notenoughinfo ).toString() );
 
253
                                                skipContact();
254
254
                                        }
255
255
 
256
256
                                        // discard this vcard
264
264
                                        }
265
265
                                        catch( Vcard.ParseException e ) {
266
266
                                                skipContact();
267
 
                                                if( !showContinue(
 
267
                                                showContinueOrAbort(
268
268
                                                        getText( R.string.error_vcf_parse ).toString()
269
269
                                                        + fileName +
270
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
271
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
272
 
                                                {
273
 
                                                        finish( ACTION_ABORT );
274
 
                                                }
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
275
272
 
276
273
                                                // Although we're continuing, we still need to abort
277
274
                                                // this vCard.  Further lines will be ignored until we
471
468
                private String extractCollonPartFromLine( ContentLine content_line,
472
469
                        boolean former )
473
470
                {
474
 
                        String ret = null;
475
 
 
476
471
                        // split line into name and value parts and check to make sure we
477
472
                        // only got 2 parts and that the first part is not zero in length
478
473
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
479
474
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
480
 
                                ret = parts[ former? 0 : 1 ];
 
475
                                return parts[ former? 0 : 1 ].trim();
481
476
 
482
 
                        return ret;
 
477
                        return null;
483
478
                }
484
479
 
485
480
                private String extractNameAndParamsFromLine( ContentLine content_line )
486
481
                {
487
 
                        return extractCollonPartFromLine( content_line, true ).trim();
 
482
                        return extractCollonPartFromLine( content_line, true );
488
483
                }
489
484
 
490
485
                private String extractValueFromLine( ContentLine content_line )
508
503
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
509
504
                                {
510
505
                                        // yes, get it!
511
 
                                        String value = extractValueFromLine( content_line ).trim();
512
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
506
                                        String value = extractValueFromLine( content_line );
 
507
                                        if( value == null || (
 
508
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
509
                                        {
513
510
                                                throw new ParseException( R.string.error_vcf_version );
 
511
                                        }
514
512
                                        _version = value;
515
513
 
516
514
                                        // parse any buffers we've been accumulating while we waited
607
605
                                ) );
608
606
                                boolean is_interesting_field =
609
607
                                        interesting_fields.contains(
610
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
608
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
611
609
 
612
610
                                // parse encoding parameter
613
611
                                String encoding = checkParam( name_param_parts, "ENCODING" );
614
612
                                if( encoding != null )
615
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
613
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
616
614
                                if( is_interesting_field && encoding != null &&
617
615
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
618
616
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
624
622
                                // parse charset parameter
625
623
                                String charset = checkParam( name_param_parts, "CHARSET" );
626
624
                                if( charset != null )
627
 
                                        charset = charset.toUpperCase( Locale.US );
 
625
                                        charset = charset.toUpperCase( Locale.ENGLISH );
628
626
                                if( charset != null &&
629
627
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
630
628
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
1106
1104
                                // this is in the specs!)
1107
1105
                                String[] parts = type_params[ a ].split( "," );
1108
1106
                                for( int i = 0; i < parts.length; i++ ) {
1109
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1107
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1110
1108
                                        if( valid_types.contains( ucpart ) )
1111
1109
                                                types.add( ucpart );
1112
1110
                                }
1115
1113
                        // get 2.1-style type param
1116
1114
                        if( _version.equals( "2.1" ) ) {
1117
1115
                                for( int i = 1; i < params.length; i++ ) {
1118
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1119
1117
                                        if( valid_types.contains( ucparam ) )
1120
1118
                                                types.add( ucparam );
1121
1119
                                }