/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-11 13:00:52 UTC
  • Revision ID: edam@waxworlds.org-20090111130052-q9bh0zz4ey47egbv
- updated 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;
 
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
{
 
20
        public final static int ACTION_GOBACK = 0;
42
21
        public final static int ACTION_ABORT = 1;
43
22
        public final static int ACTION_ALLDONE = 2;
44
23
 
51
30
        private Doit _doit;
52
31
        private int _response;
53
32
        private int _responseExtra;
 
33
        private HashMap< String, Long > _contacts;
 
34
        private HashMap< Long, HashSet< String > > _contactNumbers;
 
35
        private HashMap< Long, HashSet< String > > _contactEmails;
54
36
        private int _mergeSetting;
55
37
        private int _lastMergeDecision;
56
38
        private boolean _abort = false;
57
39
        private boolean _isFinished = false;
58
 
        private ContactsCache _contactsCache = null;
59
40
 
60
41
        public class ContactData
61
42
        {
109
90
                        }
110
91
                }
111
92
 
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
93
                public String _name = null;
132
94
                public HashMap< String, PhoneData > _phones = null;
133
95
                public HashMap< String, EmailData > _emails = null;
134
 
                public HashMap< String, AddressData > _addresses = null;
135
96
 
136
97
                protected void setName( String name )
137
98
                {
148
109
                        if( _phones == null ) _phones = new HashMap< String, PhoneData >();
149
110
                        if( !_phones.containsKey( number ) )
150
111
                                _phones.put( number,
151
 
                                        new PhoneData( number, type, isPreferred ) );
 
112
                                                new PhoneData( number, type, isPreferred ) );
152
113
                }
153
114
 
154
115
                protected void addEmail( String email, int type, boolean isPreferred )
157
118
                        if( !_emails.containsKey( email ) )
158
119
                                _emails.put( email, new EmailData( email, type, isPreferred ) );
159
120
                }
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
121
        }
169
122
 
170
 
        @SuppressWarnings("serial")
171
123
        protected class AbortImportException extends Exception { };
172
124
 
173
125
        public Importer( Doit doit )
175
127
                _doit = doit;
176
128
 
177
129
                SharedPreferences prefs = getSharedPreferences();
178
 
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
 
130
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
179
131
        }
180
132
 
181
133
        @Override
183
135
        {
184
136
                try
185
137
                {
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 );
 
138
                        // cache current contact names
 
139
                        buildContactsCache();
192
140
 
193
141
                        // do the import
194
142
                        onImport();
254
202
        {
255
203
                checkAbort();
256
204
                _doit._handler.sendMessage( Message.obtain(
257
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
205
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
258
206
                try {
259
207
                        wait();
260
208
                }
261
209
                catch( InterruptedException e ) { }
262
 
 
263
210
                // no need to check if an abortion happened during the wait, we are
264
211
                // about to finish anyway!
265
212
                finish( ACTION_ABORT );
275
222
        {
276
223
                checkAbort();
277
224
                _doit._handler.sendMessage( Message.obtain(
278
 
                        _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
225
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
279
226
                try {
280
227
                        wait();
281
228
                }
282
229
                catch( InterruptedException e ) { }
283
 
 
284
230
                // no need to check if an abortion happened during the wait, we are
285
231
                // about to finish anyway!
286
232
                finish( ACTION_ABORT );
296
242
        {
297
243
                checkAbort();
298
244
                _doit._handler.sendMessage( Message.obtain(
299
 
                        _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
245
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
300
246
                try {
301
247
                        wait();
302
248
                }
312
258
        {
313
259
                checkAbort();
314
260
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
315
 
                        Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
261
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
316
262
        }
317
263
 
318
264
        protected void setProgressMax( int maxProgress )
320
266
        {
321
267
                checkAbort();
322
268
                _doit._handler.sendMessage( Message.obtain(
323
 
                        _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
324
 
                        new Integer( maxProgress ) ) );
 
269
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
 
270
                                new Integer( maxProgress ) ) );
325
271
        }
326
272
 
327
273
        protected void setTmpProgress( int tmpProgress ) throws AbortImportException
328
274
        {
329
275
                checkAbort();
330
276
                _doit._handler.sendMessage( Message.obtain(
331
 
                        _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
332
 
                        new Integer( tmpProgress ) ) );
 
277
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
 
278
                                new Integer( tmpProgress ) ) );
333
279
        }
334
280
 
335
281
        protected void setProgress( int progress ) throws AbortImportException
336
282
        {
337
283
                checkAbort();
338
284
                _doit._handler.sendMessage( Message.obtain(
339
 
                        _doit._handler, Doit.MESSAGE_SETPROGRESS,
340
 
                        new Integer( progress ) ) );
 
285
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
 
286
                                new Integer( progress ) ) );
341
287
        }
342
288
 
343
289
        protected void finish( int action ) throws AbortImportException
346
292
                int message;
347
293
                switch( action )
348
294
                {
349
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
 
295
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
 
296
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
350
297
                default:        // fall through
351
 
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
 
298
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
352
299
                }
353
300
                _doit._handler.sendEmptyMessage( message );
354
301
 
376
323
                // handle special cases
377
324
                switch( mergeSetting )
378
325
                {
379
 
                case Doit.ACTION_KEEP:
 
326
                case R.id.merge_keep:
380
327
                        // if we keep contacts on duplicate, we better check for one
381
 
                        return !_contactsCache.exists( name );
 
328
                        return !_contacts.containsKey( name );
382
329
 
383
 
                case Doit.ACTION_PROMPT:
 
330
                case R.id.merge_prompt:
384
331
                        // if we are prompting on duplicate, we better check for one
385
 
                        if( !_contactsCache.exists( name ) )
 
332
                        if( !_contacts.containsKey( name ) )
386
333
                                return true;
387
334
 
388
335
                        // ok, it exists, so do prompt
389
336
                        _doit._handler.sendMessage( Message.obtain(
390
 
                                _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
337
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
391
338
                        try {
392
339
                                wait();
393
340
                        }
400
347
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
401
348
                                _mergeSetting = _response;
402
349
 
403
 
                        // recurse, with our new merge setting
 
350
                        // recurse, with out new merge setting
404
351
                        return isImportRequired( name, _response );
405
352
                }
406
353
 
429
376
                // does contact exist already?
430
377
                Uri contactUri = null;
431
378
                Long id;
432
 
                if( ( id = (Long)_contactsCache.getId( contact._name ) ) != null )
 
379
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
433
380
                {
434
381
                        // should we skip this import altogether?
435
 
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
 
382
                        if( _lastMergeDecision == R.id.merge_keep ) return;
436
383
 
437
384
                        // get contact's URI
438
385
                        contactUri = ContentUris.withAppendedId(
439
 
                                Contacts.People.CONTENT_URI, id );
 
386
                                        Contacts.People.CONTENT_URI, id );
440
387
 
441
388
                        // should we destroy the existing contact before importing?
442
 
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
 
389
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
443
390
                                _doit.getContentResolver().delete( contactUri, null, null );
444
391
                                contactUri = null;
445
392
 
446
 
                                // update the UI
447
 
                                _doit._handler.sendEmptyMessage(
448
 
                                                Doit.MESSAGE_CONTACTOVERWRITTEN );
 
393
                                // upate the UI
 
394
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
449
395
                                uiInformed = true;
450
396
 
451
397
                                // update cache
452
 
                                _contactsCache.remove( contact._name );
 
398
                                _contacts.remove( contact._name );
453
399
                        }
454
400
                }
455
401
 
460
406
                        // create a new contact
461
407
                        values.put( Contacts.People.NAME, contact._name );
462
408
                        contactUri = _doit.getContentResolver().insert(
463
 
                                Contacts.People.CONTENT_URI, values );
 
409
                                        Contacts.People.CONTENT_URI, values );
464
410
                        id = ContentUris.parseId( contactUri );
465
411
                        if( id <= 0 ) return;   // shouldn't happen!
466
412
 
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
 
                        }
 
413
                        // add them to the "My Contacts" group
 
414
                        Contacts.People.addToGroup(
 
415
                                        _doit.getContentResolver(), id,
 
416
                                        Contacts.Groups.GROUP_MY_CONTACTS );
475
417
 
476
418
                        // update cache
477
 
                        _contactsCache.put( id, contact._name );
 
419
                        _contacts.put( contact._name, id );
478
420
 
479
421
                        // update UI
480
422
                        if( !uiInformed ) {
492
434
                        importContactPhones( contactUri, contact._phones );
493
435
                if( contact._emails != null )
494
436
                        importContactEmails( contactUri, contact._emails );
495
 
                if( contact._addresses != null )
496
 
                        importContactAddresses( contactUri, contact._addresses );
497
437
        }
498
438
 
499
439
        private void importContactPhones( Uri contactUri,
502
442
                Long contactId = ContentUris.parseId( contactUri );
503
443
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
504
444
                                Contacts.People.Phones.CONTENT_DIRECTORY );
505
 
                Set< String > phonesKeys = phones.keySet();
506
445
 
507
446
                // add phone numbers
508
 
                Iterator< String > i = phonesKeys.iterator();
 
447
                Set phonesKeys = phones.keySet();
 
448
                Iterator i = phonesKeys.iterator();
509
449
                while( i.hasNext() ) {
510
450
                        ContactData.PhoneData phone = phones.get( i.next() );
511
451
 
518
458
                        // anyway, so it's not a problem).
519
459
                        String number = sanitisePhoneNumber( phone._number );
520
460
                        if( number == null ) continue;
521
 
                        if( _contactsCache.hasNumber( contactId, number ) ) continue;
 
461
                        HashSet< String > numbers = _contactNumbers.get( contactId );
 
462
                        if( numbers != null && numbers.contains( number ) ) continue;
522
463
 
523
464
                        // add phone number
524
465
                        ContentValues values = new ContentValues();
526
467
                        values.put( Contacts.Phones.NUMBER, phone._number );
527
468
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
528
469
                        _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
470
                }
534
471
        }
535
472
 
539
476
                Long contactId = ContentUris.parseId( contactUri );
540
477
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
541
478
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
542
 
                Set< String > emailsKeys = emails.keySet();
543
479
 
544
 
                // add email addresses
545
 
                Iterator< String > i = emailsKeys.iterator();
 
480
                // add phone numbers
 
481
                Set emailsKeys = emails.keySet();
 
482
                Iterator i = emailsKeys.iterator();
546
483
                while( i.hasNext() ) {
547
484
                        ContactData.EmailData email = emails.get( i.next() );
548
485
 
549
 
                        // we don't want to add this email address if it exists already or
550
 
                        // we would introduce duplicates.
 
486
                        // like with phone numbers, we don't want to add this email address
 
487
                        // if it exists already or we would introduce duplicates.
551
488
                        String address = sanitiseEmailAddress( email.getAddress() );
552
489
                        if( address == null ) continue;
553
 
                        if( _contactsCache.hasEmail( contactId, address ) ) continue;
 
490
                        HashSet< String > addresses = _contactEmails.get( contactId );
 
491
                        if( addresses != null && addresses.contains( address ) ) continue;
554
492
 
555
493
                        // add phone number
556
494
                        ContentValues values = new ContentValues();
560
498
                        if( email.isPreferred() )
561
499
                                values.put( Contacts.ContactMethods.ISPRIMARY, 1 );
562
500
                        _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 );
568
 
                }
569
 
        }
570
 
 
571
 
        private void importContactAddresses( Uri contactUri,
572
 
                HashMap< String, ContactData.AddressData > addresses )
573
 
        {
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() );
 
501
                                        values );
601
502
                }
602
503
        }
603
504
 
609
510
                }
610
511
        }
611
512
 
612
 
        static public String sanitisePhoneNumber( String number )
 
513
        private void buildContactsCache() throws AbortImportException
 
514
        {
 
515
                // update UI
 
516
                setProgressMessage( R.string.doit_caching );
 
517
 
 
518
                String[] cols;
 
519
                Cursor cur;
 
520
 
 
521
                // init contacts caches
 
522
                _contacts = new HashMap< String, Long >();
 
523
                _contactNumbers = new HashMap< Long, HashSet< String > >();
 
524
                _contactEmails = new HashMap< Long, HashSet< String > >();
 
525
 
 
526
                // query and store map of contact names to ids
 
527
                cols = new String[] { Contacts.People._ID, Contacts.People.NAME };
 
528
                cur = _doit.managedQuery( Contacts.People.CONTENT_URI,
 
529
                                cols, null, null, null);
 
530
                if( cur.moveToFirst() ) {
 
531
                        int idCol = cur.getColumnIndex( Contacts.People._ID );
 
532
                        int nameCol = cur.getColumnIndex( Contacts.People.NAME );
 
533
                        do {
 
534
                                _contacts.put( cur.getString( nameCol ), cur.getLong( idCol ) );
 
535
                        } while( cur.moveToNext() );
 
536
                }
 
537
 
 
538
                // query and store map of contact ids to sets of phone numbers
 
539
                cols = new String[] { Contacts.Phones.PERSON_ID,
 
540
                                Contacts.Phones.NUMBER };
 
541
                cur = _doit.managedQuery( Contacts.Phones.CONTENT_URI,
 
542
                                cols, null, null, null);
 
543
                if( cur.moveToFirst() ) {
 
544
                        int personIdCol = cur.getColumnIndex( Contacts.Phones.PERSON_ID );
 
545
                        int numberCol = cur.getColumnIndex( Contacts.Phones.NUMBER );
 
546
                        do {
 
547
                                Long id = cur.getLong( personIdCol );
 
548
                                String number = sanitisePhoneNumber(
 
549
                                                cur.getString( numberCol ) );
 
550
                                if( number != null ) {
 
551
                                        HashSet< String > numbers = _contactNumbers.get( id );
 
552
                                        if( numbers == null ) {
 
553
                                                _contactNumbers.put( id, new HashSet< String >() );
 
554
                                                numbers = _contactNumbers.get( id );
 
555
                                        }
 
556
                                        numbers.add( number );
 
557
                                }
 
558
                        } while( cur.moveToNext() );
 
559
                }
 
560
 
 
561
                // query and store map of contact ids to sets of email addresses
 
562
                cols = new String[] { Contacts.ContactMethods.PERSON_ID,
 
563
                                Contacts.ContactMethods.DATA };
 
564
                cur = _doit.managedQuery( Contacts.ContactMethods.CONTENT_URI,
 
565
                                cols, Contacts.ContactMethods.KIND + " = ?",
 
566
                                new String[] { "" + Contacts.KIND_EMAIL }, null );
 
567
                if( cur.moveToFirst() ) {
 
568
                        int personIdCol = cur.getColumnIndex(
 
569
                                        Contacts.ContactMethods.PERSON_ID );
 
570
                        int addressCol = cur.getColumnIndex(
 
571
                                        Contacts.ContactMethods.DATA );
 
572
                        do {
 
573
                                Long id = cur.getLong( personIdCol );
 
574
                                String address = sanitiseEmailAddress(
 
575
                                                cur.getString( addressCol ) );
 
576
                                if( address != null ) {
 
577
                                        HashSet< String > addresses = _contactEmails.get( id );
 
578
                                        if( addresses == null ) {
 
579
                                                _contactEmails.put( id, new HashSet< String >() );
 
580
                                                addresses = _contactEmails.get( id );
 
581
                                        }
 
582
                                        addresses.add( address );
 
583
                                }
 
584
                        } while( cur.moveToNext() );
 
585
                }
 
586
        }
 
587
 
 
588
        private String sanitisePhoneNumber( String number )
613
589
        {
614
590
                number = number.replaceAll( "[-\\(\\) ]", "" );
615
 
                Pattern p = Pattern.compile( "^[\\+0-9#*]+" );
 
591
                Pattern p = Pattern.compile( "^\\+?[0-9]+" );
616
592
                Matcher m = p.matcher( number );
617
593
                if( m.lookingAt() ) return m.group( 0 );
618
594
                return null;
619
595
        }
620
596
 
621
 
        static public String sanitiseEmailAddress( String address )
 
597
        private String sanitiseEmailAddress( String address )
622
598
        {
623
599
                address = address.trim();
624
600
                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])?)+$" );
 
601
                                "^[^ @]+@[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?(\\.[a-zA-Z]([-a-zA-Z0-9]*[a-zA-z0-9])?)+$" );
626
602
                Matcher m = p.matcher( address );
627
603
                if( m.matches() ) {
628
604
                        String[] bits = address.split( "@" );