/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-13 18:58:52 UTC
  • Revision ID: edam@waxworlds.org-20110313185852-s74kvkbv8k0v0lwc
- accept parameters that are quoted (this doesn't appear to be part of the standards AFAICT, but Evolution apparently quotes parameter values)

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 our new merge setting
 
374
                        // recurse, with out 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
 
                                // update the UI
418
 
                                _doit._handler.sendEmptyMessage(
419
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
417
                                // upate the UI
 
418
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
420
419
                                uiInformed = true;
421
420
 
422
421
                                // update cache
431
430
                        // create a new contact
432
431
                        values.put( Contacts.People.NAME, contact._name );
433
432
                        contactUri = _doit.getContentResolver().insert(
434
 
                                Contacts.People.CONTENT_URI, values );
 
433
                                        Contacts.People.CONTENT_URI, values );
435
434
                        id = ContentUris.parseId( contactUri );
436
435
                        if( id <= 0 ) return;   // shouldn't happen!
437
436
 
440
439
                                Contacts.People.addToMyContactsGroup(
441
440
                                        _doit.getContentResolver(), id );
442
441
                        }
443
 
                        catch( IllegalStateException e ) {
444
 
                                // ignore any failure
445
 
                        }
 
442
                        catch( IllegalStateException e ) { }
446
443
 
447
444
                        // update cache
448
445
                        _contacts.put( contact._name, id );
544
541
                        if( email.isPreferred() )
545
542
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
546
543
                        _doit.getContentResolver().insert( contactContactMethodsUri,
547
 
                                values );
 
544
                                        values );
548
545
                }
549
546
 
550
547
                // now add those email addresses to the cache to prevent the addition of
589
586
                // query and store map of contact names to ids
590
587
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
591
588
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
592
 
                        cols, null, null, null);
 
589
                                cols, null, null, null);
593
590
                if( cur.moveToFirst() ) {
594
591
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
595
592
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
602
599
                cols = new String[] { Contacts.Phones.PERSON_ID,
603
600
                                Contacts.Phones.NUMBER };
604
601
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
605
 
                        cols, null, null, null);
 
602
                                cols, null, null, null);
606
603
                if( cur.moveToFirst() ) {
607
604
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
608
605
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
629
626
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
630
627
                if( cur.moveToFirst() ) {
631
628
                        int personIdCol = cur.getColumnIndex(
632
 
                                Contacts.ContactMethods.PERSON_ID );
 
629
                                        Contacts.ContactMethods.PERSON_ID );
633
630
                        int addressCol = cur.getColumnIndex(
634
 
                                Contacts.ContactMethods.DATA );
 
631
                                        Contacts.ContactMethods.DATA );
635
632
                        do {
636
633
                                Long id = cur.getLong( personIdCol );
637
634
                                String address = sanitiseEmailAddress(
638
 
                                        cur.getString( addressCol ) );
 
635
                                                cur.getString( addressCol ) );
639
636
                                if( address != null ) {
640
637
                                        HashSet< String > addresses = _contactEmails.get( id );
641
638
                                        if( addresses == null ) {
661
658
        {
662
659
                address = address.trim();
663
660
                Pattern p = Pattern.compile(
664
 
                        "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
 
661
                                "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
665
662
                Matcher m = p.matcher( address );
666
663
                if( m.matches() ) {
667
664
                        String[] bits = address.split( "@" );