/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2012-04-24 09:56:17 UTC
  • Revision ID: tim@ed.am-20120424095617-lsfkkpkrlqk9xthn
updated all URLs, email addresses and package names to ed.am

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * VCFImporter.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
26
26
import java.io.BufferedReader;
27
27
import java.io.File;
116
116
                setProgress( 0 );
117
117
                for( int i = 0; i < files.length; i++ )
118
118
                        importVCardFile( files[ i ] );
 
119
                setProgress( _vcard_count );
119
120
        }
120
121
 
121
122
        private void countVCardFile( File file ) throws AbortImportException
168
169
                        FileInputStream istream = new FileInputStream( file );
169
170
                        byte[] content = new byte[ (int)file.length() ];
170
171
                        istream.read( content );
 
172
                        istream = null;
171
173
 
172
174
                        // import
173
175
                        importVCardFileContent( content, file.getName() );
186
188
        {
187
189
                // go through lines
188
190
                Vcard vcard = null;
 
191
                int vcard_start_line = 0;
189
192
                ContentLineIterator cli = new ContentLineIterator( content );
190
193
                while( cli.hasNext() )
191
194
                {
205
208
                        if( vcard == null ) {
206
209
                                // look for vcard beginning
207
210
                                if( line.matches( "^BEGIN:VCARD" ) ) {
208
 
                                        setProgress( ++_progress );
 
211
                                        setProgress( _progress++ );
209
212
                                        vcard = new Vcard();
 
213
                                        vcard_start_line = cli.getLineNumber();
210
214
                                }
211
215
                        }
212
216
                        else {
213
217
                                // look for vcard content or ending
214
218
                                if( line.matches( "^END:VCARD" ) )
215
219
                                {
216
 
                                        // store vcard and do away with it
 
220
                                        // finalise the vcard/contact
217
221
                                        try {
218
 
                                                vcard.finaliseParsing();
 
222
                                                vcard.finaliseVcard();
 
223
 
 
224
                                                // pass the finalised contact to the importer
219
225
                                                importContact( vcard );
220
226
                                        }
221
227
                                        catch( Vcard.ParseException e ) {
222
 
                                                skipContact();
223
 
                                                if( !showContinue(
224
 
                                                        getText( R.string.error_vcf_parse ).toString()
225
 
                                                        + fileName + "\n" + e.getMessage() ) )
226
 
                                                {
227
 
                                                        finish( ACTION_ABORT );
228
 
                                                }
229
 
                                        }
230
 
                                        catch( Vcard.SkipContactException e ) {
231
 
                                                skipContact();
232
 
                                                // do nothing
233
 
                                        }
 
228
                                                if( !showContinue(
 
229
                                                        getText( R.string.error_vcf_parse ).toString()
 
230
                                                        + fileName +
 
231
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
232
                                                        + cli.getLineNumber() + ":\n" + e.getMessage() ) )
 
233
                                                {
 
234
                                                        finish( ACTION_ABORT );
 
235
                                                }
 
236
                                                else
 
237
                                                        skipContact();
 
238
                                        }
 
239
                                        catch( ContactData.ContactNotIdentifiableException e ) {
 
240
                                                if( !showContinue(
 
241
                                                        getText( R.string.error_vcf_parse ).toString()
 
242
                                                        + fileName +
 
243
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
244
                                                        + vcard_start_line + ":\n" + getText(
 
245
                                                                R.string.error_vcf_notenoughinfo ).toString()
 
246
                                                ) )
 
247
                                                {
 
248
                                                        finish( ACTION_ABORT );
 
249
                                                }
 
250
                                                else
 
251
                                                        skipContact();
 
252
                                        }
 
253
 
 
254
                                        // discard this vcard
234
255
                                        vcard = null;
235
256
                                }
236
257
                                else
244
265
                                                skipContact();
245
266
                                                if( !showContinue(
246
267
                                                        getText( R.string.error_vcf_parse ).toString()
247
 
                                                        + fileName + "\n" + e.getMessage() ) )
 
268
                                                        + fileName +
 
269
                                                        getText( R.string.error_vcf_parse_line ).toString()
 
270
                                                        + cli.getLineNumber() + "\n" + e.getMessage() ) )
248
271
                                                {
249
272
                                                        finish( ACTION_ABORT );
250
273
                                                }
254
277
                                                // get to another BEGIN:VCARD line.
255
278
                                                vcard = null;
256
279
                                        }
257
 
                                        catch( Vcard.SkipContactException e ) {
 
280
                                        catch( Vcard.SkipImportException e ) {
258
281
                                                skipContact();
259
282
                                                // abort this vCard. Further lines will be ignored until
260
283
                                                // we get to another BEGIN:VCARD line.
269
292
        {
270
293
                protected byte[] _content = null;
271
294
                protected int _pos = 0;
 
295
                protected int _line = 0;
272
296
 
273
297
                public ContentLineIterator( byte[] content )
274
298
                {
294
318
                                        int to = ( _pos > 0 && _content[ _pos - 1 ] == '\r' &&
295
319
                                                _pos > initial_pos )? _pos - 1 : _pos;
296
320
                                        _pos++;
 
321
                                        _line++;
297
322
                                        return ByteBuffer.wrap( _content, initial_pos,
298
323
                                                to - initial_pos );
299
324
                                }
302
327
                        if( _pos != initial_pos ) {
303
328
                                int to = _pos;
304
329
                                _pos++;
 
330
                                _line++;
305
331
                                return ByteBuffer.wrap( _content, initial_pos,
306
332
                                        to - initial_pos );
307
333
                        }
326
352
                        return _pos > 0 && _pos < _content.length &&
327
353
                                _content[ _pos - 1 ] == '\n' && _content[ _pos ] == ' ';
328
354
                }
 
355
 
 
356
                public int getLineNumber()
 
357
                {
 
358
                        return _line;
 
359
                }
329
360
        }
330
361
 
331
362
        private class Vcard extends ContactData
332
363
        {
333
364
                private final static int NAMELEVEL_NONE = 0;
334
 
                private final static int NAMELEVEL_FN = 1;
335
 
                private final static int NAMELEVEL_N = 2;
 
365
                private final static int NAMELEVEL_N = 1;
 
366
                private final static int NAMELEVEL_FN = 2;
336
367
 
337
368
                private final static int MULTILINE_NONE = 0;
338
369
                private final static int MULTILINE_ENCODED = 1; // v2.1 quoted-printable
387
418
                }
388
419
 
389
420
                @SuppressWarnings("serial")
390
 
                protected class SkipContactException extends Exception { }
 
421
                protected class SkipImportException extends Exception { }
391
422
 
392
423
                private String extractCollonPartFromLine( ByteBuffer buffer,
393
424
                        String line, boolean former )
429
460
 
430
461
                public void parseLine( ByteBuffer buffer, String line,
431
462
                        boolean next_line_looks_folded )
432
 
                        throws ParseException, SkipContactException,
 
463
                        throws ParseException, SkipImportException,
433
464
                        AbortImportException
434
465
                {
435
466
                        // do we have a version yet?
535
566
                                for( int i = 0; i < name_param_parts.length; i++ )
536
567
                                        name_param_parts[ i ] = name_param_parts[ i ].trim();
537
568
 
 
569
                                // determine whether we care about this entry
 
570
                                final HashSet< String > interesting_fields =
 
571
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
572
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
573
                                ) );
 
574
                                boolean is_interesting_field =
 
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
 
576
 
538
577
                                // parse encoding parameter
539
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
540
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
541
 
                                if( encoding != null && !encoding.equals( "8BIT" ) &&
 
580
                                if( is_interesting_field && encoding != null &&
 
581
                                        !encoding.equals( "8BIT" ) &&
542
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
543
583
                                        //&& !encoding.equals( "BASE64" ) )
544
584
                                {
548
588
                                // parse charset parameter
549
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
550
590
                                if( charset != null ) charset = charset.toUpperCase();
551
 
                                if( charset != null && !charset.equals( "US-ASCII" ) &&
 
591
                                if( charset != null &&
 
592
                                        !charset.equals( "US-ASCII" ) &&
552
593
                                        !charset.equals( "ASCII" ) &&
553
594
                                        !charset.equals( "UTF-8" ) )
554
595
                                {
568
609
                                                _parser_multiline_state = MULTILINE_ENCODED;
569
610
                                }
570
611
 
571
 
                                // convert 8-bit ASCII charset to US-ASCII
572
 
                                if( charset == null || charset.equals( "ASCII" ) ) {
 
612
                                // convert 8-bit US-ASCII charset to UTF-8 (where no charset is
 
613
                                // specified for a v2.1 vcard entry, we assume it's US-ASCII)
 
614
                                if( ( charset == null && _version.equals( "2.1" ) ) ||
 
615
                                        ( charset != null && (
 
616
                                                charset.equals( "ASCII" ) ||
 
617
                                                charset.equals( "US-ASCII" ) ) ) )
 
618
                                {
573
619
                                        value = transcodeAsciiToUtf8( value );
574
 
                                        charset = "UTF-8";
575
620
                                }
576
621
 
577
 
                                // process charset
 
622
                                // process charset (value is now in UTF-8)
578
623
                                String string_value;
579
624
                                try {
580
625
                                        string_value = new String( value.array(), value.position(),
581
 
                                                value.limit() - value.position(), charset );
 
626
                                                value.limit() - value.position(), "UTF-8" );
582
627
                                } catch( UnsupportedEncodingException e ) {
583
628
                                        throw new ParseException( R.string.error_vcf_charset );
584
629
                                }
631
676
                                        parseEMAIL( name_param_parts, complete_value );
632
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
633
678
                                        parseADR( name_param_parts, complete_value );
 
679
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
680
                                        parseLABEL( name_param_parts, complete_value );
634
681
                        }
635
682
                }
636
683
 
649
696
                        return ( count & 1 ) == 1;
650
697
                }
651
698
 
652
 
                private String[] splitValueBySemicolon( String value )
 
699
                private String[] splitValueByCharacter( String value, char character )
653
700
                {
654
 
                        // split string in to parts by semicolon
 
701
                        // split string in to parts by specified character
655
702
                        ArrayList< String > parts = new ArrayList< String >(
656
 
                                Arrays.asList( value.split(  ";" ) ) );
 
703
                                Arrays.asList( value.split( "" + character ) ) );
657
704
 
658
705
                        // go through parts
659
706
                        for( int a = 0; a < parts.size(); a++ )
667
714
                                if( a < parts.size() - 1 &&
668
715
                                        doesStringEndInAnEscapeChar( str ) )
669
716
                                {
670
 
                                        // join the next part to this part and remove the next part
 
717
                                        // append the escaped character, join the next part to this
 
718
                                        // part and remove the next part
671
719
                                        parts.set( a, str.substring( 0, str.length() - 1 ) +
672
 
                                                ';' + parts.get( a + 1 ) );
 
720
                                                character + parts.get( a + 1 ) );
673
721
                                        parts.remove( a + 1 );
674
722
 
675
723
                                        // re-visit this part
686
734
                        return parts.toArray( ret );
687
735
                }
688
736
 
 
737
                private String unescapeValue( String value )
 
738
                {
 
739
                        StringBuilder ret = new StringBuilder( value.length() );
 
740
                        boolean in_escape = false;
 
741
                        for( int a = 0; a < value.length(); a++ )
 
742
                        {
 
743
                                int c = value.codePointAt( a );
 
744
 
 
745
                                // process a normal character
 
746
                                if( !in_escape ) {
 
747
                                        if( c == '\\' )
 
748
                                                in_escape = true;
 
749
                                        else
 
750
                                                ret.append( Character.toChars( c ) );
 
751
                                        continue;
 
752
                                }
 
753
 
 
754
                                // process an escape sequence
 
755
                                in_escape = false;
 
756
                                switch( c )
 
757
                                {
 
758
                                case 'N':
 
759
                                case 'n':
 
760
                                        // add newline
 
761
                                        ret.append( '\n' );
 
762
                                        break;
 
763
                                case '\\':
 
764
                                case ',':
 
765
                                case ';':
 
766
                                        // add escaped character
 
767
                                        ret.append( Character.toChars( c ) );
 
768
                                        break;
 
769
                                default:
 
770
                                        // unknown escape sequence, so add it unescaped
 
771
                                        ret.append( "\\" );
 
772
                                        ret.append( Character.toChars( c ) );
 
773
                                        break;
 
774
                                }
 
775
                        }
 
776
 
 
777
                        return ret.toString();
 
778
                }
 
779
 
689
780
                private void parseN( String[] params, String value )
690
781
                {
691
782
                        // already got a better name?
692
783
                        if( _name_level >= NAMELEVEL_N ) return;
693
784
 
694
785
                        // get name parts
695
 
                        String[] name_parts = splitValueBySemicolon( value );
 
786
                        String[] name_parts = splitValueByCharacter( value, ';' );
696
787
 
697
788
                        // build name
698
789
                        value = "";
699
 
                        if( name_parts.length > 1 && name_parts[ 1 ].length() > 0 )
700
 
                                value += name_parts[ 1 ];
701
 
                        if( name_parts.length > 0 && name_parts[ 0 ].length() > 0 )
702
 
                                value += ( value.length() == 0? "" : " " ) + name_parts[ 0 ];
 
790
                        final int[] part_order = { 3, 1, 2, 0, 4 };
 
791
                        for( int a = 0; a < part_order.length; a++ )
 
792
                                if( name_parts.length > part_order[ a ] &&
 
793
                                        name_parts[ part_order[ a ] ].length() > 0 )
 
794
                                {
 
795
                                        // split this part in to it's comma-separated bits
 
796
                                        String[] name_part_parts = splitValueByCharacter(
 
797
                                                name_parts[ part_order[ a ] ], ',' );
 
798
                                        for( int b = 0; b < name_part_parts.length; b++ )
 
799
                                                if( name_part_parts[ b ].length() > 0 )
 
800
                                                {
 
801
                                                        if( value.length() == 0 ) value += " ";
 
802
                                                        value += name_part_parts[ b ];
 
803
                                                }
 
804
                                }
703
805
 
704
806
                        // set name
705
 
                        setName( value );
 
807
                        setName( unescapeValue( value ) );
706
808
                        _name_level = NAMELEVEL_N;
707
809
                }
708
810
 
712
814
                        if( _name_level >= NAMELEVEL_FN ) return;
713
815
 
714
816
                        // set name
715
 
                        setName( value );
 
817
                        setName( unescapeValue( value ) );
716
818
                        _name_level = NAMELEVEL_FN;
717
819
                }
718
820
 
719
821
                private void parseORG( String[] params, String value )
720
822
                {
721
823
                        // get org parts
722
 
                        String[] org_parts = splitValueBySemicolon( value );
 
824
                        String[] org_parts = splitValueByCharacter( value, ';' );
723
825
                        if( org_parts == null || org_parts.length < 1 ) return;
724
826
 
725
827
                        // build organisation name
727
829
                                String.valueOf( org_parts[ 0 ] ) );
728
830
                        for( int a = 1; a < org_parts.length; a++ )
729
831
                                builder.append( ", " ).append( org_parts[ a ] );
730
 
                        String organisation = builder.toString();
 
832
                        String organisation = unescapeValue( builder.toString() );
731
833
 
732
834
                        // set organisation name (using a title we've previously found)
733
835
                        addOrganisation( organisation, _cached_title, true );
744
846
 
745
847
                private void parseTITLE( String[] params, String value )
746
848
                {
 
849
                        value = unescapeValue( value );
 
850
 
747
851
                        // if we previously had an organisation, look it up and append this
748
852
                        // title to it
749
853
                        if( _cached_organisation != null && hasOrganisations() ) {
806
910
                        else
807
911
                                type = Contacts.ContactMethods.TYPE_HOME;
808
912
 
809
 
                        addEmail( value, type, is_preferred );
 
913
                        addEmail( unescapeValue( value ), type, is_preferred );
810
914
                }
811
915
 
812
916
                private void parseADR( String[] params, String value )
813
917
                {
814
918
                        // get address parts
815
 
                        String[] adr_parts = splitValueBySemicolon( value );
 
919
                        String[] adr_parts = splitValueByCharacter( value, ';' );
816
920
 
817
921
                        // build address
818
922
                        value = "";
819
 
                        for( int a = 0; a < adr_parts.length; a++ ) {
820
 
                                if( value.length() > 0 ) value += "\n";
821
 
                                value += adr_parts[ a ].trim();
822
 
                        }
823
 
 
824
 
                        Set< String > types = extractTypes( params, Arrays.asList(
825
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
826
 
 
827
 
                        // add address
828
 
                        int type;
829
 
                        if( types.contains( "WORK" ) )
830
 
                                type = Contacts.ContactMethods.TYPE_WORK;
831
 
                        else
832
 
                                type = Contacts.ContactMethods.TYPE_HOME;
833
 
 
834
 
                        addAddress( value, type );
835
 
                }
836
 
 
837
 
                public void finaliseParsing()
838
 
                        throws ParseException, SkipContactException,
839
 
                        AbortImportException
 
923
                        for( int a = 0; a < adr_parts.length; a++ )
 
924
                                if( adr_parts[ a ].length() > 0 )
 
925
                                {
 
926
                                        // split this part in to it's comma-separated bits
 
927
                                        String[] adr_part_parts =
 
928
                                                splitValueByCharacter( adr_parts[ a ], ',' );
 
929
                                        for( int b = 0; b < adr_part_parts.length; b++ )
 
930
                                                if( adr_part_parts[ b ].length() > 0 )
 
931
                                                {
 
932
                                                        if( value.length() > 0 ) value += "\n";
 
933
                                                        value += adr_part_parts[ b ];
 
934
                                                }
 
935
                                }
 
936
 
 
937
                        Set< String > types = extractTypes( params, Arrays.asList(
 
938
                                "PREF", "WORK", "HOME" ) );
 
939
 
 
940
                        // add address
 
941
                        int type;
 
942
                        if( types.contains( "WORK" ) )
 
943
                                type = Contacts.ContactMethods.TYPE_WORK;
 
944
                        else
 
945
                                type = Contacts.ContactMethods.TYPE_HOME;
 
946
 
 
947
                        addAddress( unescapeValue( value ), type );
 
948
                }
 
949
 
 
950
                private void parseLABEL( String[] params, String value )
 
951
                {
 
952
                        Set< String > types = extractTypes( params, Arrays.asList(
 
953
                                "PREF", "WORK", "HOME" ) );
 
954
 
 
955
                        // add address
 
956
                        int type;
 
957
                        if( types.contains( "WORK" ) )
 
958
                                type = Contacts.ContactMethods.TYPE_WORK;
 
959
                        else
 
960
                                type = Contacts.ContactMethods.TYPE_HOME;
 
961
 
 
962
                        addAddress( unescapeValue( value ), type );
 
963
                }
 
964
 
 
965
                public void finaliseVcard()
 
966
                        throws ParseException, ContactNotIdentifiableException
840
967
                {
841
968
                        // missing version (and data is present)
842
969
                        if( _version == null && _buffers != null )
843
970
                                throw new ParseException( R.string.error_vcf_malformed );
844
971
 
845
 
                        // check if we should import this contact
846
 
                        try {
847
 
                                if( !isImportRequired( this ) )
848
 
                                        throw new SkipContactException();
849
 
                        }
850
 
                        catch( ContactNeedsMoreInfoException e ) {
851
 
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
852
 
                        }
 
972
                        // finalise the parent class
 
973
                        finalise();
853
974
                }
854
975
 
 
976
                /**
 
977
                 * Amongst the params, find the value of the first, only, of any with
 
978
                 * the specified name
 
979
                 * @param params
 
980
                 * @param name
 
981
                 * @return a value, or null
 
982
                 */
855
983
                private String checkParam( String[] params, String name )
856
984
                {
 
985
                        String[] res = checkParams( params, name );
 
986
                        return res.length > 0? res[ 0 ] : null;
 
987
                }
 
988
 
 
989
                /**
 
990
                 * Amongst the params, find the values of any with the specified name
 
991
                 * @param params
 
992
                 * @param name
 
993
                 * @return an array of values, or null
 
994
                 */
 
995
                private String[] checkParams( String[] params, String name )
 
996
                {
 
997
                        HashSet< String > ret = new HashSet< String >();
 
998
 
857
999
                        Pattern p = Pattern.compile(
858
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
859
1001
                        for( int i = 0; i < params.length; i++ ) {
860
1002
                                Matcher m = p.matcher( params[ i ] );
861
1003
                                if( m.matches() )
862
 
                                        return m.group( 2 );
 
1004
                                        ret.add( m.group( 2 ) );
863
1005
                        }
864
 
                        return null;
 
1006
 
 
1007
                        return (String[]) ret.toArray( new String[ ret.size() ] );
865
1008
                }
866
1009
 
 
1010
                /**
 
1011
                 * Amongst the params, return any type values present. For v2.1 vCards,
 
1012
                 * those types are just parameters. For v3.0, they are prefixed with
 
1013
                 * "TYPE=". There may also be multiple type parameters.
 
1014
                 * @param params
 
1015
                 * @param a list of type values to look for
 
1016
                 * @return a set of present type values
 
1017
                 */
867
1018
                private Set< String > extractTypes( String[] params,
868
1019
                        List< String > valid_types )
869
1020
                {
870
1021
                        HashSet< String > types = new HashSet< String >();
871
1022
 
872
1023
                        // get 3.0-style TYPE= param
873
 
                        String type_param;
874
 
                        if( ( type_param = checkParam( params, "TYPE" ) ) != null ) {
875
 
                                String[] parts = type_param.split( "," );
 
1024
                        String type_params[] = checkParams( params, "TYPE" );
 
1025
                        for( int a = 0; a < type_params.length; a++ )
 
1026
                        {
 
1027
                                // check for a comma-separated list of types (why? this isn't in
 
1028
                                // the specs!)
 
1029
                                String[] parts = type_params[ a ].split( "," );
876
1030
                                for( int i = 0; i < parts.length; i++ )
877
1031
                                        if( valid_types.contains( parts[ i ] ) )
878
1032
                                                types.add( parts[ i ] );