/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-03-04 16:29:21 UTC
  • Revision ID: tim@ed.am-20130304162921-0b456wmqs00zrjp2
corrected typo

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 2012 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Export Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/export-contacts
 
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
{
55
54
 
56
55
                // check if the output file already exists
57
56
                if( file.exists() && file.length() > 0 )
58
 
                        showContinue( R.string.error_vcf_exists );
 
57
                        if( !showContinue( R.string.error_vcf_exists ) )
 
58
                                finish( ACTION_ABORT );
59
59
 
60
60
                // open file
61
61
                try {
82
82
                        // length of the line we'll be pulling off
83
83
                        int len = 75;
84
84
 
 
85
                        // if splitting at this length would break apart a codepoint, use
 
86
                        // one less char
 
87
                        if( Character.isHighSurrogate( line.charAt( len - 1 ) ) )
 
88
                                len--;
 
89
 
85
90
                        // count how many backslashes would be at the end of the line we're
86
91
                        // pulling off
87
92
                        int count = 0;
170
175
                        return false;
171
176
 
172
177
                // append header
173
 
                out.append( "VCARD:BEGIN\n" );
 
178
                out.append( "BEGIN:VCARD\n" );
174
179
                out.append( "VERSION:3.0\n" );
175
180
 
176
181
                // append formatted name
211
216
                        for( int a = 0; a < numbers.size(); a++ ) {
212
217
                                ArrayList< String > types = new ArrayList< String >();
213
218
                                switch( numbers.get( a ).getType() ) {
214
 
                                case Contacts.Phones.TYPE_HOME:
 
219
                                case ContactData.TYPE_HOME:
215
220
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
216
 
                                case Contacts.Phones.TYPE_WORK:
 
221
                                case ContactData.TYPE_WORK:
217
222
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
218
 
                                case Contacts.Phones.TYPE_FAX_HOME:
 
223
                                case ContactData.TYPE_FAX_HOME:
219
224
                                        types.add( "FAX" ); types.add( "HOME" ); break;
220
 
                                case Contacts.Phones.TYPE_FAX_WORK:
 
225
                                case ContactData.TYPE_FAX_WORK:
221
226
                                        types.add( "FAX" ); types.add( "WORK" ); break;
222
 
                                case Contacts.Phones.TYPE_PAGER:
 
227
                                case ContactData.TYPE_PAGER:
223
228
                                        types.add( "PAGER" ); break;
224
 
                                case Contacts.Phones.TYPE_MOBILE:
 
229
                                case ContactData.TYPE_MOBILE:
225
230
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
226
231
                                }
227
232
                                if( a == 0 ) types.add( "PREF" );
239
244
                                ArrayList< String > types = new ArrayList< String >();
240
245
                                types.add( "INTERNET" );
241
246
                                switch( emails.get( a ).getType() ) {
242
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
247
                                case ContactData.TYPE_HOME:
243
248
                                        types.add( "HOME" ); break;
244
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
249
                                case ContactData.TYPE_WORK:
245
250
                                        types.add( "WORK" ); break;
246
251
                                }
247
252
                                out.append( fold( "EMAIL" +
258
263
                                ArrayList< String > types = new ArrayList< String >();
259
264
                                types.add( "POSTAL" );
260
265
                                switch( addresses.get( a ).getType() ) {
261
 
                                case Contacts.ContactMethods.TYPE_HOME:
 
266
                                case ContactData.TYPE_HOME:
262
267
                                        types.add( "HOME" ); break;
263
 
                                case Contacts.ContactMethods.TYPE_WORK:
 
268
                                case ContactData.TYPE_WORK:
264
269
                                        types.add( "WORK" ); break;
265
270
                                }
 
271
                                // we use LABEL because is accepts formatted text (whereas ADR
 
272
                                // expects semicolon-delimited fields with specific purposes)
266
273
                                out.append( fold( "LABEL" +
267
274
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
268
275
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
269
276
                        }
270
277
                }
271
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
 
272
285
                // append footer
273
 
                out.append( "VCARD:END\n" );
 
286
                out.append( "END:VCARD\n" );
 
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
                }
274
298
 
275
299
                // write to file
276
300
                try {