/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-06-22 19:31:44 UTC
  • Revision ID: tim@ed.am-20130622193144-0lo7w92qagow0kmu
minor style tweaks

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Exporter.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2012 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
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
{
217
216
                        for( int a = 0; a < numbers.size(); a++ ) {
218
217
                                ArrayList< String > types = new ArrayList< String >();
219
218
                                switch( numbers.get( a ).getType() ) {
220
 
                                case Contacts.Phones.TYPE_HOME:
 
219
                                case ContactData.TYPE_HOME:
221
220
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
222
 
                                case Contacts.Phones.TYPE_WORK:
 
221
                                case ContactData.TYPE_WORK:
223
222
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
224
 
                                case Contacts.Phones.TYPE_FAX_HOME:
 
223
                                case ContactData.TYPE_FAX_HOME:
225
224
                                        types.add( "FAX" ); types.add( "HOME" ); break;
226
 
                                case Contacts.Phones.TYPE_FAX_WORK:
 
225
                                case ContactData.TYPE_FAX_WORK:
227
226
                                        types.add( "FAX" ); types.add( "WORK" ); break;
228
 
                                case Contacts.Phones.TYPE_PAGER:
 
227
                                case ContactData.TYPE_PAGER:
229
228
                                        types.add( "PAGER" ); break;
230
 
                                case Contacts.Phones.TYPE_MOBILE:
 
229
                                case ContactData.TYPE_MOBILE:
231
230
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
232
231
                                }
233
232
                                if( a == 0 ) types.add( "PREF" );
245
244
                                ArrayList< String > types = new ArrayList< String >();
246
245
                                types.add( "INTERNET" );
247
246
                                switch( emails.get( a ).getType() ) {
248
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
247
                                case ContactData.TYPE_HOME:
249
248
                                        types.add( "HOME" ); break;
250
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
249
                                case ContactData.TYPE_WORK:
251
250
                                        types.add( "WORK" ); break;
252
251
                                }
253
252
                                out.append( fold( "EMAIL" +
264
263
                                ArrayList< String > types = new ArrayList< String >();
265
264
                                types.add( "POSTAL" );
266
265
                                switch( addresses.get( a ).getType() ) {
267
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
266
                                case ContactData.TYPE_HOME:
268
267
                                        types.add( "HOME" ); break;
269
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
268
                                case ContactData.TYPE_WORK:
270
269
                                        types.add( "WORK" ); break;
271
270
                                }
 
271
                                // we use LABEL because is accepts formatted text (whereas ADR
 
272
                                // expects semicolon-delimited fields with specific purposes)
272
273
                                out.append( fold( "LABEL" +
273
274
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
274
275
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
275
276
                        }
276
277
                }
277
278
 
 
279
                // append notes
 
280
                ArrayList< String > notes = contact.getNotes();
 
281
                if( notes != null )
 
282
                        for( int a = 0; a < notes.size(); a++ )
 
283
                                out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
 
284
 
278
285
                // append footer
279
286
                out.append( "END:VCARD\n" );
280
287
 
 
288
                // replace '\n' with "\r\n" (spec requires CRLF)
 
289
                int pos = 0;
 
290
                while( true ) {
 
291
                        pos = out.indexOf( "\n", pos );
 
292
                        if( pos == -1 ) break;
 
293
                        out.replace( pos, pos + 1, "\r\n" );
 
294
 
 
295
                        // skip our inserted string
 
296
                        pos += 2;
 
297
                }
 
298
 
281
299
                // write to file
282
300
                try {
283
301
                        _ostream.write( out.toString().getBytes() );