/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: edam
  • Date: 2012-12-21 13:26:31 UTC
  • Revision ID: tim@ed.am-20121221132631-ofj30d60lvuxp4a3
added ContactsContract backend; removed references to Contacts types (conversion to/from backend types now done in backends); added support for exporting NOTEs

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 <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
 
7
 * to as "this program"). For more information, see
8
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
36
36
public class VcardExporter extends Exporter
37
37
{
38
38
        protected FileOutputStream _ostream = null;
39
 
        protected boolean _first_contact = true;
40
39
 
41
40
        public VcardExporter( Doit doit )
42
41
        {
164
163
                return buffer.toString();
165
164
        }
166
165
 
 
166
 
167
167
        @Override
168
168
        protected boolean exportContact( ContactData contact )
169
169
                throws AbortExportException
174
174
                if( contact.getPrimaryIdentifier() == null )
175
175
                        return false;
176
176
 
177
 
                // append newline
178
 
                if( _first_contact )
179
 
                        _first_contact = false;
180
 
                else
181
 
                        out.append( "\n" );
182
 
 
183
177
                // append header
184
178
                out.append( "BEGIN:VCARD\n" );
185
179
                out.append( "VERSION:3.0\n" );
274
268
                                case ContactData.TYPE_WORK:
275
269
                                        types.add( "WORK" ); break;
276
270
                                }
277
 
                                // we use LABEL because is accepts formatted text (whereas ADR
278
 
                                // expects semicolon-delimited fields with specific purposes)
279
271
                                out.append( fold( "LABEL" +
280
272
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
281
273
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
291
283
                // append footer
292
284
                out.append( "END:VCARD\n" );
293
285
 
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
 
 
305
286
                // write to file
306
287
                try {
307
288
                        _ostream.write( out.toString().getBytes() );