/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts

« back to all changes in this revision

Viewing changes to src/org/waxworlds/importcontacts/Importer.java

  • Committer: edam
  • Date: 2009-01-10 16:14:11 UTC
  • Revision ID: edam@waxworlds.org-20090110161411-4d17l9hs9d6j277q
Initial import

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * Importer.java
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 
 *
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
9
 
 *
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.
14
 
 *
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.
19
 
 *
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/>.
22
 
 */
23
 
 
24
 
package org.waxworlds.edam.importcontacts;
 
1
package org.waxworlds.importcontacts;
25
2
 
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;
32
10
import android.content.ContentUris;
33
11
import android.content.ContentValues;
34
12
import android.content.SharedPreferences;
 
13
import android.database.Cursor;
35
14
import android.net.Uri;
36
15
import android.os.Message;
37
16
import android.provider.Contacts;
38
17
 
39
 
 
40
18
public class Importer extends Thread
41
19
{
42
 
        public final static int ACTION_ABORT = 1;
43
 
        public final static int ACTION_ALLDONE = 2;
 
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;
44
33
 
45
34
        public final static int RESPONSE_NEGATIVE = 0;
46
35
        public final static int RESPONSE_POSITIVE = 1;
51
40
        private Doit _doit;
52
41
        private int _response;
53
42
        private int _responseExtra;
 
43
        private HashMap< String, Long > _contacts;
 
44
        private HashMap< Long, HashSet< String > > _contactNumbers;
 
45
        private HashMap< Long, HashSet< String > > _contactEmails;
54
46
        private int _mergeSetting;
55
47
        private int _lastMergeDecision;
56
48
        private boolean _abort = false;
57
 
        private boolean _isFinished = false;
58
 
        private ContactsCache _contactsCache = null;
59
49
 
60
50
        public class ContactData
61
51
        {
109
99
                        }
110
100
                }
111
101
 
112
 
                class AddressData
113
 
                {
114
 
                        private String _address;
115
 
                        public int _type;
116
 
 
117
 
                        public AddressData( String address, int type ) {
118
 
                                _address = address;
119
 
                                _type = type;
120
 
                        }
121
 
 
122
 
                        public String getAddress() {
123
 
                                return _address;
124
 
                        }
125
 
 
126
 
                        public int getType() {
127
 
                                return _type;
128
 
                        }
129
 
                }
130
 
 
131
102
                public String _name = null;
132
103
                public HashMap< String, PhoneData > _phones = null;
133
104
                public HashMap< String, EmailData > _emails = null;
134
 
                public HashMap< String, AddressData > _addresses = null;
135
105
 
136
106
                protected void setName( String name )
137
107
                {
148
118
                        if( _phones == null ) _phones = new HashMap< String, PhoneData >();
149
119
                        if( !_phones.containsKey( number ) )
150
120
                                _phones.put( number,
151
 
                                        new PhoneData( number, type, isPreferred ) );
 
121
                                                new PhoneData( number, type, isPreferred ) );
152
122
                }
153
123
 
154
124
                protected void addEmail( String email, int type, boolean isPreferred )
157
127
                        if( !_emails.containsKey( email ) )
158
128
                                _emails.put( email, new EmailData( email, type, isPreferred ) );
159
129
                }
160
 
 
161
 
                protected void addAddress( String address, int type )
162
 
                {
163
 
                        if( _addresses == null ) _addresses =
164
 
                                new HashMap< String, AddressData >();
165
 
                        if( !_addresses.containsKey( address ) )
166
 
                                _addresses.put( address, new AddressData( address, type ) );
167
 
                }
168
130
        }
169
131
 
170
 
        @SuppressWarnings("serial")
171
132
        protected class AbortImportException extends Exception { };
172
133
 
173
134
        public Importer( Doit doit )
175
136
                _doit = doit;
176
137
 
177
138
                SharedPreferences prefs = getSharedPreferences();
178
 
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
 
139
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
179
140
        }
180
141
 
181
142
        @Override
183
144
        {
184
145
                try
185
146
                {
186
 
                        // update UI
187
 
                        setProgressMessage( R.string.doit_caching );
188
 
 
189
 
                        // build a cache of existing contacts
190
 
                        _contactsCache = new ContactsCache();
191
 
                        _contactsCache.buildCache( _doit );
 
147
                        // cache current contact names
 
148
                        buildContactsCache();
192
149
 
193
150
                        // do the import
194
151
                        onImport();
195
152
 
196
153
                        // done!
197
 
                        finish( ACTION_ALLDONE );
 
154
                        finish();
198
155
                }
199
156
                catch( AbortImportException e )
200
157
                {}
201
 
 
202
 
                // flag as finished to prevent interrupts
203
 
                setIsFinished();
204
 
        }
205
 
 
206
 
        synchronized private void setIsFinished()
207
 
        {
208
 
                _isFinished = true;
209
158
        }
210
159
 
211
160
        protected void onImport() throws AbortImportException
229
178
                notify();
230
179
        }
231
180
 
232
 
        synchronized public boolean setAbort()
 
181
        synchronized public void setAbort()
233
182
        {
234
 
                if( !_isFinished && !_abort ) {
235
 
                        _abort = true;
236
 
                        notify();
237
 
                        return true;
238
 
                }
239
 
                return false;
 
183
                _abort = true;
240
184
        }
241
185
 
242
186
        protected SharedPreferences getSharedPreferences()
254
198
        {
255
199
                checkAbort();
256
200
                _doit._handler.sendMessage( Message.obtain(
257
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
201
                                _doit._handler, MESSAGE_ERROR, message ) );
258
202
                try {
259
203
                        wait();
260
204
                }
261
205
                catch( InterruptedException e ) { }
262
 
 
263
 
                // no need to check if an abortion happened during the wait, we are
264
 
                // about to finish anyway!
265
 
                finish( ACTION_ABORT );
 
206
                finish( true );
266
207
        }
267
208
 
268
209
        protected void showFatalError( int res ) throws AbortImportException
275
216
        {
276
217
                checkAbort();
277
218
                _doit._handler.sendMessage( Message.obtain(
278
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
219
                                _doit._handler, MESSAGE_ERROR, message ) );
279
220
                try {
280
221
                        wait();
281
222
                }
282
223
                catch( InterruptedException e ) { }
283
 
 
284
 
                // no need to check if an abortion happened during the wait, we are
285
 
                // about to finish anyway!
286
 
                finish( ACTION_ABORT );
 
224
                finish( false );
287
225
        }
288
226
 
289
227
        protected boolean showContinue( int res ) throws AbortImportException
296
234
        {
297
235
                checkAbort();
298
236
                _doit._handler.sendMessage( Message.obtain(
299
 
                        _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
237
                                _doit._handler, MESSAGE_CONTINUEORABORT, message ) );
300
238
                try {
301
239
                        wait();
302
240
                }
303
241
                catch( InterruptedException e ) { }
304
 
 
305
 
                // check if an abortion happened during the wait
306
 
                checkAbort();
307
 
 
308
242
                return _response == RESPONSE_POSITIVE;
309
243
        }
310
244
 
312
246
        {
313
247
                checkAbort();
314
248
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
315
 
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
249
                                MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
316
250
        }
317
251
 
318
252
        protected void setProgressMax( int maxProgress )
320
254
        {
321
255
                checkAbort();
322
256
                _doit._handler.sendMessage( Message.obtain(
323
 
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
 
                        new Integer( maxProgress ) ) );
 
257
                                _doit._handler, MESSAGE_SETMAXPROGRESS,
 
258
                                new Integer( maxProgress ) ) );
325
259
        }
326
260
 
327
261
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
328
262
        {
329
263
                checkAbort();
330
264
                _doit._handler.sendMessage( Message.obtain(
331
 
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
 
                        new Integer( tmpProgress ) ) );
 
265
                                _doit._handler, MESSAGE_SETTMPPROGRESS,
 
266
                                new Integer( tmpProgress ) ) );
333
267
        }
334
268
 
335
269
        protected void setProgress( int progress ) throws AbortImportException
336
270
        {
337
271
                checkAbort();
338
272
                _doit._handler.sendMessage( Message.obtain(
339
 
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
340
 
                        new Integer( progress ) ) );
341
 
        }
342
 
 
343
 
        protected void finish( int action ) throws AbortImportException
344
 
        {
345
 
                // update UI to reflect action
346
 
                int message;
347
 
                switch( action )
348
 
                {
349
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
350
 
                default:        // fall through
351
 
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
352
 
                }
353
 
                _doit._handler.sendEmptyMessage( message );
354
 
 
355
 
                // stop
356
 
                throw new AbortImportException();
 
273
                                _doit._handler, MESSAGE_SETPROGRESS,
 
274
                                new Integer( progress ) ) );
 
275
        }
 
276
 
 
277
        protected void finish() throws AbortImportException
 
278
        {
 
279
                finish( false );
 
280
        }
 
281
 
 
282
        protected void abort() throws AbortImportException
 
283
        {
 
284
                finish( true );
357
285
        }
358
286
 
359
287
        protected CharSequence getText( int res )
368
296
                return isImportRequired( name, _mergeSetting );
369
297
        }
370
298
 
371
 
        synchronized private boolean isImportRequired( String name,
372
 
                        int mergeSetting ) throws AbortImportException
 
299
        synchronized private boolean isImportRequired( String name, int mergeSetting )
373
300
        {
374
301
                _lastMergeDecision = mergeSetting;
375
302
 
376
303
                // handle special cases
377
304
                switch( mergeSetting )
378
305
                {
379
 
                case Doit.ACTION_KEEP:
 
306
                case R.id.merge_keep:
380
307
                        // if we keep contacts on duplicate, we better check for one
381
 
                        return !_contactsCache.exists( name );
 
308
                        return !_contacts.containsKey( name );
382
309
 
383
 
                case Doit.ACTION_PROMPT:
 
310
                case R.id.merge_prompt:
384
311
                        // if we are prompting on duplicate, we better check for one
385
 
                        if( !_contactsCache.exists( name ) )
 
312
                        if( !_contacts.containsKey( name ) )
386
313
                                return true;
387
314
 
388
315
                        // ok, it exists, so do prompt
389
316
                        _doit._handler.sendMessage( Message.obtain(
390
 
                                _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
317
                                        _doit._handler, MESSAGE_MERGEPROMPT, name ) );
391
318
                        try {
392
319
                                wait();
393
320
                        }
394
321
                        catch( InterruptedException e ) { }
395
322
 
396
 
                        // check if an abortion happened during the wait
397
 
                        checkAbort();
398
 
 
399
323
                        // if "always" was selected, make choice permenant
400
324
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
401
325
                                _mergeSetting = _response;
402
326
 
403
 
                        // recurse, with our new merge setting
 
327
                        // recurse, with out new merge setting
404
328
                        return isImportRequired( name, _response );
405
329
                }
406
330
 
412
336
        protected void skipContact() throws AbortImportException
413
337
        {
414
338
                checkAbort();
415
 
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
339
                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
416
340
        }
417
341
 
418
342
        protected void importContact( ContactData contact )
420
344
        {
421
345
                checkAbort();
422
346
 
423
 
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
424
 
//                      finish( ACTION_ABORT );
 
347
                if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
 
348
                        abort();
425
349
 
426
350
                ContentValues values = new ContentValues();
427
351
                boolean uiInformed = false;
429
353
                // does contact exist already?
430
354
                Uri contactUri = null;
431
355
                Long id;
432
 
                if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
 
356
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
433
357
                {
434
358
                        // should we skip this import altogether?
435
 
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
 
359
                        if( _lastMergeDecision == R.id.merge_keep ) return;
436
360
 
437
361
                        // get contact's URI
438
362
                        contactUri = ContentUris.withAppendedId(
439
 
                                Contacts.People.CONTENT_URI, id );
 
363
                                        Contacts.People.CONTENT_URI, id );
440
364
 
441
365
                        // should we destroy the existing contact before importing?
442
 
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
 
366
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
443
367
                                _doit.getContentResolver().delete( contactUri, null, null );
444
368
                                contactUri = null;
445
369
 
446
 
                                // update the UI
447
 
                                _doit._handler.sendEmptyMessage(
448
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
370
                                // upate the UI
 
371
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
449
372
                                uiInformed = true;
450
373
 
451
374
                                // update cache
452
 
                                _contactsCache.remove( contact._name );
 
375
                                _contacts.remove( contact._name );
453
376
                        }
454
377
                }
455
378
 
460
383
                        // create a new contact
461
384
                        values.put( Contacts.People.NAME, contact._name );
462
385
                        contactUri = _doit.getContentResolver().insert(
463
 
                                Contacts.People.CONTENT_URI, values );
 
386
                                        Contacts.People.CONTENT_URI, values );
464
387
                        id = ContentUris.parseId( contactUri );
465
388
                        if( id <= 0 ) return;   // shouldn't happen!
466
389
 
467
 
                        // try to add them to the "My Contacts" group
468
 
                        try {
469
 
                                Contacts.People.addToMyContactsGroup(
470
 
                                        _doit.getContentResolver(), id );
471
 
                        }
472
 
                        catch( IllegalStateException e ) {
473
 
                                // ignore any failure
474
 
                        }
 
390
                        // add them to the "My Contacts" group
 
391
                        Contacts.People.addToGroup(
 
392
                                        _doit.getContentResolver(), id,
 
393
                                        Contacts.Groups.GROUP_MY_CONTACTS );
475
394
 
476
395
                        // update cache
477
 
                        _contactsCache.put( id, contact._name );
 
396
                        _contacts.put( contact._name, id );
478
397
 
479
398
                        // update UI
480
399
                        if( !uiInformed ) {
481
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
 
400
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
482
401
                                uiInformed = true;
483
402
                        }
484
403
                }
485
404
 
486
405
                // update UI
487
406
                if( !uiInformed )
488
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
407
                        _doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
489
408
 
490
409
                // import contact parts
491
410
                if( contact._phones != null )
492
411
                        importContactPhones( contactUri, contact._phones );
493
412
                if( contact._emails != null )
494
413
                        importContactEmails( contactUri, contact._emails );
495
 
                if( contact._addresses != null )
496
 
                        importContactAddresses( contactUri, contact._addresses );
497
414
        }
498
415
 
499
416
        private void importContactPhones( Uri contactUri,
502
419
                Long contactId = ContentUris.parseId( contactUri );
503
420
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
504
421
                                Contacts.People.Phones.CONTENT_DIRECTORY );
505
 
                Set< String > phonesKeys = phones.keySet();
506
422
 
507
423
                // add phone numbers
508
 
                Iterator< String > i = phonesKeys.iterator();
 
424
                Set phonesKeys = phones.keySet();
 
425
                Iterator i = phonesKeys.iterator();
509
426
                while( i.hasNext() ) {
510
427
                        ContactData.PhoneData phone = phones.get( i.next() );
511
428
 
518
435
                        // anyway, so it's not a problem).
519
436
                        String number = sanitisePhoneNumber( phone._number );
520
437
                        if( number == null ) continue;
521
 
                        if( _contactsCache.hasNumber( contactId, number ) ) continue;
 
438
                        HashSet< String > numbers = _contactNumbers.get( contactId );
 
439
                        if( numbers != null && numbers.contains( number ) ) continue;
522
440
 
523
441
                        // add phone number
524
442
                        ContentValues values = new ContentValues();
526
444
                        values.put( Contacts.Phones.NUMBER, phone._number );
527
445
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
528
446
                        _doit.getContentResolver().insert( contactPhonesUri, values );
529
 
 
530
 
                        // and add this address to the cache to prevent a addition of
531
 
                        // duplicate date from another file
532
 
                        _contactsCache.addNumber( contactId, number );
533
447
                }
534
448
        }
535
449
 
539
453
                Long contactId = ContentUris.parseId( contactUri );
540
454
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
541
455
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
542
 
                Set< String > emailsKeys = emails.keySet();
543
456
 
544
 
                // add email addresses
545
 
                Iterator< String > i = emailsKeys.iterator();
 
457
                // add phone numbers
 
458
                Set emailsKeys = emails.keySet();
 
459
                Iterator i = emailsKeys.iterator();
546
460
                while( i.hasNext() ) {
547
461
                        ContactData.EmailData email = emails.get( i.next() );
548
462
 
549
 
                        // we don't want to add this email address if it exists already or
550
 
                        // we would introduce duplicates.
 
463
                        // like with phone numbers, we don't want to add this email address
 
464
                        // if it exists already or we would introduce duplicates.
551
465
                        String address = sanitiseEmailAddress( email.getAddress() );
552
466
                        if( address == null ) continue;
553
 
                        if( _contactsCache.hasEmail( contactId, address ) ) continue;
 
467
                        HashSet< String > addresses = _contactEmails.get( contactId );
 
468
                        if( addresses != null && addresses.contains( address ) ) continue;
554
469
 
555
470
                        // add phone number
556
471
                        ContentValues values = new ContentValues();
560
475
                        if( email.isPreferred() )
561
476
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
562
477
                        _doit.getContentResolver().insert( contactContactMethodsUri,
563
 
                                values );
564
 
 
565
 
                        // and add this address to the cache to prevent a addition of
566
 
                        // duplicate date from another file
567
 
                        _contactsCache.addEmail( contactId, address );
 
478
                                        values );
568
479
                }
569
480
        }
570
481
 
571
 
        private void importContactAddresses( Uri contactUri,
572
 
                HashMap< String, ContactData.AddressData > addresses )
 
482
        synchronized private void finish( boolean offerBack )
 
483
                        throws AbortImportException
573
484
        {
574
 
                Long contactId = ContentUris.parseId( contactUri );
575
 
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
576
 
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
577
 
                Set< String > addressesKeys = addresses.keySet();
578
 
 
579
 
                // add addresses
580
 
                Iterator< String > i = addressesKeys.iterator();
581
 
                while( i.hasNext() ) {
582
 
                        ContactData.AddressData address = addresses.get( i.next() );
583
 
 
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() ) )
588
 
                                continue;
589
 
 
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,
596
 
                                values );
597
 
 
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() );
601
 
                }
 
485
                // notify Doit that we're finished
 
486
                _doit._handler.sendEmptyMessage(
 
487
                                offerBack? MESSAGE_FINISHED_BACK : MESSAGE_FINISHED );
 
488
 
 
489
                // stop
 
490
                throw new AbortImportException();
602
491
        }
603
492
 
604
 
        synchronized protected void checkAbort() throws AbortImportException
 
493
        synchronized private void checkAbort() throws AbortImportException
605
494
        {
606
495
                if( _abort ) {
607
496
                        // stop
609
498
                }
610
499
        }
611
500
 
612
 
        static public String sanitisePhoneNumber( String number )
 
501
        private void buildContactsCache() throws AbortImportException
 
502
        {
 
503
                // update UI
 
504
                setProgressMessage( R.string.doit_caching );
 
505
 
 
506
                String[] cols;
 
507
                Cursor cur;
 
508
 
 
509
                // init contacts caches
 
510
                _contacts = new HashMap< String, Long >();
 
511
                _contactNumbers = new HashMap< Long, HashSet< String > >();
 
512
                _contactEmails = new HashMap< Long, HashSet< String > >();
 
513
 
 
514
                // query and store map of contact names to ids
 
515
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
 
516
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
 
517
                                cols, null, null, null);
 
518
                if( cur.moveToFirst() ) {
 
519
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
 
520
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
 
521
                        do {
 
522
                                _contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
 
523
                        } while( cur.moveToNext() );
 
524
                }
 
525
 
 
526
                // query and store map of contact ids to sets of phone numbers
 
527
                cols = new String[] { Contacts.Phones.PERSON_ID,
 
528
                                Contacts.Phones.NUMBER };
 
529
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
 
530
                                cols, null, null, null);
 
531
                if( cur.moveToFirst() ) {
 
532
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
 
533
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
 
534
                        do {
 
535
                                Long id = cur.getLong( personIdCol );
 
536
                                String number = sanitisePhoneNumber(
 
537
                                                cur.getString( numberCol ) );
 
538
                                if( number != null ) {
 
539
                                        HashSet< String > numbers = _contactNumbers.get( id );
 
540
                                        if( numbers == null ) {
 
541
                                                _contactNumbers.put( id, new HashSet< String >() );
 
542
                                                numbers = _contactNumbers.get( id );
 
543
                                        }
 
544
                                        numbers.add( number );
 
545
                                }
 
546
                        } while( cur.moveToNext() );
 
547
                }
 
548
 
 
549
                // query and store map of contact ids to sets of email addresses
 
550
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
 
551
                                Contacts.ContactMethods.DATA };
 
552
                cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
553
                                cols, Contacts.ContactMethods.KIND + " = ?",
 
554
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
 
555
                if( cur.moveToFirst() ) {
 
556
                        int personIdCol = cur.getColumnIndex(
 
557
                                        Contacts.ContactMethods.PERSON_ID );
 
558
                        int addressCol = cur.getColumnIndex(
 
559
                                        Contacts.ContactMethods.DATA );
 
560
                        do {
 
561
                                Long id = cur.getLong( personIdCol );
 
562
                                String address = sanitiseEmailAddress(
 
563
                                                cur.getString( addressCol ) );
 
564
                                if( address != null ) {
 
565
                                        HashSet< String > addresses = _contactEmails.get( id );
 
566
                                        if( addresses == null ) {
 
567
                                                _contactEmails.put( id, new HashSet< String >() );
 
568
                                                addresses = _contactEmails.get( id );
 
569
                                        }
 
570
                                        addresses.add( address );
 
571
                                }
 
572
                        } while( cur.moveToNext() );
 
573
                }
 
574
        }
 
575
 
 
576
        private String sanitisePhoneNumber( String number )
613
577
        {
614
578
                number = number.replaceAll( "[-\\(\\) ]", "" );
615
 
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
 
579
                Pattern p = Pattern.compile( "^\\+?[0-9]+" );
616
580
                Matcher m = p.matcher( number );
617
581
                if( m.lookingAt() ) return m.group( 0 );
618
582
                return null;
619
583
        }
620
584
 
621
 
        static public String sanitiseEmailAddress( String address )
 
585
        private String sanitiseEmailAddress( String address )
622
586
        {
623
587
                address = address.trim();
624
588
                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])?)+$" );
 
589
                                "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
626
590
                Matcher m = p.matcher( address );
627
591
                if( m.matches() ) {
628
592
                        String[] bits = address.split( "@" );