/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-05-30 15:33:01 UTC
  • Revision ID: edam@waxworlds.org-20110530153301-oor6ci9b3hf9clul
- refactored some code to do with how contacts are imported
- Vcards (and ContactData) instances now generate a CacheIdentifier when they are finalised so that ContactData instances that do not have enough information to identify them can be discovered then
- importContact() now calls the private method checkForDuplicate(), renamed from isImportRequired(), and return if it is not
- importContact() and checkForDuplicate() now use the ContactData's generated CacheIdentifier

Show diffs side-by-side

added added

removed removed

48
48
import android.provider.Contacts;
49
49
import android.provider.Contacts.PhonesColumns;
50
50
 
51
 
public class VCFImporter extends Importer
 
51
public class VcardImporter extends Importer
52
52
{
53
53
        private int _vcard_count = 0;
54
54
        private int _progress = 0;
55
55
 
56
 
        public VCFImporter( Doit doit )
 
56
        public VcardImporter( Doit doit )
57
57
        {
58
58
                super( doit );
59
59
        }
185
185
                throws AbortImportException
186
186
        {
187
187
                // go through lines
188
 
                VCard vcard = null;
 
188
                Vcard vcard = null;
189
189
                ContentLineIterator cli = new ContentLineIterator( content );
190
190
                while( cli.hasNext() )
191
191
                {
206
206
                                // look for vcard beginning
207
207
                                if( line.matches( "^BEGIN:VCARD" ) ) {
208
208
                                        setProgress( ++_progress );
209
 
                                        vcard = new VCard();
 
209
                                        vcard = new Vcard();
210
210
                                }
211
211
                        }
212
212
                        else {
213
213
                                // look for vcard content or ending
214
214
                                if( line.matches( "^END:VCARD" ) )
215
215
                                {
216
 
                                        // store vcard and do away with it
 
216
                                        // finalise the vcard/contact
217
217
                                        try {
218
 
                                                vcard.finaliseParsing();
219
 
                                                importContact( vcard );
 
218
                                                vcard.finaliseVcard();
220
219
                                        }
221
 
                                        catch( VCard.ParseException e ) {
 
220
                                        catch( Vcard.ParseException e ) {
222
221
                                                skipContact();
223
222
                                                if( !showContinue(
224
223
                                                        getText( R.string.error_vcf_parse ).toString()
227
226
                                                        finish( ACTION_ABORT );
228
227
                                                }
229
228
                                        }
230
 
                                        catch( VCard.SkipContactException e ) {
231
 
                                                skipContact();
232
 
                                                // do nothing
233
 
                                        }
 
229
 
 
230
                                        // pass the finalised contact to the importer
 
231
                                        importContact( vcard );
 
232
 
 
233
                                        // and discard it
234
234
                                        vcard = null;
235
235
                                }
236
236
                                else
240
240
                                                vcard.parseLine( buffer, line,
241
241
                                                        cli.doesNextLineLookFolded() );
242
242
                                        }
243
 
                                        catch( VCard.ParseException e ) {
 
243
                                        catch( Vcard.ParseException e ) {
244
244
                                                skipContact();
245
245
                                                if( !showContinue(
246
246
                                                        getText( R.string.error_vcf_parse ).toString()
254
254
                                                // get to another BEGIN:VCARD line.
255
255
                                                vcard = null;
256
256
                                        }
257
 
                                        catch( VCard.SkipContactException e ) {
 
257
                                        catch( Vcard.SkipImportException e ) {
258
258
                                                skipContact();
259
259
                                                // abort this vCard. Further lines will be ignored until
260
260
                                                // we get to another BEGIN:VCARD line.
328
328
                }
329
329
        }
330
330
 
331
 
        private class VCard extends ContactData
 
331
        private class Vcard extends ContactData
332
332
        {
333
333
                private final static int NAMELEVEL_NONE = 0;
334
334
                private final static int NAMELEVEL_FN = 1;
382
382
 
383
383
                        public ParseException( int res )
384
384
                        {
385
 
                                super( VCFImporter.this.getText( res ).toString() );
 
385
                                super( VcardImporter.this.getText( res ).toString() );
386
386
                        }
387
387
                }
388
388
 
389
389
                @SuppressWarnings("serial")
390
 
                protected class SkipContactException extends Exception { }
 
390
                protected class SkipImportException extends Exception { }
391
391
 
392
392
                private String extractCollonPartFromLine( ByteBuffer buffer,
393
393
                        String line, boolean former )
429
429
 
430
430
                public void parseLine( ByteBuffer buffer, String line,
431
431
                        boolean next_line_looks_folded )
432
 
                        throws ParseException, SkipContactException,
 
432
                        throws ParseException, SkipImportException,
433
433
                        AbortImportException
434
434
                {
435
435
                        // do we have a version yet?
834
834
                        addAddress( value, type );
835
835
                }
836
836
 
837
 
                public void finaliseParsing()
838
 
                        throws ParseException, SkipContactException,
839
 
                        AbortImportException
 
837
                public void finaliseVcard()
 
838
                        throws ParseException
840
839
                {
841
840
                        // missing version (and data is present)
842
841
                        if( _version == null && _buffers != null )
843
842
                                throw new ParseException( R.string.error_vcf_malformed );
844
843
 
845
 
                        // check if we should import this contact
 
844
                        // finalise the parent class
846
845
                        try {
847
 
                                if( !isImportRequired( this ) )
848
 
                                        throw new SkipContactException();
 
846
                                finalise();
849
847
                        }
850
 
                        catch( ContactNeedsMoreInfoException e ) {
 
848
                        catch( ContactNotIdentifiableException e ) {
851
849
                                throw new ParseException( R.string.error_vcf_notenoughinfo );
852
850
                        }
853
851
                }