/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-04-24 09:56:17 UTC
  • Revision ID: tim@ed.am-20120424095617-lsfkkpkrlqk9xthn
updated all URLs, email addresses and package names to ed.am

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 <edam@waxworlds.org>
 
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
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
 
8
 * http://ed.am/dev/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 org.waxworlds.edam.importcontacts;
 
24
package am.ed.importcontacts;
25
25
 
 
26
import java.util.Arrays;
26
27
import java.util.HashMap;
 
28
import java.util.HashSet;
27
29
import java.util.Iterator;
28
30
import java.util.Set;
29
31
import java.util.regex.Matcher;
35
37
import android.net.Uri;
36
38
import android.os.Message;
37
39
import android.provider.Contacts;
 
40
import android.provider.Contacts.PhonesColumns;
38
41
 
39
42
 
40
43
public class Importer extends Thread
57
60
        private boolean _is_finished = false;
58
61
        private ContactsCache _contacts_cache = null;
59
62
 
60
 
        @SuppressWarnings("serial")
61
 
        protected class ContactNeedsMoreInfoException extends Exception
62
 
        {
63
 
        }
64
 
 
65
63
        /**
66
64
         * Data about a contact
67
65
         */
122
120
                        }
123
121
                }
124
122
 
 
123
                @SuppressWarnings("serial")
 
124
                protected class ContactNotIdentifiableException extends Exception
 
125
                {
 
126
                }
 
127
 
125
128
                protected String _name = null;
126
129
                protected String _primary_organisation = null;
127
 
                protected boolean _primary_organisation_is_preferred = false;
 
130
                protected boolean _primary_organisation_is_preferred;
128
131
                protected String _primary_number = null;
129
 
                protected boolean _primary_number_is_preferred = false;
 
132
                protected int _primary_number_type;
 
133
                protected boolean _primary_number_is_preferred;
130
134
                protected String _primary_email = null;
131
 
                protected boolean _primary_email_is_preferred = false;
 
135
                protected boolean _primary_email_is_preferred;
132
136
                protected HashMap< String, ExtraDetail > _organisations = null;
133
137
                protected HashMap< String, PreferredDetail > _numbers = null;
134
138
                protected HashMap< String, PreferredDetail > _emails = null;
135
139
                protected HashMap< String, TypeDetail > _addresses = null;
136
140
 
 
141
                private ContactsCache.CacheIdentifier _cache_identifier = null;
 
142
 
137
143
                protected void setName( String name )
138
144
                {
139
145
                        _name = name;
173
179
                                        new ExtraDetail( 0, false, title ) );
174
180
 
175
181
                        // if this is the first organisation added, or it's a preferred
176
 
                        // organisation and a previous organisation wasn't, then remember
177
 
                        // that this is the "primary organisation".
 
182
                        // organisation and the current primary organisation isn't, then
 
183
                        // record this as the primary organisation.
178
184
                        if( _primary_organisation == null ||
179
185
                                ( is_preferred && !_primary_organisation_is_preferred ) )
180
186
                        {
221
227
                                _numbers.put( number,
222
228
                                        new PreferredDetail( type, false ) );
223
229
 
 
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
 
224
234
                        // if this is the first number added, or it's a preferred number
225
 
                        // and a previous number wasn't, then remember that this is the
226
 
                        // "primary 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.
227
239
                        if( _primary_number == null ||
228
 
                                ( is_preferred && !_primary_number_is_preferred ) )
 
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 ) ) )
229
244
                        {
230
245
                                _primary_number = number;
 
246
                                _primary_number_type = type;
231
247
                                _primary_number_is_preferred = is_preferred;
232
248
                        }
233
249
                }
269
285
                        if( !_emails.containsKey( email ) )
270
286
                                _emails.put( email, new PreferredDetail( type, false ) );
271
287
 
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".
 
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.
275
291
                        if( _primary_email == null ||
276
292
                                ( is_preferred && !_primary_email_is_preferred ) )
277
293
                        {
326
342
                }
327
343
 
328
344
                protected void finalise()
 
345
                        throws ContactNotIdentifiableException
329
346
                {
330
347
                        // ensure that if there is a primary number, it is preferred so
331
348
                        // that there is always one preferred number. Android will assign
349
366
                                _organisations.put( _primary_organisation,
350
367
                                        new ExtraDetail( 0, true, data.getExtra() ) );
351
368
                        }
 
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;
352
380
                }
353
381
 
354
382
                private String sanitisePhoneNumber( String number )
569
597
                return _doit.getText( res );
570
598
        }
571
599
 
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
 
600
        synchronized private boolean checkForDuplicate(
 
601
                ContactsCache.CacheIdentifier cache_identifier, int merge_setting )
 
602
                throws AbortImportException
582
603
        {
583
604
                _last_merge_decision = merge_setting;
584
605
 
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();
 
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)
591
609
 
592
610
                // handle special cases
593
611
                switch( merge_setting )
594
612
                {
595
613
                case Doit.ACTION_KEEP:
596
614
                        // if we keep contacts on duplicate, we better check for one
597
 
                        return !_contacts_cache.canLookup( identifier );
 
615
                        return !_contacts_cache.canLookup( cache_identifier );
598
616
 
599
617
                case Doit.ACTION_PROMPT:
600
618
                        // if we are prompting on duplicate, we better check for one and if
601
619
                        // the contact doesn'te exist, we want to import it
602
 
                        if( !_contacts_cache.canLookup( identifier ) )
 
620
                        if( !_contacts_cache.canLookup( cache_identifier ) )
603
621
                                return true;
604
622
 
605
623
                        // ok, it exists, so do prompt
606
624
                        _doit._handler.sendMessage( Message.obtain( _doit._handler,
607
 
                                Doit.MESSAGE_MERGEPROMPT, identifier.getDetail() ) );
 
625
                                Doit.MESSAGE_MERGEPROMPT, cache_identifier.getDetail() ) );
608
626
                        try {
609
627
                                wait();
610
628
                        }
618
636
                                _merge_setting = _response;
619
637
 
620
638
                        // recurse, with our new merge setting
621
 
                        return isImportRequired( contact, _response );
 
639
                        return checkForDuplicate( cache_identifier, _response );
622
640
                }
623
641
 
624
642
                // for all other cases (either overwriting or merging) we will need the
637
655
        {
638
656
                checkAbort();
639
657
 
 
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
 
640
670
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
671
//                      finish( ACTION_ABORT );
642
672
 
643
 
                ContentValues values = new ContentValues();
 
673
                // keep track of whether we've informed the UI of what we're doing
644
674
                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 );
 
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 );
656
679
 
657
680
                // does contact exist already?
658
681
                if( id != null )
671
694
                                _doit.getContentResolver().delete( contact_uri, null, null );
672
695
 
673
696
                                // update cache
674
 
                                _contacts_cache.removeLookup( identifier );
 
697
                                _contacts_cache.removeLookup( contact.getCacheIdentifier() );
675
698
                                _contacts_cache.removeAssociatedData( id );
676
699
 
677
700
                                // show that we're overwriting a contact
689
712
                if( id == null )
690
713
                {
691
714
                        // create a new contact
 
715
                        ContentValues values = new ContentValues();
692
716
                        values.put( Contacts.People.NAME, contact._name );
693
717
                        Uri contact_uri = _doit.getContentResolver().insert(
694
718
                                Contacts.People.CONTENT_URI, values );
718
742
                }
719
743
 
720
744
                // if we haven't already shown that we're overwriting or creating a
721
 
                // contact show that we're merging a contact
 
745
                // contact, show that we're merging a contact
722
746
                if( !ui_informed )
723
747
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
724
748