/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/org/waxworlds/edam/importcontacts/VcardImporter.java

  • Committer: edam
  • Date: 2011-06-04 16:52:28 UTC
  • Revision ID: edam@waxworlds.org-20110604165228-jam230oo29u1m06u
- properly handle multiple TYPE= params in one entry in a v3.0 vCard
- when deciding which phone number to use as the pimary number, a voice number takes precedence over a non-voice (fax or pager) number of the same standing in terms of being preferred or not.

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 <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
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://ed.am/dev/android/import-contacts
 
8
 * http://www.waxworlds.org/edam/software/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 am.ed.importcontacts;
 
24
package org.waxworlds.edam.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;
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() );
569
568
 
570
569
                                // determine whether we care about this entry
571
570
                                final HashSet< String > interesting_fields =
572
 
                                        new HashSet< String >( Arrays.asList( new String[] { "N",
573
 
                                                "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR", "LABEL" }
 
571
                                        new HashSet< String >( Arrays.asList( new String[]
 
572
                                                { "N", "FN", "ORG", "TITLE", "TEL", "EMAIL", "ADR" }
574
573
                                ) );
575
574
                                boolean is_interesting_field =
576
575
                                        interesting_fields.contains( name_param_parts[ 0 ] );
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" ) &&
679
676
                                        parseEMAIL( name_param_parts, complete_value );
680
677
                                else if( name_param_parts[ 0 ].equals( "ADR" ) )
681
678
                                        parseADR( name_param_parts, complete_value );
682
 
                                else if( name_param_parts[ 0 ].equals( "LABEL" ) )
683
 
                                        parseLABEL( name_param_parts, complete_value );
684
679
                        }
685
680
                }
686
681
 
882
877
                        int type;
883
878
                        if( types.contains( "FAX" ) )
884
879
                                if( types.contains( "HOME" ) )
885
 
                                        type = TYPE_FAX_HOME;
 
880
                                        type = PhonesColumns.TYPE_FAX_HOME;
886
881
                                else
887
 
                                        type = TYPE_FAX_WORK;
 
882
                                        type = PhonesColumns.TYPE_FAX_WORK;
888
883
                        else if( types.contains( "CELL" ) || types.contains( "VIDEO" ) )
889
 
                                type = TYPE_MOBILE;
 
884
                                type = PhonesColumns.TYPE_MOBILE;
890
885
                        else if( types.contains( "PAGER" ) )
891
 
                                type = TYPE_PAGER;
 
886
                                type = PhonesColumns.TYPE_PAGER;
892
887
                        else if( types.contains( "WORK" ) )
893
 
                                type = TYPE_WORK;
 
888
                                type = PhonesColumns.TYPE_WORK;
894
889
                        else
895
 
                                type = TYPE_HOME;
 
890
                                type = PhonesColumns.TYPE_HOME;
896
891
 
897
892
                        // add phone number
898
893
                        addNumber( value, type, is_preferred );
909
904
                        boolean is_preferred = types.contains( "PREF" );
910
905
                        int type;
911
906
                        if( types.contains( "WORK" ) )
912
 
                                type = TYPE_WORK;
 
907
                                type = Contacts.ContactMethods.TYPE_WORK;
913
908
                        else
914
 
                                type = TYPE_HOME;
 
909
                                type = Contacts.ContactMethods.TYPE_HOME;
915
910
 
916
911
                        addEmail( unescapeValue( value ), type, is_preferred );
917
912
                }
938
933
                                }
939
934
 
940
935
                        Set< String > types = extractTypes( params, Arrays.asList(
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;
 
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;
964
944
 
965
945
                        addAddress( unescapeValue( value ), type );
966
946
                }