/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-28 00:45:02 UTC
  • Revision ID: edam@waxworlds.org-20090128004502-erfklxlwmwhkx4i5
- added scroll view to all layouts
- added intro activity (with disclaimer)
- added action constants so that updates (and changed ids) won't affect saved data
- updated todo

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
 
1
24
package org.waxworlds.importcontacts;
2
25
 
3
26
import android.app.AlertDialog;
16
39
import android.widget.LinearLayout;
17
40
import android.widget.ProgressBar;
18
41
import android.widget.TextView;
 
42
import android.widget.Toast;
19
43
 
20
44
public class Doit extends WizardActivity
21
45
{
23
47
        private final static int DIALOG_CONTINUEORABORT = 1;
24
48
        private final static int DIALOG_MERGEPROMPT = 2;
25
49
 
 
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;
 
53
        public final static int MESSAGE_ERROR = 3;
 
54
        public final static int MESSAGE_CONTINUEORABORT = 4;
 
55
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
 
56
        public final static int MESSAGE_SETMAXPROGRESS = 6;
 
57
        public final static int MESSAGE_SETTMPPROGRESS = 7;
 
58
        public final static int MESSAGE_SETPROGRESS = 8;
 
59
        public final static int MESSAGE_MERGEPROMPT = 9;
 
60
        public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
 
61
        public final static int MESSAGE_CONTACTCREATED = 11;
 
62
        public final static int MESSAGE_CONTACTMERGED = 12;
 
63
        public final static int MESSAGE_CONTACTSKIPPED = 13;
 
64
 
 
65
        public final static int ACTION_PROMPT = 0;
 
66
        public final static int ACTION_KEEP = 1;
 
67
        public final static int ACTION_MERGE_MERGE = 2;
 
68
        public final static int ACTION_OVERWRITE = 3;
 
69
 
26
70
        private boolean _startedProgress;
27
71
        private int _maxProgress;
28
72
        private int _tmpProgress;
36
80
        private int _countMerges;
37
81
        private int _countSkips;
38
82
 
39
 
        protected Importer _importer;
 
83
        protected Importer _importer = null;
40
84
 
41
85
        public Handler _handler;
42
86
 
46
90
                public void handleMessage( Message msg ) {
47
91
                        switch( msg.what )
48
92
                        {
49
 
                        case Importer.MESSAGE_FINISHED:
 
93
                        case MESSAGE_FINISHED_ALLDONE:
 
94
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
95
                                                setVisibility( View.VISIBLE );
 
96
                                // fall through
 
97
                        case MESSAGE_FINISHED:
50
98
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
51
99
                                                setVisibility( View.VISIBLE );
52
100
                                break;
53
 
                        case Importer.MESSAGE_FINISHED_BACK:
 
101
                        case MESSAGE_FINISHED_GOBACK:
54
102
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
55
103
                                break;
56
 
                        case Importer.MESSAGE_ERROR:
 
104
                        case MESSAGE_ERROR:
57
105
                                Doit.this._dialogMessage = (String)msg.obj;
58
106
                                showDialog( DIALOG_ERROR );
59
107
                                break;
60
 
                        case Importer.MESSAGE_CONTINUEORABORT:
 
108
                        case MESSAGE_CONTINUEORABORT:
61
109
                                Doit.this._dialogMessage = (String)msg.obj;
62
110
                                showDialog( DIALOG_CONTINUEORABORT );
63
111
                                break;
64
 
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
 
112
                        case MESSAGE_SETPROGRESSMESSAGE:
65
113
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
66
114
                                                setText( (String)msg.obj );
67
115
                                break;
68
 
                        case Importer.MESSAGE_SETMAXPROGRESS:
 
116
                        case MESSAGE_SETMAXPROGRESS:
69
117
                                if( _maxProgress > 0 ) {
70
118
                                        if( _tmpProgress == _maxProgress )
71
119
                                                _tmpProgress = (Integer)msg.obj;
75
123
                                _maxProgress = (Integer)msg.obj;
76
124
                                updateProgress();
77
125
                                break;
78
 
                        case Importer.MESSAGE_SETTMPPROGRESS:
 
126
                        case MESSAGE_SETTMPPROGRESS:
79
127
                                _tmpProgress = (Integer)msg.obj;
80
128
                                updateProgress();
81
129
                                break;
82
 
                        case Importer.MESSAGE_SETPROGRESS:
 
130
                        case MESSAGE_SETPROGRESS:
83
131
                                _startedProgress = true;
84
132
                                _progress = (Integer)msg.obj;
85
133
                                updateProgress();
86
134
                                break;
87
 
                        case Importer.MESSAGE_MERGEPROMPT:
 
135
                        case MESSAGE_MERGEPROMPT:
88
136
                                _dialogMessage = (String)msg.obj;
89
137
                                showDialog( DIALOG_MERGEPROMPT );
90
138
                                break;
91
 
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
 
139
                        case MESSAGE_CONTACTOVERWRITTEN:
92
140
                                _countOverwrites++;
93
141
                                updateStats();
94
142
                                break;
95
 
                        case Importer.MESSAGE_CONTACTCREATED:
 
143
                        case MESSAGE_CONTACTCREATED:
96
144
                                _countCreates++;
97
145
                                updateStats();
98
146
                                break;
99
 
                        case Importer.MESSAGE_CONTACTMERGED:
 
147
                        case MESSAGE_CONTACTMERGED:
100
148
                                _countMerges++;
101
149
                                updateStats();
102
150
                                break;
103
 
                        case Importer.MESSAGE_CONTACTSKIPPED:
 
151
                        case MESSAGE_CONTACTSKIPPED:
104
152
                                _countSkips++;
105
153
                                updateStats();
106
154
                                break;
136
184
                        }
137
185
                } );
138
186
 
139
 
 
140
 
 
141
187
                _startedProgress = false;
142
188
                _maxProgress = 0;
143
189
                _tmpProgress = 0;
158
204
        {
159
205
                super.onPause();
160
206
 
 
207
                // saving the state of an import sounds complicated! Lets just abort!
161
208
                abortImport();
162
209
 
163
 
                // notify the user
164
 
 
165
 
 
 
210
                // destroy some stuff
 
211
                _importer = null;
 
212
                _handler = null;
166
213
        }
167
214
 
168
215
        @Override
242
289
                        int responseExtra = _mergePromptAlwaysSelected?
243
290
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
244
291
                        Doit.this._mergePromptDialog.dismiss();
245
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
246
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
292
                        Doit.this._mergePromptDialog = null;    // don't keep a reference!
 
293
                        Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
294
                                        responseExtra );
247
295
                }
248
296
        };
249
297
 
 
298
        public static int convertIdToAction( int id ) {
 
299
                switch( id ) {
 
300
                case R.id.merge_keep:           return ACTION_KEEP;
 
301
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
302
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
303
                default: return ACTION_PROMPT;
 
304
                }
 
305
        }
 
306
 
 
307
        public static int convertActionToId( int action ) {
 
308
                switch( action ) {
 
309
                case ACTION_KEEP:               return R.id.merge_keep;
 
310
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
311
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
312
                default: return R.id.merge_prompt;
 
313
                }
 
314
        }
 
315
 
250
316
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
317
                        new DialogInterface.OnCancelListener() {
252
318
                public void onCancel( DialogInterface dialog ) {
259
325
        protected void onActivityResult( int requestCode, int resultCode,
260
326
                        Intent data )
261
327
        {
 
328
                // if we're cancelling, abort any import
262
329
                if( resultCode == RESULT_CANCELED )
263
330
                        abortImport();
264
331
        }
337
404
        {
338
405
                if( _importer != null )
339
406
                {
340
 
                        _importer.setAbort();
341
 
                        while( true ) {
342
 
                                try {
343
 
                                        _importer.join();
344
 
                                        break;
 
407
                        // try and flag worker thread - did we need to?
 
408
                        if( _importer.setAbort() )
 
409
                        {
 
410
                                // wait for worker thread to end
 
411
                                while( true ) {
 
412
                                        try {
 
413
                                                _importer.join();
 
414
                                                break;
 
415
                                        }
 
416
                                        catch( InterruptedException e ) {}
345
417
                                }
346
 
                                catch( InterruptedException e ) {}
 
418
 
 
419
                                // notify the user
 
420
                        Toast.makeText( this, R.string.doit_importaborted,
 
421
                                        Toast.LENGTH_LONG ).show();
347
422
                        }
348
423
                }
349
424
        }