/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:51:35 UTC
  • Revision ID: tim@ed.am-20121219175135-1cpuafp76jg1ib1p
added preliminary (buggy) ContactsContract backend

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;
38
38
import java.util.HashSet;
39
39
import java.util.Iterator;
40
40
import java.util.List;
 
41
import java.util.Locale;
41
42
import java.util.NoSuchElementException;
42
43
import java.util.Set;
43
44
import java.util.Vector;
44
45
import java.util.regex.Matcher;
45
46
import java.util.regex.Pattern;
46
47
 
 
48
import android.annotation.SuppressLint;
47
49
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" )
61
62
        @Override
62
63
        protected void onImport() throws AbortImportException
63
64
        {
82
83
                                // get files
83
84
                                class VCardFilter implements FilenameFilter {
84
85
                                        public boolean accept( File dir, String name ) {
85
 
                                                return name.toLowerCase().endsWith( ".vcf" );
 
86
                                                return name.toLowerCase( Locale.US ).endsWith( ".vcf" );
86
87
                                        }
87
88
                                }
88
89
                                files = file.listFiles( new VCardFilter() );
568
569
 
569
570
                                // determine whether we care about this entry
570
571
                                final HashSet< String > interesting_fields =
571
 
                                        new HashSet< String >( Arrays.asList( new String[]
572
 
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
 
572
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
 
573
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
573
574
                                ) );
574
575
                                boolean is_interesting_field =
575
576
                                        interesting_fields.contains( name_param_parts[ 0 ] );
576
577
 
577
578
                                // parse encoding parameter
578
579
                                String encoding = checkParam( name_param_parts, "ENCODING" );
579
 
                                if( encoding != null ) encoding = encoding.toUpperCase();
 
580
                                if( encoding != null )
 
581
                                        encoding = encoding.toUpperCase( Locale.US );
580
582
                                if( is_interesting_field && encoding != null &&
581
583
                                        !encoding.equals( "8BIT" ) &&
582
584
                                        !encoding.equals( "QUOTED-PRINTABLE" ) )
587
589
 
588
590
                                // parse charset parameter
589
591
                                String charset = checkParam( name_param_parts, "CHARSET" );
590
 
                                if( charset != null ) charset = charset.toUpperCase();
 
592
                                if( charset != null )
 
593
                                        charset = charset.toUpperCase( Locale.US );
591
594
                                if( charset != null &&
592
595
                                        !charset.equals( "US-ASCII" ) &&
593
596
                                        !charset.equals( "ASCII" ) &&
676
679
                                        parseEMAIL( name_param_parts, complete_value );
677
680
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
678
681
                                        parseADR( name_param_parts, complete_value );
 
682
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
 
683
                                        parseLABEL( name_param_parts, complete_value );
679
684
                        }
680
685
                }
681
686
 
877
882
                        int type;
878
883
                        if( types.contains( "FAX" ) )
879
884
                                if( types.contains( "HOME" ) )
880
 
                                        type = PhonesColumns.TYPE_FAX_HOME;
 
885
                                        type = TYPE_FAX_HOME;
881
886
                                else
882
 
                                        type = PhonesColumns.TYPE_FAX_WORK;
 
887
                                        type = TYPE_FAX_WORK;
883
888
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
884
 
                                type = PhonesColumns.TYPE_MOBILE;
 
889
                                type = TYPE_MOBILE;
885
890
                        else if( types.contains( "PAGER" ) )
886
 
                                type = PhonesColumns.TYPE_PAGER;
 
891
                                type = TYPE_PAGER;
887
892
                        else if( types.contains( "WORK" ) )
888
 
                                type = PhonesColumns.TYPE_WORK;
 
893
                                type = TYPE_WORK;
889
894
                        else
890
 
                                type = PhonesColumns.TYPE_HOME;
 
895
                                type = TYPE_HOME;
891
896
 
892
897
                        // add phone number
893
898
                        addNumber( value, type, is_preferred );
904
909
                        boolean is_preferred = types.contains( "PREF" );
905
910
                        int type;
906
911
                        if( types.contains( "WORK" ) )
907
 
                                type = Contacts.ContactMethods.TYPE_WORK;
 
912
                                type = TYPE_WORK;
908
913
                        else
909
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
914
                                type = TYPE_HOME;
910
915
 
911
916
                        addEmail( unescapeValue( value ), type, is_preferred );
912
917
                }
933
938
                                }
934
939
 
935
940
                        Set< String > types = extractTypes( params, Arrays.asList(
936
 
                                "PREF", "WORK", "HOME", "INTERNET" ) );
937
 
 
938
 
                        // add address
939
 
                        int type;
940
 
                        if( types.contains( "WORK" ) )
941
 
                                type = Contacts.ContactMethods.TYPE_WORK;
942
 
                        else
943
 
                                type = Contacts.ContactMethods.TYPE_HOME;
 
941
                                "PREF", "WORK", "HOME" ) );
 
942
 
 
943
                        // add address
 
944
                        int type;
 
945
                        if( types.contains( "WORK" ) )
 
946
                                type = TYPE_WORK;
 
947
                        else
 
948
                                type = TYPE_HOME;
 
949
 
 
950
                        addAddress( unescapeValue( value ), type );
 
951
                }
 
952
 
 
953
                private void parseLABEL( String[] params, String value )
 
954
                {
 
955
                        Set< String > types = extractTypes( params, Arrays.asList(
 
956
                                "PREF", "WORK", "HOME" ) );
 
957
 
 
958
                        // add address
 
959
                        int type;
 
960
                        if( types.contains( "WORK" ) )
 
961
                                type = TYPE_WORK;
 
962
                        else
 
963
                                type = TYPE_HOME;
944
964
 
945
965
                        addAddress( unescapeValue( value ), type );
946
966
                }