/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;
40
38
 
 
39
 
41
40
public class Importer extends Thread
42
41
{
43
42
        public final static int ACTION_ABORT = 1;
52
51
        private Doit _doit;
53
52
        private int _response;
54
53
        private int _responseExtra;
55
 
        private HashMap< String, Long > _contacts;
56
 
        private HashMap< Long, HashSet< String > > _contactNumbers;
57
 
        private HashMap< Long, HashSet< String > > _contactEmails;
58
54
        private int _mergeSetting;
59
55
        private int _lastMergeDecision;
60
56
        private boolean _abort = false;
61
57
        private boolean _isFinished = false;
 
58
        private ContactsCache _contactsCache = null;
62
59
 
63
60
        public class ContactData
64
61
        {
112
109
                        }
113
110
                }
114
111
 
 
112
                class AddressData
 
113
                {
 
114
                        private String _address;
 
115
                        public int _type;
 
116
 
 
117
                        public AddressData( String address, int type ) {
 
118
                                _address = address;
 
119
                                _type = type;
 
120
                        }
 
121
 
 
122
                        public String getAddress() {
 
123
                                return _address;
 
124
                        }
 
125
 
 
126
                        public int getType() {
 
127
                                return _type;
 
128
                        }
 
129
                }
 
130
 
115
131
                public String _name = null;
116
132
                public HashMap< String, PhoneData > _phones = null;
117
133
                public HashMap< String, EmailData > _emails = null;
 
134
                public HashMap< String, AddressData > _addresses = null;
118
135
 
119
136
                protected void setName( String name )
120
137
                {
140
157
                        if( !_emails.containsKey( email ) )
141
158
                                _emails.put( email, new EmailData( email, type, isPreferred ) );
142
159
                }
 
160
 
 
161
                protected void addAddress( String address, int type )
 
162
                {
 
163
                        if( _addresses == null ) _addresses =
 
164
                                new HashMap< String, AddressData >();
 
165
                        if( !_addresses.containsKey( address ) )
 
166
                                _addresses.put( address, new AddressData( address, type ) );
 
167
                }
143
168
        }
144
169
 
145
170
        @SuppressWarnings("serial")
158
183
        {
159
184
                try
160
185
                {
161
 
                        // cache current contact names
162
 
                        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 );
163
192
 
164
193
                        // do the import
165
194
                        onImport();
349
378
                {
350
379
                case Doit.ACTION_KEEP:
351
380
                        // if we keep contacts on duplicate, we better check for one
352
 
                        return !_contacts.containsKey( name );
 
381
                        return !_contactsCache.exists( name );
353
382
 
354
383
                case Doit.ACTION_PROMPT:
355
384
                        // if we are prompting on duplicate, we better check for one
356
 
                        if( !_contacts.containsKey( name ) )
 
385
                        if( !_contactsCache.exists( name ) )
357
386
                                return true;
358
387
 
359
388
                        // ok, it exists, so do prompt
400
429
                // does contact exist already?
401
430
                Uri contactUri = null;
402
431
                Long id;
403
 
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
 
432
                if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
404
433
                {
405
434
                        // should we skip this import altogether?
406
435
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
420
449
                                uiInformed = true;
421
450
 
422
451
                                // update cache
423
 
                                _contacts.remove( contact._name );
 
452
                                _contactsCache.remove( contact._name );
424
453
                        }
425
454
                }
426
455
 
445
474
                        }
446
475
 
447
476
                        // update cache
448
 
                        _contacts.put( contact._name, id );
 
477
                        _contactsCache.put( id, contact._name );
449
478
 
450
479
                        // update UI
451
480
                        if( !uiInformed ) {
463
492
                        importContactPhones( contactUri, contact._phones );
464
493
                if( contact._emails != null )
465
494
                        importContactEmails( contactUri, contact._emails );
 
495
                if( contact._addresses != null )
 
496
                        importContactAddresses( contactUri, contact._addresses );
466
497
        }
467
498
 
468
499
        private void importContactPhones( Uri contactUri,
487
518
                        // anyway, so it's not a problem).
488
519
                        String number = sanitisePhoneNumber( phone._number );
489
520
                        if( number == null ) continue;
490
 
                        HashSet< String > numbers = _contactNumbers.get( contactId );
491
 
                        if( numbers != null && numbers.contains( number ) ) continue;
 
521
                        if( _contactsCache.hasNumber( contactId, number ) ) continue;
492
522
 
493
523
                        // add phone number
494
524
                        ContentValues values = new ContentValues();
496
526
                        values.put( Contacts.Phones.NUMBER, phone._number );
497
527
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
498
528
                        _doit.getContentResolver().insert( contactPhonesUri, values );
499
 
                }
500
 
 
501
 
                // now add those phone numbers to the cache to prevent the addition of
502
 
                // duplicate data from another file
503
 
                i = phonesKeys.iterator();
504
 
                while( i.hasNext() ) {
505
 
                        ContactData.PhoneData phone = phones.get( i.next() );
506
 
 
507
 
                        String number = sanitisePhoneNumber( phone._number );
508
 
                        if( number != null ) {
509
 
                                HashSet< String > numbers = _contactNumbers.get( contactId );
510
 
                                if( numbers == null ) {
511
 
                                        _contactNumbers.put( contactId, new HashSet< String >() );
512
 
                                        numbers = _contactNumbers.get( contactId );
513
 
                                }
514
 
                                numbers.add( number );
515
 
                        }
 
529
 
 
530
                        // and add this address to the cache to prevent a addition of
 
531
                        // duplicate date from another file
 
532
                        _contactsCache.addNumber( contactId, number );
516
533
                }
517
534
        }
518
535
 
529
546
                while( i.hasNext() ) {
530
547
                        ContactData.EmailData email = emails.get( i.next() );
531
548
 
532
 
                        // like with phone numbers, we don't want to add this email address
533
 
                        // if it exists already or we would introduce duplicates.
 
549
                        // we don't want to add this email address if it exists already or
 
550
                        // we would introduce duplicates.
534
551
                        String address = sanitiseEmailAddress( email.getAddress() );
535
552
                        if( address == null ) continue;
536
 
                        HashSet< String > addresses = _contactEmails.get( contactId );
537
 
                        if( addresses != null && addresses.contains( address ) ) continue;
 
553
                        if( _contactsCache.hasEmail( contactId, address ) ) continue;
538
554
 
539
555
                        // add phone number
540
556
                        ContentValues values = new ContentValues();
545
561
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
546
562
                        _doit.getContentResolver().insert( contactContactMethodsUri,
547
563
                                values );
 
564
 
 
565
                        // and add this address to the cache to prevent a addition of
 
566
                        // duplicate date from another file
 
567
                        _contactsCache.addEmail( contactId, address );
548
568
                }
549
 
 
550
 
                // now add those email addresses to the cache to prevent the addition of
551
 
                // duplicate data from another file
552
 
                i = emailsKeys.iterator();
 
569
        }
 
570
 
 
571
        private void importContactAddresses( Uri contactUri,
 
572
                HashMap< String, ContactData.AddressData > addresses )
 
573
        {
 
574
                Long contactId = ContentUris.parseId( contactUri );
 
575
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
 
576
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
577
                Set< String > addressesKeys = addresses.keySet();
 
578
 
 
579
                // add addresses
 
580
                Iterator< String > i = addressesKeys.iterator();
553
581
                while( i.hasNext() ) {
554
 
                        ContactData.EmailData email = emails.get( i.next() );
555
 
 
556
 
                        String address = sanitiseEmailAddress( email.getAddress() );
557
 
                        if( address != null ) {
558
 
                                HashSet< String > addresses = _contactEmails.get( contactId );
559
 
                                if( addresses == null ) {
560
 
                                        _contactEmails.put( contactId, new HashSet< String >() );
561
 
                                        addresses = _contactEmails.get( contactId );
562
 
                                }
563
 
                                addresses.add( address );
564
 
                        }
 
582
                        ContactData.AddressData address = addresses.get( i.next() );
 
583
 
 
584
                        // we don't want to add this address if it exists already or we
 
585
                        // would introduce duplicates
 
586
                        if( address == null ) continue;
 
587
                        if( _contactsCache.hasAddress( contactId, address.getAddress() ) )
 
588
                                continue;
 
589
 
 
590
                        // add postal address
 
591
                        ContentValues values = new ContentValues();
 
592
                        values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
 
593
                        values.put( Contacts.ContactMethods.DATA, address.getAddress() );
 
594
                        values.put( Contacts.ContactMethods.TYPE, address.getType() );
 
595
                        _doit.getContentResolver().insert( contactContactMethodsUri,
 
596
                                values );
 
597
 
 
598
                        // and add this address to the cache to prevent a addition of
 
599
                        // duplicate date from another file
 
600
                        _contactsCache.addAddress( contactId, address.getAddress() );
565
601
                }
566
602
        }
567
603
 
573
609
                }
574
610
        }
575
611
 
576
 
        private void buildContactsCache() throws AbortImportException
577
 
        {
578
 
                // update UI
579
 
                setProgressMessage( R.string.doit_caching );
580
 
 
581
 
                String[] cols;
582
 
                Cursor cur;
583
 
 
584
 
                // init contacts caches
585
 
                _contacts = new HashMap< String, Long >();
586
 
                _contactNumbers = new HashMap< Long, HashSet< String > >();
587
 
                _contactEmails = new HashMap< Long, HashSet< String > >();
588
 
 
589
 
                // query and store map of contact names to ids
590
 
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
591
 
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
592
 
                        cols, null, null, null);
593
 
                if( cur.moveToFirst() ) {
594
 
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
595
 
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
596
 
                        do {
597
 
                                _contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
598
 
                        } while( cur.moveToNext() );
599
 
                }
600
 
 
601
 
                // query and store map of contact ids to sets of phone numbers
602
 
                cols = new String[] { Contacts.Phones.PERSON_ID,
603
 
                                Contacts.Phones.NUMBER };
604
 
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
605
 
                        cols, null, null, null);
606
 
                if( cur.moveToFirst() ) {
607
 
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
608
 
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
609
 
                        do {
610
 
                                Long id = cur.getLong( personIdCol );
611
 
                                String number = sanitisePhoneNumber(
612
 
                                                cur.getString( numberCol ) );
613
 
                                if( number != null ) {
614
 
                                        HashSet< String > numbers = _contactNumbers.get( id );
615
 
                                        if( numbers == null ) {
616
 
                                                _contactNumbers.put( id, new HashSet< String >() );
617
 
                                                numbers = _contactNumbers.get( id );
618
 
                                        }
619
 
                                        numbers.add( number );
620
 
                                }
621
 
                        } while( cur.moveToNext() );
622
 
                }
623
 
 
624
 
                // query and store map of contact ids to sets of email addresses
625
 
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
626
 
                                Contacts.ContactMethods.DATA };
627
 
                cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
628
 
                                cols, Contacts.ContactMethods.KIND + " = ?",
629
 
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
630
 
                if( cur.moveToFirst() ) {
631
 
                        int personIdCol = cur.getColumnIndex(
632
 
                                Contacts.ContactMethods.PERSON_ID );
633
 
                        int addressCol = cur.getColumnIndex(
634
 
                                Contacts.ContactMethods.DATA );
635
 
                        do {
636
 
                                Long id = cur.getLong( personIdCol );
637
 
                                String address = sanitiseEmailAddress(
638
 
                                        cur.getString( addressCol ) );
639
 
                                if( address != null ) {
640
 
                                        HashSet< String > addresses = _contactEmails.get( id );
641
 
                                        if( addresses == null ) {
642
 
                                                _contactEmails.put( id, new HashSet< String >() );
643
 
                                                addresses = _contactEmails.get( id );
644
 
                                        }
645
 
                                        addresses.add( address );
646
 
                                }
647
 
                        } while( cur.moveToNext() );
648
 
                }
649
 
        }
650
 
 
651
 
        private String sanitisePhoneNumber( String number )
 
612
        static public String sanitisePhoneNumber( String number )
652
613
        {
653
614
                number = number.replaceAll( "[-\\(\\) ]", "" );
654
615
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
657
618
                return null;
658
619
        }
659
620
 
660
 
        private String sanitiseEmailAddress( String address )
 
621
        static public String sanitiseEmailAddress( String address )
661
622
        {
662
623
                address = address.trim();
663
624
                Pattern p = Pattern.compile(