/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 19:16:04 UTC
  • Revision ID: edam@waxworlds.org-20090110191604-24xjo5brlvislhqw
- added toaster message about import abortion in onPause()
- added TODO list

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