/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

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