/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: 2010-07-04 14:54:18 UTC
  • Revision ID: edam@waxworlds.org-20100704145418-vcpya2sxsop0dlrq
- initial checkin
- copied intro and basic vcf-configure activities from import-contacts
- copied WizzardActivity class from import-contacts

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * Exporter.java
3
 
 *
4
 
 * Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
5
 
 *
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
9
 
 *
10
 
 * This program is free software: you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
 */
23
 
 
24
 
package org.waxworlds.edam.exportcontacts;
25
 
 
26
 
import java.io.File;
27
 
import java.io.FileNotFoundException;
28
 
import java.io.FileOutputStream;
29
 
import java.io.IOException;
30
 
import java.util.AbstractCollection;
31
 
import java.util.ArrayList;
32
 
import java.util.Iterator;
33
 
 
34
 
import android.content.SharedPreferences;
35
 
import android.provider.Contacts;
36
 
 
37
 
public class VcardExporter extends Exporter
38
 
{
39
 
        protected FileOutputStream _ostream = null;
40
 
 
41
 
        public VcardExporter( Doit doit )
42
 
        {
43
 
                super( doit );
44
 
        }
45
 
 
46
 
        @Override
47
 
        protected void preExport() throws AbortExportException
48
 
        {
49
 
                SharedPreferences prefs = getSharedPreferences();
50
 
 
51
 
                // create output filename
52
 
                String filename = prefs.getString( "filename", "android-contacts.vcf" );
53
 
                File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
54
 
                        filename );
55
 
 
56
 
                // check if the output file already exists
57
 
                if( file.exists() && file.length() > 0 )
58
 
                        showContinue( R.string.error_vcf_exists );
59
 
 
60
 
                // open file
61
 
                try {
62
 
                        _ostream = new FileOutputStream( file );
63
 
                }
64
 
                catch( FileNotFoundException e ) {
65
 
                        showError( R.string.error_filenotfound );
66
 
                }
67
 
        }
68
 
 
69
 
        /**
70
 
         * Do line folding at 75 chars
71
 
         * @param raw string
72
 
         * @return folded string
73
 
         */
74
 
        private String fold( String line )
75
 
        {
76
 
                StringBuilder ret = new StringBuilder( line.length() );
77
 
 
78
 
                // keep pulling off the first line's worth of chars, while the string is
79
 
                // still longer than a line should be
80
 
                while( line.length() > 75 )
81
 
                {
82
 
                        // length of the line we'll be pulling off
83
 
                        int len = 75;
84
 
 
85
 
                        // count how many backslashes would be at the end of the line we're
86
 
                        // pulling off
87
 
                        int count = 0;
88
 
                        for( int a = len - 1; a >= 0; a-- )
89
 
                                if( line.charAt( a ) == '\\' )
90
 
                                        count++;
91
 
                                else
92
 
                                        break;
93
 
 
94
 
                        // if there would be an odd number of slashes at the end of the line
95
 
                        // then pull off one fewer characters so that we don't break apart
96
 
                        // escape sequences
97
 
                        if( count % 2 == 1 )
98
 
                                len--;
99
 
 
100
 
                        // pull off the line and add it to the output, folded
101
 
                        ret.append( line.substring( 0, len ) + "\n " );
102
 
                        line = line.substring( len );
103
 
                }
104
 
 
105
 
                // add any remaining data
106
 
                ret.append( line );
107
 
 
108
 
                return ret.toString();
109
 
        }
110
 
 
111
 
        /**
112
 
         * Do unsafe character escaping
113
 
         * @param raw string
114
 
         * @return escaped string
115
 
         */
116
 
        private String escape( String str )
117
 
        {
118
 
                StringBuilder ret = new StringBuilder( str.length() );
119
 
                for( int a = 0; a < str.length(); a++ )
120
 
                {
121
 
                        int c = str.codePointAt( a );
122
 
                        switch( c )
123
 
                        {
124
 
                        case '\n':
125
 
                                // append escaped newline
126
 
                                ret.append( "\\n" );
127
 
                                break;
128
 
                        case ',':
129
 
                        case ';':
130
 
                        case '\\':
131
 
                                // append return character
132
 
                                ret.append( '\\' );
133
 
                                // fall through
134
 
                        default:
135
 
                                // append character
136
 
                                ret.append( Character.toChars( c ) );
137
 
                        }
138
 
                }
139
 
 
140
 
                return ret.toString();
141
 
        }
142
 
 
143
 
        /**
144
 
         * join
145
 
         */
146
 
        @SuppressWarnings( "rawtypes" )
147
 
        public static String join( AbstractCollection s, String delimiter)
148
 
        {
149
 
                StringBuffer buffer = new StringBuffer();
150
 
                Iterator iter = s.iterator();
151
 
                if( iter.hasNext() ) {
152
 
                        buffer.append( iter.next() );
153
 
                        while( iter.hasNext() ) {
154
 
                                buffer.append( delimiter );
155
 
                                buffer.append( iter.next() );
156
 
                        }
157
 
                }
158
 
                return buffer.toString();
159
 
        }
160
 
 
161
 
 
162
 
        @Override
163
 
        protected boolean exportContact( ContactData contact )
164
 
                throws AbortExportException
165
 
        {
166
 
                StringBuilder out = new StringBuilder();
167
 
 
168
 
                // skip if the contact has no identifiable features
169
 
                if( contact.getPrimaryIdentifier() == null )
170
 
                        return false;
171
 
 
172
 
                // append header
173
 
                out.append( "VCARD:BEGIN\n" );
174
 
                out.append( "VERSION:3.0\n" );
175
 
 
176
 
                // append formatted name
177
 
                String name = contact.getName();
178
 
                if( name == null ) name = "";
179
 
                out.append( fold( "FN:" + escape( name ) ) + "\n" );
180
 
 
181
 
                // append name
182
 
                String[] bits = name.split( " +" );
183
 
                StringBuilder tmp = new StringBuilder();
184
 
                for( int a = 1; a < bits.length - 1; a++ ) {
185
 
                        if( a > 1 ) tmp.append( " " );
186
 
                        tmp.append( escape( bits[ a ] ) );
187
 
                }
188
 
                String value = escape( bits[ bits.length - 1 ] ) + ";" +
189
 
                        ( bits.length > 1? escape( bits[ 0 ] ) : "" ) + ";" +
190
 
                        tmp.toString() + ";;";
191
 
                out.append( fold( "N:" + value ) + "\n" );
192
 
 
193
 
                // append organisations and titles
194
 
                ArrayList< Exporter.ContactData.OrganisationDetail > organisations =
195
 
                        contact.getOrganisations();
196
 
                if( organisations != null ) {
197
 
                        for( int a = 0; a < organisations.size(); a++ ) {
198
 
                                if( organisations.get( a ).getOrganisation() != null )
199
 
                                        out.append( fold( "ORG:" + escape(
200
 
                                                organisations.get( a ).getOrganisation() ) ) + "\n" );
201
 
                                if( organisations.get( a ).getTitle() != null )
202
 
                                        out.append( fold( "TITLE:" + escape(
203
 
                                                organisations.get( a ).getTitle() ) ) + "\n" );
204
 
                        }
205
 
                }
206
 
 
207
 
                // append phone numbers
208
 
                ArrayList< Exporter.ContactData.NumberDetail > numbers =
209
 
                        contact.getNumbers();
210
 
                if( numbers != null ) {
211
 
                        for( int a = 0; a < numbers.size(); a++ ) {
212
 
                                ArrayList< String > types = new ArrayList< String >();
213
 
                                switch( numbers.get( a ).getType() ) {
214
 
                                case Contacts.Phones.TYPE_HOME:
215
 
                                        types.add( "VOICE" ); types.add( "HOME" ); break;
216
 
                                case Contacts.Phones.TYPE_WORK:
217
 
                                        types.add( "VOICE" ); types.add( "WORK" ); break;
218
 
                                case Contacts.Phones.TYPE_FAX_HOME:
219
 
                                        types.add( "FAX" ); types.add( "HOME" ); break;
220
 
                                case Contacts.Phones.TYPE_FAX_WORK:
221
 
                                        types.add( "FAX" ); types.add( "WORK" ); break;
222
 
                                case Contacts.Phones.TYPE_PAGER:
223
 
                                        types.add( "PAGER" ); break;
224
 
                                case Contacts.Phones.TYPE_MOBILE:
225
 
                                        types.add( "VOICE" ); types.add( "CELL" ); break;
226
 
                                }
227
 
                                if( a == 0 ) types.add( "PREF" );
228
 
                                out.append( fold( "TEL" +
229
 
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
230
 
                                        ":" + escape( numbers.get( a ).getNumber() ) ) + "\n" );
231
 
                        }
232
 
                }
233
 
 
234
 
                // append email addresses
235
 
                ArrayList< Exporter.ContactData.EmailDetail > emails =
236
 
                        contact.getEmails();
237
 
                if( emails != null ) {
238
 
                        for( int a = 0; a < emails.size(); a++ ) {
239
 
                                ArrayList< String > types = new ArrayList< String >();
240
 
                                types.add( "INTERNET" );
241
 
                                switch( emails.get( a ).getType() ) {
242
 
                                case Contacts.ContactMethods.TYPE_HOME:
243
 
                                        types.add( "HOME" ); break;
244
 
                                case Contacts.ContactMethods.TYPE_WORK:
245
 
                                        types.add( "WORK" ); break;
246
 
                                }
247
 
                                out.append( fold( "EMAIL" +
248
 
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
249
 
                                        ":" + escape( emails.get( a ).getEmail() ) ) + "\n" );
250
 
                        }
251
 
                }
252
 
 
253
 
                // append addresses
254
 
                ArrayList< Exporter.ContactData.AddressDetail > addresses =
255
 
                        contact.getAddresses();
256
 
                if( addresses != null ) {
257
 
                        for( int a = 0; a < addresses.size(); a++ ) {
258
 
                                ArrayList< String > types = new ArrayList< String >();
259
 
                                types.add( "POSTAL" );
260
 
                                switch( addresses.get( a ).getType() ) {
261
 
                                case Contacts.ContactMethods.TYPE_HOME:
262
 
                                        types.add( "HOME" ); break;
263
 
                                case Contacts.ContactMethods.TYPE_WORK:
264
 
                                        types.add( "WORK" ); break;
265
 
                                }
266
 
                                out.append( fold( "LABEL" +
267
 
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
268
 
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
269
 
                        }
270
 
                }
271
 
 
272
 
                // append footer
273
 
                out.append( "VCARD:END\n" );
274
 
 
275
 
                // write to file
276
 
                try {
277
 
                        _ostream.write( out.toString().getBytes() );
278
 
                        _ostream.flush();
279
 
                }
280
 
                catch( IOException e ) {
281
 
                        showError( R.string.error_ioerror );
282
 
                }
283
 
 
284
 
                return true;
285
 
        }
286
 
 
287
 
}