/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-12-19 17:41:29 UTC
  • Revision ID: tim@ed.am-20121219174129-41i7vrz0jviideqi
fix intro strings

Show diffs side-by-side

added added

removed removed

38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
41
 
import java.util.Locale;
42
41
import java.util.NoSuchElementException;
43
42
import java.util.Set;
44
43
import java.util.Vector;
45
44
import java.util.regex.Matcher;
46
45
import java.util.regex.Pattern;
47
46
 
48
 
import android.annotation.SuppressLint;
49
47
import android.content.SharedPreferences;
 
48
import android.provider.Contacts;
 
49
import android.provider.Contacts.PhonesColumns;
50
50
 
51
51
public class VcardImporter extends Importer
52
52
{
58
58
                super( doit );
59
59
        }
60
60
 
61
 
        @SuppressLint( "SdCardPath" )
62
61
        @Override
63
62
        protected void onImport() throws AbortImportException
64
63
        {
83
82
                                // get files
84
83
                                class VCardFilter implements FilenameFilter {
85
84
                                        public boolean accept( File dir, String name ) {
86
 
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
 
85
                                                return name.toLowerCase().endsWith( ".vcf" );
87
86
                                        }
88
87
                                }
89
88
                                files = file.listFiles( new VCardFilter() );
577
576
 
578
577
                                // parse encoding parameter
579
578
                                String encoding = checkParam( name_param_parts, "ENCODING" );
580
 
                                if( encoding != null )
581
 
                                        encoding = encoding.toUpperCase( Locale.US );
 
579
                                if( encoding != null ) encoding = encoding.toUpperCase();
582
580
                                if( is_interesting_field && encoding != null &&
583
581
                                        !encoding.equals( "8BIT" ) &&
584
582
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
589
587
 
590
588
                                // parse charset parameter
591
589
                                String charset = checkParam( name_param_parts, "CHARSET" );
592
 
                                if( charset != null )
593
 
                                        charset = charset.toUpperCase( Locale.US );
 
590
                                if( charset != null ) charset = charset.toUpperCase();
594
591
                                if( charset != null &&
595
592
                                        !charset.equals( "US-ASCII" ) &&
596
593
                                        !charset.equals( "ASCII" ) &&
681
678
                                        parseADR( name_param_parts, complete_value );
682
679
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
683
680
                                        parseLABEL( name_param_parts, complete_value );
684
 
                                else if( name_param_parts[ 0 ].equals( "NOTE" ) )
685
 
                                        parseNOTE( name_param_parts, complete_value );
686
681
                        }
687
682
                }
688
683
 
760
755
                                in_escape = false;
761
756
                                switch( c )
762
757
                                {
763
 
                                case 'T':
764
 
                                case 't':
765
 
                                        // add tab (invalid/non-standard, but accepted)
766
 
                                        ret.append( '\t' );
767
 
                                        break;
768
758
                                case 'N':
769
759
                                case 'n':
770
760
                                        // add newline
778
768
                                        break;
779
769
                                default:
780
770
                                        // unknown escape sequence, so add it unescaped
781
 
                                        // (invalid/non-standard, but accepted)
782
771
                                        ret.append( "\\" );
783
772
                                        ret.append( Character.toChars( c ) );
784
773
                                        break;
890
879
                        int type;
891
880
                        if( types.contains( "FAX" ) )
892
881
                                if( types.contains( "HOME" ) )
893
 
                                        type = TYPE_FAX_HOME;
 
882
                                        type = PhonesColumns.TYPE_FAX_HOME;
894
883
                                else
895
 
                                        type = TYPE_FAX_WORK;
 
884
                                        type = PhonesColumns.TYPE_FAX_WORK;
896
885
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
897
 
                                type = TYPE_MOBILE;
 
886
                                type = PhonesColumns.TYPE_MOBILE;
898
887
                        else if( types.contains( "PAGER" ) )
899
 
                                type = TYPE_PAGER;
 
888
                                type = PhonesColumns.TYPE_PAGER;
900
889
                        else if( types.contains( "WORK" ) )
901
 
                                type = TYPE_WORK;
 
890
                                type = PhonesColumns.TYPE_WORK;
902
891
                        else
903
 
                                type = TYPE_HOME;
 
892
                                type = PhonesColumns.TYPE_HOME;
904
893
 
905
894
                        // add phone number
906
895
                        addNumber( value, type, is_preferred );
917
906
                        boolean is_preferred = types.contains( "PREF" );
918
907
                        int type;
919
908
                        if( types.contains( "WORK" ) )
920
 
                                type = TYPE_WORK;
 
909
                                type = Contacts.ContactMethods.TYPE_WORK;
921
910
                        else
922
 
                                type = TYPE_HOME;
 
911
                                type = Contacts.ContactMethods.TYPE_HOME;
923
912
 
924
913
                        addEmail( unescapeValue( value ), type, is_preferred );
925
914
                }
951
940
                        // add address
952
941
                        int type;
953
942
                        if( types.contains( "WORK" ) )
954
 
                                type = TYPE_WORK;
 
943
                                type = Contacts.ContactMethods.TYPE_WORK;
955
944
                        else
956
 
                                type = TYPE_HOME;
 
945
                                type = Contacts.ContactMethods.TYPE_HOME;
957
946
 
958
947
                        addAddress( unescapeValue( value ), type );
959
948
                }
966
955
                        // add address
967
956
                        int type;
968
957
                        if( types.contains( "WORK" ) )
969
 
                                type = TYPE_WORK;
 
958
                                type = Contacts.ContactMethods.TYPE_WORK;
970
959
                        else
971
 
                                type = TYPE_HOME;
 
960
                                type = Contacts.ContactMethods.TYPE_HOME;
972
961
 
973
962
                        addAddress( unescapeValue( value ), type );
974
963
                }
975
964
 
976
 
                private void parseNOTE( String[] params, String value )
977
 
                {
978
 
                        addNote( unescapeValue( value ) );
979
 
                }
980
 
 
981
965
                public void finaliseVcard()
982
966
                        throws ParseException, ContactNotIdentifiableException
983
967
                {
1013
997
                        HashSet< String > ret = new HashSet< String >();
1014
998
 
1015
999
                        Pattern p = Pattern.compile(
1016
 
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$",
1017
 
                                Pattern.CASE_INSENSITIVE );
 
1000
                                "^" + name + "[ \\t]*=[ \\t]*(\"?)(.*)\\1$" );
1018
1001
                        for( int i = 0; i < params.length; i++ ) {
1019
1002
                                Matcher m = p.matcher( params[ i ] );
1020
1003
                                if( m.matches() )
1028
1011
                 * Amongst the params, return any type values present. For v2.1 vCards,
1029
1012
                 * those types are just parameters. For v3.0, they are prefixed with
1030
1013
                 * "TYPE=". There may also be multiple type parameters.
1031
 
                 * @param params an array of params to look for types in
1032
 
                 * @param valid_types an list of upper-case type values to look for
 
1014
                 * @param params
 
1015
                 * @param a list of type values to look for
1033
1016
                 * @return a set of present type values
1034
1017
                 */
1035
1018
                private Set< String > extractTypes( String[] params,
1045
1028
                                // the specs!)
1046
1029
                                String[] parts = type_params[ a ].split( "," );
1047
1030
                                for( int i = 0; i < parts.length; i++ )
1048
 
                                        parts[ i ] = parts[ i ].toUpperCase( Locale.US );
1049
 
                                for( int i = 0; i < parts.length; i++ )
1050
1031
                                        if( valid_types.contains( parts[ i ] ) )
1051
1032
                                                types.add( parts[ i ] );
1052
1033
                        }