/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: 2013-07-16 16:24:59 UTC
  • Revision ID: tim@ed.am-20130716162459-u5z1iogael46ukp3
separate vcards with a newline

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
        {
164
164
                return buffer.toString();
165
165
        }
166
166
 
167
 
 
168
167
        @Override
169
168
        protected boolean exportContact( ContactData contact )
170
169
                throws AbortExportException
175
174
                if( contact.getPrimaryIdentifier() == null )
176
175
                        return false;
177
176
 
 
177
                // append newline
 
178
                if( _first_contact )
 
179
                        _first_contact = false;
 
180
                else
 
181
                        out.append( "\n" );
 
182
 
178
183
                // append header
179
184
                out.append( "BEGIN:VCARD\n" );
180
185
                out.append( "VERSION:3.0\n" );
217
222
                        for( int a = 0; a < numbers.size(); a++ ) {
218
223
                                ArrayList< String > types = new ArrayList< String >();
219
224
                                switch( numbers.get( a ).getType() ) {
220
 
                                case Contacts.Phones.TYPE_HOME:
 
225
                                case ContactData.TYPE_HOME:
221
226
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
222
 
                                case Contacts.Phones.TYPE_WORK:
 
227
                                case ContactData.TYPE_WORK:
223
228
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
224
 
                                case Contacts.Phones.TYPE_FAX_HOME:
 
229
                                case ContactData.TYPE_FAX_HOME:
225
230
                                        types.add( "FAX" ); types.add( "HOME" ); break;
226
 
                                case Contacts.Phones.TYPE_FAX_WORK:
 
231
                                case ContactData.TYPE_FAX_WORK:
227
232
                                        types.add( "FAX" ); types.add( "WORK" ); break;
228
 
                                case Contacts.Phones.TYPE_PAGER:
 
233
                                case ContactData.TYPE_PAGER:
229
234
                                        types.add( "PAGER" ); break;
230
 
                                case Contacts.Phones.TYPE_MOBILE:
 
235
                                case ContactData.TYPE_MOBILE:
231
236
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
232
237
                                }
233
238
                                if( a == 0 ) types.add( "PREF" );
245
250
                                ArrayList< String > types = new ArrayList< String >();
246
251
                                types.add( "INTERNET" );
247
252
                                switch( emails.get( a ).getType() ) {
248
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
253
                                case ContactData.TYPE_HOME:
249
254
                                        types.add( "HOME" ); break;
250
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
255
                                case ContactData.TYPE_WORK:
251
256
                                        types.add( "WORK" ); break;
252
257
                                }
253
258
                                out.append( fold( "EMAIL" +
264
269
                                ArrayList< String > types = new ArrayList< String >();
265
270
                                types.add( "POSTAL" );
266
271
                                switch( addresses.get( a ).getType() ) {
267
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
272
                                case ContactData.TYPE_HOME:
268
273
                                        types.add( "HOME" ); break;
269
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
274
                                case ContactData.TYPE_WORK:
270
275
                                        types.add( "WORK" ); break;
271
276
                                }
 
277
                                // we use LABEL because is accepts formatted text (whereas ADR
 
278
                                // expects semicolon-delimited fields with specific purposes)
272
279
                                out.append( fold( "LABEL" +
273
280
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
274
281
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
275
282
                        }
276
283
                }
277
284
 
 
285
                // append notes
 
286
                ArrayList< String > notes = contact.getNotes();
 
287
                if( notes != null )
 
288
                        for( int a = 0; a < notes.size(); a++ )
 
289
                                out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
 
290
 
278
291
                // append footer
279
292
                out.append( "END:VCARD\n" );
280
293
 
 
294
                // replace '\n' with "\r\n" (spec requires CRLF)
 
295
                int pos = 0;
 
296
                while( true ) {
 
297
                        pos = out.indexOf( "\n", pos );
 
298
                        if( pos == -1 ) break;
 
299
                        out.replace( pos, pos + 1, "\r\n" );
 
300
 
 
301
                        // skip our inserted string
 
302
                        pos += 2;
 
303
                }
 
304
 
281
305
                // write to file
282
306
                try {
283
307
                        _ostream.write( out.toString().getBytes() );