/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/VcardExporter.java

  • Committer: Tim Marston
  • Date: 2014-03-02 11:00:38 UTC
  • Revision ID: tim@ed.am-20140302110038-2ojg0b3pfi8vqasq
add permission to read from sdcard (how did this ever work!?)

Show diffs side-by-side

added added

removed removed

49
49
                SharedPreferences prefs = getSharedPreferences();
50
50
 
51
51
                // create output filename
52
 
                File file = new File( ConfigureVCF.getSdCardPathPrefix() +
53
 
                        prefs.getString( "path", "/" ) +
54
 
                        prefs.getString( "filename", "android-contacts.vcf" ) );
 
52
                String filename = prefs.getString( "filename", "android-contacts.vcf" );
 
53
                File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
 
54
                        filename );
55
55
 
56
56
                // check if the output file already exists
57
57
                if( file.exists() && file.length() > 0 )
58
 
                        showContinueOrAbort( R.string.error_vcf_exists );
 
58
                        if( !showContinue( R.string.error_vcf_exists ) )
 
59
                                finish( ACTION_ABORT );
59
60
 
60
61
                // open file
61
62
                try {
62
63
                        _ostream = new FileOutputStream( file );
63
64
                }
64
65
                catch( FileNotFoundException e ) {
65
 
                        showError( getText( R.string.error_filenotfound ) +
66
 
                                file.getPath() );
 
66
                        showError( R.string.error_filenotfound );
67
67
                }
68
68
        }
69
69
 
199
199
                return value.matches( date_and_or_time );
200
200
        }
201
201
 
202
 
        protected void writeToFile( byte data[], String identifier )
203
 
                throws AbortExportException
204
 
        {
205
 
                // write to file
206
 
                try {
207
 
                        _ostream.write( data );
208
 
                        _ostream.flush();
209
 
                }
210
 
                catch( IOException e ) {
211
 
                        showError( R.string.error_ioerror );
212
 
                }
213
 
        }
214
 
 
215
202
        @Override
216
203
        protected boolean exportContact( ContactData contact )
217
204
                throws AbortExportException
233
220
                out.append( "VERSION:3.0\n" );
234
221
 
235
222
                // append formatted name
236
 
                String identifier = contact.getPrimaryIdentifier();
237
 
                if( identifier != null ) identifier = identifier.trim();
238
 
                if( identifier == null || identifier.length() == 0 ) {
239
 
                        showContinueOrAbort( R.string.error_vcf_noname );
240
 
                        return false;
241
 
                }
242
 
                out.append( fold( "FN:" + escape( identifier ) ) + "\n" );
243
 
 
244
 
                // append name
245
223
                String name = contact.getName();
246
224
                if( name == null ) name = "";
 
225
                out.append( fold( "FN:" + escape( name ) ) + "\n" );
 
226
 
 
227
                // append name
247
228
                String[] bits = name.split( " +" );
248
229
                StringBuilder tmp = new StringBuilder();
249
230
                for( int a = 1; a < bits.length - 1; a++ ) {
368
349
                }
369
350
 
370
351
                // write to file
371
 
                writeToFile( out.toString().getBytes(), identifier );
 
352
                try {
 
353
                        _ostream.write( out.toString().getBytes() );
 
354
                        _ostream.flush();
 
355
                }
 
356
                catch( IOException e ) {
 
357
                        showError( R.string.error_ioerror );
 
358
                }
372
359
 
373
360
                return true;
374
361
        }