157
141
if( !_emails.containsKey( email ) )
158
142
_emails.put( email, new EmailData( email, type, isPreferred ) );
161
protected void addAddress( String address, int type )
163
if( _addresses == null ) _addresses =
164
new HashMap< String, AddressData >();
165
if( !_addresses.containsKey( address ) )
166
_addresses.put( address, new AddressData( address, type ) );
170
@SuppressWarnings("serial")
171
146
protected class AbortImportException extends Exception { };
173
148
public Importer( Doit doit )
322
291
_doit._handler.sendMessage( Message.obtain(
323
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
new Integer( maxProgress ) ) );
292
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
293
new Integer( maxProgress ) ) );
327
296
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
330
299
_doit._handler.sendMessage( Message.obtain(
331
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
new Integer( tmpProgress ) ) );
300
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
301
new Integer( tmpProgress ) ) );
335
304
protected void setProgress( int progress ) throws AbortImportException
338
307
_doit._handler.sendMessage( Message.obtain(
339
_doit._handler, Doit.MESSAGE_SETPROGRESS,
340
new Integer( progress ) ) );
308
_doit._handler, Doit.MESSAGE_SETPROGRESS,
309
new Integer( progress ) ) );
343
312
protected void finish( int action ) throws AbortImportException
376
346
// handle special cases
377
347
switch( mergeSetting )
379
case Doit.ACTION_KEEP:
349
case R.id.merge_keep:
380
350
// if we keep contacts on duplicate, we better check for one
381
return !_contactsCache.exists( name );
351
return !_contacts.containsKey( name );
383
case Doit.ACTION_PROMPT:
353
case R.id.merge_prompt:
384
354
// if we are prompting on duplicate, we better check for one
385
if( !_contactsCache.exists( name ) )
355
if( !_contacts.containsKey( name ) )
388
358
// ok, it exists, so do prompt
389
359
_doit._handler.sendMessage( Message.obtain(
390
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
360
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
429
399
// does contact exist already?
430
400
Uri contactUri = null;
432
if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
402
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
434
404
// should we skip this import altogether?
435
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
405
if( _lastMergeDecision == R.id.merge_keep ) return;
437
407
// get contact's URI
438
408
contactUri = ContentUris.withAppendedId(
439
Contacts.People.CONTENT_URI, id );
409
Contacts.People.CONTENT_URI, id );
441
411
// should we destroy the existing contact before importing?
442
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
412
if( _lastMergeDecision == R.id.merge_overwrite ) {
443
413
_doit.getContentResolver().delete( contactUri, null, null );
444
414
contactUri = null;
447
_doit._handler.sendEmptyMessage(
448
Doit.MESSAGE_CONTACTOVERWRITTEN );
417
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
449
418
uiInformed = true;
452
_contactsCache.remove( contact._name );
421
_contacts.remove( contact._name );
460
429
// create a new contact
461
430
values.put( Contacts.People.NAME, contact._name );
462
431
contactUri = _doit.getContentResolver().insert(
463
Contacts.People.CONTENT_URI, values );
432
Contacts.People.CONTENT_URI, values );
464
433
id = ContentUris.parseId( contactUri );
465
434
if( id <= 0 ) return; // shouldn't happen!
467
// try to add them to the "My Contacts" group
469
Contacts.People.addToMyContactsGroup(
470
_doit.getContentResolver(), id );
472
catch( IllegalStateException e ) {
473
// ignore any failure
436
// add them to the "My Contacts" group
437
Contacts.People.addToGroup(
438
_doit.getContentResolver(), id,
439
Contacts.Groups.GROUP_MY_CONTACTS );
477
_contactsCache.put( id, contact._name );
442
_contacts.put( contact._name, id );
480
445
if( !uiInformed ) {
539
499
Long contactId = ContentUris.parseId( contactUri );
540
500
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
541
501
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
542
Set< String > emailsKeys = emails.keySet();
544
// add email addresses
545
Iterator< String > i = emailsKeys.iterator();
504
Set emailsKeys = emails.keySet();
505
Iterator i = emailsKeys.iterator();
546
506
while( i.hasNext() ) {
547
507
ContactData.EmailData email = emails.get( i.next() );
549
// we don't want to add this email address if it exists already or
550
// we would introduce duplicates.
509
// like with phone numbers, we don't want to add this email address
510
// if it exists already or we would introduce duplicates.
551
511
String address = sanitiseEmailAddress( email.getAddress() );
552
512
if( address == null ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
513
HashSet< String > addresses = _contactEmails.get( contactId );
514
if( addresses != null && addresses.contains( address ) ) continue;
555
516
// add phone number
556
517
ContentValues values = new ContentValues();
560
521
if( email.isPreferred() )
561
522
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
562
523
_doit.getContentResolver().insert( contactContactMethodsUri,
565
// and add this address to the cache to prevent a addition of
566
// duplicate date from another file
567
_contactsCache.addEmail( contactId, address );
571
private void importContactAddresses( Uri contactUri,
572
HashMap< String, ContactData.AddressData > addresses )
574
Long contactId = ContentUris.parseId( contactUri );
575
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
576
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
577
Set< String > addressesKeys = addresses.keySet();
580
Iterator< String > i = addressesKeys.iterator();
581
while( i.hasNext() ) {
582
ContactData.AddressData address = addresses.get( i.next() );
584
// we don't want to add this address if it exists already or we
585
// would introduce duplicates
586
if( address == null ) continue;
587
if( _contactsCache.hasAddress( contactId, address.getAddress() ) )
590
// add postal address
591
ContentValues values = new ContentValues();
592
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
593
values.put( Contacts.ContactMethods.DATA, address.getAddress() );
594
values.put( Contacts.ContactMethods.TYPE, address.getType() );
595
_doit.getContentResolver().insert( contactContactMethodsUri,
598
// and add this address to the cache to prevent a addition of
599
// duplicate date from another file
600
_contactsCache.addAddress( contactId, address.getAddress() );
612
static public String sanitisePhoneNumber( String number )
536
private void buildContactsCache() throws AbortImportException
539
setProgressMessage( R.string.doit_caching );
544
// init contacts caches
545
_contacts = new HashMap< String, Long >();
546
_contactNumbers = new HashMap< Long, HashSet< String > >();
547
_contactEmails = new HashMap< Long, HashSet< String > >();
549
// query and store map of contact names to ids
550
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
551
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
552
cols, null, null, null);
553
if( cur.moveToFirst() ) {
554
int idCol = cur.getColumnIndex( Contacts.People._ID );
555
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
557
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
558
} while( cur.moveToNext() );
561
// query and store map of contact ids to sets of phone numbers
562
cols = new String[] { Contacts.Phones.PERSON_ID,
563
Contacts.Phones.NUMBER };
564
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
565
cols, null, null, null);
566
if( cur.moveToFirst() ) {
567
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
568
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
570
Long id = cur.getLong( personIdCol );
571
String number = sanitisePhoneNumber(
572
cur.getString( numberCol ) );
573
if( number != null ) {
574
HashSet< String > numbers = _contactNumbers.get( id );
575
if( numbers == null ) {
576
_contactNumbers.put( id, new HashSet< String >() );
577
numbers = _contactNumbers.get( id );
579
numbers.add( number );
581
} while( cur.moveToNext() );
584
// query and store map of contact ids to sets of email addresses
585
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
586
Contacts.ContactMethods.DATA };
587
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
588
cols, Contacts.ContactMethods.KIND + " = ?",
589
new String[] { "" + Contacts.KIND_EMAIL }, null );
590
if( cur.moveToFirst() ) {
591
int personIdCol = cur.getColumnIndex(
592
Contacts.ContactMethods.PERSON_ID );
593
int addressCol = cur.getColumnIndex(
594
Contacts.ContactMethods.DATA );
596
Long id = cur.getLong( personIdCol );
597
String address = sanitiseEmailAddress(
598
cur.getString( addressCol ) );
599
if( address != null ) {
600
HashSet< String > addresses = _contactEmails.get( id );
601
if( addresses == null ) {
602
_contactEmails.put( id, new HashSet< String >() );
603
addresses = _contactEmails.get( id );
605
addresses.add( address );
607
} while( cur.moveToNext() );
611
private String sanitisePhoneNumber( String number )
614
613
number = number.replaceAll( "[-\\(\\) ]", "" );
615
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
614
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
616
615
Matcher m = p.matcher( number );
617
616
if( m.lookingAt() ) return m.group( 0 );
621
static public String sanitiseEmailAddress( String address )
620
private String sanitiseEmailAddress( String address )
623
622
address = address.trim();
624
623
Pattern p = Pattern.compile(
625
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
624
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
626
625
Matcher m = p.matcher( address );
627
626
if( m.matches() ) {
628
627
String[] bits = address.split( "@" );