/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

1
1
/*
2
2
 * Importer.java
3
3
 *
4
 
 * Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
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
8
 
 * http://ed.am/dev/android/import-contacts
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
 
import java.util.Arrays;
27
26
import java.util.HashMap;
28
 
import java.util.HashSet;
29
27
import java.util.Iterator;
30
28
import java.util.Set;
31
29
import java.util.regex.Matcher;
37
35
import android.net.Uri;
38
36
import android.os.Message;
39
37
import android.provider.Contacts;
40
 
import android.provider.Contacts.PhonesColumns;
41
38
 
42
39
 
43
40
public class Importer extends Thread
60
57
        private boolean _is_finished = false;
61
58
        private ContactsCache _contacts_cache = null;
62
59
 
 
60
        @SuppressWarnings("serial")
 
61
        protected class ContactNeedsMoreInfoException extends Exception
 
62
        {
 
63
        }
 
64
 
63
65
        /**
64
66
         * Data about a contact
65
67
         */
120
122
                        }
121
123
                }
122
124
 
123
 
                @SuppressWarnings("serial")
124
 
                protected class ContactNotIdentifiableException extends Exception
125
 
                {
126
 
                }
127
 
 
128
125
                protected String _name = null;
129
126
                protected String _primary_organisation = null;
130
 
                protected boolean _primary_organisation_is_preferred;
 
127
                protected boolean _primary_organisation_is_preferred = false;
131
128
                protected String _primary_number = null;
132
 
                protected int _primary_number_type;
133
 
                protected boolean _primary_number_is_preferred;
 
129
                protected boolean _primary_number_is_preferred = false;
134
130
                protected String _primary_email = null;
135
 
                protected boolean _primary_email_is_preferred;
 
131
                protected boolean _primary_email_is_preferred = false;
136
132
                protected HashMap< String, ExtraDetail > _organisations = null;
137
133
                protected HashMap< String, PreferredDetail > _numbers = null;
138
134
                protected HashMap< String, PreferredDetail > _emails = null;
139
135
                protected HashMap< String, TypeDetail > _addresses = null;
140
136
 
141
 
                private ContactsCache.CacheIdentifier _cache_identifier = null;
142
 
 
143
137
                protected void setName( String name )
144
138
                {
145
139
                        _name = name;
179
173
                                        new ExtraDetail( 0, false, title ) );
180
174
 
181
175
                        // if this is the first organisation added, or it's a preferred
182
 
                        // organisation and the current primary organisation isn't, then
183
 
                        // record this as the primary organisation.
 
176
                        // organisation and a previous organisation wasn't, then remember
 
177
                        // that this is the "primary organisation".
184
178
                        if( _primary_organisation == null ||
185
179
                                ( is_preferred && !_primary_organisation_is_preferred ) )
186
180
                        {
227
221
                                _numbers.put( number,
228
222
                                        new PreferredDetail( type, false ) );
229
223
 
230
 
                        final Set< Integer > non_voice_types = new HashSet< Integer >(
231
 
                                Arrays.asList( PhonesColumns.TYPE_FAX_HOME,
232
 
                                        PhonesColumns.TYPE_FAX_WORK, PhonesColumns.TYPE_PAGER ) );
233
 
 
234
224
                        // if this is the first number added, or it's a preferred number
235
 
                        // and the current primary number isn't, or this number is on equal
236
 
                        // standing with the primary number in terms of preference and it is
237
 
                        // a voice number and the primary number isn't, then record this as
238
 
                        // the primary number.
 
225
                        // and a previous number wasn't, then remember that this is the
 
226
                        // "primary number".
239
227
                        if( _primary_number == null ||
240
 
                                ( is_preferred && !_primary_number_is_preferred ) ||
241
 
                                ( is_preferred == _primary_number_is_preferred &&
242
 
                                        !non_voice_types.contains( type ) &&
243
 
                                        non_voice_types.contains( _primary_number_type ) ) )
 
228
                                ( is_preferred && !_primary_number_is_preferred ) )
244
229
                        {
245
230
                                _primary_number = number;
246
 
                                _primary_number_type = type;
247
231
                                _primary_number_is_preferred = is_preferred;
248
232
                        }
249
233
                }
285
269
                        if( !_emails.containsKey( email ) )
286
270
                                _emails.put( email, new PreferredDetail( type, false ) );
287
271
 
288
 
                        // if this is the first email added, or it's a preferred email and
289
 
                        // the current primary organisation isn't, then record this as the
290
 
                        // primary email.
 
272
                        // if this is the first email added, or it's a preferred email
 
273
                        // and a previous email wasn't, then remember that this is the
 
274
                        // "primary email".
291
275
                        if( _primary_email == null ||
292
276
                                ( is_preferred && !_primary_email_is_preferred ) )
293
277
                        {
342
326
                }
343
327
 
344
328
                protected void finalise()
345
 
                        throws ContactNotIdentifiableException
346
329
                {
347
330
                        // ensure that if there is a primary number, it is preferred so
348
331
                        // that there is always one preferred number. Android will assign
366
349
                                _organisations.put( _primary_organisation,
367
350
                                        new ExtraDetail( 0, true, data.getExtra() ) );
368
351
                        }
369
 
 
370
 
                        // create a cache identifier from this contact data, which can be
371
 
                        // used to look-up an existing contact
372
 
                        _cache_identifier = ContactsCache.createIdentifier( this );
373
 
                        if( _cache_identifier == null )
374
 
                                throw new ContactNotIdentifiableException();
375
 
                }
376
 
 
377
 
                public ContactsCache.CacheIdentifier getCacheIdentifier()
378
 
                {
379
 
                        return _cache_identifier;
380
352
                }
381
353
 
382
354
                private String sanitisePhoneNumber( String number )
597
569
                return _doit.getText( res );
598
570
        }
599
571
 
600
 
        synchronized private boolean checkForDuplicate(
601
 
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
602
 
                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
603
582
        {
604
583
                _last_merge_decision = merge_setting;
605
584
 
606
 
                // it is ok to use contact.getCacheIdentifier(). The contact has already
607
 
                // been finalised, which means a valid cache identifier will have been
608
 
                // 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();
609
591
 
610
592
                // handle special cases
611
593
                switch( merge_setting )
612
594
                {
613
595
                case Doit.ACTION_KEEP:
614
596
                        // if we keep contacts on duplicate, we better check for one
615
 
                        return !_contacts_cache.canLookup( cache_identifier );
 
597
                        return !_contacts_cache.canLookup( identifier );
616
598
 
617
599
                case Doit.ACTION_PROMPT:
618
600
                        // if we are prompting on duplicate, we better check for one and if
619
601
                        // the contact doesn'te exist, we want to import it
620
 
                        if( !_contacts_cache.canLookup( cache_identifier ) )
 
602
                        if( !_contacts_cache.canLookup( identifier ) )
621
603
                                return true;
622
604
 
623
605
                        // ok, it exists, so do prompt
624
606
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
625
 
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
 
607
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
626
608
                        try {
627
609
                                wait();
628
610
                        }
636
618
                                _merge_setting = _response;
637
619
 
638
620
                        // recurse, with our new merge setting
639
 
                        return checkForDuplicate( cache_identifier, _response );
 
621
                        return isImportRequired( contact, _response );
640
622
                }
641
623
 
642
624
                // for all other cases (either overwriting or merging) we will need the
655
637
        {
656
638
                checkAbort();
657
639
 
658
 
                // It is expected that we use contact.getCacheIdentifier() here. The
659
 
                // contact we are passed should have been successfully finalise()d,
660
 
                // which includes generating a valid cache identifier.
661
 
                ContactsCache.CacheIdentifier cache_identifier =
662
 
                        contact.getCacheIdentifier();
663
 
 
664
 
                // check to see if this contact is a duplicate and should be skipped
665
 
                if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
666
 
                        skipContact();
667
 
                        return;
668
 
                }
669
 
 
670
640
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
671
641
//                      finish( ACTION_ABORT );
672
642
 
673
 
                // keep track of whether we've informed the UI of what we're doing
 
643
                ContentValues values = new ContentValues();
674
644
                boolean ui_informed = false;
675
 
 
676
 
                // attempt to lookup the id of an existing contact in the cache with
677
 
                // this contact data's cache identifier
678
 
                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 );
679
656
 
680
657
                // does contact exist already?
681
658
                if( id != null )
694
671
                                _doit.getContentResolver().delete( contact_uri, null, null );
695
672
 
696
673
                                // update cache
697
 
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
 
674
                                _contacts_cache.removeLookup( identifier );
698
675
                                _contacts_cache.removeAssociatedData( id );
699
676
 
700
677
                                // show that we're overwriting a contact
712
689
                if( id == null )
713
690
                {
714
691
                        // create a new contact
715
 
                        ContentValues values = new ContentValues();
716
692
                        values.put( Contacts.People.NAME, contact._name );
717
693
                        Uri contact_uri = _doit.getContentResolver().insert(
718
694
                                Contacts.People.CONTENT_URI, values );
742
718
                }
743
719
 
744
720
                // if we haven't already shown that we're overwriting or creating a
745
 
                // contact, show that we're merging a contact
 
721
                // contact show that we're merging a contact
746
722
                if( !ui_informed )
747
723
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
748
724