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
27
import java.util.HashSet;
15
38
import android.os.Message;
16
39
import android.provider.Contacts;
18
42
public class Importer extends Thread
20
public final static int ACTION_GOBACK = 0;
21
44
public final static int ACTION_ABORT = 1;
22
45
public final static int ACTION_ALLDONE = 2;
33
56
private HashMap< String, Long > _contacts;
34
57
private HashMap< Long, HashSet< String > > _contactNumbers;
35
58
private HashMap< Long, HashSet< String > > _contactEmails;
59
private HashMap< Long, HashSet< String > > _contactAddresses;
36
60
private int _mergeSetting;
37
61
private int _lastMergeDecision;
38
62
private boolean _abort = false;
119
private String _address;
122
public AddressData( String address, int type ) {
127
public String getAddress() {
131
public int getType() {
93
136
public String _name = null;
94
137
public HashMap< String, PhoneData > _phones = null;
95
138
public HashMap< String, EmailData > _emails = null;
139
public HashMap< String, AddressData > _addresses = null;
97
141
protected void setName( String name )
109
153
if( _phones == null ) _phones = new HashMap< String, PhoneData >();
110
154
if( !_phones.containsKey( number ) )
111
155
_phones.put( number,
112
new PhoneData( number, type, isPreferred ) );
156
new PhoneData( number, type, isPreferred ) );
115
159
protected void addEmail( String email, int type, boolean isPreferred )
118
162
if( !_emails.containsKey( email ) )
119
163
_emails.put( email, new EmailData( email, type, isPreferred ) );
166
protected void addAddress( String address, int type )
168
if( _addresses == null ) _addresses =
169
new HashMap< String, AddressData >();
170
if( !_addresses.containsKey( address ) )
171
_addresses.put( address, new AddressData( address, type ) );
175
@SuppressWarnings("serial")
123
176
protected class AbortImportException extends Exception { };
125
178
public Importer( Doit doit )
204
257
_doit._handler.sendMessage( Message.obtain(
205
_doit._handler, Doit.MESSAGE_ERROR, message ) );
258
_doit._handler, Doit.MESSAGE_ERROR, message ) );
209
262
catch( InterruptedException e ) { }
210
264
// no need to check if an abortion happened during the wait, we are
211
265
// about to finish anyway!
212
266
finish( ACTION_ABORT );
224
278
_doit._handler.sendMessage( Message.obtain(
225
_doit._handler, Doit.MESSAGE_ERROR, message ) );
279
_doit._handler, Doit.MESSAGE_ERROR, message ) );
229
283
catch( InterruptedException e ) { }
230
285
// no need to check if an abortion happened during the wait, we are
231
286
// about to finish anyway!
232
287
finish( ACTION_ABORT );
260
315
_doit._handler.sendMessage( Message.obtain( _doit._handler,
261
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
316
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
264
319
protected void setProgressMax( int maxProgress )
268
323
_doit._handler.sendMessage( Message.obtain(
269
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
270
new Integer( maxProgress ) ) );
324
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
325
new Integer( maxProgress ) ) );
273
328
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
276
331
_doit._handler.sendMessage( Message.obtain(
277
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
278
new Integer( tmpProgress ) ) );
332
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
333
new Integer( tmpProgress ) ) );
281
336
protected void setProgress( int progress ) throws AbortImportException
284
339
_doit._handler.sendMessage( Message.obtain(
285
_doit._handler, Doit.MESSAGE_SETPROGRESS,
286
new Integer( progress ) ) );
340
_doit._handler, Doit.MESSAGE_SETPROGRESS,
341
new Integer( progress ) ) );
289
344
protected void finish( int action ) throws AbortImportException
295
case ACTION_GOBACK: message = Doit.MESSAGE_FINISHED_GOBACK; break;
296
case ACTION_ALLDONE: message = Doit.MESSAGE_FINISHED_ALLDONE; break;
350
case ACTION_ALLDONE: message = Doit.MESSAGE_ALLDONE; break;
297
351
default: // fall through
298
case ACTION_ABORT: message = Doit.MESSAGE_FINISHED; break;
352
case ACTION_ABORT: message = Doit.MESSAGE_ABORT; break;
300
354
_doit._handler.sendEmptyMessage( message );
323
377
// handle special cases
324
378
switch( mergeSetting )
326
case R.id.merge_keep:
380
case Doit.ACTION_KEEP:
327
381
// if we keep contacts on duplicate, we better check for one
328
382
return !_contacts.containsKey( name );
330
case R.id.merge_prompt:
384
case Doit.ACTION_PROMPT:
331
385
// if we are prompting on duplicate, we better check for one
332
386
if( !_contacts.containsKey( name ) )
335
389
// ok, it exists, so do prompt
336
390
_doit._handler.sendMessage( Message.obtain(
337
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
391
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
347
401
if( _responseExtra == RESPONSEEXTRA_ALWAYS )
348
402
_mergeSetting = _response;
350
// recurse, with out new merge setting
404
// recurse, with our new merge setting
351
405
return isImportRequired( name, _response );
379
433
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
381
435
// should we skip this import altogether?
382
if( _lastMergeDecision == R.id.merge_keep ) return;
436
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
384
438
// get contact's URI
385
439
contactUri = ContentUris.withAppendedId(
386
Contacts.People.CONTENT_URI, id );
440
Contacts.People.CONTENT_URI, id );
388
442
// should we destroy the existing contact before importing?
389
if( _lastMergeDecision == R.id.merge_overwrite ) {
443
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
390
444
_doit.getContentResolver().delete( contactUri, null, null );
391
445
contactUri = null;
394
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
448
_doit._handler.sendEmptyMessage(
449
Doit.MESSAGE_CONTACTOVERWRITTEN );
395
450
uiInformed = true;
406
461
// create a new contact
407
462
values.put( Contacts.People.NAME, contact._name );
408
463
contactUri = _doit.getContentResolver().insert(
409
Contacts.People.CONTENT_URI, values );
464
Contacts.People.CONTENT_URI, values );
410
465
id = ContentUris.parseId( contactUri );
411
466
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 );
468
// try to add them to the "My Contacts" group
470
Contacts.People.addToMyContactsGroup(
471
_doit.getContentResolver(), id );
473
catch( IllegalStateException e ) {
474
// ignore any failure
419
478
_contacts.put( contact._name, id );
434
493
importContactPhones( contactUri, contact._phones );
435
494
if( contact._emails != null )
436
495
importContactEmails( contactUri, contact._emails );
496
if( contact._addresses != null )
497
importContactAddresses( contactUri, contact._addresses );
439
500
private void importContactPhones( Uri contactUri,
442
503
Long contactId = ContentUris.parseId( contactUri );
443
504
Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
444
505
Contacts.People.Phones.CONTENT_DIRECTORY );
506
Set< String > phonesKeys = phones.keySet();
446
508
// add phone numbers
447
Set phonesKeys = phones.keySet();
448
Iterator i = phonesKeys.iterator();
509
Iterator< String > i = phonesKeys.iterator();
449
510
while( i.hasNext() ) {
450
511
ContactData.PhoneData phone = phones.get( i.next() );
458
519
// anyway, so it's not a problem).
459
520
String number = sanitisePhoneNumber( phone._number );
460
521
if( number == null ) continue;
461
HashSet< String > numbers = _contactNumbers.get( contactId );
462
if( numbers != null && numbers.contains( number ) ) continue;
522
HashSet< String > cache = _contactNumbers.get( contactId );
523
if( cache != null && cache.contains( number ) ) continue;
464
525
// add phone number
465
526
ContentValues values = new ContentValues();
467
528
values.put( Contacts.Phones.NUMBER, phone._number );
468
529
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
469
530
_doit.getContentResolver().insert( contactPhonesUri, values );
532
// and add this address to the cache to prevent a addition of
533
// duplicate date from another file
534
if( cache == null ) {
535
cache = new HashSet< String >();
536
_contactNumbers.put( contactId, cache );
476
545
Long contactId = ContentUris.parseId( contactUri );
477
546
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
478
547
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
548
Set< String > emailsKeys = emails.keySet();
481
Set emailsKeys = emails.keySet();
482
Iterator i = emailsKeys.iterator();
550
// add email addresses
551
Iterator< String > i = emailsKeys.iterator();
483
552
while( i.hasNext() ) {
484
553
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.
555
// we don't want to add this email address if it exists already or
556
// we would introduce duplicates.
488
557
String address = sanitiseEmailAddress( email.getAddress() );
489
558
if( address == null ) continue;
490
HashSet< String > addresses = _contactEmails.get( contactId );
491
if( addresses != null && addresses.contains( address ) ) continue;
559
HashSet< String > cache = _contactEmails.get( contactId );
560
if( cache != null && cache.contains( address ) ) continue;
493
562
// add phone number
494
563
ContentValues values = new ContentValues();
498
567
if( email.isPreferred() )
499
568
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
500
569
_doit.getContentResolver().insert( contactContactMethodsUri,
572
// and add this address to the cache to prevent a addition of
573
// duplicate date from another file
574
if( cache == null ) {
575
cache = new HashSet< String >();
576
_contactEmails.put( contactId, cache );
578
cache.add( address );
582
private void importContactAddresses( Uri contactUri,
583
HashMap< String, ContactData.AddressData > addresses )
585
Long contactId = ContentUris.parseId( contactUri );
586
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
587
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
588
Set< String > addressesKeys = addresses.keySet();
591
Iterator< String > i = addressesKeys.iterator();
592
while( i.hasNext() ) {
593
ContactData.AddressData address = addresses.get( i.next() );
595
// we don't want to add this address if it exists already or we
596
// would introduce duplicates
597
if( address == null ) continue;
598
HashSet< String > cache = _contactAddresses.get( contactId );
599
if( cache != null && cache.contains( address.getAddress() ) )
602
// add postal address
603
ContentValues values = new ContentValues();
604
values.put( Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL );
605
values.put( Contacts.ContactMethods.DATA, address.getAddress() );
606
values.put( Contacts.ContactMethods.TYPE, address.getType() );
607
_doit.getContentResolver().insert( contactContactMethodsUri,
610
// and add this address to the cache to prevent a addition of
611
// duplicate date from another file
612
if( cache == null ) {
613
cache = new HashSet< String >();
614
_contactAddresses.put( contactId, cache );
616
cache.add( address.getAddress() );
522
637
_contacts = new HashMap< String, Long >();
523
638
_contactNumbers = new HashMap< Long, HashSet< String > >();
524
639
_contactEmails = new HashMap< Long, HashSet< String > >();
640
_contactAddresses = new HashMap< Long, HashSet< String > >();
526
642
// query and store map of contact names to ids
527
643
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
528
644
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
529
cols, null, null, null);
645
cols, null, null, null);
530
646
if( cur.moveToFirst() ) {
531
647
int idCol = cur.getColumnIndex( Contacts.People._ID );
532
648
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
539
655
cols = new String[] { Contacts.Phones.PERSON_ID,
540
656
Contacts.Phones.NUMBER };
541
657
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
542
cols, null, null, null);
658
cols, null, null, null);
543
659
if( cur.moveToFirst() ) {
544
660
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
545
661
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
550
666
if( number != null ) {
551
667
HashSet< String > numbers = _contactNumbers.get( id );
552
668
if( numbers == null ) {
553
_contactNumbers.put( id, new HashSet< String >() );
554
numbers = _contactNumbers.get( id );
669
numbers = new HashSet< String >();
670
_contactNumbers.put( id, numbers );
556
672
numbers.add( number );
566
682
new String[] { "" + Contacts.KIND_EMAIL }, null );
567
683
if( cur.moveToFirst() ) {
568
684
int personIdCol = cur.getColumnIndex(
569
Contacts.ContactMethods.PERSON_ID );
685
Contacts.ContactMethods.PERSON_ID );
570
686
int addressCol = cur.getColumnIndex(
571
Contacts.ContactMethods.DATA );
687
Contacts.ContactMethods.DATA );
573
689
Long id = cur.getLong( personIdCol );
574
690
String address = sanitiseEmailAddress(
575
cur.getString( addressCol ) );
691
cur.getString( addressCol ) );
576
692
if( address != null ) {
577
693
HashSet< String > addresses = _contactEmails.get( id );
578
694
if( addresses == null ) {
579
_contactEmails.put( id, new HashSet< String >() );
580
addresses = _contactEmails.get( id );
695
addresses = new HashSet< String >();
696
_contactEmails.put( id, addresses );
698
addresses.add( address );
700
} while( cur.moveToNext() );
703
// query and store map of contact ids to sets of postal addresses
704
cols = new String[] { Contacts.ContactMethods.PERSON_ID,
705
Contacts.ContactMethods.DATA };
706
cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
707
cols, Contacts.ContactMethods.KIND + " = ?",
708
new String[] { "" + Contacts.KIND_POSTAL }, null );
709
if( cur.moveToFirst() ) {
710
int personIdCol = cur.getColumnIndex(
711
Contacts.ContactMethods.PERSON_ID );
712
int addressCol = cur.getColumnIndex(
713
Contacts.ContactMethods.DATA );
715
Long id = cur.getLong( personIdCol );
716
String address = cur.getString( addressCol );
717
if( address != null ) {
718
HashSet< String > addresses = _contactAddresses.get( id );
719
if( addresses == null ) {
720
addresses = new HashSet< String >();
721
_contactAddresses.put( id, addresses );
582
723
addresses.add( address );
588
729
private String sanitisePhoneNumber( String number )
590
731
number = number.replaceAll( "[-\\(\\) ]", "" );
591
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
732
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
592
733
Matcher m = p.matcher( number );
593
734
if( m.lookingAt() ) return m.group( 0 );
599
740
address = address.trim();
600
741
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])?)+$" );
742
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
602
743
Matcher m = p.matcher( address );
603
744
if( m.matches() ) {
604
745
String[] bits = address.split( "@" );