/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-01-28 00:45:02 UTC
  • Revision ID: edam@waxworlds.org-20090128004502-erfklxlwmwhkx4i5
- added scroll view to all layouts
- added intro activity (with disclaimer)
- added action constants so that updates (and changed ids) won't affect saved data
- updated todo

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;
36
36
import android.widget.Button;
37
37
import android.widget.CheckBox;
38
38
import android.widget.CompoundButton;
 
39
import android.widget.LinearLayout;
39
40
import android.widget.ProgressBar;
40
41
import android.widget.TextView;
41
42
import android.widget.Toast;
46
47
        private final static int DIALOG_CONTINUEORABORT = 1;
47
48
        private final static int DIALOG_MERGEPROMPT = 2;
48
49
 
49
 
        public final static int MESSAGE_ALLDONE = 0;
50
 
        public final static int MESSAGE_ABORT = 1;
 
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;
51
53
        public final static int MESSAGE_ERROR = 3;
52
54
        public final static int MESSAGE_CONTINUEORABORT = 4;
53
55
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
65
67
        public final static int ACTION_MERGE_MERGE = 2;
66
68
        public final static int ACTION_OVERWRITE = 3;
67
69
 
68
 
        public final static int NEXT_BEGIN = 0;
69
 
        public final static int NEXT_CLOSE = 1;
70
 
 
71
70
        private boolean _startedProgress;
72
71
        private int _maxProgress;
73
72
        private int _tmpProgress;
75
74
        protected String _dialogMessage;
76
75
        private Dialog _mergePromptDialog;
77
76
        private boolean _mergePromptAlwaysSelected;
78
 
        private int _nextAction;
79
 
        private int _currentDialogId;
80
77
 
81
78
        private int _countOverwrites;
82
79
        private int _countCreates;
93
90
                public void handleMessage( Message msg ) {
94
91
                        switch( msg.what )
95
92
                        {
96
 
                        case MESSAGE_ALLDONE:
 
93
                        case MESSAGE_FINISHED_ALLDONE:
97
94
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
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
                                                setVisibility( View.VISIBLE );
 
96
                                // fall through
 
97
                        case MESSAGE_FINISHED:
 
98
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
 
99
                                                setVisibility( View.VISIBLE );
103
100
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
 
101
                        case MESSAGE_FINISHED_GOBACK:
 
102
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
106
103
                                break;
107
104
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
105
                                Doit.this._dialogMessage = (String)msg.obj;
109
106
                                showDialog( DIALOG_ERROR );
110
107
                                break;
111
108
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
109
                                Doit.this._dialogMessage = (String)msg.obj;
113
110
                                showDialog( DIALOG_CONTINUEORABORT );
114
111
                                break;
115
112
                        case MESSAGE_SETPROGRESSMESSAGE:
118
115
                                break;
119
116
                        case MESSAGE_SETMAXPROGRESS:
120
117
                                if( _maxProgress > 0 ) {
121
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
118
                                        if( _tmpProgress == _maxProgress )
122
119
                                                _tmpProgress = (Integer)msg.obj;
123
 
                                        if( _progress == _maxProgress - 1 )
 
120
                                        if( _progress == _maxProgress )
124
121
                                                _progress = (Integer)msg.obj;
125
122
                                }
126
123
                                _maxProgress = (Integer)msg.obj;
170
167
                // hide page 2
171
168
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
172
169
 
173
 
                // set up abort button
174
 
                Button begin = (Button)findViewById( R.id.abort );
 
170
                // set up begin button
 
171
                Button begin = (Button)findViewById( R.id.doit_begin );
175
172
                begin.setOnClickListener( new View.OnClickListener() {
176
173
                        public void onClick( View view ) {
177
 
                                manualAbort();
 
174
                                importContacts();
 
175
                        }
 
176
                } );
 
177
 
 
178
                // set up close button
 
179
                Button close = (Button)findViewById( R.id.doit_close );
 
180
                close.setOnClickListener( new View.OnClickListener() {
 
181
                        public void onClick( View view ) {
 
182
                                setResult( RESULT_CANCELED );
 
183
                                finish();
178
184
                        }
179
185
                } );
180
186
 
189
195
                _countMerges = 0;
190
196
                _countSkips = 0;
191
197
 
192
 
                updateNext( NEXT_BEGIN );
193
 
 
194
198
                updateProgress();
195
199
                updateStats();
196
200
        }
200
204
        {
201
205
                super.onPause();
202
206
 
203
 
                // close any open dialogs
204
 
                try {
205
 
                        dismissDialog( _currentDialogId );
206
 
                }
207
 
                catch( Exception e ) {
208
 
                }
209
 
 
210
207
                // saving the state of an import sounds complicated! Lets just abort!
211
 
                manualAbort( true );
 
208
                abortImport();
 
209
 
 
210
                // destroy some stuff
 
211
                _importer = null;
 
212
                _handler = null;
212
213
        }
213
214
 
214
215
        @Override
215
 
        protected Dialog onCreateDialog( int id )
 
216
        protected Dialog onCreateDialog(int id)
216
217
        {
217
218
                switch( id )
218
219
                {
219
220
                case DIALOG_ERROR:
220
221
                        return new AlertDialog.Builder( this )
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
                                        .setIcon( R.drawable.alert_dialog_icon )
 
223
                                        .setTitle( R.string.error_title )
 
224
                                        .setMessage( "" )
 
225
                                        .setPositiveButton( R.string.error_ok,
 
226
                                                        new DialogInterface.OnClickListener() {
226
227
                                                public void onClick(DialogInterface dialog,
227
 
                                                        int whichButton)
228
 
                                                {
 
228
                                                                int whichButton) {
229
229
                                                        Doit.this._importer.wake();
230
230
                                                }
231
231
                                        } )
232
 
                                .setOnCancelListener( _dialogOnCancelListener )
233
 
                                .create();
 
232
                                        .setOnCancelListener( _dialogOnCancelListener )
 
233
                                        .create();
234
234
                case DIALOG_CONTINUEORABORT:
235
235
                        return new AlertDialog.Builder( this )
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();
 
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
                                                        Doit.this._importer.wake(
 
244
                                                                        Importer.RESPONSE_POSITIVE );
 
245
                                                }
 
246
                                        } )
 
247
                                        .setNegativeButton( R.string.error_abort,
 
248
                                                        new DialogInterface.OnClickListener() {
 
249
                                                public void onClick(DialogInterface dialog,
 
250
                                                                int whichButton) {
 
251
                                                        Doit.this._importer.wake(
 
252
                                                                        Importer.RESPONSE_NEGATIVE );
 
253
                                                }
 
254
                                        } )
 
255
                                        .setOnCancelListener( _dialogOnCancelListener )
 
256
                                        .create();
259
257
                case DIALOG_MERGEPROMPT:
260
258
                        // custom layout in an AlertDialog
261
259
                        LayoutInflater factory = LayoutInflater.from( this );
262
260
                        final View dialogView = factory.inflate(
263
 
                                R.layout.mergeprompt, null );
 
261
                                        R.layout.mergeprompt, null );
264
262
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
265
 
                                setOnCheckedChangeListener(
266
 
                                        new CompoundButton.OnCheckedChangeListener() {
267
 
                                                public void onCheckedChanged( CompoundButton buttonView,
268
 
                                                        boolean isChecked )
269
 
                                                {
270
 
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
271
 
                                                }
272
 
                                        } );
 
263
                                        setOnCheckedChangeListener(
 
264
                                                        new CompoundButton.OnCheckedChangeListener() {
 
265
                                public void onCheckedChanged( CompoundButton buttonView,
 
266
                                                boolean isChecked ) {
 
267
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
268
                                }
 
269
                        } );
273
270
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
271
                                        setOnClickListener( _mergePromptButtonListener );
275
272
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
276
 
                                setOnClickListener( _mergePromptButtonListener );
 
273
                                        setOnClickListener( _mergePromptButtonListener );
277
274
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
278
 
                                setOnClickListener( _mergePromptButtonListener );
279
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
280
 
                                setOnClickListener( _mergePromptButtonListener );
 
275
                                        setOnClickListener( _mergePromptButtonListener );
281
276
                        _mergePromptAlwaysSelected = false;
282
277
                        return new AlertDialog.Builder( this )
283
 
                                .setIcon( R.drawable.alert_dialog_icon )
284
 
                                .setTitle( R.string.mergeprompt_title )
285
 
                                .setView( dialogView )
286
 
                                .setOnCancelListener( _dialogOnCancelListener )
287
 
                                .create();
 
278
                                        .setIcon( R.drawable.alert_dialog_icon )
 
279
                                        .setTitle( R.string.mergeprompt_title )
 
280
                                        .setView( dialogView )
 
281
                                        .setOnCancelListener( _dialogOnCancelListener )
 
282
                                        .create();
288
283
                }
289
284
                return null;
290
285
        }
291
286
 
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)
 
287
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
288
                public void onClick( View view ) {
 
289
                        int responseExtra = _mergePromptAlwaysSelected?
 
290
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
308
291
                        Doit.this._mergePromptDialog.dismiss();
309
 
                        Doit.this._mergePromptDialog = null;
 
292
                        Doit.this._mergePromptDialog = null;    // don't keep a reference!
 
293
                        Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
294
                                        responseExtra );
310
295
                }
311
296
        };
312
297
 
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
298
        public static int convertIdToAction( int id ) {
361
299
                switch( id ) {
362
300
                case R.id.merge_keep:           return ACTION_KEEP;
389
327
        {
390
328
                // if we're cancelling, abort any import
391
329
                if( resultCode == RESULT_CANCELED )
392
 
                        abortImport( true );
 
330
                        abortImport();
393
331
        }
394
332
 
395
333
        @Override
396
334
        protected void onPrepareDialog(int id, Dialog dialog)
397
335
        {
398
 
                _currentDialogId = id;
399
 
 
400
336
                switch( id )
401
337
                {
402
338
                case DIALOG_ERROR:      // fall through
464
400
                                "" + _countSkips );
465
401
        }
466
402
 
467
 
        private void abortImport( boolean showToasterPopup )
 
403
        private void abortImport()
468
404
        {
469
405
                if( _importer != null )
470
406
                {
481
417
                                }
482
418
 
483
419
                                // notify the user
484
 
                                if( showToasterPopup )
485
 
                                        Toast.makeText( this, R.string.doit_importaborted,
486
 
                                                        Toast.LENGTH_LONG ).show();
 
420
                        Toast.makeText( this, R.string.doit_importaborted,
 
421
                                        Toast.LENGTH_LONG ).show();
487
422
                        }
488
423
                }
489
 
 
490
 
                // destroy some stuff
491
 
                _importer = null;
492
 
                _handler = null;
493
424
        }
494
425
}