/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
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
 
                // close any open dialogs
204
 
                try {
205
 
                        dismissDialog( _currentDialogId );
206
 
                }
207
 
                catch( Exception e ) {
208
 
                }
209
 
 
210
 
                // saving the state of an import sounds complicated! Lets just abort!
211
 
                manualAbort( true );
 
161
                abortImport();
 
162
 
 
163
                // notify the user
 
164
 
 
165
 
212
166
        }
213
167
 
214
168
        @Override
215
 
        protected Dialog onCreateDialog( int id )
 
169
        protected Dialog onCreateDialog(int id)
216
170
        {
217
171
                switch( id )
218
172
                {
219
173
                case DIALOG_ERROR:
220
174
                        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() {
 
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() {
226
180
                                                public void onClick(DialogInterface dialog,
227
 
                                                        int whichButton)
228
 
                                                {
 
181
                                                                int whichButton) {
229
182
                                                        Doit.this._importer.wake();
230
183
                                                }
231
184
                                        } )
232
 
                                .setOnCancelListener( _dialogOnCancelListener )
233
 
                                .create();
 
185
                                        .setOnCancelListener( _dialogOnCancelListener )
 
186
                                        .create();
234
187
                case DIALOG_CONTINUEORABORT:
235
188
                        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();
 
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();
259
210
                case DIALOG_MERGEPROMPT:
260
211
                        // custom layout in an AlertDialog
261
212
                        LayoutInflater factory = LayoutInflater.from( this );
262
213
                        final View dialogView = factory.inflate(
263
 
                                R.layout.mergeprompt, null );
 
214
                                        R.layout.mergeprompt, null );
264
215
                        ( (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
 
                                        } );
 
216
                                        setOnCheckedChangeListener(
 
217
                                                        new CompoundButton.OnCheckedChangeListener() {
 
218
                                public void onCheckedChanged( CompoundButton buttonView,
 
219
                                                boolean isChecked ) {
 
220
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
221
                                }
 
222
                        } );
273
223
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
224
                                        setOnClickListener( _mergePromptButtonListener );
275
225
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
276
 
                                setOnClickListener( _mergePromptButtonListener );
 
226
                                        setOnClickListener( _mergePromptButtonListener );
277
227
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
278
 
                                setOnClickListener( _mergePromptButtonListener );
279
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
280
 
                                setOnClickListener( _mergePromptButtonListener );
 
228
                                        setOnClickListener( _mergePromptButtonListener );
281
229
                        _mergePromptAlwaysSelected = false;
282
230
                        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();
 
231
                                        .setIcon( R.drawable.alert_dialog_icon )
 
232
                                        .setTitle( R.string.mergeprompt_title )
 
233
                                        .setView( dialogView )
 
234
                                        .setOnCancelListener( _dialogOnCancelListener )
 
235
                                        .create();
288
236
                }
289
237
                return null;
290
238
        }
291
239
 
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)
 
240
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
241
                public void onClick( View view ) {
 
242
                        int responseExtra = _mergePromptAlwaysSelected?
 
243
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
308
244
                        Doit.this._mergePromptDialog.dismiss();
309
 
                        Doit.this._mergePromptDialog = null;
 
245
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
246
                        Doit.this._importer.wake( view.getId(), responseExtra );
310
247
                }
311
248
        };
312
249
 
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
 
        public static int convertIdToAction( int id ) {
361
 
                switch( id ) {
362
 
                case R.id.merge_keep:           return ACTION_KEEP;
363
 
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
364
 
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
365
 
                default: return ACTION_PROMPT;
366
 
                }
367
 
        }
368
 
 
369
 
        public static int convertActionToId( int action ) {
370
 
                switch( action ) {
371
 
                case ACTION_KEEP:               return R.id.merge_keep;
372
 
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
373
 
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
374
 
                default: return R.id.merge_prompt;
375
 
                }
376
 
        }
377
 
 
378
250
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
379
251
                        new DialogInterface.OnCancelListener() {
380
252
                public void onCancel( DialogInterface dialog ) {
387
259
        protected void onActivityResult( int requestCode, int resultCode,
388
260
                        Intent data )
389
261
        {
390
 
                // if we're cancelling, abort any import
391
262
                if( resultCode == RESULT_CANCELED )
392
 
                        abortImport( true );
 
263
                        abortImport();
393
264
        }
394
265
 
395
266
        @Override
396
267
        protected void onPrepareDialog(int id, Dialog dialog)
397
268
        {
398
 
                _currentDialogId = id;
399
 
 
400
269
                switch( id )
401
270
                {
402
271
                case DIALOG_ERROR:      // fall through
464
333
                                "" + _countSkips );
465
334
        }
466
335
 
467
 
        private void abortImport( boolean showToasterPopup )
 
336
        private void abortImport()
468
337
        {
469
338
                if( _importer != null )
470
339
                {
471
 
                        // try and flag worker thread - did we need to?
472
 
                        if( _importer.setAbort() )
473
 
                        {
474
 
                                // wait for worker thread to end
475
 
                                while( true ) {
476
 
                                        try {
477
 
                                                _importer.join();
478
 
                                                break;
479
 
                                        }
480
 
                                        catch( InterruptedException e ) {}
 
340
                        _importer.setAbort();
 
341
                        while( true ) {
 
342
                                try {
 
343
                                        _importer.join();
 
344
                                        break;
481
345
                                }
482
 
 
483
 
                                // notify the user
484
 
                                if( showToasterPopup )
485
 
                                        Toast.makeText( this, R.string.doit_importaborted,
486
 
                                                        Toast.LENGTH_LONG ).show();
 
346
                                catch( InterruptedException e ) {}
487
347
                        }
488
348
                }
489
 
 
490
 
                // destroy some stuff
491
 
                _importer = null;
492
 
                _handler = null;
493
349
        }
494
350
}