/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/VCFImporter.java

  • Committer: edam
  • Date: 2011-05-02 18:28:24 UTC
  • Revision ID: edam@waxworlds.org-20110502182824-acgdi3qfxfzqgely
- fixed logic for vcard field types (home, work, cell, etc) so it works
- updated NEWS and TODO
- rewrote most of ContactsCache, including a new ContactIdentifier class to identify contacts in the cache and new cache building code
- contacts now identified in the same way that Andoid displays them (by name, or organisation, or number, or email, in that order)
- propper handling and support for organisations and titles
- validation of imported contact now done by Importer, not VcfImporter
- separated sanitisation and normalisation (for cache lookups)
- generacised PhoneData, EmailData and AddressData classes
- ContactData is now aware of primary numbers, emails and organisations (defaults to the first prefrred one seen, or the first one seen where none is preferred)

Show diffs side-by-side

added added

removed removed

44
44
import java.util.regex.Matcher;
45
45
import java.util.regex.Pattern;
46
46
 
 
47
import org.waxworlds.edam.importcontacts.Importer.ContactData.ExtraDetail;
 
48
 
47
49
import android.content.SharedPreferences;
48
50
import android.provider.Contacts;
49
51
import android.provider.Contacts.PhonesColumns;
50
52
 
51
 
public class VcardImporter extends Importer
 
53
public class VCFImporter extends Importer
52
54
{
53
 
        private int _vcard_count = 0;
 
55
        private int _vCardCount = 0;
54
56
        private int _progress = 0;
55
57
 
56
 
        public VcardImporter( Doit doit )
 
58
        public VCFImporter( Doit doit )
57
59
        {
58
60
                super( doit );
59
61
        }
110
112
                        countVCardFile( files[ i ] );
111
113
                        setTmpProgress( i );
112
114
                }
113
 
                setProgressMax( _vcard_count ); // will also update tmp progress
 
115
                setProgressMax( _vCardCount );  // will also update tmp progress
114
116
 
115
117
                // import them
116
118
                setProgress( 0 );
128
130
 
129
131
                        // read
130
132
                        String line;
131
 
                        boolean in_vcard = false;
 
133
                        boolean inVCard = false;
132
134
                        while( ( line = reader.readLine() ) != null )
133
135
                        {
134
 
                                if( !in_vcard ) {
 
136
                                if( !inVCard ) {
135
137
                                        // look for vcard beginning
136
138
                                        if( line.matches( "^BEGIN:VCARD" ) ) {
137
 
                                                in_vcard = true;
138
 
                                                _vcard_count++;
 
139
                                                inVCard = true;
 
140
                                                _vCardCount++;
139
141
                                        }
140
142
                                }
141
143
                                else if( line.matches( "^END:VCARD" ) )
142
 
                                        in_vcard = false;
 
144
                                        inVCard = false;
143
145
                        }
144
146
 
145
147
                }
185
187
                throws AbortImportException
186
188
        {
187
189
                // go through lines
188
 
                Vcard vcard = null;
 
190
                VCard vCard = null;
189
191
                ContentLineIterator cli = new ContentLineIterator( content );
190
192
                while( cli.hasNext() )
191
193
                {
202
204
                                line = "";
203
205
                        }
204
206
 
205
 
                        if( vcard == null ) {
 
207
                        if( vCard == null ) {
206
208
                                // look for vcard beginning
207
209
                                if( line.matches( "^BEGIN:VCARD" ) ) {
208
210
                                        setProgress( ++_progress );
209
 
                                        vcard = new Vcard();
 
211
                                        vCard = new VCard();
210
212
                                }
211
213
                        }
212
214
                        else {
215
217
                                {
216
218
                                        // store vcard and do away with it
217
219
                                        try {
218
 
                                                vcard.finaliseParsing();
219
 
                                                importContact( vcard );
 
220
                                                vCard.finaliseParsing();
 
221
                                                importContact( vCard );
220
222
                                        }
221
 
                                        catch( Vcard.ParseException e ) {
 
223
                                        catch( VCard.ParseException e ) {
222
224
                                                skipContact();
223
225
                                                if( !showContinue(
224
226
                                                        getText( R.string.error_vcf_parse ).toString()
227
229
                                                        finish( ACTION_ABORT );
228
230
                                                }
229
231
                                        }
230
 
                                        catch( Vcard.SkipContactException e ) {
 
232
                                        catch( VCard.SkipContactException e ) {
231
233
                                                skipContact();
232
234
                                                // do nothing
233
235
                                        }
234
 
                                        vcard = null;
 
236
                                        vCard = null;
235
237
                                }
236
238
                                else
237
239
                                {
238
240
                                        // try giving the line to the vcard
239
241
                                        try {
240
 
                                                vcard.parseLine( buffer, line,
 
242
                                                vCard.parseLine( buffer, line,
241
243
                                                        cli.doesNextLineLookFolded() );
242
244
                                        }
243
 
                                        catch( Vcard.ParseException e ) {
 
245
                                        catch( VCard.ParseException e ) {
244
246
                                                skipContact();
245
247
                                                if( !showContinue(
246
248
                                                        getText( R.string.error_vcf_parse ).toString()
252
254
                                                // although we're continuing, we still need to abort
253
255
                                                // this vCard. Further lines will be ignored until we
254
256
                                                // get to another BEGIN:VCARD line.
255
 
                                                vcard = null;
 
257
                                                vCard = null;
256
258
                                        }
257
 
                                        catch( Vcard.SkipContactException e ) {
 
259
                                        catch( VCard.SkipContactException e ) {
258
260
                                                skipContact();
259
261
                                                // abort this vCard. Further lines will be ignored until
260
262
                                                // we get to another BEGIN:VCARD line.
261
 
                                                vcard = null;
 
263
                                                vCard = null;
262
264
                                        }
263
265
                                }
264
266
                        }
328
330
                }
329
331
        }
330
332
 
331
 
        private class Vcard extends ContactData
 
333
        private class VCard extends ContactData
332
334
        {
333
335
                private final static int NAMELEVEL_NONE = 0;
334
336
                private final static int NAMELEVEL_FN = 1;
382
384
 
383
385
                        public ParseException( int res )
384
386
                        {
385
 
                                super( VcardImporter.this.getText( res ).toString() );
 
387
                                super( VCFImporter.this.getText( res ).toString() );
386
388
                        }
387
389
                }
388
390
 
771
773
                                "PAGER", "BBS", "MODEM", "CAR", "ISDN", "VIDEO" ) );
772
774
 
773
775
                        // here's the logic...
774
 
                        boolean is_preferred = types.contains( "PREF" );
 
776
                        boolean preferred = types.contains( "PREF" );
775
777
                        int type;
776
778
                        if( types.contains( "FAX" ) )
777
779
                                if( types.contains( "HOME" ) )
788
790
                                type = PhonesColumns.TYPE_HOME;
789
791
 
790
792
                        // add phone number
791
 
                        addNumber( value, type, is_preferred );
 
793
                        addNumber( value, type, preferred );
792
794
                }
793
795
 
794
796
                public void parseEMAIL( String[] params, String value )
799
801
                                "PREF", "WORK", "HOME", "INTERNET" ) );
800
802
 
801
803
                        // add email address
802
 
                        boolean is_preferred = types.contains( "PREF" );
 
804
                        boolean preferred = types.contains( "PREF" );
803
805
                        int type;
804
806
                        if( types.contains( "WORK" ) )
805
807
                                type = Contacts.ContactMethods.TYPE_WORK;
806
808
                        else
807
809
                                type = Contacts.ContactMethods.TYPE_HOME;
808
810
 
809
 
                        addEmail( value, type, is_preferred );
 
811
                        addEmail( value, type, preferred );
810
812
                }
811
813
 
812
814
                private void parseADR( String[] params, String value )