/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-13 06:37:52 UTC
  • Revision ID: edam@waxworlds.org-20090113063752-kjbyjqksaez1g7uv
updated todo list

Show diffs side-by-side

added added

removed removed

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;
61
62
        public final static int MESSAGE_CONTACTMERGED = 12;
62
63
        public final static int MESSAGE_CONTACTSKIPPED = 13;
63
64
 
64
 
        public final static int ACTION_PROMPT = 0;
65
 
        public final static int ACTION_KEEP = 1;
66
 
        public final static int ACTION_MERGE_MERGE = 2;
67
 
        public final static int ACTION_OVERWRITE = 3;
68
 
 
69
 
        public final static int NEXT_BEGIN = 0;
70
 
        public final static int NEXT_CLOSE = 1;
71
 
 
72
65
        private boolean _startedProgress;
73
66
        private int _maxProgress;
74
67
        private int _tmpProgress;
76
69
        protected String _dialogMessage;
77
70
        private Dialog _mergePromptDialog;
78
71
        private boolean _mergePromptAlwaysSelected;
79
 
        private int _nextAction;
80
72
 
81
73
        private int _countOverwrites;
82
74
        private int _countCreates;
98
90
                                                setVisibility( View.VISIBLE );
99
91
                                // fall through
100
92
                        case MESSAGE_FINISHED:
101
 
                                updateNext( NEXT_CLOSE );
102
 
                                findViewById( R.id.doit_abort_disp ).setVisibility(
103
 
                                                View.GONE );
 
93
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
 
94
                                                setVisibility( View.VISIBLE );
104
95
                                break;
105
96
                        case MESSAGE_FINISHED_GOBACK:
106
97
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
107
 
                                findViewById( R.id.doit_abort_disp ).setVisibility(
108
 
                                                View.GONE );
109
98
                                break;
110
99
                        case MESSAGE_ERROR:
111
100
                                Doit.this._dialogMessage = (String)msg.obj;
121
110
                                break;
122
111
                        case MESSAGE_SETMAXPROGRESS:
123
112
                                if( _maxProgress > 0 ) {
124
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
113
                                        if( _tmpProgress == _maxProgress )
125
114
                                                _tmpProgress = (Integer)msg.obj;
126
 
                                        if( _progress == _maxProgress - 1 )
 
115
                                        if( _progress == _maxProgress )
127
116
                                                _progress = (Integer)msg.obj;
128
117
                                }
129
118
                                _maxProgress = (Integer)msg.obj;
173
162
                // hide page 2
174
163
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
175
164
 
176
 
                // set up abort button
177
 
                Button begin = (Button)findViewById( R.id.abort );
 
165
                // set up begin button
 
166
                Button begin = (Button)findViewById( R.id.doit_begin );
178
167
                begin.setOnClickListener( new View.OnClickListener() {
179
168
                        public void onClick( View view ) {
180
 
                                manualAbort();
 
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();
181
179
                        }
182
180
                } );
183
181
 
192
190
                _countMerges = 0;
193
191
                _countSkips = 0;
194
192
 
195
 
                updateNext( NEXT_BEGIN );
196
 
 
197
193
                updateProgress();
198
194
                updateStats();
199
195
        }
204
200
                super.onPause();
205
201
 
206
202
                // saving the state of an import sounds complicated! Lets just abort!
207
 
                abortImport( true );
 
203
                abortImport();
 
204
 
 
205
                // destroy some stuff
 
206
                _importer = null;
 
207
                _handler = null;
208
208
        }
209
209
 
210
210
        @Override
268
268
                                        setOnClickListener( _mergePromptButtonListener );
269
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
270
270
                                        setOnClickListener( _mergePromptButtonListener );
271
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
272
 
                                        setOnClickListener( _mergePromptButtonListener );
273
271
                        _mergePromptAlwaysSelected = false;
274
272
                        return new AlertDialog.Builder( this )
275
273
                                        .setIcon( R.drawable.alert_dialog_icon )
282
280
        }
283
281
 
284
282
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
285
 
                public void onClick( View view )
286
 
                {
287
 
                        // handle abort
288
 
                        if( view.getId() == R.id.abort )
289
 
                                manualAbort();
290
 
 
291
 
                        // else, response
292
 
                        else {
293
 
                                int responseExtra = _mergePromptAlwaysSelected?
294
 
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
295
 
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
296
 
                                                responseExtra );
297
 
                        }
298
 
 
299
 
                        // close dialog and free (don't keep a reference)
 
283
                public void onClick( View view ) {
 
284
                        int responseExtra = _mergePromptAlwaysSelected?
 
285
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
300
286
                        Doit.this._mergePromptDialog.dismiss();
301
 
                        Doit.this._mergePromptDialog = null;
 
287
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
288
                        Doit.this._importer.wake( view.getId(), responseExtra );
302
289
                }
303
290
        };
304
291
 
305
 
        @Override
306
 
        protected void onNext()
307
 
        {
308
 
                Button next = (Button)findViewById( R.id.next );
309
 
                next.setEnabled( false );
310
 
 
311
 
                switch( _nextAction )
312
 
                {
313
 
                case NEXT_BEGIN:
314
 
                        importContacts();
315
 
                        break;
316
 
                case NEXT_CLOSE:
317
 
                        setResult( RESULT_CANCELED );
318
 
                        finish();
319
 
                        break;
320
 
                }
321
 
        }
322
 
 
323
 
        private void manualAbort()
324
 
        {
325
 
                abortImport( false );
326
 
                updateNext( NEXT_CLOSE );
327
 
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
328
 
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
329
 
        }
330
 
 
331
 
        private void updateNext( int nextAction )
332
 
        {
333
 
                Button next = (Button)findViewById( R.id.next );
334
 
                switch( nextAction ) {
335
 
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
336
 
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
337
 
                }
338
 
                next.setEnabled( true );
339
 
                _nextAction = nextAction;
340
 
        }
341
 
 
342
 
        public static int convertIdToAction( int id ) {
343
 
                switch( id ) {
344
 
                case R.id.merge_keep:           return ACTION_KEEP;
345
 
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
346
 
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
347
 
                default: return ACTION_PROMPT;
348
 
                }
349
 
        }
350
 
 
351
 
        public static int convertActionToId( int action ) {
352
 
                switch( action ) {
353
 
                case ACTION_KEEP:               return R.id.merge_keep;
354
 
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
355
 
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
356
 
                default: return R.id.merge_prompt;
357
 
                }
358
 
        }
359
 
 
360
292
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
361
293
                        new DialogInterface.OnCancelListener() {
362
294
                public void onCancel( DialogInterface dialog ) {
371
303
        {
372
304
                // if we're cancelling, abort any import
373
305
                if( resultCode == RESULT_CANCELED )
374
 
                        abortImport( true );
 
306
                        abortImport();
375
307
        }
376
308
 
377
309
        @Override
444
376
                                "" + _countSkips );
445
377
        }
446
378
 
447
 
        private void abortImport( boolean showToasterPopup )
 
379
        private void abortImport()
448
380
        {
449
381
                if( _importer != null )
450
382
                {
461
393
                                }
462
394
 
463
395
                                // notify the user
464
 
                                if( showToasterPopup )
465
 
                                        Toast.makeText( this, R.string.doit_importaborted,
466
 
                                                        Toast.LENGTH_LONG ).show();
 
396
                        Toast.makeText( this, R.string.doit_importaborted,
 
397
                                        Toast.LENGTH_LONG ).show();
467
398
                        }
468
399
                }
469
 
 
470
 
                // destroy some stuff
471
 
                _importer = null;
472
 
                _handler = null;
473
400
        }
474
401
}