/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 14:51:26 UTC
  • Revision ID: tim@ed.am-20121221145126-l30l0yii8xkqs1m2
added a new class to VcardImporter, a ContentLine, which represents the data returned by the ContentLineIterator (including whether the next line looks folded, and the ability to generate/cache a US-ASCII String of the line)

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