/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-04-22 15:36:06 UTC
  • Revision ID: edam@waxworlds.org-20110422153606-9x9l0nbmvx6oxfxu
- pulled contacts cache out in to seperate class

Show diffs side-by-side

added added

removed removed

24
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import java.util.HashMap;
27
 
import java.util.HashSet;
28
27
import java.util.Iterator;
29
28
import java.util.Set;
30
29
import java.util.regex.Matcher;
33
32
import android.content.ContentUris;
34
33
import android.content.ContentValues;
35
34
import android.content.SharedPreferences;
36
 
import android.database.Cursor;
37
35
import android.net.Uri;
38
36
import android.os.Message;
39
37
import android.provider.Contacts;
53
51
        private Doit _doit;
54
52
        private int _response;
55
53
        private int _responseExtra;
56
 
        private HashMap< String, Long > _contacts;
57
 
        private HashMap< Long, HashSet< String > > _contactNumbers;
58
 
        private HashMap< Long, HashSet< String > > _contactEmails;
59
 
        private HashMap< Long, HashSet< String > > _contactAddresses;
60
54
        private int _mergeSetting;
61
55
        private int _lastMergeDecision;
62
56
        private boolean _abort = false;
63
57
        private boolean _isFinished = false;
 
58
        private ContactsCache _contactsCache = null;
64
59
 
65
60
        public class ContactData
66
61
        {
188
183
        {
189
184
                try
190
185
                {
191
 
                        // cache current contact names
192
 
                        buildContactsCache();
 
186
                        // update UI
 
187
                        setProgressMessage( R.string.doit_caching );
 
188
 
 
189
                        // build a cache of existing contacts
 
190
                        _contactsCache = new ContactsCache();
 
191
                        _contactsCache.buildCache( _doit );
193
192
 
194
193
                        // do the import
195
194
                        onImport();
379
378
                {
380
379
                case Doit.ACTION_KEEP:
381
380
                        // if we keep contacts on duplicate, we better check for one
382
 
                        return !_contacts.containsKey( name );
 
381
                        return !_contactsCache.exists( name );
383
382
 
384
383
                case Doit.ACTION_PROMPT:
385
384
                        // if we are prompting on duplicate, we better check for one
386
 
                        if( !_contacts.containsKey( name ) )
 
385
                        if( !_contactsCache.exists( name ) )
387
386
                                return true;
388
387
 
389
388
                        // ok, it exists, so do prompt
430
429
                // does contact exist already?
431
430
                Uri contactUri = null;
432
431
                Long id;
433
 
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
 
432
                if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
434
433
                {
435
434
                        // should we skip this import altogether?
436
435
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
450
449
                                uiInformed = true;
451
450
 
452
451
                                // update cache
453
 
                                _contacts.remove( contact._name );
 
452
                                _contactsCache.remove( contact._name );
454
453
                        }
455
454
                }
456
455
 
475
474
                        }
476
475
 
477
476
                        // update cache
478
 
                        _contacts.put( contact._name, id );
 
477
                        _contactsCache.put( id, contact._name );
479
478
 
480
479
                        // update UI
481
480
                        if( !uiInformed ) {
519
518
                        // anyway, so it's not a problem).
520
519
                        String number = sanitisePhoneNumber( phone._number );
521
520
                        if( number == null ) continue;
522
 
                        HashSet< String > cache = _contactNumbers.get( contactId );
523
 
                        if( cache != null && cache.contains( number ) ) continue;
 
521
                        if( _contactsCache.hasNumber( contactId, number ) ) continue;
524
522
 
525
523
                        // add phone number
526
524
                        ContentValues values = new ContentValues();
531
529
 
532
530
                        // and add this address to the cache to prevent a addition of
533
531
                        // duplicate date from another file
534
 
                        if( cache == null ) {
535
 
                                cache = new HashSet< String >();
536
 
                                _contactNumbers.put( contactId, cache );
537
 
                        }
538
 
                        cache.add( number );
 
532
                        _contactsCache.addNumber( contactId, number );
539
533
                }
540
534
        }
541
535
 
556
550
                        // we would introduce duplicates.
557
551
                        String address = sanitiseEmailAddress( email.getAddress() );
558
552
                        if( address == null ) continue;
559
 
                        HashSet< String > cache = _contactEmails.get( contactId );
560
 
                        if( cache != null && cache.contains( address ) ) continue;
 
553
                        if( _contactsCache.hasEmail( contactId, address ) ) continue;
561
554
 
562
555
                        // add phone number
563
556
                        ContentValues values = new ContentValues();
571
564
 
572
565
                        // and add this address to the cache to prevent a addition of
573
566
                        // duplicate date from another file
574
 
                        if( cache == null ) {
575
 
                                cache = new HashSet< String >();
576
 
                                _contactEmails.put( contactId, cache );
577
 
                        }
578
 
                        cache.add( address );
 
567
                        _contactsCache.addEmail( contactId, address );
579
568
                }
580
569
        }
581
570
 
595
584
                        // we don't want to add this address if it exists already or we
596
585
                        // would introduce duplicates
597
586
                        if( address == null ) continue;
598
 
                        HashSet< String > cache = _contactAddresses.get( contactId );
599
 
                        if( cache != null && cache.contains( address.getAddress() ) )
 
587
                        if( _contactsCache.hasAddress( contactId, address.getAddress() ) )
600
588
                                continue;
601
589
 
602
590
                        // add postal address
609
597
 
610
598
                        // and add this address to the cache to prevent a addition of
611
599
                        // duplicate date from another file
612
 
                        if( cache == null ) {
613
 
                                cache = new HashSet< String >();
614
 
                                _contactAddresses.put( contactId, cache );
615
 
                        }
616
 
                        cache.add( address.getAddress() );
 
600
                        _contactsCache.addAddress( contactId, address.getAddress() );
617
601
                }
618
602
        }
619
603
 
625
609
                }
626
610
        }
627
611
 
628
 
        private void buildContactsCache() throws AbortImportException
629
 
        {
630
 
                // update UI
631
 
                setProgressMessage( R.string.doit_caching );
632
 
 
633
 
                String[] cols;
634
 
                Cursor cur;
635
 
 
636
 
                // init contacts caches
637
 
                _contacts = new HashMap< String, Long >();
638
 
                _contactNumbers = new HashMap< Long, HashSet< String > >();
639
 
                _contactEmails = new HashMap< Long, HashSet< String > >();
640
 
                _contactAddresses = new HashMap< Long, HashSet< String > >();
641
 
 
642
 
                // query and store map of contact names to ids
643
 
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
644
 
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
645
 
                        cols, null, null, null);
646
 
                if( cur.moveToFirst() ) {
647
 
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
648
 
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
649
 
                        do {
650
 
                                _contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
651
 
                        } while( cur.moveToNext() );
652
 
                }
653
 
 
654
 
                // query and store map of contact ids to sets of phone numbers
655
 
                cols = new String[] { Contacts.Phones.PERSON_ID,
656
 
                                Contacts.Phones.NUMBER };
657
 
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
658
 
                        cols, null, null, null);
659
 
                if( cur.moveToFirst() ) {
660
 
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
661
 
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
662
 
                        do {
663
 
                                Long id = cur.getLong( personIdCol );
664
 
                                String number = sanitisePhoneNumber(
665
 
                                                cur.getString( numberCol ) );
666
 
                                if( number != null ) {
667
 
                                        HashSet< String > numbers = _contactNumbers.get( id );
668
 
                                        if( numbers == null ) {
669
 
                                                numbers = new HashSet< String >();
670
 
                                                _contactNumbers.put( id, numbers );
671
 
                                        }
672
 
                                        numbers.add( number );
673
 
                                }
674
 
                        } while( cur.moveToNext() );
675
 
                }
676
 
 
677
 
                // query and store map of contact ids to sets of email addresses
678
 
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
679
 
                                Contacts.ContactMethods.DATA };
680
 
                cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
681
 
                                cols, Contacts.ContactMethods.KIND + " = ?",
682
 
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
683
 
                if( cur.moveToFirst() ) {
684
 
                        int personIdCol = cur.getColumnIndex(
685
 
                                Contacts.ContactMethods.PERSON_ID );
686
 
                        int addressCol = cur.getColumnIndex(
687
 
                                Contacts.ContactMethods.DATA );
688
 
                        do {
689
 
                                Long id = cur.getLong( personIdCol );
690
 
                                String address = sanitiseEmailAddress(
691
 
                                        cur.getString( addressCol ) );
692
 
                                if( address != null ) {
693
 
                                        HashSet< String > addresses = _contactEmails.get( id );
694
 
                                        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 );
722
 
                                        }
723
 
                                        addresses.add( address );
724
 
                                }
725
 
                        } while( cur.moveToNext() );
726
 
                }
727
 
        }
728
 
 
729
 
        private String sanitisePhoneNumber( String number )
 
612
        static public String sanitisePhoneNumber( String number )
730
613
        {
731
614
                number = number.replaceAll( "[-\\(\\) ]", "" );
732
615
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
735
618
                return null;
736
619
        }
737
620
 
738
 
        private String sanitiseEmailAddress( String address )
 
621
        static public String sanitiseEmailAddress( String address )
739
622
        {
740
623
                address = address.trim();
741
624
                Pattern p = Pattern.compile(