/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-05-05 21:49:43 UTC
  • Revision ID: edam@waxworlds.org-20110505214943-bg0cn6qz0gr49dlk
- updated TODO
- made varibale names consistent (camelCaseVariables now_use_underscores)

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 _startedProgress;
72
 
        private int _maxProgress;
73
 
        private int _tmpProgress;
 
71
        private boolean _started_progress;
 
72
        private int _max_progress;
 
73
        private int _tmp_progress;
74
74
        private int _progress;
75
 
        protected String _dialogMessage;
76
 
        private Dialog _mergePromptDialog;
77
 
        private boolean _mergePromptAlwaysSelected;
78
 
        private int _nextAction;
79
 
        private int _currentDialogId;
 
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;
80
80
 
81
 
        private int _countOverwrites;
82
 
        private int _countCreates;
83
 
        private int _countMerges;
84
 
        private int _countSkips;
 
81
        private int _count_overwrites;
 
82
        private int _count_creates;
 
83
        private int _count_merges;
 
84
        private int _count_skips;
85
85
 
86
86
        protected Importer _importer = null;
87
87
 
105
105
                                manualAbort();
106
106
                                break;
107
107
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
108
                                _dialog_message = (String)msg.obj;
109
109
                                showDialog( DIALOG_ERROR );
110
110
                                break;
111
111
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
112
                                _dialog_message = (String)msg.obj;
113
113
                                showDialog( DIALOG_CONTINUEORABORT );
114
114
                                break;
115
115
                        case MESSAGE_SETPROGRESSMESSAGE:
117
117
                                        setText( (String)msg.obj );
118
118
                                break;
119
119
                        case MESSAGE_SETMAXPROGRESS:
120
 
                                if( _maxProgress > 0 ) {
121
 
                                        if( _tmpProgress == _maxProgress - 1 )
122
 
                                                _tmpProgress = (Integer)msg.obj;
123
 
                                        if( _progress == _maxProgress - 1 )
 
120
                                if( _max_progress > 0 ) {
 
121
                                        if( _tmp_progress == _max_progress - 1 )
 
122
                                                _tmp_progress = (Integer)msg.obj;
 
123
                                        if( _progress == _max_progress - 1 )
124
124
                                                _progress = (Integer)msg.obj;
125
125
                                }
126
 
                                _maxProgress = (Integer)msg.obj;
 
126
                                _max_progress = (Integer)msg.obj;
127
127
                                updateProgress();
128
128
                                break;
129
129
                        case MESSAGE_SETTMPPROGRESS:
130
 
                                _tmpProgress = (Integer)msg.obj;
 
130
                                _tmp_progress = (Integer)msg.obj;
131
131
                                updateProgress();
132
132
                                break;
133
133
                        case MESSAGE_SETPROGRESS:
134
 
                                _startedProgress = true;
 
134
                                _started_progress = true;
135
135
                                _progress = (Integer)msg.obj;
136
136
                                updateProgress();
137
137
                                break;
138
138
                        case MESSAGE_MERGEPROMPT:
139
 
                                _dialogMessage = (String)msg.obj;
 
139
                                _dialog_message = (String)msg.obj;
140
140
                                showDialog( DIALOG_MERGEPROMPT );
141
141
                                break;
142
142
                        case MESSAGE_CONTACTOVERWRITTEN:
143
 
                                _countOverwrites++;
 
143
                                _count_overwrites++;
144
144
                                updateStats();
145
145
                                break;
146
146
                        case MESSAGE_CONTACTCREATED:
147
 
                                _countCreates++;
 
147
                                _count_creates++;
148
148
                                updateStats();
149
149
                                break;
150
150
                        case MESSAGE_CONTACTMERGED:
151
 
                                _countMerges++;
 
151
                                _count_merges++;
152
152
                                updateStats();
153
153
                                break;
154
154
                        case MESSAGE_CONTACTSKIPPED:
155
 
                                _countSkips++;
 
155
                                _count_skips++;
156
156
                                updateStats();
157
157
                                break;
158
158
                        default:
162
162
        }
163
163
 
164
164
        @Override
165
 
        protected void onCreate(Bundle savedInstanceState)
 
165
        protected void onCreate(Bundle saved_instance_state)
166
166
        {
167
167
                setContentView( R.layout.doit );
168
 
                super.onCreate( savedInstanceState );
 
168
                super.onCreate( saved_instance_state );
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
 
                _startedProgress = false;
182
 
                _maxProgress = 0;
183
 
                _tmpProgress = 0;
 
181
                _started_progress = false;
 
182
                _max_progress = 0;
 
183
                _tmp_progress = 0;
184
184
                _progress = 0;
185
185
                _handler = new DoitHandler();
186
186
 
187
 
                _countOverwrites = 0;
188
 
                _countCreates = 0;
189
 
                _countMerges = 0;
190
 
                _countSkips = 0;
 
187
                _count_overwrites = 0;
 
188
                _count_creates = 0;
 
189
                _count_merges = 0;
 
190
                _count_skips = 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( _nextAction != NEXT_CLOSE )
 
204
                if( _next_action != 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
223
                                                        Doit.this._importer.wake();
224
224
                                                }
225
225
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
 
226
                                .setOnCancelListener( _dialog_on_cancel_listener )
227
227
                                .create();
228
228
                case DIALOG_CONTINUEORABORT:
229
229
                        return new AlertDialog.Builder( this )
232
232
                                .setMessage( "" )
233
233
                                .setPositiveButton( R.string.error_continue,
234
234
                                        new DialogInterface.OnClickListener() {
235
 
                                                public void onClick(DialogInterface dialog,
236
 
                                                        int whichButton)
 
235
                                                public void onClick( DialogInterface dialog,
 
236
                                                        int which_button )
237
237
                                                {
238
238
                                                        Doit.this._importer.wake(
239
239
                                                                Importer.RESPONSE_POSITIVE );
241
241
                                        } )
242
242
                                .setNegativeButton( R.string.error_abort,
243
243
                                        new DialogInterface.OnClickListener() {
244
 
                                                public void onClick(DialogInterface dialog,
245
 
                                                        int whichButton)
 
244
                                                public void onClick( DialogInterface dialog,
 
245
                                                        int which_button )
246
246
                                                {
247
247
                                                        Doit.this._importer.wake(
248
248
                                                                Importer.RESPONSE_NEGATIVE );
249
249
                                                }
250
250
                                        } )
251
 
                                .setOnCancelListener( _dialogOnCancelListener )
 
251
                                .setOnCancelListener( _dialog_on_cancel_listener )
252
252
                                .create();
253
253
                case DIALOG_MERGEPROMPT:
254
254
                        // custom layout in an AlertDialog
255
255
                        LayoutInflater factory = LayoutInflater.from( this );
256
 
                        final View dialogView = factory.inflate(
 
256
                        final View dialog_view = factory.inflate(
257
257
                                R.layout.mergeprompt, null );
258
 
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
 
258
                        ( (CheckBox)dialog_view.findViewById( R.id.mergeprompt_always ) ).
259
259
                                setOnCheckedChangeListener(
260
260
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged( CompoundButton buttonView,
262
 
                                                        boolean isChecked )
 
261
                                                public void onCheckedChanged(
 
262
                                                        CompoundButton button_view, boolean is_checked )
263
263
                                                {
264
 
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
264
                                                        Doit.this._merge_prompt_always_selected =
 
265
                                                                is_checked;
265
266
                                                }
266
267
                                        } );
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;
 
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;
276
277
                        return new AlertDialog.Builder( this )
277
278
                                .setIcon( R.drawable.alert_dialog_icon )
278
279
                                .setTitle( R.string.mergeprompt_title )
279
 
                                .setView( dialogView )
280
 
                                .setOnCancelListener( _dialogOnCancelListener )
 
280
                                .setView( dialog_view )
 
281
                                .setOnCancelListener( _dialog_on_cancel_listener )
281
282
                                .create();
282
283
                }
283
284
                return null;
284
285
        }
285
286
 
286
 
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
287
        private OnClickListener _merge_prompt_button_listener =
 
288
                new OnClickListener()
 
289
        {
287
290
                public void onClick( View view )
288
291
                {
289
292
                        // handle abort
292
295
 
293
296
                        // else, response (just check we haven't aborted already!)
294
297
                        else if( Doit.this._importer != null ) {
295
 
                                int responseExtra = _mergePromptAlwaysSelected?
 
298
                                int response_extra = _merge_prompt_always_selected?
296
299
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
297
300
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
298
 
                                        responseExtra );
 
301
                                        response_extra );
299
302
                        }
300
303
 
301
304
                        // close dialog and free (don't keep a reference)
302
 
                        Doit.this._mergePromptDialog.dismiss();
303
 
                        Doit.this._mergePromptDialog = null;
 
305
                        Doit.this._merge_prompt_dialog.dismiss();
 
306
                        Doit.this._merge_prompt_dialog = null;
304
307
                }
305
308
        };
306
309
 
310
313
                Button next = (Button)findViewById( R.id.next );
311
314
                next.setEnabled( false );
312
315
 
313
 
                switch( _nextAction )
 
316
                switch( _next_action )
314
317
                {
315
318
                case NEXT_BEGIN:
316
319
                        importContacts();
327
330
                manualAbort( false );
328
331
        }
329
332
 
330
 
        private void manualAbort( boolean showToasterPopup )
 
333
        private void manualAbort( boolean show_toaster_popup )
331
334
        {
332
 
                abortImport( showToasterPopup );
 
335
                abortImport( show_toaster_popup );
333
336
 
334
337
                updateNext( NEXT_CLOSE );
335
338
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
341
344
 
342
345
                // close any open dialogs
343
346
                try {
344
 
                        dismissDialog( _currentDialogId );
 
347
                        dismissDialog( _current_dialog_id );
345
348
                }
346
349
                catch( Exception e ) {
347
350
                        // ignore errors
348
351
                }
349
352
        }
350
353
 
351
 
        private void updateNext( int nextAction )
 
354
        private void updateNext( int next_action )
352
355
        {
353
356
                Button next = (Button)findViewById( R.id.next );
354
 
                switch( nextAction ) {
 
357
                switch( next_action ) {
355
358
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
356
359
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
357
360
                }
358
361
                next.setEnabled( true );
359
 
                _nextAction = nextAction;
 
362
                _next_action = next_action;
360
363
        }
361
364
 
362
365
        public static int convertIdToAction( int id ) {
377
380
                }
378
381
        }
379
382
 
380
 
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
381
 
                        new DialogInterface.OnCancelListener() {
 
383
        private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
 
384
                new DialogInterface.OnCancelListener()
 
385
        {
382
386
                public void onCancel( DialogInterface dialog ) {
383
387
                        manualAbort();
384
388
                }
386
390
 
387
391
 
388
392
        @Override
389
 
        protected void onActivityResult( int requestCode, int resultCode,
390
 
                        Intent data )
 
393
        protected void onActivityResult( int request_code, int result_code,
 
394
                Intent data )
391
395
        {
392
396
                // if we're cancelling, abort any import
393
 
                if( resultCode == RESULT_CANCELED )
 
397
                if( result_code == RESULT_CANCELED )
394
398
                        abortImport( true );
395
399
        }
396
400
 
397
401
        @Override
398
 
        protected void onPrepareDialog(int id, Dialog dialog)
 
402
        protected void onPrepareDialog( int id, Dialog dialog )
399
403
        {
400
 
                _currentDialogId = id;
 
404
                _current_dialog_id = id;
401
405
 
402
406
                switch( id )
403
407
                {
404
408
                case DIALOG_ERROR:      // fall through
405
409
                case DIALOG_CONTINUEORABORT:
406
410
                        // set dialog message
407
 
                        ( (AlertDialog)dialog ).setMessage( _dialogMessage );
 
411
                        ( (AlertDialog)dialog ).setMessage( _dialog_message );
408
412
                        break;
409
413
                case DIALOG_MERGEPROMPT:
410
414
                        // set contact's name
411
 
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
412
 
                                _dialogMessage );
 
415
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) )
 
416
                                .setText( _dialog_message );
413
417
                        // and set up reference to dialog
414
 
                        _mergePromptDialog = dialog;
 
418
                        _merge_prompt_dialog = dialog;
415
419
                        break;
416
420
                }
417
421
 
437
441
        private void updateProgress()
438
442
        {
439
443
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
440
 
                TextView outOf = (TextView)findViewById( R.id.doit_outof );
 
444
                TextView out_of = (TextView)findViewById( R.id.doit_outof );
441
445
 
442
 
                if( _maxProgress > 0 )
 
446
                if( _max_progress > 0 )
443
447
                {
444
 
                        bar.setMax( _maxProgress );
445
 
                        bar.setSecondaryProgress( _tmpProgress );
 
448
                        bar.setMax( _max_progress );
 
449
                        bar.setSecondaryProgress( _tmp_progress );
446
450
 
447
 
                        if( _startedProgress )
 
451
                        if( _started_progress )
448
452
                        {
449
453
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
450
 
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
451
 
                                outOf.setText( _progress + "/" + _maxProgress );
 
454
                                        (int)Math.round( 100 * _progress / _max_progress ) + "%" );
 
455
                                out_of.setText( _progress + "/" + _max_progress );
452
456
                                bar.setProgress( _progress );
453
457
                        }
454
458
                }
457
461
        private void updateStats()
458
462
        {
459
463
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
460
 
                        "" + _countOverwrites );
 
464
                        "" + _count_overwrites );
461
465
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
462
 
                        "" + _countCreates );
 
466
                        "" + _count_creates );
463
467
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
464
 
                        "" + _countMerges );
 
468
                        "" + _count_merges );
465
469
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
466
 
                        "" + _countSkips );
 
470
                        "" + _count_skips );
467
471
        }
468
472
 
469
 
        private void abortImport( boolean showToasterPopup )
 
473
        private void abortImport( boolean show_toaster_popup )
470
474
        {
471
475
                if( _importer != null )
472
476
                {
483
487
                                }
484
488
 
485
489
                                // notify the user
486
 
                                if( showToasterPopup )
 
490
                                if( show_toaster_popup )
487
491
                                        Toast.makeText( this, R.string.doit_importaborted,
488
492
                                                Toast.LENGTH_LONG ).show();
489
493
                        }