/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-19 20:33:09 UTC
  • Revision ID: edam@waxworlds.org-20110319203309-5dzfyqrxwk94jtin
- formatting: removed some double-indents on overrunning lines
- updated TODO and NEWS
- rewrote central logic of parser so it makes more sense, looks nicer and has a small optimisation (getting name and params from line only when necessary)
- optimised unnecessary mutliple converting of lines to US-ASCII
- re-wrote line extraction from vcards so that we can lookahead for v3 folded lines
- added support for v3 folded lines

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:
113
116
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
114
 
                                                setText( (String)msg.obj );
 
117
                                        setText( (String)msg.obj );
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?
 
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?
290
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
                        // ignore errors
 
348
                }
 
349
        }
 
350
 
 
351
        private void updateNext( int nextAction )
 
352
        {
 
353
                Button next = (Button)findViewById( R.id.next );
 
354
                switch( nextAction ) {
 
355
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
356
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
357
                }
 
358
                next.setEnabled( true );
 
359
                _nextAction = nextAction;
 
360
        }
 
361
 
298
362
        public static int convertIdToAction( int id ) {
299
363
                switch( id ) {
300
364
                case R.id.merge_keep:           return ACTION_KEEP;
316
380
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
317
381
                        new DialogInterface.OnCancelListener() {
318
382
                public void onCancel( DialogInterface dialog ) {
319
 
                        setResult( RESULT_CANCELED );
320
 
                        finish();
 
383
                        manualAbort();
321
384
                }
322
385
        };
323
386
 
 
387
 
324
388
        @Override
325
389
        protected void onActivityResult( int requestCode, int resultCode,
326
390
                        Intent data )
327
391
        {
328
392
                // if we're cancelling, abort any import
329
393
                if( resultCode == RESULT_CANCELED )
330
 
                        abortImport();
 
394
                        abortImport( true );
331
395
        }
332
396
 
333
397
        @Override
334
398
        protected void onPrepareDialog(int id, Dialog dialog)
335
399
        {
 
400
                _currentDialogId = id;
 
401
 
336
402
                switch( id )
337
403
                {
338
404
                case DIALOG_ERROR:      // fall through
343
409
                case DIALOG_MERGEPROMPT:
344
410
                        // set contact's name
345
411
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
346
 
                                        _dialogMessage );
 
412
                                _dialogMessage );
347
413
                        // and set up reference to dialog
348
414
                        _mergePromptDialog = dialog;
349
415
                        break;
381
447
                        if( _startedProgress )
382
448
                        {
383
449
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
384
 
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
450
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
385
451
                                outOf.setText( _progress + "/" + _maxProgress );
386
452
                                bar.setProgress( _progress );
387
453
                        }
391
457
        private void updateStats()
392
458
        {
393
459
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
394
 
                                "" + _countOverwrites );
 
460
                        "" + _countOverwrites );
395
461
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
396
 
                                "" + _countCreates );
 
462
                        "" + _countCreates );
397
463
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
398
 
                                "" + _countMerges );
 
464
                        "" + _countMerges );
399
465
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
400
 
                                "" + _countSkips );
 
466
                        "" + _countSkips );
401
467
        }
402
468
 
403
 
        private void abortImport()
 
469
        private void abortImport( boolean showToasterPopup )
404
470
        {
405
471
                if( _importer != null )
406
472
                {
417
483
                                }
418
484
 
419
485
                                // notify the user
420
 
                        Toast.makeText( this, R.string.doit_importaborted,
421
 
                                        Toast.LENGTH_LONG ).show();
 
486
                                if( showToasterPopup )
 
487
                                        Toast.makeText( this, R.string.doit_importaborted,
 
488
                                                Toast.LENGTH_LONG ).show();
422
489
                        }
423
490
                }
 
491
 
 
492
                // destroy some stuff
 
493
                _importer = null;
 
494
                _handler = null;
424
495
        }
425
496
}