/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
1
package org.waxworlds.importcontacts;
25
2
 
26
3
import android.app.AlertDialog;
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_FINISHED_ALLDONE = 0;
50
 
        public final static int MESSAGE_FINISHED = 1;
51
 
        public final static int MESSAGE_FINISHED_GOBACK = 2;
52
 
        public final static int MESSAGE_ERROR = 3;
53
 
        public final static int MESSAGE_CONTINUEORABORT = 4;
54
 
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
55
 
        public final static int MESSAGE_SETMAXPROGRESS = 6;
56
 
        public final static int MESSAGE_SETTMPPROGRESS = 7;
57
 
        public final static int MESSAGE_SETPROGRESS = 8;
58
 
        public final static int MESSAGE_MERGEPROMPT = 9;
59
 
        public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
60
 
        public final static int MESSAGE_CONTACTCREATED = 11;
61
 
        public final static int MESSAGE_CONTACTMERGED = 12;
62
 
        public final static int MESSAGE_CONTACTSKIPPED = 13;
63
 
 
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
26
        private boolean _startedProgress;
73
27
        private int _maxProgress;
74
28
        private int _tmpProgress;
76
30
        protected String _dialogMessage;
77
31
        private Dialog _mergePromptDialog;
78
32
        private boolean _mergePromptAlwaysSelected;
79
 
        private int _nextAction;
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_FINISHED_ALLDONE:
97
 
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
49
                        case Importer.MESSAGE_FINISHED:
 
50
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
98
51
                                                setVisibility( View.VISIBLE );
99
 
                                // fall through
100
 
                        case MESSAGE_FINISHED:
101
 
                                updateNext( NEXT_CLOSE );
102
 
                                findViewById( R.id.doit_abort_disp ).setVisibility(
103
 
                                                View.GONE );
104
52
                                break;
105
 
                        case MESSAGE_FINISHED_GOBACK:
 
53
                        case Importer.MESSAGE_FINISHED_BACK:
106
54
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
107
 
                                findViewById( R.id.doit_abort_disp ).setVisibility(
108
 
                                                View.GONE );
109
55
                                break;
110
 
                        case MESSAGE_ERROR:
 
56
                        case Importer.MESSAGE_ERROR:
111
57
                                Doit.this._dialogMessage = (String)msg.obj;
112
58
                                showDialog( DIALOG_ERROR );
113
59
                                break;
114
 
                        case MESSAGE_CONTINUEORABORT:
 
60
                        case Importer.MESSAGE_CONTINUEORABORT:
115
61
                                Doit.this._dialogMessage = (String)msg.obj;
116
62
                                showDialog( DIALOG_CONTINUEORABORT );
117
63
                                break;
118
 
                        case MESSAGE_SETPROGRESSMESSAGE:
 
64
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
119
65
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
120
66
                                                setText( (String)msg.obj );
121
67
                                break;
122
 
                        case MESSAGE_SETMAXPROGRESS:
 
68
                        case Importer.MESSAGE_SETMAXPROGRESS:
123
69
                                if( _maxProgress > 0 ) {
124
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
70
                                        if( _tmpProgress == _maxProgress )
125
71
                                                _tmpProgress = (Integer)msg.obj;
126
 
                                        if( _progress == _maxProgress - 1 )
 
72
                                        if( _progress == _maxProgress )
127
73
                                                _progress = (Integer)msg.obj;
128
74
                                }
129
75
                                _maxProgress = (Integer)msg.obj;
130
76
                                updateProgress();
131
77
                                break;
132
 
                        case MESSAGE_SETTMPPROGRESS:
 
78
                        case Importer.MESSAGE_SETTMPPROGRESS:
133
79
                                _tmpProgress = (Integer)msg.obj;
134
80
                                updateProgress();
135
81
                                break;
136
 
                        case MESSAGE_SETPROGRESS:
 
82
                        case Importer.MESSAGE_SETPROGRESS:
137
83
                                _startedProgress = true;
138
84
                                _progress = (Integer)msg.obj;
139
85
                                updateProgress();
140
86
                                break;
141
 
                        case MESSAGE_MERGEPROMPT:
 
87
                        case Importer.MESSAGE_MERGEPROMPT:
142
88
                                _dialogMessage = (String)msg.obj;
143
89
                                showDialog( DIALOG_MERGEPROMPT );
144
90
                                break;
145
 
                        case MESSAGE_CONTACTOVERWRITTEN:
 
91
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
146
92
                                _countOverwrites++;
147
93
                                updateStats();
148
94
                                break;
149
 
                        case MESSAGE_CONTACTCREATED:
 
95
                        case Importer.MESSAGE_CONTACTCREATED:
150
96
                                _countCreates++;
151
97
                                updateStats();
152
98
                                break;
153
 
                        case MESSAGE_CONTACTMERGED:
 
99
                        case Importer.MESSAGE_CONTACTMERGED:
154
100
                                _countMerges++;
155
101
                                updateStats();
156
102
                                break;
157
 
                        case MESSAGE_CONTACTSKIPPED:
 
103
                        case Importer.MESSAGE_CONTACTSKIPPED:
158
104
                                _countSkips++;
159
105
                                updateStats();
160
106
                                break;
173
119
                // hide page 2
174
120
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
175
121
 
176
 
                // set up abort button
177
 
                Button begin = (Button)findViewById( R.id.abort );
 
122
                // set up begin button
 
123
                Button begin = (Button)findViewById( R.id.doit_begin );
178
124
                begin.setOnClickListener( new View.OnClickListener() {
179
125
                        public void onClick( View view ) {
180
 
                                manualAbort();
181
 
                        }
182
 
                } );
 
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
 
183
140
 
184
141
                _startedProgress = false;
185
142
                _maxProgress = 0;
192
149
                _countMerges = 0;
193
150
                _countSkips = 0;
194
151
 
195
 
                updateNext( NEXT_BEGIN );
196
 
 
197
152
                updateProgress();
198
153
                updateStats();
199
154
        }
203
158
        {
204
159
                super.onPause();
205
160
 
206
 
                // saving the state of an import sounds complicated! Lets just abort!
207
 
                abortImport( true );
 
161
                abortImport();
 
162
 
 
163
                // notify the user
 
164
 
 
165
 
208
166
        }
209
167
 
210
168
        @Override
268
226
                                        setOnClickListener( _mergePromptButtonListener );
269
227
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
270
228
                                        setOnClickListener( _mergePromptButtonListener );
271
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
272
 
                                        setOnClickListener( _mergePromptButtonListener );
273
229
                        _mergePromptAlwaysSelected = false;
274
230
                        return new AlertDialog.Builder( this )
275
231
                                        .setIcon( R.drawable.alert_dialog_icon )
281
237
                return null;
282
238
        }
283
239
 
284
 
        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)
 
240
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
241
                public void onClick( View view ) {
 
242
                        int responseExtra = _mergePromptAlwaysSelected?
 
243
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
300
244
                        Doit.this._mergePromptDialog.dismiss();
301
 
                        Doit.this._mergePromptDialog = null;
 
245
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
246
                        Doit.this._importer.wake( view.getId(), responseExtra );
302
247
                }
303
248
        };
304
249
 
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
250
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
361
251
                        new DialogInterface.OnCancelListener() {
362
252
                public void onCancel( DialogInterface dialog ) {
369
259
        protected void onActivityResult( int requestCode, int resultCode,
370
260
                        Intent data )
371
261
        {
372
 
                // if we're cancelling, abort any import
373
262
                if( resultCode == RESULT_CANCELED )
374
 
                        abortImport( true );
 
263
                        abortImport();
375
264
        }
376
265
 
377
266
        @Override
444
333
                                "" + _countSkips );
445
334
        }
446
335
 
447
 
        private void abortImport( boolean showToasterPopup )
 
336
        private void abortImport()
448
337
        {
449
338
                if( _importer != null )
450
339
                {
451
 
                        // try and flag worker thread - did we need to?
452
 
                        if( _importer.setAbort() )
453
 
                        {
454
 
                                // wait for worker thread to end
455
 
                                while( true ) {
456
 
                                        try {
457
 
                                                _importer.join();
458
 
                                                break;
459
 
                                        }
460
 
                                        catch( InterruptedException e ) {}
 
340
                        _importer.setAbort();
 
341
                        while( true ) {
 
342
                                try {
 
343
                                        _importer.join();
 
344
                                        break;
461
345
                                }
462
 
 
463
 
                                // notify the user
464
 
                                if( showToasterPopup )
465
 
                                        Toast.makeText( this, R.string.doit_importaborted,
466
 
                                                        Toast.LENGTH_LONG ).show();
 
346
                                catch( InterruptedException e ) {}
467
347
                        }
468
348
                }
469
 
 
470
 
                // destroy some stuff
471
 
                _importer = null;
472
 
                _handler = null;
473
349
        }
474
350
}