4
* Copyright (C) 2011 to 2012 Tim Marston <tim@ed.am>
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
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package am.ed.exportcontacts;
27
import java.io.FileNotFoundException;
28
import java.io.FileOutputStream;
29
import java.io.IOException;
30
import java.util.AbstractCollection;
31
import java.util.ArrayList;
32
import java.util.Iterator;
34
import android.content.SharedPreferences;
36
public class VcardExporter extends Exporter
38
protected FileOutputStream _ostream = null;
40
public VcardExporter( Doit doit )
46
protected void preExport() throws AbortExportException
48
SharedPreferences prefs = getSharedPreferences();
50
// create output filename
51
String filename = prefs.getString( "filename", "android-contacts.vcf" );
52
File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
55
// check if the output file already exists
56
if( file.exists() && file.length() > 0 )
57
if( !showContinue( R.string.error_vcf_exists ) )
58
finish( ACTION_ABORT );
62
_ostream = new FileOutputStream( file );
64
catch( FileNotFoundException e ) {
65
showError( R.string.error_filenotfound );
70
* Do line folding at 75 chars
72
* @return folded string
74
private String fold( String line )
76
StringBuilder ret = new StringBuilder( line.length() );
78
// keep pulling off the first line's worth of chars, while the string is
79
// still longer than a line should be
80
while( line.length() > 75 )
82
// length of the line we'll be pulling off
85
// if splitting at this length would break apart a codepoint, use
87
if( Character.isHighSurrogate( line.charAt( len - 1 ) ) )
90
// count how many backslashes would be at the end of the line we're
93
for( int a = len - 1; a >= 0; a-- )
94
if( line.charAt( a ) == '\\' )
99
// if there would be an odd number of slashes at the end of the line
100
// then pull off one fewer characters so that we don't break apart
105
// pull off the line and add it to the output, folded
106
ret.append( line.substring( 0, len ) + "\n " );
107
line = line.substring( len );
110
// add any remaining data
113
return ret.toString();
117
* Do unsafe character escaping
119
* @return escaped string
121
private String escape( String str )
123
StringBuilder ret = new StringBuilder( str.length() );
124
for( int a = 0; a < str.length(); a++ )
126
int c = str.codePointAt( a );
130
// append escaped newline
136
// append return character
141
ret.append( Character.toChars( c ) );
145
return ret.toString();
151
@SuppressWarnings( "rawtypes" )
152
public static String join( AbstractCollection s, String delimiter)
154
StringBuffer buffer = new StringBuffer();
155
Iterator iter = s.iterator();
156
if( iter.hasNext() ) {
157
buffer.append( iter.next() );
158
while( iter.hasNext() ) {
159
buffer.append( delimiter );
160
buffer.append( iter.next() );
163
return buffer.toString();
168
protected boolean exportContact( ContactData contact )
169
throws AbortExportException
171
StringBuilder out = new StringBuilder();
173
// skip if the contact has no identifiable features
174
if( contact.getPrimaryIdentifier() == null )
178
out.append( "BEGIN:VCARD\n" );
179
out.append( "VERSION:3.0\n" );
181
// append formatted name
182
String name = contact.getName();
183
if( name == null ) name = "";
184
out.append( fold( "FN:" + escape( name ) ) + "\n" );
187
String[] bits = name.split( " +" );
188
StringBuilder tmp = new StringBuilder();
189
for( int a = 1; a < bits.length - 1; a++ ) {
190
if( a > 1 ) tmp.append( " " );
191
tmp.append( escape( bits[ a ] ) );
193
String value = escape( bits[ bits.length - 1 ] ) + ";" +
194
( bits.length > 1? escape( bits[ 0 ] ) : "" ) + ";" +
195
tmp.toString() + ";;";
196
out.append( fold( "N:" + value ) + "\n" );
198
// append organisations and titles
199
ArrayList< Exporter.ContactData.OrganisationDetail > organisations =
200
contact.getOrganisations();
201
if( organisations != null ) {
202
for( int a = 0; a < organisations.size(); a++ ) {
203
if( organisations.get( a ).getOrganisation() != null )
204
out.append( fold( "ORG:" + escape(
205
organisations.get( a ).getOrganisation() ) ) + "\n" );
206
if( organisations.get( a ).getTitle() != null )
207
out.append( fold( "TITLE:" + escape(
208
organisations.get( a ).getTitle() ) ) + "\n" );
212
// append phone numbers
213
ArrayList< Exporter.ContactData.NumberDetail > numbers =
214
contact.getNumbers();
215
if( numbers != null ) {
216
for( int a = 0; a < numbers.size(); a++ ) {
217
ArrayList< String > types = new ArrayList< String >();
218
switch( numbers.get( a ).getType() ) {
219
case ContactData.TYPE_HOME:
220
types.add( "VOICE" ); types.add( "HOME" ); break;
221
case ContactData.TYPE_WORK:
222
types.add( "VOICE" ); types.add( "WORK" ); break;
223
case ContactData.TYPE_FAX_HOME:
224
types.add( "FAX" ); types.add( "HOME" ); break;
225
case ContactData.TYPE_FAX_WORK:
226
types.add( "FAX" ); types.add( "WORK" ); break;
227
case ContactData.TYPE_PAGER:
228
types.add( "PAGER" ); break;
229
case ContactData.TYPE_MOBILE:
230
types.add( "VOICE" ); types.add( "CELL" ); break;
232
if( a == 0 ) types.add( "PREF" );
233
out.append( fold( "TEL" +
234
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
235
":" + escape( numbers.get( a ).getNumber() ) ) + "\n" );
239
// append email addresses
240
ArrayList< Exporter.ContactData.EmailDetail > emails =
242
if( emails != null ) {
243
for( int a = 0; a < emails.size(); a++ ) {
244
ArrayList< String > types = new ArrayList< String >();
245
types.add( "INTERNET" );
246
switch( emails.get( a ).getType() ) {
247
case ContactData.TYPE_HOME:
248
types.add( "HOME" ); break;
249
case ContactData.TYPE_WORK:
250
types.add( "WORK" ); break;
252
out.append( fold( "EMAIL" +
253
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
254
":" + escape( emails.get( a ).getEmail() ) ) + "\n" );
259
ArrayList< Exporter.ContactData.AddressDetail > addresses =
260
contact.getAddresses();
261
if( addresses != null ) {
262
for( int a = 0; a < addresses.size(); a++ ) {
263
ArrayList< String > types = new ArrayList< String >();
264
types.add( "POSTAL" );
265
switch( addresses.get( a ).getType() ) {
266
case ContactData.TYPE_HOME:
267
types.add( "HOME" ); break;
268
case ContactData.TYPE_WORK:
269
types.add( "WORK" ); break;
271
// we use LABEL because is accepts formatted text (whereas ADR
272
// expects semicolon-delimited fields with specific purposes)
273
out.append( fold( "LABEL" +
274
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
275
":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
280
ArrayList< String > notes = contact.getNotes();
282
for( int a = 0; a < notes.size(); a++ )
283
out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
286
out.append( "END:VCARD\n" );
288
// replace '\n' with "\r\n" (spec requires CRLF)
291
pos = out.indexOf( "\n", pos );
292
if( pos == -1 ) break;
293
out.replace( pos, pos + 1, "\r\n" );
295
// skip our inserted string
301
_ostream.write( out.toString().getBytes() );
304
catch( IOException e ) {
305
showError( R.string.error_ioerror );