4
* Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/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 org.waxworlds.edam.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;
35
import android.provider.Contacts;
37
public class VcardExporter extends Exporter
39
protected FileOutputStream _ostream = null;
41
public VcardExporter( Doit doit )
47
protected void preExport() throws AbortExportException
49
SharedPreferences prefs = getSharedPreferences();
51
// create output filename
52
String filename = prefs.getString( "filename", "android-contacts.vcf" );
53
File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
56
// check if the output file already exists
57
if( file.exists() && file.length() > 0 )
58
if( !showContinue( R.string.error_vcf_exists ) )
59
finish( ACTION_ABORT );
63
_ostream = new FileOutputStream( file );
65
catch( FileNotFoundException e ) {
66
showError( R.string.error_filenotfound );
71
* Do line folding at 75 chars
73
* @return folded string
75
private String fold( String line )
77
StringBuilder ret = new StringBuilder( line.length() );
79
// keep pulling off the first line's worth of chars, while the string is
80
// still longer than a line should be
81
while( line.length() > 75 )
83
// length of the line we'll be pulling off
86
// if splitting at this length would break apart a codepoint, use
88
if( Character.isHighSurrogate( line.charAt( len - 1 ) ) )
91
// count how many backslashes would be at the end of the line we're
94
for( int a = len - 1; a >= 0; a-- )
95
if( line.charAt( a ) == '\\' )
100
// if there would be an odd number of slashes at the end of the line
101
// then pull off one fewer characters so that we don't break apart
106
// pull off the line and add it to the output, folded
107
ret.append( line.substring( 0, len ) + "\n " );
108
line = line.substring( len );
111
// add any remaining data
114
return ret.toString();
118
* Do unsafe character escaping
120
* @return escaped string
122
private String escape( String str )
124
StringBuilder ret = new StringBuilder( str.length() );
125
for( int a = 0; a < str.length(); a++ )
127
int c = str.codePointAt( a );
131
// append escaped newline
137
// append return character
142
ret.append( Character.toChars( c ) );
146
return ret.toString();
152
@SuppressWarnings( "rawtypes" )
153
public static String join( AbstractCollection s, String delimiter)
155
StringBuffer buffer = new StringBuffer();
156
Iterator iter = s.iterator();
157
if( iter.hasNext() ) {
158
buffer.append( iter.next() );
159
while( iter.hasNext() ) {
160
buffer.append( delimiter );
161
buffer.append( iter.next() );
164
return buffer.toString();
169
protected boolean exportContact( ContactData contact )
170
throws AbortExportException
172
StringBuilder out = new StringBuilder();
174
// skip if the contact has no identifiable features
175
if( contact.getPrimaryIdentifier() == null )
179
out.append( "VCARD:BEGIN\n" );
180
out.append( "VERSION:3.0\n" );
182
// append formatted name
183
String name = contact.getName();
184
if( name == null ) name = "";
185
out.append( fold( "FN:" + escape( name ) ) + "\n" );
188
String[] bits = name.split( " +" );
189
StringBuilder tmp = new StringBuilder();
190
for( int a = 1; a < bits.length - 1; a++ ) {
191
if( a > 1 ) tmp.append( " " );
192
tmp.append( escape( bits[ a ] ) );
194
String value = escape( bits[ bits.length - 1 ] ) + ";" +
195
( bits.length > 1? escape( bits[ 0 ] ) : "" ) + ";" +
196
tmp.toString() + ";;";
197
out.append( fold( "N:" + value ) + "\n" );
199
// append organisations and titles
200
ArrayList< Exporter.ContactData.OrganisationDetail > organisations =
201
contact.getOrganisations();
202
if( organisations != null ) {
203
for( int a = 0; a < organisations.size(); a++ ) {
204
if( organisations.get( a ).getOrganisation() != null )
205
out.append( fold( "ORG:" + escape(
206
organisations.get( a ).getOrganisation() ) ) + "\n" );
207
if( organisations.get( a ).getTitle() != null )
208
out.append( fold( "TITLE:" + escape(
209
organisations.get( a ).getTitle() ) ) + "\n" );
213
// append phone numbers
214
ArrayList< Exporter.ContactData.NumberDetail > numbers =
215
contact.getNumbers();
216
if( numbers != null ) {
217
for( int a = 0; a < numbers.size(); a++ ) {
218
ArrayList< String > types = new ArrayList< String >();
219
switch( numbers.get( a ).getType() ) {
220
case Contacts.Phones.TYPE_HOME:
221
types.add( "VOICE" ); types.add( "HOME" ); break;
222
case Contacts.Phones.TYPE_WORK:
223
types.add( "VOICE" ); types.add( "WORK" ); break;
224
case Contacts.Phones.TYPE_FAX_HOME:
225
types.add( "FAX" ); types.add( "HOME" ); break;
226
case Contacts.Phones.TYPE_FAX_WORK:
227
types.add( "FAX" ); types.add( "WORK" ); break;
228
case Contacts.Phones.TYPE_PAGER:
229
types.add( "PAGER" ); break;
230
case Contacts.Phones.TYPE_MOBILE:
231
types.add( "VOICE" ); types.add( "CELL" ); break;
233
if( a == 0 ) types.add( "PREF" );
234
out.append( fold( "TEL" +
235
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
236
":" + escape( numbers.get( a ).getNumber() ) ) + "\n" );
240
// append email addresses
241
ArrayList< Exporter.ContactData.EmailDetail > emails =
243
if( emails != null ) {
244
for( int a = 0; a < emails.size(); a++ ) {
245
ArrayList< String > types = new ArrayList< String >();
246
types.add( "INTERNET" );
247
switch( emails.get( a ).getType() ) {
248
case Contacts.ContactMethods.TYPE_HOME:
249
types.add( "HOME" ); break;
250
case Contacts.ContactMethods.TYPE_WORK:
251
types.add( "WORK" ); break;
253
out.append( fold( "EMAIL" +
254
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
255
":" + escape( emails.get( a ).getEmail() ) ) + "\n" );
260
ArrayList< Exporter.ContactData.AddressDetail > addresses =
261
contact.getAddresses();
262
if( addresses != null ) {
263
for( int a = 0; a < addresses.size(); a++ ) {
264
ArrayList< String > types = new ArrayList< String >();
265
types.add( "POSTAL" );
266
switch( addresses.get( a ).getType() ) {
267
case Contacts.ContactMethods.TYPE_HOME:
268
types.add( "HOME" ); break;
269
case Contacts.ContactMethods.TYPE_WORK:
270
types.add( "WORK" ); break;
272
out.append( fold( "LABEL" +
273
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
274
":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
279
out.append( "VCARD:END\n" );
283
_ostream.write( out.toString().getBytes() );
286
catch( IOException e ) {
287
showError( R.string.error_ioerror );