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;
1
package org.waxworlds.importcontacts;
26
3
import java.util.HashMap;
4
import java.util.HashSet;
27
5
import java.util.Iterator;
28
6
import java.util.Set;
29
7
import java.util.regex.Matcher;
157
118
if( !_emails.containsKey( email ) )
158
119
_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
123
protected class AbortImportException extends Exception { };
173
125
public Importer( Doit doit )
322
268
_doit._handler.sendMessage( Message.obtain(
323
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
new Integer( maxProgress ) ) );
269
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
270
new Integer( maxProgress ) ) );
327
273
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
330
276
_doit._handler.sendMessage( Message.obtain(
331
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
new Integer( tmpProgress ) ) );
277
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
278
new Integer( tmpProgress ) ) );
335
281
protected void setProgress( int progress ) throws AbortImportException
338
284
_doit._handler.sendMessage( Message.obtain(
339
_doit._handler, Doit.MESSAGE_SETPROGRESS,
340
new Integer( progress ) ) );
285
_doit._handler, Doit.MESSAGE_SETPROGRESS,
286
new Integer( progress ) ) );
343
289
protected void finish( int action ) throws AbortImportException
376
323
// handle special cases
377
324
switch( mergeSetting )
379
case Doit.ACTION_KEEP:
326
case R.id.merge_keep:
380
327
// if we keep contacts on duplicate, we better check for one
381
return !_contactsCache.exists( name );
328
return !_contacts.containsKey( name );
383
case Doit.ACTION_PROMPT:
330
case R.id.merge_prompt:
384
331
// if we are prompting on duplicate, we better check for one
385
if( !_contactsCache.exists( name ) )
332
if( !_contacts.containsKey( name ) )
388
335
// ok, it exists, so do prompt
389
336
_doit._handler.sendMessage( Message.obtain(
390
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
337
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
429
376
// does contact exist already?
430
377
Uri contactUri = null;
432
if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
379
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
434
381
// should we skip this import altogether?
435
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
382
if( _lastMergeDecision == R.id.merge_keep ) return;
437
384
// get contact's URI
438
385
contactUri = ContentUris.withAppendedId(
439
Contacts.People.CONTENT_URI, id );
386
Contacts.People.CONTENT_URI, id );
441
388
// should we destroy the existing contact before importing?
442
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
389
if( _lastMergeDecision == R.id.merge_overwrite ) {
443
390
_doit.getContentResolver().delete( contactUri, null, null );
444
391
contactUri = null;
447
_doit._handler.sendEmptyMessage(
448
Doit.MESSAGE_CONTACTOVERWRITTEN );
394
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
449
395
uiInformed = true;
452
_contactsCache.remove( contact._name );
398
_contacts.remove( contact._name );
460
406
// create a new contact
461
407
values.put( Contacts.People.NAME, contact._name );
462
408
contactUri = _doit.getContentResolver().insert(
463
Contacts.People.CONTENT_URI, values );
409
Contacts.People.CONTENT_URI, values );
464
410
id = ContentUris.parseId( contactUri );
465
411
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
413
// add them to the "My Contacts" group
414
Contacts.People.addToGroup(
415
_doit.getContentResolver(), id,
416
Contacts.Groups.GROUP_MY_CONTACTS );
477
_contactsCache.put( id, contact._name );
419
_contacts.put( contact._name, id );
480
422
if( !uiInformed ) {
539
476
Long contactId = ContentUris.parseId( contactUri );
540
477
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
541
478
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
542
Set< String > emailsKeys = emails.keySet();
544
// add email addresses
545
Iterator< String > i = emailsKeys.iterator();
481
Set emailsKeys = emails.keySet();
482
Iterator i = emailsKeys.iterator();
546
483
while( i.hasNext() ) {
547
484
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.
486
// like with phone numbers, we don't want to add this email address
487
// if it exists already or we would introduce duplicates.
551
488
String address = sanitiseEmailAddress( email.getAddress() );
552
489
if( address == null ) continue;
553
if( _contactsCache.hasEmail( contactId, address ) ) continue;
490
HashSet< String > addresses = _contactEmails.get( contactId );
491
if( addresses != null && addresses.contains( address ) ) continue;
555
493
// add phone number
556
494
ContentValues values = new ContentValues();
560
498
if( email.isPreferred() )
561
499
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
562
500
_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 )
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 )
614
590
number = number.replaceAll( "[-\\(\\) ]", "" );
615
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
591
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
616
592
Matcher m = p.matcher( number );
617
593
if( m.lookingAt() ) return m.group( 0 );
621
static public String sanitiseEmailAddress( String address )
597
private String sanitiseEmailAddress( String address )
623
599
address = address.trim();
624
600
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])?)+$" );
601
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
626
602
Matcher m = p.matcher( address );
627
603
if( m.matches() ) {
628
604
String[] bits = address.split( "@" );