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

  • Committer: edam
  • Date: 2009-02-02 07:07:52 UTC
  • Revision ID: edam@waxworlds.org-20090202070752-2lp8igdsdjyu9fic
- bugfix: add contacts to the "my contacts" group didn't actually work on a real device. So we're doing it a different way.
- updated todo list

Show diffs side-by-side

added added

removed removed

21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package org.waxworlds.importcontacts;
25
25
 
26
26
import java.util.HashMap;
27
27
import java.util.HashSet;
40
40
 
41
41
public class Importer extends Thread
42
42
{
 
43
        public final static int ACTION_GOBACK = 0;
43
44
        public final static int ACTION_ABORT = 1;
44
45
        public final static int ACTION_ALLDONE = 2;
45
46
 
131
132
                        if( _phones == null ) _phones = new HashMap< String, PhoneData >();
132
133
                        if( !_phones.containsKey( number ) )
133
134
                                _phones.put( number,
134
 
                                        new PhoneData( number, type, isPreferred ) );
 
135
                                                new PhoneData( number, type, isPreferred ) );
135
136
                }
136
137
 
137
138
                protected void addEmail( String email, int type, boolean isPreferred )
142
143
                }
143
144
        }
144
145
 
145
 
        @SuppressWarnings("serial")
146
146
        protected class AbortImportException extends Exception { };
147
147
 
148
148
        public Importer( Doit doit )
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
                }
232
232
                catch( InterruptedException e ) { }
233
 
 
234
233
                // no need to check if an abortion happened during the wait, we are
235
234
                // about to finish anyway!
236
235
                finish( ACTION_ABORT );
246
245
        {
247
246
                checkAbort();
248
247
                _doit._handler.sendMessage( Message.obtain(
249
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
248
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
250
249
                try {
251
250
                        wait();
252
251
                }
253
252
                catch( InterruptedException e ) { }
254
 
 
255
253
                // no need to check if an abortion happened during the wait, we are
256
254
                // about to finish anyway!
257
255
                finish( ACTION_ABORT );
267
265
        {
268
266
                checkAbort();
269
267
                _doit._handler.sendMessage( Message.obtain(
270
 
                        _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
268
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
271
269
                try {
272
270
                        wait();
273
271
                }
283
281
        {
284
282
                checkAbort();
285
283
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
286
 
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
284
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
287
285
        }
288
286
 
289
287
        protected void setProgressMax( int maxProgress )
291
289
        {
292
290
                checkAbort();
293
291
                _doit._handler.sendMessage( Message.obtain(
294
 
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
295
 
                        new Integer( maxProgress ) ) );
 
292
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
 
293
                                new Integer( maxProgress ) ) );
296
294
        }
297
295
 
298
296
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
299
297
        {
300
298
                checkAbort();
301
299
                _doit._handler.sendMessage( Message.obtain(
302
 
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
303
 
                        new Integer( tmpProgress ) ) );
 
300
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
 
301
                                new Integer( tmpProgress ) ) );
304
302
        }
305
303
 
306
304
        protected void setProgress( int progress ) throws AbortImportException
307
305
        {
308
306
                checkAbort();
309
307
                _doit._handler.sendMessage( Message.obtain(
310
 
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
311
 
                        new Integer( progress ) ) );
 
308
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
 
309
                                new Integer( progress ) ) );
312
310
        }
313
311
 
314
312
        protected void finish( int action ) throws AbortImportException
317
315
                int message;
318
316
                switch( action )
319
317
                {
320
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
 
318
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
 
319
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
321
320
                default:        // fall through
322
 
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
 
321
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
323
322
                }
324
323
                _doit._handler.sendEmptyMessage( message );
325
324
 
358
357
 
359
358
                        // ok, it exists, so do prompt
360
359
                        _doit._handler.sendMessage( Message.obtain(
361
 
                                _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
360
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
362
361
                        try {
363
362
                                wait();
364
363
                        }
371
370
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
372
371
                                _mergeSetting = _response;
373
372
 
374
 
                        // recurse, with our new merge setting
 
373
                        // recurse, with out new merge setting
375
374
                        return isImportRequired( name, _response );
376
375
                }
377
376
 
407
406
 
408
407
                        // get contact's URI
409
408
                        contactUri = ContentUris.withAppendedId(
410
 
                                Contacts.People.CONTENT_URI, id );
 
409
                                        Contacts.People.CONTENT_URI, id );
411
410
 
412
411
                        // should we destroy the existing contact before importing?
413
412
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
414
413
                                _doit.getContentResolver().delete( contactUri, null, null );
415
414
                                contactUri = null;
416
415
 
417
 
                                // update the UI
418
 
                                _doit._handler.sendEmptyMessage(
419
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
416
                                // upate the UI
 
417
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
420
418
                                uiInformed = true;
421
419
 
422
420
                                // update cache
431
429
                        // create a new contact
432
430
                        values.put( Contacts.People.NAME, contact._name );
433
431
                        contactUri = _doit.getContentResolver().insert(
434
 
                                Contacts.People.CONTENT_URI, values );
 
432
                                        Contacts.People.CONTENT_URI, values );
435
433
                        id = ContentUris.parseId( contactUri );
436
434
                        if( id <= 0 ) return;   // shouldn't happen!
437
435
 
438
 
                        // try to add them to the "My Contacts" group
439
 
                        try {
440
 
                                Contacts.People.addToMyContactsGroup(
 
436
                        // add them to the "My Contacts" group
 
437
                        Contacts.People.addToMyContactsGroup(
441
438
                                        _doit.getContentResolver(), id );
442
 
                        }
443
 
                        catch( IllegalStateException e ) {
444
 
                                // ignore any failure
445
 
                        }
446
439
 
447
440
                        // update cache
448
441
                        _contacts.put( contact._name, id );
471
464
                Long contactId = ContentUris.parseId( contactUri );
472
465
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
473
466
                                Contacts.People.Phones.CONTENT_DIRECTORY );
474
 
                Set< String > phonesKeys = phones.keySet();
 
467
                Set phonesKeys = phones.keySet();
475
468
 
476
469
                // add phone numbers
477
 
                Iterator< String > i = phonesKeys.iterator();
 
470
                Iterator i = phonesKeys.iterator();
478
471
                while( i.hasNext() ) {
479
472
                        ContactData.PhoneData phone = phones.get( i.next() );
480
473
 
522
515
                Long contactId = ContentUris.parseId( contactUri );
523
516
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
524
517
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
525
 
                Set< String > emailsKeys = emails.keySet();
 
518
                Set emailsKeys = emails.keySet();
526
519
 
527
520
                // add email addresses
528
 
                Iterator< String > i = emailsKeys.iterator();
 
521
                Iterator i = emailsKeys.iterator();
529
522
                while( i.hasNext() ) {
530
523
                        ContactData.EmailData email = emails.get( i.next() );
531
524
 
544
537
                        if( email.isPreferred() )
545
538
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
546
539
                        _doit.getContentResolver().insert( contactContactMethodsUri,
547
 
                                values );
 
540
                                        values );
548
541
                }
549
542
 
550
543
                // now add those email addresses to the cache to prevent the addition of
589
582
                // query and store map of contact names to ids
590
583
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
591
584
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
592
 
                        cols, null, null, null);
 
585
                                cols, null, null, null);
593
586
                if( cur.moveToFirst() ) {
594
587
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
595
588
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
602
595
                cols = new String[] { Contacts.Phones.PERSON_ID,
603
596
                                Contacts.Phones.NUMBER };
604
597
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
605
 
                        cols, null, null, null);
 
598
                                cols, null, null, null);
606
599
                if( cur.moveToFirst() ) {
607
600
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
608
601
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
629
622
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
630
623
                if( cur.moveToFirst() ) {
631
624
                        int personIdCol = cur.getColumnIndex(
632
 
                                Contacts.ContactMethods.PERSON_ID );
 
625
                                        Contacts.ContactMethods.PERSON_ID );
633
626
                        int addressCol = cur.getColumnIndex(
634
 
                                Contacts.ContactMethods.DATA );
 
627
                                        Contacts.ContactMethods.DATA );
635
628
                        do {
636
629
                                Long id = cur.getLong( personIdCol );
637
630
                                String address = sanitiseEmailAddress(
638
 
                                        cur.getString( addressCol ) );
 
631
                                                cur.getString( addressCol ) );
639
632
                                if( address != null ) {
640
633
                                        HashSet< String > addresses = _contactEmails.get( id );
641
634
                                        if( addresses == null ) {
651
644
        private String sanitisePhoneNumber( String number )
652
645
        {
653
646
                number = number.replaceAll( "[-\\(\\) ]", "" );
654
 
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
 
647
                Pattern p = Pattern.compile( "^\\+?[0-9]+" );
655
648
                Matcher m = p.matcher( number );
656
649
                if( m.lookingAt() ) return m.group( 0 );
657
650
                return null;
661
654
        {
662
655
                address = address.trim();
663
656
                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])?)+$" );
 
657
                                "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
665
658
                Matcher m = p.matcher( address );
666
659
                if( m.matches() ) {
667
660
                        String[] bits = address.split( "@" );