/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-05-10 12:16:15 UTC
  • Revision ID: tim@ed.am-20130510121615-g8qhsu0h3t0ymrt6
Tags: 1.3.2
fix spacing in NEWS

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 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;
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();
 
247
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
248
                                                ) )
 
249
                                                {
 
250
                                                        finish( ACTION_ABORT );
 
251
                                                }
 
252
                                                else
 
253
                                                        skipContact();
254
254
                                        }
255
255
 
256
256
                                        // discard this vcard
264
264
                                        }
265
265
                                        catch( Vcard.ParseException e ) {
266
266
                                                skipContact();
267
 
                                                showContinueOrAbort(
 
267
                                                if( !showContinue(
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() );
 
271
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
272
                                                {
 
273
                                                        finish( ACTION_ABORT );
 
274
                                                }
272
275
 
273
 
                                                // Although we're continuing, we still need to abort
274
 
                                                // this vCard.  Further lines will be ignored until we
 
276
                                                // although we're continuing, we still need to abort
 
277
                                                // this vCard. Further lines will be ignored until we
275
278
                                                // get to another BEGIN:VCARD line.
276
279
                                                vcard = null;
277
280
                                        }
278
281
                                        catch( Vcard.SkipImportException e ) {
279
282
                                                skipContact();
280
 
                                                // Abort this vCard.  Further lines will be ignored until
 
283
                                                // abort this vCard. Further lines will be ignored until
281
284
                                                // we get to another BEGIN:VCARD line.
282
285
                                                vcard = null;
283
286
                                        }
468
471
                private String extractCollonPartFromLine( ContentLine content_line,
469
472
                        boolean former )
470
473
                {
 
474
                        String ret = null;
 
475
 
471
476
                        // split line into name and value parts and check to make sure we
472
477
                        // only got 2 parts and that the first part is not zero in length
473
478
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
474
479
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
475
 
                                return parts[ former? 0 : 1 ].trim();
 
480
                                ret = parts[ former? 0 : 1 ];
476
481
 
477
 
                        return null;
 
482
                        return ret;
478
483
                }
479
484
 
480
485
                private String extractNameAndParamsFromLine( ContentLine content_line )
481
486
                {
482
 
                        return extractCollonPartFromLine( content_line, true );
 
487
                        return extractCollonPartFromLine( content_line, true ).trim();
483
488
                }
484
489
 
485
490
                private String extractValueFromLine( ContentLine content_line )
503
508
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
504
509
                                {
505
510
                                        // yes, get it!
506
 
                                        String value = extractValueFromLine( content_line );
507
 
                                        if( value == null || (
508
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
509
 
                                        {
 
511
                                        String value = extractValueFromLine( content_line ).trim();
 
512
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
510
513
                                                throw new ParseException( R.string.error_vcf_version );
511
 
                                        }
512
514
                                        _version = value;
513
515
 
514
516
                                        // parse any buffers we've been accumulating while we waited
605
607
                                ) );
606
608
                                boolean is_interesting_field =
607
609
                                        interesting_fields.contains(
608
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
 
610
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
609
611
 
610
612
                                // parse encoding parameter
611
613
                                String encoding = checkParam( name_param_parts, "ENCODING" );
612
614
                                if( encoding != null )
613
 
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
 
615
                                        encoding = encoding.toUpperCase( Locale.US );
614
616
                                if( is_interesting_field && encoding != null &&
615
617
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
616
618
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
622
624
                                // parse charset parameter
623
625
                                String charset = checkParam( name_param_parts, "CHARSET" );
624
626
                                if( charset != null )
625
 
                                        charset = charset.toUpperCase( Locale.ENGLISH );
 
627
                                        charset = charset.toUpperCase( Locale.US );
626
628
                                if( charset != null &&
627
629
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
628
630
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
721
723
                                        parseLABEL( name_param_parts, complete_value );
722
724
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
723
725
                                        parseNOTE( name_param_parts, complete_value );
724
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
725
 
                                        parseBDAY( name_param_parts, complete_value );
726
726
                        }
727
727
                }
728
728
 
752
752
                        {
753
753
                                String str = parts.get( a );
754
754
 
755
 
                                // Look for parts that end in an escape character, but ignore
756
 
                                // 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
757
757
                                // end of the final part in parseLine() and handled multi-lines
758
758
                                // accordingly.
759
759
                                if( a < parts.size() - 1 &&
1029
1029
                        addNote( unescapeValue( value ) );
1030
1030
                }
1031
1031
 
1032
 
                private void parseBDAY( String[] params, String value )
1033
 
                {
1034
 
                        setBirthday( value );
1035
 
                }
1036
 
 
1037
1032
                public void finaliseVcard()
1038
1033
                        throws ParseException, ContactNotIdentifiableException
1039
1034
                {
1047
1042
 
1048
1043
                /**
1049
1044
                 * Amongst the params, find the value of the first, only, of any with
1050
 
                 * the specified name.
1051
 
                 *
 
1045
                 * the specified name
1052
1046
                 * @param params
1053
1047
                 * @param name
1054
1048
                 * @return a value, or null
1060
1054
                }
1061
1055
 
1062
1056
                /**
1063
 
                 * Amongst the params, find the values of any with the specified name.
1064
 
                 *
 
1057
                 * Amongst the params, find the values of any with the specified name
1065
1058
                 * @param params
1066
1059
                 * @param name
1067
1060
                 * @return an array of values, or null
1083
1076
                }
1084
1077
 
1085
1078
                /**
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
 
                 *
 
1079
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1080
                 * those types are just parameters. For v3.0, they are prefixed with
 
1081
                 * "TYPE=". There may also be multiple type parameters.
1090
1082
                 * @param params an array of params to look for types in
1091
1083
                 * @param valid_types an list of upper-case type values to look for
1092
1084
                 * @return a set of present type values
1104
1096
                                // this is in the specs!)
1105
1097
                                String[] parts = type_params[ a ].split( "," );
1106
1098
                                for( int i = 0; i < parts.length; i++ ) {
1107
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
 
1099
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
1108
1100
                                        if( valid_types.contains( ucpart ) )
1109
1101
                                                types.add( ucpart );
1110
1102
                                }
1113
1105
                        // get 2.1-style type param
1114
1106
                        if( _version.equals( "2.1" ) ) {
1115
1107
                                for( int i = 1; i < params.length; i++ ) {
1116
 
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
 
1108
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
1117
1109
                                        if( valid_types.contains( ucparam ) )
1118
1110
                                                types.add( ucparam );
1119
1111
                                }
1144
1136
                                else if( ch == '=' && i == in.limit() - 1 )
1145
1137
                                {
1146
1138
                                        // we found a '=' at the end of a line signifying a multi-
1147
 
                                        // line string, so we don't add it
 
1139
                                        // line string, so we don't add it.
1148
1140
                                        another = true;
1149
1141
                                        continue;
1150
1142
                                }