/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 14:04:51 UTC
  • Revision ID: edam@waxworlds.org-20110530140451-d99fy3zoi6zq1jf2
- renamed VCFImporter to VcardImporter and VCard to Vcard

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
 
60
65
        /**
61
66
         * Data about a contact
62
67
         */
117
122
                        }
118
123
                }
119
124
 
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
 
 
139
137
                protected void setName( String name )
140
138
                {
141
139
                        _name = name;
328
326
                }
329
327
 
330
328
                protected void finalise()
331
 
                        throws ContactNotIdentifiableException
332
329
                {
333
330
                        // ensure that if there is a primary number, it is preferred so
334
331
                        // that there is always one preferred number. Android will assign
352
349
                                _organisations.put( _primary_organisation,
353
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
354
351
                        }
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;
366
352
                }
367
353
 
368
354
                private String sanitisePhoneNumber( String number )
583
569
                return _doit.getText( res );
584
570
        }
585
571
 
586
 
        synchronized private boolean checkForDuplicate(
587
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
588
 
                throws AbortImportException
 
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
589
582
        {
590
583
                _last_merge_decision = merge_setting;
591
584
 
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)
 
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();
595
591
 
596
592
                // handle special cases
597
593
                switch( merge_setting )
598
594
                {
599
595
                case Doit.ACTION_KEEP:
600
596
                        // if we keep contacts on duplicate, we better check for one
601
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
597
                        return !_contacts_cache.canLookup( identifier );
602
598
 
603
599
                case Doit.ACTION_PROMPT:
604
600
                        // if we are prompting on duplicate, we better check for one and if
605
601
                        // the contact doesn'te exist, we want to import it
606
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
 
602
                        if( !_contacts_cache.canLookup( identifier ) )
607
603
                                return true;
608
604
 
609
605
                        // ok, it exists, so do prompt
610
606
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
611
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
607
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
612
608
                        try {
613
609
                                wait();
614
610
                        }
622
618
                                _merge_setting = _response;
623
619
 
624
620
                        // recurse, with our new merge setting
625
 
                        return checkForDuplicate( cache_identifier, _response );
 
621
                        return isImportRequired( contact, _response );
626
622
                }
627
623
 
628
624
                // for all other cases (either overwriting or merging) we will need the
641
637
        {
642
638
                checkAbort();
643
639
 
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
 
 
656
640
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
657
641
//                      finish( ACTION_ABORT );
658
642
 
659
 
                // keep track of whether we've informed the UI of what we're doing
 
643
                ContentValues values = new ContentValues();
660
644
                boolean ui_informed = false;
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 );
 
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 );
665
656
 
666
657
                // does contact exist already?
667
658
                if( id != null )
680
671
                                _doit.getContentResolver().delete( contact_uri, null, null );
681
672
 
682
673
                                // update cache
683
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
 
674
                                _contacts_cache.removeLookup( identifier );
684
675
                                _contacts_cache.removeAssociatedData( id );
685
676
 
686
677
                                // show that we're overwriting a contact
698
689
                if( id == null )
699
690
                {
700
691
                        // create a new contact
701
 
                        ContentValues values = new ContentValues();
702
692
                        values.put( Contacts.People.NAME, contact._name );
703
693
                        Uri contact_uri = _doit.getContentResolver().insert(
704
694
                                Contacts.People.CONTENT_URI, values );
728
718
                }
729
719
 
730
720
                // if we haven't already shown that we're overwriting or creating a
731
 
                // contact, show that we're merging a contact
 
721
                // contact show that we're merging a contact
732
722
                if( !ui_informed )
733
723
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
734
724