/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

Lines of Context:
36
36
import android.widget.Button;
37
37
import android.widget.CheckBox;
38
38
import android.widget.CompoundButton;
39
 
import android.widget.LinearLayout;
40
39
import android.widget.ProgressBar;
41
40
import android.widget.TextView;
42
41
import android.widget.Toast;
67
66
        public final static int ACTION_MERGE_MERGE = 2;
68
67
        public final static int ACTION_OVERWRITE = 3;
69
68
 
 
69
        public final static int NEXT_BEGIN = 0;
 
70
        public final static int NEXT_CLOSE = 1;
 
71
 
70
72
        private boolean _startedProgress;
71
73
        private int _maxProgress;
72
74
        private int _tmpProgress;
74
76
        protected String _dialogMessage;
75
77
        private Dialog _mergePromptDialog;
76
78
        private boolean _mergePromptAlwaysSelected;
 
79
        private int _nextAction;
77
80
 
78
81
        private int _countOverwrites;
79
82
        private int _countCreates;
95
98
                                                setVisibility( View.VISIBLE );
96
99
                                // fall through
97
100
                        case MESSAGE_FINISHED:
98
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
99
 
                                                setVisibility( View.VISIBLE );
 
101
                                updateNext( NEXT_CLOSE );
 
102
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
103
                                                View.GONE );
100
104
                                break;
101
105
                        case MESSAGE_FINISHED_GOBACK:
102
106
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
107
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
108
                                                View.GONE );
103
109
                                break;
104
110
                        case MESSAGE_ERROR:
105
111
                                Doit.this._dialogMessage = (String)msg.obj;
115
121
                                break;
116
122
                        case MESSAGE_SETMAXPROGRESS:
117
123
                                if( _maxProgress > 0 ) {
118
 
                                        if( _tmpProgress == _maxProgress )
 
124
                                        if( _tmpProgress == _maxProgress - 1 )
119
125
                                                _tmpProgress = (Integer)msg.obj;
120
 
                                        if( _progress == _maxProgress )
 
126
                                        if( _progress == _maxProgress - 1 )
121
127
                                                _progress = (Integer)msg.obj;
122
128
                                }
123
129
                                _maxProgress = (Integer)msg.obj;
167
173
                // hide page 2
168
174
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
169
175
 
170
 
                // set up begin button
171
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
176
                // set up abort button
 
177
                Button begin = (Button)findViewById( R.id.abort );
172
178
                begin.setOnClickListener( new View.OnClickListener() {
173
179
                        public void onClick( View view ) {
174
 
                                importContacts();
175
 
                        }
176
 
                } );
177
 
 
178
 
                // set up close button
179
 
                Button close = (Button)findViewById( R.id.doit_close );
180
 
                close.setOnClickListener( new View.OnClickListener() {
181
 
                        public void onClick( View view ) {
182
 
                                setResult( RESULT_CANCELED );
183
 
                                finish();
 
180
                                manualAbort();
184
181
                        }
185
182
                } );
186
183
 
195
192
                _countMerges = 0;
196
193
                _countSkips = 0;
197
194
 
 
195
                updateNext( NEXT_BEGIN );
 
196
 
198
197
                updateProgress();
199
198
                updateStats();
200
199
        }
205
204
                super.onPause();
206
205
 
207
206
                // saving the state of an import sounds complicated! Lets just abort!
208
 
                abortImport();
209
 
 
210
 
                // destroy some stuff
211
 
                _importer = null;
212
 
                _handler = null;
 
207
                abortImport( true );
213
208
        }
214
209
 
215
210
        @Override
273
268
                                        setOnClickListener( _mergePromptButtonListener );
274
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
275
270
                                        setOnClickListener( _mergePromptButtonListener );
 
271
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
272
                                        setOnClickListener( _mergePromptButtonListener );
276
273
                        _mergePromptAlwaysSelected = false;
277
274
                        return new AlertDialog.Builder( this )
278
275
                                        .setIcon( R.drawable.alert_dialog_icon )
285
282
        }
286
283
 
287
284
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
288
 
                public void onClick( View view ) {
289
 
                        int responseExtra = _mergePromptAlwaysSelected?
290
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
285
                public void onClick( View view )
 
286
                {
 
287
                        // handle abort
 
288
                        if( view.getId() == R.id.abort )
 
289
                                manualAbort();
 
290
 
 
291
                        // else, response
 
292
                        else {
 
293
                                int responseExtra = _mergePromptAlwaysSelected?
 
294
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
295
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
296
                                                responseExtra );
 
297
                        }
 
298
 
 
299
                        // close dialog and free (don't keep a reference)
291
300
                        Doit.this._mergePromptDialog.dismiss();
292
 
                        Doit.this._mergePromptDialog = null;    // don't keep a reference!
293
 
                        Doit.this._importer.wake( convertIdToAction( view.getId() ),
294
 
                                        responseExtra );
 
301
                        Doit.this._mergePromptDialog = null;
295
302
                }
296
303
        };
297
304
 
 
305
        @Override
 
306
        protected void onNext()
 
307
        {
 
308
                Button next = (Button)findViewById( R.id.next );
 
309
                next.setEnabled( false );
 
310
 
 
311
                switch( _nextAction )
 
312
                {
 
313
                case NEXT_BEGIN:
 
314
                        importContacts();
 
315
                        break;
 
316
                case NEXT_CLOSE:
 
317
                        setResult( RESULT_CANCELED );
 
318
                        finish();
 
319
                        break;
 
320
                }
 
321
        }
 
322
 
 
323
        private void manualAbort()
 
324
        {
 
325
                abortImport( false );
 
326
                updateNext( NEXT_CLOSE );
 
327
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
328
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
 
329
        }
 
330
 
 
331
        private void updateNext( int nextAction )
 
332
        {
 
333
                Button next = (Button)findViewById( R.id.next );
 
334
                switch( nextAction ) {
 
335
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
336
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
337
                }
 
338
                next.setEnabled( true );
 
339
                _nextAction = nextAction;
 
340
        }
 
341
 
298
342
        public static int convertIdToAction( int id ) {
299
343
                switch( id ) {
300
344
                case R.id.merge_keep:           return ACTION_KEEP;
327
371
        {
328
372
                // if we're cancelling, abort any import
329
373
                if( resultCode == RESULT_CANCELED )
330
 
                        abortImport();
 
374
                        abortImport( true );
331
375
        }
332
376
 
333
377
        @Override
400
444
                                "" + _countSkips );
401
445
        }
402
446
 
403
 
        private void abortImport()
 
447
        private void abortImport( boolean showToasterPopup )
404
448
        {
405
449
                if( _importer != null )
406
450
                {
417
461
                                }
418
462
 
419
463
                                // notify the user
420
 
                        Toast.makeText( this, R.string.doit_importaborted,
421
 
                                        Toast.LENGTH_LONG ).show();
 
464
                                if( showToasterPopup )
 
465
                                        Toast.makeText( this, R.string.doit_importaborted,
 
466
                                                        Toast.LENGTH_LONG ).show();
422
467
                        }
423
468
                }
 
469
 
 
470
                // destroy some stuff
 
471
                _importer = null;
 
472
                _handler = null;
424
473
        }
425
474
}