/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-05-08 15:09:13 UTC
  • Revision ID: tim@ed.am-20120508150913-t9qzbbs1kld691dy
abstracted the android contacts API in to an interface, ready to be switched to
another depending on the platform version.

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2012 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
7
 * to as "this program"). For more information, see
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;
 
37
 
38
38
 
39
39
public class Importer extends Thread
40
40
{
62
62
         */
63
63
        public class ContactData
64
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
 
 
72
65
                class TypeDetail
73
66
                {
74
67
                        protected int _type;
232
225
                                        new PreferredDetail( type, false ) );
233
226
 
234
227
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
235
 
                                Arrays.asList( TYPE_FAX_HOME, TYPE_FAX_WORK, TYPE_PAGER ) );
 
228
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
 
229
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
236
230
 
237
231
                        // if this is the first number added, or it's a preferred number
238
232
                        // and the current primary number isn't, or this number is on equal
277
271
                        email = sanitisesEmailAddress( email );
278
272
                        if( email == null )
279
273
                        {
280
 
                                // TODO: warn that an imported email address is being ignored
 
274
                                // TODO: warn that an imported email addtrss is being ignored
281
275
                                return;
282
276
                        }
283
277
 
399
393
                        Matcher m = p.matcher( email );
400
394
                        if( m.matches() ) {
401
395
                                String[] bits = email.split( "@" );
402
 
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase( Locale.US );
 
396
                                return bits[ 0 ] + "@" + bits[ 1 ].toLowerCase();
403
397
                        }
404
398
                        return null;
405
399
                }
424
418
                        // update UI
425
419
                        setProgressMessage( R.string.doit_caching );
426
420
 
427
 
                        // create the appropriate backend
428
421
//                      if( Integer.parseInt( android.os.Build.VERSION.SDK ) >= 5 )
429
 
//                              _backend = new ContactsContractBackend( _doit );
 
422
//                              _backend = new ContactsContractBackend();
430
423
//                      else
431
424
                                _backend = new ContactsBackend( _doit );
432
425
 
565
558
                checkAbort();
566
559
                _doit._handler.sendMessage( Message.obtain(
567
560
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
568
 
                        Integer.valueOf( max_progress ) ) );
 
561
                        new Integer( max_progress ) ) );
569
562
        }
570
563
 
571
564
        protected void setTmpProgress( int tmp_progress )
574
567
                checkAbort();
575
568
                _doit._handler.sendMessage( Message.obtain(
576
569
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
577
 
                        Integer.valueOf( tmp_progress ) ) );
 
570
                        new Integer( tmp_progress ) ) );
578
571
        }
579
572
 
580
573
        protected void setProgress( int progress ) throws AbortImportException
582
575
                checkAbort();
583
576
                _doit._handler.sendMessage( Message.obtain(
584
577
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
585
 
                        Integer.valueOf( progress ) ) );
 
578
                        new Integer( progress ) ) );
586
579
        }
587
580
 
588
581
        protected void finish( int action ) throws AbortImportException
606
599
                return _doit.getText( res );
607
600
        }
608
601
 
609
 
        /**
610
 
         * Should we skip a contact, given whether it exists or not and the current
611
 
         * merge setting?  This routine handles throwing up a prompt, if required.
612
 
         * @param contact_detail the display name of the contact
613
 
         * @param exists true if this contact matches one in the cache
614
 
         * @param merge_setting the merge setting to use
615
 
         * @return true if the contact should be skipped outright
616
 
         * @throws AbortImportException
617
 
         */
618
 
        synchronized private boolean shouldWeSkipContact( String contact_detail,
619
 
                boolean exists, int merge_setting ) throws AbortImportException
 
602
        synchronized private boolean checkForDuplicate(
 
603
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
 
604
                throws AbortImportException
620
605
        {
621
606
                _last_merge_decision = merge_setting;
622
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
 
623
612
                // handle special cases
624
613
                switch( merge_setting )
625
614
                {
626
615
                case Doit.ACTION_KEEP:
627
 
                        // if we are skipping on a duplicate, check for one
628
 
                        return exists;
 
616
                        // if we keep contacts on duplicate, we better check for one
 
617
                        return !_contacts_cache.canLookup( cache_identifier );
629
618
 
630
619
                case Doit.ACTION_PROMPT:
631
 
                        // if we are prompting on duplicate, then we can say that we won't
632
 
                        // skip if there isn't one
633
 
                        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;
634
624
 
635
 
                        // ok, duplicate exists, so do prompt
 
625
                        // ok, it exists, so do prompt
636
626
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
637
 
                                Doit.MESSAGE_MERGEPROMPT, contact_detail ) );
 
627
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
638
628
                        try {
639
629
                                wait();
640
630
                        }
648
638
                                _merge_setting = _response;
649
639
 
650
640
                        // recurse, with our new merge setting
651
 
                        return shouldWeSkipContact( contact_detail, exists, _response );
 
641
                        return checkForDuplicate( cache_identifier, _response );
652
642
                }
653
643
 
654
 
                // for all other cases (either overwriting or merging) we don't skip
655
 
                return false;
 
644
                // for all other cases (either overwriting or merging) we will need the
 
645
                // imported data
 
646
                return true;
656
647
        }
657
648
 
658
649
        protected void skipContact() throws AbortImportException
659
650
        {
660
651
                checkAbort();
661
 
 
662
 
                // show that we're skipping a new contact
663
652
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
664
653
        }
665
654
 
674
663
                ContactsCache.CacheIdentifier cache_identifier =
675
664
                        contact.getCacheIdentifier();
676
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
 
677
672
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
678
673
//                      finish( ACTION_ABORT );
679
674
 
 
675
                // keep track of whether we've informed the UI of what we're doing
 
676
                boolean ui_informed = false;
 
677
 
680
678
                // attempt to lookup the id of an existing contact in the cache with
681
679
                // this contact data's cache identifier
682
680
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
683
681
 
684
 
                // check to see if this contact should be skipped
685
 
                if( shouldWeSkipContact( cache_identifier.getDetail(), id != null,
686
 
                        _merge_setting ) )
 
682
                // does contact exist already?
 
683
                if( id != null )
687
684
                {
688
 
                        // show that we're skipping a contact
689
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
690
 
                        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( contact.getCacheIdentifier() );
 
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
                        }
691
706
                }
692
707
 
693
 
                // if a contact exists, and we're overwriting, destroy the existing
694
 
                // contact before importing
695
 
                boolean contact_deleted = false;
696
 
                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 )
697
711
                {
698
 
                        contact_deleted = true;
699
 
 
700
 
                        // remove from device
701
 
                        _backend.deleteContact( id );
 
712
                        // create a new contact
 
713
                        id = _backend.addContact( contact._name );
 
714
                        if( id == null || id <= 0 )
 
715
                                showError( R.string.error_unabletoaddcontact );
702
716
 
703
717
                        // update cache
704
 
                        _contacts_cache.removeLookup( cache_identifier );
705
 
                        _contacts_cache.removeAssociatedData( id );
706
 
 
707
 
                        // show that we're overwriting a contact
708
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
709
 
 
710
 
                        // discard the contact id
711
 
                        id = null;
712
 
                }
713
 
 
714
 
                try {
715
 
                        // if we don't have a contact id yet (or we did, but we destroyed it
716
 
                        // when we deleted the contact), we'll have to create a new contact
717
 
                        if( id == null )
718
 
                        {
719
 
                                // create a new contact
720
 
                                id = _backend.addContact( contact._name );
721
 
 
722
 
                                // update cache
723
 
                                _contacts_cache.addLookup( cache_identifier, id );
724
 
 
725
 
                                // if we haven't already shown that we're overwriting a contact,
726
 
                                // show that we're creating a new contact
727
 
                                if( !contact_deleted )
728
 
                                        _doit._handler.sendEmptyMessage(
729
 
                                                Doit.MESSAGE_CONTACTCREATED );
 
718
                        _contacts_cache.addLookup(
 
719
                                ContactsCache.createIdentifier( contact ), id );
 
720
 
 
721
                        // if we haven't already shown that we're overwriting a contact,
 
722
                        // show that we're creating a new contact
 
723
                        if( !ui_informed ) {
 
724
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
 
725
                                ui_informed = true;
730
726
                        }
731
 
                        else
732
 
                                // show that we're merging with an existing contact
733
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
734
 
 
735
 
                        // import contact parts
736
 
                        if( contact.hasNumbers() )
737
 
                                importContactPhones( id, contact.getNumbers() );
738
 
                        if( contact.hasEmails() )
739
 
                                importContactEmails( id, contact.getEmails() );
740
 
                        if( contact.hasAddresses() )
741
 
                                importContactAddresses( id, contact.getAddresses() );
742
 
                        if( contact.hasOrganisations() )
743
 
                                importContactOrganisations( id, contact.getOrganisations() );
744
 
                }
745
 
                catch( Backend.ContactCreationException e )
746
 
                {
747
 
                        showError( R.string.error_unabletoaddcontact );
748
 
                }
 
727
                }
 
728
 
 
729
                // if we haven't already shown that we're overwriting or creating a
 
730
                // contact, show that we're merging a contact
 
731
                if( !ui_informed )
 
732
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
733
 
 
734
                // import contact parts
 
735
                if( contact.hasNumbers() )
 
736
                        importContactPhones( id, contact.getNumbers() );
 
737
                if( contact.hasEmails() )
 
738
                        importContactEmails( id, contact.getEmails() );
 
739
                if( contact.hasAddresses() )
 
740
                        importContactAddresses( id, contact.getAddresses() );
 
741
                if( contact.hasOrganisations() )
 
742
                        importContactOrganisations( id, contact.getOrganisations() );
749
743
        }
750
744
 
751
745
        private void importContactPhones( Long id,
752
 
                HashMap< String, ContactData.PreferredDetail > datas )
753
 
                throws ContactCreationException
 
746
                        HashMap< String, ContactData.PreferredDetail > datas )
754
747
        {
755
748
                // add phone numbers
756
749
                Set< String > datas_keys = datas.keySet();
779
772
        }
780
773
 
781
774
        private void importContactEmails( Long id,
782
 
                HashMap< String, ContactData.PreferredDetail > datas )
783
 
                throws ContactCreationException
 
775
                        HashMap< String, ContactData.PreferredDetail > datas )
784
776
        {
785
777
                // add email addresses
786
778
                Set< String > datas_keys = datas.keySet();
805
797
 
806
798
        private void importContactAddresses( Long id,
807
799
                HashMap< String, ContactData.TypeDetail > datas )
808
 
                throws ContactCreationException
809
800
        {
810
801
                // add addresses
811
802
                Set< String > datas_keys = datas.keySet();
830
821
 
831
822
        private void importContactOrganisations( Long id,
832
823
                HashMap< String, ContactData.ExtraDetail > datas )
833
 
                throws ContactCreationException
834
824
        {
835
825
                // add addresses
836
826
                Set< String > datas_keys = datas.keySet();