1
package org.waxworlds.importcontacts;
4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Import Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/android/import-contacts
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24
package org.waxworlds.edam.importcontacts;
3
26
import java.util.HashMap;
4
import java.util.HashSet;
5
27
import java.util.Iterator;
6
28
import java.util.Set;
7
29
import java.util.regex.Matcher;
118
157
if( !_emails.containsKey( email ) )
119
158
_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")
123
171
protected class AbortImportException extends Exception { };
125
173
public Importer( Doit doit )
268
322
_doit._handler.sendMessage( Message.obtain(
269
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
270
new Integer( maxProgress ) ) );
323
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
new Integer( maxProgress ) ) );
273
327
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
276
330
_doit._handler.sendMessage( Message.obtain(
277
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
278
new Integer( tmpProgress ) ) );
331
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
new Integer( tmpProgress ) ) );
281
335
protected void setProgress( int progress ) throws AbortImportException
284
338
_doit._handler.sendMessage( Message.obtain(
285
_doit._handler, Doit.MESSAGE_SETPROGRESS,
286
new Integer( progress ) ) );
339
_doit._handler, Doit.MESSAGE_SETPROGRESS,
340
new Integer( progress ) ) );
289
343
protected void finish( int action ) throws AbortImportException
323
376
// handle special cases
324
377
switch( mergeSetting )
326
case R.id.merge_keep:
379
case Doit.ACTION_KEEP:
327
380
// if we keep contacts on duplicate, we better check for one
328
return !_contacts.containsKey( name );
381
return !_contactsCache.exists( name );
330
case R.id.merge_prompt:
383
case Doit.ACTION_PROMPT:
331
384
// if we are prompting on duplicate, we better check for one
332
if( !_contacts.containsKey( name ) )
385
if( !_contactsCache.exists( name ) )
335
388
// ok, it exists, so do prompt
336
389
_doit._handler.sendMessage( Message.obtain(
337
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
390
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
376
429
// does contact exist already?
377
430
Uri contactUri = null;
379
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
432
if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
381
434
// should we skip this import altogether?
382
if( _lastMergeDecision == R.id.merge_keep ) return;
435
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
384
437
// get contact's URI
385
438
contactUri = ContentUris.withAppendedId(
386
Contacts.People.CONTENT_URI, id );
439
Contacts.People.CONTENT_URI, id );
388
441
// should we destroy the existing contact before importing?
389
if( _lastMergeDecision == R.id.merge_overwrite ) {
442
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
390
443
_doit.getContentResolver().delete( contactUri, null, null );
391
444
contactUri = null;
394
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
447
_doit._handler.sendEmptyMessage(
448
Doit.MESSAGE_CONTACTOVERWRITTEN );
395
449
uiInformed = true;
398
_contacts.remove( contact._name );
452
_contactsCache.remove( contact._name );
406
460
// create a new contact
407
461
values.put( Contacts.People.NAME, contact._name );
408
462
contactUri = _doit.getContentResolver().insert(
409
Contacts.People.CONTENT_URI, values );
463
Contacts.People.CONTENT_URI, values );
410
464
id = ContentUris.parseId( contactUri );
411
465
if( id <= 0 ) return; // shouldn't happen!
413
// add them to the "My Contacts" group
414
Contacts.People.addToGroup(
415
_doit.getContentResolver(), id,
416
Contacts.Groups.GROUP_MY_CONTACTS );
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
419
_contacts.put( contact._name, id );
477
_contactsCache.put( id, contact._name );
422
480
if( !uiInformed ) {
476
539
Long contactId = ContentUris.parseId( contactUri );
477
540
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
478
541
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
542
Set< String > emailsKeys = emails.keySet();
481
Set emailsKeys = emails.keySet();
482
Iterator i = emailsKeys.iterator();
544
// add email addresses
545
Iterator< String > i = emailsKeys.iterator();
483
546
while( i.hasNext() ) {
484
547
ContactData.EmailData email = emails.get( i.next() );
486
// like with phone numbers, we don't want to add this email address
487
// if it exists already or we would introduce duplicates.
549
// we don't want to add this email address if it exists already or
550
// we would introduce duplicates.
488
551
String address = sanitiseEmailAddress( email.getAddress() );
489
552
if( address == null ) continue;
490
HashSet< String > addresses = _contactEmails.get( contactId );
491
if( addresses != null && addresses.contains( address ) ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
493
555
// add phone number
494
556
ContentValues values = new ContentValues();
498
560
if( email.isPreferred() )
499
561
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
500
562
_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() );
513
private void buildContactsCache() throws AbortImportException
516
setProgressMessage( R.string.doit_caching );
521
// init contacts caches
522
_contacts = new HashMap< String, Long >();
523
_contactNumbers = new HashMap< Long, HashSet< String > >();
524
_contactEmails = new HashMap< Long, HashSet< String > >();
526
// query and store map of contact names to ids
527
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
528
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
529
cols, null, null, null);
530
if( cur.moveToFirst() ) {
531
int idCol = cur.getColumnIndex( Contacts.People._ID );
532
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
534
_contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
535
} while( cur.moveToNext() );
538
// query and store map of contact ids to sets of phone numbers
539
cols = new String[] { Contacts.Phones.PERSON_ID,
540
Contacts.Phones.NUMBER };
541
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
542
cols, null, null, null);
543
if( cur.moveToFirst() ) {
544
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
545
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
547
Long id = cur.getLong( personIdCol );
548
String number = sanitisePhoneNumber(
549
cur.getString( numberCol ) );
550
if( number != null ) {
551
HashSet< String > numbers = _contactNumbers.get( id );
552
if( numbers == null ) {
553
_contactNumbers.put( id, new HashSet< String >() );
554
numbers = _contactNumbers.get( id );
556
numbers.add( number );
558
} while( cur.moveToNext() );
561
// query and store map of contact ids to sets of email addresses
562
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
563
Contacts.ContactMethods.DATA };
564
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
565
cols, Contacts.ContactMethods.KIND + " = ?",
566
new String[] { "" + Contacts.KIND_EMAIL }, null );
567
if( cur.moveToFirst() ) {
568
int personIdCol = cur.getColumnIndex(
569
Contacts.ContactMethods.PERSON_ID );
570
int addressCol = cur.getColumnIndex(
571
Contacts.ContactMethods.DATA );
573
Long id = cur.getLong( personIdCol );
574
String address = sanitiseEmailAddress(
575
cur.getString( addressCol ) );
576
if( address != null ) {
577
HashSet< String > addresses = _contactEmails.get( id );
578
if( addresses == null ) {
579
_contactEmails.put( id, new HashSet< String >() );
580
addresses = _contactEmails.get( id );
582
addresses.add( address );
584
} while( cur.moveToNext() );
588
private String sanitisePhoneNumber( String number )
612
static public String sanitisePhoneNumber( String number )
590
614
number = number.replaceAll( "[-\\(\\) ]", "" );
591
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
615
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
592
616
Matcher m = p.matcher( number );
593
617
if( m.lookingAt() ) return m.group( 0 );
597
private String sanitiseEmailAddress( String address )
621
static public String sanitiseEmailAddress( String address )
599
623
address = address.trim();
600
624
Pattern p = Pattern.compile(
601
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
625
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
602
626
Matcher m = p.matcher( address );
603
627
if( m.matches() ) {
604
628
String[] bits = address.split( "@" );