/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/edam/importcontacts/Importer.java

  • Committer: edam
  • Date: 2011-03-23 07:50:13 UTC
  • Revision ID: edam@waxworlds.org-20110323075013-qnza5odtlsjtcz0p
- updated TODO and NEWS
- handle different multiline schemes better
- import addresses
- check for escaped semi-colons and newlines in N, ADR and ORG lines
- minor optimisations

Show diffs side-by-side

added added

removed removed

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