/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-01 17:33:48 UTC
  • Revision ID: tim@ed.am-20140301173348-967vav70j5zm4nhc
remove a hard-coded path to external storage in favour of obtaining it from
Android

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
7
 * to as "this program").  For more information, see
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
 
149
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() );
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
 
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
                {