/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:58:35 UTC
  • Revision ID: tim@ed.am-20140302105835-szgzmrvr7pa5kljw
updated NEWS

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2013 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;
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
 
133
140
                        boolean in_vcard = false;
134
141
                        while( ( line = reader.readLine() ) != null )
135
142
                        {
136
 
                                if( !in_vcard ) {
 
143
                                if( !in_vcard )
 
144
                                {
137
145
                                        // look for vcard beginning
138
 
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
 
146
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
139
147
                                                in_vcard = true;
140
148
                                                _vcard_count++;
141
149
                                        }
 
150
                                        // check for vMsg files
 
151
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
 
152
                                                showError( getText( R.string.error_vcf_vmsgfile )
 
153
                                                        + file.getName() );
 
154
                                        }
142
155
                                }
143
 
                                else if( line.matches( "^END:VCARD" ) )
 
156
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
144
157
                                        in_vcard = false;
145
158
                        }
 
159
                        reader.close();
146
160
 
147
161
                }
148
162
                catch( FileNotFoundException e ) {
170
184
                        FileInputStream istream = new FileInputStream( file );
171
185
                        byte[] content = new byte[ (int)file.length() ];
172
186
                        istream.read( content );
173
 
                        istream = null;
 
187
                        istream.close();
174
188
 
175
189
                        // import
176
190
                        importVCardFileContent( content, file.getName() );
203
217
 
204
218
                        if( vcard == null ) {
205
219
                                // look for vcard beginning
206
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD" ) ) {
 
220
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
207
221
                                        setProgress( _progress++ );
208
222
                                        vcard = new Vcard();
209
223
                                        vcard_start_line = cli.getLineNumber();
211
225
                        }
212
226
                        else {
213
227
                                // look for vcard content or ending
214
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD" ) )
 
228
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
215
229
                                {
216
230
                                        // finalise the vcard/contact
217
231
                                        try {
267
281
                                                        finish( ACTION_ABORT );
268
282
                                                }
269
283
 
270
 
                                                // although we're continuing, we still need to abort
271
 
                                                // this vCard. Further lines will be ignored until we
 
284
                                                // Although we're continuing, we still need to abort
 
285
                                                // this vCard.  Further lines will be ignored until we
272
286
                                                // get to another BEGIN:VCARD line.
273
287
                                                vcard = null;
274
288
                                        }
275
289
                                        catch( Vcard.SkipImportException e ) {
276
290
                                                skipContact();
277
 
                                                // abort this vCard. Further lines will be ignored until
 
291
                                                // Abort this vCard.  Further lines will be ignored until
278
292
                                                // we get to another BEGIN:VCARD line.
279
293
                                                vcard = null;
280
294
                                        }
465
479
                private String extractCollonPartFromLine( ContentLine content_line,
466
480
                        boolean former )
467
481
                {
468
 
                        String ret = null;
469
 
 
470
482
                        // split line into name and value parts and check to make sure we
471
483
                        // only got 2 parts and that the first part is not zero in length
472
484
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
473
485
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
474
 
                                ret = parts[ former? 0 : 1 ];
 
486
                                return parts[ former? 0 : 1 ].trim();
475
487
 
476
 
                        return ret;
 
488
                        return null;
477
489
                }
478
490
 
479
491
                private String extractNameAndParamsFromLine( ContentLine content_line )
503
515
                                {
504
516
                                        // yes, get it!
505
517
                                        String value = extractValueFromLine( content_line );
506
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
518
                                        if( value == null || (
 
519
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
520
                                        {
507
521
                                                throw new ParseException( R.string.error_vcf_version );
 
522
                                        }
508
523
                                        _version = value;
509
524
 
510
525
                                        // parse any buffers we've been accumulating while we waited
717
732
                                        parseLABEL( name_param_parts, complete_value );
718
733
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
719
734
                                        parseNOTE( name_param_parts, complete_value );
 
735
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
736
                                        parseBDAY( name_param_parts, complete_value );
720
737
                        }
721
738
                }
722
739
 
746
763
                        {
747
764
                                String str = parts.get( a );
748
765
 
749
 
                                // look for parts that end in an escape character, but ignore
750
 
                                // the final part. We've already detected escape chars at the
 
766
                                // Look for parts that end in an escape character, but ignore
 
767
                                // the final part.  We've already detected escape chars at the
751
768
                                // end of the final part in parseLine() and handled multi-lines
752
769
                                // accordingly.
753
770
                                if( a < parts.size() - 1 &&
1023
1040
                        addNote( unescapeValue( value ) );
1024
1041
                }
1025
1042
 
 
1043
                private void parseBDAY( String[] params, String value )
 
1044
                {
 
1045
                        setBirthday( value );
 
1046
                }
 
1047
 
1026
1048
                public void finaliseVcard()
1027
1049
                        throws ParseException, ContactNotIdentifiableException
1028
1050
                {
1036
1058
 
1037
1059
                /**
1038
1060
                 * Amongst the params, find the value of the first, only, of any with
1039
 
                 * the specified name
 
1061
                 * the specified name.
 
1062
                 *
1040
1063
                 * @param params
1041
1064
                 * @param name
1042
1065
                 * @return a value, or null
1048
1071
                }
1049
1072
 
1050
1073
                /**
1051
 
                 * Amongst the params, find the values of any with the specified name
 
1074
                 * Amongst the params, find the values of any with the specified name.
 
1075
                 *
1052
1076
                 * @param params
1053
1077
                 * @param name
1054
1078
                 * @return an array of values, or null
1070
1094
                }
1071
1095
 
1072
1096
                /**
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.
 
1097
                 * Amongst the params, return any type values present.  For v2.1 vCards,
 
1098
                 * those types are just parameters.  For v3.0, they are prefixed with
 
1099
                 * "TYPE=".  There may also be multiple type parameters.
 
1100
                 *
1076
1101
                 * @param params an array of params to look for types in
1077
1102
                 * @param valid_types an list of upper-case type values to look for
1078
1103
                 * @return a set of present type values
1130
1155
                                else if( ch == '=' && i == in.limit() - 1 )
1131
1156
                                {
1132
1157
                                        // we found a '=' at the end of a line signifying a multi-
1133
 
                                        // line string, so we don't add it.
 
1158
                                        // line string, so we don't add it
1134
1159
                                        another = true;
1135
1160
                                        continue;
1136
1161
                                }