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;
18
41
public class Importer extends Thread
20
public final static int MESSAGE_FINISHED = 0;
21
public final static int MESSAGE_FINISHED_BACK = 1;
22
public final static int MESSAGE_ERROR = 2;
23
public final static int MESSAGE_CONTINUEORABORT = 3;
24
public final static int MESSAGE_SETPROGRESSMESSAGE = 4;
25
public final static int MESSAGE_SETMAXPROGRESS = 5;
26
public final static int MESSAGE_SETTMPPROGRESS = 6;
27
public final static int MESSAGE_SETPROGRESS = 7;
28
public final static int MESSAGE_MERGEPROMPT = 8;
29
public final static int MESSAGE_CONTACTOVERWRITTEN = 9;
30
public final static int MESSAGE_CONTACTCREATED = 10;
31
public final static int MESSAGE_CONTACTMERGED = 11;
32
public final static int MESSAGE_CONTACTSKIPPED = 12;
43
public final static int ACTION_ABORT = 1;
44
public final static int ACTION_ALLDONE = 2;
34
46
public final static int RESPONSE_NEGATIVE = 0;
35
47
public final static int RESPONSE_POSITIVE = 1;
118
131
if( _phones == null ) _phones = new HashMap< String, PhoneData >();
119
132
if( !_phones.containsKey( number ) )
120
133
_phones.put( number,
121
new PhoneData( number, type, isPreferred ) );
134
new PhoneData( number, type, isPreferred ) );
124
137
protected void addEmail( String email, int type, boolean isPreferred )
168
finish( ACTION_ALLDONE );
156
170
catch( AbortImportException e )
173
// flag as finished to prevent interrupts
177
synchronized private void setIsFinished()
160
182
protected void onImport() throws AbortImportException
200
227
_doit._handler.sendMessage( Message.obtain(
201
_doit._handler, MESSAGE_ERROR, message ) );
228
_doit._handler, Doit.MESSAGE_ERROR, message ) );
205
232
catch( InterruptedException e ) { }
234
// no need to check if an abortion happened during the wait, we are
235
// about to finish anyway!
236
finish( ACTION_ABORT );
209
239
protected void showFatalError( int res ) throws AbortImportException
218
248
_doit._handler.sendMessage( Message.obtain(
219
_doit._handler, MESSAGE_ERROR, message ) );
249
_doit._handler, Doit.MESSAGE_ERROR, message ) );
223
253
catch( InterruptedException e ) { }
255
// no need to check if an abortion happened during the wait, we are
256
// about to finish anyway!
257
finish( ACTION_ABORT );
227
260
protected boolean showContinue( int res ) throws AbortImportException
236
269
_doit._handler.sendMessage( Message.obtain(
237
_doit._handler, MESSAGE_CONTINUEORABORT, message ) );
270
_doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
241
274
catch( InterruptedException e ) { }
276
// check if an abortion happened during the wait
242
279
return _response == RESPONSE_POSITIVE;
248
285
_doit._handler.sendMessage( Message.obtain( _doit._handler,
249
MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
286
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
252
289
protected void setProgressMax( int maxProgress )
256
293
_doit._handler.sendMessage( Message.obtain(
257
_doit._handler, MESSAGE_SETMAXPROGRESS,
258
new Integer( maxProgress ) ) );
294
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
295
new Integer( maxProgress ) ) );
261
298
protected void setTmpProgress( int tmpProgress ) throws AbortImportException
264
301
_doit._handler.sendMessage( Message.obtain(
265
_doit._handler, MESSAGE_SETTMPPROGRESS,
266
new Integer( tmpProgress ) ) );
302
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
303
new Integer( tmpProgress ) ) );
269
306
protected void setProgress( int progress ) throws AbortImportException
272
309
_doit._handler.sendMessage( Message.obtain(
273
_doit._handler, MESSAGE_SETPROGRESS,
274
new Integer( progress ) ) );
277
protected void finish() throws AbortImportException
282
protected void abort() throws AbortImportException
310
_doit._handler, Doit.MESSAGE_SETPROGRESS,
311
new Integer( progress ) ) );
314
protected void finish( int action ) throws AbortImportException
316
// update UI to reflect action
320
case ACTION_ALLDONE: message = Doit.MESSAGE_ALLDONE; break;
321
default: // fall through
322
case ACTION_ABORT: message = Doit.MESSAGE_ABORT; break;
324
_doit._handler.sendEmptyMessage( message );
327
throw new AbortImportException();
287
330
protected CharSequence getText( int res )
296
339
return isImportRequired( name, _mergeSetting );
299
synchronized private boolean isImportRequired( String name, int mergeSetting )
342
synchronized private boolean isImportRequired( String name,
343
int mergeSetting ) throws AbortImportException
301
345
_lastMergeDecision = mergeSetting;
303
347
// handle special cases
304
348
switch( mergeSetting )
306
case R.id.merge_keep:
350
case Doit.ACTION_KEEP:
307
351
// if we keep contacts on duplicate, we better check for one
308
352
return !_contacts.containsKey( name );
310
case R.id.merge_prompt:
354
case Doit.ACTION_PROMPT:
311
355
// if we are prompting on duplicate, we better check for one
312
356
if( !_contacts.containsKey( name ) )
315
359
// ok, it exists, so do prompt
316
360
_doit._handler.sendMessage( Message.obtain(
317
_doit._handler, MESSAGE_MERGEPROMPT, name ) );
361
_doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
321
365
catch( InterruptedException e ) { }
367
// check if an abortion happened during the wait
323
370
// if "always" was selected, make choice permenant
324
371
if( _responseExtra == RESPONSEEXTRA_ALWAYS )
325
372
_mergeSetting = _response;
327
// recurse, with out new merge setting
374
// recurse, with our new merge setting
328
375
return isImportRequired( name, _response );
336
383
protected void skipContact() throws AbortImportException
339
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
386
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
342
389
protected void importContact( ContactData contact )
347
if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
394
// if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
395
// finish( ACTION_ABORT );
350
397
ContentValues values = new ContentValues();
351
398
boolean uiInformed = false;
356
403
if( ( id = (Long)_contacts.get( contact._name ) ) != null )
358
405
// should we skip this import altogether?
359
if( _lastMergeDecision == R.id.merge_keep ) return;
406
if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
361
408
// get contact's URI
362
409
contactUri = ContentUris.withAppendedId(
363
Contacts.People.CONTENT_URI, id );
410
Contacts.People.CONTENT_URI, id );
365
412
// should we destroy the existing contact before importing?
366
if( _lastMergeDecision == R.id.merge_overwrite ) {
413
if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
367
414
_doit.getContentResolver().delete( contactUri, null, null );
368
415
contactUri = null;
371
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
418
_doit._handler.sendEmptyMessage(
419
Doit.MESSAGE_CONTACTOVERWRITTEN );
372
420
uiInformed = true;
383
431
// create a new contact
384
432
values.put( Contacts.People.NAME, contact._name );
385
433
contactUri = _doit.getContentResolver().insert(
386
Contacts.People.CONTENT_URI, values );
434
Contacts.People.CONTENT_URI, values );
387
435
id = ContentUris.parseId( contactUri );
388
436
if( id <= 0 ) return; // shouldn't happen!
390
// add them to the "My Contacts" group
391
Contacts.People.addToGroup(
392
_doit.getContentResolver(), id,
393
Contacts.Groups.GROUP_MY_CONTACTS );
438
// try to add them to the "My Contacts" group
440
Contacts.People.addToMyContactsGroup(
441
_doit.getContentResolver(), id );
443
catch( IllegalStateException e ) {
444
// ignore any failure
396
448
_contacts.put( contact._name, id );
399
451
if( !uiInformed ) {
400
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
452
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
401
453
uiInformed = true;
406
458
if( !uiInformed )
407
_doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
459
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
409
461
// import contact parts
410
462
if( contact._phones != null )
419
471
Long contactId = ContentUris.parseId( contactUri );
420
472
Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
421
473
Contacts.People.Phones.CONTENT_DIRECTORY );
474
Set< String > phonesKeys = phones.keySet();
423
476
// add phone numbers
424
Set phonesKeys = phones.keySet();
425
Iterator i = phonesKeys.iterator();
477
Iterator< String > i = phonesKeys.iterator();
426
478
while( i.hasNext() ) {
427
479
ContactData.PhoneData phone = phones.get( i.next() );
445
497
if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
446
498
_doit.getContentResolver().insert( contactPhonesUri, values );
501
// now add those phone numbers to the cache to prevent the addition of
502
// duplicate data from another file
503
i = phonesKeys.iterator();
504
while( i.hasNext() ) {
505
ContactData.PhoneData phone = phones.get( i.next() );
507
String number = sanitisePhoneNumber( phone._number );
508
if( number != null ) {
509
HashSet< String > numbers = _contactNumbers.get( contactId );
510
if( numbers == null ) {
511
_contactNumbers.put( contactId, new HashSet< String >() );
512
numbers = _contactNumbers.get( contactId );
514
numbers.add( number );
450
519
private void importContactEmails( Uri contactUri,
453
522
Long contactId = ContentUris.parseId( contactUri );
454
523
Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
455
524
Contacts.People.ContactMethods.CONTENT_DIRECTORY );
525
Set< String > emailsKeys = emails.keySet();
458
Set emailsKeys = emails.keySet();
459
Iterator i = emailsKeys.iterator();
527
// add email addresses
528
Iterator< String > i = emailsKeys.iterator();
460
529
while( i.hasNext() ) {
461
530
ContactData.EmailData email = emails.get( i.next() );
475
544
if( email.isPreferred() )
476
545
values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
477
546
_doit.getContentResolver().insert( contactContactMethodsUri,
482
synchronized private void finish( boolean offerBack )
483
throws AbortImportException
485
// notify Doit that we're finished
486
_doit._handler.sendEmptyMessage(
487
offerBack? MESSAGE_FINISHED_BACK : MESSAGE_FINISHED );
490
throw new AbortImportException();
493
synchronized private void checkAbort() throws AbortImportException
550
// now add those email addresses to the cache to prevent the addition of
551
// duplicate data from another file
552
i = emailsKeys.iterator();
553
while( i.hasNext() ) {
554
ContactData.EmailData email = emails.get( i.next() );
556
String address = sanitiseEmailAddress( email.getAddress() );
557
if( address != null ) {
558
HashSet< String > addresses = _contactEmails.get( contactId );
559
if( addresses == null ) {
560
_contactEmails.put( contactId, new HashSet< String >() );
561
addresses = _contactEmails.get( contactId );
563
addresses.add( address );
568
synchronized protected void checkAbort() throws AbortImportException
514
589
// query and store map of contact names to ids
515
590
cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
516
591
cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
517
cols, null, null, null);
592
cols, null, null, null);
518
593
if( cur.moveToFirst() ) {
519
594
int idCol = cur.getColumnIndex( Contacts.People._ID );
520
595
int nameCol = cur.getColumnIndex( Contacts.People.NAME );
527
602
cols = new String[] { Contacts.Phones.PERSON_ID,
528
603
Contacts.Phones.NUMBER };
529
604
cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
530
cols, null, null, null);
605
cols, null, null, null);
531
606
if( cur.moveToFirst() ) {
532
607
int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
533
608
int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
554
629
new String[] { "" + Contacts.KIND_EMAIL }, null );
555
630
if( cur.moveToFirst() ) {
556
631
int personIdCol = cur.getColumnIndex(
557
Contacts.ContactMethods.PERSON_ID );
632
Contacts.ContactMethods.PERSON_ID );
558
633
int addressCol = cur.getColumnIndex(
559
Contacts.ContactMethods.DATA );
634
Contacts.ContactMethods.DATA );
561
636
Long id = cur.getLong( personIdCol );
562
637
String address = sanitiseEmailAddress(
563
cur.getString( addressCol ) );
638
cur.getString( addressCol ) );
564
639
if( address != null ) {
565
640
HashSet< String > addresses = _contactEmails.get( id );
566
641
if( addresses == null ) {
576
651
private String sanitisePhoneNumber( String number )
578
653
number = number.replaceAll( "[-\\(\\) ]", "" );
579
Pattern p = Pattern.compile( "^\\+?[0-9]+" );
654
Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
580
655
Matcher m = p.matcher( number );
581
656
if( m.lookingAt() ) return m.group( 0 );
587
662
address = address.trim();
588
663
Pattern p = Pattern.compile(
589
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
664
"^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
590
665
Matcher m = p.matcher( address );
591
666
if( m.matches() ) {
592
667
String[] bits = address.split( "@" );