/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-10 16:14:11 UTC
  • Revision ID: edam@waxworlds.org-20090110161411-4d17l9hs9d6j277q
Initial import

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * Doit.java
3
 
 *
4
 
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
5
 
 *
6
 
 * This file is part of the Import Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
8
 
 * http://www.waxworlds.org/edam/software/android/import-contacts
9
 
 *
10
 
 * This program is free software: you can redistribute it and/or modify
11
 
 * it under the terms of the GNU General Public License as published by
12
 
 * the Free Software Foundation, either version 3 of the License, or
13
 
 * (at your option) any later version.
14
 
 *
15
 
 * This program is distributed in the hope that it will be useful,
16
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 
 * GNU General Public License for more details.
19
 
 *
20
 
 * You should have received a copy of the GNU General Public License
21
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 
 */
23
 
 
24
 
package org.waxworlds.edam.importcontacts;
 
1
package org.waxworlds.importcontacts;
25
2
 
26
3
import android.app.AlertDialog;
27
4
import android.app.Dialog;
36
13
import android.widget.Button;
37
14
import android.widget.CheckBox;
38
15
import android.widget.CompoundButton;
 
16
import android.widget.LinearLayout;
39
17
import android.widget.ProgressBar;
40
18
import android.widget.TextView;
41
 
import android.widget.Toast;
42
19
 
43
20
public class Doit extends WizardActivity
44
21
{
46
23
        private final static int DIALOG_CONTINUEORABORT = 1;
47
24
        private final static int DIALOG_MERGEPROMPT = 2;
48
25
 
49
 
        public final static int MESSAGE_ALLDONE = 0;
50
 
        public final static int MESSAGE_ABORT = 1;
51
 
        public final static int MESSAGE_ERROR = 3;
52
 
        public final static int MESSAGE_CONTINUEORABORT = 4;
53
 
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
54
 
        public final static int MESSAGE_SETMAXPROGRESS = 6;
55
 
        public final static int MESSAGE_SETTMPPROGRESS = 7;
56
 
        public final static int MESSAGE_SETPROGRESS = 8;
57
 
        public final static int MESSAGE_MERGEPROMPT = 9;
58
 
        public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
59
 
        public final static int MESSAGE_CONTACTCREATED = 11;
60
 
        public final static int MESSAGE_CONTACTMERGED = 12;
61
 
        public final static int MESSAGE_CONTACTSKIPPED = 13;
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
 
 
71
26
        private boolean _startedProgress;
72
27
        private int _maxProgress;
73
28
        private int _tmpProgress;
75
30
        protected String _dialogMessage;
76
31
        private Dialog _mergePromptDialog;
77
32
        private boolean _mergePromptAlwaysSelected;
78
 
        private int _nextAction;
79
 
        private int _currentDialogId;
80
33
 
81
34
        private int _countOverwrites;
82
35
        private int _countCreates;
83
36
        private int _countMerges;
84
37
        private int _countSkips;
85
38
 
86
 
        protected Importer _importer = null;
 
39
        protected Importer _importer;
87
40
 
88
41
        public Handler _handler;
89
42
 
93
46
                public void handleMessage( Message msg ) {
94
47
                        switch( msg.what )
95
48
                        {
96
 
                        case MESSAGE_ALLDONE:
97
 
                                ( (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 );
103
 
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
106
 
                                break;
107
 
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
49
                        case Importer.MESSAGE_FINISHED:
 
50
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
 
51
                                                setVisibility( View.VISIBLE );
 
52
                                break;
 
53
                        case Importer.MESSAGE_FINISHED_BACK:
 
54
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
55
                                break;
 
56
                        case Importer.MESSAGE_ERROR:
 
57
                                Doit.this._dialogMessage = (String)msg.obj;
109
58
                                showDialog( DIALOG_ERROR );
110
59
                                break;
111
 
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
60
                        case Importer.MESSAGE_CONTINUEORABORT:
 
61
                                Doit.this._dialogMessage = (String)msg.obj;
113
62
                                showDialog( DIALOG_CONTINUEORABORT );
114
63
                                break;
115
 
                        case MESSAGE_SETPROGRESSMESSAGE:
 
64
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
116
65
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
117
 
                                        setText( (String)msg.obj );
 
66
                                                setText( (String)msg.obj );
118
67
                                break;
119
 
                        case MESSAGE_SETMAXPROGRESS:
 
68
                        case Importer.MESSAGE_SETMAXPROGRESS:
120
69
                                if( _maxProgress > 0 ) {
121
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
70
                                        if( _tmpProgress == _maxProgress )
122
71
                                                _tmpProgress = (Integer)msg.obj;
123
 
                                        if( _progress == _maxProgress - 1 )
 
72
                                        if( _progress == _maxProgress )
124
73
                                                _progress = (Integer)msg.obj;
125
74
                                }
126
75
                                _maxProgress = (Integer)msg.obj;
127
76
                                updateProgress();
128
77
                                break;
129
 
                        case MESSAGE_SETTMPPROGRESS:
 
78
                        case Importer.MESSAGE_SETTMPPROGRESS:
130
79
                                _tmpProgress = (Integer)msg.obj;
131
80
                                updateProgress();
132
81
                                break;
133
 
                        case MESSAGE_SETPROGRESS:
 
82
                        case Importer.MESSAGE_SETPROGRESS:
134
83
                                _startedProgress = true;
135
84
                                _progress = (Integer)msg.obj;
136
85
                                updateProgress();
137
86
                                break;
138
 
                        case MESSAGE_MERGEPROMPT:
 
87
                        case Importer.MESSAGE_MERGEPROMPT:
139
88
                                _dialogMessage = (String)msg.obj;
140
89
                                showDialog( DIALOG_MERGEPROMPT );
141
90
                                break;
142
 
                        case MESSAGE_CONTACTOVERWRITTEN:
 
91
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
143
92
                                _countOverwrites++;
144
93
                                updateStats();
145
94
                                break;
146
 
                        case MESSAGE_CONTACTCREATED:
 
95
                        case Importer.MESSAGE_CONTACTCREATED:
147
96
                                _countCreates++;
148
97
                                updateStats();
149
98
                                break;
150
 
                        case MESSAGE_CONTACTMERGED:
 
99
                        case Importer.MESSAGE_CONTACTMERGED:
151
100
                                _countMerges++;
152
101
                                updateStats();
153
102
                                break;
154
 
                        case MESSAGE_CONTACTSKIPPED:
 
103
                        case Importer.MESSAGE_CONTACTSKIPPED:
155
104
                                _countSkips++;
156
105
                                updateStats();
157
106
                                break;
170
119
                // hide page 2
171
120
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
172
121
 
173
 
                // set up abort button
174
 
                Button begin = (Button)findViewById( R.id.abort );
 
122
                // set up begin button
 
123
                Button begin = (Button)findViewById( R.id.doit_begin );
175
124
                begin.setOnClickListener( new View.OnClickListener() {
176
125
                        public void onClick( View view ) {
177
 
                                manualAbort();
178
 
                        }
179
 
                } );
 
126
                                importContacts();
 
127
                        }
 
128
                } );
 
129
 
 
130
                // set up close button
 
131
                Button close = (Button)findViewById( R.id.doit_close );
 
132
                close.setOnClickListener( new View.OnClickListener() {
 
133
                        public void onClick( View view ) {
 
134
                                setResult( RESULT_CANCELED );
 
135
                                finish();
 
136
                        }
 
137
                } );
 
138
 
 
139
 
180
140
 
181
141
                _startedProgress = false;
182
142
                _maxProgress = 0;
189
149
                _countMerges = 0;
190
150
                _countSkips = 0;
191
151
 
192
 
                updateNext( NEXT_BEGIN );
193
 
 
194
152
                updateProgress();
195
153
                updateStats();
196
154
        }
200
158
        {
201
159
                super.onPause();
202
160
 
203
 
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _nextAction != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
161
                abortImport();
 
162
 
 
163
                // notify the user
 
164
 
 
165
 
206
166
        }
207
167
 
208
168
        @Override
209
 
        protected Dialog onCreateDialog( int id )
 
169
        protected Dialog onCreateDialog(int id)
210
170
        {
211
171
                switch( id )
212
172
                {
213
173
                case DIALOG_ERROR:
214
174
                        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() {
 
175
                                        .setIcon( R.drawable.alert_dialog_icon )
 
176
                                        .setTitle( R.string.error_title )
 
177
                                        .setMessage( "" )
 
178
                                        .setPositiveButton( R.string.error_ok,
 
179
                                                        new DialogInterface.OnClickListener() {
220
180
                                                public void onClick(DialogInterface dialog,
221
 
                                                        int whichButton)
222
 
                                                {
 
181
                                                                int whichButton) {
223
182
                                                        Doit.this._importer.wake();
224
183
                                                }
225
184
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
227
 
                                .create();
 
185
                                        .setOnCancelListener( _dialogOnCancelListener )
 
186
                                        .create();
228
187
                case DIALOG_CONTINUEORABORT:
229
188
                        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();
 
189
                                        .setIcon( R.drawable.alert_dialog_icon )
 
190
                                        .setTitle( R.string.error_title )
 
191
                                        .setMessage( "" )
 
192
                                        .setPositiveButton( R.string.error_continue,
 
193
                                                        new DialogInterface.OnClickListener() {
 
194
                                                public void onClick(DialogInterface dialog,
 
195
                                                                int whichButton) {
 
196
                                                        Doit.this._importer.wake(
 
197
                                                                        Importer.RESPONSE_POSITIVE );
 
198
                                                }
 
199
                                        } )
 
200
                                        .setNegativeButton( R.string.error_abort,
 
201
                                                        new DialogInterface.OnClickListener() {
 
202
                                                public void onClick(DialogInterface dialog,
 
203
                                                                int whichButton) {
 
204
                                                        Doit.this._importer.wake(
 
205
                                                                        Importer.RESPONSE_NEGATIVE );
 
206
                                                }
 
207
                                        } )
 
208
                                        .setOnCancelListener( _dialogOnCancelListener )
 
209
                                        .create();
253
210
                case DIALOG_MERGEPROMPT:
254
211
                        // custom layout in an AlertDialog
255
212
                        LayoutInflater factory = LayoutInflater.from( this );
256
213
                        final View dialogView = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
 
214
                                        R.layout.mergeprompt, null );
258
215
                        ( (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
 
                                        } );
 
216
                                        setOnCheckedChangeListener(
 
217
                                                        new CompoundButton.OnCheckedChangeListener() {
 
218
                                public void onCheckedChanged( CompoundButton buttonView,
 
219
                                                boolean isChecked ) {
 
220
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
221
                                }
 
222
                        } );
267
223
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
268
 
                                setOnClickListener( _mergePromptButtonListener );
 
224
                                        setOnClickListener( _mergePromptButtonListener );
269
225
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
270
 
                                setOnClickListener( _mergePromptButtonListener );
 
226
                                        setOnClickListener( _mergePromptButtonListener );
271
227
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
272
 
                                setOnClickListener( _mergePromptButtonListener );
273
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
228
                                        setOnClickListener( _mergePromptButtonListener );
275
229
                        _mergePromptAlwaysSelected = false;
276
230
                        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();
 
231
                                        .setIcon( R.drawable.alert_dialog_icon )
 
232
                                        .setTitle( R.string.mergeprompt_title )
 
233
                                        .setView( dialogView )
 
234
                                        .setOnCancelListener( _dialogOnCancelListener )
 
235
                                        .create();
282
236
                }
283
237
                return null;
284
238
        }
285
239
 
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?
 
240
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
241
                public void onClick( View view ) {
 
242
                        int responseExtra = _mergePromptAlwaysSelected?
296
243
                                        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
244
                        Doit.this._mergePromptDialog.dismiss();
303
 
                        Doit.this._mergePromptDialog = null;
 
245
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
246
                        Doit.this._importer.wake( view.getId(), responseExtra );
304
247
                }
305
248
        };
306
249
 
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
250
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
381
251
                        new DialogInterface.OnCancelListener() {
382
252
                public void onCancel( DialogInterface dialog ) {
383
 
                        manualAbort();
 
253
                        setResult( RESULT_CANCELED );
 
254
                        finish();
384
255
                }
385
256
        };
386
257
 
387
 
 
388
258
        @Override
389
259
        protected void onActivityResult( int requestCode, int resultCode,
390
260
                        Intent data )
391
261
        {
392
 
                // if we're cancelling, abort any import
393
262
                if( resultCode == RESULT_CANCELED )
394
 
                        abortImport( true );
 
263
                        abortImport();
395
264
        }
396
265
 
397
266
        @Override
398
267
        protected void onPrepareDialog(int id, Dialog dialog)
399
268
        {
400
 
                _currentDialogId = id;
401
 
 
402
269
                switch( id )
403
270
                {
404
271
                case DIALOG_ERROR:      // fall through
409
276
                case DIALOG_MERGEPROMPT:
410
277
                        // set contact's name
411
278
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
412
 
                                _dialogMessage );
 
279
                                        _dialogMessage );
413
280
                        // and set up reference to dialog
414
281
                        _mergePromptDialog = dialog;
415
282
                        break;
447
314
                        if( _startedProgress )
448
315
                        {
449
316
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
450
 
                                        (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
317
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
451
318
                                outOf.setText( _progress + "/" + _maxProgress );
452
319
                                bar.setProgress( _progress );
453
320
                        }
457
324
        private void updateStats()
458
325
        {
459
326
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
460
 
                        "" + _countOverwrites );
 
327
                                "" + _countOverwrites );
461
328
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
462
 
                        "" + _countCreates );
 
329
                                "" + _countCreates );
463
330
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
464
 
                        "" + _countMerges );
 
331
                                "" + _countMerges );
465
332
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
466
 
                        "" + _countSkips );
 
333
                                "" + _countSkips );
467
334
        }
468
335
 
469
 
        private void abortImport( boolean showToasterPopup )
 
336
        private void abortImport()
470
337
        {
471
338
                if( _importer != null )
472
339
                {
473
 
                        // try and flag worker thread - did we need to?
474
 
                        if( _importer.setAbort() )
475
 
                        {
476
 
                                // wait for worker thread to end
477
 
                                while( true ) {
478
 
                                        try {
479
 
                                                _importer.join();
480
 
                                                break;
481
 
                                        }
482
 
                                        catch( InterruptedException e ) {}
 
340
                        _importer.setAbort();
 
341
                        while( true ) {
 
342
                                try {
 
343
                                        _importer.join();
 
344
                                        break;
483
345
                                }
484
 
 
485
 
                                // notify the user
486
 
                                if( showToasterPopup )
487
 
                                        Toast.makeText( this, R.string.doit_importaborted,
488
 
                                                Toast.LENGTH_LONG ).show();
 
346
                                catch( InterruptedException e ) {}
489
347
                        }
490
348
                }
491
 
 
492
 
                // destroy some stuff
493
 
                _importer = null;
494
 
                _handler = null;
495
349
        }
496
350
}