/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-19 20:33:09 UTC
  • Revision ID: edam@waxworlds.org-20110319203309-5dzfyqrxwk94jtin
- formatting: removed some double-indents on overrunning lines
- updated TODO and NEWS
- rewrote central logic of parser so it makes more sense, looks nicer and has a small optimisation (getting name and params from line only when necessary)
- optimised unnecessary mutliple converting of lines to US-ASCII
- re-wrote line extraction from vcards so that we can lookahead for v3 folded lines
- added support for v3 folded lines

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
                {
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")
493
463
                        importContactPhones( contactUri, contact._phones );
494
464
                if( contact._emails != null )
495
465
                        importContactEmails( contactUri, contact._emails );
496
 
                if( contact._addresses != null )
497
 
                        importContactAddresses( contactUri, contact._addresses );
498
466
        }
499
467
 
500
468
        private void importContactPhones( Uri contactUri,
519
487
                        // anyway, so it's not a problem).
520
488
                        String number = sanitisePhoneNumber( phone._number );
521
489
                        if( number == null ) continue;
522
 
                        HashSet< String > cache = _contactNumbers.get( contactId );
523
 
                        if( cache != null && cache.contains( number ) ) continue;
 
490
                        HashSet< String > numbers = _contactNumbers.get( contactId );
 
491
                        if( numbers != null && numbers.contains( number ) ) continue;
524
492
 
525
493
                        // add phone number
526
494
                        ContentValues values = new ContentValues();
528
496
                        values.put( Contacts.Phones.NUMBER, phone._number );
529
497
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
530
498
                        _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 );
 
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 );
537
515
                        }
538
 
                        cache.add( number );
539
516
                }
540
517
        }
541
518
 
552
529
                while( i.hasNext() ) {
553
530
                        ContactData.EmailData email = emails.get( i.next() );
554
531
 
555
 
                        // we don't want to add this email address if it exists already or
556
 
                        // we would introduce duplicates.
 
532
                        // like with phone numbers, we don't want to add this email address
 
533
                        // if it exists already or we would introduce duplicates.
557
534
                        String address = sanitiseEmailAddress( email.getAddress() );
558
535
                        if( address == null ) continue;
559
 
                        HashSet< String > cache = _contactEmails.get( contactId );
560
 
                        if( cache != null && cache.contains( address ) ) continue;
 
536
                        HashSet< String > addresses = _contactEmails.get( contactId );
 
537
                        if( addresses != null && addresses.contains( address ) ) continue;
561
538
 
562
539
                        // add phone number
563
540
                        ContentValues values = new ContentValues();
568
545
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
569
546
                        _doit.getContentResolver().insert( contactContactMethodsUri,
570
547
                                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 );
579
548
                }
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();
 
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();
592
553
                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 );
 
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 );
615
564
                        }
616
 
                        cache.add( address.getAddress() );
617
565
                }
618
566
        }
619
567
 
637
585
                _contacts = new HashMap< String, Long >();
638
586
                _contactNumbers = new HashMap< Long, HashSet< String > >();
639
587
                _contactEmails = new HashMap< Long, HashSet< String > >();
640
 
                _contactAddresses = new HashMap< Long, HashSet< String > >();
641
588
 
642
589
                // query and store map of contact names to ids
643
590
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
666
613
                                if( number != null ) {
667
614
                                        HashSet< String > numbers = _contactNumbers.get( id );
668
615
                                        if( numbers == null ) {
669
 
                                                numbers = new HashSet< String >();
670
 
                                                _contactNumbers.put( id, numbers );
 
616
                                                _contactNumbers.put( id, new HashSet< String >() );
 
617
                                                numbers = _contactNumbers.get( id );
671
618
                                        }
672
619
                                        numbers.add( number );
673
620
                                }
692
639
                                if( address != null ) {
693
640
                                        HashSet< String > addresses = _contactEmails.get( id );
694
641
                                        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 );
 
642
                                                _contactEmails.put( id, new HashSet< String >() );
 
643
                                                addresses = _contactEmails.get( id );
722
644
                                        }
723
645
                                        addresses.add( address );
724
646
                                }