/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:03:07 UTC
  • Revision ID: tim@ed.am-20140302110307-nnrn9dhfnjg06lve
removed some unused code, fixed locale warnings and made showContinueOrAbort()
abort implicitly (rather than return false); added check for unidentifiable
contacts; fixed spelling mistake

Show diffs side-by-side

added added

removed removed

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