/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;
62
60
        public final static int MESSAGE_CONTACTMERGED = 12;
63
61
        public final static int MESSAGE_CONTACTSKIPPED = 13;
64
62
 
 
63
        public final static int ACTION_PROMPT = 0;
 
64
        public final static int ACTION_KEEP = 1;
 
65
        public final static int ACTION_MERGE_MERGE = 2;
 
66
        public final static int ACTION_OVERWRITE = 3;
 
67
 
 
68
        public final static int NEXT_BEGIN = 0;
 
69
        public final static int NEXT_CLOSE = 1;
 
70
 
65
71
        private boolean _startedProgress;
66
72
        private int _maxProgress;
67
73
        private int _tmpProgress;
69
75
        protected String _dialogMessage;
70
76
        private Dialog _mergePromptDialog;
71
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
72
80
 
73
81
        private int _countOverwrites;
74
82
        private int _countCreates;
85
93
                public void handleMessage( Message msg ) {
86
94
                        switch( msg.what )
87
95
                        {
88
 
                        case MESSAGE_FINISHED_ALLDONE:
 
96
                        case MESSAGE_ALLDONE:
89
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
90
 
                                                setVisibility( View.VISIBLE );
91
 
                                // fall through
92
 
                        case MESSAGE_FINISHED:
93
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
94
 
                                                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 );
95
103
                                break;
96
 
                        case MESSAGE_FINISHED_GOBACK:
97
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
98
106
                                break;
99
107
                        case MESSAGE_ERROR:
100
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
101
109
                                showDialog( DIALOG_ERROR );
102
110
                                break;
103
111
                        case MESSAGE_CONTINUEORABORT:
104
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
105
113
                                showDialog( DIALOG_CONTINUEORABORT );
106
114
                                break;
107
115
                        case MESSAGE_SETPROGRESSMESSAGE:
110
118
                                break;
111
119
                        case MESSAGE_SETMAXPROGRESS:
112
120
                                if( _maxProgress > 0 ) {
113
 
                                        if( _tmpProgress == _maxProgress )
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
114
122
                                                _tmpProgress = (Integer)msg.obj;
115
 
                                        if( _progress == _maxProgress )
 
123
                                        if( _progress == _maxProgress - 1 )
116
124
                                                _progress = (Integer)msg.obj;
117
125
                                }
118
126
                                _maxProgress = (Integer)msg.obj;
162
170
                // hide page 2
163
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
164
172
 
165
 
                // set up begin button
166
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
173
                // set up abort button
 
174
                Button begin = (Button)findViewById( R.id.abort );
167
175
                begin.setOnClickListener( new View.OnClickListener() {
168
176
                        public void onClick( View view ) {
169
 
                                importContacts();
170
 
                        }
171
 
                } );
172
 
 
173
 
                // set up close button
174
 
                Button close = (Button)findViewById( R.id.doit_close );
175
 
                close.setOnClickListener( new View.OnClickListener() {
176
 
                        public void onClick( View view ) {
177
 
                                setResult( RESULT_CANCELED );
178
 
                                finish();
 
177
                                manualAbort();
179
178
                        }
180
179
                } );
181
180
 
190
189
                _countMerges = 0;
191
190
                _countSkips = 0;
192
191
 
 
192
                updateNext( NEXT_BEGIN );
 
193
 
193
194
                updateProgress();
194
195
                updateStats();
195
196
        }
200
201
                super.onPause();
201
202
 
202
203
                // saving the state of an import sounds complicated! Lets just abort!
203
 
                abortImport();
204
 
 
205
 
                // destroy some stuff
206
 
                _importer = null;
207
 
                _handler = null;
 
204
                if( _nextAction != NEXT_CLOSE )
 
205
                        manualAbort( true );
208
206
        }
209
207
 
210
208
        @Override
211
 
        protected Dialog onCreateDialog(int id)
 
209
        protected Dialog onCreateDialog( int id )
212
210
        {
213
211
                switch( id )
214
212
                {
215
213
                case DIALOG_ERROR:
216
214
                        return new AlertDialog.Builder( this )
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() {
 
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() {
222
220
                                                public void onClick(DialogInterface dialog,
223
 
                                                                int whichButton) {
 
221
                                                        int whichButton)
 
222
                                                {
224
223
                                                        Doit.this._importer.wake();
225
224
                                                }
226
225
                                        } )
227
 
                                        .setOnCancelListener( _dialogOnCancelListener )
228
 
                                        .create();
 
226
                                .setOnCancelListener( _dialogOnCancelListener )
 
227
                                .create();
229
228
                case DIALOG_CONTINUEORABORT:
230
229
                        return new AlertDialog.Builder( this )
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();
 
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();
252
253
                case DIALOG_MERGEPROMPT:
253
254
                        // custom layout in an AlertDialog
254
255
                        LayoutInflater factory = LayoutInflater.from( this );
255
256
                        final View dialogView = factory.inflate(
256
 
                                        R.layout.mergeprompt, null );
 
257
                                R.layout.mergeprompt, null );
257
258
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
258
 
                                        setOnCheckedChangeListener(
259
 
                                                        new CompoundButton.OnCheckedChangeListener() {
260
 
                                public void onCheckedChanged( CompoundButton buttonView,
261
 
                                                boolean isChecked ) {
262
 
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
263
 
                                }
264
 
                        } );
 
259
                                setOnCheckedChangeListener(
 
260
                                        new CompoundButton.OnCheckedChangeListener() {
 
261
                                                public void onCheckedChanged( CompoundButton buttonView,
 
262
                                                        boolean isChecked )
 
263
                                                {
 
264
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
265
                                                }
 
266
                                        } );
265
267
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
266
 
                                        setOnClickListener( _mergePromptButtonListener );
 
268
                                setOnClickListener( _mergePromptButtonListener );
267
269
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
268
 
                                        setOnClickListener( _mergePromptButtonListener );
 
270
                                setOnClickListener( _mergePromptButtonListener );
269
271
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
270
 
                                        setOnClickListener( _mergePromptButtonListener );
 
272
                                setOnClickListener( _mergePromptButtonListener );
 
273
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
274
                                setOnClickListener( _mergePromptButtonListener );
271
275
                        _mergePromptAlwaysSelected = false;
272
276
                        return new AlertDialog.Builder( this )
273
 
                                        .setIcon( R.drawable.alert_dialog_icon )
274
 
                                        .setTitle( R.string.mergeprompt_title )
275
 
                                        .setView( dialogView )
276
 
                                        .setOnCancelListener( _dialogOnCancelListener )
277
 
                                        .create();
 
277
                                .setIcon( R.drawable.alert_dialog_icon )
 
278
                                .setTitle( R.string.mergeprompt_title )
 
279
                                .setView( dialogView )
 
280
                                .setOnCancelListener( _dialogOnCancelListener )
 
281
                                .create();
278
282
                }
279
283
                return null;
280
284
        }
281
285
 
282
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
283
 
                public void onClick( View view ) {
284
 
                        int responseExtra = _mergePromptAlwaysSelected?
285
 
                                        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)
286
302
                        Doit.this._mergePromptDialog.dismiss();
287
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
288
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
303
                        Doit.this._mergePromptDialog = null;
289
304
                }
290
305
        };
291
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
 
 
361
        public static int convertIdToAction( int id ) {
 
362
                switch( id ) {
 
363
                case R.id.merge_keep:           return ACTION_KEEP;
 
364
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
365
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
366
                default: return ACTION_PROMPT;
 
367
                }
 
368
        }
 
369
 
 
370
        public static int convertActionToId( int action ) {
 
371
                switch( action ) {
 
372
                case ACTION_KEEP:               return R.id.merge_keep;
 
373
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
374
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
375
                default: return R.id.merge_prompt;
 
376
                }
 
377
        }
 
378
 
292
379
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
293
380
                        new DialogInterface.OnCancelListener() {
294
381
                public void onCancel( DialogInterface dialog ) {
295
 
                        setResult( RESULT_CANCELED );
296
 
                        finish();
 
382
                        manualAbort();
297
383
                }
298
384
        };
299
385
 
 
386
 
300
387
        @Override
301
388
        protected void onActivityResult( int requestCode, int resultCode,
302
389
                        Intent data )
303
390
        {
304
391
                // if we're cancelling, abort any import
305
392
                if( resultCode == RESULT_CANCELED )
306
 
                        abortImport();
 
393
                        abortImport( true );
307
394
        }
308
395
 
309
396
        @Override
310
397
        protected void onPrepareDialog(int id, Dialog dialog)
311
398
        {
 
399
                _currentDialogId = id;
 
400
 
312
401
                switch( id )
313
402
                {
314
403
                case DIALOG_ERROR:      // fall through
376
465
                                "" + _countSkips );
377
466
        }
378
467
 
379
 
        private void abortImport()
 
468
        private void abortImport( boolean showToasterPopup )
380
469
        {
381
470
                if( _importer != null )
382
471
                {
393
482
                                }
394
483
 
395
484
                                // notify the user
396
 
                        Toast.makeText( this, R.string.doit_importaborted,
397
 
                                        Toast.LENGTH_LONG ).show();
 
485
                                if( showToasterPopup )
 
486
                                        Toast.makeText( this, R.string.doit_importaborted,
 
487
                                                        Toast.LENGTH_LONG ).show();
398
488
                        }
399
489
                }
 
490
 
 
491
                // destroy some stuff
 
492
                _importer = null;
 
493
                _handler = null;
400
494
        }
401
495
}