/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: 2010-07-04 14:44:48 UTC
  • Revision ID: edam@waxworlds.org-20100704144448-1m30v811opup20fs
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
- massively simplified the WizzardActivity class so it works propperly
- moved all code to org.waxworlds.edam
- added an "aborted" message when the importion is aborted
- simplified the 3 actions the worker thread can take when stopping (only 2 were actualy used) to "aborted" or "alldone"
- changed intro message to match website
- bugfix: don't blow up when the My Contacts group is missing

Show diffs side-by-side

added added

removed removed

38
38
import android.os.Message;
39
39
import android.provider.Contacts;
40
40
 
41
 
 
42
41
public class Importer extends Thread
43
42
{
44
43
        public final static int ACTION_ABORT = 1;
56
55
        private HashMap< String, Long > _contacts;
57
56
        private HashMap< Long, HashSet< String > > _contactNumbers;
58
57
        private HashMap< Long, HashSet< String > > _contactEmails;
59
 
        private HashMap< Long, HashSet< String > > _contactAddresses;
60
58
        private int _mergeSetting;
61
59
        private int _lastMergeDecision;
62
60
        private boolean _abort = false;
114
112
                        }
115
113
                }
116
114
 
117
 
                class AddressData
118
 
                {
119
 
                        private String _address;
120
 
                        public int _type;
121
 
 
122
 
                        public AddressData( String address, int type ) {
123
 
                                _address = address;
124
 
                                _type = type;
125
 
                        }
126
 
 
127
 
                        public String getAddress() {
128
 
                                return _address;
129
 
                        }
130
 
 
131
 
                        public int getType() {
132
 
                                return _type;
133
 
                        }
134
 
                }
135
 
 
136
115
                public String _name = null;
137
116
                public HashMap< String, PhoneData > _phones = null;
138
117
                public HashMap< String, EmailData > _emails = null;
139
 
                public HashMap< String, AddressData > _addresses = null;
140
118
 
141
119
                protected void setName( String name )
142
120
                {
153
131
                        if( _phones == null ) _phones = new HashMap< String, PhoneData >();
154
132
                        if( !_phones.containsKey( number ) )
155
133
                                _phones.put( number,
156
 
                                        new PhoneData( number, type, isPreferred ) );
 
134
                                                new PhoneData( number, type, isPreferred ) );
157
135
                }
158
136
 
159
137
                protected void addEmail( String email, int type, boolean isPreferred )
162
140
                        if( !_emails.containsKey( email ) )
163
141
                                _emails.put( email, new EmailData( email, type, isPreferred ) );
164
142
                }
165
 
 
166
 
                protected void addAddress( String address, int type )
167
 
                {
168
 
                        if( _addresses == null ) _addresses =
169
 
                                new HashMap< String, AddressData >();
170
 
                        if( !_addresses.containsKey( address ) )
171
 
                                _addresses.put( address, new AddressData( address, type ) );
172
 
                }
173
143
        }
174
144
 
175
145
        @SuppressWarnings("serial")
255
225
        {
256
226
                checkAbort();
257
227
                _doit._handler.sendMessage( Message.obtain(
258
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
228
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
259
229
                try {
260
230
                        wait();
261
231
                }
276
246
        {
277
247
                checkAbort();
278
248
                _doit._handler.sendMessage( Message.obtain(
279
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
249
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
280
250
                try {
281
251
                        wait();
282
252
                }
297
267
        {
298
268
                checkAbort();
299
269
                _doit._handler.sendMessage( Message.obtain(
300
 
                        _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
270
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
301
271
                try {
302
272
                        wait();
303
273
                }
313
283
        {
314
284
                checkAbort();
315
285
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
316
 
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
286
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
317
287
        }
318
288
 
319
289
        protected void setProgressMax( int maxProgress )
321
291
        {
322
292
                checkAbort();
323
293
                _doit._handler.sendMessage( Message.obtain(
324
 
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
325
 
                        new Integer( maxProgress ) ) );
 
294
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
 
295
                                new Integer( maxProgress ) ) );
326
296
        }
327
297
 
328
298
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
329
299
        {
330
300
                checkAbort();
331
301
                _doit._handler.sendMessage( Message.obtain(
332
 
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
333
 
                        new Integer( tmpProgress ) ) );
 
302
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
 
303
                                new Integer( tmpProgress ) ) );
334
304
        }
335
305
 
336
306
        protected void setProgress( int progress ) throws AbortImportException
337
307
        {
338
308
                checkAbort();
339
309
                _doit._handler.sendMessage( Message.obtain(
340
 
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
341
 
                        new Integer( progress ) ) );
 
310
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
 
311
                                new Integer( progress ) ) );
342
312
        }
343
313
 
344
314
        protected void finish( int action ) throws AbortImportException
388
358
 
389
359
                        // ok, it exists, so do prompt
390
360
                        _doit._handler.sendMessage( Message.obtain(
391
 
                                _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
361
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
392
362
                        try {
393
363
                                wait();
394
364
                        }
401
371
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
402
372
                                _mergeSetting = _response;
403
373
 
404
 
                        // recurse, with our new merge setting
 
374
                        // recurse, with out new merge setting
405
375
                        return isImportRequired( name, _response );
406
376
                }
407
377
 
437
407
 
438
408
                        // get contact's URI
439
409
                        contactUri = ContentUris.withAppendedId(
440
 
                                Contacts.People.CONTENT_URI, id );
 
410
                                        Contacts.People.CONTENT_URI, id );
441
411
 
442
412
                        // should we destroy the existing contact before importing?
443
413
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
444
414
                                _doit.getContentResolver().delete( contactUri, null, null );
445
415
                                contactUri = null;
446
416
 
447
 
                                // update the UI
448
 
                                _doit._handler.sendEmptyMessage(
449
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
417
                                // upate the UI
 
418
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
450
419
                                uiInformed = true;
451
420
 
452
421
                                // update cache
461
430
                        // create a new contact
462
431
                        values.put( Contacts.People.NAME, contact._name );
463
432
                        contactUri = _doit.getContentResolver().insert(
464
 
                                Contacts.People.CONTENT_URI, values );
 
433
                                        Contacts.People.CONTENT_URI, values );
465
434
                        id = ContentUris.parseId( contactUri );
466
435
                        if( id <= 0 ) return;   // shouldn't happen!
467
436
 
470
439
                                Contacts.People.addToMyContactsGroup(
471
440
                                        _doit.getContentResolver(), id );
472
441
                        }
473
 
                        catch( IllegalStateException e ) {
474
 
                                // ignore any failure
475
 
                        }
 
442
                        catch( IllegalStateException e ) { }
476
443
 
477
444
                        // update cache
478
445
                        _contacts.put( contact._name, id );
493
460
                        importContactPhones( contactUri, contact._phones );
494
461
                if( contact._emails != null )
495
462
                        importContactEmails( contactUri, contact._emails );
496
 
                if( contact._addresses != null )
497
 
                        importContactAddresses( contactUri, contact._addresses );
498
463
        }
499
464
 
500
465
        private void importContactPhones( Uri contactUri,
519
484
                        // anyway, so it's not a problem).
520
485
                        String number = sanitisePhoneNumber( phone._number );
521
486
                        if( number == null ) continue;
522
 
                        HashSet< String > cache = _contactNumbers.get( contactId );
523
 
                        if( cache != null && cache.contains( number ) ) continue;
 
487
                        HashSet< String > numbers = _contactNumbers.get( contactId );
 
488
                        if( numbers != null && numbers.contains( number ) ) continue;
524
489
 
525
490
                        // add phone number
526
491
                        ContentValues values = new ContentValues();
528
493
                        values.put( Contacts.Phones.NUMBER, phone._number );
529
494
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
530
495
                        _doit.getContentResolver().insert( contactPhonesUri, values );
531
 
 
532
 
                        // and add this address to the cache to prevent a addition of
533
 
                        // duplicate date from another file
534
 
                        if( cache == null ) {
535
 
                                cache = new HashSet< String >();
536
 
                                _contactNumbers.put( contactId, cache );
 
496
                }
 
497
 
 
498
                // now add those phone numbers to the cache to prevent the addition of
 
499
                // duplicate data from another file
 
500
                i = phonesKeys.iterator();
 
501
                while( i.hasNext() ) {
 
502
                        ContactData.PhoneData phone = phones.get( i.next() );
 
503
 
 
504
                        String number = sanitisePhoneNumber( phone._number );
 
505
                        if( number != null ) {
 
506
                                HashSet< String > numbers = _contactNumbers.get( contactId );
 
507
                                if( numbers == null ) {
 
508
                                        _contactNumbers.put( contactId, new HashSet< String >() );
 
509
                                        numbers = _contactNumbers.get( contactId );
 
510
                                }
 
511
                                numbers.add( number );
537
512
                        }
538
 
                        cache.add( number );
539
513
                }
540
514
        }
541
515
 
552
526
                while( i.hasNext() ) {
553
527
                        ContactData.EmailData email = emails.get( i.next() );
554
528
 
555
 
                        // we don't want to add this email address if it exists already or
556
 
                        // we would introduce duplicates.
 
529
                        // like with phone numbers, we don't want to add this email address
 
530
                        // if it exists already or we would introduce duplicates.
557
531
                        String address = sanitiseEmailAddress( email.getAddress() );
558
532
                        if( address == null ) continue;
559
 
                        HashSet< String > cache = _contactEmails.get( contactId );
560
 
                        if( cache != null && cache.contains( address ) ) continue;
 
533
                        HashSet< String > addresses = _contactEmails.get( contactId );
 
534
                        if( addresses != null && addresses.contains( address ) ) continue;
561
535
 
562
536
                        // add phone number
563
537
                        ContentValues values = new ContentValues();
567
541
                        if( email.isPreferred() )
568
542
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
569
543
                        _doit.getContentResolver().insert( contactContactMethodsUri,
570
 
                                values );
571
 
 
572
 
                        // and add this address to the cache to prevent a addition of
573
 
                        // duplicate date from another file
574
 
                        if( cache == null ) {
575
 
                                cache = new HashSet< String >();
576
 
                                _contactEmails.put( contactId, cache );
577
 
                        }
578
 
                        cache.add( address );
 
544
                                        values );
579
545
                }
580
 
        }
581
 
 
582
 
        private void importContactAddresses( Uri contactUri,
583
 
                HashMap< String, ContactData.AddressData > addresses )
584
 
        {
585
 
                Long contactId = ContentUris.parseId( contactUri );
586
 
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
587
 
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
588
 
                Set< String > addressesKeys = addresses.keySet();
589
 
 
590
 
                // add addresses
591
 
                Iterator< String > i = addressesKeys.iterator();
 
546
 
 
547
                // now add those email addresses to the cache to prevent the addition of
 
548
                // duplicate data from another file
 
549
                i = emailsKeys.iterator();
592
550
                while( i.hasNext() ) {
593
 
                        ContactData.AddressData address = addresses.get( i.next() );
594
 
 
595
 
                        // we don't want to add this address if it exists already or we
596
 
                        // would introduce duplicates
597
 
                        if( address == null ) continue;
598
 
                        HashSet< String > cache = _contactAddresses.get( contactId );
599
 
                        if( cache != null && cache.contains( address.getAddress() ) )
600
 
                                continue;
601
 
 
602
 
                        // add postal address
603
 
                        ContentValues values = new ContentValues();
604
 
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
605
 
                        values.put( Contacts.ContactMethods.DATA, address.getAddress() );
606
 
                        values.put( Contacts.ContactMethods.TYPE, address.getType() );
607
 
                        _doit.getContentResolver().insert( contactContactMethodsUri,
608
 
                                values );
609
 
 
610
 
                        // and add this address to the cache to prevent a addition of
611
 
                        // duplicate date from another file
612
 
                        if( cache == null ) {
613
 
                                cache = new HashSet< String >();
614
 
                                _contactAddresses.put( contactId, cache );
 
551
                        ContactData.EmailData email = emails.get( i.next() );
 
552
 
 
553
                        String address = sanitiseEmailAddress( email.getAddress() );
 
554
                        if( address != null ) {
 
555
                                HashSet< String > addresses = _contactEmails.get( contactId );
 
556
                                if( addresses == null ) {
 
557
                                        _contactEmails.put( contactId, new HashSet< String >() );
 
558
                                        addresses = _contactEmails.get( contactId );
 
559
                                }
 
560
                                addresses.add( address );
615
561
                        }
616
 
                        cache.add( address.getAddress() );
617
562
                }
618
563
        }
619
564
 
637
582
                _contacts = new HashMap< String, Long >();
638
583
                _contactNumbers = new HashMap< Long, HashSet< String > >();
639
584
                _contactEmails = new HashMap< Long, HashSet< String > >();
640
 
                _contactAddresses = new HashMap< Long, HashSet< String > >();
641
585
 
642
586
                // query and store map of contact names to ids
643
587
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
644
588
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
645
 
                        cols, null, null, null);
 
589
                                cols, null, null, null);
646
590
                if( cur.moveToFirst() ) {
647
591
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
648
592
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
655
599
                cols = new String[] { Contacts.Phones.PERSON_ID,
656
600
                                Contacts.Phones.NUMBER };
657
601
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
658
 
                        cols, null, null, null);
 
602
                                cols, null, null, null);
659
603
                if( cur.moveToFirst() ) {
660
604
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
661
605
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
666
610
                                if( number != null ) {
667
611
                                        HashSet< String > numbers = _contactNumbers.get( id );
668
612
                                        if( numbers == null ) {
669
 
                                                numbers = new HashSet< String >();
670
 
                                                _contactNumbers.put( id, numbers );
 
613
                                                _contactNumbers.put( id, new HashSet< String >() );
 
614
                                                numbers = _contactNumbers.get( id );
671
615
                                        }
672
616
                                        numbers.add( number );
673
617
                                }
682
626
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
683
627
                if( cur.moveToFirst() ) {
684
628
                        int personIdCol = cur.getColumnIndex(
685
 
                                Contacts.ContactMethods.PERSON_ID );
 
629
                                        Contacts.ContactMethods.PERSON_ID );
686
630
                        int addressCol = cur.getColumnIndex(
687
 
                                Contacts.ContactMethods.DATA );
 
631
                                        Contacts.ContactMethods.DATA );
688
632
                        do {
689
633
                                Long id = cur.getLong( personIdCol );
690
634
                                String address = sanitiseEmailAddress(
691
 
                                        cur.getString( addressCol ) );
 
635
                                                cur.getString( addressCol ) );
692
636
                                if( address != null ) {
693
637
                                        HashSet< String > addresses = _contactEmails.get( id );
694
638
                                        if( addresses == null ) {
695
 
                                                addresses = new HashSet< String >();
696
 
                                                _contactEmails.put( id, addresses );
697
 
                                        }
698
 
                                        addresses.add( address );
699
 
                                }
700
 
                        } while( cur.moveToNext() );
701
 
                }
702
 
 
703
 
                // query and store map of contact ids to sets of postal addresses
704
 
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
705
 
                        Contacts.ContactMethods.DATA };
706
 
                cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
707
 
                        cols, Contacts.ContactMethods.KIND + " = ?",
708
 
                        new String[] { "" + Contacts.KIND_POSTAL }, null );
709
 
                if( cur.moveToFirst() ) {
710
 
                        int personIdCol = cur.getColumnIndex(
711
 
                                Contacts.ContactMethods.PERSON_ID );
712
 
                        int addressCol = cur.getColumnIndex(
713
 
                                Contacts.ContactMethods.DATA );
714
 
                        do {
715
 
                                Long id = cur.getLong( personIdCol );
716
 
                                String address = cur.getString( addressCol );
717
 
                                if( address != null ) {
718
 
                                        HashSet< String > addresses = _contactAddresses.get( id );
719
 
                                        if( addresses == null ) {
720
 
                                                addresses = new HashSet< String >();
721
 
                                                _contactAddresses.put( id, addresses );
 
639
                                                _contactEmails.put( id, new HashSet< String >() );
 
640
                                                addresses = _contactEmails.get( id );
722
641
                                        }
723
642
                                        addresses.add( address );
724
643
                                }
729
648
        private String sanitisePhoneNumber( String number )
730
649
        {
731
650
                number = number.replaceAll( "[-\\(\\) ]", "" );
732
 
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
 
651
                Pattern p = Pattern.compile( "^\\+?[0-9]+" );
733
652
                Matcher m = p.matcher( number );
734
653
                if( m.lookingAt() ) return m.group( 0 );
735
654
                return null;
739
658
        {
740
659
                address = address.trim();
741
660
                Pattern p = Pattern.compile(
742
 
                        "^[^ @]+@[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])?)+$" );
743
662
                Matcher m = p.matcher( address );
744
663
                if( m.matches() ) {
745
664
                        String[] bits = address.split( "@" );