4
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
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
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.
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.
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/>.
1
24
package org.waxworlds.importcontacts;
3
26
import android.app.AlertDialog;
13
36
import android.widget.Button;
14
37
import android.widget.CheckBox;
15
38
import android.widget.CompoundButton;
16
import android.widget.LinearLayout;
17
39
import android.widget.ProgressBar;
18
40
import android.widget.TextView;
41
import android.widget.Toast;
20
43
public class Doit extends WizardActivity
23
46
private final static int DIALOG_CONTINUEORABORT = 1;
24
47
private final static int DIALOG_MERGEPROMPT = 2;
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;
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;
69
public final static int NEXT_BEGIN = 0;
70
public final static int NEXT_CLOSE = 1;
26
72
private boolean _startedProgress;
27
73
private int _maxProgress;
28
74
private int _tmpProgress;
30
76
protected String _dialogMessage;
31
77
private Dialog _mergePromptDialog;
32
78
private boolean _mergePromptAlwaysSelected;
79
private int _nextAction;
34
81
private int _countOverwrites;
35
82
private int _countCreates;
36
83
private int _countMerges;
37
84
private int _countSkips;
39
protected Importer _importer;
86
protected Importer _importer = null;
41
88
public Handler _handler;
46
93
public void handleMessage( Message msg ) {
49
case Importer.MESSAGE_FINISHED:
50
( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
96
case MESSAGE_FINISHED_ALLDONE:
97
( (TextView)findViewById( R.id.doit_alldone ) ).
51
98
setVisibility( View.VISIBLE );
100
case MESSAGE_FINISHED:
101
updateNext( NEXT_CLOSE );
102
findViewById( R.id.doit_abort_disp ).setVisibility(
53
case Importer.MESSAGE_FINISHED_BACK:
105
case MESSAGE_FINISHED_GOBACK:
54
106
( (Button)findViewById( R.id.back ) ).setEnabled( true );
107
findViewById( R.id.doit_abort_disp ).setVisibility(
56
case Importer.MESSAGE_ERROR:
57
111
Doit.this._dialogMessage = (String)msg.obj;
58
112
showDialog( DIALOG_ERROR );
60
case Importer.MESSAGE_CONTINUEORABORT:
114
case MESSAGE_CONTINUEORABORT:
61
115
Doit.this._dialogMessage = (String)msg.obj;
62
116
showDialog( DIALOG_CONTINUEORABORT );
64
case Importer.MESSAGE_SETPROGRESSMESSAGE:
118
case MESSAGE_SETPROGRESSMESSAGE:
65
119
( (TextView)findViewById( R.id.doit_percentage ) ).
66
120
setText( (String)msg.obj );
68
case Importer.MESSAGE_SETMAXPROGRESS:
122
case MESSAGE_SETMAXPROGRESS:
69
123
if( _maxProgress > 0 ) {
70
if( _tmpProgress == _maxProgress )
124
if( _tmpProgress == _maxProgress - 1 )
71
125
_tmpProgress = (Integer)msg.obj;
72
if( _progress == _maxProgress )
126
if( _progress == _maxProgress - 1 )
73
127
_progress = (Integer)msg.obj;
75
129
_maxProgress = (Integer)msg.obj;
78
case Importer.MESSAGE_SETTMPPROGRESS:
132
case MESSAGE_SETTMPPROGRESS:
79
133
_tmpProgress = (Integer)msg.obj;
82
case Importer.MESSAGE_SETPROGRESS:
136
case MESSAGE_SETPROGRESS:
83
137
_startedProgress = true;
84
138
_progress = (Integer)msg.obj;
87
case Importer.MESSAGE_MERGEPROMPT:
141
case MESSAGE_MERGEPROMPT:
88
142
_dialogMessage = (String)msg.obj;
89
143
showDialog( DIALOG_MERGEPROMPT );
91
case Importer.MESSAGE_CONTACTOVERWRITTEN:
145
case MESSAGE_CONTACTOVERWRITTEN:
92
146
_countOverwrites++;
95
case Importer.MESSAGE_CONTACTCREATED:
149
case MESSAGE_CONTACTCREATED:
99
case Importer.MESSAGE_CONTACTMERGED:
153
case MESSAGE_CONTACTMERGED:
103
case Importer.MESSAGE_CONTACTSKIPPED:
157
case MESSAGE_CONTACTSKIPPED:
120
174
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
122
// set up begin button
123
Button begin = (Button)findViewById( R.id.doit_begin );
176
// set up abort button
177
Button begin = (Button)findViewById( R.id.abort );
124
178
begin.setOnClickListener( new View.OnClickListener() {
125
179
public void onClick( View view ) {
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 );
141
184
_startedProgress = false;
142
185
_maxProgress = 0;
226
268
setOnClickListener( _mergePromptButtonListener );
227
269
( (Button)dialogView.findViewById( R.id.merge_merge ) ).
228
270
setOnClickListener( _mergePromptButtonListener );
271
( (Button)dialogView.findViewById( R.id.abort ) ).
272
setOnClickListener( _mergePromptButtonListener );
229
273
_mergePromptAlwaysSelected = false;
230
274
return new AlertDialog.Builder( this )
231
275
.setIcon( R.drawable.alert_dialog_icon )
240
284
private OnClickListener _mergePromptButtonListener = new OnClickListener() {
241
public void onClick( View view ) {
242
int responseExtra = _mergePromptAlwaysSelected?
243
Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
285
public void onClick( View view )
288
if( view.getId() == R.id.abort )
293
int responseExtra = _mergePromptAlwaysSelected?
294
Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
295
Doit.this._importer.wake( convertIdToAction( view.getId() ),
299
// close dialog and free (don't keep a reference)
244
300
Doit.this._mergePromptDialog.dismiss();
245
Doit.this._mergePromptDialog = null; // dont keep a reference!
246
Doit.this._importer.wake( view.getId(), responseExtra );
301
Doit.this._mergePromptDialog = null;
306
protected void onNext()
308
Button next = (Button)findViewById( R.id.next );
309
next.setEnabled( false );
311
switch( _nextAction )
317
setResult( RESULT_CANCELED );
323
private void manualAbort()
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 );
331
private void updateNext( int nextAction )
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;
338
next.setEnabled( true );
339
_nextAction = nextAction;
342
public static int convertIdToAction( int 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;
351
public static int convertActionToId( int 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;
250
360
private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
361
new DialogInterface.OnCancelListener() {
252
362
public void onCancel( DialogInterface dialog ) {
333
444
"" + _countSkips );
336
private void abortImport()
447
private void abortImport( boolean showToasterPopup )
338
449
if( _importer != null )
340
_importer.setAbort();
451
// try and flag worker thread - did we need to?
452
if( _importer.setAbort() )
454
// wait for worker thread to end
460
catch( InterruptedException e ) {}
346
catch( InterruptedException e ) {}
464
if( showToasterPopup )
465
Toast.makeText( this, R.string.doit_importaborted,
466
Toast.LENGTH_LONG ).show();
470
// destroy some stuff