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

  • Committer: edam
  • Date: 2011-03-19 20:33:09 UTC
  • Revision ID: edam@waxworlds.org-20110319203309-5dzfyqrxwk94jtin
- formatting: removed some double-indents on overrunning lines
- updated TODO and NEWS
- rewrote central logic of parser so it makes more sense, looks nicer and has a small optimisation (getting name and params from line only when necessary)
- optimised unnecessary mutliple converting of lines to US-ASCII
- re-wrote line extraction from vcards so that we can lookahead for v3 folded lines
- added support for v3 folded lines

Show diffs side-by-side

added added

removed removed

131
131
                        if( _phones == null ) _phones = new HashMap< String, PhoneData >();
132
132
                        if( !_phones.containsKey( number ) )
133
133
                                _phones.put( number,
134
 
                                                new PhoneData( number, type, isPreferred ) );
 
134
                                        new PhoneData( number, type, isPreferred ) );
135
135
                }
136
136
 
137
137
                protected void addEmail( String email, int type, boolean isPreferred )
225
225
        {
226
226
                checkAbort();
227
227
                _doit._handler.sendMessage( Message.obtain(
228
 
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
228
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
229
229
                try {
230
230
                        wait();
231
231
                }
246
246
        {
247
247
                checkAbort();
248
248
                _doit._handler.sendMessage( Message.obtain(
249
 
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
249
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
250
250
                try {
251
251
                        wait();
252
252
                }
267
267
        {
268
268
                checkAbort();
269
269
                _doit._handler.sendMessage( Message.obtain(
270
 
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
270
                        _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
271
271
                try {
272
272
                        wait();
273
273
                }
283
283
        {
284
284
                checkAbort();
285
285
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
286
 
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
286
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
287
287
        }
288
288
 
289
289
        protected void setProgressMax( int maxProgress )
291
291
        {
292
292
                checkAbort();
293
293
                _doit._handler.sendMessage( Message.obtain(
294
 
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
295
 
                                new Integer( maxProgress ) ) );
 
294
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
 
295
                        new Integer( maxProgress ) ) );
296
296
        }
297
297
 
298
298
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
299
299
        {
300
300
                checkAbort();
301
301
                _doit._handler.sendMessage( Message.obtain(
302
 
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
303
 
                                new Integer( tmpProgress ) ) );
 
302
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
 
303
                        new Integer( tmpProgress ) ) );
304
304
        }
305
305
 
306
306
        protected void setProgress( int progress ) throws AbortImportException
307
307
        {
308
308
                checkAbort();
309
309
                _doit._handler.sendMessage( Message.obtain(
310
 
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
311
 
                                new Integer( progress ) ) );
 
310
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
 
311
                        new Integer( progress ) ) );
312
312
        }
313
313
 
314
314
        protected void finish( int action ) throws AbortImportException
358
358
 
359
359
                        // ok, it exists, so do prompt
360
360
                        _doit._handler.sendMessage( Message.obtain(
361
 
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
361
                                _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
362
362
                        try {
363
363
                                wait();
364
364
                        }
371
371
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
372
372
                                _mergeSetting = _response;
373
373
 
374
 
                        // recurse, with out new merge setting
 
374
                        // recurse, with our new merge setting
375
375
                        return isImportRequired( name, _response );
376
376
                }
377
377
 
407
407
 
408
408
                        // get contact's URI
409
409
                        contactUri = ContentUris.withAppendedId(
410
 
                                        Contacts.People.CONTENT_URI, id );
 
410
                                Contacts.People.CONTENT_URI, id );
411
411
 
412
412
                        // should we destroy the existing contact before importing?
413
413
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
414
414
                                _doit.getContentResolver().delete( contactUri, null, null );
415
415
                                contactUri = null;
416
416
 
417
 
                                // upate the UI
418
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
417
                                // update the UI
 
418
                                _doit._handler.sendEmptyMessage(
 
419
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
419
420
                                uiInformed = true;
420
421
 
421
422
                                // update cache
430
431
                        // create a new contact
431
432
                        values.put( Contacts.People.NAME, contact._name );
432
433
                        contactUri = _doit.getContentResolver().insert(
433
 
                                        Contacts.People.CONTENT_URI, values );
 
434
                                Contacts.People.CONTENT_URI, values );
434
435
                        id = ContentUris.parseId( contactUri );
435
436
                        if( id <= 0 ) return;   // shouldn't happen!
436
437
 
439
440
                                Contacts.People.addToMyContactsGroup(
440
441
                                        _doit.getContentResolver(), id );
441
442
                        }
442
 
                        catch( IllegalStateException e ) { }
 
443
                        catch( IllegalStateException e ) {
 
444
                                // ignore any failure
 
445
                        }
443
446
 
444
447
                        // update cache
445
448
                        _contacts.put( contact._name, id );
541
544
                        if( email.isPreferred() )
542
545
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
543
546
                        _doit.getContentResolver().insert( contactContactMethodsUri,
544
 
                                        values );
 
547
                                values );
545
548
                }
546
549
 
547
550
                // now add those email addresses to the cache to prevent the addition of
586
589
                // query and store map of contact names to ids
587
590
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
588
591
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
589
 
                                cols, null, null, null);
 
592
                        cols, null, null, null);
590
593
                if( cur.moveToFirst() ) {
591
594
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
592
595
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
599
602
                cols = new String[] { Contacts.Phones.PERSON_ID,
600
603
                                Contacts.Phones.NUMBER };
601
604
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
602
 
                                cols, null, null, null);
 
605
                        cols, null, null, null);
603
606
                if( cur.moveToFirst() ) {
604
607
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
605
608
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
626
629
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
627
630
                if( cur.moveToFirst() ) {
628
631
                        int personIdCol = cur.getColumnIndex(
629
 
                                        Contacts.ContactMethods.PERSON_ID );
 
632
                                Contacts.ContactMethods.PERSON_ID );
630
633
                        int addressCol = cur.getColumnIndex(
631
 
                                        Contacts.ContactMethods.DATA );
 
634
                                Contacts.ContactMethods.DATA );
632
635
                        do {
633
636
                                Long id = cur.getLong( personIdCol );
634
637
                                String address = sanitiseEmailAddress(
635
 
                                                cur.getString( addressCol ) );
 
638
                                        cur.getString( addressCol ) );
636
639
                                if( address != null ) {
637
640
                                        HashSet< String > addresses = _contactEmails.get( id );
638
641
                                        if( addresses == null ) {
658
661
        {
659
662
                address = address.trim();
660
663
                Pattern p = Pattern.compile(
661
 
                                "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
 
664
                        "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
662
665
                Matcher m = p.matcher( address );
663
666
                if( m.matches() ) {
664
667
                        String[] bits = address.split( "@" );