/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: 2010-07-04 14:44:48 UTC
  • Revision ID: edam@waxworlds.org-20100704144448-1m30v811opup20fs
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
- massively simplified the WizzardActivity class so it works propperly
- moved all code to org.waxworlds.edam
- added an "aborted" message when the importion is aborted
- simplified the 3 actions the worker thread can take when stopping (only 2 were actualy used) to "aborted" or "alldone"
- changed intro message to match website
- bugfix: don't blow up when the My Contacts group is missing

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;
17
40
 
18
41
public class Importer extends Thread
19
42
{
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;
 
43
        public final static int ACTION_ABORT = 1;
 
44
        public final static int ACTION_ALLDONE = 2;
33
45
 
34
46
        public final static int RESPONSE_NEGATIVE = 0;
35
47
        public final static int RESPONSE_POSITIVE = 1;
46
58
        private int _mergeSetting;
47
59
        private int _lastMergeDecision;
48
60
        private boolean _abort = false;
 
61
        private boolean _isFinished = false;
49
62
 
50
63
        public class ContactData
51
64
        {
129
142
                }
130
143
        }
131
144
 
 
145
        @SuppressWarnings("serial")
132
146
        protected class AbortImportException extends Exception { };
133
147
 
134
148
        public Importer( Doit doit )
136
150
                _doit = doit;
137
151
 
138
152
                SharedPreferences prefs = getSharedPreferences();
139
 
                _mergeSetting = prefs.getInt( "merge_setting", 0 );
 
153
                _mergeSetting = prefs.getInt( "merge_setting", Doit.ACTION_PROMPT );
140
154
        }
141
155
 
142
156
        @Override
151
165
                        onImport();
152
166
 
153
167
                        // done!
154
 
                        finish();
 
168
                        finish( ACTION_ALLDONE );
155
169
                }
156
170
                catch( AbortImportException e )
157
171
                {}
 
172
 
 
173
                // flag as finished to prevent interrupts
 
174
                setIsFinished();
 
175
        }
 
176
 
 
177
        synchronized private void setIsFinished()
 
178
        {
 
179
                _isFinished = true;
158
180
        }
159
181
 
160
182
        protected void onImport() throws AbortImportException
178
200
                notify();
179
201
        }
180
202
 
181
 
        synchronized public void setAbort()
 
203
        synchronized public boolean setAbort()
182
204
        {
183
 
                _abort = true;
 
205
                if( !_isFinished && !_abort ) {
 
206
                        _abort = true;
 
207
                        notify();
 
208
                        return true;
 
209
                }
 
210
                return false;
184
211
        }
185
212
 
186
213
        protected SharedPreferences getSharedPreferences()
198
225
        {
199
226
                checkAbort();
200
227
                _doit._handler.sendMessage( Message.obtain(
201
 
                                _doit._handler, MESSAGE_ERROR, message ) );
 
228
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
202
229
                try {
203
230
                        wait();
204
231
                }
205
232
                catch( InterruptedException e ) { }
206
 
                finish( true );
 
233
 
 
234
                // no need to check if an abortion happened during the wait, we are
 
235
                // about to finish anyway!
 
236
                finish( ACTION_ABORT );
207
237
        }
208
238
 
209
239
        protected void showFatalError( int res ) throws AbortImportException
216
246
        {
217
247
                checkAbort();
218
248
                _doit._handler.sendMessage( Message.obtain(
219
 
                                _doit._handler, MESSAGE_ERROR, message ) );
 
249
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
220
250
                try {
221
251
                        wait();
222
252
                }
223
253
                catch( InterruptedException e ) { }
224
 
                finish( false );
 
254
 
 
255
                // no need to check if an abortion happened during the wait, we are
 
256
                // about to finish anyway!
 
257
                finish( ACTION_ABORT );
225
258
        }
226
259
 
227
260
        protected boolean showContinue( int res ) throws AbortImportException
234
267
        {
235
268
                checkAbort();
236
269
                _doit._handler.sendMessage( Message.obtain(
237
 
                                _doit._handler, MESSAGE_CONTINUEORABORT, message ) );
 
270
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
238
271
                try {
239
272
                        wait();
240
273
                }
241
274
                catch( InterruptedException e ) { }
 
275
 
 
276
                // check if an abortion happened during the wait
 
277
                checkAbort();
 
278
 
242
279
                return _response == RESPONSE_POSITIVE;
243
280
        }
244
281
 
246
283
        {
247
284
                checkAbort();
248
285
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
249
 
                                MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
286
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
250
287
        }
251
288
 
252
289
        protected void setProgressMax( int maxProgress )
254
291
        {
255
292
                checkAbort();
256
293
                _doit._handler.sendMessage( Message.obtain(
257
 
                                _doit._handler, MESSAGE_SETMAXPROGRESS,
 
294
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
258
295
                                new Integer( maxProgress ) ) );
259
296
        }
260
297
 
262
299
        {
263
300
                checkAbort();
264
301
                _doit._handler.sendMessage( Message.obtain(
265
 
                                _doit._handler, MESSAGE_SETTMPPROGRESS,
 
302
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
266
303
                                new Integer( tmpProgress ) ) );
267
304
        }
268
305
 
270
307
        {
271
308
                checkAbort();
272
309
                _doit._handler.sendMessage( Message.obtain(
273
 
                                _doit._handler, MESSAGE_SETPROGRESS,
 
310
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
274
311
                                new Integer( progress ) ) );
275
312
        }
276
313
 
277
 
        protected void finish() throws AbortImportException
 
314
        protected void finish( int action ) throws AbortImportException
278
315
        {
279
 
                finish( false );
280
 
        }
 
316
                // update UI to reflect action
 
317
                int message;
 
318
                switch( action )
 
319
                {
 
320
                case ACTION_ALLDONE:    message = Doit.MESSAGE_ALLDONE; break;
 
321
                default:        // fall through
 
322
                case ACTION_ABORT:              message = Doit.MESSAGE_ABORT; break;
 
323
                }
 
324
                _doit._handler.sendEmptyMessage( message );
281
325
 
282
 
        protected void abort() throws AbortImportException
283
 
        {
284
 
                finish( true );
 
326
                // stop
 
327
                throw new AbortImportException();
285
328
        }
286
329
 
287
330
        protected CharSequence getText( int res )
296
339
                return isImportRequired( name, _mergeSetting );
297
340
        }
298
341
 
299
 
        synchronized private boolean isImportRequired( String name, int mergeSetting )
 
342
        synchronized private boolean isImportRequired( String name,
 
343
                        int mergeSetting ) throws AbortImportException
300
344
        {
301
345
                _lastMergeDecision = mergeSetting;
302
346
 
303
347
                // handle special cases
304
348
                switch( mergeSetting )
305
349
                {
306
 
                case R.id.merge_keep:
 
350
                case Doit.ACTION_KEEP:
307
351
                        // if we keep contacts on duplicate, we better check for one
308
352
                        return !_contacts.containsKey( name );
309
353
 
310
 
                case R.id.merge_prompt:
 
354
                case Doit.ACTION_PROMPT:
311
355
                        // if we are prompting on duplicate, we better check for one
312
356
                        if( !_contacts.containsKey( name ) )
313
357
                                return true;
314
358
 
315
359
                        // ok, it exists, so do prompt
316
360
                        _doit._handler.sendMessage( Message.obtain(
317
 
                                        _doit._handler, MESSAGE_MERGEPROMPT, name ) );
 
361
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
318
362
                        try {
319
363
                                wait();
320
364
                        }
321
365
                        catch( InterruptedException e ) { }
322
366
 
 
367
                        // check if an abortion happened during the wait
 
368
                        checkAbort();
 
369
 
323
370
                        // if "always" was selected, make choice permenant
324
371
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
325
372
                                _mergeSetting = _response;
336
383
        protected void skipContact() throws AbortImportException
337
384
        {
338
385
                checkAbort();
339
 
                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
 
386
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
340
387
        }
341
388
 
342
389
        protected void importContact( ContactData contact )
344
391
        {
345
392
                checkAbort();
346
393
 
347
 
                if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
348
 
                        abort();
 
394
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
 
395
//                      finish( ACTION_ABORT );
349
396
 
350
397
                ContentValues values = new ContentValues();
351
398
                boolean uiInformed = false;
356
403
                if( ( id = (Long)_contacts.get( contact._name ) ) != null )
357
404
                {
358
405
                        // should we skip this import altogether?
359
 
                        if( _lastMergeDecision == R.id.merge_keep ) return;
 
406
                        if( _lastMergeDecision == Doit.ACTION_KEEP ) return;
360
407
 
361
408
                        // get contact's URI
362
409
                        contactUri = ContentUris.withAppendedId(
363
410
                                        Contacts.People.CONTENT_URI, id );
364
411
 
365
412
                        // should we destroy the existing contact before importing?
366
 
                        if( _lastMergeDecision == R.id.merge_overwrite ) {
 
413
                        if( _lastMergeDecision == Doit.ACTION_OVERWRITE ) {
367
414
                                _doit.getContentResolver().delete( contactUri, null, null );
368
415
                                contactUri = null;
369
416
 
370
417
                                // upate the UI
371
 
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
 
418
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
372
419
                                uiInformed = true;
373
420
 
374
421
                                // update cache
387
434
                        id = ContentUris.parseId( contactUri );
388
435
                        if( id <= 0 ) return;   // shouldn't happen!
389
436
 
390
 
                        // add them to the "My Contacts" group
391
 
                        Contacts.People.addToGroup(
392
 
                                        _doit.getContentResolver(), id,
393
 
                                        Contacts.Groups.GROUP_MY_CONTACTS );
 
437
                        // try to add them to the "My Contacts" group
 
438
                        try {
 
439
                                Contacts.People.addToMyContactsGroup(
 
440
                                        _doit.getContentResolver(), id );
 
441
                        }
 
442
                        catch( IllegalStateException e ) { }
394
443
 
395
444
                        // update cache
396
445
                        _contacts.put( contact._name, id );
397
446
 
398
447
                        // update UI
399
448
                        if( !uiInformed ) {
400
 
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
 
449
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
401
450
                                uiInformed = true;
402
451
                        }
403
452
                }
404
453
 
405
454
                // update UI
406
455
                if( !uiInformed )
407
 
                        _doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
 
456
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
408
457
 
409
458
                // import contact parts
410
459
                if( contact._phones != null )
419
468
                Long contactId = ContentUris.parseId( contactUri );
420
469
                Uri contactPhonesUri = Uri.withAppendedPath( contactUri,
421
470
                                Contacts.People.Phones.CONTENT_DIRECTORY );
 
471
                Set< String > phonesKeys = phones.keySet();
422
472
 
423
473
                // add phone numbers
424
 
                Set phonesKeys = phones.keySet();
425
 
                Iterator i = phonesKeys.iterator();
 
474
                Iterator< String > i = phonesKeys.iterator();
426
475
                while( i.hasNext() ) {
427
476
                        ContactData.PhoneData phone = phones.get( i.next() );
428
477
 
445
494
                        if( phone._isPreferred ) values.put( Contacts.Phones.ISPRIMARY, 1 );
446
495
                        _doit.getContentResolver().insert( contactPhonesUri, values );
447
496
                }
 
497
 
 
498
                // now add those phone numbers to the cache to prevent the addition of
 
499
                // duplicate data from another file
 
500
                i = phonesKeys.iterator();
 
501
                while( i.hasNext() ) {
 
502
                        ContactData.PhoneData phone = phones.get( i.next() );
 
503
 
 
504
                        String number = sanitisePhoneNumber( phone._number );
 
505
                        if( number != null ) {
 
506
                                HashSet< String > numbers = _contactNumbers.get( contactId );
 
507
                                if( numbers == null ) {
 
508
                                        _contactNumbers.put( contactId, new HashSet< String >() );
 
509
                                        numbers = _contactNumbers.get( contactId );
 
510
                                }
 
511
                                numbers.add( number );
 
512
                        }
 
513
                }
448
514
        }
449
515
 
450
516
        private void importContactEmails( Uri contactUri,
453
519
                Long contactId = ContentUris.parseId( contactUri );
454
520
                Uri contactContactMethodsUri = Uri.withAppendedPath( contactUri,
455
521
                                Contacts.People.ContactMethods.CONTENT_DIRECTORY );
 
522
                Set< String > emailsKeys = emails.keySet();
456
523
 
457
 
                // add phone numbers
458
 
                Set emailsKeys = emails.keySet();
459
 
                Iterator i = emailsKeys.iterator();
 
524
                // add email addresses
 
525
                Iterator< String > i = emailsKeys.iterator();
460
526
                while( i.hasNext() ) {
461
527
                        ContactData.EmailData email = emails.get( i.next() );
462
528
 
477
543
                        _doit.getContentResolver().insert( contactContactMethodsUri,
478
544
                                        values );
479
545
                }
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
 
546
 
 
547
                // now add those email addresses to the cache to prevent the addition of
 
548
                // duplicate data from another file
 
549
                i = emailsKeys.iterator();
 
550
                while( i.hasNext() ) {
 
551
                        ContactData.EmailData email = emails.get( i.next() );
 
552
 
 
553
                        String address = sanitiseEmailAddress( email.getAddress() );
 
554
                        if( address != null ) {
 
555
                                HashSet< String > addresses = _contactEmails.get( contactId );
 
556
                                if( addresses == null ) {
 
557
                                        _contactEmails.put( contactId, new HashSet< String >() );
 
558
                                        addresses = _contactEmails.get( contactId );
 
559
                                }
 
560
                                addresses.add( address );
 
561
                        }
 
562
                }
 
563
        }
 
564
 
 
565
        synchronized protected void checkAbort() throws AbortImportException
494
566
        {
495
567
                if( _abort ) {
496
568
                        // stop