/android/import-contacts

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

« back to all changes in this revision

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

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

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * Importer.java
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 
 *
6
 
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
 
 *
10
 
 * This program is free software: you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
 */
23
 
 
24
1
package org.waxworlds.importcontacts;
25
2
 
26
3
import java.util.HashMap;
40
17
 
41
18
public class Importer extends Thread
42
19
{
43
 
        public final static int ACTION_GOBACK = 0;
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;
59
46
        private int _mergeSetting;
60
47
        private int _lastMergeDecision;
61
48
        private boolean _abort = false;
62
 
        private boolean _isFinished = false;
63
49
 
64
50
        public class ContactData
65
51
        {
150
136
                _doit = doit;
151
137
 
152
138
                SharedPreferences prefs = getSharedPreferences();
153
 
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
 
139
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
154
140
        }
155
141
 
156
142
        @Override
165
151
                        onImport();
166
152
 
167
153
                        // done!
168
 
                        finish( ACTION_ALLDONE );
 
154
                        finish();
169
155
                }
170
156
                catch( AbortImportException e )
171
157
                {}
172
 
 
173
 
                // flag as finished to prevent interrupts
174
 
                setIsFinished();
175
 
        }
176
 
 
177
 
        synchronized private void setIsFinished()
178
 
        {
179
 
                _isFinished = true;
180
158
        }
181
159
 
182
160
        protected void onImport() throws AbortImportException
200
178
                notify();
201
179
        }
202
180
 
203
 
        synchronized public boolean setAbort()
 
181
        synchronized public void setAbort()
204
182
        {
205
 
                if( !_isFinished && !_abort ) {
206
 
                        _abort = true;
207
 
                        notify();
208
 
                        return true;
209
 
                }
210
 
                return false;
 
183
                _abort = true;
211
184
        }
212
185
 
213
186
        protected SharedPreferences getSharedPreferences()
225
198
        {
226
199
                checkAbort();
227
200
                _doit._handler.sendMessage( Message.obtain(
228
 
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
201
                                _doit._handler, MESSAGE_ERROR, message ) );
229
202
                try {
230
203
                        wait();
231
204
                }
232
205
                catch( InterruptedException e ) { }
233
 
                // no need to check if an abortion happened during the wait, we are
234
 
                // about to finish anyway!
235
 
                finish( ACTION_ABORT );
 
206
                finish( true );
236
207
        }
237
208
 
238
209
        protected void showFatalError( int res ) throws AbortImportException
245
216
        {
246
217
                checkAbort();
247
218
                _doit._handler.sendMessage( Message.obtain(
248
 
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
 
219
                                _doit._handler, MESSAGE_ERROR, message ) );
249
220
                try {
250
221
                        wait();
251
222
                }
252
223
                catch( InterruptedException e ) { }
253
 
                // no need to check if an abortion happened during the wait, we are
254
 
                // about to finish anyway!
255
 
                finish( ACTION_ABORT );
 
224
                finish( false );
256
225
        }
257
226
 
258
227
        protected boolean showContinue( int res ) throws AbortImportException
265
234
        {
266
235
                checkAbort();
267
236
                _doit._handler.sendMessage( Message.obtain(
268
 
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
 
237
                                _doit._handler, MESSAGE_CONTINUEORABORT, message ) );
269
238
                try {
270
239
                        wait();
271
240
                }
272
241
                catch( InterruptedException e ) { }
273
 
 
274
 
                // check if an abortion happened during the wait
275
 
                checkAbort();
276
 
 
277
242
                return _response == RESPONSE_POSITIVE;
278
243
        }
279
244
 
281
246
        {
282
247
                checkAbort();
283
248
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
284
 
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
249
                                MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
285
250
        }
286
251
 
287
252
        protected void setProgressMax( int maxProgress )
289
254
        {
290
255
                checkAbort();
291
256
                _doit._handler.sendMessage( Message.obtain(
292
 
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
 
257
                                _doit._handler, MESSAGE_SETMAXPROGRESS,
293
258
                                new Integer( maxProgress ) ) );
294
259
        }
295
260
 
297
262
        {
298
263
                checkAbort();
299
264
                _doit._handler.sendMessage( Message.obtain(
300
 
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
 
265
                                _doit._handler, MESSAGE_SETTMPPROGRESS,
301
266
                                new Integer( tmpProgress ) ) );
302
267
        }
303
268
 
305
270
        {
306
271
                checkAbort();
307
272
                _doit._handler.sendMessage( Message.obtain(
308
 
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
 
273
                                _doit._handler, MESSAGE_SETPROGRESS,
309
274
                                new Integer( progress ) ) );
310
275
        }
311
276
 
312
 
        protected void finish( int action ) throws AbortImportException
 
277
        protected void finish() throws AbortImportException
313
278
        {
314
 
                // update UI to reflect action
315
 
                int message;
316
 
                switch( action )
317
 
                {
318
 
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
319
 
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
320
 
                default:        // fall through
321
 
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
322
 
                }
323
 
                _doit._handler.sendEmptyMessage( message );
 
279
                finish( false );
 
280
        }
324
281
 
325
 
                // stop
326
 
                throw new AbortImportException();
 
282
        protected void abort() throws AbortImportException
 
283
        {
 
284
                finish( true );
327
285
        }
328
286
 
329
287
        protected CharSequence getText( int res )
338
296
                return isImportRequired( name, _mergeSetting );
339
297
        }
340
298
 
341
 
        synchronized private boolean isImportRequired( String name,
342
 
                        int mergeSetting ) throws AbortImportException
 
299
        synchronized private boolean isImportRequired( String name, int mergeSetting )
343
300
        {
344
301
                _lastMergeDecision = mergeSetting;
345
302
 
346
303
                // handle special cases
347
304
                switch( mergeSetting )
348
305
                {
349
 
                case Doit.ACTION_KEEP:
 
306
                case R.id.merge_keep:
350
307
                        // if we keep contacts on duplicate, we better check for one
351
308
                        return !_contacts.containsKey( name );
352
309
 
353
 
                case Doit.ACTION_PROMPT:
 
310
                case R.id.merge_prompt:
354
311
                        // if we are prompting on duplicate, we better check for one
355
312
                        if( !_contacts.containsKey( name ) )
356
313
                                return true;
357
314
 
358
315
                        // ok, it exists, so do prompt
359
316
                        _doit._handler.sendMessage( Message.obtain(
360
 
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
 
317
                                        _doit._handler, MESSAGE_MERGEPROMPT, name ) );
361
318
                        try {
362
319
                                wait();
363
320
                        }
364
321
                        catch( InterruptedException e ) { }
365
322
 
366
 
                        // check if an abortion happened during the wait
367
 
                        checkAbort();
368
 
 
369
323
                        // if "always" was selected, make choice permenant
370
324
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
371
325
                                _mergeSetting = _response;
382
336
        protected void skipContact() throws AbortImportException
383
337
        {
384
338
                checkAbort();
385
 
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
 
339
                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
386
340
        }
387
341
 
388
342
        protected void importContact( ContactData contact )
390
344
        {
391
345
                checkAbort();
392
346
 
393
 
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
394
 
//                      finish( ACTION_ABORT );
 
347
                if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
 
348
                        abort();
395
349
 
396
350
                ContentValues values = new ContentValues();
397
351
                boolean uiInformed = false;
402
356
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
403
357
                {
404
358
                        // should we skip this import altogether?
405
 
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
 
359
                        if( _lastMergeDecision == R.id.merge_keep ) return;
406
360
 
407
361
                        // get contact's URI
408
362
                        contactUri = ContentUris.withAppendedId(
409
363
                                        Contacts.People.CONTENT_URI, id );
410
364
 
411
365
                        // should we destroy the existing contact before importing?
412
 
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
 
366
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
413
367
                                _doit.getContentResolver().delete( contactUri, null, null );
414
368
                                contactUri = null;
415
369
 
416
370
                                // upate the UI
417
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
 
371
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
418
372
                                uiInformed = true;
419
373
 
420
374
                                // update cache
434
388
                        if( id <= 0 ) return;   // shouldn't happen!
435
389
 
436
390
                        // add them to the "My Contacts" group
437
 
                        Contacts.People.addToMyContactsGroup(
438
 
                                        _doit.getContentResolver(), id );
 
391
                        Contacts.People.addToGroup(
 
392
                                        _doit.getContentResolver(), id,
 
393
                                        Contacts.Groups.GROUP_MY_CONTACTS );
439
394
 
440
395
                        // update cache
441
396
                        _contacts.put( contact._name, id );
442
397
 
443
398
                        // update UI
444
399
                        if( !uiInformed ) {
445
 
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
 
400
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
446
401
                                uiInformed = true;
447
402
                        }
448
403
                }
449
404
 
450
405
                // update UI
451
406
                if( !uiInformed )
452
 
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
 
407
                        _doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
453
408
 
454
409
                // import contact parts
455
410
                if( contact._phones != null )
464
419
                Long contactId = ContentUris.parseId( contactUri );
465
420
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
466
421
                                Contacts.People.Phones.CONTENT_DIRECTORY );
 
422
 
 
423
                // add phone numbers
467
424
                Set phonesKeys = phones.keySet();
468
 
 
469
 
                // add phone numbers
470
425
                Iterator i = phonesKeys.iterator();
471
426
                while( i.hasNext() ) {
472
427
                        ContactData.PhoneData phone = phones.get( i.next() );
490
445
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
491
446
                        _doit.getContentResolver().insert( contactPhonesUri, values );
492
447
                }
493
 
 
494
 
                // now add those phone numbers to the cache to prevent the addition of
495
 
                // duplicate data from another file
496
 
                i = phonesKeys.iterator();
497
 
                while( i.hasNext() ) {
498
 
                        ContactData.PhoneData phone = phones.get( i.next() );
499
 
 
500
 
                        String number = sanitisePhoneNumber( phone._number );
501
 
                        if( number != null ) {
502
 
                                HashSet< String > numbers = _contactNumbers.get( contactId );
503
 
                                if( numbers == null ) {
504
 
                                        _contactNumbers.put( contactId, new HashSet< String >() );
505
 
                                        numbers = _contactNumbers.get( contactId );
506
 
                                }
507
 
                                numbers.add( number );
508
 
                        }
509
 
                }
510
448
        }
511
449
 
512
450
        private void importContactEmails( Uri contactUri,
515
453
                Long contactId = ContentUris.parseId( contactUri );
516
454
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
517
455
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
456
 
 
457
                // add phone numbers
518
458
                Set emailsKeys = emails.keySet();
519
 
 
520
 
                // add email addresses
521
459
                Iterator i = emailsKeys.iterator();
522
460
                while( i.hasNext() ) {
523
461
                        ContactData.EmailData email = emails.get( i.next() );
539
477
                        _doit.getContentResolver().insert( contactContactMethodsUri,
540
478
                                        values );
541
479
                }
542
 
 
543
 
                // now add those email addresses to the cache to prevent the addition of
544
 
                // duplicate data from another file
545
 
                i = emailsKeys.iterator();
546
 
                while( i.hasNext() ) {
547
 
                        ContactData.EmailData email = emails.get( i.next() );
548
 
 
549
 
                        String address = sanitiseEmailAddress( email.getAddress() );
550
 
                        if( address != null ) {
551
 
                                HashSet< String > addresses = _contactEmails.get( contactId );
552
 
                                if( addresses == null ) {
553
 
                                        _contactEmails.put( contactId, new HashSet< String >() );
554
 
                                        addresses = _contactEmails.get( contactId );
555
 
                                }
556
 
                                addresses.add( address );
557
 
                        }
558
 
                }
559
 
        }
560
 
 
561
 
        synchronized protected void checkAbort() throws AbortImportException
 
480
        }
 
481
 
 
482
        synchronized private void finish( boolean offerBack )
 
483
                        throws AbortImportException
 
484
        {
 
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();
 
491
        }
 
492
 
 
493
        synchronized private void checkAbort() throws AbortImportException
562
494
        {
563
495
                if( _abort ) {
564
496
                        // stop