4
* Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
4
* Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
6
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/export-contacts
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/export-contacts
10
10
* This program is free software: you can redistribute it and/or modify
11
11
* it under the terms of the GNU General Public License as published by
21
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package am.ed.exportcontacts;
24
package org.waxworlds.edam.exportcontacts;
26
26
import java.io.File;
27
27
import java.io.FileNotFoundException;
32
32
import java.util.Iterator;
34
34
import android.content.SharedPreferences;
35
import android.provider.Contacts;
36
37
public class VcardExporter extends Exporter
38
39
protected FileOutputStream _ostream = null;
39
protected boolean _first_contact = true;
41
41
public VcardExporter( Doit doit )
164
164
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 );
203
169
protected boolean exportContact( ContactData contact )
257
217
for( int a = 0; a < numbers.size(); a++ ) {
258
218
ArrayList< String > types = new ArrayList< String >();
259
219
switch( numbers.get( a ).getType() ) {
260
case ContactData.TYPE_HOME:
220
case Contacts.Phones.TYPE_HOME:
261
221
types.add( "VOICE" ); types.add( "HOME" ); break;
262
case ContactData.TYPE_WORK:
222
case Contacts.Phones.TYPE_WORK:
263
223
types.add( "VOICE" ); types.add( "WORK" ); break;
264
case ContactData.TYPE_FAX_HOME:
224
case Contacts.Phones.TYPE_FAX_HOME:
265
225
types.add( "FAX" ); types.add( "HOME" ); break;
266
case ContactData.TYPE_FAX_WORK:
226
case Contacts.Phones.TYPE_FAX_WORK:
267
227
types.add( "FAX" ); types.add( "WORK" ); break;
268
case ContactData.TYPE_PAGER:
228
case Contacts.Phones.TYPE_PAGER:
269
229
types.add( "PAGER" ); break;
270
case ContactData.TYPE_MOBILE:
230
case Contacts.Phones.TYPE_MOBILE:
271
231
types.add( "VOICE" ); types.add( "CELL" ); break;
273
233
if( a == 0 ) types.add( "PREF" );
285
245
ArrayList< String > types = new ArrayList< String >();
286
246
types.add( "INTERNET" );
287
247
switch( emails.get( a ).getType() ) {
288
case ContactData.TYPE_HOME:
248
case Contacts.ContactMethods.TYPE_HOME:
289
249
types.add( "HOME" ); break;
290
case ContactData.TYPE_WORK:
250
case Contacts.ContactMethods.TYPE_WORK:
291
251
types.add( "WORK" ); break;
293
253
out.append( fold( "EMAIL" +
304
264
ArrayList< String > types = new ArrayList< String >();
305
265
types.add( "POSTAL" );
306
266
switch( addresses.get( a ).getType() ) {
307
case ContactData.TYPE_HOME:
267
case Contacts.ContactMethods.TYPE_HOME:
308
268
types.add( "HOME" ); break;
309
case ContactData.TYPE_WORK:
269
case Contacts.ContactMethods.TYPE_WORK:
310
270
types.add( "WORK" ); break;
312
// we use LABEL because is accepts formatted text (whereas ADR
313
// expects semicolon-delimited fields with specific purposes)
314
272
out.append( fold( "LABEL" +
315
273
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
316
274
":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
321
ArrayList< String > notes = contact.getNotes();
323
for( int a = 0; a < notes.size(); a++ )
324
out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
327
String birthday = contact.getBirthday().trim();
328
if( birthday != null ) {
329
if( isValidDateAndOrTime( birthday ) )
330
out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
333
fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
337
279
out.append( "END:VCARD\n" );
339
// replace '\n' with "\r\n" (spec requires CRLF)
342
pos = out.indexOf( "\n", pos );
343
if( pos == -1 ) break;
344
out.replace( pos, pos + 1, "\r\n" );
346
// skip our inserted string
352
283
_ostream.write( out.toString().getBytes() );