/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/org/waxworlds/edam/exportcontacts/VcardExporter.java

  • Committer: edam
  • Date: 2011-06-11 08:22:04 UTC
  • Revision ID: edam@waxworlds.org-20110611082204-u2v1ri3a8iayq9b4
- added ContactReader interface
- added ContactsContactReader class to read old-style android.Contacts data
- updated FileChooser from import contacts app
- updated TODO
- added Doit activity
- added Exporter
- added VcardExporter that writes vCards

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 <edam@waxworlds.org>
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://ed.am/dev/android/export-contacts
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/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 am.ed.exportcontacts;
 
24
package org.waxworlds.edam.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;
35
36
 
36
37
public class VcardExporter extends Exporter
37
38
{
38
39
        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
 
                        if( !showContinue( R.string.error_vcf_exists ) )
59
 
                                finish( ACTION_ABORT );
 
58
                        showContinue( R.string.error_vcf_exists );
60
59
 
61
60
                // open file
62
61
                try {
83
82
                        // length of the line we'll be pulling off
84
83
                        int len = 75;
85
84
 
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
 
 
91
85
                        // count how many backslashes would be at the end of the line we're
92
86
                        // pulling off
93
87
                        int count = 0;
164
158
                return buffer.toString();
165
159
        }
166
160
 
 
161
 
167
162
        @Override
168
163
        protected boolean exportContact( ContactData contact )
169
164
                throws AbortExportException
174
169
                if( contact.getPrimaryIdentifier() == null )
175
170
                        return false;
176
171
 
177
 
                // append newline
178
 
                if( _first_contact )
179
 
                        _first_contact = false;
180
 
                else
181
 
                        out.append( "\n" );
182
 
 
183
172
                // append header
184
 
                out.append( "BEGIN:VCARD\n" );
 
173
                out.append( "VCARD:BEGIN\n" );
185
174
                out.append( "VERSION:3.0\n" );
186
175
 
187
176
                // append formatted name
222
211
                        for( int a = 0; a < numbers.size(); a++ ) {
223
212
                                ArrayList< String > types = new ArrayList< String >();
224
213
                                switch( numbers.get( a ).getType() ) {
225
 
                                case ContactData.TYPE_HOME:
 
214
                                case Contacts.Phones.TYPE_HOME:
226
215
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
227
 
                                case ContactData.TYPE_WORK:
 
216
                                case Contacts.Phones.TYPE_WORK:
228
217
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
229
 
                                case ContactData.TYPE_FAX_HOME:
 
218
                                case Contacts.Phones.TYPE_FAX_HOME:
230
219
                                        types.add( "FAX" ); types.add( "HOME" ); break;
231
 
                                case ContactData.TYPE_FAX_WORK:
 
220
                                case Contacts.Phones.TYPE_FAX_WORK:
232
221
                                        types.add( "FAX" ); types.add( "WORK" ); break;
233
 
                                case ContactData.TYPE_PAGER:
 
222
                                case Contacts.Phones.TYPE_PAGER:
234
223
                                        types.add( "PAGER" ); break;
235
 
                                case ContactData.TYPE_MOBILE:
 
224
                                case Contacts.Phones.TYPE_MOBILE:
236
225
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
237
226
                                }
238
227
                                if( a == 0 ) types.add( "PREF" );
250
239
                                ArrayList< String > types = new ArrayList< String >();
251
240
                                types.add( "INTERNET" );
252
241
                                switch( emails.get( a ).getType() ) {
253
 
                                case ContactData.TYPE_HOME:
 
242
                                case Contacts.ContactMethods.TYPE_HOME:
254
243
                                        types.add( "HOME" ); break;
255
 
                                case ContactData.TYPE_WORK:
 
244
                                case Contacts.ContactMethods.TYPE_WORK:
256
245
                                        types.add( "WORK" ); break;
257
246
                                }
258
247
                                out.append( fold( "EMAIL" +
269
258
                                ArrayList< String > types = new ArrayList< String >();
270
259
                                types.add( "POSTAL" );
271
260
                                switch( addresses.get( a ).getType() ) {
272
 
                                case ContactData.TYPE_HOME:
 
261
                                case Contacts.ContactMethods.TYPE_HOME:
273
262
                                        types.add( "HOME" ); break;
274
 
                                case ContactData.TYPE_WORK:
 
263
                                case Contacts.ContactMethods.TYPE_WORK:
275
264
                                        types.add( "WORK" ); break;
276
265
                                }
277
 
                                // we use LABEL because is accepts formatted text (whereas ADR
278
 
                                // expects semicolon-delimited fields with specific purposes)
279
266
                                out.append( fold( "LABEL" +
280
267
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
281
268
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
282
269
                        }
283
270
                }
284
271
 
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
 
 
291
272
                // append footer
292
 
                out.append( "END:VCARD\n" );
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
 
                }
 
273
                out.append( "VCARD:END\n" );
304
274
 
305
275
                // write to file
306
276
                try {