/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: 2014-03-02 10:59:21 UTC
  • Revision ID: tim@ed.am-20140302105921-0q7ytfva236xsv8s
removed some unused code, fixed locale warnings and made showContinueOrAbort()
abort implicitly (rather than return false)

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() );
228
236
                                                importContact( vcard );
229
237
                                        }
230
238
                                        catch( Vcard.ParseException e ) {
231
 
                                                if( !showContinue(
 
239
                                                showContinueOrAbort(
232
240
                                                        getText( R.string.error_vcf_parse ).toString()
233
241
                                                        + fileName +
234
242
                                                        getText( R.string.error_vcf_parse_line ).toString()
235
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
236
 
                                                {
237
 
                                                        finish( ACTION_ABORT );
238
 
                                                }
239
 
                                                else
240
 
                                                        skipContact();
 
243
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
 
244
                                                skipContact();
241
245
                                        }
242
246
                                        catch( ContactData.ContactNotIdentifiableException e ) {
243
 
                                                if( !showContinue(
 
247
                                                showContinueOrAbort(
244
248
                                                        getText( R.string.error_vcf_parse ).toString()
245
249
                                                        + fileName +
246
250
                                                        getText( R.string.error_vcf_parse_line ).toString()
247
251
                                                        + vcard_start_line + ":\n" + getText(
248
 
                                                                R.string.error_vcf_notenoughinfo ).toString()
249
 
                                                ) )
250
 
                                                {
251
 
                                                        finish( ACTION_ABORT );
252
 
                                                }
253
 
                                                else
254
 
                                                        skipContact();
 
252
                                                                R.string.error_vcf_notenoughinfo ).toString() );
 
253
                                                skipContact();
255
254
                                        }
256
255
 
257
256
                                        // discard this vcard
265
264
                                        }
266
265
                                        catch( Vcard.ParseException e ) {
267
266
                                                skipContact();
268
 
                                                if( !showContinue(
 
267
                                                showContinueOrAbort(
269
268
                                                        getText( R.string.error_vcf_parse ).toString()
270
269
                                                        + fileName +
271
270
                                                        getText( R.string.error_vcf_parse_line ).toString()
272
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
273
 
                                                {
274
 
                                                        finish( ACTION_ABORT );
275
 
                                                }
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
276
272
 
277
273
                                                // Although we're continuing, we still need to abort
278
274
                                                // this vCard.  Further lines will be ignored until we
609
605
                                ) );
610
606
                                boolean is_interesting_field =
611
607
                                        interesting_fields.contains(
612
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
608
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
613
609
 
614
610
                                // parse encoding parameter
615
611
                                String encoding = checkParam( name_param_parts, "ENCODING" );
616
612
                                if( encoding != null )
617
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
613
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
618
614
                                if( is_interesting_field && encoding != null &&
619
615
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
620
616
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
626
622
                                // parse charset parameter
627
623
                                String charset = checkParam( name_param_parts, "CHARSET" );
628
624
                                if( charset != null )
629
 
                                        charset = charset.toUpperCase( Locale.US );
 
625
                                        charset = charset.toUpperCase( Locale.ENGLISH );
630
626
                                if( charset != null &&
631
627
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
632
628
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
1108
1104
                                // this is in the specs!)
1109
1105
                                String[] parts = type_params[ a ].split( "," );
1110
1106
                                for( int i = 0; i < parts.length; i++ ) {
1111
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1107
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1112
1108
                                        if( valid_types.contains( ucpart ) )
1113
1109
                                                types.add( ucpart );
1114
1110
                                }
1117
1113
                        // get 2.1-style type param
1118
1114
                        if( _version.equals( "2.1" ) ) {
1119
1115
                                for( int i = 1; i < params.length; i++ ) {
1120
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1121
1117
                                        if( valid_types.contains( ucparam ) )
1122
1118
                                                types.add( ucparam );
1123
1119
                                }