/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-07-16 16:24:59 UTC
  • Revision ID: tim@ed.am-20130716162459-u5z1iogael46ukp3
separate vcards with a newline

Show diffs side-by-side

added added

removed removed

49
49
                SharedPreferences prefs = getSharedPreferences();
50
50
 
51
51
                // create output filename
52
 
                File file = new File( ConfigureVCF.getSdCardPathPrefix() +
53
 
                        prefs.getString( "path", "/" ) +
54
 
                        prefs.getString( "filename", "android-contacts.vcf" ) );
 
52
                String filename = prefs.getString( "filename", "android-contacts.vcf" );
 
53
                File file = new File( "/sdcard" + prefs.getString( "location", "/" ) +
 
54
                        filename );
55
55
 
56
56
                // check if the output file already exists
57
57
                if( file.exists() && file.length() > 0 )
58
 
                        showContinueOrAbort( R.string.error_vcf_exists );
 
58
                        if( !showContinue( R.string.error_vcf_exists ) )
 
59
                                finish( ACTION_ABORT );
59
60
 
60
61
                // open file
61
62
                try {
62
63
                        _ostream = new FileOutputStream( file );
63
64
                }
64
65
                catch( FileNotFoundException e ) {
65
 
                        showError( getText( R.string.error_filenotfound ) +
66
 
                                file.getPath() );
 
66
                        showError( R.string.error_filenotfound );
67
67
                }
68
68
        }
69
69
 
164
164
                return buffer.toString();
165
165
        }
166
166
 
167
 
        /**
168
 
         * Is the provided value a valid date-and-or-time, as per the spec?
169
 
         *
170
 
         * @param value the value
171
 
         * @return true if it is
172
 
         */
173
 
        protected boolean isValidDateAndOrTime( String value )
174
 
        {
175
 
                // ISO 8601:2004 4.1.2 date with 4.1.2.3 a) and b) reduced accuracy
176
 
                String date =
177
 
                        "[0-9]{4}(?:-?[0-9]{2}(?:-?[0-9]{2})?)?";
178
 
 
179
 
                // ISO 8601:2000 5.2.1.3 d), e) and f) truncated date representation
180
 
                String date_trunc =
181
 
                        "--(?:[0-9]{2}(?:-?[0-9]{2})?|-[0-9]{2})";
182
 
 
183
 
                // ISO 8601:2004 4.2.2 time with 4.2.2.3 reduced accuracy, 4.2.4 UTC and
184
 
                // 4.2.5 zone offset, no 4.2.2.4 decimal fraction and no 4.2.3 24:00
185
 
                // midnight
186
 
                String time =
187
 
                        "(?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9](?::?(?:60|[0-5][0-9]))?)?" +
188
 
                        "(?:Z|[-+](?:[0-1][0-9]|2[0-3])(?::?[0-5][0-9])?)?";
189
 
 
190
 
                // ISO 8601:2000 5.3.1.4 a), b) and c) truncated time representation
191
 
                String time_trunc =
192
 
                        "-(?:[0-5][0-9](?::?(?:60|[0-5][0-9]))?|-(?:60|[0-5][0-9]))";
193
 
 
194
 
                // RFC6350 (vCard 3.0) date-and-or-time with mandatory time designator
195
 
                String date_and_or_time =
196
 
                        "(?:" + date + "|" + date_trunc + ")?" +
197
 
                        "(?:T(?:" + time + "|" + time_trunc + "))?";
198
 
 
199
 
                return value.matches( date_and_or_time );
200
 
        }
201
 
 
202
 
        protected void writeToFile( byte data[], String identifier )
203
 
                throws AbortExportException
204
 
        {
205
 
                // write to file
206
 
                try {
207
 
                        _ostream.write( data );
208
 
                        _ostream.flush();
209
 
                }
210
 
                catch( IOException e ) {
211
 
                        showError( R.string.error_ioerror );
212
 
                }
213
 
        }
214
 
 
215
167
        @Override
216
168
        protected boolean exportContact( ContactData contact )
217
169
                throws AbortExportException
233
185
                out.append( "VERSION:3.0\n" );
234
186
 
235
187
                // append formatted name
236
 
                String identifier = contact.getPrimaryIdentifier();
237
 
                if( identifier != null ) identifier = identifier.trim();
238
 
                if( identifier == null || identifier.length() == 0 ) {
239
 
                        showContinueOrAbort( R.string.error_vcf_noname );
240
 
                        return false;
241
 
                }
242
 
                out.append( fold( "FN:" + escape( identifier ) ) + "\n" );
243
 
 
244
 
                // append name
245
188
                String name = contact.getName();
246
189
                if( name == null ) name = "";
 
190
                out.append( fold( "FN:" + escape( name ) ) + "\n" );
 
191
 
 
192
                // append name
247
193
                String[] bits = name.split( " +" );
248
194
                StringBuilder tmp = new StringBuilder();
249
195
                for( int a = 1; a < bits.length - 1; a++ ) {
342
288
                        for( int a = 0; a < notes.size(); a++ )
343
289
                                out.append( fold( "NOTE:" + escape( notes.get( a ) ) ) + "\n" );
344
290
 
345
 
                // append birthday
346
 
                String birthday = contact.getBirthday();
347
 
                if( birthday != null ) {
348
 
                        birthday.trim();
349
 
                        if( isValidDateAndOrTime( birthday ) )
350
 
                                out.append( fold( "BDAY:" + escape( birthday ) ) + "\n" );
351
 
                        else
352
 
                                out.append(
353
 
                                        fold( "BDAY;VALUE=text:" + escape( birthday ) ) + "\n" );
354
 
                }
355
 
 
356
291
                // append footer
357
292
                out.append( "END:VCARD\n" );
358
293
 
368
303
                }
369
304
 
370
305
                // write to file
371
 
                writeToFile( out.toString().getBytes(), identifier );
 
306
                try {
 
307
                        _ostream.write( out.toString().getBytes() );
 
308
                        _ostream.flush();
 
309
                }
 
310
                catch( IOException e ) {
 
311
                        showError( R.string.error_ioerror );
 
312
                }
372
313
 
373
314
                return true;
374
315
        }