/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
1 by edam
Initial import
1
package org.waxworlds.importcontacts;
2
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;
2 by edam
- added toaster message about import abortion in onPause()
19
import android.widget.Toast;
1 by edam
Initial import
20
21
public class Doit extends WizardActivity
22
{
23
	private final static int DIALOG_ERROR = 0;
24
	private final static int DIALOG_CONTINUEORABORT = 1;
25
	private final static int DIALOG_MERGEPROMPT = 2;
26
27
	private boolean _startedProgress;
28
	private int _maxProgress;
29
	private int _tmpProgress;
30
	private int _progress;
31
	protected String _dialogMessage;
32
	private Dialog _mergePromptDialog;
33
	private boolean _mergePromptAlwaysSelected;
34
35
	private int _countOverwrites;
36
	private int _countCreates;
37
	private int _countMerges;
38
	private int _countSkips;
39
40
	protected Importer _importer;
41
42
	public Handler _handler;
43
44
	public class DoitHandler extends Handler
45
	{
46
		@Override
47
		public void handleMessage( Message msg ) {
48
			switch( msg.what )
49
			{
50
			case Importer.MESSAGE_FINISHED:
51
				( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
52
						setVisibility( View.VISIBLE );
53
				break;
54
			case Importer.MESSAGE_FINISHED_BACK:
55
				( (Button)findViewById( R.id.back ) ).setEnabled( true );
56
				break;
57
			case Importer.MESSAGE_ERROR:
58
				Doit.this._dialogMessage = (String)msg.obj;
59
				showDialog( DIALOG_ERROR );
60
				break;
61
			case Importer.MESSAGE_CONTINUEORABORT:
62
				Doit.this._dialogMessage = (String)msg.obj;
63
				showDialog( DIALOG_CONTINUEORABORT );
64
				break;
65
			case Importer.MESSAGE_SETPROGRESSMESSAGE:
66
				( (TextView)findViewById( R.id.doit_percentage ) ).
67
						setText( (String)msg.obj );
68
				break;
69
			case Importer.MESSAGE_SETMAXPROGRESS:
70
				if( _maxProgress > 0 ) {
71
					if( _tmpProgress == _maxProgress )
72
						_tmpProgress = (Integer)msg.obj;
73
					if( _progress == _maxProgress )
74
						_progress = (Integer)msg.obj;
75
				}
76
				_maxProgress = (Integer)msg.obj;
77
				updateProgress();
78
				break;
79
			case Importer.MESSAGE_SETTMPPROGRESS:
80
				_tmpProgress = (Integer)msg.obj;
81
				updateProgress();
82
				break;
83
			case Importer.MESSAGE_SETPROGRESS:
84
				_startedProgress = true;
85
				_progress = (Integer)msg.obj;
86
				updateProgress();
87
				break;
88
			case Importer.MESSAGE_MERGEPROMPT:
89
				_dialogMessage = (String)msg.obj;
90
				showDialog( DIALOG_MERGEPROMPT );
91
				break;
92
			case Importer.MESSAGE_CONTACTOVERWRITTEN:
93
				_countOverwrites++;
94
				updateStats();
95
				break;
96
			case Importer.MESSAGE_CONTACTCREATED:
97
				_countCreates++;
98
				updateStats();
99
				break;
100
			case Importer.MESSAGE_CONTACTMERGED:
101
				_countMerges++;
102
				updateStats();
103
				break;
104
			case Importer.MESSAGE_CONTACTSKIPPED:
105
				_countSkips++;
106
				updateStats();
107
				break;
108
			default:
109
				super.handleMessage( msg );
110
			}
111
		}
112
	}
113
114
	@Override
115
	protected void onCreate(Bundle savedInstanceState)
116
	{
117
		setContentView( R.layout.doit );
118
		super.onCreate( savedInstanceState );
119
120
		// hide page 2
121
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
122
123
		// set up begin button
124
		Button begin = (Button)findViewById( R.id.doit_begin );
125
		begin.setOnClickListener( new View.OnClickListener() {
126
			public void onClick( View view ) {
127
				importContacts();
128
			}
129
		} );
130
131
		// set up close button
132
		Button close = (Button)findViewById( R.id.doit_close );
133
		close.setOnClickListener( new View.OnClickListener() {
134
			public void onClick( View view ) {
135
				setResult( RESULT_CANCELED );
136
				finish();
137
			}
138
		} );
139
140
		_startedProgress = false;
141
		_maxProgress = 0;
142
		_tmpProgress = 0;
143
		_progress = 0;
144
		_handler = new DoitHandler();
145
146
		_countOverwrites = 0;
147
		_countCreates = 0;
148
		_countMerges = 0;
149
		_countSkips = 0;
150
151
		updateProgress();
152
		updateStats();
153
	}
154
155
	@Override
156
	protected void onPause()
157
	{
158
		super.onPause();
159
2 by edam
- added toaster message about import abortion in onPause()
160
		// saving the state of an import sounds complicated! Lets just abort!
1 by edam
Initial import
161
		abortImport();
162
163
		// notify the user
2 by edam
- added toaster message about import abortion in onPause()
164
        Toast.makeText( this, R.string.doit_importaborted,
165
        		Toast.LENGTH_LONG ).show();
1 by edam
Initial import
166
	}
167
168
	@Override
169
	protected Dialog onCreateDialog(int id)
170
	{
171
		switch( id )
172
		{
173
		case DIALOG_ERROR:
174
			return new AlertDialog.Builder( this )
175
					.setIcon( R.drawable.alert_dialog_icon )
176
					.setTitle( R.string.error_title )
177
					.setMessage( "" )
178
					.setPositiveButton( R.string.error_ok,
179
							new DialogInterface.OnClickListener() {
180
						public void onClick(DialogInterface dialog,
181
								int whichButton) {
182
							Doit.this._importer.wake();
183
						}
184
					} )
185
					.setOnCancelListener( _dialogOnCancelListener )
186
					.create();
187
		case DIALOG_CONTINUEORABORT:
188
			return new AlertDialog.Builder( this )
189
					.setIcon( R.drawable.alert_dialog_icon )
190
					.setTitle( R.string.error_title )
191
					.setMessage( "" )
192
					.setPositiveButton( R.string.error_continue,
193
							new DialogInterface.OnClickListener() {
194
						public void onClick(DialogInterface dialog,
195
								int whichButton) {
196
							Doit.this._importer.wake(
197
									Importer.RESPONSE_POSITIVE );
198
						}
199
					} )
200
					.setNegativeButton( R.string.error_abort,
201
							new DialogInterface.OnClickListener() {
202
						public void onClick(DialogInterface dialog,
203
								int whichButton) {
204
							Doit.this._importer.wake(
205
									Importer.RESPONSE_NEGATIVE );
206
						}
207
					} )
208
					.setOnCancelListener( _dialogOnCancelListener )
209
					.create();
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;
221
				}
222
			} );
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 )
235
					.create();
236
		}
237
		return null;
238
	}
239
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 );
247
		}
248
	};
249
250
	private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
			new DialogInterface.OnCancelListener() {
252
		public void onCancel( DialogInterface dialog ) {
253
			setResult( RESULT_CANCELED );
254
			finish();
255
		}
256
	};
257
258
	@Override
259
	protected void onActivityResult( int requestCode, int resultCode,
260
			Intent data )
261
	{
2 by edam
- added toaster message about import abortion in onPause()
262
		// if we're cancelling, abort any import
1 by edam
Initial import
263
		if( resultCode == RESULT_CANCELED )
264
			abortImport();
265
	}
266
267
	@Override
268
	protected void onPrepareDialog(int id, Dialog dialog)
269
	{
270
		switch( id )
271
		{
272
		case DIALOG_ERROR:	// fall through
273
		case DIALOG_CONTINUEORABORT:
274
			// set dialog message
275
			( (AlertDialog)dialog ).setMessage( _dialogMessage );
276
			break;
277
		case DIALOG_MERGEPROMPT:
278
			// set contact's name
279
			( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
280
					_dialogMessage );
281
			// and set up reference to dialog
282
			_mergePromptDialog = dialog;
283
			break;
284
		}
285
286
		super.onPrepareDialog( id, dialog );
287
	}
288
289
	private void importContacts()
290
	{
291
		// switch interfaces
292
		( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
293
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
294
295
		// disable back button
296
		( (Button)findViewById( R.id.back ) ).setEnabled( false );
297
298
		// create importer
299
		_importer = new VCFImporter( this );
300
301
		// start the service's thread
302
		_importer.start();
303
	}
304
305
	private void updateProgress()
306
	{
307
		ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
308
		TextView outOf = (TextView)findViewById( R.id.doit_outof );
309
310
		if( _maxProgress > 0 )
311
		{
312
			bar.setMax( _maxProgress );
313
			bar.setSecondaryProgress( _tmpProgress );
314
315
			if( _startedProgress )
316
			{
317
				( (TextView)findViewById( R.id.doit_percentage ) ).setText(
318
						(int)Math.round( 100 * _progress / _maxProgress ) + "%" );
319
				outOf.setText( _progress + "/" + _maxProgress );
320
				bar.setProgress( _progress );
321
			}
322
		}
323
	}
324
325
	private void updateStats()
326
	{
327
		( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
328
				"" + _countOverwrites );
329
		( (TextView)findViewById( R.id.doit_creates ) ).setText(
330
				"" + _countCreates );
331
		( (TextView)findViewById( R.id.doit_merges ) ).setText(
332
				"" + _countMerges );
333
		( (TextView)findViewById( R.id.doit_skips ) ).setText(
334
				"" + _countSkips );
335
	}
336
337
	private void abortImport()
338
	{
339
		if( _importer != null )
340
		{
341
			_importer.setAbort();
342
			while( true ) {
343
				try {
344
					_importer.join();
345
					break;
346
				}
347
				catch( InterruptedException e ) {}
348
			}
349
		}
350
	}
351
}