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 )
204
if( vCard == null ) {
205
if( vcard == null ) {
205
206
// look for vcard beginning
206
207
if( line.matches( "^BEGIN:VCARD" ) ) {
207
208
setProgress( ++_progress );
212
213
// look for vcard content or ending
213
214
if( line.matches( "^END:VCARD" ) )
215
// store vcard and do away with it
216
// finalise the vcard/contact
217
vCard.finaliseParsing();
218
importContact( vCard );
218
vcard.finaliseVcard();
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 );
229
catch( VCard.SkipContactException e ) {
230
// pass the finalised contact to the importer
231
importContact( vcard );
237
238
// try giving the line to the vcard
239
vCard.parseLine( buffer, line,
240
vcard.parseLine( buffer, line,
240
241
cli.doesNextLineLookFolded() );
242
catch( VCard.ParseException e ) {
243
catch( Vcard.ParseException e ) {
244
245
if( !showContinue(
245
246
getText( R.string.error_vcf_parse ).toString()
251
252
// although we're continuing, we still need to abort
252
253
// this vCard. Further lines will be ignored until we
253
254
// get to another BEGIN:VCARD line.
256
catch( VCard.SkipContactException e ) {
257
catch( Vcard.SkipImportException e ) {
258
259
// abort this vCard. Further lines will be ignored until
259
260
// we get to another BEGIN:VCARD line.
330
private class VCard extends ContactData
331
private class Vcard extends ContactData
332
333
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;
334
private final static int NAMELEVEL_FN = 1;
335
private final static int NAMELEVEL_N = 2;
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;
349
351
protected class UnencodeResult
381
383
public ParseException( int res )
383
super( VCFImporter.this.getText( res ).toString() );
385
super( VcardImporter.this.getText( res ).toString() );
387
389
@SuppressWarnings("serial")
388
protected class SkipContactException extends Exception { }
390
protected class SkipImportException extends Exception { }
390
392
private String extractCollonPartFromLine( ByteBuffer buffer,
391
393
String line, boolean former )
428
430
public void parseLine( ByteBuffer buffer, String line,
429
431
boolean next_line_looks_folded )
430
throws ParseException, SkipContactException,
432
throws ParseException, SkipImportException,
431
433
AbortImportException
433
435
// do we have a version yet?
621
623
parseFN( name_param_parts, complete_value );
622
624
else if( name_param_parts[ 0 ].equals( "ORG" ) )
623
625
parseORG( name_param_parts, complete_value );
626
else if( name_param_parts[ 0 ].equals( "TITLE" ) )
627
parseTITLE( name_param_parts, complete_value );
624
628
else if( name_param_parts[ 0 ].equals( "TEL" ) )
625
629
parseTEL( name_param_parts, complete_value );
626
630
else if( name_param_parts[ 0 ].equals( "EMAIL" ) )
703
705
setName( value );
704
706
_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
709
private void parseFN( String[] params, String value )
713
throws ParseException, SkipContactException
715
711
// already got a better name?
716
712
if( _name_level >= NAMELEVEL_FN ) return;
723
719
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
722
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;
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;
745
765
private void parseTEL( String[] params, String value )
746
throws ParseException
748
767
if( value.length() == 0 ) return;
752
771
"PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
754
773
// 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;
774
boolean is_preferred = types.contains( "PREF" );
764
776
if( types.contains( "FAX" ) )
765
777
if( types.contains( "HOME" ) )
766
778
type = PhonesColumns.TYPE_FAX_HOME;
768
780
type = PhonesColumns.TYPE_FAX_WORK;
769
if( types.contains( "PAGER" ) )
781
else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
782
type = PhonesColumns.TYPE_MOBILE;
783
else if( types.contains( "PAGER" ) )
770
784
type = PhonesColumns.TYPE_PAGER;
785
else if( types.contains( "WORK" ) )
786
type = PhonesColumns.TYPE_WORK;
788
type = PhonesColumns.TYPE_HOME;
772
790
// add phone number
773
addPhone( value, type, preferred );
791
addNumber( value, type, is_preferred );
776
794
public void parseEMAIL( String[] params, String value )
777
throws ParseException
779
796
if( value.length() == 0 ) return;
782
799
"PREF", "WORK", "HOME", "INTERNET" ) );
784
801
// add email address
785
boolean preferred = types.contains( "PREF" );
802
boolean is_preferred = types.contains( "PREF" );
786
804
if( types.contains( "WORK" ) )
787
addEmail( value, Contacts.ContactMethods.TYPE_WORK, preferred );
805
type = Contacts.ContactMethods.TYPE_WORK;
789
addEmail( value, Contacts.ContactMethods.TYPE_HOME, preferred );
807
type = Contacts.ContactMethods.TYPE_HOME;
809
addEmail( value, type, is_preferred );
792
812
private void parseADR( String[] params, String value )
793
throws ParseException, SkipContactException
795
814
// get address parts
796
815
String[] adr_parts = splitValueBySemicolon( value );
806
825
"PREF", "WORK", "HOME", "INTERNET" ) );
809
829
if( types.contains( "WORK" ) )
810
addAddress( value, Contacts.ContactMethods.TYPE_WORK );
830
type = Contacts.ContactMethods.TYPE_WORK;
812
addAddress( value, Contacts.ContactMethods.TYPE_HOME);
832
type = Contacts.ContactMethods.TYPE_HOME;
834
addAddress( value, type );
815
public void finaliseParsing()
816
throws ParseException, SkipContactException,
837
public void finaliseVcard()
838
throws ParseException
819
840
// missing version (and data is present)
820
841
if( _version == null && _buffers != null )
821
842
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();
844
// finalise the parent class
848
catch( ContactNotIdentifiableException e ) {
849
throw new ParseException( R.string.error_vcf_notenoughinfo );
834
853
private String checkParam( String[] params, String name )