/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/exportcontacts/VcardExporter.java

  • Committer: edam
  • Date: 2011-06-11 08:22:04 UTC
  • Revision ID: edam@waxworlds.org-20110611082204-u2v1ri3a8iayq9b4
- added ContactReader interface
- added ContactsContactReader class to read old-style android.Contacts data
- updated FileChooser from import contacts app
- updated TODO
- added Doit activity
- added Exporter
- added VcardExporter that writes vCards

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Exporter.java
3
3
 *
4
 
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
5
5
 *
6
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
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.exportcontacts;
 
24
package org.waxworlds.edam.exportcontacts;
25
25
 
26
26
import java.io.File;
27
27
import java.io.FileNotFoundException;
32
32
import java.util.Iterator;
33
33
 
34
34
import android.content.SharedPreferences;
 
35
import android.provider.Contacts;
35
36
 
36
37
public class VcardExporter extends Exporter
37
38
{
38
39
        protected FileOutputStream _ostream = null;
39
 
        protected boolean _first_contact = true;
40
40
 
41
41
        public VcardExporter( Doit doit )
42
42
        {
49
49
                SharedPreferences prefs = getSharedPreferences();
50
50
 
51
51
                // create output filename
52
 
                File file = new File( ConfigureVCF.getSdCardPathPrefix() +
53
 
                        prefs.getString( "path", "/" ) +
54
 
                        prefs.getString( "filename", "android-contacts.vcf" ) );
 
52
                String filename = prefs.getString( "filename", "android-contacts.vcf" );
 
53
                File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
 
54
                        filename );
55
55
 
56
56
                // check if the output file already exists
57
57
                if( file.exists() && file.length() > 0 )
58
 
                        showContinueOrAbort( R.string.error_vcf_exists );
 
58
                        showContinue( R.string.error_vcf_exists );
59
59
 
60
60
                // open file
61
61
                try {
62
62
                        _ostream = new FileOutputStream( file );
63
63
                }
64
64
                catch( FileNotFoundException e ) {
65
 
                        showError( getText( R.string.error_filenotfound ) +
66
 
                                file.getPath() );
 
65
                        showError( R.string.error_filenotfound );
67
66
                }
68
67
        }
69
68
 
83
82
                        // length of the line we'll be pulling off
84
83
                        int len = 75;
85
84
 
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
 
 
91
85
                        // count how many backslashes would be at the end of the line we're
92
86
                        // pulling off
93
87
                        int count = 0;
164
158
                return buffer.toString();
165
159
        }
166
160
 
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
 
 
202
 
        protected void writeToFile( byte data[], String identifier )
203
 
                throws AbortExportException
204
 
        {
205
 
                // write to file
206
 
                try {
207
 
                        _ostream.write( data );
208
 
                        _ostream.flush();
209
 
                }
210
 
                catch( IOException e ) {
211
 
                        showError( R.string.error_ioerror );
212
 
                }
213
 
        }
214
161
 
215
162
        @Override
216
163
        protected boolean exportContact( ContactData contact )
222
169
                if( contact.getPrimaryIdentifier() == null )
223
170
                        return false;
224
171
 
225
 
                // append newline
226
 
                if( _first_contact )
227
 
                        _first_contact = false;
228
 
                else
229
 
                        out.append( "\n" );
230
 
 
231
172
                // append header
232
 
                out.append( "BEGIN:VCARD\n" );
 
173
                out.append( "VCARD:BEGIN\n" );
233
174
                out.append( "VERSION:3.0\n" );
234
175
 
235
176
                // append formatted name
236
 
                String identifier = contact.getPrimaryIdentifier();
237
 
                if( identifier != null ) identifier = identifier.trim();
238
 
                if( identifier == null || identifier.length() == 0 ) {
239
 
                        showContinueOrAbort( R.string.error_vcf_noname );
240
 
                        return false;
241
 
                }
242
 
                out.append( fold( "FN:" + escape( identifier ) ) + "\n" );
243
 
 
244
 
                // append name
245
177
                String name = contact.getName();
246
178
                if( name == null ) name = "";
 
179
                out.append( fold( "FN:" + escape( name ) ) + "\n" );
 
180
 
 
181
                // append name
247
182
                String[] bits = name.split( " +" );
248
183
                StringBuilder tmp = new StringBuilder();
249
184
                for( int a = 1; a < bits.length - 1; a++ ) {
276
211
                        for( int a = 0; a < numbers.size(); a++ ) {
277
212
                                ArrayList< String > types = new ArrayList< String >();
278
213
                                switch( numbers.get( a ).getType() ) {
279
 
                                case ContactData.TYPE_HOME:
 
214
                                case Contacts.Phones.TYPE_HOME:
280
215
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
281
 
                                case ContactData.TYPE_WORK:
 
216
                                case Contacts.Phones.TYPE_WORK:
282
217
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
283
 
                                case ContactData.TYPE_FAX_HOME:
 
218
                                case Contacts.Phones.TYPE_FAX_HOME:
284
219
                                        types.add( "FAX" ); types.add( "HOME" ); break;
285
 
                                case ContactData.TYPE_FAX_WORK:
 
220
                                case Contacts.Phones.TYPE_FAX_WORK:
286
221
                                        types.add( "FAX" ); types.add( "WORK" ); break;
287
 
                                case ContactData.TYPE_PAGER:
 
222
                                case Contacts.Phones.TYPE_PAGER:
288
223
                                        types.add( "PAGER" ); break;
289
 
                                case ContactData.TYPE_MOBILE:
 
224
                                case Contacts.Phones.TYPE_MOBILE:
290
225
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
291
226
                                }
292
227
                                if( a == 0 ) types.add( "PREF" );
304
239
                                ArrayList< String > types = new ArrayList< String >();
305
240
                                types.add( "INTERNET" );
306
241
                                switch( emails.get( a ).getType() ) {
307
 
                                case ContactData.TYPE_HOME:
 
242
                                case Contacts.ContactMethods.TYPE_HOME:
308
243
                                        types.add( "HOME" ); break;
309
 
                                case ContactData.TYPE_WORK:
 
244
                                case Contacts.ContactMethods.TYPE_WORK:
310
245
                                        types.add( "WORK" ); break;
311
246
                                }
312
247
                                out.append( fold( "EMAIL" +
323
258
                                ArrayList< String > types = new ArrayList< String >();
324
259
                                types.add( "POSTAL" );
325
260
                                switch( addresses.get( a ).getType() ) {
326
 
                                case ContactData.TYPE_HOME:
 
261
                                case Contacts.ContactMethods.TYPE_HOME:
327
262
                                        types.add( "HOME" ); break;
328
 
                                case ContactData.TYPE_WORK:
 
263
                                case Contacts.ContactMethods.TYPE_WORK:
329
264
                                        types.add( "WORK" ); break;
330
265
                                }
331
 
                                // we use LABEL because is accepts formatted text (whereas ADR
332
 
                                // expects semicolon-delimited fields with specific purposes)
333
266
                                out.append( fold( "LABEL" +
334
267
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
335
268
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
336
269
                        }
337
270
                }
338
271
 
339
 
                // append notes
340
 
                ArrayList< String > notes = contact.getNotes();
341
 
                if( notes != null )
342
 
                        for( int a = 0; a < notes.size(); a++ )
343
 
                                out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
344
 
 
345
 
                // append birthday
346
 
                String birthday = contact.getBirthday();
347
 
                if( birthday != null ) {
348
 
                        birthday.trim();
349
 
                        if( isValidDateAndOrTime( birthday ) )
350
 
                                out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
351
 
                        else
352
 
                                out.append(
353
 
                                        fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
354
 
                }
355
 
 
356
272
                // append footer
357
 
                out.append( "END:VCARD\n" );
358
 
 
359
 
                // replace '\n' with "\r\n" (spec requires CRLF)
360
 
                int pos = 0;
361
 
                while( true ) {
362
 
                        pos = out.indexOf( "\n", pos );
363
 
                        if( pos == -1 ) break;
364
 
                        out.replace( pos, pos + 1, "\r\n" );
365
 
 
366
 
                        // skip our inserted string
367
 
                        pos += 2;
368
 
                }
 
273
                out.append( "VCARD:END\n" );
369
274
 
370
275
                // write to file
371
 
                writeToFile( out.toString().getBytes(), identifier );
 
276
                try {
 
277
                        _ostream.write( out.toString().getBytes() );
 
278
                        _ostream.flush();
 
279
                }
 
280
                catch( IOException e ) {
 
281
                        showError( R.string.error_ioerror );
 
282
                }
372
283
 
373
284
                return true;
374
285
        }