/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: 2013-07-19 15:56:41 UTC
  • Revision ID: tim@ed.am-20130719155641-h75c8i80tzbpyn9o
added new family pic!

Show diffs side-by-side

added added

removed removed

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
 
156
149
                                else if( line.matches( "(?i)END[ \t]*:[ \t]*VCARD.*" ) )
157
150
                                        in_vcard = false;
158
151
                        }
159
 
                        reader.close();
160
152
 
161
153
                }
162
154
                catch( FileNotFoundException e ) {
184
176
                        FileInputStream istream = new FileInputStream( file );
185
177
                        byte[] content = new byte[ (int)file.length() ];
186
178
                        istream.read( content );
187
 
                        istream.close();
 
179
                        istream = null;
188
180
 
189
181
                        // import
190
182
                        importVCardFileContent( content, file.getName() );
479
471
                private String extractCollonPartFromLine( ContentLine content_line,
480
472
                        boolean former )
481
473
                {
 
474
                        String ret = null;
 
475
 
482
476
                        // split line into name and value parts and check to make sure we
483
477
                        // only got 2 parts and that the first part is not zero in length
484
478
                        String[] parts = content_line.getUsAsciiLine().split( ":", 2 );
485
479
                        if( parts.length == 2 && parts[ 0 ].length() > 0 )
486
 
                                return parts[ former? 0 : 1 ].trim();
 
480
                                ret = parts[ former? 0 : 1 ];
487
481
 
488
 
                        return null;
 
482
                        return ret;
489
483
                }
490
484
 
491
485
                private String extractNameAndParamsFromLine( ContentLine content_line )
492
486
                {
493
 
                        return extractCollonPartFromLine( content_line, true );
 
487
                        return extractCollonPartFromLine( content_line, true ).trim();
494
488
                }
495
489
 
496
490
                private String extractValueFromLine( ContentLine content_line )
514
508
                                        name_and_params.equalsIgnoreCase( "VERSION" ) )
515
509
                                {
516
510
                                        // yes, get it!
517
 
                                        String value = extractValueFromLine( content_line );
518
 
                                        if( value == null || (
519
 
                                                !value.equals( "2.1" ) && !value.equals( "3.0" ) ) )
520
 
                                        {
 
511
                                        String value = extractValueFromLine( content_line ).trim();
 
512
                                        if( !value.equals( "2.1" ) && !value.equals( "3.0" ) )
521
513
                                                throw new ParseException( R.string.error_vcf_version );
522
 
                                        }
523
514
                                        _version = value;
524
515
 
525
516
                                        // parse any buffers we've been accumulating while we waited