/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-11 16:47:01 UTC
  • Revision ID: edam@waxworlds.org-20090111164701-tvnnv5axo6jd7ck0
- added GPL header comments to all files
- added GPLv3 to project as COPYING

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
 
26
65
        private boolean _startedProgress;
27
66
        private int _maxProgress;
28
67
        private int _tmpProgress;
36
75
        private int _countMerges;
37
76
        private int _countSkips;
38
77
 
39
 
        protected Importer _importer;
 
78
        protected Importer _importer = null;
40
79
 
41
80
        public Handler _handler;
42
81
 
46
85
                public void handleMessage( Message msg ) {
47
86
                        switch( msg.what )
48
87
                        {
49
 
                        case Importer.MESSAGE_FINISHED:
 
88
                        case MESSAGE_FINISHED_ALLDONE:
 
89
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
90
                                                setVisibility( View.VISIBLE );
 
91
                                // fall through
 
92
                        case MESSAGE_FINISHED:
50
93
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
51
94
                                                setVisibility( View.VISIBLE );
52
95
                                break;
53
 
                        case Importer.MESSAGE_FINISHED_BACK:
 
96
                        case MESSAGE_FINISHED_GOBACK:
54
97
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
55
98
                                break;
56
 
                        case Importer.MESSAGE_ERROR:
 
99
                        case MESSAGE_ERROR:
57
100
                                Doit.this._dialogMessage = (String)msg.obj;
58
101
                                showDialog( DIALOG_ERROR );
59
102
                                break;
60
 
                        case Importer.MESSAGE_CONTINUEORABORT:
 
103
                        case MESSAGE_CONTINUEORABORT:
61
104
                                Doit.this._dialogMessage = (String)msg.obj;
62
105
                                showDialog( DIALOG_CONTINUEORABORT );
63
106
                                break;
64
 
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
 
107
                        case MESSAGE_SETPROGRESSMESSAGE:
65
108
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
66
109
                                                setText( (String)msg.obj );
67
110
                                break;
68
 
                        case Importer.MESSAGE_SETMAXPROGRESS:
 
111
                        case MESSAGE_SETMAXPROGRESS:
69
112
                                if( _maxProgress > 0 ) {
70
113
                                        if( _tmpProgress == _maxProgress )
71
114
                                                _tmpProgress = (Integer)msg.obj;
75
118
                                _maxProgress = (Integer)msg.obj;
76
119
                                updateProgress();
77
120
                                break;
78
 
                        case Importer.MESSAGE_SETTMPPROGRESS:
 
121
                        case MESSAGE_SETTMPPROGRESS:
79
122
                                _tmpProgress = (Integer)msg.obj;
80
123
                                updateProgress();
81
124
                                break;
82
 
                        case Importer.MESSAGE_SETPROGRESS:
 
125
                        case MESSAGE_SETPROGRESS:
83
126
                                _startedProgress = true;
84
127
                                _progress = (Integer)msg.obj;
85
128
                                updateProgress();
86
129
                                break;
87
 
                        case Importer.MESSAGE_MERGEPROMPT:
 
130
                        case MESSAGE_MERGEPROMPT:
88
131
                                _dialogMessage = (String)msg.obj;
89
132
                                showDialog( DIALOG_MERGEPROMPT );
90
133
                                break;
91
 
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
 
134
                        case MESSAGE_CONTACTOVERWRITTEN:
92
135
                                _countOverwrites++;
93
136
                                updateStats();
94
137
                                break;
95
 
                        case Importer.MESSAGE_CONTACTCREATED:
 
138
                        case MESSAGE_CONTACTCREATED:
96
139
                                _countCreates++;
97
140
                                updateStats();
98
141
                                break;
99
 
                        case Importer.MESSAGE_CONTACTMERGED:
 
142
                        case MESSAGE_CONTACTMERGED:
100
143
                                _countMerges++;
101
144
                                updateStats();
102
145
                                break;
103
 
                        case Importer.MESSAGE_CONTACTSKIPPED:
 
146
                        case MESSAGE_CONTACTSKIPPED:
104
147
                                _countSkips++;
105
148
                                updateStats();
106
149
                                break;
136
179
                        }
137
180
                } );
138
181
 
139
 
 
140
 
 
141
182
                _startedProgress = false;
142
183
                _maxProgress = 0;
143
184
                _tmpProgress = 0;
158
199
        {
159
200
                super.onPause();
160
201
 
 
202
                // saving the state of an import sounds complicated! Lets just abort!
161
203
                abortImport();
162
204
 
163
 
                // notify the user
164
 
 
165
 
 
 
205
                // destroy some stuff
 
206
                _importer = null;
 
207
                _handler = null;
166
208
        }
167
209
 
168
210
        @Override
259
301
        protected void onActivityResult( int requestCode, int resultCode,
260
302
                        Intent data )
261
303
        {
 
304
                // if we're cancelling, abort any import
262
305
                if( resultCode == RESULT_CANCELED )
263
306
                        abortImport();
264
307
        }
337
380
        {
338
381
                if( _importer != null )
339
382
                {
340
 
                        _importer.setAbort();
341
 
                        while( true ) {
342
 
                                try {
343
 
                                        _importer.join();
344
 
                                        break;
 
383
                        // try and flag worker thread - did we need to?
 
384
                        if( _importer.setAbort() )
 
385
                        {
 
386
                                // wait for worker thread to end
 
387
                                while( true ) {
 
388
                                        try {
 
389
                                                _importer.join();
 
390
                                                break;
 
391
                                        }
 
392
                                        catch( InterruptedException e ) {}
345
393
                                }
346
 
                                catch( InterruptedException e ) {}
 
394
 
 
395
                                // notify the user
 
396
                        Toast.makeText( this, R.string.doit_importaborted,
 
397
                                        Toast.LENGTH_LONG ).show();
347
398
                        }
348
399
                }
349
400
        }