/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/am/ed/importcontacts/Importer.java

  • Committer: Tim Marston
  • Date: 2013-03-17 12:07:41 UTC
  • Revision ID: tim@ed.am-20130317120741-pq94jr0ajq1dsqw8
updated NEWS

Show diffs side-by-side

added added

removed removed

27
27
import java.util.HashMap;
28
28
import java.util.HashSet;
29
29
import java.util.Iterator;
 
30
import java.util.Locale;
30
31
import java.util.Set;
31
32
import java.util.regex.Matcher;
32
33
import java.util.regex.Pattern;
33
34
 
 
35
import am.ed.importcontacts.Backend.ContactCreationException;
34
36
import android.content.SharedPreferences;
35
37
import android.os.Message;
36
 
import android.provider.Contacts.PhonesColumns;
37
38
 
38
39
public class Importer extends Thread
39
40
{
61
62
         */
62
63
        public class ContactData
63
64
        {
 
65
                public final static int TYPE_HOME = 0;
 
66
                public final static int TYPE_WORK = 1;
 
67
                public final static int TYPE_MOBILE = 2;        // only used with phones
 
68
                public final static int TYPE_FAX_HOME = 3;      // only used with phones
 
69
                public final static int TYPE_FAX_WORK = 4;      // only used with phones
 
70
                public final static int TYPE_PAGER = 5;         // only used with phones
 
71
 
64
72
                class TypeDetail
65
73
                {
66
74
                        protected int _type;
133
141
                protected HashMap< String, PreferredDetail > _numbers = null;
134
142
                protected HashMap< String, PreferredDetail > _emails = null;
135
143
                protected HashMap< String, TypeDetail > _addresses = null;
 
144
                protected HashSet< String > _notes = null;
136
145
 
137
146
                private ContactsCache.CacheIdentifier _cache_identifier = null;
138
147
 
224
233
                                        new PreferredDetail( type, false ) );
225
234
 
226
235
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
227
 
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
228
 
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
 
236
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
229
237
 
230
238
                        // if this is the first number added, or it's a preferred number
231
239
                        // and the current primary number isn't, or this number is on equal
337
345
                        return _addresses;
338
346
                }
339
347
 
 
348
                protected void addNote( String note )
 
349
                {
 
350
                        if( _notes == null ) _notes = new HashSet< String >();
 
351
                        if( !_notes.contains( note ) )
 
352
                                _notes.add( note );
 
353
                }
 
354
 
 
355
                public boolean hasNotes()
 
356
                {
 
357
                        return _notes != null && _notes.size() > 0;
 
358
                }
 
359
 
 
360
                public HashSet< String > getNotes()
 
361
                {
 
362
                        return _notes;
 
363
                }
 
364
 
340
365
                protected void finalise()
341
366
                        throws ContactNotIdentifiableException
342
367
                {
365
390
 
366
391
                        // create a cache identifier from this contact data, which can be
367
392
                        // used to look-up an existing contact
368
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
393
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
369
394
                        if( _cache_identifier == null )
370
395
                                throw new ContactNotIdentifiableException();
371
396
                }
392
417
                        Matcher m = p.matcher( email );
393
418
                        if( m.matches() ) {
394
419
                                String[] bits = email.split( "@" );
395
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
 
420
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
396
421
                        }
397
422
                        return null;
398
423
                }
418
443
                        setProgressMessage( R.string.doit_caching );
419
444
 
420
445
                        // create the appropriate backend
421
 
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
422
 
//                              _backend = new ContactsContractBackend( _doit );
423
 
//                      else
 
446
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
447
                                _backend = new ContactsContractBackend( _doit );
 
448
                        else
424
449
                                _backend = new ContactsBackend( _doit );
425
450
 
426
451
                        // create a cache of existing contacts and populate it
558
583
                checkAbort();
559
584
                _doit._handler.sendMessage( Message.obtain(
560
585
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
561
 
                        new Integer( max_progress ) ) );
 
586
                        Integer.valueOf( max_progress ) ) );
562
587
        }
563
588
 
564
589
        protected void setTmpProgress( int tmp_progress )
567
592
                checkAbort();
568
593
                _doit._handler.sendMessage( Message.obtain(
569
594
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
570
 
                        new Integer( tmp_progress ) ) );
 
595
                        Integer.valueOf( tmp_progress ) ) );
571
596
        }
572
597
 
573
598
        protected void setProgress( int progress ) throws AbortImportException
575
600
                checkAbort();
576
601
                _doit._handler.sendMessage( Message.obtain(
577
602
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
578
 
                        new Integer( progress ) ) );
 
603
                        Integer.valueOf( progress ) ) );
579
604
        }
580
605
 
581
606
        protected void finish( int action ) throws AbortImportException
599
624
                return _doit.getText( res );
600
625
        }
601
626
 
602
 
        synchronized private boolean checkForDuplicate(
603
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
604
 
                throws AbortImportException
 
627
        /**
 
628
         * Should we skip a contact, given whether it exists or not and the current
 
629
         * merge setting?  This routine handles throwing up a prompt, if required.
 
630
         * @param contact_detail the display name of the contact
 
631
         * @param exists true if this contact matches one in the cache
 
632
         * @param merge_setting the merge setting to use
 
633
         * @return true if the contact should be skipped outright
 
634
         * @throws AbortImportException
 
635
         */
 
636
        synchronized private boolean shouldWeSkipContact( String contact_detail,
 
637
                boolean exists, int merge_setting ) throws AbortImportException
605
638
        {
606
639
                _last_merge_decision = merge_setting;
607
640
 
608
 
                // it is ok to use contact.getCacheIdentifier(). The contact has already
609
 
                // been finalised, which means a valid cache identifier will have been
610
 
                // created for it (or it would have been skipped)
611
 
 
612
641
                // handle special cases
613
642
                switch( merge_setting )
614
643
                {
615
644
                case Doit.ACTION_KEEP:
616
 
                        // if we keep contacts on duplicate, we better check for one
617
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
645
                        // if we are skipping on a duplicate, check for one
 
646
                        return exists;
618
647
 
619
648
                case Doit.ACTION_PROMPT:
620
 
                        // if we are prompting on duplicate, we better check for one and if
621
 
                        // the contact doesn'te exist, we want to import it
622
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
623
 
                                return true;
 
649
                        // if we are prompting on duplicate, then we can say that we won't
 
650
                        // skip if there isn't one
 
651
                        if( !exists ) return false;
624
652
 
625
 
                        // ok, it exists, so do prompt
 
653
                        // ok, duplicate exists, so do prompt
626
654
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
627
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
655
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
628
656
                        try {
629
657
                                wait();
630
658
                        }
638
666
                                _merge_setting = _response;
639
667
 
640
668
                        // recurse, with our new merge setting
641
 
                        return checkForDuplicate( cache_identifier, _response );
 
669
                        return shouldWeSkipContact( contact_detail, exists, _response );
642
670
                }
643
671
 
644
 
                // for all other cases (either overwriting or merging) we will need the
645
 
                // imported data
646
 
                return true;
 
672
                // for all other cases (either overwriting or merging) we don't skip
 
673
                return false;
647
674
        }
648
675
 
649
676
        protected void skipContact() throws AbortImportException
650
677
        {
651
678
                checkAbort();
 
679
 
 
680
                // show that we're skipping a new contact
652
681
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
653
682
        }
654
683
 
663
692
                ContactsCache.CacheIdentifier cache_identifier =
664
693
                        contact.getCacheIdentifier();
665
694
 
666
 
                // check to see if this contact is a duplicate and should be skipped
667
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
668
 
                        skipContact();
669
 
                        return;
670
 
                }
671
 
 
672
695
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
673
696
//                      finish( ACTION_ABORT );
674
697
 
675
 
                // keep track of whether we've informed the UI of what we're doing
676
 
                boolean ui_informed = false;
677
 
 
678
698
                // attempt to lookup the id of an existing contact in the cache with
679
699
                // this contact data's cache identifier
680
700
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
681
701
 
682
 
                // does contact exist already?
683
 
                if( id != null )
684
 
                {
685
 
                        // should we skip this import altogether?
686
 
                        if( _last_merge_decision == Doit.ACTION_KEEP ) return;
687
 
 
688
 
                        // should we destroy the existing contact before importing?
689
 
                        if( _last_merge_decision == Doit.ACTION_OVERWRITE )
 
702
                // check to see if this contact should be skipped
 
703
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
 
704
                        _merge_setting ) )
 
705
                {
 
706
                        // show that we're skipping a contact
 
707
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
708
                        return;
 
709
                }
 
710
 
 
711
                // if a contact exists, and we're overwriting, destroy the existing
 
712
                // contact before importing
 
713
                boolean contact_deleted = false;
 
714
                if( id != null && _last_merge_decision == Doit.ACTION_OVERWRITE )
 
715
                {
 
716
                        contact_deleted = true;
 
717
 
 
718
                        // remove from device
 
719
                        _backend.deleteContact( id );
 
720
 
 
721
                        // update cache
 
722
                        _contacts_cache.removeLookup( cache_identifier );
 
723
                        _contacts_cache.removeAssociatedData( id );
 
724
 
 
725
                        // show that we're overwriting a contact
 
726
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
727
 
 
728
                        // discard the contact id
 
729
                        id = null;
 
730
                }
 
731
 
 
732
                try {
 
733
                        // if we don't have a contact id yet (or we did, but we destroyed it
 
734
                        // when we deleted the contact), we'll have to create a new contact
 
735
                        if( id == null )
690
736
                        {
691
 
                                // remove from device
692
 
                                _backend.deleteContact( id );
 
737
                                // create a new contact
 
738
                                id = _backend.addContact( contact._name );
693
739
 
694
740
                                // update cache
695
 
                                _contacts_cache.removeLookup( cache_identifier );
696
 
                                _contacts_cache.removeAssociatedData( id );
697
 
 
698
 
                                // show that we're overwriting a contact
699
 
                                _doit._handler.sendEmptyMessage(
700
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
701
 
                                ui_informed = true;
702
 
 
703
 
                                // discard the contact id
704
 
                                id = null;
 
741
                                _contacts_cache.addLookup( cache_identifier, id );
 
742
 
 
743
                                // if we haven't already shown that we're overwriting a contact,
 
744
                                // show that we're creating a new contact
 
745
                                if( !contact_deleted )
 
746
                                        _doit._handler.sendEmptyMessage(
 
747
                                                Doit.MESSAGE_CONTACTCREATED );
705
748
                        }
 
749
                        else
 
750
                                // show that we're merging with an existing contact
 
751
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
752
 
 
753
                        // import contact parts
 
754
                        if( contact.hasNumbers() )
 
755
                                importContactPhones( id, contact.getNumbers() );
 
756
                        if( contact.hasEmails() )
 
757
                                importContactEmails( id, contact.getEmails() );
 
758
                        if( contact.hasAddresses() )
 
759
                                importContactAddresses( id, contact.getAddresses() );
 
760
                        if( contact.hasOrganisations() )
 
761
                                importContactOrganisations( id, contact.getOrganisations() );
 
762
                        if( contact.hasNotes() )
 
763
                                importContactNotes( id, contact.getNotes() );
706
764
                }
707
 
 
708
 
                // if we don't have a contact id yet (or we did, but we destroyed it
709
 
                // when we deleted the contact), we'll have to create a new contact
710
 
                if( id == null )
 
765
                catch( Backend.ContactCreationException e )
711
766
                {
712
 
                        // create a new contact
713
 
                        id = _backend.addContact( contact._name );
714
 
                        if( id == null )
715
 
                                showError( R.string.error_unabletoaddcontact );
716
 
 
717
 
                        // update cache
718
 
                        _contacts_cache.addLookup( cache_identifier, id );
719
 
 
720
 
                        // if we haven't already shown that we're overwriting a contact,
721
 
                        // show that we're creating a new contact
722
 
                        if( !ui_informed ) {
723
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
724
 
                                ui_informed = true;
725
 
                        }
 
767
                        showError( R.string.error_unabletoaddcontact );
726
768
                }
727
 
 
728
 
                // if we haven't already shown that we're overwriting or creating a
729
 
                // contact, show that we're merging a contact
730
 
                if( !ui_informed )
731
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
732
 
 
733
 
                // import contact parts
734
 
                if( contact.hasNumbers() )
735
 
                        importContactPhones( id, contact.getNumbers() );
736
 
                if( contact.hasEmails() )
737
 
                        importContactEmails( id, contact.getEmails() );
738
 
                if( contact.hasAddresses() )
739
 
                        importContactAddresses( id, contact.getAddresses() );
740
 
                if( contact.hasOrganisations() )
741
 
                        importContactOrganisations( id, contact.getOrganisations() );
742
769
        }
743
770
 
744
771
        private void importContactPhones( Long id,
745
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
772
                HashMap< String, ContactData.PreferredDetail > datas )
 
773
                throws ContactCreationException
746
774
        {
747
775
                // add phone numbers
748
776
                Set< String > datas_keys = datas.keySet();
771
799
        }
772
800
 
773
801
        private void importContactEmails( Long id,
774
 
                        HashMap< String, ContactData.PreferredDetail > datas )
 
802
                HashMap< String, ContactData.PreferredDetail > datas )
 
803
                throws ContactCreationException
775
804
        {
776
805
                // add email addresses
777
806
                Set< String > datas_keys = datas.keySet();
796
825
 
797
826
        private void importContactAddresses( Long id,
798
827
                HashMap< String, ContactData.TypeDetail > datas )
 
828
                throws ContactCreationException
799
829
        {
800
830
                // add addresses
801
831
                Set< String > datas_keys = datas.keySet();
820
850
 
821
851
        private void importContactOrganisations( Long id,
822
852
                HashMap< String, ContactData.ExtraDetail > datas )
 
853
                throws ContactCreationException
823
854
        {
824
855
                // add addresses
825
856
                Set< String > datas_keys = datas.keySet();
842
873
                }
843
874
        }
844
875
 
 
876
        private void importContactNotes( Long id,
 
877
                HashSet< String > datas )
 
878
                throws ContactCreationException
 
879
        {
 
880
                // add notes
 
881
                Iterator< String > i = datas.iterator();
 
882
                while( i.hasNext() ) {
 
883
                        String note = i.next();
 
884
 
 
885
                        // we don't want to add this note if it exists already or we would
 
886
                        // introduce duplicates
 
887
                        if( _contacts_cache.hasAssociatedNote( id, note ) )
 
888
                                continue;
 
889
 
 
890
                        // add note
 
891
                        _backend.addContactNote( id, note );
 
892
 
 
893
                        // and add this note to the cache to prevent a addition of duplicate
 
894
                        // date from another file
 
895
                        _contacts_cache.addAssociatedNote( id, note );
 
896
                }
 
897
 
 
898
        }
 
899
 
845
900
        synchronized protected void checkAbort() throws AbortImportException
846
901
        {
847
902
                if( _abort ) {