1
package org.waxworlds.importcontacts;
3
import android.app.AlertDialog;
4
import android.app.Dialog;
5
import android.content.DialogInterface;
6
import android.content.Intent;
7
import android.os.Bundle;
8
import android.os.Handler;
9
import android.os.Message;
10
import android.view.LayoutInflater;
11
import android.view.View;
12
import android.view.View.OnClickListener;
13
import android.widget.Button;
14
import android.widget.CheckBox;
15
import android.widget.CompoundButton;
16
import android.widget.LinearLayout;
17
import android.widget.ProgressBar;
18
import android.widget.TextView;
20
public class Doit extends WizardActivity
22
private final static int DIALOG_ERROR = 0;
23
private final static int DIALOG_CONTINUEORABORT = 1;
24
private final static int DIALOG_MERGEPROMPT = 2;
26
private boolean _startedProgress;
27
private int _maxProgress;
28
private int _tmpProgress;
29
private int _progress;
30
protected String _dialogMessage;
31
private Dialog _mergePromptDialog;
32
private boolean _mergePromptAlwaysSelected;
34
private int _countOverwrites;
35
private int _countCreates;
36
private int _countMerges;
37
private int _countSkips;
39
protected Importer _importer;
41
public Handler _handler;
43
public class DoitHandler extends Handler
46
public void handleMessage( Message msg ) {
49
case Importer.MESSAGE_FINISHED:
50
( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
51
setVisibility( View.VISIBLE );
53
case Importer.MESSAGE_FINISHED_BACK:
54
( (Button)findViewById( R.id.back ) ).setEnabled( true );
56
case Importer.MESSAGE_ERROR:
57
Doit.this._dialogMessage = (String)msg.obj;
58
showDialog( DIALOG_ERROR );
60
case Importer.MESSAGE_CONTINUEORABORT:
61
Doit.this._dialogMessage = (String)msg.obj;
62
showDialog( DIALOG_CONTINUEORABORT );
64
case Importer.MESSAGE_SETPROGRESSMESSAGE:
65
( (TextView)findViewById( R.id.doit_percentage ) ).
66
setText( (String)msg.obj );
68
case Importer.MESSAGE_SETMAXPROGRESS:
69
if( _maxProgress > 0 ) {
70
if( _tmpProgress == _maxProgress )
71
_tmpProgress = (Integer)msg.obj;
72
if( _progress == _maxProgress )
73
_progress = (Integer)msg.obj;
75
_maxProgress = (Integer)msg.obj;
78
case Importer.MESSAGE_SETTMPPROGRESS:
79
_tmpProgress = (Integer)msg.obj;
82
case Importer.MESSAGE_SETPROGRESS:
83
_startedProgress = true;
84
_progress = (Integer)msg.obj;
87
case Importer.MESSAGE_MERGEPROMPT:
88
_dialogMessage = (String)msg.obj;
89
showDialog( DIALOG_MERGEPROMPT );
91
case Importer.MESSAGE_CONTACTOVERWRITTEN:
95
case Importer.MESSAGE_CONTACTCREATED:
99
case Importer.MESSAGE_CONTACTMERGED:
103
case Importer.MESSAGE_CONTACTSKIPPED:
108
super.handleMessage( msg );
114
protected void onCreate(Bundle savedInstanceState)
116
setContentView( R.layout.doit );
117
super.onCreate( savedInstanceState );
120
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
122
// set up begin button
123
Button begin = (Button)findViewById( R.id.doit_begin );
124
begin.setOnClickListener( new View.OnClickListener() {
125
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
_startedProgress = false;
145
_handler = new DoitHandler();
147
_countOverwrites = 0;
157
protected void onPause()
169
protected Dialog onCreateDialog(int id)
174
return new AlertDialog.Builder( this )
175
.setIcon( R.drawable.alert_dialog_icon )
176
.setTitle( R.string.error_title )
178
.setPositiveButton( R.string.error_ok,
179
new DialogInterface.OnClickListener() {
180
public void onClick(DialogInterface dialog,
182
Doit.this._importer.wake();
185
.setOnCancelListener( _dialogOnCancelListener )
187
case DIALOG_CONTINUEORABORT:
188
return new AlertDialog.Builder( this )
189
.setIcon( R.drawable.alert_dialog_icon )
190
.setTitle( R.string.error_title )
192
.setPositiveButton( R.string.error_continue,
193
new DialogInterface.OnClickListener() {
194
public void onClick(DialogInterface dialog,
196
Doit.this._importer.wake(
197
Importer.RESPONSE_POSITIVE );
200
.setNegativeButton( R.string.error_abort,
201
new DialogInterface.OnClickListener() {
202
public void onClick(DialogInterface dialog,
204
Doit.this._importer.wake(
205
Importer.RESPONSE_NEGATIVE );
208
.setOnCancelListener( _dialogOnCancelListener )
210
case DIALOG_MERGEPROMPT:
211
// custom layout in an AlertDialog
212
LayoutInflater factory = LayoutInflater.from( this );
213
final View dialogView = factory.inflate(
214
R.layout.mergeprompt, null );
215
( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
216
setOnCheckedChangeListener(
217
new CompoundButton.OnCheckedChangeListener() {
218
public void onCheckedChanged( CompoundButton buttonView,
219
boolean isChecked ) {
220
Doit.this._mergePromptAlwaysSelected = isChecked;
223
( (Button)dialogView.findViewById( R.id.merge_keep ) ).
224
setOnClickListener( _mergePromptButtonListener );
225
( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
226
setOnClickListener( _mergePromptButtonListener );
227
( (Button)dialogView.findViewById( R.id.merge_merge ) ).
228
setOnClickListener( _mergePromptButtonListener );
229
_mergePromptAlwaysSelected = false;
230
return new AlertDialog.Builder( this )
231
.setIcon( R.drawable.alert_dialog_icon )
232
.setTitle( R.string.mergeprompt_title )
233
.setView( dialogView )
234
.setOnCancelListener( _dialogOnCancelListener )
240
private OnClickListener _mergePromptButtonListener = new OnClickListener() {
241
public void onClick( View view ) {
242
int responseExtra = _mergePromptAlwaysSelected?
243
Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
244
Doit.this._mergePromptDialog.dismiss();
245
Doit.this._mergePromptDialog = null; // dont keep a reference!
246
Doit.this._importer.wake( view.getId(), responseExtra );
250
private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
new DialogInterface.OnCancelListener() {
252
public void onCancel( DialogInterface dialog ) {
253
setResult( RESULT_CANCELED );
259
protected void onActivityResult( int requestCode, int resultCode,
262
if( resultCode == RESULT_CANCELED )
267
protected void onPrepareDialog(int id, Dialog dialog)
271
case DIALOG_ERROR: // fall through
272
case DIALOG_CONTINUEORABORT:
273
// set dialog message
274
( (AlertDialog)dialog ).setMessage( _dialogMessage );
276
case DIALOG_MERGEPROMPT:
277
// set contact's name
278
( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
280
// and set up reference to dialog
281
_mergePromptDialog = dialog;
285
super.onPrepareDialog( id, dialog );
288
private void importContacts()
291
( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
292
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
294
// disable back button
295
( (Button)findViewById( R.id.back ) ).setEnabled( false );
298
_importer = new VCFImporter( this );
300
// start the service's thread
304
private void updateProgress()
306
ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
307
TextView outOf = (TextView)findViewById( R.id.doit_outof );
309
if( _maxProgress > 0 )
311
bar.setMax( _maxProgress );
312
bar.setSecondaryProgress( _tmpProgress );
314
if( _startedProgress )
316
( (TextView)findViewById( R.id.doit_percentage ) ).setText(
317
(int)Math.round( 100 * _progress / _maxProgress ) + "%" );
318
outOf.setText( _progress + "/" + _maxProgress );
319
bar.setProgress( _progress );
324
private void updateStats()
326
( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
327
"" + _countOverwrites );
328
( (TextView)findViewById( R.id.doit_creates ) ).setText(
329
"" + _countCreates );
330
( (TextView)findViewById( R.id.doit_merges ) ).setText(
332
( (TextView)findViewById( R.id.doit_skips ) ).setText(
336
private void abortImport()
338
if( _importer != null )
340
_importer.setAbort();
346
catch( InterruptedException e ) {}