/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: edam
  • Date: 2012-12-19 17:41:04 UTC
  • Revision ID: tim@ed.am-20121219174104-ly9xyjxdhqt0tu9b
ignore temporary files in eclipse project

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;
31
30
import java.util.Set;
32
31
import java.util.regex.Matcher;
33
32
import java.util.regex.Pattern;
34
33
 
35
 
import am.ed.importcontacts.Backend.ContactCreationException;
36
34
import android.content.SharedPreferences;
37
35
import android.os.Message;
 
36
import android.provider.Contacts.PhonesColumns;
38
37
 
39
38
public class Importer extends Thread
40
39
{
62
61
         */
63
62
        public class ContactData
64
63
        {
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
 
 
72
64
                class TypeDetail
73
65
                {
74
66
                        protected int _type;
141
133
                protected HashMap< String, PreferredDetail > _numbers = null;
142
134
                protected HashMap< String, PreferredDetail > _emails = null;
143
135
                protected HashMap< String, TypeDetail > _addresses = null;
144
 
                protected HashSet< String > _notes = null;
145
136
 
146
137
                private ContactsCache.CacheIdentifier _cache_identifier = null;
147
138
 
233
224
                                        new PreferredDetail( type, false ) );
234
225
 
235
226
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
236
 
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
227
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
 
228
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
237
229
 
238
230
                        // if this is the first number added, or it's a preferred number
239
231
                        // and the current primary number isn't, or this number is on equal
345
337
                        return _addresses;
346
338
                }
347
339
 
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
 
 
365
340
                protected void finalise()
366
341
                        throws ContactNotIdentifiableException
367
342
                {
390
365
 
391
366
                        // create a cache identifier from this contact data, which can be
392
367
                        // used to look-up an existing contact
393
 
                        _cache_identifier = ContactsCache.CacheIdentifier.factory( this );
 
368
                        _cache_identifier = ContactsCache.createIdentifier( this );
394
369
                        if( _cache_identifier == null )
395
370
                                throw new ContactNotIdentifiableException();
396
371
                }
417
392
                        Matcher m = p.matcher( email );
418
393
                        if( m.matches() ) {
419
394
                                String[] bits = email.split( "@" );
420
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
 
395
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
421
396
                        }
422
397
                        return null;
423
398
                }
443
418
                        setProgressMessage( R.string.doit_caching );
444
419
 
445
420
                        // create the appropriate backend
446
 
                        if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
447
 
                                _backend = new ContactsContractBackend( _doit );
448
 
                        else
 
421
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
 
422
//                              _backend = new ContactsContractBackend( _doit );
 
423
//                      else
449
424
                                _backend = new ContactsBackend( _doit );
450
425
 
451
426
                        // create a cache of existing contacts and populate it
583
558
                checkAbort();
584
559
                _doit._handler.sendMessage( Message.obtain(
585
560
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
586
 
                        Integer.valueOf( max_progress ) ) );
 
561
                        new Integer( max_progress ) ) );
587
562
        }
588
563
 
589
564
        protected void setTmpProgress( int tmp_progress )
592
567
                checkAbort();
593
568
                _doit._handler.sendMessage( Message.obtain(
594
569
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
595
 
                        Integer.valueOf( tmp_progress ) ) );
 
570
                        new Integer( tmp_progress ) ) );
596
571
        }
597
572
 
598
573
        protected void setProgress( int progress ) throws AbortImportException
600
575
                checkAbort();
601
576
                _doit._handler.sendMessage( Message.obtain(
602
577
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
603
 
                        Integer.valueOf( progress ) ) );
 
578
                        new Integer( progress ) ) );
604
579
        }
605
580
 
606
581
        protected void finish( int action ) throws AbortImportException
624
599
                return _doit.getText( res );
625
600
        }
626
601
 
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
 
602
        synchronized private boolean checkForDuplicate(
 
603
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
 
604
                throws AbortImportException
638
605
        {
639
606
                _last_merge_decision = merge_setting;
640
607
 
 
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
 
641
612
                // handle special cases
642
613
                switch( merge_setting )
643
614
                {
644
615
                case Doit.ACTION_KEEP:
645
 
                        // if we are skipping on a duplicate, check for one
646
 
                        return exists;
 
616
                        // if we keep contacts on duplicate, we better check for one
 
617
                        return !_contacts_cache.canLookup( cache_identifier );
647
618
 
648
619
                case Doit.ACTION_PROMPT:
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;
 
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;
652
624
 
653
 
                        // ok, duplicate exists, so do prompt
 
625
                        // ok, it exists, so do prompt
654
626
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
655
 
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
 
627
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
656
628
                        try {
657
629
                                wait();
658
630
                        }
666
638
                                _merge_setting = _response;
667
639
 
668
640
                        // recurse, with our new merge setting
669
 
                        return shouldWeSkipContact( contact_detail, exists, _response );
 
641
                        return checkForDuplicate( cache_identifier, _response );
670
642
                }
671
643
 
672
 
                // for all other cases (either overwriting or merging) we don't skip
673
 
                return false;
 
644
                // for all other cases (either overwriting or merging) we will need the
 
645
                // imported data
 
646
                return true;
674
647
        }
675
648
 
676
649
        protected void skipContact() throws AbortImportException
677
650
        {
678
651
                checkAbort();
679
 
 
680
 
                // show that we're skipping a new contact
681
652
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
682
653
        }
683
654
 
692
663
                ContactsCache.CacheIdentifier cache_identifier =
693
664
                        contact.getCacheIdentifier();
694
665
 
 
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
 
695
672
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
696
673
//                      finish( ACTION_ABORT );
697
674
 
 
675
                // keep track of whether we've informed the UI of what we're doing
 
676
                boolean ui_informed = false;
 
677
 
698
678
                // attempt to lookup the id of an existing contact in the cache with
699
679
                // this contact data's cache identifier
700
680
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
701
681
 
702
 
                // check to see if this contact should be skipped
703
 
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
704
 
                        _merge_setting ) )
 
682
                // does contact exist already?
 
683
                if( id != null )
705
684
                {
706
 
                        // show that we're skipping a contact
707
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
708
 
                        return;
 
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 )
 
690
                        {
 
691
                                // remove from device
 
692
                                _backend.deleteContact( id );
 
693
 
 
694
                                // 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;
 
705
                        }
709
706
                }
710
707
 
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 )
 
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 )
715
711
                {
716
 
                        contact_deleted = true;
717
 
 
718
 
                        // remove from device
719
 
                        _backend.deleteContact( id );
 
712
                        // create a new contact
 
713
                        id = _backend.addContact( contact._name );
 
714
                        if( id == null )
 
715
                                showError( R.string.error_unabletoaddcontact );
720
716
 
721
717
                        // 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 )
736
 
                        {
737
 
                                // create a new contact
738
 
                                id = _backend.addContact( contact._name );
739
 
 
740
 
                                // update cache
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 );
 
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;
748
725
                        }
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() );
764
 
                }
765
 
                catch( Backend.ContactCreationException e )
766
 
                {
767
 
                        showError( R.string.error_unabletoaddcontact );
768
 
                }
 
726
                }
 
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() );
769
742
        }
770
743
 
771
744
        private void importContactPhones( Long id,
772
 
                HashMap< String, ContactData.PreferredDetail > datas )
773
 
                throws ContactCreationException
 
745
                        HashMap< String, ContactData.PreferredDetail > datas )
774
746
        {
775
747
                // add phone numbers
776
748
                Set< String > datas_keys = datas.keySet();
799
771
        }
800
772
 
801
773
        private void importContactEmails( Long id,
802
 
                HashMap< String, ContactData.PreferredDetail > datas )
803
 
                throws ContactCreationException
 
774
                        HashMap< String, ContactData.PreferredDetail > datas )
804
775
        {
805
776
                // add email addresses
806
777
                Set< String > datas_keys = datas.keySet();
825
796
 
826
797
        private void importContactAddresses( Long id,
827
798
                HashMap< String, ContactData.TypeDetail > datas )
828
 
                throws ContactCreationException
829
799
        {
830
800
                // add addresses
831
801
                Set< String > datas_keys = datas.keySet();
850
820
 
851
821
        private void importContactOrganisations( Long id,
852
822
                HashMap< String, ContactData.ExtraDetail > datas )
853
 
                throws ContactCreationException
854
823
        {
855
824
                // add addresses
856
825
                Set< String > datas_keys = datas.keySet();
873
842
                }
874
843
        }
875
844
 
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
 
 
900
845
        synchronized protected void checkAbort() throws AbortImportException
901
846
        {
902
847
                if( _abort ) {