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

  • Committer: edam
  • Date: 2010-10-28 15:49:21 UTC
  • Revision ID: edam@waxworlds.org-20101028154921-98svlxlpno3cpzb8
- added file chooser
- changed file/dir entry box for a button that opens the file chooser
- added dialog to ask if you want to select a dir to scan or a file
- fixed bug where you could abort as dialog opened and the dialog wasn't cancelled
- fixed crash where you could abort as the merge prompt opened and it would try to use the just-destroyed importer
- fixed bug where closing the application at the end would show "aborted!" erroniously

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.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import android.app.AlertDialog;
27
27
import android.app.Dialog;
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;
47
46
        private final static int DIALOG_CONTINUEORABORT = 1;
48
47
        private final static int DIALOG_MERGEPROMPT = 2;
49
48
 
50
 
        public final static int MESSAGE_FINISHED_ALLDONE = 0;
51
 
        public final static int MESSAGE_FINISHED = 1;
52
 
        public final static int MESSAGE_FINISHED_GOBACK = 2;
 
49
        public final static int MESSAGE_ALLDONE = 0;
 
50
        public final static int MESSAGE_ABORT = 1;
53
51
        public final static int MESSAGE_ERROR = 3;
54
52
        public final static int MESSAGE_CONTINUEORABORT = 4;
55
53
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
67
65
        public final static int ACTION_MERGE_MERGE = 2;
68
66
        public final static int ACTION_OVERWRITE = 3;
69
67
 
 
68
        public final static int NEXT_BEGIN = 0;
 
69
        public final static int NEXT_CLOSE = 1;
 
70
 
70
71
        private boolean _startedProgress;
71
72
        private int _maxProgress;
72
73
        private int _tmpProgress;
74
75
        protected String _dialogMessage;
75
76
        private Dialog _mergePromptDialog;
76
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
77
80
 
78
81
        private int _countOverwrites;
79
82
        private int _countCreates;
90
93
                public void handleMessage( Message msg ) {
91
94
                        switch( msg.what )
92
95
                        {
93
 
                        case MESSAGE_FINISHED_ALLDONE:
 
96
                        case MESSAGE_ALLDONE:
94
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
95
 
                                                setVisibility( View.VISIBLE );
96
 
                                // fall through
97
 
                        case MESSAGE_FINISHED:
98
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
99
 
                                                setVisibility( View.VISIBLE );
 
98
                                        setVisibility( View.VISIBLE );
 
99
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
100
                                updateNext( NEXT_CLOSE );
 
101
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
102
                                                View.GONE );
100
103
                                break;
101
 
                        case MESSAGE_FINISHED_GOBACK:
102
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
103
106
                                break;
104
107
                        case MESSAGE_ERROR:
105
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
106
109
                                showDialog( DIALOG_ERROR );
107
110
                                break;
108
111
                        case MESSAGE_CONTINUEORABORT:
109
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
110
113
                                showDialog( DIALOG_CONTINUEORABORT );
111
114
                                break;
112
115
                        case MESSAGE_SETPROGRESSMESSAGE:
115
118
                                break;
116
119
                        case MESSAGE_SETMAXPROGRESS:
117
120
                                if( _maxProgress > 0 ) {
118
 
                                        if( _tmpProgress == _maxProgress )
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
119
122
                                                _tmpProgress = (Integer)msg.obj;
120
 
                                        if( _progress == _maxProgress )
 
123
                                        if( _progress == _maxProgress - 1 )
121
124
                                                _progress = (Integer)msg.obj;
122
125
                                }
123
126
                                _maxProgress = (Integer)msg.obj;
167
170
                // hide page 2
168
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
169
172
 
170
 
                // set up begin button
171
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
173
                // set up abort button
 
174
                Button begin = (Button)findViewById( R.id.abort );
172
175
                begin.setOnClickListener( new View.OnClickListener() {
173
176
                        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();
 
177
                                manualAbort();
184
178
                        }
185
179
                } );
186
180
 
195
189
                _countMerges = 0;
196
190
                _countSkips = 0;
197
191
 
 
192
                updateNext( NEXT_BEGIN );
 
193
 
198
194
                updateProgress();
199
195
                updateStats();
200
196
        }
205
201
                super.onPause();
206
202
 
207
203
                // saving the state of an import sounds complicated! Lets just abort!
208
 
                abortImport();
209
 
 
210
 
                // destroy some stuff
211
 
                _importer = null;
212
 
                _handler = null;
 
204
                if( _nextAction != NEXT_CLOSE )
 
205
                        manualAbort( true );
213
206
        }
214
207
 
215
208
        @Override
216
 
        protected Dialog onCreateDialog(int id)
 
209
        protected Dialog onCreateDialog( int id )
217
210
        {
218
211
                switch( id )
219
212
                {
220
213
                case DIALOG_ERROR:
221
214
                        return new AlertDialog.Builder( this )
222
 
                                        .setIcon( R.drawable.alert_dialog_icon )
223
 
                                        .setTitle( R.string.error_title )
224
 
                                        .setMessage( "" )
225
 
                                        .setPositiveButton( R.string.error_ok,
226
 
                                                        new DialogInterface.OnClickListener() {
 
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() {
227
220
                                                public void onClick(DialogInterface dialog,
228
 
                                                                int whichButton) {
 
221
                                                        int whichButton)
 
222
                                                {
229
223
                                                        Doit.this._importer.wake();
230
224
                                                }
231
225
                                        } )
232
 
                                        .setOnCancelListener( _dialogOnCancelListener )
233
 
                                        .create();
 
226
                                .setOnCancelListener( _dialogOnCancelListener )
 
227
                                .create();
234
228
                case DIALOG_CONTINUEORABORT:
235
229
                        return new AlertDialog.Builder( this )
236
 
                                        .setIcon( R.drawable.alert_dialog_icon )
237
 
                                        .setTitle( R.string.error_title )
238
 
                                        .setMessage( "" )
239
 
                                        .setPositiveButton( R.string.error_continue,
240
 
                                                        new DialogInterface.OnClickListener() {
241
 
                                                public void onClick(DialogInterface dialog,
242
 
                                                                int whichButton) {
243
 
                                                        Doit.this._importer.wake(
244
 
                                                                        Importer.RESPONSE_POSITIVE );
245
 
                                                }
246
 
                                        } )
247
 
                                        .setNegativeButton( R.string.error_abort,
248
 
                                                        new DialogInterface.OnClickListener() {
249
 
                                                public void onClick(DialogInterface dialog,
250
 
                                                                int whichButton) {
251
 
                                                        Doit.this._importer.wake(
252
 
                                                                        Importer.RESPONSE_NEGATIVE );
253
 
                                                }
254
 
                                        } )
255
 
                                        .setOnCancelListener( _dialogOnCancelListener )
256
 
                                        .create();
 
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();
257
253
                case DIALOG_MERGEPROMPT:
258
254
                        // custom layout in an AlertDialog
259
255
                        LayoutInflater factory = LayoutInflater.from( this );
260
256
                        final View dialogView = factory.inflate(
261
 
                                        R.layout.mergeprompt, null );
 
257
                                R.layout.mergeprompt, null );
262
258
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
263
 
                                        setOnCheckedChangeListener(
264
 
                                                        new CompoundButton.OnCheckedChangeListener() {
265
 
                                public void onCheckedChanged( CompoundButton buttonView,
266
 
                                                boolean isChecked ) {
267
 
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
268
 
                                }
269
 
                        } );
 
259
                                setOnCheckedChangeListener(
 
260
                                        new CompoundButton.OnCheckedChangeListener() {
 
261
                                                public void onCheckedChanged( CompoundButton buttonView,
 
262
                                                        boolean isChecked )
 
263
                                                {
 
264
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
265
                                                }
 
266
                                        } );
270
267
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
271
 
                                        setOnClickListener( _mergePromptButtonListener );
 
268
                                setOnClickListener( _mergePromptButtonListener );
272
269
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
273
 
                                        setOnClickListener( _mergePromptButtonListener );
 
270
                                setOnClickListener( _mergePromptButtonListener );
274
271
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
275
 
                                        setOnClickListener( _mergePromptButtonListener );
 
272
                                setOnClickListener( _mergePromptButtonListener );
 
273
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
274
                                setOnClickListener( _mergePromptButtonListener );
276
275
                        _mergePromptAlwaysSelected = false;
277
276
                        return new AlertDialog.Builder( this )
278
 
                                        .setIcon( R.drawable.alert_dialog_icon )
279
 
                                        .setTitle( R.string.mergeprompt_title )
280
 
                                        .setView( dialogView )
281
 
                                        .setOnCancelListener( _dialogOnCancelListener )
282
 
                                        .create();
 
277
                                .setIcon( R.drawable.alert_dialog_icon )
 
278
                                .setTitle( R.string.mergeprompt_title )
 
279
                                .setView( dialogView )
 
280
                                .setOnCancelListener( _dialogOnCancelListener )
 
281
                                .create();
283
282
                }
284
283
                return null;
285
284
        }
286
285
 
287
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
288
 
                public void onClick( View view ) {
289
 
                        int responseExtra = _mergePromptAlwaysSelected?
290
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
286
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
287
                public void onClick( View view )
 
288
                {
 
289
                        // handle abort
 
290
                        if( view.getId() == R.id.abort )
 
291
                                manualAbort();
 
292
 
 
293
                        // else, response (just check we haven't aborted already!)
 
294
                        else if( Doit.this._importer != null ) {
 
295
                                int responseExtra = _mergePromptAlwaysSelected?
 
296
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
297
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
298
                                                responseExtra );
 
299
                        }
 
300
 
 
301
                        // close dialog and free (don't keep a reference)
291
302
                        Doit.this._mergePromptDialog.dismiss();
292
 
                        Doit.this._mergePromptDialog = null;    // don't keep a reference!
293
 
                        Doit.this._importer.wake( convertIdToAction( view.getId() ),
294
 
                                        responseExtra );
 
303
                        Doit.this._mergePromptDialog = null;
295
304
                }
296
305
        };
297
306
 
 
307
        @Override
 
308
        protected void onNext()
 
309
        {
 
310
                Button next = (Button)findViewById( R.id.next );
 
311
                next.setEnabled( false );
 
312
 
 
313
                switch( _nextAction )
 
314
                {
 
315
                case NEXT_BEGIN:
 
316
                        importContacts();
 
317
                        break;
 
318
                case NEXT_CLOSE:
 
319
                        setResult( RESULT_OK );
 
320
                        finish();
 
321
                        break;
 
322
                }
 
323
        }
 
324
 
 
325
        private void manualAbort()
 
326
        {
 
327
                manualAbort( false );
 
328
        }
 
329
 
 
330
        private void manualAbort( boolean showToasterPopup )
 
331
        {
 
332
                abortImport( showToasterPopup );
 
333
 
 
334
                updateNext( NEXT_CLOSE );
 
335
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
336
                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
                }
 
348
        }
 
349
 
 
350
        private void updateNext( int nextAction )
 
351
        {
 
352
                Button next = (Button)findViewById( R.id.next );
 
353
                switch( nextAction ) {
 
354
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
355
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
356
                }
 
357
                next.setEnabled( true );
 
358
                _nextAction = nextAction;
 
359
        }
 
360
 
298
361
        public static int convertIdToAction( int id ) {
299
362
                switch( id ) {
300
363
                case R.id.merge_keep:           return ACTION_KEEP;
316
379
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
317
380
                        new DialogInterface.OnCancelListener() {
318
381
                public void onCancel( DialogInterface dialog ) {
319
 
                        setResult( RESULT_CANCELED );
320
 
                        finish();
 
382
                        manualAbort();
321
383
                }
322
384
        };
323
385
 
 
386
 
324
387
        @Override
325
388
        protected void onActivityResult( int requestCode, int resultCode,
326
389
                        Intent data )
327
390
        {
328
391
                // if we're cancelling, abort any import
329
392
                if( resultCode == RESULT_CANCELED )
330
 
                        abortImport();
 
393
                        abortImport( true );
331
394
        }
332
395
 
333
396
        @Override
334
397
        protected void onPrepareDialog(int id, Dialog dialog)
335
398
        {
 
399
                _currentDialogId = id;
 
400
 
336
401
                switch( id )
337
402
                {
338
403
                case DIALOG_ERROR:      // fall through
400
465
                                "" + _countSkips );
401
466
        }
402
467
 
403
 
        private void abortImport()
 
468
        private void abortImport( boolean showToasterPopup )
404
469
        {
405
470
                if( _importer != null )
406
471
                {
417
482
                                }
418
483
 
419
484
                                // notify the user
420
 
                        Toast.makeText( this, R.string.doit_importaborted,
421
 
                                        Toast.LENGTH_LONG ).show();
 
485
                                if( showToasterPopup )
 
486
                                        Toast.makeText( this, R.string.doit_importaborted,
 
487
                                                        Toast.LENGTH_LONG ).show();
422
488
                        }
423
489
                }
 
490
 
 
491
                // destroy some stuff
 
492
                _importer = null;
 
493
                _handler = null;
424
494
        }
425
495
}