/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: edam
  • Date: 2012-12-21 21:05:13 UTC
  • Revision ID: tim@ed.am-20121221210513-scyf50006hqadgjw
updated 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() );
141
133
                        boolean in_vcard = false;
142
134
                        while( ( line = reader.readLine() ) != null )
143
135
                        {
144
 
                                if( !in_vcard )
145
 
                                {
 
136
                                if( !in_vcard ) {
146
137
                                        // look for vcard beginning
147
 
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
 
138
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
148
139
                                                in_vcard = true;
149
140
                                                _vcard_count++;
150
141
                                        }
151
 
                                        // check for vMsg files
152
 
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
153
 
                                                showError( getText( R.string.error_vcf_vmsgfile )
154
 
                                                        + file.getName() );
155
 
                                        }
156
142
                                }
157
 
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
 
143
                                else if( line.matches( "^END:VCARD" ) )
158
144
                                        in_vcard = false;
159
145
                        }
160
 
                        reader.close();
161
146
 
162
147
                }
163
148
                catch( FileNotFoundException e ) {
185
170
                        FileInputStream istream = new FileInputStream( file );
186
171
                        byte[] content = new byte[ (int)file.length() ];
187
172
                        istream.read( content );
188
 
                        istream.close();
 
173
                        istream = null;
189
174
 
190
175
                        // import
191
176
                        importVCardFileContent( content, file.getName() );
218
203
 
219
204
                        if( vcard == null ) {
220
205
                                // look for vcard beginning
221
 
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
 
206
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
222
207
                                        setProgress( _progress++ );
223
208
                                        vcard = new Vcard();
224
209
                                        vcard_start_line = cli.getLineNumber();
226
211
                        }
227
212
                        else {
228
213
                                // look for vcard content or ending
229
 
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
 
214
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
230
215
                                {
231
216
                                        // finalise the vcard/contact
232
217
                                        try {
236
221
                                                importContact( vcard );
237
222
                                        }
238
223
                                        catch( Vcard.ParseException e ) {
239
 
                                                showContinueOrAbort(
 
224
                                                if( !showContinue(
240
225
                                                        getText( R.string.error_vcf_parse ).toString()
241
226
                                                        + fileName +
242
227
                                                        getText( R.string.error_vcf_parse_line ).toString()
243
 
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() );
244
 
                                                skipContact();
 
228
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
229
                                                {
 
230
                                                        finish( ACTION_ABORT );
 
231
                                                }
 
232
                                                else
 
233
                                                        skipContact();
245
234
                                        }
246
235
                                        catch( ContactData.ContactNotIdentifiableException e ) {
247
 
                                                showContinueOrAbort(
 
236
                                                if( !showContinue(
248
237
                                                        getText( R.string.error_vcf_parse ).toString()
249
238
                                                        + fileName +
250
239
                                                        getText( R.string.error_vcf_parse_line ).toString()
251
240
                                                        + vcard_start_line + ":\n" + getText(
252
 
                                                                R.string.error_vcf_notenoughinfo ).toString() );
253
 
                                                skipContact();
254
 
                                        }
255
 
                                        catch( Vcard.SkipImportException e ) {
256
 
                                                skipContact();
 
241
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
242
                                                ) )
 
243
                                                {
 
244
                                                        finish( ACTION_ABORT );
 
245
                                                }
 
246
                                                else
 
247
                                                        skipContact();
257
248
                                        }
258
249
 
259
250
                                        // discard this vcard
267
258
                                        }
268
259
                                        catch( Vcard.ParseException e ) {
269
260
                                                skipContact();
270
 
                                                showContinueOrAbort(
 
261
                                                if( !showContinue(
271
262
                                                        getText( R.string.error_vcf_parse ).toString()
272
263
                                                        + fileName +
273
264
                                                        getText( R.string.error_vcf_parse_line ).toString()
274
 
                                                        + cli.getLineNumber() + "\n" + e.getMessage() );
 
265
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
 
266
                                                {
 
267
                                                        finish( ACTION_ABORT );
 
268
                                                }
275
269
 
276
 
                                                // Although we're continuing, we still need to abort
277
 
                                                // this vCard.  Further lines will be ignored until we
 
270
                                                // although we're continuing, we still need to abort
 
271
                                                // this vCard. Further lines will be ignored until we
278
272
                                                // get to another BEGIN:VCARD line.
279
273
                                                vcard = null;
280
274
                                        }
281
275
                                        catch( Vcard.SkipImportException e ) {
282
276
                                                skipContact();
283
 
                                                // Abort this vCard.  Further lines will be ignored until
 
277
                                                // abort this vCard. Further lines will be ignored until
284
278
                                                // we get to another BEGIN:VCARD line.
285
279
                                                vcard = null;
286
280
                                        }
471
465
                private String extractCollonPartFromLine( ContentLine content_line,
472
466
                        boolean former )
473
467
                {
 
468
                        String ret = null;
 
469
 
474
470
                        // split line into name and value parts and check to make sure we
475
471
                        // only got 2 parts and that the first part is not zero in length
476
472
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
477
473
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
478
 
                                return parts[ former? 0 : 1 ].trim();
 
474
                                ret = parts[ former? 0 : 1 ];
479
475
 
480
 
                        return null;
 
476
                        return ret;
481
477
                }
482
478
 
483
479
                private String extractNameAndParamsFromLine( ContentLine content_line )
507
503
                                {
508
504
                                        // yes, get it!
509
505
                                        String value = extractValueFromLine( content_line );
510
 
                                        if( value == null || (
511
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
512
 
                                        {
 
506
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
513
507
                                                throw new ParseException( R.string.error_vcf_version );
514
 
                                        }
515
508
                                        _version = value;
516
509
 
517
510
                                        // parse any buffers we've been accumulating while we waited
608
601
                                ) );
609
602
                                boolean is_interesting_field =
610
603
                                        interesting_fields.contains(
611
 
                                                name_param_parts[ 0 ].toUpperCase( Locale.ENGLISH ) );
 
604
                                                name_param_parts[ 0 ].toUpperCase( Locale.US ) );
612
605
 
613
606
                                // parse encoding parameter
614
607
                                String encoding = checkParam( name_param_parts, "ENCODING" );
615
608
                                if( encoding != null )
616
 
                                        encoding = encoding.toUpperCase( Locale.ENGLISH );
 
609
                                        encoding = encoding.toUpperCase( Locale.US );
617
610
                                if( is_interesting_field && encoding != null &&
618
611
                                        !encoding.equalsIgnoreCase( "8BIT" ) &&
619
612
                                        !encoding.equalsIgnoreCase( "QUOTED-PRINTABLE" ) )
625
618
                                // parse charset parameter
626
619
                                String charset = checkParam( name_param_parts, "CHARSET" );
627
620
                                if( charset != null )
628
 
                                        charset = charset.toUpperCase( Locale.ENGLISH );
 
621
                                        charset = charset.toUpperCase( Locale.US );
629
622
                                if( charset != null &&
630
623
                                        !charset.equalsIgnoreCase( "US-ASCII" ) &&
631
624
                                        !charset.equalsIgnoreCase( "ASCII" ) &&
724
717
                                        parseLABEL( name_param_parts, complete_value );
725
718
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
726
719
                                        parseNOTE( name_param_parts, complete_value );
727
 
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
728
 
                                        parseBDAY( name_param_parts, complete_value );
729
720
                        }
730
721
                }
731
722
 
755
746
                        {
756
747
                                String str = parts.get( a );
757
748
 
758
 
                                // Look for parts that end in an escape character, but ignore
759
 
                                // the final part.  We've already detected escape chars at the
 
749
                                // look for parts that end in an escape character, but ignore
 
750
                                // the final part. We've already detected escape chars at the
760
751
                                // end of the final part in parseLine() and handled multi-lines
761
752
                                // accordingly.
762
753
                                if( a < parts.size() - 1 &&
1032
1023
                        addNote( unescapeValue( value ) );
1033
1024
                }
1034
1025
 
1035
 
                private void parseBDAY( String[] params, String value )
1036
 
                {
1037
 
                        setBirthday( value );
1038
 
                }
1039
 
 
1040
1026
                public void finaliseVcard()
1041
 
                        throws ParseException, ContactNotIdentifiableException,
1042
 
                                SkipImportException, AbortImportException
 
1027
                        throws ParseException, ContactNotIdentifiableException
1043
1028
                {
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
 
                        }
 
1029
                        // missing version (and data is present)
 
1030
                        if( _version == null && _content_lines != null )
 
1031
                                throw new ParseException( R.string.error_vcf_malformed );
1052
1032
 
1053
1033
                        // finalise the parent class
1054
1034
                        finalise();
1056
1036
 
1057
1037
                /**
1058
1038
                 * Amongst the params, find the value of the first, only, of any with
1059
 
                 * the specified name.
1060
 
                 *
 
1039
                 * the specified name
1061
1040
                 * @param params
1062
1041
                 * @param name
1063
1042
                 * @return a value, or null
1069
1048
                }
1070
1049
 
1071
1050
                /**
1072
 
                 * Amongst the params, find the values of any with the specified name.
1073
 
                 *
 
1051
                 * Amongst the params, find the values of any with the specified name
1074
1052
                 * @param params
1075
1053
                 * @param name
1076
1054
                 * @return an array of values, or null
1092
1070
                }
1093
1071
 
1094
1072
                /**
1095
 
                 * Amongst the params, return any type values present.  For v2.1 vCards,
1096
 
                 * those types are just parameters.  For v3.0, they are prefixed with
1097
 
                 * "TYPE=".  There may also be multiple type parameters.
1098
 
                 *
 
1073
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1074
                 * those types are just parameters. For v3.0, they are prefixed with
 
1075
                 * "TYPE=". There may also be multiple type parameters.
1099
1076
                 * @param params an array of params to look for types in
1100
1077
                 * @param valid_types an list of upper-case type values to look for
1101
1078
                 * @return a set of present type values
1113
1090
                                // this is in the specs!)
1114
1091
                                String[] parts = type_params[ a ].split( "," );
1115
1092
                                for( int i = 0; i < parts.length; i++ ) {
1116
 
                                        String ucpart = parts[ i ].toUpperCase( Locale.ENGLISH );
 
1093
                                        String ucpart = parts[ i ].toUpperCase( Locale.US );
1117
1094
                                        if( valid_types.contains( ucpart ) )
1118
1095
                                                types.add( ucpart );
1119
1096
                                }
1122
1099
                        // get 2.1-style type param
1123
1100
                        if( _version.equals( "2.1" ) ) {
1124
1101
                                for( int i = 1; i < params.length; i++ ) {
1125
 
                                        String ucparam = params[ i ].toUpperCase( Locale.ENGLISH );
 
1102
                                        String ucparam = params[ i ].toUpperCase( Locale.US );
1126
1103
                                        if( valid_types.contains( ucparam ) )
1127
1104
                                                types.add( ucparam );
1128
1105
                                }
1153
1130
                                else if( ch == '=' && i == in.limit() - 1 )
1154
1131
                                {
1155
1132
                                        // we found a '=' at the end of a line signifying a multi-
1156
 
                                        // line string, so we don't add it
 
1133
                                        // line string, so we don't add it.
1157
1134
                                        another = true;
1158
1135
                                        continue;
1159
1136
                                }