/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: 2011-03-23 07:50:13 UTC
  • Revision ID: edam@waxworlds.org-20110323075013-qnza5odtlsjtcz0p
- updated TODO and NEWS
- handle different multiline schemes better
- import addresses
- check for escaped semi-colons and newlines in N, ADR and ORG lines
- minor optimisations

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;
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_FINISHED_ALLDONE = 0;
50
 
        public final static int MESSAGE_FINISHED = 1;
51
 
        public final static int MESSAGE_FINISHED_GOBACK = 2;
 
49
        public final static int MESSAGE_ALLDONE = 0;
 
50
        public final static int MESSAGE_ABORT = 1;
52
51
        public final static int MESSAGE_ERROR = 3;
53
52
        public final static int MESSAGE_CONTINUEORABORT = 4;
54
53
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
77
76
        private Dialog _mergePromptDialog;
78
77
        private boolean _mergePromptAlwaysSelected;
79
78
        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_FINISHED_ALLDONE:
 
96
                        case MESSAGE_ALLDONE:
97
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
98
 
                                                setVisibility( View.VISIBLE );
99
 
                                // fall through
100
 
                        case MESSAGE_FINISHED:
 
98
                                        setVisibility( View.VISIBLE );
 
99
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
101
100
                                updateNext( NEXT_CLOSE );
102
101
                                findViewById( R.id.doit_abort_disp ).setVisibility(
103
 
                                                View.GONE );
 
102
                                        View.GONE );
104
103
                                break;
105
 
                        case MESSAGE_FINISHED_GOBACK:
106
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
107
 
                                findViewById( R.id.doit_abort_disp ).setVisibility(
108
 
                                                View.GONE );
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
109
106
                                break;
110
107
                        case MESSAGE_ERROR:
111
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
112
109
                                showDialog( DIALOG_ERROR );
113
110
                                break;
114
111
                        case MESSAGE_CONTINUEORABORT:
115
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
116
113
                                showDialog( DIALOG_CONTINUEORABORT );
117
114
                                break;
118
115
                        case MESSAGE_SETPROGRESSMESSAGE:
119
116
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
120
 
                                                setText( (String)msg.obj );
 
117
                                        setText( (String)msg.obj );
121
118
                                break;
122
119
                        case MESSAGE_SETMAXPROGRESS:
123
120
                                if( _maxProgress > 0 ) {
204
201
                super.onPause();
205
202
 
206
203
                // saving the state of an import sounds complicated! Lets just abort!
207
 
                abortImport( true );
 
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 );
271
273
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
272
 
                                        setOnClickListener( _mergePromptButtonListener );
 
274
                                setOnClickListener( _mergePromptButtonListener );
273
275
                        _mergePromptAlwaysSelected = false;
274
276
                        return new AlertDialog.Builder( this )
275
 
                                        .setIcon( R.drawable.alert_dialog_icon )
276
 
                                        .setTitle( R.string.mergeprompt_title )
277
 
                                        .setView( dialogView )
278
 
                                        .setOnCancelListener( _dialogOnCancelListener )
279
 
                                        .create();
 
277
                                .setIcon( R.drawable.alert_dialog_icon )
 
278
                                .setTitle( R.string.mergeprompt_title )
 
279
                                .setView( dialogView )
 
280
                                .setOnCancelListener( _dialogOnCancelListener )
 
281
                                .create();
280
282
                }
281
283
                return null;
282
284
        }
283
285
 
284
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
286
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
285
287
                public void onClick( View view )
286
288
                {
287
289
                        // handle abort
288
290
                        if( view.getId() == R.id.abort )
289
291
                                manualAbort();
290
292
 
291
 
                        // else, response
292
 
                        else {
 
293
                        // else, response (just check we haven't aborted already!)
 
294
                        else if( Doit.this._importer != null ) {
293
295
                                int responseExtra = _mergePromptAlwaysSelected?
294
 
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
296
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
295
297
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
296
 
                                                responseExtra );
 
298
                                        responseExtra );
297
299
                        }
298
300
 
299
301
                        // close dialog and free (don't keep a reference)
314
316
                        importContacts();
315
317
                        break;
316
318
                case NEXT_CLOSE:
317
 
                        setResult( RESULT_CANCELED );
 
319
                        setResult( RESULT_OK );
318
320
                        finish();
319
321
                        break;
320
322
                }
322
324
 
323
325
        private void manualAbort()
324
326
        {
325
 
                abortImport( false );
 
327
                manualAbort( false );
 
328
        }
 
329
 
 
330
        private void manualAbort( boolean showToasterPopup )
 
331
        {
 
332
                abortImport( showToasterPopup );
 
333
 
326
334
                updateNext( NEXT_CLOSE );
327
335
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
328
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
                        // ignore errors
 
348
                }
329
349
        }
330
350
 
331
351
        private void updateNext( int nextAction )
360
380
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
361
381
                        new DialogInterface.OnCancelListener() {
362
382
                public void onCancel( DialogInterface dialog ) {
363
 
                        setResult( RESULT_CANCELED );
364
 
                        finish();
 
383
                        manualAbort();
365
384
                }
366
385
        };
367
386
 
 
387
 
368
388
        @Override
369
389
        protected void onActivityResult( int requestCode, int resultCode,
370
390
                        Intent data )
377
397
        @Override
378
398
        protected void onPrepareDialog(int id, Dialog dialog)
379
399
        {
 
400
                _currentDialogId = id;
 
401
 
380
402
                switch( id )
381
403
                {
382
404
                case DIALOG_ERROR:      // fall through
387
409
                case DIALOG_MERGEPROMPT:
388
410
                        // set contact's name
389
411
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
390
 
                                        _dialogMessage );
 
412
                                _dialogMessage );
391
413
                        // and set up reference to dialog
392
414
                        _mergePromptDialog = dialog;
393
415
                        break;
425
447
                        if( _startedProgress )
426
448
                        {
427
449
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
428
 
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
450
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
429
451
                                outOf.setText( _progress + "/" + _maxProgress );
430
452
                                bar.setProgress( _progress );
431
453
                        }
435
457
        private void updateStats()
436
458
        {
437
459
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
438
 
                                "" + _countOverwrites );
 
460
                        "" + _countOverwrites );
439
461
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
440
 
                                "" + _countCreates );
 
462
                        "" + _countCreates );
441
463
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
442
 
                                "" + _countMerges );
 
464
                        "" + _countMerges );
443
465
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
444
 
                                "" + _countSkips );
 
466
                        "" + _countSkips );
445
467
        }
446
468
 
447
469
        private void abortImport( boolean showToasterPopup )
463
485
                                // notify the user
464
486
                                if( showToasterPopup )
465
487
                                        Toast.makeText( this, R.string.doit_importaborted,
466
 
                                                        Toast.LENGTH_LONG ).show();
 
488
                                                Toast.LENGTH_LONG ).show();
467
489
                        }
468
490
                }
469
491