/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-23 07:50:13 UTC
  • Revision ID: edam@waxworlds.org-20110323075013-qnza5odtlsjtcz0p
- updated TODO and NEWS
- handle different multiline schemes better
- import addresses
- check for escaped semi-colons and newlines in N, ADR and ORG lines
- minor optimisations

Show diffs side-by-side

added added

removed removed

38
38
import android.os.Message;
39
39
import android.provider.Contacts;
40
40
 
 
41
 
41
42
public class Importer extends Thread
42
43
{
43
44
        public final static int ACTION_ABORT = 1;
55
56
        private HashMap< String, Long > _contacts;
56
57
        private HashMap< Long, HashSet< String > > _contactNumbers;
57
58
        private HashMap< Long, HashSet< String > > _contactEmails;
 
59
        private HashMap< Long, HashSet< String > > _contactAddresses;
58
60
        private int _mergeSetting;
59
61
        private int _lastMergeDecision;
60
62
        private boolean _abort = false;
112
114
                        }
113
115
                }
114
116
 
 
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
 
115
136
                public String _name = null;
116
137
                public HashMap< String, PhoneData > _phones = null;
117
138
                public HashMap< String, EmailData > _emails = null;
 
139
                public HashMap< String, AddressData > _addresses = null;
118
140
 
119
141
                protected void setName( String name )
120
142
                {
140
162
                        if( !_emails.containsKey( email ) )
141
163
                                _emails.put( email, new EmailData( email, type, isPreferred ) );
142
164
                }
 
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
                }
143
173
        }
144
174
 
145
175
        @SuppressWarnings("serial")
463
493
                        importContactPhones( contactUri, contact._phones );
464
494
                if( contact._emails != null )
465
495
                        importContactEmails( contactUri, contact._emails );
 
496
                if( contact._addresses != null )
 
497
                        importContactAddresses( contactUri, contact._addresses );
466
498
        }
467
499
 
468
500
        private void importContactPhones( Uri contactUri,
487
519
                        // anyway, so it's not a problem).
488
520
                        String number = sanitisePhoneNumber( phone._number );
489
521
                        if( number == null ) continue;
490
 
                        HashSet< String > numbers = _contactNumbers.get( contactId );
491
 
                        if( numbers != null && numbers.contains( number ) ) continue;
 
522
                        HashSet< String > cache = _contactNumbers.get( contactId );
 
523
                        if( cache != null && cache.contains( number ) ) continue;
492
524
 
493
525
                        // add phone number
494
526
                        ContentValues values = new ContentValues();
496
528
                        values.put( Contacts.Phones.NUMBER, phone._number );
497
529
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
498
530
                        _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 );
 
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 );
515
537
                        }
 
538
                        cache.add( number );
516
539
                }
517
540
        }
518
541
 
529
552
                while( i.hasNext() ) {
530
553
                        ContactData.EmailData email = emails.get( i.next() );
531
554
 
532
 
                        // like with phone numbers, we don't want to add this email address
533
 
                        // if it exists already or we would introduce duplicates.
 
555
                        // we don't want to add this email address if it exists already or
 
556
                        // we would introduce duplicates.
534
557
                        String address = sanitiseEmailAddress( email.getAddress() );
535
558
                        if( address == null ) continue;
536
 
                        HashSet< String > addresses = _contactEmails.get( contactId );
537
 
                        if( addresses != null && addresses.contains( address ) ) continue;
 
559
                        HashSet< String > cache = _contactEmails.get( contactId );
 
560
                        if( cache != null && cache.contains( address ) ) continue;
538
561
 
539
562
                        // add phone number
540
563
                        ContentValues values = new ContentValues();
545
568
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
546
569
                        _doit.getContentResolver().insert( contactContactMethodsUri,
547
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 );
548
579
                }
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();
 
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();
553
592
                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 );
 
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 );
564
615
                        }
 
616
                        cache.add( address.getAddress() );
565
617
                }
566
618
        }
567
619
 
585
637
                _contacts = new HashMap< String, Long >();
586
638
                _contactNumbers = new HashMap< Long, HashSet< String > >();
587
639
                _contactEmails = new HashMap< Long, HashSet< String > >();
 
640
                _contactAddresses = new HashMap< Long, HashSet< String > >();
588
641
 
589
642
                // query and store map of contact names to ids
590
643
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
613
666
                                if( number != null ) {
614
667
                                        HashSet< String > numbers = _contactNumbers.get( id );
615
668
                                        if( numbers == null ) {
616
 
                                                _contactNumbers.put( id, new HashSet< String >() );
617
 
                                                numbers = _contactNumbers.get( id );
 
669
                                                numbers = new HashSet< String >();
 
670
                                                _contactNumbers.put( id, numbers );
618
671
                                        }
619
672
                                        numbers.add( number );
620
673
                                }
639
692
                                if( address != null ) {
640
693
                                        HashSet< String > addresses = _contactEmails.get( id );
641
694
                                        if( addresses == null ) {
642
 
                                                _contactEmails.put( id, new HashSet< String >() );
643
 
                                                addresses = _contactEmails.get( id );
 
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 );
644
722
                                        }
645
723
                                        addresses.add( address );
646
724
                                }