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