bzr branch
http://bzr.ed.am/android/import-contacts
6
by edam
- added GPL header comments to all files |
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
by edam
Initial import |
24 |
package org.waxworlds.importcontacts; |
25 |
||
26 |
import android.app.AlertDialog; |
|
27 |
import android.app.Dialog; |
|
28 |
import android.content.DialogInterface; |
|
29 |
import android.content.Intent; |
|
30 |
import android.os.Bundle; |
|
31 |
import android.os.Handler; |
|
32 |
import android.os.Message; |
|
33 |
import android.view.LayoutInflater; |
|
34 |
import android.view.View; |
|
35 |
import android.view.View.OnClickListener; |
|
36 |
import android.widget.Button; |
|
37 |
import android.widget.CheckBox; |
|
38 |
import android.widget.CompoundButton; |
|
39 |
import android.widget.ProgressBar; |
|
40 |
import android.widget.TextView; |
|
2
by edam
- added toaster message about import abortion in onPause() |
41 |
import android.widget.Toast; |
1
by edam
Initial import |
42 |
|
43 |
public class Doit extends WizardActivity |
|
44 |
{ |
|
45 |
private final static int DIALOG_ERROR = 0; |
|
46 |
private final static int DIALOG_CONTINUEORABORT = 1; |
|
47 |
private final static int DIALOG_MERGEPROMPT = 2; |
|
48 |
||
3
by edam
- added "all done" message |
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 |
||
9
by edam
- added scroll view to all layouts |
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 |
||
10
by edam
- made behaviour of next button overridable |
69 |
public final static int NEXT_BEGIN = 0; |
70 |
public final static int NEXT_CLOSE = 1; |
|
71 |
||
1
by edam
Initial import |
72 |
private boolean _startedProgress; |
73 |
private int _maxProgress; |
|
74 |
private int _tmpProgress; |
|
75 |
private int _progress; |
|
76 |
protected String _dialogMessage; |
|
77 |
private Dialog _mergePromptDialog; |
|
78 |
private boolean _mergePromptAlwaysSelected; |
|
10
by edam
- made behaviour of next button overridable |
79 |
private int _nextAction; |
1
by edam
Initial import |
80 |
|
81 |
private int _countOverwrites; |
|
82 |
private int _countCreates; |
|
83 |
private int _countMerges; |
|
84 |
private int _countSkips; |
|
85 |
||
3
by edam
- added "all done" message |
86 |
protected Importer _importer = null; |
1
by edam
Initial import |
87 |
|
88 |
public Handler _handler; |
|
89 |
||
90 |
public class DoitHandler extends Handler |
|
91 |
{ |
|
92 |
@Override |
|
93 |
public void handleMessage( Message msg ) { |
|
94 |
switch( msg.what ) |
|
95 |
{ |
|
3
by edam
- added "all done" message |
96 |
case MESSAGE_FINISHED_ALLDONE: |
97 |
( (TextView)findViewById( R.id.doit_alldone ) ). |
|
98 |
setVisibility( View.VISIBLE ); |
|
99 |
// fall through |
|
100 |
case MESSAGE_FINISHED: |
|
10
by edam
- made behaviour of next button overridable |
101 |
updateNext( NEXT_CLOSE ); |
102 |
findViewById( R.id.doit_abort_disp ).setVisibility( |
|
103 |
View.GONE ); |
|
1
by edam
Initial import |
104 |
break; |
3
by edam
- added "all done" message |
105 |
case MESSAGE_FINISHED_GOBACK: |
1
by edam
Initial import |
106 |
( (Button)findViewById( R.id.back ) ).setEnabled( true ); |
10
by edam
- made behaviour of next button overridable |
107 |
findViewById( R.id.doit_abort_disp ).setVisibility( |
108 |
View.GONE ); |
|
1
by edam
Initial import |
109 |
break; |
3
by edam
- added "all done" message |
110 |
case MESSAGE_ERROR: |
1
by edam
Initial import |
111 |
Doit.this._dialogMessage = (String)msg.obj; |
112 |
showDialog( DIALOG_ERROR ); |
|
113 |
break; |
|
3
by edam
- added "all done" message |
114 |
case MESSAGE_CONTINUEORABORT: |
1
by edam
Initial import |
115 |
Doit.this._dialogMessage = (String)msg.obj; |
116 |
showDialog( DIALOG_CONTINUEORABORT ); |
|
117 |
break; |
|
3
by edam
- added "all done" message |
118 |
case MESSAGE_SETPROGRESSMESSAGE: |
1
by edam
Initial import |
119 |
( (TextView)findViewById( R.id.doit_percentage ) ). |
120 |
setText( (String)msg.obj ); |
|
121 |
break; |
|
3
by edam
- added "all done" message |
122 |
case MESSAGE_SETMAXPROGRESS: |
1
by edam
Initial import |
123 |
if( _maxProgress > 0 ) { |
10
by edam
- made behaviour of next button overridable |
124 |
if( _tmpProgress == _maxProgress - 1 ) |
1
by edam
Initial import |
125 |
_tmpProgress = (Integer)msg.obj; |
10
by edam
- made behaviour of next button overridable |
126 |
if( _progress == _maxProgress - 1 ) |
1
by edam
Initial import |
127 |
_progress = (Integer)msg.obj; |
128 |
} |
|
129 |
_maxProgress = (Integer)msg.obj; |
|
130 |
updateProgress(); |
|
131 |
break; |
|
3
by edam
- added "all done" message |
132 |
case MESSAGE_SETTMPPROGRESS: |
1
by edam
Initial import |
133 |
_tmpProgress = (Integer)msg.obj; |
134 |
updateProgress(); |
|
135 |
break; |
|
3
by edam
- added "all done" message |
136 |
case MESSAGE_SETPROGRESS: |
1
by edam
Initial import |
137 |
_startedProgress = true; |
138 |
_progress = (Integer)msg.obj; |
|
139 |
updateProgress(); |
|
140 |
break; |
|
3
by edam
- added "all done" message |
141 |
case MESSAGE_MERGEPROMPT: |
1
by edam
Initial import |
142 |
_dialogMessage = (String)msg.obj; |
143 |
showDialog( DIALOG_MERGEPROMPT ); |
|
144 |
break; |
|
3
by edam
- added "all done" message |
145 |
case MESSAGE_CONTACTOVERWRITTEN: |
1
by edam
Initial import |
146 |
_countOverwrites++; |
147 |
updateStats(); |
|
148 |
break; |
|
3
by edam
- added "all done" message |
149 |
case MESSAGE_CONTACTCREATED: |
1
by edam
Initial import |
150 |
_countCreates++; |
151 |
updateStats(); |
|
152 |
break; |
|
3
by edam
- added "all done" message |
153 |
case MESSAGE_CONTACTMERGED: |
1
by edam
Initial import |
154 |
_countMerges++; |
155 |
updateStats(); |
|
156 |
break; |
|
3
by edam
- added "all done" message |
157 |
case MESSAGE_CONTACTSKIPPED: |
1
by edam
Initial import |
158 |
_countSkips++; |
159 |
updateStats(); |
|
160 |
break; |
|
161 |
default: |
|
162 |
super.handleMessage( msg ); |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
||
167 |
@Override |
|
168 |
protected void onCreate(Bundle savedInstanceState) |
|
169 |
{ |
|
170 |
setContentView( R.layout.doit ); |
|
171 |
super.onCreate( savedInstanceState ); |
|
172 |
||
173 |
// hide page 2 |
|
174 |
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE ); |
|
175 |
||
10
by edam
- made behaviour of next button overridable |
176 |
// set up abort button |
177 |
Button begin = (Button)findViewById( R.id.abort ); |
|
1
by edam
Initial import |
178 |
begin.setOnClickListener( new View.OnClickListener() { |
179 |
public void onClick( View view ) { |
|
10
by edam
- made behaviour of next button overridable |
180 |
manualAbort(); |
1
by edam
Initial import |
181 |
} |
182 |
} ); |
|
183 |
||
184 |
_startedProgress = false; |
|
185 |
_maxProgress = 0; |
|
186 |
_tmpProgress = 0; |
|
187 |
_progress = 0; |
|
188 |
_handler = new DoitHandler(); |
|
189 |
||
190 |
_countOverwrites = 0; |
|
191 |
_countCreates = 0; |
|
192 |
_countMerges = 0; |
|
193 |
_countSkips = 0; |
|
194 |
||
10
by edam
- made behaviour of next button overridable |
195 |
updateNext( NEXT_BEGIN ); |
196 |
||
1
by edam
Initial import |
197 |
updateProgress(); |
198 |
updateStats(); |
|
199 |
} |
|
200 |
||
201 |
@Override |
|
202 |
protected void onPause() |
|
203 |
{ |
|
204 |
super.onPause(); |
|
205 |
||
2
by edam
- added toaster message about import abortion in onPause() |
206 |
// saving the state of an import sounds complicated! Lets just abort! |
10
by edam
- made behaviour of next button overridable |
207 |
abortImport( true ); |
1
by edam
Initial import |
208 |
} |
209 |
||
210 |
@Override |
|
211 |
protected Dialog onCreateDialog(int id) |
|
212 |
{ |
|
213 |
switch( id ) |
|
214 |
{ |
|
215 |
case DIALOG_ERROR: |
|
216 |
return new AlertDialog.Builder( this ) |
|
217 |
.setIcon( R.drawable.alert_dialog_icon ) |
|
218 |
.setTitle( R.string.error_title ) |
|
219 |
.setMessage( "" ) |
|
220 |
.setPositiveButton( R.string.error_ok, |
|
221 |
new DialogInterface.OnClickListener() { |
|
222 |
public void onClick(DialogInterface dialog, |
|
223 |
int whichButton) { |
|
224 |
Doit.this._importer.wake(); |
|
225 |
} |
|
226 |
} ) |
|
227 |
.setOnCancelListener( _dialogOnCancelListener ) |
|
228 |
.create(); |
|
229 |
case DIALOG_CONTINUEORABORT: |
|
230 |
return new AlertDialog.Builder( this ) |
|
231 |
.setIcon( R.drawable.alert_dialog_icon ) |
|
232 |
.setTitle( R.string.error_title ) |
|
233 |
.setMessage( "" ) |
|
234 |
.setPositiveButton( R.string.error_continue, |
|
235 |
new DialogInterface.OnClickListener() { |
|
236 |
public void onClick(DialogInterface dialog, |
|
237 |
int whichButton) { |
|
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 |
Doit.this._importer.wake( |
|
247 |
Importer.RESPONSE_NEGATIVE ); |
|
248 |
} |
|
249 |
} ) |
|
250 |
.setOnCancelListener( _dialogOnCancelListener ) |
|
251 |
.create(); |
|
252 |
case DIALOG_MERGEPROMPT: |
|
253 |
// custom layout in an AlertDialog |
|
254 |
LayoutInflater factory = LayoutInflater.from( this ); |
|
255 |
final View dialogView = factory.inflate( |
|
256 |
R.layout.mergeprompt, null ); |
|
257 |
( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ). |
|
258 |
setOnCheckedChangeListener( |
|
259 |
new CompoundButton.OnCheckedChangeListener() { |
|
260 |
public void onCheckedChanged( CompoundButton buttonView, |
|
261 |
boolean isChecked ) { |
|
262 |
Doit.this._mergePromptAlwaysSelected = isChecked; |
|
263 |
} |
|
264 |
} ); |
|
265 |
( (Button)dialogView.findViewById( R.id.merge_keep ) ). |
|
266 |
setOnClickListener( _mergePromptButtonListener ); |
|
267 |
( (Button)dialogView.findViewById( R.id.merge_overwrite ) ). |
|
268 |
setOnClickListener( _mergePromptButtonListener ); |
|
269 |
( (Button)dialogView.findViewById( R.id.merge_merge ) ). |
|
270 |
setOnClickListener( _mergePromptButtonListener ); |
|
10
by edam
- made behaviour of next button overridable |
271 |
( (Button)dialogView.findViewById( R.id.abort ) ). |
272 |
setOnClickListener( _mergePromptButtonListener ); |
|
1
by edam
Initial import |
273 |
_mergePromptAlwaysSelected = false; |
274 |
return new AlertDialog.Builder( this ) |
|
275 |
.setIcon( R.drawable.alert_dialog_icon ) |
|
276 |
.setTitle( R.string.mergeprompt_title ) |
|
277 |
.setView( dialogView ) |
|
278 |
.setOnCancelListener( _dialogOnCancelListener ) |
|
279 |
.create(); |
|
280 |
} |
|
281 |
return null; |
|
282 |
} |
|
283 |
||
13
by edam
- converted project to use Android 1.5 SDK |
284 |
private OnClickListener _mergePromptButtonListener = new OnClickListener() { |
10
by edam
- made behaviour of next button overridable |
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) |
|
1
by edam
Initial import |
300 |
Doit.this._mergePromptDialog.dismiss(); |
10
by edam
- made behaviour of next button overridable |
301 |
Doit.this._mergePromptDialog = null; |
1
by edam
Initial import |
302 |
} |
303 |
}; |
|
304 |
||
10
by edam
- made behaviour of next button overridable |
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 |
||
9
by edam
- added scroll view to all layouts |
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 |
||
1
by edam
Initial import |
360 |
private DialogInterface.OnCancelListener _dialogOnCancelListener = |
361 |
new DialogInterface.OnCancelListener() { |
|
362 |
public void onCancel( DialogInterface dialog ) { |
|
363 |
setResult( RESULT_CANCELED ); |
|
364 |
finish(); |
|
365 |
} |
|
366 |
}; |
|
367 |
||
368 |
@Override |
|
369 |
protected void onActivityResult( int requestCode, int resultCode, |
|
370 |
Intent data ) |
|
371 |
{ |
|
2
by edam
- added toaster message about import abortion in onPause() |
372 |
// if we're cancelling, abort any import |
1
by edam
Initial import |
373 |
if( resultCode == RESULT_CANCELED ) |
10
by edam
- made behaviour of next button overridable |
374 |
abortImport( true ); |
1
by edam
Initial import |
375 |
} |
376 |
||
377 |
@Override |
|
378 |
protected void onPrepareDialog(int id, Dialog dialog) |
|
379 |
{ |
|
380 |
switch( id ) |
|
381 |
{ |
|
382 |
case DIALOG_ERROR: // fall through |
|
383 |
case DIALOG_CONTINUEORABORT: |
|
384 |
// set dialog message |
|
385 |
( (AlertDialog)dialog ).setMessage( _dialogMessage ); |
|
386 |
break; |
|
387 |
case DIALOG_MERGEPROMPT: |
|
388 |
// set contact's name |
|
389 |
( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText( |
|
390 |
_dialogMessage ); |
|
391 |
// and set up reference to dialog |
|
392 |
_mergePromptDialog = dialog; |
|
393 |
break; |
|
394 |
} |
|
395 |
||
396 |
super.onPrepareDialog( id, dialog ); |
|
397 |
} |
|
398 |
||
399 |
private void importContacts() |
|
400 |
{ |
|
401 |
// switch interfaces |
|
402 |
( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE ); |
|
403 |
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE ); |
|
404 |
||
405 |
// disable back button |
|
406 |
( (Button)findViewById( R.id.back ) ).setEnabled( false ); |
|
407 |
||
408 |
// create importer |
|
409 |
_importer = new VCFImporter( this ); |
|
410 |
||
411 |
// start the service's thread |
|
412 |
_importer.start(); |
|
413 |
} |
|
414 |
||
415 |
private void updateProgress() |
|
416 |
{ |
|
417 |
ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress ); |
|
418 |
TextView outOf = (TextView)findViewById( R.id.doit_outof ); |
|
419 |
||
420 |
if( _maxProgress > 0 ) |
|
421 |
{ |
|
422 |
bar.setMax( _maxProgress ); |
|
423 |
bar.setSecondaryProgress( _tmpProgress ); |
|
424 |
||
425 |
if( _startedProgress ) |
|
426 |
{ |
|
427 |
( (TextView)findViewById( R.id.doit_percentage ) ).setText( |
|
428 |
(int)Math.round( 100 * _progress / _maxProgress ) + "%" ); |
|
429 |
outOf.setText( _progress + "/" + _maxProgress ); |
|
430 |
bar.setProgress( _progress ); |
|
431 |
} |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
private void updateStats() |
|
436 |
{ |
|
437 |
( (TextView)findViewById( R.id.doit_overwrites ) ).setText( |
|
438 |
"" + _countOverwrites ); |
|
439 |
( (TextView)findViewById( R.id.doit_creates ) ).setText( |
|
440 |
"" + _countCreates ); |
|
441 |
( (TextView)findViewById( R.id.doit_merges ) ).setText( |
|
442 |
"" + _countMerges ); |
|
443 |
( (TextView)findViewById( R.id.doit_skips ) ).setText( |
|
444 |
"" + _countSkips ); |
|
445 |
} |
|
446 |
||
10
by edam
- made behaviour of next button overridable |
447 |
private void abortImport( boolean showToasterPopup ) |
1
by edam
Initial import |
448 |
{ |
449 |
if( _importer != null ) |
|
450 |
{ |
|
3
by edam
- added "all done" message |
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 ) {} |
|
1
by edam
Initial import |
461 |
} |
3
by edam
- added "all done" message |
462 |
|
463 |
// notify the user |
|
10
by edam
- made behaviour of next button overridable |
464 |
if( showToasterPopup ) |
465 |
Toast.makeText( this, R.string.doit_importaborted, |
|
466 |
Toast.LENGTH_LONG ).show(); |
|
1
by edam
Initial import |
467 |
} |
468 |
} |
|
10
by edam
- made behaviour of next button overridable |
469 |
|
470 |
// destroy some stuff |
|
471 |
_importer = null; |
|
472 |
_handler = null; |
|
1
by edam
Initial import |
473 |
} |
474 |
} |