/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/importcontacts/Doit.java

  • Committer: edam
  • Date: 2009-02-02 07:07:52 UTC
  • Revision ID: edam@waxworlds.org-20090202070752-2lp8igdsdjyu9fic
- bugfix: add contacts to the "my contacts" group didn't actually work on a real device. So we're doing it a different way.
- updated todo list

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.edam.importcontacts;
 
24
package org.waxworlds.importcontacts;
25
25
 
26
26
import android.app.AlertDialog;
27
27
import android.app.Dialog;
46
46
        private final static int DIALOG_CONTINUEORABORT = 1;
47
47
        private final static int DIALOG_MERGEPROMPT = 2;
48
48
 
49
 
        public final static int MESSAGE_ALLDONE = 0;
50
 
        public final static int MESSAGE_ABORT = 1;
 
49
        public final static int MESSAGE_FINISHED_ALLDONE = 0;
 
50
        public final static int MESSAGE_FINISHED = 1;
 
51
        public final static int MESSAGE_FINISHED_GOBACK = 2;
51
52
        public final static int MESSAGE_ERROR = 3;
52
53
        public final static int MESSAGE_CONTINUEORABORT = 4;
53
54
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
68
69
        public final static int NEXT_BEGIN = 0;
69
70
        public final static int NEXT_CLOSE = 1;
70
71
 
71
 
        private boolean _started_progress;
72
 
        private int _max_progress;
73
 
        private int _tmp_progress;
 
72
        private boolean _startedProgress;
 
73
        private int _maxProgress;
 
74
        private int _tmpProgress;
74
75
        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;
 
76
        protected String _dialogMessage;
 
77
        private Dialog _mergePromptDialog;
 
78
        private boolean _mergePromptAlwaysSelected;
 
79
        private int _nextAction;
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
 
93
93
                public void handleMessage( Message msg ) {
94
94
                        switch( msg.what )
95
95
                        {
96
 
                        case MESSAGE_ALLDONE:
 
96
                        case MESSAGE_FINISHED_ALLDONE:
97
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
98
 
                                        setVisibility( View.VISIBLE );
99
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
98
                                                setVisibility( View.VISIBLE );
 
99
                                // fall through
 
100
                        case MESSAGE_FINISHED:
100
101
                                updateNext( NEXT_CLOSE );
101
102
                                findViewById( R.id.doit_abort_disp ).setVisibility(
102
 
                                        View.GONE );
 
103
                                                View.GONE );
103
104
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
 
105
                        case MESSAGE_FINISHED_GOBACK:
 
106
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
107
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
108
                                                View.GONE );
106
109
                                break;
107
110
                        case MESSAGE_ERROR:
108
 
                                _dialog_message = (String)msg.obj;
 
111
                                Doit.this._dialogMessage = (String)msg.obj;
109
112
                                showDialog( DIALOG_ERROR );
110
113
                                break;
111
114
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialog_message = (String)msg.obj;
 
115
                                Doit.this._dialogMessage = (String)msg.obj;
113
116
                                showDialog( DIALOG_CONTINUEORABORT );
114
117
                                break;
115
118
                        case MESSAGE_SETPROGRESSMESSAGE:
116
119
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
117
 
                                        setText( (String)msg.obj );
 
120
                                                setText( (String)msg.obj );
118
121
                                break;
119
122
                        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 )
 
123
                                if( _maxProgress > 0 ) {
 
124
                                        if( _tmpProgress == _maxProgress - 1 )
 
125
                                                _tmpProgress = (Integer)msg.obj;
 
126
                                        if( _progress == _maxProgress - 1 )
124
127
                                                _progress = (Integer)msg.obj;
125
128
                                }
126
 
                                _max_progress = (Integer)msg.obj;
 
129
                                _maxProgress = (Integer)msg.obj;
127
130
                                updateProgress();
128
131
                                break;
129
132
                        case MESSAGE_SETTMPPROGRESS:
130
 
                                _tmp_progress = (Integer)msg.obj;
 
133
                                _tmpProgress = (Integer)msg.obj;
131
134
                                updateProgress();
132
135
                                break;
133
136
                        case MESSAGE_SETPROGRESS:
134
 
                                _started_progress = true;
 
137
                                _startedProgress = true;
135
138
                                _progress = (Integer)msg.obj;
136
139
                                updateProgress();
137
140
                                break;
138
141
                        case MESSAGE_MERGEPROMPT:
139
 
                                _dialog_message = (String)msg.obj;
 
142
                                _dialogMessage = (String)msg.obj;
140
143
                                showDialog( DIALOG_MERGEPROMPT );
141
144
                                break;
142
145
                        case MESSAGE_CONTACTOVERWRITTEN:
143
 
                                _count_overwrites++;
 
146
                                _countOverwrites++;
144
147
                                updateStats();
145
148
                                break;
146
149
                        case MESSAGE_CONTACTCREATED:
147
 
                                _count_creates++;
 
150
                                _countCreates++;
148
151
                                updateStats();
149
152
                                break;
150
153
                        case MESSAGE_CONTACTMERGED:
151
 
                                _count_merges++;
 
154
                                _countMerges++;
152
155
                                updateStats();
153
156
                                break;
154
157
                        case MESSAGE_CONTACTSKIPPED:
155
 
                                _count_skips++;
 
158
                                _countSkips++;
156
159
                                updateStats();
157
160
                                break;
158
161
                        default:
162
165
        }
163
166
 
164
167
        @Override
165
 
        protected void onCreate(Bundle saved_instance_state)
 
168
        protected void onCreate(Bundle savedInstanceState)
166
169
        {
167
170
                setContentView( R.layout.doit );
168
 
                super.onCreate( saved_instance_state );
 
171
                super.onCreate( savedInstanceState );
169
172
 
170
173
                // hide page 2
171
174
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
178
181
                        }
179
182
                } );
180
183
 
181
 
                _started_progress = false;
182
 
                _max_progress = 0;
183
 
                _tmp_progress = 0;
 
184
                _startedProgress = false;
 
185
                _maxProgress = 0;
 
186
                _tmpProgress = 0;
184
187
                _progress = 0;
185
188
                _handler = new DoitHandler();
186
189
 
187
 
                _count_overwrites = 0;
188
 
                _count_creates = 0;
189
 
                _count_merges = 0;
190
 
                _count_skips = 0;
 
190
                _countOverwrites = 0;
 
191
                _countCreates = 0;
 
192
                _countMerges = 0;
 
193
                _countSkips = 0;
191
194
 
192
195
                updateNext( NEXT_BEGIN );
193
196
 
201
204
                super.onPause();
202
205
 
203
206
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _next_action != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
207
                abortImport( true );
206
208
        }
207
209
 
208
210
        @Override
209
 
        protected Dialog onCreateDialog( int id )
 
211
        protected Dialog onCreateDialog(int id)
210
212
        {
211
213
                switch( id )
212
214
                {
213
215
                case DIALOG_ERROR:
214
216
                        return new AlertDialog.Builder( this )
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() {
220
 
                                                public void onClick( DialogInterface dialog,
221
 
                                                        int whichButton )
222
 
                                                {
 
217
                                        .setIcon( R.drawable.alert_dialog_icon )
 
218
                                        .setTitle( R.string.error_title )
 
219
                                        .setMessage( "" )
 
220
                                        .setPositiveButton( R.string.error_ok,
 
221
                                                        new DialogInterface.OnClickListener() {
 
222
                                                public void onClick(DialogInterface dialog,
 
223
                                                                int whichButton) {
223
224
                                                        Doit.this._importer.wake();
224
225
                                                }
225
226
                                        } )
226
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
227
 
                                .create();
 
227
                                        .setOnCancelListener( _dialogOnCancelListener )
 
228
                                        .create();
228
229
                case DIALOG_CONTINUEORABORT:
229
230
                        return new AlertDialog.Builder( this )
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 which_button )
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 which_button )
246
 
                                                {
247
 
                                                        Doit.this._importer.wake(
248
 
                                                                Importer.RESPONSE_NEGATIVE );
249
 
                                                }
250
 
                                        } )
251
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
252
 
                                .create();
 
231
                                        .setIcon( R.drawable.alert_dialog_icon )
 
232
                                        .setTitle( R.string.error_title )
 
233
                                        .setMessage( "" )
 
234
                                        .setPositiveButton( R.string.error_continue,
 
235
                                                        new DialogInterface.OnClickListener() {
 
236
                                                public void onClick(DialogInterface dialog,
 
237
                                                                int whichButton) {
 
238
                                                        Doit.this._importer.wake(
 
239
                                                                        Importer.RESPONSE_POSITIVE );
 
240
                                                }
 
241
                                        } )
 
242
                                        .setNegativeButton( R.string.error_abort,
 
243
                                                        new DialogInterface.OnClickListener() {
 
244
                                                public void onClick(DialogInterface dialog,
 
245
                                                                int whichButton) {
 
246
                                                        Doit.this._importer.wake(
 
247
                                                                        Importer.RESPONSE_NEGATIVE );
 
248
                                                }
 
249
                                        } )
 
250
                                        .setOnCancelListener( _dialogOnCancelListener )
 
251
                                        .create();
253
252
                case DIALOG_MERGEPROMPT:
254
253
                        // custom layout in an AlertDialog
255
254
                        LayoutInflater factory = LayoutInflater.from( this );
256
 
                        final View dialog_view = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
258
 
                        ( (CheckBox)dialog_view.findViewById( R.id.mergeprompt_always ) ).
259
 
                                setOnCheckedChangeListener(
260
 
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged(
262
 
                                                        CompoundButton button_view, boolean is_checked )
263
 
                                                {
264
 
                                                        Doit.this._merge_prompt_always_selected =
265
 
                                                                is_checked;
266
 
                                                }
267
 
                                        } );
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;
 
255
                        final View dialogView = factory.inflate(
 
256
                                        R.layout.mergeprompt, null );
 
257
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
 
258
                                        setOnCheckedChangeListener(
 
259
                                                        new CompoundButton.OnCheckedChangeListener() {
 
260
                                public void onCheckedChanged( CompoundButton buttonView,
 
261
                                                boolean isChecked ) {
 
262
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
263
                                }
 
264
                        } );
 
265
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
 
266
                                        setOnClickListener( _mergePromptButtonListener );
 
267
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
 
268
                                        setOnClickListener( _mergePromptButtonListener );
 
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
 
270
                                        setOnClickListener( _mergePromptButtonListener );
 
271
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
272
                                        setOnClickListener( _mergePromptButtonListener );
 
273
                        _mergePromptAlwaysSelected = false;
277
274
                        return new AlertDialog.Builder( this )
278
 
                                .setIcon( R.drawable.alert_dialog_icon )
279
 
                                .setTitle( R.string.mergeprompt_title )
280
 
                                .setView( dialog_view )
281
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
282
 
                                .create();
 
275
                                        .setIcon( R.drawable.alert_dialog_icon )
 
276
                                        .setTitle( R.string.mergeprompt_title )
 
277
                                        .setView( dialogView )
 
278
                                        .setOnCancelListener( _dialogOnCancelListener )
 
279
                                        .create();
283
280
                }
284
281
                return null;
285
282
        }
286
283
 
287
 
        private OnClickListener _merge_prompt_button_listener =
288
 
                new OnClickListener()
289
 
        {
 
284
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
290
285
                public void onClick( View view )
291
286
                {
292
287
                        // handle abort
293
288
                        if( view.getId() == R.id.abort )
294
289
                                manualAbort();
295
290
 
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;
 
291
                        // else, response
 
292
                        else {
 
293
                                int responseExtra = _mergePromptAlwaysSelected?
 
294
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
300
295
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
301
 
                                        response_extra );
 
296
                                                responseExtra );
302
297
                        }
303
298
 
304
299
                        // close dialog and free (don't keep a reference)
305
 
                        Doit.this._merge_prompt_dialog.dismiss();
306
 
                        Doit.this._merge_prompt_dialog = null;
 
300
                        Doit.this._mergePromptDialog.dismiss();
 
301
                        Doit.this._mergePromptDialog = null;
307
302
                }
308
303
        };
309
304
 
313
308
                Button next = (Button)findViewById( R.id.next );
314
309
                next.setEnabled( false );
315
310
 
316
 
                switch( _next_action )
 
311
                switch( _nextAction )
317
312
                {
318
313
                case NEXT_BEGIN:
319
314
                        importContacts();
320
315
                        break;
321
316
                case NEXT_CLOSE:
322
 
                        setResult( RESULT_OK );
 
317
                        setResult( RESULT_CANCELED );
323
318
                        finish();
324
319
                        break;
325
320
                }
327
322
 
328
323
        private void manualAbort()
329
324
        {
330
 
                manualAbort( false );
331
 
        }
332
 
 
333
 
        private void manualAbort( boolean show_toaster_popup )
334
 
        {
335
 
                abortImport( show_toaster_popup );
336
 
 
 
325
                abortImport( false );
337
326
                updateNext( NEXT_CLOSE );
338
327
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
339
328
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
340
 
                ( (TextView)findViewById( R.id.doit_aborted ) ).
341
 
                        setVisibility( View.VISIBLE );
342
 
                ( (TextView)findViewById( R.id.doit_alldone ) ).
343
 
                        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
329
        }
353
330
 
354
 
        private void updateNext( int next_action )
 
331
        private void updateNext( int nextAction )
355
332
        {
356
333
                Button next = (Button)findViewById( R.id.next );
357
 
                switch( next_action ) {
 
334
                switch( nextAction ) {
358
335
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
359
336
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
360
337
                }
361
338
                next.setEnabled( true );
362
 
                _next_action = next_action;
 
339
                _nextAction = nextAction;
363
340
        }
364
341
 
365
342
        public static int convertIdToAction( int id ) {
380
357
                }
381
358
        }
382
359
 
383
 
        private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
384
 
                new DialogInterface.OnCancelListener()
385
 
        {
 
360
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
 
361
                        new DialogInterface.OnCancelListener() {
386
362
                public void onCancel( DialogInterface dialog ) {
387
 
                        manualAbort();
 
363
                        setResult( RESULT_CANCELED );
 
364
                        finish();
388
365
                }
389
366
        };
390
367
 
391
 
 
392
368
        @Override
393
 
        protected void onActivityResult( int request_code, int result_code,
394
 
                Intent data )
 
369
        protected void onActivityResult( int requestCode, int resultCode,
 
370
                        Intent data )
395
371
        {
396
372
                // if we're cancelling, abort any import
397
 
                if( result_code == RESULT_CANCELED )
 
373
                if( resultCode == RESULT_CANCELED )
398
374
                        abortImport( true );
399
375
        }
400
376
 
401
377
        @Override
402
 
        protected void onPrepareDialog( int id, Dialog dialog )
 
378
        protected void onPrepareDialog(int id, Dialog dialog)
403
379
        {
404
 
                _current_dialog_id = id;
405
 
 
406
380
                switch( id )
407
381
                {
408
382
                case DIALOG_ERROR:      // fall through
409
383
                case DIALOG_CONTINUEORABORT:
410
384
                        // set dialog message
411
 
                        ( (AlertDialog)dialog ).setMessage( _dialog_message );
 
385
                        ( (AlertDialog)dialog ).setMessage( _dialogMessage );
412
386
                        break;
413
387
                case DIALOG_MERGEPROMPT:
414
388
                        // set contact's name
415
 
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) )
416
 
                                .setText( _dialog_message );
 
389
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
 
390
                                        _dialogMessage );
417
391
                        // and set up reference to dialog
418
 
                        _merge_prompt_dialog = dialog;
 
392
                        _mergePromptDialog = dialog;
419
393
                        break;
420
394
                }
421
395
 
441
415
        private void updateProgress()
442
416
        {
443
417
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
444
 
                TextView out_of = (TextView)findViewById( R.id.doit_outof );
 
418
                TextView outOf = (TextView)findViewById( R.id.doit_outof );
445
419
 
446
 
                if( _max_progress > 0 )
 
420
                if( _maxProgress > 0 )
447
421
                {
448
 
                        bar.setMax( _max_progress );
449
 
                        bar.setSecondaryProgress( _tmp_progress );
 
422
                        bar.setMax( _maxProgress );
 
423
                        bar.setSecondaryProgress( _tmpProgress );
450
424
 
451
 
                        if( _started_progress )
 
425
                        if( _startedProgress )
452
426
                        {
453
427
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
454
 
                                        (int)Math.round( 100 * _progress / _max_progress ) + "%" );
455
 
                                out_of.setText( _progress + "/" + _max_progress );
 
428
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
429
                                outOf.setText( _progress + "/" + _maxProgress );
456
430
                                bar.setProgress( _progress );
457
431
                        }
458
432
                }
461
435
        private void updateStats()
462
436
        {
463
437
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
464
 
                        "" + _count_overwrites );
 
438
                                "" + _countOverwrites );
465
439
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
466
 
                        "" + _count_creates );
 
440
                                "" + _countCreates );
467
441
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
468
 
                        "" + _count_merges );
 
442
                                "" + _countMerges );
469
443
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
470
 
                        "" + _count_skips );
 
444
                                "" + _countSkips );
471
445
        }
472
446
 
473
 
        private void abortImport( boolean show_toaster_popup )
 
447
        private void abortImport( boolean showToasterPopup )
474
448
        {
475
449
                if( _importer != null )
476
450
                {
487
461
                                }
488
462
 
489
463
                                // notify the user
490
 
                                if( show_toaster_popup )
 
464
                                if( showToasterPopup )
491
465
                                        Toast.makeText( this, R.string.doit_importaborted,
492
 
                                                Toast.LENGTH_LONG ).show();
 
466
                                                        Toast.LENGTH_LONG ).show();
493
467
                        }
494
468
                }
495
469