/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-07-13 19:13:38 UTC
  • Revision ID: edam@waxworlds.org-20100713191338-e23wre83uellvtt4
Tags: 1.0
- updated NEWS, TODO and manifest

Show diffs side-by-side

added added

removed removed

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
 
200
200
        {
201
201
                super.onPause();
202
202
 
 
203
                // close any open dialogs
 
204
                try {
 
205
                        dismissDialog( _currentDialogId );
 
206
                }
 
207
                catch( Exception e ) {
 
208
                }
 
209
 
203
210
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _next_action != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
211
                manualAbort( true );
206
212
        }
207
213
 
208
214
        @Override
217
223
                                .setMessage( "" )
218
224
                                .setPositiveButton( R.string.error_ok,
219
225
                                        new DialogInterface.OnClickListener() {
220
 
                                                public void onClick( DialogInterface dialog,
221
 
                                                        int whichButton )
 
226
                                                public void onClick(DialogInterface dialog,
 
227
                                                        int whichButton)
222
228
                                                {
223
229
                                                        Doit.this._importer.wake();
224
230
                                                }
225
231
                                        } )
226
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
232
                                .setOnCancelListener( _dialogOnCancelListener )
227
233
                                .create();
228
234
                case DIALOG_CONTINUEORABORT:
229
235
                        return new AlertDialog.Builder( this )
232
238
                                .setMessage( "" )
233
239
                                .setPositiveButton( R.string.error_continue,
234
240
                                        new DialogInterface.OnClickListener() {
235
 
                                                public void onClick( DialogInterface dialog,
236
 
                                                        int which_button )
 
241
                                                public void onClick(DialogInterface dialog,
 
242
                                                        int whichButton)
237
243
                                                {
238
244
                                                        Doit.this._importer.wake(
239
245
                                                                Importer.RESPONSE_POSITIVE );
241
247
                                        } )
242
248
                                .setNegativeButton( R.string.error_abort,
243
249
                                        new DialogInterface.OnClickListener() {
244
 
                                                public void onClick( DialogInterface dialog,
245
 
                                                        int which_button )
 
250
                                                public void onClick(DialogInterface dialog,
 
251
                                                        int whichButton)
246
252
                                                {
247
253
                                                        Doit.this._importer.wake(
248
254
                                                                Importer.RESPONSE_NEGATIVE );
249
255
                                                }
250
256
                                        } )
251
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
257
                                .setOnCancelListener( _dialogOnCancelListener )
252
258
                                .create();
253
259
                case DIALOG_MERGEPROMPT:
254
260
                        // custom layout in an AlertDialog
255
261
                        LayoutInflater factory = LayoutInflater.from( this );
256
 
                        final View dialog_view = factory.inflate(
 
262
                        final View dialogView = factory.inflate(
257
263
                                R.layout.mergeprompt, null );
258
 
                        ( (CheckBox)dialog_view.findViewById( R.id.mergeprompt_always ) ).
 
264
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
259
265
                                setOnCheckedChangeListener(
260
266
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged(
262
 
                                                        CompoundButton button_view, boolean is_checked )
 
267
                                                public void onCheckedChanged( CompoundButton buttonView,
 
268
                                                        boolean isChecked )
263
269
                                                {
264
 
                                                        Doit.this._merge_prompt_always_selected =
265
 
                                                                is_checked;
 
270
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
266
271
                                                }
267
272
                                        } );
268
 
                        ( (Button)dialog_view.findViewById( R.id.merge_keep ) ).
269
 
                                setOnClickListener( _merge_prompt_button_listener );
270
 
                        ( (Button)dialog_view.findViewById( R.id.merge_overwrite ) ).
271
 
                                setOnClickListener( _merge_prompt_button_listener );
272
 
                        ( (Button)dialog_view.findViewById( R.id.merge_merge ) ).
273
 
                                setOnClickListener( _merge_prompt_button_listener );
274
 
                        ( (Button)dialog_view.findViewById( R.id.abort ) ).
275
 
                                setOnClickListener( _merge_prompt_button_listener );
276
 
                        _merge_prompt_always_selected = false;
 
273
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
 
274
                                setOnClickListener( _mergePromptButtonListener );
 
275
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
 
276
                                setOnClickListener( _mergePromptButtonListener );
 
277
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
 
278
                                setOnClickListener( _mergePromptButtonListener );
 
279
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
280
                                setOnClickListener( _mergePromptButtonListener );
 
281
                        _mergePromptAlwaysSelected = false;
277
282
                        return new AlertDialog.Builder( this )
278
283
                                .setIcon( R.drawable.alert_dialog_icon )
279
284
                                .setTitle( R.string.mergeprompt_title )
280
 
                                .setView( dialog_view )
281
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
 
285
                                .setView( dialogView )
 
286
                                .setOnCancelListener( _dialogOnCancelListener )
282
287
                                .create();
283
288
                }
284
289
                return null;
285
290
        }
286
291
 
287
 
        private OnClickListener _merge_prompt_button_listener =
288
 
                new OnClickListener()
289
 
        {
 
292
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
290
293
                public void onClick( View view )
291
294
                {
292
295
                        // handle abort
293
296
                        if( view.getId() == R.id.abort )
294
297
                                manualAbort();
295
298
 
296
 
                        // else, response (just check we haven't aborted already!)
297
 
                        else if( Doit.this._importer != null ) {
298
 
                                int response_extra = _merge_prompt_always_selected?
299
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
299
                        // else, response
 
300
                        else {
 
301
                                int responseExtra = _mergePromptAlwaysSelected?
 
302
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
300
303
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
301
 
                                        response_extra );
 
304
                                                responseExtra );
302
305
                        }
303
306
 
304
307
                        // close dialog and free (don't keep a reference)
305
 
                        Doit.this._merge_prompt_dialog.dismiss();
306
 
                        Doit.this._merge_prompt_dialog = null;
 
308
                        Doit.this._mergePromptDialog.dismiss();
 
309
                        Doit.this._mergePromptDialog = null;
307
310
                }
308
311
        };
309
312
 
313
316
                Button next = (Button)findViewById( R.id.next );
314
317
                next.setEnabled( false );
315
318
 
316
 
                switch( _next_action )
 
319
                switch( _nextAction )
317
320
                {
318
321
                case NEXT_BEGIN:
319
322
                        importContacts();
330
333
                manualAbort( false );
331
334
        }
332
335
 
333
 
        private void manualAbort( boolean show_toaster_popup )
 
336
        private void manualAbort( boolean showToasterPopup )
334
337
        {
335
 
                abortImport( show_toaster_popup );
 
338
                abortImport( showToasterPopup );
336
339
 
337
340
                updateNext( NEXT_CLOSE );
338
341
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
341
344
                        setVisibility( View.VISIBLE );
342
345
                ( (TextView)findViewById( R.id.doit_alldone ) ).
343
346
                        setVisibility( View.GONE );
344
 
 
345
 
                // close any open dialogs
346
 
                try {
347
 
                        dismissDialog( _current_dialog_id );
348
 
                }
349
 
                catch( Exception e ) {
350
 
                        // ignore errors
351
 
                }
352
347
        }
353
348
 
354
 
        private void updateNext( int next_action )
 
349
        private void updateNext( int nextAction )
355
350
        {
356
351
                Button next = (Button)findViewById( R.id.next );
357
 
                switch( next_action ) {
 
352
                switch( nextAction ) {
358
353
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
359
354
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
360
355
                }
361
356
                next.setEnabled( true );
362
 
                _next_action = next_action;
 
357
                _nextAction = nextAction;
363
358
        }
364
359
 
365
360
        public static int convertIdToAction( int id ) {
380
375
                }
381
376
        }
382
377
 
383
 
        private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
384
 
                new DialogInterface.OnCancelListener()
385
 
        {
 
378
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
 
379
                        new DialogInterface.OnCancelListener() {
386
380
                public void onCancel( DialogInterface dialog ) {
387
 
                        manualAbort();
 
381
                        setResult( RESULT_CANCELED );
 
382
                        finish();
388
383
                }
389
384
        };
390
385
 
391
 
 
392
386
        @Override
393
 
        protected void onActivityResult( int request_code, int result_code,
394
 
                Intent data )
 
387
        protected void onActivityResult( int requestCode, int resultCode,
 
388
                        Intent data )
395
389
        {
396
390
                // if we're cancelling, abort any import
397
 
                if( result_code == RESULT_CANCELED )
 
391
                if( resultCode == RESULT_CANCELED )
398
392
                        abortImport( true );
399
393
        }
400
394
 
401
395
        @Override
402
 
        protected void onPrepareDialog( int id, Dialog dialog )
 
396
        protected void onPrepareDialog(int id, Dialog dialog)
403
397
        {
404
 
                _current_dialog_id = id;
 
398
                _currentDialogId = id;
405
399
 
406
400
                switch( id )
407
401
                {
408
402
                case DIALOG_ERROR:      // fall through
409
403
                case DIALOG_CONTINUEORABORT:
410
404
                        // set dialog message
411
 
                        ( (AlertDialog)dialog ).setMessage( _dialog_message );
 
405
                        ( (AlertDialog)dialog ).setMessage( _dialogMessage );
412
406
                        break;
413
407
                case DIALOG_MERGEPROMPT:
414
408
                        // set contact's name
415
 
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) )
416
 
                                .setText( _dialog_message );
 
409
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
 
410
                                        _dialogMessage );
417
411
                        // and set up reference to dialog
418
 
                        _merge_prompt_dialog = dialog;
 
412
                        _mergePromptDialog = dialog;
419
413
                        break;
420
414
                }
421
415
 
441
435
        private void updateProgress()
442
436
        {
443
437
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
444
 
                TextView out_of = (TextView)findViewById( R.id.doit_outof );
 
438
                TextView outOf = (TextView)findViewById( R.id.doit_outof );
445
439
 
446
 
                if( _max_progress > 0 )
 
440
                if( _maxProgress > 0 )
447
441
                {
448
 
                        bar.setMax( _max_progress );
449
 
                        bar.setSecondaryProgress( _tmp_progress );
 
442
                        bar.setMax( _maxProgress );
 
443
                        bar.setSecondaryProgress( _tmpProgress );
450
444
 
451
 
                        if( _started_progress )
 
445
                        if( _startedProgress )
452
446
                        {
453
447
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
454
 
                                        (int)Math.round( 100 * _progress / _max_progress ) + "%" );
455
 
                                out_of.setText( _progress + "/" + _max_progress );
 
448
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
449
                                outOf.setText( _progress + "/" + _maxProgress );
456
450
                                bar.setProgress( _progress );
457
451
                        }
458
452
                }
461
455
        private void updateStats()
462
456
        {
463
457
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
464
 
                        "" + _count_overwrites );
 
458
                                "" + _countOverwrites );
465
459
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
466
 
                        "" + _count_creates );
 
460
                                "" + _countCreates );
467
461
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
468
 
                        "" + _count_merges );
 
462
                                "" + _countMerges );
469
463
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
470
 
                        "" + _count_skips );
 
464
                                "" + _countSkips );
471
465
        }
472
466
 
473
 
        private void abortImport( boolean show_toaster_popup )
 
467
        private void abortImport( boolean showToasterPopup )
474
468
        {
475
469
                if( _importer != null )
476
470
                {
487
481
                                }
488
482
 
489
483
                                // notify the user
490
 
                                if( show_toaster_popup )
 
484
                                if( showToasterPopup )
491
485
                                        Toast.makeText( this, R.string.doit_importaborted,
492
 
                                                Toast.LENGTH_LONG ).show();
 
486
                                                        Toast.LENGTH_LONG ).show();
493
487
                        }
494
488
                }
495
489