/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
 
136
143
                                if( !in_vcard )
137
144
                                {
138
145
                                        // look for vcard beginning
139
 
                                        if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
146
                                        if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
140
147
                                                in_vcard = true;
141
148
                                                _vcard_count++;
142
149
                                        }
143
150
                                        // check for vMsg files
144
 
                                        else if( line.matches( "^BEGIN[ \t]*:[ \t]*VMSG.*$" ) ) {
 
151
                                        else if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VMSG.*" ) ) {
145
152
                                                showError( getText( R.string.error_vcf_vmsgfile )
146
153
                                                        + file.getName() );
147
154
                                        }
148
155
                                }
149
 
                                else if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
156
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
150
157
                                        in_vcard = false;
151
158
                        }
 
159
                        reader.close();
152
160
 
153
161
                }
154
162
                catch( FileNotFoundException e ) {
176
184
                        FileInputStream istream = new FileInputStream( file );
177
185
                        byte[] content = new byte[ (int)file.length() ];
178
186
                        istream.read( content );
179
 
                        istream = null;
 
187
                        istream.close();
180
188
 
181
189
                        // import
182
190
                        importVCardFileContent( content, file.getName() );
209
217
 
210
218
                        if( vcard == null ) {
211
219
                                // look for vcard beginning
212
 
                                if( line.matches( "^BEGIN[ \t]*:[ \t]*VCARD.*$" ) ) {
 
220
                                if( line.matches( "(?i)BEGIN[ \t]*:[ \t]*VCARD.*" ) ) {
213
221
                                        setProgress( _progress++ );
214
222
                                        vcard = new Vcard();
215
223
                                        vcard_start_line = cli.getLineNumber();
217
225
                        }
218
226
                        else {
219
227
                                // look for vcard content or ending
220
 
                                if( line.matches( "^END[ \t]*:[ \t]*VCARD.*$" ) )
 
228
                                if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
221
229
                                {
222
230
                                        // finalise the vcard/contact
223
231
                                        try {
273
281
                                                        finish( ACTION_ABORT );
274
282
                                                }
275
283
 
276
 
                                                // although we're continuing, we still need to abort
277
 
                                                // 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
278
286
                                                // get to another BEGIN:VCARD line.
279
287
                                                vcard = null;
280
288
                                        }
281
289
                                        catch( Vcard.SkipImportException e ) {
282
290
                                                skipContact();
283
 
                                                // abort this vCard. Further lines will be ignored until
 
291
                                                // Abort this vCard.  Further lines will be ignored until
284
292
                                                // we get to another BEGIN:VCARD line.
285
293
                                                vcard = null;
286
294
                                        }
471
479
                private String extractCollonPartFromLine( ContentLine content_line,
472
480
                        boolean former )
473
481
                {
474
 
                        String ret = null;
475
 
 
476
482
                        // split line into name and value parts and check to make sure we
477
483
                        // only got 2 parts and that the first part is not zero in length
478
484
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
479
485
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
480
 
                                ret = parts[ former? 0 : 1 ];
 
486
                                return parts[ former? 0 : 1 ].trim();
481
487
 
482
 
                        return ret;
 
488
                        return null;
483
489
                }
484
490
 
485
491
                private String extractNameAndParamsFromLine( ContentLine content_line )
486
492
                {
487
 
                        return extractCollonPartFromLine( content_line, true ).trim();
 
493
                        return extractCollonPartFromLine( content_line, true );
488
494
                }
489
495
 
490
496
                private String extractValueFromLine( ContentLine content_line )
508
514
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
509
515
                                {
510
516
                                        // yes, get it!
511
 
                                        String value = extractValueFromLine( content_line ).trim();
512
 
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
 
517
                                        String value = extractValueFromLine( content_line );
 
518
                                        if( value == null || (
 
519
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
 
520
                                        {
513
521
                                                throw new ParseException( R.string.error_vcf_version );
 
522
                                        }
514
523
                                        _version = value;
515
524
 
516
525
                                        // parse any buffers we've been accumulating while we waited
723
732
                                        parseLABEL( name_param_parts, complete_value );
724
733
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "NOTE" ) )
725
734
                                        parseNOTE( name_param_parts, complete_value );
 
735
                                else if( name_param_parts[ 0 ].equalsIgnoreCase( "BDAY" ) )
 
736
                                        parseBDAY( name_param_parts, complete_value );
726
737
                        }
727
738
                }
728
739
 
752
763
                        {
753
764
                                String str = parts.get( a );
754
765
 
755
 
                                // look for parts that end in an escape character, but ignore
756
 
                                // 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
757
768
                                // end of the final part in parseLine() and handled multi-lines
758
769
                                // accordingly.
759
770
                                if( a < parts.size() - 1 &&
1029
1040
                        addNote( unescapeValue( value ) );
1030
1041
                }
1031
1042
 
 
1043
                private void parseBDAY( String[] params, String value )
 
1044
                {
 
1045
                        setBirthday( value );
 
1046
                }
 
1047
 
1032
1048
                public void finaliseVcard()
1033
1049
                        throws ParseException, ContactNotIdentifiableException
1034
1050
                {
1042
1058
 
1043
1059
                /**
1044
1060
                 * Amongst the params, find the value of the first, only, of any with
1045
 
                 * the specified name
 
1061
                 * the specified name.
 
1062
                 *
1046
1063
                 * @param params
1047
1064
                 * @param name
1048
1065
                 * @return a value, or null
1054
1071
                }
1055
1072
 
1056
1073
                /**
1057
 
                 * 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
                 *
1058
1076
                 * @param params
1059
1077
                 * @param name
1060
1078
                 * @return an array of values, or null
1076
1094
                }
1077
1095
 
1078
1096
                /**
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.
 
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
                 *
1082
1101
                 * @param params an array of params to look for types in
1083
1102
                 * @param valid_types an list of upper-case type values to look for
1084
1103
                 * @return a set of present type values
1136
1155
                                else if( ch == '=' && i == in.limit() - 1 )
1137
1156
                                {
1138
1157
                                        // we found a '=' at the end of a line signifying a multi-
1139
 
                                        // line string, so we don't add it.
 
1158
                                        // line string, so we don't add it
1140
1159
                                        another = true;
1141
1160
                                        continue;
1142
1161
                                }