bzr branch
http://bzr.ed.am/android/export-contacts
5
by edam
- added ContactReader interface |
1 |
/* |
2 |
* Exporter.java |
|
3 |
* |
|
31
by Tim Marston
separate vcards with a newline |
4 |
* Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am> |
5
by edam
- added ContactReader interface |
5 |
* |
6 |
* This file is part of the Export Contacts program (hereafter referred |
|
30
by Tim Marston
minor style tweaks |
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; |
|
31
by Tim Marston
separate vcards with a newline |
39 |
protected boolean _first_contact = true; |
5
by edam
- added ContactReader interface |
40 |
|
41 |
public VcardExporter( Doit doit ) |
|
42 |
{ |
|
43 |
super( doit ); |
|
44 |
} |
|
45 |
||
46 |
@Override |
|
47 |
protected void preExport() throws AbortExportException |
|
48 |
{ |
|
49 |
SharedPreferences prefs = getSharedPreferences(); |
|
50 |
||
51 |
// create output filename |
|
52 |
String filename = prefs.getString( "filename", "android-contacts.vcf" ); |
|
53 |
File file = new File( "/sdcard" + prefs.getString( "location", "/" ) + |
|
54 |
filename ); |
|
55 |
||
56 |
// check if the output file already exists |
|
57 |
if( file.exists() && file.length() > 0 ) |
|
6
by edam
- fixed a couple of comment headers |
58 |
if( !showContinue( R.string.error_vcf_exists ) ) |
59 |
finish( ACTION_ABORT ); |
|
5
by edam
- added ContactReader interface |
60 |
|
61 |
// open file |
|
62 |
try { |
|
63 |
_ostream = new FileOutputStream( file ); |
|
64 |
} |
|
65 |
catch( FileNotFoundException e ) { |
|
66 |
showError( R.string.error_filenotfound ); |
|
67 |
} |
|
68 |
} |
|
69 |
||
70 |
/** |
|
71 |
* Do line folding at 75 chars |
|
72 |
* @param raw string |
|
73 |
* @return folded string |
|
74 |
*/ |
|
75 |
private String fold( String line ) |
|
76 |
{ |
|
77 |
StringBuilder ret = new StringBuilder( line.length() ); |
|
78 |
||
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 ) |
|
82 |
{ |
|
83 |
// length of the line we'll be pulling off |
|
84 |
int len = 75; |
|
85 |
||
6
by edam
- fixed a couple of comment headers |
86 |
// if splitting at this length would break apart a codepoint, use |
87 |
// one less char |
|
88 |
if( Character.isHighSurrogate( line.charAt( len - 1 ) ) ) |
|
89 |
len--; |
|
90 |
||
5
by edam
- added ContactReader interface |
91 |
// count how many backslashes would be at the end of the line we're |
92 |
// pulling off |
|
93 |
int count = 0; |
|
94 |
for( int a = len - 1; a >= 0; a-- ) |
|
95 |
if( line.charAt( a ) == '\\' ) |
|
96 |
count++; |
|
97 |
else |
|
98 |
break; |
|
99 |
||
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 |
|
102 |
// escape sequences |
|
103 |
if( count % 2 == 1 ) |
|
104 |
len--; |
|
105 |
||
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 ); |
|
109 |
} |
|
110 |
||
111 |
// add any remaining data |
|
112 |
ret.append( line ); |
|
113 |
||
114 |
return ret.toString(); |
|
115 |
} |
|
116 |
||
117 |
/** |
|
118 |
* Do unsafe character escaping |
|
119 |
* @param raw string |
|
120 |
* @return escaped string |
|
121 |
*/ |
|
122 |
private String escape( String str ) |
|
123 |
{ |
|
124 |
StringBuilder ret = new StringBuilder( str.length() ); |
|
125 |
for( int a = 0; a < str.length(); a++ ) |
|
126 |
{ |
|
127 |
int c = str.codePointAt( a ); |
|
128 |
switch( c ) |
|
129 |
{ |
|
130 |
case '\n': |
|
131 |
// append escaped newline |
|
132 |
ret.append( "\\n" ); |
|
133 |
break; |
|
134 |
case ',': |
|
135 |
case ';': |
|
136 |
case '\\': |
|
137 |
// append return character |
|
138 |
ret.append( '\\' ); |
|
139 |
// fall through |
|
140 |
default: |
|
141 |
// append character |
|
142 |
ret.append( Character.toChars( c ) ); |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
return ret.toString(); |
|
147 |
} |
|
148 |
||
149 |
/** |
|
150 |
* join |
|
151 |
*/ |
|
152 |
@SuppressWarnings( "rawtypes" ) |
|
153 |
public static String join( AbstractCollection s, String delimiter) |
|
154 |
{ |
|
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() ); |
|
162 |
} |
|
163 |
} |
|
164 |
return buffer.toString(); |
|
165 |
} |
|
166 |
||
32
by Tim Marston
added support for birthdays |
167 |
/** |
168 |
* Is the provided value a valid date-and-or-time, as per the spec? |
|
169 |
* |
|
170 |
* @param value the value |
|
171 |
* @return true if it is |
|
172 |
*/ |
|
173 |
protected boolean isValidDateAndOrTime( String value ) |
|
174 |
{ |
|
175 |
// ISO 8601:2004 4.1.2 date with 4.1.2.3 a) and b) reduced accuracy |
|
176 |
String date = |
|
177 |
"[0-9]{4}(?:-?[0-9]{2}(?:-?[0-9]{2})?)?"; |
|
178 |
||
179 |
// ISO 8601:2000 5.2.1.3 d), e) and f) truncated date representation |
|
180 |
String date_trunc = |
|
181 |
"--(?:[0-9]{2}(?:-?[0-9]{2})?|-[0-9]{2})"; |
|
182 |
||
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 |
|
185 |
// midnight |
|
186 |
String time = |
|
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])?)?"; |
|
189 |
||
190 |
// ISO 8601:2000 5.3.1.4 a), b) and c) truncated time representation |
|
191 |
String time_trunc = |
|
192 |
"-(?:[0-5][0-9](?::?(?:60|[0-5][0-9]))?|-(?:60|[0-5][0-9]))"; |
|
193 |
||
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 + "))?"; |
|
198 |
||
199 |
return value.matches( date_and_or_time ); |
|
200 |
} |
|
201 |
||
5
by edam
- added ContactReader interface |
202 |
@Override |
203 |
protected boolean exportContact( ContactData contact ) |
|
204 |
throws AbortExportException |
|
205 |
{ |
|
206 |
StringBuilder out = new StringBuilder(); |
|
207 |
||
208 |
// skip if the contact has no identifiable features |
|
209 |
if( contact.getPrimaryIdentifier() == null ) |
|
210 |
return false; |
|
211 |
||
31
by Tim Marston
separate vcards with a newline |
212 |
// append newline |
213 |
if( _first_contact ) |
|
214 |
_first_contact = false; |
|
215 |
else |
|
216 |
out.append( "\n" ); |
|
217 |
||
5
by edam
- added ContactReader interface |
218 |
// append header |
8
by edam
- fixed rather glaring error in vcard beginand end fields |
219 |
out.append( "BEGIN:VCARD\n" ); |
5
by edam
- added ContactReader interface |
220 |
out.append( "VERSION:3.0\n" ); |
221 |
||
222 |
// append formatted name |
|
223 |
String name = contact.getName(); |
|
224 |
if( name == null ) name = ""; |
|
225 |
out.append( fold( "FN:" + escape( name ) ) + "\n" ); |
|
226 |
||
227 |
// append name |
|
228 |
String[] bits = name.split( " +" ); |
|
229 |
StringBuilder tmp = new StringBuilder(); |
|
230 |
for( int a = 1; a < bits.length - 1; a++ ) { |
|
231 |
if( a > 1 ) tmp.append( " " ); |
|
232 |
tmp.append( escape( bits[ a ] ) ); |
|
233 |
} |
|
234 |
String value = escape( bits[ bits.length - 1 ] ) + ";" + |
|
235 |
( bits.length > 1? escape( bits[ 0 ] ) : "" ) + ";" + |
|
236 |
tmp.toString() + ";;"; |
|
237 |
out.append( fold( "N:" + value ) + "\n" ); |
|
238 |
||
239 |
// append organisations and titles |
|
240 |
ArrayList< Exporter.ContactData.OrganisationDetail > organisations = |
|
241 |
contact.getOrganisations(); |
|
242 |
if( organisations != null ) { |
|
243 |
for( int a = 0; a < organisations.size(); a++ ) { |
|
244 |
if( organisations.get( a ).getOrganisation() != null ) |
|
245 |
out.append( fold( "ORG:" + escape( |
|
246 |
organisations.get( a ).getOrganisation() ) ) + "\n" ); |
|
247 |
if( organisations.get( a ).getTitle() != null ) |
|
248 |
out.append( fold( "TITLE:" + escape( |
|
249 |
organisations.get( a ).getTitle() ) ) + "\n" ); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
// append phone numbers |
|
254 |
ArrayList< Exporter.ContactData.NumberDetail > numbers = |
|
255 |
contact.getNumbers(); |
|
256 |
if( numbers != null ) { |
|
257 |
for( int a = 0; a < numbers.size(); a++ ) { |
|
258 |
ArrayList< String > types = new ArrayList< String >(); |
|
259 |
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 |
260 |
case ContactData.TYPE_HOME: |
5
by edam
- added ContactReader interface |
261 |
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 |
262 |
case ContactData.TYPE_WORK: |
5
by edam
- added ContactReader interface |
263 |
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 |
264 |
case ContactData.TYPE_FAX_HOME: |
5
by edam
- added ContactReader interface |
265 |
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 |
266 |
case ContactData.TYPE_FAX_WORK: |
5
by edam
- added ContactReader interface |
267 |
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 |
268 |
case ContactData.TYPE_PAGER: |
5
by edam
- added ContactReader interface |
269 |
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 |
270 |
case ContactData.TYPE_MOBILE: |
5
by edam
- added ContactReader interface |
271 |
types.add( "VOICE" ); types.add( "CELL" ); break; |
272 |
} |
|
273 |
if( a == 0 ) types.add( "PREF" ); |
|
274 |
out.append( fold( "TEL" + |
|
275 |
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) + |
|
276 |
":" + escape( numbers.get( a ).getNumber() ) ) + "\n" ); |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
// append email addresses |
|
281 |
ArrayList< Exporter.ContactData.EmailDetail > emails = |
|
282 |
contact.getEmails(); |
|
283 |
if( emails != null ) { |
|
284 |
for( int a = 0; a < emails.size(); a++ ) { |
|
285 |
ArrayList< String > types = new ArrayList< String >(); |
|
286 |
types.add( "INTERNET" ); |
|
287 |
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 |
288 |
case ContactData.TYPE_HOME: |
5
by edam
- added ContactReader interface |
289 |
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 |
290 |
case ContactData.TYPE_WORK: |
5
by edam
- added ContactReader interface |
291 |
types.add( "WORK" ); break; |
292 |
} |
|
293 |
out.append( fold( "EMAIL" + |
|
294 |
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) + |
|
295 |
":" + escape( emails.get( a ).getEmail() ) ) + "\n" ); |
|
296 |
} |
|
297 |
} |
|
298 |
||
299 |
// append addresses |
|
300 |
ArrayList< Exporter.ContactData.AddressDetail > addresses = |
|
301 |
contact.getAddresses(); |
|
302 |
if( addresses != null ) { |
|
303 |
for( int a = 0; a < addresses.size(); a++ ) { |
|
304 |
ArrayList< String > types = new ArrayList< String >(); |
|
305 |
types.add( "POSTAL" ); |
|
306 |
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 |
307 |
case ContactData.TYPE_HOME: |
5
by edam
- added ContactReader interface |
308 |
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 |
309 |
case ContactData.TYPE_WORK: |
5
by edam
- added ContactReader interface |
310 |
types.add( "WORK" ); break; |
311 |
} |
|
21
by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n) |
312 |
// we use LABEL because is accepts formatted text (whereas ADR |
313 |
// expects semicolon-delimited fields with specific purposes) |
|
5
by edam
- added ContactReader interface |
314 |
out.append( fold( "LABEL" + |
315 |
( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) + |
|
316 |
":" + escape( addresses.get( a ).getAddress() ) ) + "\n" ); |
|
317 |
} |
|
318 |
} |
|
319 |
||
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 |
320 |
// append notes |
321 |
ArrayList< String > notes = contact.getNotes(); |
|
322 |
if( notes != null ) |
|
323 |
for( int a = 0; a < notes.size(); a++ ) |
|
324 |
out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" ); |
|
325 |
||
32
by Tim Marston
added support for birthdays |
326 |
// append birthday |
327 |
String birthday = contact.getBirthday().trim(); |
|
328 |
if( birthday != null ) { |
|
329 |
if( isValidDateAndOrTime( birthday ) ) |
|
330 |
out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" ); |
|
331 |
else |
|
332 |
out.append( |
|
333 |
fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" ); |
|
334 |
} |
|
335 |
||
5
by edam
- added ContactReader interface |
336 |
// append footer |
8
by edam
- fixed rather glaring error in vcard beginand end fields |
337 |
out.append( "END:VCARD\n" ); |
5
by edam
- added ContactReader interface |
338 |
|
21
by edam
fixed column values in Contacts backend; don't write-out empty notes; remember to close my queries; switched from wrappers to static valueOf() functions; fix line-endings (should be \r\n) |
339 |
// replace '\n' with "\r\n" (spec requires CRLF) |
340 |
int pos = 0; |
|
341 |
while( true ) { |
|
342 |
pos = out.indexOf( "\n", pos ); |
|
343 |
if( pos == -1 ) break; |
|
344 |
out.replace( pos, pos + 1, "\r\n" ); |
|
345 |
||
346 |
// skip our inserted string |
|
347 |
pos += 2; |
|
348 |
} |
|
349 |
||
5
by edam
- added ContactReader interface |
350 |
// write to file |
351 |
try { |
|
352 |
_ostream.write( out.toString().getBytes() ); |
|
353 |
_ostream.flush(); |
|
354 |
} |
|
355 |
catch( IOException e ) { |
|
356 |
showError( R.string.error_ioerror ); |
|
357 |
} |
|
358 |
||
359 |
return true; |
|
360 |
} |
|
361 |
||
362 |
} |