/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
        }
201
205
                super.onPause();
202
206
 
203
207
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _nextAction != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
208
                abortImport();
 
209
 
 
210
                // destroy some stuff
 
211
                _importer = null;
 
212
                _handler = null;
206
213
        }
207
214
 
208
215
        @Override
209
 
        protected Dialog onCreateDialog( int id )
 
216
        protected Dialog onCreateDialog(int id)
210
217
        {
211
218
                switch( id )
212
219
                {
213
220
                case DIALOG_ERROR:
214
221
                        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() {
 
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() {
220
227
                                                public void onClick(DialogInterface dialog,
221
 
                                                        int whichButton)
222
 
                                                {
 
228
                                                                int whichButton) {
223
229
                                                        Doit.this._importer.wake();
224
230
                                                }
225
231
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
227
 
                                .create();
 
232
                                        .setOnCancelListener( _dialogOnCancelListener )
 
233
                                        .create();
228
234
                case DIALOG_CONTINUEORABORT:
229
235
                        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 whichButton)
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 whichButton)
246
 
                                                {
247
 
                                                        Doit.this._importer.wake(
248
 
                                                                Importer.RESPONSE_NEGATIVE );
249
 
                                                }
250
 
                                        } )
251
 
                                .setOnCancelListener( _dialogOnCancelListener )
252
 
                                .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();
253
257
                case DIALOG_MERGEPROMPT:
254
258
                        // custom layout in an AlertDialog
255
259
                        LayoutInflater factory = LayoutInflater.from( this );
256
260
                        final View dialogView = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
 
261
                                        R.layout.mergeprompt, null );
258
262
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
259
 
                                setOnCheckedChangeListener(
260
 
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged( CompoundButton buttonView,
262
 
                                                        boolean isChecked )
263
 
                                                {
264
 
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
265
 
                                                }
266
 
                                        } );
 
263
                                        setOnCheckedChangeListener(
 
264
                                                        new CompoundButton.OnCheckedChangeListener() {
 
265
                                public void onCheckedChanged( CompoundButton buttonView,
 
266
                                                boolean isChecked ) {
 
267
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
268
                                }
 
269
                        } );
267
270
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
268
 
                                setOnClickListener( _mergePromptButtonListener );
 
271
                                        setOnClickListener( _mergePromptButtonListener );
269
272
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
270
 
                                setOnClickListener( _mergePromptButtonListener );
 
273
                                        setOnClickListener( _mergePromptButtonListener );
271
274
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
272
 
                                setOnClickListener( _mergePromptButtonListener );
273
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
275
                                        setOnClickListener( _mergePromptButtonListener );
275
276
                        _mergePromptAlwaysSelected = false;
276
277
                        return new AlertDialog.Builder( this )
277
 
                                .setIcon( R.drawable.alert_dialog_icon )
278
 
                                .setTitle( R.string.mergeprompt_title )
279
 
                                .setView( dialogView )
280
 
                                .setOnCancelListener( _dialogOnCancelListener )
281
 
                                .create();
 
278
                                        .setIcon( R.drawable.alert_dialog_icon )
 
279
                                        .setTitle( R.string.mergeprompt_title )
 
280
                                        .setView( dialogView )
 
281
                                        .setOnCancelListener( _dialogOnCancelListener )
 
282
                                        .create();
282
283
                }
283
284
                return null;
284
285
        }
285
286
 
286
 
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
287
 
                public void onClick( View view )
288
 
                {
289
 
                        // handle abort
290
 
                        if( view.getId() == R.id.abort )
291
 
                                manualAbort();
292
 
 
293
 
                        // else, response (just check we haven't aborted already!)
294
 
                        else if( Doit.this._importer != null ) {
295
 
                                int responseExtra = _mergePromptAlwaysSelected?
296
 
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
297
 
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
298
 
                                                responseExtra );
299
 
                        }
300
 
 
301
 
                        // 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;
302
291
                        Doit.this._mergePromptDialog.dismiss();
303
 
                        Doit.this._mergePromptDialog = null;
 
292
                        Doit.this._mergePromptDialog = null;    // don't keep a reference!
 
293
                        Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
294
                                        responseExtra );
304
295
                }
305
296
        };
306
297
 
307
 
        @Override
308
 
        protected void onNext()
309
 
        {
310
 
                Button next = (Button)findViewById( R.id.next );
311
 
                next.setEnabled( false );
312
 
 
313
 
                switch( _nextAction )
314
 
                {
315
 
                case NEXT_BEGIN:
316
 
                        importContacts();
317
 
                        break;
318
 
                case NEXT_CLOSE:
319
 
                        setResult( RESULT_OK );
320
 
                        finish();
321
 
                        break;
322
 
                }
323
 
        }
324
 
 
325
 
        private void manualAbort()
326
 
        {
327
 
                manualAbort( false );
328
 
        }
329
 
 
330
 
        private void manualAbort( boolean showToasterPopup )
331
 
        {
332
 
                abortImport( showToasterPopup );
333
 
 
334
 
                updateNext( NEXT_CLOSE );
335
 
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
336
 
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
337
 
                ( (TextView)findViewById( R.id.doit_aborted ) ).
338
 
                        setVisibility( View.VISIBLE );
339
 
                ( (TextView)findViewById( R.id.doit_alldone ) ).
340
 
                        setVisibility( View.GONE );
341
 
 
342
 
                // close any open dialogs
343
 
                try {
344
 
                        dismissDialog( _currentDialogId );
345
 
                }
346
 
                catch( Exception e ) {
347
 
                }
348
 
        }
349
 
 
350
 
        private void updateNext( int nextAction )
351
 
        {
352
 
                Button next = (Button)findViewById( R.id.next );
353
 
                switch( nextAction ) {
354
 
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
355
 
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
356
 
                }
357
 
                next.setEnabled( true );
358
 
                _nextAction = nextAction;
359
 
        }
360
 
 
361
298
        public static int convertIdToAction( int id ) {
362
299
                switch( id ) {
363
300
                case R.id.merge_keep:           return ACTION_KEEP;
379
316
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
380
317
                        new DialogInterface.OnCancelListener() {
381
318
                public void onCancel( DialogInterface dialog ) {
382
 
                        manualAbort();
 
319
                        setResult( RESULT_CANCELED );
 
320
                        finish();
383
321
                }
384
322
        };
385
323
 
386
 
 
387
324
        @Override
388
325
        protected void onActivityResult( int requestCode, int resultCode,
389
326
                        Intent data )
390
327
        {
391
328
                // if we're cancelling, abort any import
392
329
                if( resultCode == RESULT_CANCELED )
393
 
                        abortImport( true );
 
330
                        abortImport();
394
331
        }
395
332
 
396
333
        @Override
397
334
        protected void onPrepareDialog(int id, Dialog dialog)
398
335
        {
399
 
                _currentDialogId = id;
400
 
 
401
336
                switch( id )
402
337
                {
403
338
                case DIALOG_ERROR:      // fall through
465
400
                                "" + _countSkips );
466
401
        }
467
402
 
468
 
        private void abortImport( boolean showToasterPopup )
 
403
        private void abortImport()
469
404
        {
470
405
                if( _importer != null )
471
406
                {
482
417
                                }
483
418
 
484
419
                                // notify the user
485
 
                                if( showToasterPopup )
486
 
                                        Toast.makeText( this, R.string.doit_importaborted,
487
 
                                                        Toast.LENGTH_LONG ).show();
 
420
                        Toast.makeText( this, R.string.doit_importaborted,
 
421
                                        Toast.LENGTH_LONG ).show();
488
422
                        }
489
423
                }
490
 
 
491
 
                // destroy some stuff
492
 
                _importer = null;
493
 
                _handler = null;
494
424
        }
495
425
}