/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-05-30 15:33:01 UTC
  • Revision ID: edam@waxworlds.org-20110530153301-oor6ci9b3hf9clul
- refactored some code to do with how contacts are imported
- Vcards (and ContactData) instances now generate a CacheIdentifier when they are finalised so that ContactData instances that do not have enough information to identify them can be discovered then
- importContact() now calls the private method checkForDuplicate(), renamed from isImportRequired(), and return if it is not
- importContact() and checkForDuplicate() now use the ContactData's generated CacheIdentifier

Show diffs side-by-side

added added

removed removed

57
57
        private boolean _is_finished = false;
58
58
        private ContactsCache _contacts_cache = null;
59
59
 
60
 
        @SuppressWarnings("serial")
61
 
        protected class ContactNeedsMoreInfoException extends Exception
62
 
        {
63
 
        }
64
 
 
65
60
        /**
66
61
         * Data about a contact
67
62
         */
122
117
                        }
123
118
                }
124
119
 
 
120
                @SuppressWarnings("serial")
 
121
                protected class ContactNotIdentifiableException extends Exception
 
122
                {
 
123
                }
 
124
 
125
125
                protected String _name = null;
126
126
                protected String _primary_organisation = null;
127
127
                protected boolean _primary_organisation_is_preferred = false;
134
134
                protected HashMap< String, PreferredDetail > _emails = null;
135
135
                protected HashMap< String, TypeDetail > _addresses = null;
136
136
 
 
137
                private ContactsCache.CacheIdentifier _cache_identifier = null;
 
138
 
137
139
                protected void setName( String name )
138
140
                {
139
141
                        _name = name;
326
328
                }
327
329
 
328
330
                protected void finalise()
 
331
                        throws ContactNotIdentifiableException
329
332
                {
330
333
                        // ensure that if there is a primary number, it is preferred so
331
334
                        // that there is always one preferred number. Android will assign
349
352
                                _organisations.put( _primary_organisation,
350
353
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
354
                        }
 
355
 
 
356
                        // create a cache identifier from this contact data, which can be
 
357
                        // used to look-up an existing contact
 
358
                        _cache_identifier = ContactsCache.createIdentifier( this );
 
359
                        if( _cache_identifier == null )
 
360
                                throw new ContactNotIdentifiableException();
 
361
                }
 
362
 
 
363
                public ContactsCache.CacheIdentifier getCacheIdentifier()
 
364
                {
 
365
                        return _cache_identifier;
352
366
                }
353
367
 
354
368
                private String sanitisePhoneNumber( String number )
569
583
                return _doit.getText( res );
570
584
        }
571
585
 
572
 
        protected boolean isImportRequired( ContactData contact )
573
 
                        throws AbortImportException, ContactNeedsMoreInfoException
574
 
        {
575
 
                checkAbort();
576
 
                return isImportRequired( contact, _merge_setting );
577
 
        }
578
 
 
579
 
        synchronized private boolean isImportRequired(
580
 
                ContactData contact, int merge_setting )
581
 
                throws AbortImportException, ContactNeedsMoreInfoException
 
586
        synchronized private boolean checkForDuplicate(
 
587
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
 
588
                throws AbortImportException
582
589
        {
583
590
                _last_merge_decision = merge_setting;
584
591
 
585
 
                // create a cache identifier which we can use to detect if this contact
586
 
                // is valid for importing
587
 
                ContactsCache.CacheIdentifier identifier =
588
 
                        ContactsCache.createIdentifier( contact );
589
 
                if( identifier == null )
590
 
                        throw new ContactNeedsMoreInfoException();
 
592
                // it is ok to use contact.getCacheIdentifier(). The contact has already
 
593
                // been finalised, which means a valid cache identifier will have been
 
594
                // created for it (or it would have been skipped)
591
595
 
592
596
                // handle special cases
593
597
                switch( merge_setting )
594
598
                {
595
599
                case Doit.ACTION_KEEP:
596
600
                        // if we keep contacts on duplicate, we better check for one
597
 
                        return !_contacts_cache.canLookup( identifier );
 
601
                        return !_contacts_cache.canLookup( cache_identifier );
598
602
 
599
603
                case Doit.ACTION_PROMPT:
600
604
                        // if we are prompting on duplicate, we better check for one and if
601
605
                        // the contact doesn'te exist, we want to import it
602
 
                        if( !_contacts_cache.canLookup( identifier ) )
 
606
                        if( !_contacts_cache.canLookup( cache_identifier ) )
603
607
                                return true;
604
608
 
605
609
                        // ok, it exists, so do prompt
606
610
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
607
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
611
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
608
612
                        try {
609
613
                                wait();
610
614
                        }
618
622
                                _merge_setting = _response;
619
623
 
620
624
                        // recurse, with our new merge setting
621
 
                        return isImportRequired( contact, _response );
 
625
                        return checkForDuplicate( cache_identifier, _response );
622
626
                }
623
627
 
624
628
                // for all other cases (either overwriting or merging) we will need the
637
641
        {
638
642
                checkAbort();
639
643
 
 
644
                // It is expected that we use contact.getCacheIdentifier() here. The
 
645
                // contact we are passed should have been successfully finalise()d,
 
646
                // which includes generating a valid cache identifier.
 
647
                ContactsCache.CacheIdentifier cache_identifier =
 
648
                        contact.getCacheIdentifier();
 
649
 
 
650
                // check to see if this contact is a duplicate and should be skipped
 
651
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
 
652
                        skipContact();
 
653
                        return;
 
654
                }
 
655
 
640
656
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
657
//                      finish( ACTION_ABORT );
642
658
 
643
 
                ContentValues values = new ContentValues();
 
659
                // keep track of whether we've informed the UI of what we're doing
644
660
                boolean ui_informed = false;
645
 
                Long id = null;
646
 
 
647
 
                // give the contact a chance to finalise it's data
648
 
                contact.finalise();
649
 
 
650
 
                // create something, from the contact data, that we can use to identify
651
 
                // a cache entry and attempt to lookup the id of an existing contact in
652
 
                // the cache with it
653
 
                ContactsCache.CacheIdentifier identifier =
654
 
                        ContactsCache.createIdentifier( contact );
655
 
                if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
 
661
 
 
662
                // attempt to lookup the id of an existing contact in the cache with
 
663
                // this contact data's cache identifier
 
664
                Long id = (Long)_contacts_cache.lookup( cache_identifier );
656
665
 
657
666
                // does contact exist already?
658
667
                if( id != null )
671
680
                                _doit.getContentResolver().delete( contact_uri, null, null );
672
681
 
673
682
                                // update cache
674
 
                                _contacts_cache.removeLookup( identifier );
 
683
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
675
684
                                _contacts_cache.removeAssociatedData( id );
676
685
 
677
686
                                // show that we're overwriting a contact
689
698
                if( id == null )
690
699
                {
691
700
                        // create a new contact
 
701
                        ContentValues values = new ContentValues();
692
702
                        values.put( Contacts.People.NAME, contact._name );
693
703
                        Uri contact_uri = _doit.getContentResolver().insert(
694
704
                                Contacts.People.CONTENT_URI, values );
718
728
                }
719
729
 
720
730
                // if we haven't already shown that we're overwriting or creating a
721
 
                // contact show that we're merging a contact
 
731
                // contact, show that we're merging a contact
722
732
                if( !ui_informed )
723
733
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
724
734