44
44
import java.util.regex.Matcher;
45
45
import java.util.regex.Pattern;
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
47
49
import android.content.SharedPreferences;
48
50
import android.provider.Contacts;
49
51
import android.provider.Contacts.PhonesColumns;
51
public class VcardImporter extends Importer
53
public class VCFImporter extends Importer
53
private int _vcard_count = 0;
55
private int _vCardCount = 0;
54
56
private int _progress = 0;
56
public VcardImporter( Doit doit )
58
public VCFImporter( Doit doit )
110
112
countVCardFile( files[ i ] );
111
113
setTmpProgress( i );
113
setProgressMax( _vcard_count ); // will also update tmp progress
115
setProgressMax( _vCardCount ); // will also update tmp progress
116
118
setProgress( 0 );
117
119
for( int i = 0; i < files.length; i++ )
118
120
importVCardFile( files[ i ] );
119
setProgress( _vcard_count );
122
123
private void countVCardFile( File file ) throws AbortImportException
132
boolean in_vcard = false;
133
boolean inVCard = false;
133
134
while( ( line = reader.readLine() ) != null )
136
137
// look for vcard beginning
137
138
if( line.matches( "^BEGIN:VCARD" ) ) {
142
143
else if( line.matches( "^END:VCARD" ) )
208
if( vcard == null ) {
207
if( vCard == null ) {
209
208
// look for vcard beginning
210
209
if( line.matches( "^BEGIN:VCARD" ) ) {
211
setProgress( _progress++ );
213
vcard_start_line = cli.getLineNumber();
210
setProgress( ++_progress );
217
215
// look for vcard content or ending
218
216
if( line.matches( "^END:VCARD" ) )
220
// finalise the vcard/contact
218
// store vcard and do away with it
222
vcard.finaliseVcard();
224
// pass the finalised contact to the importer
225
importContact( vcard );
227
catch( Vcard.ParseException e ) {
229
getText( R.string.error_vcf_parse ).toString()
231
getText( R.string.error_vcf_parse_line ).toString()
232
+ cli.getLineNumber() + ":\n" + e.getMessage() ) )
234
finish( ACTION_ABORT );
239
catch( ContactData.ContactNotIdentifiableException e ) {
241
getText( R.string.error_vcf_parse ).toString()
243
getText( R.string.error_vcf_parse_line ).toString()
244
+ vcard_start_line + ":\n" + getText(
245
R.string.error_vcf_notenoughinfo ).toString()
248
finish( ACTION_ABORT );
254
// discard this vcard
220
vCard.finaliseParsing();
221
importContact( vCard );
223
catch( VCard.ParseException e ) {
226
getText( R.string.error_vcf_parse ).toString()
227
+ fileName + "\n" + e.getMessage() ) )
229
finish( ACTION_ABORT );
232
catch( VCard.SkipContactException e ) {
259
240
// try giving the line to the vcard
261
vcard.parseLine( buffer, line,
242
vCard.parseLine( buffer, line,
262
243
cli.doesNextLineLookFolded() );
264
catch( Vcard.ParseException e ) {
245
catch( VCard.ParseException e ) {
266
247
if( !showContinue(
267
248
getText( R.string.error_vcf_parse ).toString()
269
getText( R.string.error_vcf_parse_line ).toString()
270
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
249
+ fileName + "\n" + e.getMessage() ) )
272
251
finish( ACTION_ABORT );
275
254
// although we're continuing, we still need to abort
276
255
// this vCard. Further lines will be ignored until we
277
256
// get to another BEGIN:VCARD line.
280
catch( Vcard.SkipImportException e ) {
259
catch( VCard.SkipContactException e ) {
282
261
// abort this vCard. Further lines will be ignored until
283
262
// we get to another BEGIN:VCARD line.
352
328
return _pos > 0 && _pos < _content.length &&
353
329
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
362
private class Vcard extends ContactData
333
private class VCard extends ContactData
364
335
private final static int NAMELEVEL_NONE = 0;
365
336
private final static int NAMELEVEL_FN = 1;
414
385
public ParseException( int res )
416
super( VcardImporter.this.getText( res ).toString() );
387
super( VCFImporter.this.getText( res ).toString() );
420
391
@SuppressWarnings("serial")
421
protected class SkipImportException extends Exception { }
392
protected class SkipContactException extends Exception { }
423
394
private String extractCollonPartFromLine( ByteBuffer buffer,
424
395
String line, boolean former )
461
432
public void parseLine( ByteBuffer buffer, String line,
462
433
boolean next_line_looks_folded )
463
throws ParseException, SkipImportException,
434
throws ParseException, SkipContactException,
464
435
AbortImportException
466
437
// do we have a version yet?
566
537
for( int i = 0; i < name_param_parts.length; i++ )
567
538
name_param_parts[ i ] = name_param_parts[ i ].trim();
569
// determine whether we care about this entry
570
final HashSet< String > interesting_fields =
571
new HashSet< String >( Arrays.asList( new String[]
572
{ "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
574
boolean is_interesting_field =
575
interesting_fields.contains( name_param_parts[ 0 ] );
577
540
// parse encoding parameter
578
541
String encoding = checkParam( name_param_parts, "ENCODING" );
579
542
if( encoding != null ) encoding = encoding.toUpperCase();
580
if( is_interesting_field && encoding != null &&
581
!encoding.equals( "8BIT" ) &&
543
if( encoding != null && !encoding.equals( "8BIT" ) &&
582
544
!encoding.equals( "QUOTED-PRINTABLE" ) )
583
545
//&& !encoding.equals( "BASE64" ) )
588
550
// parse charset parameter
589
551
String charset = checkParam( name_param_parts, "CHARSET" );
590
552
if( charset != null ) charset = charset.toUpperCase();
591
if( charset != null &&
592
!charset.equals( "US-ASCII" ) &&
553
if( charset != null && !charset.equals( "US-ASCII" ) &&
593
554
!charset.equals( "ASCII" ) &&
594
555
!charset.equals( "UTF-8" ) )
812
773
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
814
775
// here's the logic...
815
boolean is_preferred = types.contains( "PREF" );
776
boolean preferred = types.contains( "PREF" );
817
778
if( types.contains( "FAX" ) )
818
779
if( types.contains( "HOME" ) )
829
790
type = PhonesColumns.TYPE_HOME;
831
792
// add phone number
832
addNumber( value, type, is_preferred );
793
addNumber( value, type, preferred );
835
796
public void parseEMAIL( String[] params, String value )
840
801
"PREF", "WORK", "HOME", "INTERNET" ) );
842
803
// add email address
843
boolean is_preferred = types.contains( "PREF" );
804
boolean preferred = types.contains( "PREF" );
845
806
if( types.contains( "WORK" ) )
846
807
type = Contacts.ContactMethods.TYPE_WORK;
848
809
type = Contacts.ContactMethods.TYPE_HOME;
850
addEmail( value, type, is_preferred );
811
addEmail( value, type, preferred );
853
814
private void parseADR( String[] params, String value )
875
836
addAddress( value, type );
878
public void finaliseVcard()
879
throws ParseException, ContactNotIdentifiableException
839
public void finaliseParsing()
840
throws ParseException, SkipContactException,
881
843
// missing version (and data is present)
882
844
if( _version == null && _buffers != null )
883
845
throw new ParseException( R.string.error_vcf_malformed );
885
// finalise the parent class
847
// check if we should import this contact
849
if( !isImportRequired( this ) )
850
throw new SkipContactException();
852
catch( ContactNeedsMoreInfoException e ) {
853
throw new ParseException( R.string.error_vcf_notenoughinfo );
889
857
private String checkParam( String[] params, String name )