/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:18:11 UTC
  • Revision ID: tim@ed.am-20160328181811-as61lllmvnv8e5os
handle vcards with missing vesion lines (treat as v2.1)

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
                                        }
 
255
                                        catch( Vcard.SkipImportException e ) {
 
256
                                                skipContact();
254
257
                                        }
255
258
 
256
259
                                        // discard this vcard
264
267
                                        }
265
268
                                        catch( Vcard.ParseException e ) {
266
269
                                                skipContact();
267
 
                                                if( !showContinue(
 
270
                                                showContinueOrAbort(
268
271
                                                        getText( R.string.error_vcf_parse ).toString()
269
272
                                                        + fileName +
270
273
                                                        getText( R.string.error_vcf_parse_line ).toString()
271
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
272
 
                                                {
273
 
                                                        finish( ACTION_ABORT );
274
 
                                                }
 
274
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
275
275
 
276
276
                                                // Although we're continuing, we still need to abort
277
277
                                                // this vCard.  Further lines will be ignored until we
608
608
                                ) );
609
609
                                boolean is_interesting_field =
610
610
                                        interesting_fields.contains(
611
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
 
611
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
612
612
 
613
613
                                // parse encoding parameter
614
614
                                String encoding = checkParam( name_param_parts, "ENCODING" );
615
615
                                if( encoding != null )
616
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
616
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
617
617
                                if( is_interesting_field && encoding != null &&
618
618
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
619
619
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
625
625
                                // parse charset parameter
626
626
                                String charset = checkParam( name_param_parts, "CHARSET" );
627
627
                                if( charset != null )
628
 
                                        charset = charset.toUpperCase( Locale.US );
 
628
                                        charset = charset.toUpperCase( Locale.ENGLISH );
629
629
                                if( charset != null &&
630
630
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
631
631
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
1038
1038
                }
1039
1039
 
1040
1040
                public void finaliseVcard()
1041
 
                        throws ParseException, ContactNotIdentifiableException
 
1041
                        throws ParseException, ContactNotIdentifiableException,
 
1042
                                SkipImportException, AbortImportException
1042
1043
                {
1043
 
                        // missing version (and data is present)
1044
 
                        if( _version == null && _content_lines != null )
1045
 
                                throw new ParseException( R.string.error_vcf_malformed );
 
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
                        }
1046
1052
 
1047
1053
                        // finalise the parent class
1048
1054
                        finalise();
1107
1113
                                // this is in the specs!)
1108
1114
                                String[] parts = type_params[ a ].split( "," );
1109
1115
                                for( int i = 0; i < parts.length; i++ ) {
1110
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
 
1116
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
1111
1117
                                        if( valid_types.contains( ucpart ) )
1112
1118
                                                types.add( ucpart );
1113
1119
                                }
1116
1122
                        // get 2.1-style type param
1117
1123
                        if( _version.equals( "2.1" ) ) {
1118
1124
                                for( int i = 1; i < params.length; i++ ) {
1119
 
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
 
1125
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
1120
1126
                                        if( valid_types.contains( ucparam ) )
1121
1127
                                                types.add( ucparam );
1122
1128
                                }