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
showContinue( R.string.error_vcf_exists );
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
// count how many backslashes would be at the end of the line we're
88
for( int a = len - 1; a >= 0; a-- )
89
if( line.charAt( a ) == '\\' )
94
// if there would be an odd number of slashes at the end of the line
95
// then pull off one fewer characters so that we don't break apart
100
// pull off the line and add it to the output, folded
101
ret.append( line.substring( 0, len ) + "\n " );
102
line = line.substring( len );
105
// add any remaining data
108
return ret.toString();
112
* Do unsafe character escaping
114
* @return escaped string
116
private String escape( String str )
118
StringBuilder ret = new StringBuilder( str.length() );
119
for( int a = 0; a < str.length(); a++ )
121
int c = str.codePointAt( a );
125
// append escaped newline
131
// append return character
136
ret.append( Character.toChars( c ) );
140
return ret.toString();
146
@SuppressWarnings( "rawtypes" )
147
public static String join( AbstractCollection s, String delimiter)
149
StringBuffer buffer = new StringBuffer();
150
Iterator iter = s.iterator();
151
if( iter.hasNext() ) {
152
buffer.append( iter.next() );
153
while( iter.hasNext() ) {
154
buffer.append( delimiter );
155
buffer.append( iter.next() );
158
return buffer.toString();
163
protected boolean exportContact( ContactData contact )
164
throws AbortExportException
166
StringBuilder out = new StringBuilder();
168
// skip if the contact has no identifiable features
169
if( contact.getPrimaryIdentifier() == null )
173
out.append( "VCARD:BEGIN\n" );
174
out.append( "VERSION:3.0\n" );
176
// append formatted name
177
String name = contact.getName();
178
if( name == null ) name = "";
179
out.append( fold( "FN:" + escape( name ) ) + "\n" );
182
String[] bits = name.split( " +" );
183
StringBuilder tmp = new StringBuilder();
184
for( int a = 1; a < bits.length - 1; a++ ) {
185
if( a > 1 ) tmp.append( " " );
186
tmp.append( escape( bits[ a ] ) );
188
String value = escape( bits[ bits.length - 1 ] ) + ";" +
189
( bits.length > 1? escape( bits[ 0 ] ) : "" ) + ";" +
190
tmp.toString() + ";;";
191
out.append( fold( "N:" + value ) + "\n" );
193
// append organisations and titles
194
ArrayList< Exporter.ContactData.OrganisationDetail > organisations =
195
contact.getOrganisations();
196
if( organisations != null ) {
197
for( int a = 0; a < organisations.size(); a++ ) {
198
if( organisations.get( a ).getOrganisation() != null )
199
out.append( fold( "ORG:" + escape(
200
organisations.get( a ).getOrganisation() ) ) + "\n" );
201
if( organisations.get( a ).getTitle() != null )
202
out.append( fold( "TITLE:" + escape(
203
organisations.get( a ).getTitle() ) ) + "\n" );
207
// append phone numbers
208
ArrayList< Exporter.ContactData.NumberDetail > numbers =
209
contact.getNumbers();
210
if( numbers != null ) {
211
for( int a = 0; a < numbers.size(); a++ ) {
212
ArrayList< String > types = new ArrayList< String >();
213
switch( numbers.get( a ).getType() ) {
214
case Contacts.Phones.TYPE_HOME:
215
types.add( "VOICE" ); types.add( "HOME" ); break;
216
case Contacts.Phones.TYPE_WORK:
217
types.add( "VOICE" ); types.add( "WORK" ); break;
218
case Contacts.Phones.TYPE_FAX_HOME:
219
types.add( "FAX" ); types.add( "HOME" ); break;
220
case Contacts.Phones.TYPE_FAX_WORK:
221
types.add( "FAX" ); types.add( "WORK" ); break;
222
case Contacts.Phones.TYPE_PAGER:
223
types.add( "PAGER" ); break;
224
case Contacts.Phones.TYPE_MOBILE:
225
types.add( "VOICE" ); types.add( "CELL" ); break;
227
if( a == 0 ) types.add( "PREF" );
228
out.append( fold( "TEL" +
229
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
230
":" + escape( numbers.get( a ).getNumber() ) ) + "\n" );
234
// append email addresses
235
ArrayList< Exporter.ContactData.EmailDetail > emails =
237
if( emails != null ) {
238
for( int a = 0; a < emails.size(); a++ ) {
239
ArrayList< String > types = new ArrayList< String >();
240
types.add( "INTERNET" );
241
switch( emails.get( a ).getType() ) {
242
case Contacts.ContactMethods.TYPE_HOME:
243
types.add( "HOME" ); break;
244
case Contacts.ContactMethods.TYPE_WORK:
245
types.add( "WORK" ); break;
247
out.append( fold( "EMAIL" +
248
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
249
":" + escape( emails.get( a ).getEmail() ) ) + "\n" );
254
ArrayList< Exporter.ContactData.AddressDetail > addresses =
255
contact.getAddresses();
256
if( addresses != null ) {
257
for( int a = 0; a < addresses.size(); a++ ) {
258
ArrayList< String > types = new ArrayList< String >();
259
types.add( "POSTAL" );
260
switch( addresses.get( a ).getType() ) {
261
case Contacts.ContactMethods.TYPE_HOME:
262
types.add( "HOME" ); break;
263
case Contacts.ContactMethods.TYPE_WORK:
264
types.add( "WORK" ); break;
266
out.append( fold( "LABEL" +
267
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
268
":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
273
out.append( "VCARD:END\n" );
277
_ostream.write( out.toString().getBytes() );
280
catch( IOException e ) {
281
showError( R.string.error_ioerror );