/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-12-11 23:57:07 UTC
  • Revision ID: edam@waxworlds.org-20101211235707-czyw48tt3hcopuwf
- read vCard files in as raw bytes now and convert to string only tentatively to check for version no.s and property names and params
- ASCII is now (correctly) the default charset
- added conversion from 8-bit ASCII to UTF-8 (not used on 7-bit US-ASCII) which works on raw bytes, not chars
- unencode quoted-printable now works on raw bytes, not chars

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Doit.java
3
3
 *
4
 
 * Copyright (C) 2009 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
5
 *
6
6
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program").  For more information, see
8
 
 * http://ed.am/dev/android/import-contacts
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
11
11
 * it under the terms of the GNU General Public License as published by
21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package am.ed.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import android.app.AlertDialog;
27
27
import android.app.Dialog;
68
68
        public final static int NEXT_BEGIN = 0;
69
69
        public final static int NEXT_CLOSE = 1;
70
70
 
71
 
        private boolean _started_progress;
72
 
        private int _max_progress;
73
 
        private int _tmp_progress;
 
71
        private boolean _startedProgress;
 
72
        private int _maxProgress;
 
73
        private int _tmpProgress;
74
74
        private int _progress;
75
 
        protected String _dialog_message;
76
 
        private Dialog _merge_prompt_dialog;
77
 
        private boolean _merge_prompt_always_selected;
78
 
        private int _next_action;
79
 
        private int _current_dialog_id;
 
75
        protected String _dialogMessage;
 
76
        private Dialog _mergePromptDialog;
 
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
80
80
 
81
 
        private int _count_overwrites;
82
 
        private int _count_creates;
83
 
        private int _count_merges;
84
 
        private int _count_skips;
 
81
        private int _countOverwrites;
 
82
        private int _countCreates;
 
83
        private int _countMerges;
 
84
        private int _countSkips;
85
85
 
86
86
        protected Importer _importer = null;
87
87
 
99
99
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
100
100
                                updateNext( NEXT_CLOSE );
101
101
                                findViewById( R.id.doit_abort_disp ).setVisibility(
102
 
                                        View.GONE );
 
102
                                                View.GONE );
103
103
                                break;
104
104
                        case MESSAGE_ABORT:
105
105
                                manualAbort();
106
106
                                break;
107
107
                        case MESSAGE_ERROR:
108
 
                                _dialog_message = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
109
109
                                showDialog( DIALOG_ERROR );
110
110
                                break;
111
111
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialog_message = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
113
113
                                showDialog( DIALOG_CONTINUEORABORT );
114
114
                                break;
115
115
                        case MESSAGE_SETPROGRESSMESSAGE:
116
116
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
117
 
                                        setText( (String)msg.obj );
 
117
                                                setText( (String)msg.obj );
118
118
                                break;
119
119
                        case MESSAGE_SETMAXPROGRESS:
120
 
                                if( _max_progress > 0 ) {
121
 
                                        if( _tmp_progress == _max_progress - 1 )
122
 
                                                _tmp_progress = (Integer)msg.obj;
123
 
                                        if( _progress == _max_progress - 1 )
 
120
                                if( _maxProgress > 0 ) {
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
 
122
                                                _tmpProgress = (Integer)msg.obj;
 
123
                                        if( _progress == _maxProgress - 1 )
124
124
                                                _progress = (Integer)msg.obj;
125
125
                                }
126
 
                                _max_progress = (Integer)msg.obj;
 
126
                                _maxProgress = (Integer)msg.obj;
127
127
                                updateProgress();
128
128
                                break;
129
129
                        case MESSAGE_SETTMPPROGRESS:
130
 
                                _tmp_progress = (Integer)msg.obj;
 
130
                                _tmpProgress = (Integer)msg.obj;
131
131
                                updateProgress();
132
132
                                break;
133
133
                        case MESSAGE_SETPROGRESS:
134
 
                                _started_progress = true;
 
134
                                _startedProgress = true;
135
135
                                _progress = (Integer)msg.obj;
136
136
                                updateProgress();
137
137
                                break;
138
138
                        case MESSAGE_MERGEPROMPT:
139
 
                                _dialog_message = (String)msg.obj;
 
139
                                _dialogMessage = (String)msg.obj;
140
140
                                showDialog( DIALOG_MERGEPROMPT );
141
141
                                break;
142
142
                        case MESSAGE_CONTACTOVERWRITTEN:
143
 
                                _count_overwrites++;
 
143
                                _countOverwrites++;
144
144
                                updateStats();
145
145
                                break;
146
146
                        case MESSAGE_CONTACTCREATED:
147
 
                                _count_creates++;
 
147
                                _countCreates++;
148
148
                                updateStats();
149
149
                                break;
150
150
                        case MESSAGE_CONTACTMERGED:
151
 
                                _count_merges++;
 
151
                                _countMerges++;
152
152
                                updateStats();
153
153
                                break;
154
154
                        case MESSAGE_CONTACTSKIPPED:
155
 
                                _count_skips++;
 
155
                                _countSkips++;
156
156
                                updateStats();
157
157
                                break;
158
158
                        default:
162
162
        }
163
163
 
164
164
        @Override
165
 
        protected void onCreate(Bundle saved_instance_state)
 
165
        protected void onCreate(Bundle savedInstanceState)
166
166
        {
167
167
                setContentView( R.layout.doit );
168
 
                super.onCreate( saved_instance_state );
 
168
                super.onCreate( savedInstanceState );
169
169
 
170
170
                // hide page 2
171
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
178
178
                        }
179
179
                } );
180
180
 
181
 
                _started_progress = false;
182
 
                _max_progress = 0;
183
 
                _tmp_progress = 0;
 
181
                _startedProgress = false;
 
182
                _maxProgress = 0;
 
183
                _tmpProgress = 0;
184
184
                _progress = 0;
185
185
                _handler = new DoitHandler();
186
186
 
187
 
                _count_overwrites = 0;
188
 
                _count_creates = 0;
189
 
                _count_merges = 0;
190
 
                _count_skips = 0;
 
187
                _countOverwrites = 0;
 
188
                _countCreates = 0;
 
189
                _countMerges = 0;
 
190
                _countSkips = 0;
191
191
 
192
192
                updateNext( NEXT_BEGIN );
193
193
 
201
201
                super.onPause();
202
202
 
203
203
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _next_action != NEXT_CLOSE )
 
204
                if( _nextAction != NEXT_CLOSE )
205
205
                        manualAbort( true );
206
206
        }
207
207
 
217
217
                                .setMessage( "" )
218
218
                                .setPositiveButton( R.string.error_ok,
219
219
                                        new DialogInterface.OnClickListener() {
220
 
                                                public void onClick( DialogInterface dialog,
221
 
                                                        int whichButton )
 
220
                                                public void onClick(DialogInterface dialog,
 
221
                                                        int whichButton)
222
222
                                                {
223
 
                                                        if( Doit.this != null )
224
 
                                                                Doit.this._importer.wake();
 
223
                                                        Doit.this._importer.wake();
225
224
                                                }
226
225
                                        } )
227
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
226
                                .setOnCancelListener( _dialogOnCancelListener )
228
227
                                .create();
229
228
                case DIALOG_CONTINUEORABORT:
230
229
                        return new AlertDialog.Builder( this )
233
232
                                .setMessage( "" )
234
233
                                .setPositiveButton( R.string.error_continue,
235
234
                                        new DialogInterface.OnClickListener() {
236
 
                                                public void onClick( DialogInterface dialog,
237
 
                                                        int which_button )
 
235
                                                public void onClick(DialogInterface dialog,
 
236
                                                        int whichButton)
238
237
                                                {
239
 
                                                        if( Doit.this != null )
240
 
                                                                Doit.this._importer.wake(
241
 
                                                                        Importer.RESPONSE_POSITIVE );
 
238
                                                        Doit.this._importer.wake(
 
239
                                                                Importer.RESPONSE_POSITIVE );
242
240
                                                }
243
241
                                        } )
244
242
                                .setNegativeButton( R.string.error_abort,
245
243
                                        new DialogInterface.OnClickListener() {
246
 
                                                public void onClick( DialogInterface dialog,
247
 
                                                        int which_button )
 
244
                                                public void onClick(DialogInterface dialog,
 
245
                                                        int whichButton)
248
246
                                                {
249
 
                                                        if( Doit.this != null )
250
 
                                                                Doit.this._importer.wake(
251
 
                                                                        Importer.RESPONSE_NEGATIVE );
 
247
                                                        Doit.this._importer.wake(
 
248
                                                                Importer.RESPONSE_NEGATIVE );
252
249
                                                }
253
250
                                        } )
254
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
251
                                .setOnCancelListener( _dialogOnCancelListener )
255
252
                                .create();
256
253
                case DIALOG_MERGEPROMPT:
257
254
                        // custom layout in an AlertDialog
258
255
                        LayoutInflater factory = LayoutInflater.from( this );
259
 
                        final View dialog_view = factory.inflate(
 
256
                        final View dialogView = factory.inflate(
260
257
                                R.layout.mergeprompt, null );
261
 
                        ( (CheckBox)dialog_view.findViewById( R.id.mergeprompt_always ) ).
 
258
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
262
259
                                setOnCheckedChangeListener(
263
260
                                        new CompoundButton.OnCheckedChangeListener() {
264
 
                                                public void onCheckedChanged(
265
 
                                                        CompoundButton button_view, boolean is_checked )
 
261
                                                public void onCheckedChanged( CompoundButton buttonView,
 
262
                                                        boolean isChecked )
266
263
                                                {
267
 
                                                        if( Doit.this != null )
268
 
                                                                Doit.this._merge_prompt_always_selected =
269
 
                                                                        is_checked;
 
264
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
270
265
                                                }
271
266
                                        } );
272
 
                        ( (Button)dialog_view.findViewById( R.id.merge_keep ) ).
273
 
                                setOnClickListener( _merge_prompt_button_listener );
274
 
                        ( (Button)dialog_view.findViewById( R.id.merge_overwrite ) ).
275
 
                                setOnClickListener( _merge_prompt_button_listener );
276
 
                        ( (Button)dialog_view.findViewById( R.id.merge_merge ) ).
277
 
                                setOnClickListener( _merge_prompt_button_listener );
278
 
                        ( (Button)dialog_view.findViewById( R.id.abort ) ).
279
 
                                setOnClickListener( _merge_prompt_button_listener );
280
 
                        _merge_prompt_always_selected = false;
 
267
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
 
268
                                setOnClickListener( _mergePromptButtonListener );
 
269
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
 
270
                                setOnClickListener( _mergePromptButtonListener );
 
271
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
 
272
                                setOnClickListener( _mergePromptButtonListener );
 
273
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
274
                                setOnClickListener( _mergePromptButtonListener );
 
275
                        _mergePromptAlwaysSelected = false;
281
276
                        return new AlertDialog.Builder( this )
282
277
                                .setIcon( R.drawable.alert_dialog_icon )
283
278
                                .setTitle( R.string.mergeprompt_title )
284
 
                                .setView( dialog_view )
285
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
279
                                .setView( dialogView )
 
280
                                .setOnCancelListener( _dialogOnCancelListener )
286
281
                                .create();
287
282
                }
288
283
                return null;
289
284
        }
290
285
 
291
 
        private OnClickListener _merge_prompt_button_listener =
292
 
                new OnClickListener()
293
 
        {
 
286
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
294
287
                public void onClick( View view )
295
288
                {
296
 
                        if( Doit.this == null ) return;
297
 
 
298
289
                        // handle abort
299
290
                        if( view.getId() == R.id.abort )
300
291
                                manualAbort();
301
292
 
302
293
                        // else, response (just check we haven't aborted already!)
303
294
                        else if( Doit.this._importer != null ) {
304
 
                                int response_extra = _merge_prompt_always_selected?
305
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
295
                                int responseExtra = _mergePromptAlwaysSelected?
 
296
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
306
297
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
307
 
                                        response_extra );
 
298
                                                responseExtra );
308
299
                        }
309
300
 
310
301
                        // close dialog and free (don't keep a reference)
311
 
                        Doit.this._merge_prompt_dialog.dismiss();
312
 
                        Doit.this._merge_prompt_dialog = null;
 
302
                        Doit.this._mergePromptDialog.dismiss();
 
303
                        Doit.this._mergePromptDialog = null;
313
304
                }
314
305
        };
315
306
 
319
310
                Button next = (Button)findViewById( R.id.next );
320
311
                next.setEnabled( false );
321
312
 
322
 
                switch( _next_action )
 
313
                switch( _nextAction )
323
314
                {
324
315
                case NEXT_BEGIN:
325
316
                        importContacts();
336
327
                manualAbort( false );
337
328
        }
338
329
 
339
 
        private void manualAbort( boolean show_toaster_popup )
 
330
        private void manualAbort( boolean showToasterPopup )
340
331
        {
341
 
                abortImport( show_toaster_popup );
 
332
                abortImport( showToasterPopup );
342
333
 
343
334
                updateNext( NEXT_CLOSE );
344
335
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
350
341
 
351
342
                // close any open dialogs
352
343
                try {
353
 
                        dismissDialog( _current_dialog_id );
 
344
                        dismissDialog( _currentDialogId );
354
345
                }
355
346
                catch( Exception e ) {
356
 
                        // ignore errors
357
347
                }
358
348
        }
359
349
 
360
 
        private void updateNext( int next_action )
 
350
        private void updateNext( int nextAction )
361
351
        {
362
352
                Button next = (Button)findViewById( R.id.next );
363
 
                switch( next_action ) {
 
353
                switch( nextAction ) {
364
354
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
365
355
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
366
356
                }
367
357
                next.setEnabled( true );
368
 
                _next_action = next_action;
 
358
                _nextAction = nextAction;
369
359
        }
370
360
 
371
361
        public static int convertIdToAction( int id ) {
386
376
                }
387
377
        }
388
378
 
389
 
        private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
390
 
                new DialogInterface.OnCancelListener()
391
 
        {
 
379
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
 
380
                        new DialogInterface.OnCancelListener() {
392
381
                public void onCancel( DialogInterface dialog ) {
393
382
                        manualAbort();
394
383
                }
396
385
 
397
386
 
398
387
        @Override
399
 
        protected void onActivityResult( int request_code, int result_code,
400
 
                Intent data )
 
388
        protected void onActivityResult( int requestCode, int resultCode,
 
389
                        Intent data )
401
390
        {
402
391
                // if we're cancelling, abort any import
403
 
                if( result_code == RESULT_CANCELED )
 
392
                if( resultCode == RESULT_CANCELED )
404
393
                        abortImport( true );
405
394
        }
406
395
 
407
396
        @Override
408
 
        protected void onPrepareDialog( int id, Dialog dialog )
 
397
        protected void onPrepareDialog(int id, Dialog dialog)
409
398
        {
410
 
                _current_dialog_id = id;
 
399
                _currentDialogId = id;
411
400
 
412
401
                switch( id )
413
402
                {
414
403
                case DIALOG_ERROR:      // fall through
415
404
                case DIALOG_CONTINUEORABORT:
416
405
                        // set dialog message
417
 
                        ( (AlertDialog)dialog ).setMessage( _dialog_message );
 
406
                        ( (AlertDialog)dialog ).setMessage( _dialogMessage );
418
407
                        break;
419
408
                case DIALOG_MERGEPROMPT:
420
409
                        // set contact's name
421
 
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) )
422
 
                                .setText( _dialog_message );
 
410
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
 
411
                                        _dialogMessage );
423
412
                        // and set up reference to dialog
424
 
                        _merge_prompt_dialog = dialog;
 
413
                        _mergePromptDialog = dialog;
425
414
                        break;
426
415
                }
427
416
 
438
427
                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
439
428
 
440
429
                // create importer
441
 
                _importer = new VcardImporter( this );
 
430
                _importer = new VCFImporter( this );
442
431
 
443
432
                // start the service's thread
444
433
                _importer.start();
447
436
        private void updateProgress()
448
437
        {
449
438
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
450
 
                TextView out_of = (TextView)findViewById( R.id.doit_outof );
 
439
                TextView outOf = (TextView)findViewById( R.id.doit_outof );
451
440
 
452
 
                if( _max_progress > 0 )
 
441
                if( _maxProgress > 0 )
453
442
                {
454
 
                        bar.setMax( _max_progress );
455
 
                        bar.setSecondaryProgress( _tmp_progress );
 
443
                        bar.setMax( _maxProgress );
 
444
                        bar.setSecondaryProgress( _tmpProgress );
456
445
 
457
 
                        if( _started_progress )
 
446
                        if( _startedProgress )
458
447
                        {
459
448
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
460
 
                                        (int)Math.round( 100 * _progress / _max_progress ) + "%" );
461
 
                                out_of.setText( _progress + "/" + _max_progress );
 
449
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
450
                                outOf.setText( _progress + "/" + _maxProgress );
462
451
                                bar.setProgress( _progress );
463
452
                        }
464
453
                }
467
456
        private void updateStats()
468
457
        {
469
458
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
470
 
                        "" + _count_overwrites );
 
459
                                "" + _countOverwrites );
471
460
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
472
 
                        "" + _count_creates );
 
461
                                "" + _countCreates );
473
462
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
474
 
                        "" + _count_merges );
 
463
                                "" + _countMerges );
475
464
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
476
 
                        "" + _count_skips );
 
465
                                "" + _countSkips );
477
466
        }
478
467
 
479
 
        private void abortImport( boolean show_toaster_popup )
 
468
        private void abortImport( boolean showToasterPopup )
480
469
        {
481
470
                if( _importer != null )
482
471
                {
493
482
                                }
494
483
 
495
484
                                // notify the user
496
 
                                if( show_toaster_popup )
 
485
                                if( showToasterPopup )
497
486
                                        Toast.makeText( this, R.string.doit_importaborted,
498
 
                                                Toast.LENGTH_LONG ).show();
 
487
                                                        Toast.LENGTH_LONG ).show();
499
488
                        }
500
489
                }
501
490