4
* Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
4
* Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
6
6
* This file is part of the Export 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/export-contacts
10
10
* This program is free software: you can redistribute it and/or modify
49
48
SharedPreferences prefs = getSharedPreferences();
51
50
// create output filename
52
File file = new File( ConfigureVCF.getSdCardPathPrefix() +
53
prefs.getString( "path", "/" ) +
54
prefs.getString( "filename", "android-contacts.vcf" ) );
51
String filename = prefs.getString( "filename", "android-contacts.vcf" );
52
File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
56
55
// check if the output file already exists
57
56
if( file.exists() && file.length() > 0 )
58
showContinueOrAbort( R.string.error_vcf_exists );
57
if( !showContinue( R.string.error_vcf_exists ) )
58
finish( ACTION_ABORT );
62
62
_ostream = new FileOutputStream( file );
64
64
catch( FileNotFoundException e ) {
65
showError( getText( R.string.error_filenotfound ) +
65
showError( R.string.error_filenotfound );
164
163
return buffer.toString();
168
* Is the provided value a valid date-and-or-time, as per the spec?
170
* @param value the value
171
* @return true if it is
173
protected boolean isValidDateAndOrTime( String value )
175
// ISO 8601:2004 4.1.2 date with 4.1.2.3 a) and b) reduced accuracy
177
"[0-9]{4}(?:-?[0-9]{2}(?:-?[0-9]{2})?)?";
179
// ISO 8601:2000 5.2.1.3 d), e) and f) truncated date representation
181
"--(?:[0-9]{2}(?:-?[0-9]{2})?|-[0-9]{2})";
183
// ISO 8601:2004 4.2.2 time with 4.2.2.3 reduced accuracy, 4.2.4 UTC and
184
// 4.2.5 zone offset, no 4.2.2.4 decimal fraction and no 4.2.3 24:00
187
"(?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9](?::?(?:60|[0-5][0-9]))?)?" +
188
"(?:Z|[-+](?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9])?)?";
190
// ISO 8601:2000 5.3.1.4 a), b) and c) truncated time representation
192
"-(?:[0-5][0-9](?::?(?:60|[0-5][0-9]))?|-(?:60|[0-5][0-9]))";
194
// RFC6350 (vCard 3.0) date-and-or-time with mandatory time designator
195
String date_and_or_time =
196
"(?:" + date + "|" + date_trunc + ")?" +
197
"(?:T(?:" + time + "|" + time_trunc + "))?";
199
return value.matches( date_and_or_time );
202
protected void writeToFile( byte data[], String identifier )
203
throws AbortExportException
207
_ostream.write( data );
210
catch( IOException e ) {
211
showError( R.string.error_ioerror );
216
168
protected boolean exportContact( ContactData contact )
222
174
if( contact.getPrimaryIdentifier() == null )
227
_first_contact = false;
232
178
out.append( "BEGIN:VCARD\n" );
233
179
out.append( "VERSION:3.0\n" );
235
181
// 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 );
242
out.append( fold( "FN:" + escape( identifier ) ) + "\n" );
245
182
String name = contact.getName();
246
183
if( name == null ) name = "";
184
out.append( fold( "FN:" + escape( name ) ) + "\n" );
247
187
String[] bits = name.split( " +" );
248
188
StringBuilder tmp = new StringBuilder();
249
189
for( int a = 1; a < bits.length - 1; a++ ) {
342
282
for( int a = 0; a < notes.size(); a++ )
343
283
out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
346
String birthday = contact.getBirthday();
347
if( birthday != null ) {
349
if( isValidDateAndOrTime( birthday ) )
350
out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
353
fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
357
286
out.append( "END:VCARD\n" );