/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/am/ed/exportcontacts/VcardExporter.java

  • Committer: Tim Marston
  • Date: 2014-03-01 18:03:39 UTC
  • Revision ID: tim@ed.am-20140301180339-rmfh8x7wys2inc65
new family pic

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Exporter.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
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://www.waxworlds.org/edam/software/android/export-contacts
 
7
 * to as "this program").  For more information, see
 
8
 * http://ed.am/dev/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 org.waxworlds.edam.exportcontacts;
 
24
package am.ed.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;
36
35
 
37
36
public class VcardExporter extends Exporter
38
37
{
39
38
        protected FileOutputStream _ostream = null;
 
39
        protected boolean _first_contact = true;
40
40
 
41
41
        public VcardExporter( Doit doit )
42
42
        {
55
55
 
56
56
                // check if the output file already exists
57
57
                if( file.exists() && file.length() > 0 )
58
 
                        showContinue( R.string.error_vcf_exists );
 
58
                        if( !showContinue( R.string.error_vcf_exists ) )
 
59
                                finish( ACTION_ABORT );
59
60
 
60
61
                // open file
61
62
                try {
82
83
                        // length of the line we'll be pulling off
83
84
                        int len = 75;
84
85
 
 
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
 
85
91
                        // count how many backslashes would be at the end of the line we're
86
92
                        // pulling off
87
93
                        int count = 0;
158
164
                return buffer.toString();
159
165
        }
160
166
 
 
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
        }
161
201
 
162
202
        @Override
163
203
        protected boolean exportContact( ContactData contact )
169
209
                if( contact.getPrimaryIdentifier() == null )
170
210
                        return false;
171
211
 
 
212
                // append newline
 
213
                if( _first_contact )
 
214
                        _first_contact = false;
 
215
                else
 
216
                        out.append( "\n" );
 
217
 
172
218
                // append header
173
 
                out.append( "VCARD:BEGIN\n" );
 
219
                out.append( "BEGIN:VCARD\n" );
174
220
                out.append( "VERSION:3.0\n" );
175
221
 
176
222
                // append formatted name
211
257
                        for( int a = 0; a < numbers.size(); a++ ) {
212
258
                                ArrayList< String > types = new ArrayList< String >();
213
259
                                switch( numbers.get( a ).getType() ) {
214
 
                                case Contacts.Phones.TYPE_HOME:
 
260
                                case ContactData.TYPE_HOME:
215
261
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
216
 
                                case Contacts.Phones.TYPE_WORK:
 
262
                                case ContactData.TYPE_WORK:
217
263
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
218
 
                                case Contacts.Phones.TYPE_FAX_HOME:
 
264
                                case ContactData.TYPE_FAX_HOME:
219
265
                                        types.add( "FAX" ); types.add( "HOME" ); break;
220
 
                                case Contacts.Phones.TYPE_FAX_WORK:
 
266
                                case ContactData.TYPE_FAX_WORK:
221
267
                                        types.add( "FAX" ); types.add( "WORK" ); break;
222
 
                                case Contacts.Phones.TYPE_PAGER:
 
268
                                case ContactData.TYPE_PAGER:
223
269
                                        types.add( "PAGER" ); break;
224
 
                                case Contacts.Phones.TYPE_MOBILE:
 
270
                                case ContactData.TYPE_MOBILE:
225
271
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
226
272
                                }
227
273
                                if( a == 0 ) types.add( "PREF" );
239
285
                                ArrayList< String > types = new ArrayList< String >();
240
286
                                types.add( "INTERNET" );
241
287
                                switch( emails.get( a ).getType() ) {
242
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
288
                                case ContactData.TYPE_HOME:
243
289
                                        types.add( "HOME" ); break;
244
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
290
                                case ContactData.TYPE_WORK:
245
291
                                        types.add( "WORK" ); break;
246
292
                                }
247
293
                                out.append( fold( "EMAIL" +
258
304
                                ArrayList< String > types = new ArrayList< String >();
259
305
                                types.add( "POSTAL" );
260
306
                                switch( addresses.get( a ).getType() ) {
261
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
307
                                case ContactData.TYPE_HOME:
262
308
                                        types.add( "HOME" ); break;
263
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
309
                                case ContactData.TYPE_WORK:
264
310
                                        types.add( "WORK" ); break;
265
311
                                }
 
312
                                // we use LABEL because is accepts formatted text (whereas ADR
 
313
                                // expects semicolon-delimited fields with specific purposes)
266
314
                                out.append( fold( "LABEL" +
267
315
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
268
316
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
269
317
                        }
270
318
                }
271
319
 
 
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
 
 
326
                // append birthday
 
327
                String birthday = contact.getBirthday();
 
328
                if( birthday != null ) {
 
329
                        birthday.trim();
 
330
                        if( isValidDateAndOrTime( birthday ) )
 
331
                                out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
 
332
                        else
 
333
                                out.append(
 
334
                                        fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
 
335
                }
 
336
 
272
337
                // append footer
273
 
                out.append( "VCARD:END\n" );
 
338
                out.append( "END:VCARD\n" );
 
339
 
 
340
                // replace '\n' with "\r\n" (spec requires CRLF)
 
341
                int pos = 0;
 
342
                while( true ) {
 
343
                        pos = out.indexOf( "\n", pos );
 
344
                        if( pos == -1 ) break;
 
345
                        out.replace( pos, pos + 1, "\r\n" );
 
346
 
 
347
                        // skip our inserted string
 
348
                        pos += 2;
 
349
                }
274
350
 
275
351
                // write to file
276
352
                try {