/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:35:26 UTC
  • Revision ID: edam@waxworlds.org-20090113063526-l9t1s9git4bav60a
- new contact's phone numebrs and email addresses are added to the caches after those contacts are updated to account for the situation where the same contact is imported again from another file (or the contact exists twice in the same file!?)

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;
60
62
        public final static int MESSAGE_CONTACTMERGED = 12;
61
63
        public final static int MESSAGE_CONTACTSKIPPED = 13;
62
64
 
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
 
 
71
65
        private boolean _startedProgress;
72
66
        private int _maxProgress;
73
67
        private int _tmpProgress;
75
69
        protected String _dialogMessage;
76
70
        private Dialog _mergePromptDialog;
77
71
        private boolean _mergePromptAlwaysSelected;
78
 
        private int _nextAction;
79
 
        private int _currentDialogId;
80
72
 
81
73
        private int _countOverwrites;
82
74
        private int _countCreates;
93
85
                public void handleMessage( Message msg ) {
94
86
                        switch( msg.what )
95
87
                        {
96
 
                        case MESSAGE_ALLDONE:
 
88
                        case MESSAGE_FINISHED_ALLDONE:
97
89
                                ( (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 );
 
90
                                                setVisibility( View.VISIBLE );
 
91
                                // fall through
 
92
                        case MESSAGE_FINISHED:
 
93
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
 
94
                                                setVisibility( View.VISIBLE );
103
95
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
 
96
                        case MESSAGE_FINISHED_GOBACK:
 
97
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
106
98
                                break;
107
99
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
100
                                Doit.this._dialogMessage = (String)msg.obj;
109
101
                                showDialog( DIALOG_ERROR );
110
102
                                break;
111
103
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
104
                                Doit.this._dialogMessage = (String)msg.obj;
113
105
                                showDialog( DIALOG_CONTINUEORABORT );
114
106
                                break;
115
107
                        case MESSAGE_SETPROGRESSMESSAGE:
116
108
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
117
 
                                        setText( (String)msg.obj );
 
109
                                                setText( (String)msg.obj );
118
110
                                break;
119
111
                        case MESSAGE_SETMAXPROGRESS:
120
112
                                if( _maxProgress > 0 ) {
121
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
113
                                        if( _tmpProgress == _maxProgress )
122
114
                                                _tmpProgress = (Integer)msg.obj;
123
 
                                        if( _progress == _maxProgress - 1 )
 
115
                                        if( _progress == _maxProgress )
124
116
                                                _progress = (Integer)msg.obj;
125
117
                                }
126
118
                                _maxProgress = (Integer)msg.obj;
170
162
                // hide page 2
171
163
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
172
164
 
173
 
                // set up abort button
174
 
                Button begin = (Button)findViewById( R.id.abort );
 
165
                // set up begin button
 
166
                Button begin = (Button)findViewById( R.id.doit_begin );
175
167
                begin.setOnClickListener( new View.OnClickListener() {
176
168
                        public void onClick( View view ) {
177
 
                                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();
178
179
                        }
179
180
                } );
180
181
 
189
190
                _countMerges = 0;
190
191
                _countSkips = 0;
191
192
 
192
 
                updateNext( NEXT_BEGIN );
193
 
 
194
193
                updateProgress();
195
194
                updateStats();
196
195
        }
201
200
                super.onPause();
202
201
 
203
202
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _nextAction != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
203
                abortImport();
 
204
 
 
205
                // destroy some stuff
 
206
                _importer = null;
 
207
                _handler = null;
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() {
 
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() {
220
222
                                                public void onClick(DialogInterface dialog,
221
 
                                                        int whichButton)
222
 
                                                {
 
223
                                                                int whichButton) {
223
224
                                                        Doit.this._importer.wake();
224
225
                                                }
225
226
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
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 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();
 
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
255
                        final View dialogView = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
 
256
                                        R.layout.mergeprompt, null );
258
257
                        ( (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
 
                                        } );
 
258
                                        setOnCheckedChangeListener(
 
259
                                                        new CompoundButton.OnCheckedChangeListener() {
 
260
                                public void onCheckedChanged( CompoundButton buttonView,
 
261
                                                boolean isChecked ) {
 
262
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
263
                                }
 
264
                        } );
267
265
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
268
 
                                setOnClickListener( _mergePromptButtonListener );
 
266
                                        setOnClickListener( _mergePromptButtonListener );
269
267
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
270
 
                                setOnClickListener( _mergePromptButtonListener );
 
268
                                        setOnClickListener( _mergePromptButtonListener );
271
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
272
 
                                setOnClickListener( _mergePromptButtonListener );
273
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
270
                                        setOnClickListener( _mergePromptButtonListener );
275
271
                        _mergePromptAlwaysSelected = false;
276
272
                        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();
 
273
                                        .setIcon( R.drawable.alert_dialog_icon )
 
274
                                        .setTitle( R.string.mergeprompt_title )
 
275
                                        .setView( dialogView )
 
276
                                        .setOnCancelListener( _dialogOnCancelListener )
 
277
                                        .create();
282
278
                }
283
279
                return null;
284
280
        }
285
281
 
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?
 
282
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
283
                public void onClick( View view ) {
 
284
                        int responseExtra = _mergePromptAlwaysSelected?
296
285
                                        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)
302
286
                        Doit.this._mergePromptDialog.dismiss();
303
 
                        Doit.this._mergePromptDialog = null;
 
287
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
288
                        Doit.this._importer.wake( view.getId(), responseExtra );
304
289
                }
305
290
        };
306
291
 
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
 
                        // ignore errors
348
 
                }
349
 
        }
350
 
 
351
 
        private void updateNext( int nextAction )
352
 
        {
353
 
                Button next = (Button)findViewById( R.id.next );
354
 
                switch( nextAction ) {
355
 
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
356
 
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
357
 
                }
358
 
                next.setEnabled( true );
359
 
                _nextAction = nextAction;
360
 
        }
361
 
 
362
 
        public static int convertIdToAction( int id ) {
363
 
                switch( id ) {
364
 
                case R.id.merge_keep:           return ACTION_KEEP;
365
 
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
366
 
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
367
 
                default: return ACTION_PROMPT;
368
 
                }
369
 
        }
370
 
 
371
 
        public static int convertActionToId( int action ) {
372
 
                switch( action ) {
373
 
                case ACTION_KEEP:               return R.id.merge_keep;
374
 
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
375
 
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
376
 
                default: return R.id.merge_prompt;
377
 
                }
378
 
        }
379
 
 
380
292
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
381
293
                        new DialogInterface.OnCancelListener() {
382
294
                public void onCancel( DialogInterface dialog ) {
383
 
                        manualAbort();
 
295
                        setResult( RESULT_CANCELED );
 
296
                        finish();
384
297
                }
385
298
        };
386
299
 
387
 
 
388
300
        @Override
389
301
        protected void onActivityResult( int requestCode, int resultCode,
390
302
                        Intent data )
391
303
        {
392
304
                // if we're cancelling, abort any import
393
305
                if( resultCode == RESULT_CANCELED )
394
 
                        abortImport( true );
 
306
                        abortImport();
395
307
        }
396
308
 
397
309
        @Override
398
310
        protected void onPrepareDialog(int id, Dialog dialog)
399
311
        {
400
 
                _currentDialogId = id;
401
 
 
402
312
                switch( id )
403
313
                {
404
314
                case DIALOG_ERROR:      // fall through
409
319
                case DIALOG_MERGEPROMPT:
410
320
                        // set contact's name
411
321
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
412
 
                                _dialogMessage );
 
322
                                        _dialogMessage );
413
323
                        // and set up reference to dialog
414
324
                        _mergePromptDialog = dialog;
415
325
                        break;
447
357
                        if( _startedProgress )
448
358
                        {
449
359
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
450
 
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
360
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
451
361
                                outOf.setText( _progress + "/" + _maxProgress );
452
362
                                bar.setProgress( _progress );
453
363
                        }
457
367
        private void updateStats()
458
368
        {
459
369
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
460
 
                        "" + _countOverwrites );
 
370
                                "" + _countOverwrites );
461
371
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
462
 
                        "" + _countCreates );
 
372
                                "" + _countCreates );
463
373
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
464
 
                        "" + _countMerges );
 
374
                                "" + _countMerges );
465
375
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
466
 
                        "" + _countSkips );
 
376
                                "" + _countSkips );
467
377
        }
468
378
 
469
 
        private void abortImport( boolean showToasterPopup )
 
379
        private void abortImport()
470
380
        {
471
381
                if( _importer != null )
472
382
                {
483
393
                                }
484
394
 
485
395
                                // notify the user
486
 
                                if( showToasterPopup )
487
 
                                        Toast.makeText( this, R.string.doit_importaborted,
488
 
                                                Toast.LENGTH_LONG ).show();
 
396
                        Toast.makeText( this, R.string.doit_importaborted,
 
397
                                        Toast.LENGTH_LONG ).show();
489
398
                        }
490
399
                }
491
 
 
492
 
                // destroy some stuff
493
 
                _importer = null;
494
 
                _handler = null;
495
400
        }
496
401
}