/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: 2013-07-19 15:57:39 UTC
  • Revision ID: tim@ed.am-20130719155739-50w182nof760psos
Tags: 1.3.3
bump version no. to 1.3.3

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;
48
49
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" )
61
62
        @Override
62
63
        protected void onImport() throws AbortImportException
63
64
        {
70
71
                File[] files = null;
71
72
                try
72
73
                {
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
 
 
81
74
                        // open directory
82
 
                        File file = new File( Environment.getExternalStorageDirectory(),
83
 
                                prefs.getString( "location", "/" ) );
 
75
                        String path = "/sdcard" + prefs.getString( "location", "/" );
 
76
                        File file = new File( path );
84
77
                        if( !file.exists() )
85
78
                                showError( R.string.error_locationnotfound );
86
79
 
90
83
                                // get files
91
84
                                class VCardFilter implements FilenameFilter {
92
85
                                        public boolean accept( File dir, String name ) {
93
 
                                                return name.toLowerCase( Locale.ENGLISH )
94
 
                                                        .endsWith( ".vcf" );
 
86
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
95
87
                                        }
96
88
                                }
97
89
                                files = file.listFiles( new VCardFilter() );
157
149
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
158
150
                                        in_vcard = false;
159
151
                        }
160
 
                        reader.close();
161
152
 
162
153
                }
163
154
                catch( FileNotFoundException e ) {
185
176
                        FileInputStream istream = new FileInputStream( file );
186
177
                        byte[] content = new byte[ (int)file.length() ];
187
178
                        istream.read( content );
188
 
                        istream.close();
 
179
                        istream = null;
189
180
 
190
181
                        // import
191
182
                        importVCardFileContent( content, file.getName() );
236
227
                                                importContact( vcard );
237
228
                                        }
238
229
                                        catch( Vcard.ParseException e ) {
239
 
                                                showContinueOrAbort(
 
230
                                                if( !showContinue(
240
231
                                                        getText( R.string.error_vcf_parse ).toString()
241
232
                                                        + fileName +
242
233
                                                        getText( R.string.error_vcf_parse_line ).toString()
243
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
244
 
                                                skipContact();
 
234
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
235
                                                {
 
236
                                                        finish( ACTION_ABORT );
 
237
                                                }
 
238
                                                else
 
239
                                                        skipContact();
245
240
                                        }
246
241
                                        catch( ContactData.ContactNotIdentifiableException e ) {
247
 
                                                showContinueOrAbort(
 
242
                                                if( !showContinue(
248
243
                                                        getText( R.string.error_vcf_parse ).toString()
249
244
                                                        + fileName +
250
245
                                                        getText( R.string.error_vcf_parse_line ).toString()
251
246
                                                        + vcard_start_line + ":\n" + getText(
252
 
                                                                R.string.error_vcf_notenoughinfo ).toString() );
253
 
                                                skipContact();
254
 
                                        }
255
 
                                        catch( Vcard.SkipImportException e ) {
256
 
                                                skipContact();
 
247
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
248
                                                ) )
 
249
                                                {
 
250
                                                        finish( ACTION_ABORT );
 
251
                                                }
 
252
                                                else
 
253
                                                        skipContact();
257
254
                                        }
258
255
 
259
256
                                        // discard this vcard
267
264
                                        }
268
265
                                        catch( Vcard.ParseException e ) {
269
266
                                                skipContact();
270
 
                                                showContinueOrAbort(
 
267
                                                if( !showContinue(
271
268
                                                        getText( R.string.error_vcf_parse ).toString()
272
269
                                                        + fileName +
273
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
274
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
272
                                                {
 
273
                                                        finish( ACTION_ABORT );
 
274
                                                }
275
275
 
276
276
                                                // Although we're continuing, we still need to abort
277
277
                                                // this vCard.  Further lines will be ignored until we
471
471
                private String extractCollonPartFromLine( ContentLine content_line,
472
472
                        boolean former )
473
473
                {
 
474
                        String ret = null;
 
475
 
474
476
                        // split line into name and value parts and check to make sure we
475
477
                        // only got 2 parts and that the first part is not zero in length
476
478
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
477
479
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
478
 
                                return parts[ former? 0 : 1 ].trim();
 
480
                                ret = parts[ former? 0 : 1 ];
479
481
 
480
 
                        return null;
 
482
                        return ret;
481
483
                }
482
484
 
483
485
                private String extractNameAndParamsFromLine( ContentLine content_line )
484
486
                {
485
 
                        return extractCollonPartFromLine( content_line, true );
 
487
                        return extractCollonPartFromLine( content_line, true ).trim();
486
488
                }
487
489
 
488
490
                private String extractValueFromLine( ContentLine content_line )
506
508
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
507
509
                                {
508
510
                                        // yes, get it!
509
 
                                        String value = extractValueFromLine( content_line );
510
 
                                        if( value == null || (
511
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
512
 
                                        {
 
511
                                        String value = extractValueFromLine( content_line ).trim();
 
512
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
513
513
                                                throw new ParseException( R.string.error_vcf_version );
514
 
                                        }
515
514
                                        _version = value;
516
515
 
517
516
                                        // parse any buffers we've been accumulating while we waited
608
607
                                ) );
609
608
                                boolean is_interesting_field =
610
609
                                        interesting_fields.contains(
611
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
 
610
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
612
611
 
613
612
                                // parse encoding parameter
614
613
                                String encoding = checkParam( name_param_parts, "ENCODING" );
615
614
                                if( encoding != null )
616
 
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
 
615
                                        encoding = encoding.toUpperCase( Locale.US );
617
616
                                if( is_interesting_field && encoding != null &&
618
617
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
619
618
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
625
624
                                // parse charset parameter
626
625
                                String charset = checkParam( name_param_parts, "CHARSET" );
627
626
                                if( charset != null )
628
 
                                        charset = charset.toUpperCase( Locale.ENGLISH );
 
627
                                        charset = charset.toUpperCase( Locale.US );
629
628
                                if( charset != null &&
630
629
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
631
630
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
1038
1037
                }
1039
1038
 
1040
1039
                public void finaliseVcard()
1041
 
                        throws ParseException, ContactNotIdentifiableException,
1042
 
                                SkipImportException, AbortImportException
 
1040
                        throws ParseException, ContactNotIdentifiableException
1043
1041
                {
1044
 
                        // if there was content present, but no version line, then it must
1045
 
                        // be a version 2.1 vCard; process that content now
1046
 
                        if( _version == null && _content_lines != null ) {
1047
 
                                _version = "2.1";
1048
 
                                for( int i = 0; i < _content_lines.size(); i++ )
1049
 
                                        parseLine( _content_lines.get( i ) );
1050
 
                                _content_lines = null;
1051
 
                        }
 
1042
                        // missing version (and data is present)
 
1043
                        if( _version == null && _content_lines != null )
 
1044
                                throw new ParseException( R.string.error_vcf_malformed );
1052
1045
 
1053
1046
                        // finalise the parent class
1054
1047
                        finalise();
1113
1106
                                // this is in the specs!)
1114
1107
                                String[] parts = type_params[ a ].split( "," );
1115
1108
                                for( int i = 0; i < parts.length; i++ ) {
1116
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
 
1109
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
1117
1110
                                        if( valid_types.contains( ucpart ) )
1118
1111
                                                types.add( ucpart );
1119
1112
                                }
1122
1115
                        // get 2.1-style type param
1123
1116
                        if( _version.equals( "2.1" ) ) {
1124
1117
                                for( int i = 1; i < params.length; i++ ) {
1125
 
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
 
1118
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
1126
1119
                                        if( valid_types.contains( ucparam ) )
1127
1120
                                                types.add( ucparam );
1128
1121
                                }