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 )
205
if( vcard == null ) {
204
if( vCard == null ) {
206
205
// look for vcard beginning
207
206
if( line.matches( "^BEGIN:VCARD" ) ) {
208
207
setProgress( ++_progress );
213
212
// look for vcard content or ending
214
213
if( line.matches( "^END:VCARD" ) )
216
// finalise the vcard/contact
215
// store vcard and do away with it
218
vcard.finaliseVcard();
217
vCard.finaliseParsing();
218
importContact( vCard );
220
catch( Vcard.ParseException e ) {
220
catch( VCard.ParseException e ) {
222
222
if( !showContinue(
223
223
getText( R.string.error_vcf_parse ).toString()
226
226
finish( ACTION_ABORT );
230
// pass the finalised contact to the importer
231
importContact( vcard );
229
catch( VCard.SkipContactException e ) {
238
237
// try giving the line to the vcard
240
vcard.parseLine( buffer, line,
239
vCard.parseLine( buffer, line,
241
240
cli.doesNextLineLookFolded() );
243
catch( Vcard.ParseException e ) {
242
catch( VCard.ParseException e ) {
245
244
if( !showContinue(
246
245
getText( R.string.error_vcf_parse ).toString()
252
251
// although we're continuing, we still need to abort
253
252
// this vCard. Further lines will be ignored until we
254
253
// get to another BEGIN:VCARD line.
257
catch( Vcard.SkipImportException e ) {
256
catch( VCard.SkipContactException e ) {
259
258
// abort this vCard. Further lines will be ignored until
260
259
// we get to another BEGIN:VCARD line.
331
private class Vcard extends ContactData
330
private class VCard extends ContactData
333
332
private final static int NAMELEVEL_NONE = 0;
334
private final static int NAMELEVEL_FN = 1;
335
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;
337
337
private final static int MULTILINE_NONE = 0;
338
338
private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
345
345
private int _parser_multiline_state = MULTILINE_NONE;
346
346
private String _parser_current_name_and_params = null;
347
347
private String _parser_buffered_value_so_far = "";
348
private String _cached_organisation = null;
349
private String _cached_title = null;
351
349
protected class UnencodeResult
383
381
public ParseException( int res )
385
super( VcardImporter.this.getText( res ).toString() );
383
super( VCFImporter.this.getText( res ).toString() );
389
387
@SuppressWarnings("serial")
390
protected class SkipImportException extends Exception { }
388
protected class SkipContactException extends Exception { }
392
390
private String extractCollonPartFromLine( ByteBuffer buffer,
393
391
String line, boolean former )
430
428
public void parseLine( ByteBuffer buffer, String line,
431
429
boolean next_line_looks_folded )
432
throws ParseException, SkipImportException,
430
throws ParseException, SkipContactException,
433
431
AbortImportException
435
433
// do we have a version yet?
623
621
parseFN( name_param_parts, complete_value );
624
622
else if( name_param_parts[ 0 ].equals( "ORG" ) )
625
623
parseORG( name_param_parts, complete_value );
626
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
627
parseTITLE( name_param_parts, complete_value );
628
624
else if( name_param_parts[ 0 ].equals( "TEL" ) )
629
625
parseTEL( name_param_parts, complete_value );
630
626
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
705
703
setName( value );
706
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();
709
712
private void parseFN( String[] params, String value )
713
throws ParseException, SkipContactException
711
715
// already got a better name?
712
716
if( _name_level >= NAMELEVEL_FN ) return;
719
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;
722
730
String[] org_parts = splitValueBySemicolon( value );
723
if( org_parts == null || org_parts.length < 1 ) return;
725
// build organisation name
726
StringBuilder builder = new StringBuilder(
727
String.valueOf( org_parts[ 0 ] ) );
728
for( int a = 1; a < org_parts.length; a++ )
729
builder.append( ", " ).append( org_parts[ a ] );
730
String organisation = builder.toString();
732
// set organisation name (using a title we've previously found)
733
addOrganisation( organisation, _cached_title, true );
735
// if we've not previously found a title, store this organisation
736
// name (we'll need it when we find a title to update the
737
// organisation, by name), else if we *have* previously found a
738
// title, clear it (since we just used it)
739
if( _cached_title == null )
740
_cached_organisation = organisation;
742
_cached_title = null;
745
private void parseTITLE( String[] params, String value )
747
// if we previously had an organisation, look it up and append this
749
if( _cached_organisation != null && hasOrganisations() ) {
750
HashMap< String, ExtraDetail > datas = getOrganisations();
751
ExtraDetail detail = datas.get( _cached_organisation );
753
detail.setExtra( value );
756
// same as when handling organisation, if we've not previously found
757
// an organisation we store this title, else we clear it (since we
758
// just appended this title to it)
759
if( _cached_organisation == null )
760
_cached_title = value;
762
_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;
765
745
private void parseTEL( String[] params, String value )
746
throws ParseException
767
748
if( value.length() == 0 ) return;
771
752
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
773
754
// here's the logic...
774
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;
776
764
if( types.contains( "FAX" ) )
777
765
if( types.contains( "HOME" ) )
778
766
type = PhonesColumns.TYPE_FAX_HOME;
780
768
type = PhonesColumns.TYPE_FAX_WORK;
781
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
782
type = PhonesColumns.TYPE_MOBILE;
783
else if( types.contains( "PAGER" ) )
769
if( types.contains( "PAGER" ) )
784
770
type = PhonesColumns.TYPE_PAGER;
785
else if( types.contains( "WORK" ) )
786
type = PhonesColumns.TYPE_WORK;
788
type = PhonesColumns.TYPE_HOME;
790
772
// add phone number
791
addNumber( value, type, is_preferred );
773
addPhone( value, type, preferred );
794
776
public void parseEMAIL( String[] params, String value )
777
throws ParseException
796
779
if( value.length() == 0 ) return;
799
782
"PREF", "WORK", "HOME", "INTERNET" ) );
801
784
// add email address
802
boolean is_preferred = types.contains( "PREF" );
785
boolean preferred = types.contains( "PREF" );
804
786
if( types.contains( "WORK" ) )
805
type = Contacts.ContactMethods.TYPE_WORK;
787
addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
807
type = Contacts.ContactMethods.TYPE_HOME;
809
addEmail( value, type, is_preferred );
789
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
812
792
private void parseADR( String[] params, String value )
793
throws ParseException, SkipContactException
814
795
// get address parts
815
796
String[] adr_parts = splitValueBySemicolon( value );
825
806
"PREF", "WORK", "HOME", "INTERNET" ) );
829
809
if( types.contains( "WORK" ) )
830
type = Contacts.ContactMethods.TYPE_WORK;
810
addAddress( value, Contacts.ContactMethods.TYPE_WORK );
832
type = Contacts.ContactMethods.TYPE_HOME;
834
addAddress( value, type );
812
addAddress( value, Contacts.ContactMethods.TYPE_HOME);
837
public void finaliseVcard()
838
throws ParseException
815
public void finaliseParsing()
816
throws ParseException, SkipContactException,
840
819
// missing version (and data is present)
841
820
if( _version == null && _buffers != null )
842
821
throw new ParseException( R.string.error_vcf_malformed );
844
// finalise the parent class
848
catch( ContactNotIdentifiableException e ) {
849
throw new ParseException( R.string.error_vcf_notenoughinfo );
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();
853
834
private String checkParam( String[] params, String name )