4
* Copyright (C) 2009 to 2011 Tim Marston <edam@waxworlds.org>
4
* Copyright (C) 2009 to 2011 Tim Marston <tim@ed.am>
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
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/>.
24
package org.waxworlds.edam.importcontacts;
24
package am.ed.importcontacts;
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;
123
@SuppressWarnings("serial")
124
protected class ContactNotIdentifiableException extends Exception
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;
141
private ContactsCache.CacheIdentifier _cache_identifier = null;
137
143
protected void setName( String name )
173
179
new ExtraDetail( 0, false, title ) );
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 ) )
221
227
_numbers.put( number,
222
228
new PreferredDetail( type, false ) );
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 ) );
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
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 ) ) )
230
245
_primary_number = number;
246
_primary_number_type = type;
231
247
_primary_number_is_preferred = is_preferred;
269
285
if( !_emails.containsKey( email ) )
270
286
_emails.put( email, new PreferredDetail( type, false ) );
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
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
275
291
if( _primary_email == null ||
276
292
( is_preferred && !_primary_email_is_preferred ) )
328
344
protected void finalise()
345
throws ContactNotIdentifiableException
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() ) );
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();
377
public ContactsCache.CacheIdentifier getCacheIdentifier()
379
return _cache_identifier;
354
382
private String sanitisePhoneNumber( String number )
569
597
return _doit.getText( res );
572
protected boolean isImportRequired( ContactData contact )
573
throws AbortImportException, ContactNeedsMoreInfoException
576
return isImportRequired( contact, _merge_setting );
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
583
604
_last_merge_decision = merge_setting;
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)
592
610
// handle special cases
593
611
switch( merge_setting )
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 );
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 ) )
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() ) );
618
636
_merge_setting = _response;
620
638
// recurse, with our new merge setting
621
return isImportRequired( contact, _response );
639
return checkForDuplicate( cache_identifier, _response );
624
642
// for all other cases (either overwriting or merging) we will need the
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();
664
// check to see if this contact is a duplicate and should be skipped
665
if( !checkForDuplicate( cache_identifier, _merge_setting ) ) {
640
670
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
641
671
// finish( ACTION_ABORT );
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;
647
// give the contact a chance to finalise it's data
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
653
ContactsCache.CacheIdentifier identifier =
654
ContactsCache.createIdentifier( contact );
655
if( identifier != null ) id = (Long)_contacts_cache.lookup( identifier );
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 );
657
680
// does contact exist already?
671
694
_doit.getContentResolver().delete( contact_uri, null, null );
674
_contacts_cache.removeLookup( identifier );
697
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
675
698
_contacts_cache.removeAssociatedData( id );
677
700
// show that we're overwriting a contact
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 );
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 );