/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts
5 by edam
- added ContactReader interface
1
/*
2
 * Exporter.java
3
 *
12 by edam
changed all the URLs to ed.am, including copyrights, package names and project
4
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
5 by edam
- added ContactReader interface
5
 *
6
 * This file is part of the Export Contacts program (hereafter referred
7
 * to as "this program"). For more information, see
12 by edam
changed all the URLs to ed.am, including copyrights, package names and project
8
 * http://ed.am/dev/android/export-contacts
5 by edam
- added ContactReader interface
9
 *
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.
14
 *
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.
19
 *
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/>.
22
 */
23
12 by edam
changed all the URLs to ed.am, including copyrights, package names and project
24
package am.ed.exportcontacts;
5 by edam
- added ContactReader interface
25
26
import java.io.File;
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;
33
34
import android.content.SharedPreferences;
35
36
public class VcardExporter extends Exporter
37
{
38
	protected FileOutputStream _ostream = null;
39
40
	public VcardExporter( Doit doit )
41
	{
42
		super( doit );
43
	}
44
45
	@Override
46
	protected void preExport() throws AbortExportException
47
	{
48
		SharedPreferences prefs = getSharedPreferences();
49
50
		// create output filename
51
		String filename = prefs.getString( "filename", "android-contacts.vcf" );
52
		File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
53
			filename );
54
55
		// check if the output file already exists
56
		if( file.exists() && file.length() > 0 )
6 by edam
- fixed a couple of comment headers
57
			if( !showContinue( R.string.error_vcf_exists ) )
58
				finish( ACTION_ABORT );
5 by edam
- added ContactReader interface
59
60
		// open file
61
		try {
62
			_ostream = new FileOutputStream( file );
63
		}
64
		catch( FileNotFoundException e ) {
65
			showError( R.string.error_filenotfound );
66
		}
67
	}
68
69
	/**
70
	 * Do line folding at 75 chars
71
	 * @param raw string
72
	 * @return folded string
73
	 */
74
	private String fold( String line )
75
	{
76
		StringBuilder ret = new StringBuilder( line.length() );
77
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 )
81
		{
82
			// length of the line we'll be pulling off
83
			int len = 75;
84
6 by edam
- fixed a couple of comment headers
85
			// if splitting at this length would break apart a codepoint, use
86
			// one less char
87
			if( Character.isHighSurrogate( line.charAt( len - 1 ) ) )
88
				len--;
89
5 by edam
- added ContactReader interface
90
			// count how many backslashes would be at the end of the line we're
91
			// pulling off
92
			int count = 0;
93
			for( int a = len - 1; a >= 0; a-- )
94
				if( line.charAt( a ) == '\\' )
95
					count++;
96
				else
97
					break;
98
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
101
			// escape sequences
102
			if( count % 2 == 1 )
103
				len--;
104
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 );
108
		}
109
110
		// add any remaining data
111
		ret.append( line );
112
113
		return ret.toString();
114
	}
115
116
	/**
117
	 * Do unsafe character escaping
118
	 * @param raw string
119
	 * @return escaped string
120
	 */
121
	private String escape( String str )
122
	{
123
		StringBuilder ret = new StringBuilder( str.length() );
124
		for( int a = 0; a < str.length(); a++ )
125
		{
126
			int c = str.codePointAt( a );
127
			switch( c )
128
			{
129
			case '\n':
130
				// append escaped newline
131
				ret.append( "\\n" );
132
				break;
133
			case ',':
134
			case ';':
135
			case '\\':
136
				// append return character
137
				ret.append( '\\' );
138
				// fall through
139
			default:
140
				// append character
141
				ret.append( Character.toChars( c ) );
142
			}
143
		}
144
145
		return ret.toString();
146
	}
147
148
	/**
149
	 * join
150
	 */
151
	@SuppressWarnings( "rawtypes" )
152
	public static String join( AbstractCollection s, String delimiter)
153
	{
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() );
161
			}
162
		}
163
		return buffer.toString();
164
	}
165
166
167
	@Override
168
	protected boolean exportContact( ContactData contact )
169
		throws AbortExportException
170
	{
171
		StringBuilder out = new StringBuilder();
172
173
		// skip if the contact has no identifiable features
174
		if( contact.getPrimaryIdentifier() == null )
175
			return false;
176
177
		// append header
8 by edam
- fixed rather glaring error in vcard beginand end fields
178
		out.append( "BEGIN:VCARD\n" );
5 by edam
- added ContactReader interface
179
		out.append( "VERSION:3.0\n" );
180
181
		// append formatted name
182
		String name = contact.getName();
183
		if( name == null ) name = "";
184
		out.append( fold( "FN:" + escape( name ) ) + "\n" );
185
186
		// append name
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 ] ) );
192
		}
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" );
197
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" );
209
			}
210
		}
211
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() ) {
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
219
				case ContactData.TYPE_HOME:
5 by edam
- added ContactReader interface
220
					types.add( "VOICE" ); types.add( "HOME" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
221
				case ContactData.TYPE_WORK:
5 by edam
- added ContactReader interface
222
					types.add( "VOICE" ); types.add( "WORK" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
223
				case ContactData.TYPE_FAX_HOME:
5 by edam
- added ContactReader interface
224
					types.add( "FAX" ); types.add( "HOME" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
225
				case ContactData.TYPE_FAX_WORK:
5 by edam
- added ContactReader interface
226
					types.add( "FAX" ); types.add( "WORK" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
227
				case ContactData.TYPE_PAGER:
5 by edam
- added ContactReader interface
228
					types.add( "PAGER" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
229
				case ContactData.TYPE_MOBILE:
5 by edam
- added ContactReader interface
230
					types.add( "VOICE" ); types.add( "CELL" ); break;
231
				}
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" );
236
			}
237
		}
238
239
		// append email addresses
240
		ArrayList< Exporter.ContactData.EmailDetail > emails =
241
			contact.getEmails();
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() ) {
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
247
				case ContactData.TYPE_HOME:
5 by edam
- added ContactReader interface
248
					types.add( "HOME" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
249
				case ContactData.TYPE_WORK:
5 by edam
- added ContactReader interface
250
					types.add( "WORK" ); break;
251
				}
252
				out.append( fold( "EMAIL" +
253
					( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
254
					":" + escape( emails.get( a ).getEmail() ) ) + "\n" );
255
			}
256
		}
257
258
		// append addresses
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() ) {
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
266
				case ContactData.TYPE_HOME:
5 by edam
- added ContactReader interface
267
					types.add( "HOME" ); break;
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
268
				case ContactData.TYPE_WORK:
5 by edam
- added ContactReader interface
269
					types.add( "WORK" ); break;
270
				}
271
				out.append( fold( "LABEL" +
272
					( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
273
					":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
274
			}
275
		}
276
18 by edam
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs
277
		// append notes
278
		ArrayList< String > notes = contact.getNotes();
279
		if( notes != null )
280
			for( int a = 0; a < notes.size(); a++ )
281
				out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
282
5 by edam
- added ContactReader interface
283
		// append footer
8 by edam
- fixed rather glaring error in vcard beginand end fields
284
		out.append( "END:VCARD\n" );
5 by edam
- added ContactReader interface
285
286
		// write to file
287
		try {
288
			_ostream.write( out.toString().getBytes() );
289
			_ostream.flush();
290
		}
291
		catch( IOException e ) {
292
			showError( R.string.error_ioerror );
293
		}
294
295
		return true;
296
	}
297
298
}