/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 12:58:54 UTC
  • Revision ID: edam@waxworlds.org-20090111125854-u8ofzso4jatk12me
- added "all done" message
- rewrote Importer.finish() to make the exit process more consistent
- moved Doit's message definitions to Doit
- ensure the importer is destroyed in Doit.onPause()
- only show the toaster popup if a) there is an importer to abort, and b) the abort actually did something (i.e., it's not already aborted)
- bugfix: added some checks for abortion to the Importer after wait()ing after a dialog. Importer.wake() now alsy does a notify() to break out of dialog waits.
- also, made Importer.checkAbort() protected, so it can be checked from specific importers as desired

Show diffs side-by-side

added added

removed removed

17
17
 
18
18
public class Importer extends Thread
19
19
{
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;
 
20
        public final static int ACTION_GOBACK = 0;
 
21
        public final static int ACTION_ABORT = 1;
 
22
        public final static int ACTION_ALLDONE = 2;
33
23
 
34
24
        public final static int RESPONSE_NEGATIVE = 0;
35
25
        public final static int RESPONSE_POSITIVE = 1;
46
36
        private int _mergeSetting;
47
37
        private int _lastMergeDecision;
48
38
        private boolean _abort = false;
 
39
        private boolean _isFinished = false;
49
40
 
50
41
        public class ContactData
51
42
        {
151
142
                        onImport();
152
143
 
153
144
                        // done!
154
 
                        finish();
 
145
                        finish( ACTION_ALLDONE );
155
146
                }
156
147
                catch( AbortImportException e )
157
148
                {}
 
149
 
 
150
                // flag as finished to prevent interrupts
 
151
                setIsFinished();
 
152
        }
 
153
 
 
154
        synchronized private void setIsFinished()
 
155
        {
 
156
                _isFinished = true;
158
157
        }
159
158
 
160
159
        protected void onImport() throws AbortImportException
178
177
                notify();
179
178
        }
180
179
 
181
 
        synchronized public void setAbort()
 
180
        synchronized public boolean setAbort()
182
181
        {
183
 
                _abort = true;
 
182
                if( !_isFinished && !_abort ) {
 
183
                        _abort = true;
 
184
                        notify();
 
185
                        return true;
 
186
                }
 
187
                return false;
184
188
        }
185
189
 
186
190
        protected SharedPreferences getSharedPreferences()
198
202
        {
199
203
                checkAbort();
200
204
                _doit._handler.sendMessage( Message.obtain(
201
 
                                _doit._handler, MESSAGE_ERROR, message ) );
 
205
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
202
206
                try {
203
207
                        wait();
204
208
                }
205
209
                catch( InterruptedException e ) { }
206
 
                finish( true );
 
210
                // no need to check if an abortion happened during the wait, we are
 
211
                // about to finish anyway!
 
212
                finish( ACTION_ABORT );
207
213
        }
208
214
 
209
215
        protected void showFatalError( int res ) throws AbortImportException
216
222
        {
217
223
                checkAbort();
218
224
                _doit._handler.sendMessage( Message.obtain(
219
 
                                _doit._handler, MESSAGE_ERROR, message ) );
 
225
                                _doit._handler, Doit.MESSAGE_ERROR, message ) );
220
226
                try {
221
227
                        wait();
222
228
                }
223
229
                catch( InterruptedException e ) { }
224
 
                finish( false );
 
230
                // no need to check if an abortion happened during the wait, we are
 
231
                // about to finish anyway!
 
232
                finish( ACTION_ABORT );
225
233
        }
226
234
 
227
235
        protected boolean showContinue( int res ) throws AbortImportException
234
242
        {
235
243
                checkAbort();
236
244
                _doit._handler.sendMessage( Message.obtain(
237
 
                                _doit._handler, MESSAGE_CONTINUEORABORT, message ) );
 
245
                                _doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
238
246
                try {
239
247
                        wait();
240
248
                }
241
249
                catch( InterruptedException e ) { }
 
250
 
 
251
                // check if an abortion happened during the wait
 
252
                checkAbort();
 
253
 
242
254
                return _response == RESPONSE_POSITIVE;
243
255
        }
244
256
 
246
258
        {
247
259
                checkAbort();
248
260
                _doit._handler.sendMessage( Message.obtain( _doit._handler,
249
 
                                MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
 
261
                                Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
250
262
        }
251
263
 
252
264
        protected void setProgressMax( int maxProgress )
254
266
        {
255
267
                checkAbort();
256
268
                _doit._handler.sendMessage( Message.obtain(
257
 
                                _doit._handler, MESSAGE_SETMAXPROGRESS,
 
269
                                _doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
258
270
                                new Integer( maxProgress ) ) );
259
271
        }
260
272
 
262
274
        {
263
275
                checkAbort();
264
276
                _doit._handler.sendMessage( Message.obtain(
265
 
                                _doit._handler, MESSAGE_SETTMPPROGRESS,
 
277
                                _doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
266
278
                                new Integer( tmpProgress ) ) );
267
279
        }
268
280
 
270
282
        {
271
283
                checkAbort();
272
284
                _doit._handler.sendMessage( Message.obtain(
273
 
                                _doit._handler, MESSAGE_SETPROGRESS,
 
285
                                _doit._handler, Doit.MESSAGE_SETPROGRESS,
274
286
                                new Integer( progress ) ) );
275
287
        }
276
288
 
277
 
        protected void finish() throws AbortImportException
 
289
        protected void finish( int action ) throws AbortImportException
278
290
        {
279
 
                finish( false );
280
 
        }
 
291
                // update UI to reflect action
 
292
                int message;
 
293
                switch( action )
 
294
                {
 
295
                case ACTION_GOBACK:             message = Doit.MESSAGE_FINISHED_GOBACK; break;
 
296
                case ACTION_ALLDONE:    message = Doit.MESSAGE_FINISHED_ALLDONE; break;
 
297
                default:        // fall through
 
298
                case ACTION_ABORT:              message = Doit.MESSAGE_FINISHED; break;
 
299
                }
 
300
                _doit._handler.sendEmptyMessage( message );
281
301
 
282
 
        protected void abort() throws AbortImportException
283
 
        {
284
 
                finish( true );
 
302
                // stop
 
303
                throw new AbortImportException();
285
304
        }
286
305
 
287
306
        protected CharSequence getText( int res )
296
315
                return isImportRequired( name, _mergeSetting );
297
316
        }
298
317
 
299
 
        synchronized private boolean isImportRequired( String name, int mergeSetting )
 
318
        synchronized private boolean isImportRequired( String name,
 
319
                        int mergeSetting ) throws AbortImportException
300
320
        {
301
321
                _lastMergeDecision = mergeSetting;
302
322
 
314
334
 
315
335
                        // ok, it exists, so do prompt
316
336
                        _doit._handler.sendMessage( Message.obtain(
317
 
                                        _doit._handler, MESSAGE_MERGEPROMPT, name ) );
 
337
                                        _doit._handler, Doit.MESSAGE_MERGEPROMPT, name ) );
318
338
                        try {
319
339
                                wait();
320
340
                        }
321
341
                        catch( InterruptedException e ) { }
322
342
 
 
343
                        // check if an abortion happened during the wait
 
344
                        checkAbort();
 
345
 
323
346
                        // if "always" was selected, make choice permenant
324
347
                        if( _responseExtra == RESPONSEEXTRA_ALWAYS )
325
348
                                _mergeSetting = _response;
336
359
        protected void skipContact() throws AbortImportException
337
360
        {
338
361
                checkAbort();
339
 
                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTSKIPPED );
 
362
                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
340
363
        }
341
364
 
342
365
        protected void importContact( ContactData contact )
344
367
        {
345
368
                checkAbort();
346
369
 
347
 
                if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
348
 
                        abort();
 
370
//              if( !showContinue( "====[ IMPORTING ]====\n: " + contact._name ) )
 
371
//                      finish( ACTION_ABORT );
349
372
 
350
373
                ContentValues values = new ContentValues();
351
374
                boolean uiInformed = false;
368
391
                                contactUri = null;
369
392
 
370
393
                                // upate the UI
371
 
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTOVERWRITTEN );
 
394
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTOVERWRITTEN );
372
395
                                uiInformed = true;
373
396
 
374
397
                                // update cache
397
420
 
398
421
                        // update UI
399
422
                        if( !uiInformed ) {
400
 
                                _doit._handler.sendEmptyMessage( MESSAGE_CONTACTCREATED );
 
423
                                _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTCREATED );
401
424
                                uiInformed = true;
402
425
                        }
403
426
                }
404
427
 
405
428
                // update UI
406
429
                if( !uiInformed )
407
 
                        _doit._handler.sendEmptyMessage( MESSAGE_CONTACTMERGED );
 
430
                        _doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTMERGED );
408
431
 
409
432
                // import contact parts
410
433
                if( contact._phones != null )
479
502
                }
480
503
        }
481
504
 
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
 
505
        synchronized protected void checkAbort() throws AbortImportException
494
506
        {
495
507
                if( _abort ) {
496
508
                        // stop