48
47
import android.provider.Contacts;
49
48
import android.provider.Contacts.PhonesColumns;
51
public class VcardImporter extends Importer
50
public class VCFImporter extends Importer
53
private int _vcard_count = 0;
52
private int _vCardCount = 0;
54
53
private int _progress = 0;
56
public VcardImporter( Doit doit )
55
public VCFImporter( Doit doit )
110
109
countVCardFile( files[ i ] );
111
110
setTmpProgress( i );
113
setProgressMax( _vcard_count ); // will also update tmp progress
112
setProgressMax( _vCardCount ); // will also update tmp progress
116
115
setProgress( 0 );
117
116
for( int i = 0; i < files.length; i++ )
118
117
importVCardFile( files[ i ] );
119
setProgress( _vcard_count );
122
120
private void countVCardFile( File file ) throws AbortImportException
208
if( vcard == null ) {
204
if( vCard == null ) {
209
205
// look for vcard beginning
210
206
if( line.matches( "^BEGIN:VCARD" ) ) {
211
setProgress( _progress++ );
213
vcard_start_line = cli.getLineNumber();
207
setProgress( ++_progress );
217
212
// look for vcard content or ending
218
213
if( line.matches( "^END:VCARD" ) )
220
// finalise the vcard/contact
215
// 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
217
vCard.finaliseParsing();
218
importContact( vCard );
220
catch( VCard.ParseException e ) {
223
getText( R.string.error_vcf_parse ).toString()
224
+ fileName + "\n" + e.getMessage() ) )
226
finish( ACTION_ABORT );
229
catch( VCard.SkipContactException e ) {
259
237
// try giving the line to the vcard
261
vcard.parseLine( buffer, line,
239
vCard.parseLine( buffer, line,
262
240
cli.doesNextLineLookFolded() );
264
catch( Vcard.ParseException e ) {
242
catch( VCard.ParseException e ) {
266
244
if( !showContinue(
267
245
getText( R.string.error_vcf_parse ).toString()
269
getText( R.string.error_vcf_parse_line ).toString()
270
+ cli.getLineNumber() + "\n" + e.getMessage() ) )
246
+ fileName + "\n" + e.getMessage() ) )
272
248
finish( ACTION_ABORT );
275
251
// although we're continuing, we still need to abort
276
252
// this vCard. Further lines will be ignored until we
277
253
// get to another BEGIN:VCARD line.
280
catch( Vcard.SkipImportException e ) {
256
catch( VCard.SkipContactException e ) {
282
258
// abort this vCard. Further lines will be ignored until
283
259
// we get to another BEGIN:VCARD line.
352
325
return _pos > 0 && _pos < _content.length &&
353
326
_content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
356
public int getLineNumber()
362
private class Vcard extends ContactData
330
private class VCard extends ContactData
364
332
private final static int NAMELEVEL_NONE = 0;
365
private final static int NAMELEVEL_FN = 1;
366
private final static int NAMELEVEL_N = 2;
333
private final static int NAMELEVEL_ORG = 1;
334
private final static int NAMELEVEL_FN = 2;
335
private final static int NAMELEVEL_N = 3;
368
337
private final static int MULTILINE_NONE = 0;
369
338
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
376
345
private int _parser_multiline_state = MULTILINE_NONE;
377
346
private String _parser_current_name_and_params = null;
378
347
private String _parser_buffered_value_so_far = "";
379
private String _cached_organisation = null;
380
private String _cached_title = null;
382
349
protected class UnencodeResult
414
381
public ParseException( int res )
416
super( VcardImporter.this.getText( res ).toString() );
383
super( VCFImporter.this.getText( res ).toString() );
420
387
@SuppressWarnings("serial")
421
protected class SkipImportException extends Exception { }
388
protected class SkipContactException extends Exception { }
423
390
private String extractCollonPartFromLine( ByteBuffer buffer,
424
391
String line, boolean former )
461
428
public void parseLine( ByteBuffer buffer, String line,
462
429
boolean next_line_looks_folded )
463
throws ParseException, SkipImportException,
430
throws ParseException, SkipContactException,
464
431
AbortImportException
466
433
// do we have a version yet?
566
533
for( int i = 0; i < name_param_parts.length; i++ )
567
534
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
536
// parse encoding parameter
578
537
String encoding = checkParam( name_param_parts, "ENCODING" );
579
538
if( encoding != null ) encoding = encoding.toUpperCase();
580
if( is_interesting_field && encoding != null &&
581
!encoding.equals( "8BIT" ) &&
539
if( encoding != null && !encoding.equals( "8BIT" ) &&
582
540
!encoding.equals( "QUOTED-PRINTABLE" ) )
583
541
//&& !encoding.equals( "BASE64" ) )
588
546
// parse charset parameter
589
547
String charset = checkParam( name_param_parts, "CHARSET" );
590
548
if( charset != null ) charset = charset.toUpperCase();
591
if( charset != null &&
592
!charset.equals( "US-ASCII" ) &&
549
if( charset != null && !charset.equals( "US-ASCII" ) &&
593
550
!charset.equals( "ASCII" ) &&
594
551
!charset.equals( "UTF-8" ) )
664
621
parseFN( name_param_parts, complete_value );
665
622
else if( name_param_parts[ 0 ].equals( "ORG" ) )
666
623
parseORG( name_param_parts, complete_value );
667
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
668
parseTITLE( name_param_parts, complete_value );
669
624
else if( name_param_parts[ 0 ].equals( "TEL" ) )
670
625
parseTEL( name_param_parts, complete_value );
671
626
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
746
703
setName( value );
747
704
_name_level = NAMELEVEL_N;
706
// check now to see if we need to import this contact (to avoid
707
// parsing the rest of the vCard unnecessarily)
708
if( !isImportRequired( getName() ) )
709
throw new SkipContactException();
750
712
private void parseFN( String[] params, String value )
713
throws ParseException, SkipContactException
752
715
// already got a better name?
753
716
if( _name_level >= NAMELEVEL_FN ) return;
760
723
private void parseORG( String[] params, String value )
724
throws ParseException, SkipContactException
726
// already got a better name?
727
if( _name_level >= NAMELEVEL_ORG ) return;
763
730
String[] org_parts = splitValueBySemicolon( value );
764
if( org_parts == null || org_parts.length < 1 ) return;
766
// build organisation name
767
StringBuilder builder = new StringBuilder(
768
String.valueOf( org_parts[ 0 ] ) );
769
for( int a = 1; a < org_parts.length; a++ )
770
builder.append( ", " ).append( org_parts[ a ] );
771
String organisation = builder.toString();
773
// set organisation name (using a title we've previously found)
774
addOrganisation( organisation, _cached_title, true );
776
// if we've not previously found a title, store this organisation
777
// name (we'll need it when we find a title to update the
778
// organisation, by name), else if we *have* previously found a
779
// title, clear it (since we just used it)
780
if( _cached_title == null )
781
_cached_organisation = organisation;
783
_cached_title = null;
786
private void parseTITLE( String[] params, String value )
788
// if we previously had an organisation, look it up and append this
790
if( _cached_organisation != null && hasOrganisations() ) {
791
HashMap< String, ExtraDetail > datas = getOrganisations();
792
ExtraDetail detail = datas.get( _cached_organisation );
794
detail.setExtra( value );
797
// same as when handling organisation, if we've not previously found
798
// an organisation we store this title, else we clear it (since we
799
// just appended this title to it)
800
if( _cached_organisation == null )
801
_cached_title = value;
803
_cached_organisation = null;
733
if( org_parts.length > 1 && org_parts[ 0 ].length() == 0 )
734
value = org_parts[ 1 ];
735
else if( org_parts.length > 1 && org_parts[ 1 ].length() > 0 )
736
value = org_parts[ 0 ] + ", " + org_parts[ 1 ];
738
value = org_parts[ 0 ];
742
_name_level = NAMELEVEL_ORG;
806
745
private void parseTEL( String[] params, String value )
746
throws ParseException
808
748
if( value.length() == 0 ) return;
812
752
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
814
754
// here's the logic...
815
boolean is_preferred = types.contains( "PREF" );
755
boolean preferred = types.contains( "PREF" );
756
int type = PhonesColumns.TYPE_MOBILE;
757
if( types.contains( "VOICE" ) )
758
if( types.contains( "WORK" ) )
759
type = PhonesColumns.TYPE_WORK;
761
type = PhonesColumns.TYPE_HOME;
762
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
763
type = PhonesColumns.TYPE_MOBILE;
817
764
if( types.contains( "FAX" ) )
818
765
if( types.contains( "HOME" ) )
819
766
type = PhonesColumns.TYPE_FAX_HOME;
821
768
type = PhonesColumns.TYPE_FAX_WORK;
822
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
823
type = PhonesColumns.TYPE_MOBILE;
824
else if( types.contains( "PAGER" ) )
769
if( types.contains( "PAGER" ) )
825
770
type = PhonesColumns.TYPE_PAGER;
826
else if( types.contains( "WORK" ) )
827
type = PhonesColumns.TYPE_WORK;
829
type = PhonesColumns.TYPE_HOME;
831
772
// add phone number
832
addNumber( value, type, is_preferred );
773
addPhone( value, type, preferred );
835
776
public void parseEMAIL( String[] params, String value )
777
throws ParseException
837
779
if( value.length() == 0 ) return;
840
782
"PREF", "WORK", "HOME", "INTERNET" ) );
842
784
// add email address
843
boolean is_preferred = types.contains( "PREF" );
785
boolean preferred = types.contains( "PREF" );
845
786
if( types.contains( "WORK" ) )
846
type = Contacts.ContactMethods.TYPE_WORK;
787
addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
848
type = Contacts.ContactMethods.TYPE_HOME;
850
addEmail( value, type, is_preferred );
789
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
853
792
private void parseADR( String[] params, String value )
793
throws ParseException, SkipContactException
855
795
// get address parts
856
796
String[] adr_parts = splitValueBySemicolon( value );
866
806
"PREF", "WORK", "HOME", "INTERNET" ) );
870
809
if( types.contains( "WORK" ) )
871
type = Contacts.ContactMethods.TYPE_WORK;
810
addAddress( value, Contacts.ContactMethods.TYPE_WORK );
873
type = Contacts.ContactMethods.TYPE_HOME;
875
addAddress( value, type );
812
addAddress( value, Contacts.ContactMethods.TYPE_HOME);
878
public void finaliseVcard()
879
throws ParseException, ContactNotIdentifiableException
815
public void finaliseParsing()
816
throws ParseException, SkipContactException,
881
819
// missing version (and data is present)
882
820
if( _version == null && _buffers != null )
883
821
throw new ParseException( R.string.error_vcf_malformed );
885
// finalise the parent class
823
// missing name properties?
824
if( _name_level == NAMELEVEL_NONE )
825
throw new ParseException( R.string.error_vcf_noname );
827
// check if we should import this one? If we've already got an 'N'-
828
// type name, this will already have been done by parseN() so we
829
// mustn't do this here (or it could prompt twice!)
830
if( _name_level < NAMELEVEL_N && !isImportRequired( getName() ) )
831
throw new SkipContactException();
889
834
private String checkParam( String[] params, String name )