24
24
package org.waxworlds.edam.importcontacts;
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;
123
@SuppressWarnings("serial")
124
protected class ContactNotIdentifiableException extends Exception
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;
141
private ContactsCache.CacheIdentifier _cache_identifier = null;
143
137
protected void setName( String name )
179
173
new ExtraDetail( 0, false, title ) );
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 ) )
227
221
_numbers.put( number,
228
222
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 ) );
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
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 ) )
245
230
_primary_number = number;
246
_primary_number_type = type;
247
231
_primary_number_is_preferred = is_preferred;
285
269
if( !_emails.containsKey( email ) )
286
270
_emails.put( email, new PreferredDetail( type, false ) );
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
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
291
275
if( _primary_email == null ||
292
276
( is_preferred && !_primary_email_is_preferred ) )
344
328
protected void finalise()
345
throws ContactNotIdentifiableException
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() ) );
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;
382
354
private String sanitisePhoneNumber( String number )
597
569
return _doit.getText( res );
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
576
return isImportRequired( contact, _merge_setting );
579
synchronized private boolean isImportRequired(
580
ContactData contact, int merge_setting )
581
throws AbortImportException, ContactNeedsMoreInfoException
604
583
_last_merge_decision = merge_setting;
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();
610
592
// handle special cases
611
593
switch( merge_setting )
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 );
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 ) )
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() ) );
636
618
_merge_setting = _response;
638
620
// recurse, with our new merge setting
639
return checkForDuplicate( cache_identifier, _response );
621
return isImportRequired( contact, _response );
642
624
// 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 ) ) {
670
640
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
671
641
// finish( ACTION_ABORT );
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;
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 );
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 );
680
657
// does contact exist already?
694
671
_doit.getContentResolver().delete( contact_uri, null, null );
697
_contacts_cache.removeLookup( contact.getCacheIdentifier() );
674
_contacts_cache.removeLookup( identifier );
698
675
_contacts_cache.removeAssociatedData( id );
700
677
// show that we're overwriting a contact
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 );
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 );