/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/Doit.java

  • Committer: edam
  • Date: 2009-01-28 19:55:17 UTC
  • Revision ID: edam@waxworlds.org-20090128195517-z1rmmys8tc8zvgqe
- made behaviour of next button overridable
- added abort button to merge prompt
- moved begin and close buttons to the next button, iverriding its behaviour
- added abort button to import and wired it up
- bugfix: tmp-progress bar didn't fill up when progress max was changed

Show diffs side-by-side

added added

removed removed

21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.edam.importcontacts;
 
24
package org.waxworlds.importcontacts;
25
25
 
26
26
import android.app.AlertDialog;
27
27
import android.app.Dialog;
46
46
        private final static int DIALOG_CONTINUEORABORT = 1;
47
47
        private final static int DIALOG_MERGEPROMPT = 2;
48
48
 
49
 
        public final static int MESSAGE_ALLDONE = 0;
50
 
        public final static int MESSAGE_ABORT = 1;
 
49
        public final static int MESSAGE_FINISHED_ALLDONE = 0;
 
50
        public final static int MESSAGE_FINISHED = 1;
 
51
        public final static int MESSAGE_FINISHED_GOBACK = 2;
51
52
        public final static int MESSAGE_ERROR = 3;
52
53
        public final static int MESSAGE_CONTINUEORABORT = 4;
53
54
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
76
77
        private Dialog _mergePromptDialog;
77
78
        private boolean _mergePromptAlwaysSelected;
78
79
        private int _nextAction;
79
 
        private int _currentDialogId;
80
80
 
81
81
        private int _countOverwrites;
82
82
        private int _countCreates;
93
93
                public void handleMessage( Message msg ) {
94
94
                        switch( msg.what )
95
95
                        {
96
 
                        case MESSAGE_ALLDONE:
 
96
                        case MESSAGE_FINISHED_ALLDONE:
97
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
98
 
                                        setVisibility( View.VISIBLE );
99
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
98
                                                setVisibility( View.VISIBLE );
 
99
                                // fall through
 
100
                        case MESSAGE_FINISHED:
100
101
                                updateNext( NEXT_CLOSE );
101
102
                                findViewById( R.id.doit_abort_disp ).setVisibility(
102
 
                                        View.GONE );
 
103
                                                View.GONE );
103
104
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
 
105
                        case MESSAGE_FINISHED_GOBACK:
 
106
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
107
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
108
                                                View.GONE );
106
109
                                break;
107
110
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
111
                                Doit.this._dialogMessage = (String)msg.obj;
109
112
                                showDialog( DIALOG_ERROR );
110
113
                                break;
111
114
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
115
                                Doit.this._dialogMessage = (String)msg.obj;
113
116
                                showDialog( DIALOG_CONTINUEORABORT );
114
117
                                break;
115
118
                        case MESSAGE_SETPROGRESSMESSAGE:
116
119
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
117
 
                                        setText( (String)msg.obj );
 
120
                                                setText( (String)msg.obj );
118
121
                                break;
119
122
                        case MESSAGE_SETMAXPROGRESS:
120
123
                                if( _maxProgress > 0 ) {
201
204
                super.onPause();
202
205
 
203
206
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _nextAction != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
207
                abortImport( true );
206
208
        }
207
209
 
208
210
        @Override
209
 
        protected Dialog onCreateDialog( int id )
 
211
        protected Dialog onCreateDialog(int id)
210
212
        {
211
213
                switch( id )
212
214
                {
213
215
                case DIALOG_ERROR:
214
216
                        return new AlertDialog.Builder( this )
215
 
                                .setIcon( R.drawable.alert_dialog_icon )
216
 
                                .setTitle( R.string.error_title )
217
 
                                .setMessage( "" )
218
 
                                .setPositiveButton( R.string.error_ok,
219
 
                                        new DialogInterface.OnClickListener() {
 
217
                                        .setIcon( R.drawable.alert_dialog_icon )
 
218
                                        .setTitle( R.string.error_title )
 
219
                                        .setMessage( "" )
 
220
                                        .setPositiveButton( R.string.error_ok,
 
221
                                                        new DialogInterface.OnClickListener() {
220
222
                                                public void onClick(DialogInterface dialog,
221
 
                                                        int whichButton)
222
 
                                                {
 
223
                                                                int whichButton) {
223
224
                                                        Doit.this._importer.wake();
224
225
                                                }
225
226
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
227
 
                                .create();
 
227
                                        .setOnCancelListener( _dialogOnCancelListener )
 
228
                                        .create();
228
229
                case DIALOG_CONTINUEORABORT:
229
230
                        return new AlertDialog.Builder( this )
230
 
                                .setIcon( R.drawable.alert_dialog_icon )
231
 
                                .setTitle( R.string.error_title )
232
 
                                .setMessage( "" )
233
 
                                .setPositiveButton( R.string.error_continue,
234
 
                                        new DialogInterface.OnClickListener() {
235
 
                                                public void onClick(DialogInterface dialog,
236
 
                                                        int whichButton)
237
 
                                                {
238
 
                                                        Doit.this._importer.wake(
239
 
                                                                Importer.RESPONSE_POSITIVE );
240
 
                                                }
241
 
                                        } )
242
 
                                .setNegativeButton( R.string.error_abort,
243
 
                                        new DialogInterface.OnClickListener() {
244
 
                                                public void onClick(DialogInterface dialog,
245
 
                                                        int whichButton)
246
 
                                                {
247
 
                                                        Doit.this._importer.wake(
248
 
                                                                Importer.RESPONSE_NEGATIVE );
249
 
                                                }
250
 
                                        } )
251
 
                                .setOnCancelListener( _dialogOnCancelListener )
252
 
                                .create();
 
231
                                        .setIcon( R.drawable.alert_dialog_icon )
 
232
                                        .setTitle( R.string.error_title )
 
233
                                        .setMessage( "" )
 
234
                                        .setPositiveButton( R.string.error_continue,
 
235
                                                        new DialogInterface.OnClickListener() {
 
236
                                                public void onClick(DialogInterface dialog,
 
237
                                                                int whichButton) {
 
238
                                                        Doit.this._importer.wake(
 
239
                                                                        Importer.RESPONSE_POSITIVE );
 
240
                                                }
 
241
                                        } )
 
242
                                        .setNegativeButton( R.string.error_abort,
 
243
                                                        new DialogInterface.OnClickListener() {
 
244
                                                public void onClick(DialogInterface dialog,
 
245
                                                                int whichButton) {
 
246
                                                        Doit.this._importer.wake(
 
247
                                                                        Importer.RESPONSE_NEGATIVE );
 
248
                                                }
 
249
                                        } )
 
250
                                        .setOnCancelListener( _dialogOnCancelListener )
 
251
                                        .create();
253
252
                case DIALOG_MERGEPROMPT:
254
253
                        // custom layout in an AlertDialog
255
254
                        LayoutInflater factory = LayoutInflater.from( this );
256
255
                        final View dialogView = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
 
256
                                        R.layout.mergeprompt, null );
258
257
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
259
 
                                setOnCheckedChangeListener(
260
 
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged( CompoundButton buttonView,
262
 
                                                        boolean isChecked )
263
 
                                                {
264
 
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
265
 
                                                }
266
 
                                        } );
 
258
                                        setOnCheckedChangeListener(
 
259
                                                        new CompoundButton.OnCheckedChangeListener() {
 
260
                                public void onCheckedChanged( CompoundButton buttonView,
 
261
                                                boolean isChecked ) {
 
262
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
263
                                }
 
264
                        } );
267
265
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
268
 
                                setOnClickListener( _mergePromptButtonListener );
 
266
                                        setOnClickListener( _mergePromptButtonListener );
269
267
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
270
 
                                setOnClickListener( _mergePromptButtonListener );
 
268
                                        setOnClickListener( _mergePromptButtonListener );
271
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
272
 
                                setOnClickListener( _mergePromptButtonListener );
 
270
                                        setOnClickListener( _mergePromptButtonListener );
273
271
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
272
                                        setOnClickListener( _mergePromptButtonListener );
275
273
                        _mergePromptAlwaysSelected = false;
276
274
                        return new AlertDialog.Builder( this )
277
 
                                .setIcon( R.drawable.alert_dialog_icon )
278
 
                                .setTitle( R.string.mergeprompt_title )
279
 
                                .setView( dialogView )
280
 
                                .setOnCancelListener( _dialogOnCancelListener )
281
 
                                .create();
 
275
                                        .setIcon( R.drawable.alert_dialog_icon )
 
276
                                        .setTitle( R.string.mergeprompt_title )
 
277
                                        .setView( dialogView )
 
278
                                        .setOnCancelListener( _dialogOnCancelListener )
 
279
                                        .create();
282
280
                }
283
281
                return null;
284
282
        }
285
283
 
286
 
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
284
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
287
285
                public void onClick( View view )
288
286
                {
289
287
                        // handle abort
290
288
                        if( view.getId() == R.id.abort )
291
289
                                manualAbort();
292
290
 
293
 
                        // else, response (just check we haven't aborted already!)
294
 
                        else if( Doit.this._importer != null ) {
 
291
                        // else, response
 
292
                        else {
295
293
                                int responseExtra = _mergePromptAlwaysSelected?
296
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
294
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
297
295
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
298
 
                                        responseExtra );
 
296
                                                responseExtra );
299
297
                        }
300
298
 
301
299
                        // close dialog and free (don't keep a reference)
316
314
                        importContacts();
317
315
                        break;
318
316
                case NEXT_CLOSE:
319
 
                        setResult( RESULT_OK );
 
317
                        setResult( RESULT_CANCELED );
320
318
                        finish();
321
319
                        break;
322
320
                }
324
322
 
325
323
        private void manualAbort()
326
324
        {
327
 
                manualAbort( false );
328
 
        }
329
 
 
330
 
        private void manualAbort( boolean showToasterPopup )
331
 
        {
332
 
                abortImport( showToasterPopup );
333
 
 
 
325
                abortImport( false );
334
326
                updateNext( NEXT_CLOSE );
335
327
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
336
328
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
337
 
                ( (TextView)findViewById( R.id.doit_aborted ) ).
338
 
                        setVisibility( View.VISIBLE );
339
 
                ( (TextView)findViewById( R.id.doit_alldone ) ).
340
 
                        setVisibility( View.GONE );
341
 
 
342
 
                // close any open dialogs
343
 
                try {
344
 
                        dismissDialog( _currentDialogId );
345
 
                }
346
 
                catch( Exception e ) {
347
 
                        // ignore errors
348
 
                }
349
329
        }
350
330
 
351
331
        private void updateNext( int nextAction )
380
360
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
381
361
                        new DialogInterface.OnCancelListener() {
382
362
                public void onCancel( DialogInterface dialog ) {
383
 
                        manualAbort();
 
363
                        setResult( RESULT_CANCELED );
 
364
                        finish();
384
365
                }
385
366
        };
386
367
 
387
 
 
388
368
        @Override
389
369
        protected void onActivityResult( int requestCode, int resultCode,
390
370
                        Intent data )
397
377
        @Override
398
378
        protected void onPrepareDialog(int id, Dialog dialog)
399
379
        {
400
 
                _currentDialogId = id;
401
 
 
402
380
                switch( id )
403
381
                {
404
382
                case DIALOG_ERROR:      // fall through
409
387
                case DIALOG_MERGEPROMPT:
410
388
                        // set contact's name
411
389
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
412
 
                                _dialogMessage );
 
390
                                        _dialogMessage );
413
391
                        // and set up reference to dialog
414
392
                        _mergePromptDialog = dialog;
415
393
                        break;
447
425
                        if( _startedProgress )
448
426
                        {
449
427
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
450
 
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
428
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
451
429
                                outOf.setText( _progress + "/" + _maxProgress );
452
430
                                bar.setProgress( _progress );
453
431
                        }
457
435
        private void updateStats()
458
436
        {
459
437
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
460
 
                        "" + _countOverwrites );
 
438
                                "" + _countOverwrites );
461
439
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
462
 
                        "" + _countCreates );
 
440
                                "" + _countCreates );
463
441
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
464
 
                        "" + _countMerges );
 
442
                                "" + _countMerges );
465
443
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
466
 
                        "" + _countSkips );
 
444
                                "" + _countSkips );
467
445
        }
468
446
 
469
447
        private void abortImport( boolean showToasterPopup )
485
463
                                // notify the user
486
464
                                if( showToasterPopup )
487
465
                                        Toast.makeText( this, R.string.doit_importaborted,
488
 
                                                Toast.LENGTH_LONG ).show();
 
466
                                                        Toast.LENGTH_LONG ).show();
489
467
                        }
490
468
                }
491
469