/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-04 14:44:48 UTC
  • Revision ID: edam@waxworlds.org-20100704144448-1m30v811opup20fs
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
- massively simplified the WizzardActivity class so it works propperly
- moved all code to org.waxworlds.edam
- added an "aborted" message when the importion is aborted
- simplified the 3 actions the worker thread can take when stopping (only 2 were actualy used) to "aborted" or "alldone"
- changed intro message to match website
- bugfix: don't blow up when the My Contacts group is missing

Show diffs side-by-side

added added

removed removed

21
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
22
 */
23
23
 
24
 
package org.waxworlds.importcontacts;
 
24
package org.waxworlds.edam.importcontacts;
25
25
 
26
26
import android.app.AlertDialog;
27
27
import android.app.Dialog;
36
36
import android.widget.Button;
37
37
import android.widget.CheckBox;
38
38
import android.widget.CompoundButton;
39
 
import android.widget.LinearLayout;
40
39
import android.widget.ProgressBar;
41
40
import android.widget.TextView;
42
41
import android.widget.Toast;
47
46
        private final static int DIALOG_CONTINUEORABORT = 1;
48
47
        private final static int DIALOG_MERGEPROMPT = 2;
49
48
 
50
 
        public final static int MESSAGE_FINISHED_ALLDONE = 0;
51
 
        public final static int MESSAGE_FINISHED = 1;
52
 
        public final static int MESSAGE_FINISHED_GOBACK = 2;
 
49
        public final static int MESSAGE_ALLDONE = 0;
 
50
        public final static int MESSAGE_ABORT = 1;
53
51
        public final static int MESSAGE_ERROR = 3;
54
52
        public final static int MESSAGE_CONTINUEORABORT = 4;
55
53
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
62
60
        public final static int MESSAGE_CONTACTMERGED = 12;
63
61
        public final static int MESSAGE_CONTACTSKIPPED = 13;
64
62
 
 
63
        public final static int ACTION_PROMPT = 0;
 
64
        public final static int ACTION_KEEP = 1;
 
65
        public final static int ACTION_MERGE_MERGE = 2;
 
66
        public final static int ACTION_OVERWRITE = 3;
 
67
 
 
68
        public final static int NEXT_BEGIN = 0;
 
69
        public final static int NEXT_CLOSE = 1;
 
70
 
65
71
        private boolean _startedProgress;
66
72
        private int _maxProgress;
67
73
        private int _tmpProgress;
69
75
        protected String _dialogMessage;
70
76
        private Dialog _mergePromptDialog;
71
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
72
80
 
73
81
        private int _countOverwrites;
74
82
        private int _countCreates;
85
93
                public void handleMessage( Message msg ) {
86
94
                        switch( msg.what )
87
95
                        {
88
 
                        case MESSAGE_FINISHED_ALLDONE:
 
96
                        case MESSAGE_ALLDONE:
89
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
90
 
                                                setVisibility( View.VISIBLE );
91
 
                                // fall through
92
 
                        case MESSAGE_FINISHED:
93
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
94
 
                                                setVisibility( View.VISIBLE );
 
98
                                        setVisibility( View.VISIBLE );
 
99
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
100
                                updateNext( NEXT_CLOSE );
 
101
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
102
                                                View.GONE );
95
103
                                break;
96
 
                        case MESSAGE_FINISHED_GOBACK:
97
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
98
106
                                break;
99
107
                        case MESSAGE_ERROR:
100
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
101
109
                                showDialog( DIALOG_ERROR );
102
110
                                break;
103
111
                        case MESSAGE_CONTINUEORABORT:
104
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
105
113
                                showDialog( DIALOG_CONTINUEORABORT );
106
114
                                break;
107
115
                        case MESSAGE_SETPROGRESSMESSAGE:
110
118
                                break;
111
119
                        case MESSAGE_SETMAXPROGRESS:
112
120
                                if( _maxProgress > 0 ) {
113
 
                                        if( _tmpProgress == _maxProgress )
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
114
122
                                                _tmpProgress = (Integer)msg.obj;
115
 
                                        if( _progress == _maxProgress )
 
123
                                        if( _progress == _maxProgress - 1 )
116
124
                                                _progress = (Integer)msg.obj;
117
125
                                }
118
126
                                _maxProgress = (Integer)msg.obj;
162
170
                // hide page 2
163
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
164
172
 
165
 
                // set up begin button
166
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
173
                // set up abort button
 
174
                Button begin = (Button)findViewById( R.id.abort );
167
175
                begin.setOnClickListener( new View.OnClickListener() {
168
176
                        public void onClick( View view ) {
169
 
                                importContacts();
170
 
                        }
171
 
                } );
172
 
 
173
 
                // set up close button
174
 
                Button close = (Button)findViewById( R.id.doit_close );
175
 
                close.setOnClickListener( new View.OnClickListener() {
176
 
                        public void onClick( View view ) {
177
 
                                setResult( RESULT_CANCELED );
178
 
                                finish();
 
177
                                manualAbort();
179
178
                        }
180
179
                } );
181
180
 
190
189
                _countMerges = 0;
191
190
                _countSkips = 0;
192
191
 
 
192
                updateNext( NEXT_BEGIN );
 
193
 
193
194
                updateProgress();
194
195
                updateStats();
195
196
        }
199
200
        {
200
201
                super.onPause();
201
202
 
 
203
                // close any open dialogs
 
204
                try {
 
205
                        dismissDialog( _currentDialogId );
 
206
                }
 
207
                catch( Exception e ) {
 
208
                }
 
209
 
202
210
                // saving the state of an import sounds complicated! Lets just abort!
203
 
                abortImport();
204
 
 
205
 
                // destroy some stuff
206
 
                _importer = null;
207
 
                _handler = null;
 
211
                manualAbort( true );
208
212
        }
209
213
 
210
214
        @Override
211
 
        protected Dialog onCreateDialog(int id)
 
215
        protected Dialog onCreateDialog( int id )
212
216
        {
213
217
                switch( id )
214
218
                {
215
219
                case DIALOG_ERROR:
216
220
                        return new AlertDialog.Builder( this )
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() {
 
221
                                .setIcon( R.drawable.alert_dialog_icon )
 
222
                                .setTitle( R.string.error_title )
 
223
                                .setMessage( "" )
 
224
                                .setPositiveButton( R.string.error_ok,
 
225
                                        new DialogInterface.OnClickListener() {
222
226
                                                public void onClick(DialogInterface dialog,
223
 
                                                                int whichButton) {
 
227
                                                        int whichButton)
 
228
                                                {
224
229
                                                        Doit.this._importer.wake();
225
230
                                                }
226
231
                                        } )
227
 
                                        .setOnCancelListener( _dialogOnCancelListener )
228
 
                                        .create();
 
232
                                .setOnCancelListener( _dialogOnCancelListener )
 
233
                                .create();
229
234
                case DIALOG_CONTINUEORABORT:
230
235
                        return new AlertDialog.Builder( this )
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();
 
236
                                .setIcon( R.drawable.alert_dialog_icon )
 
237
                                .setTitle( R.string.error_title )
 
238
                                .setMessage( "" )
 
239
                                .setPositiveButton( R.string.error_continue,
 
240
                                        new DialogInterface.OnClickListener() {
 
241
                                                public void onClick(DialogInterface dialog,
 
242
                                                        int whichButton)
 
243
                                                {
 
244
                                                        Doit.this._importer.wake(
 
245
                                                                Importer.RESPONSE_POSITIVE );
 
246
                                                }
 
247
                                        } )
 
248
                                .setNegativeButton( R.string.error_abort,
 
249
                                        new DialogInterface.OnClickListener() {
 
250
                                                public void onClick(DialogInterface dialog,
 
251
                                                        int whichButton)
 
252
                                                {
 
253
                                                        Doit.this._importer.wake(
 
254
                                                                Importer.RESPONSE_NEGATIVE );
 
255
                                                }
 
256
                                        } )
 
257
                                .setOnCancelListener( _dialogOnCancelListener )
 
258
                                .create();
252
259
                case DIALOG_MERGEPROMPT:
253
260
                        // custom layout in an AlertDialog
254
261
                        LayoutInflater factory = LayoutInflater.from( this );
255
262
                        final View dialogView = factory.inflate(
256
 
                                        R.layout.mergeprompt, null );
 
263
                                R.layout.mergeprompt, null );
257
264
                        ( (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
                                setOnCheckedChangeListener(
 
266
                                        new CompoundButton.OnCheckedChangeListener() {
 
267
                                                public void onCheckedChanged( CompoundButton buttonView,
 
268
                                                        boolean isChecked )
 
269
                                                {
 
270
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
271
                                                }
 
272
                                        } );
265
273
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
266
 
                                        setOnClickListener( _mergePromptButtonListener );
 
274
                                setOnClickListener( _mergePromptButtonListener );
267
275
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
268
 
                                        setOnClickListener( _mergePromptButtonListener );
 
276
                                setOnClickListener( _mergePromptButtonListener );
269
277
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
270
 
                                        setOnClickListener( _mergePromptButtonListener );
 
278
                                setOnClickListener( _mergePromptButtonListener );
 
279
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
280
                                setOnClickListener( _mergePromptButtonListener );
271
281
                        _mergePromptAlwaysSelected = false;
272
282
                        return new AlertDialog.Builder( this )
273
 
                                        .setIcon( R.drawable.alert_dialog_icon )
274
 
                                        .setTitle( R.string.mergeprompt_title )
275
 
                                        .setView( dialogView )
276
 
                                        .setOnCancelListener( _dialogOnCancelListener )
277
 
                                        .create();
 
283
                                .setIcon( R.drawable.alert_dialog_icon )
 
284
                                .setTitle( R.string.mergeprompt_title )
 
285
                                .setView( dialogView )
 
286
                                .setOnCancelListener( _dialogOnCancelListener )
 
287
                                .create();
278
288
                }
279
289
                return null;
280
290
        }
281
291
 
282
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
283
 
                public void onClick( View view ) {
284
 
                        int responseExtra = _mergePromptAlwaysSelected?
285
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
292
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
293
                public void onClick( View view )
 
294
                {
 
295
                        // handle abort
 
296
                        if( view.getId() == R.id.abort )
 
297
                                manualAbort();
 
298
 
 
299
                        // else, response
 
300
                        else {
 
301
                                int responseExtra = _mergePromptAlwaysSelected?
 
302
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
303
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
304
                                                responseExtra );
 
305
                        }
 
306
 
 
307
                        // close dialog and free (don't keep a reference)
286
308
                        Doit.this._mergePromptDialog.dismiss();
287
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
288
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
309
                        Doit.this._mergePromptDialog = null;
289
310
                }
290
311
        };
291
312
 
 
313
        @Override
 
314
        protected void onNext()
 
315
        {
 
316
                Button next = (Button)findViewById( R.id.next );
 
317
                next.setEnabled( false );
 
318
 
 
319
                switch( _nextAction )
 
320
                {
 
321
                case NEXT_BEGIN:
 
322
                        importContacts();
 
323
                        break;
 
324
                case NEXT_CLOSE:
 
325
                        setResult( RESULT_OK );
 
326
                        finish();
 
327
                        break;
 
328
                }
 
329
        }
 
330
 
 
331
        private void manualAbort()
 
332
        {
 
333
                manualAbort( false );
 
334
        }
 
335
 
 
336
        private void manualAbort( boolean showToasterPopup )
 
337
        {
 
338
                abortImport( showToasterPopup );
 
339
 
 
340
                updateNext( NEXT_CLOSE );
 
341
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
342
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
 
343
                ( (TextView)findViewById( R.id.doit_aborted ) ).
 
344
                        setVisibility( View.VISIBLE );
 
345
                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
346
                        setVisibility( View.GONE );
 
347
        }
 
348
 
 
349
        private void updateNext( int nextAction )
 
350
        {
 
351
                Button next = (Button)findViewById( R.id.next );
 
352
                switch( nextAction ) {
 
353
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
354
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
355
                }
 
356
                next.setEnabled( true );
 
357
                _nextAction = nextAction;
 
358
        }
 
359
 
 
360
        public static int convertIdToAction( int id ) {
 
361
                switch( id ) {
 
362
                case R.id.merge_keep:           return ACTION_KEEP;
 
363
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
364
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
365
                default: return ACTION_PROMPT;
 
366
                }
 
367
        }
 
368
 
 
369
        public static int convertActionToId( int action ) {
 
370
                switch( action ) {
 
371
                case ACTION_KEEP:               return R.id.merge_keep;
 
372
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
373
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
374
                default: return R.id.merge_prompt;
 
375
                }
 
376
        }
 
377
 
292
378
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
293
379
                        new DialogInterface.OnCancelListener() {
294
380
                public void onCancel( DialogInterface dialog ) {
303
389
        {
304
390
                // if we're cancelling, abort any import
305
391
                if( resultCode == RESULT_CANCELED )
306
 
                        abortImport();
 
392
                        abortImport( true );
307
393
        }
308
394
 
309
395
        @Override
310
396
        protected void onPrepareDialog(int id, Dialog dialog)
311
397
        {
 
398
                _currentDialogId = id;
 
399
 
312
400
                switch( id )
313
401
                {
314
402
                case DIALOG_ERROR:      // fall through
376
464
                                "" + _countSkips );
377
465
        }
378
466
 
379
 
        private void abortImport()
 
467
        private void abortImport( boolean showToasterPopup )
380
468
        {
381
469
                if( _importer != null )
382
470
                {
393
481
                                }
394
482
 
395
483
                                // notify the user
396
 
                        Toast.makeText( this, R.string.doit_importaborted,
397
 
                                        Toast.LENGTH_LONG ).show();
 
484
                                if( showToasterPopup )
 
485
                                        Toast.makeText( this, R.string.doit_importaborted,
 
486
                                                        Toast.LENGTH_LONG ).show();
398
487
                        }
399
488
                }
 
489
 
 
490
                // destroy some stuff
 
491
                _importer = null;
 
492
                _handler = null;
400
493
        }
401
494
}