/android/import-contacts

To get this branch, use:
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
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
24
package org.waxworlds.edam.importcontacts;
1 by edam
Initial import
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
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
49
	public final static int MESSAGE_ALLDONE = 0;
50
	public final static int MESSAGE_ABORT = 1;
3 by edam
- added "all done" message
51
	public final static int MESSAGE_ERROR = 3;
52
	public final static int MESSAGE_CONTINUEORABORT = 4;
53
	public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
54
	public final static int MESSAGE_SETMAXPROGRESS = 6;
55
	public final static int MESSAGE_SETTMPPROGRESS = 7;
56
	public final static int MESSAGE_SETPROGRESS = 8;
57
	public final static int MESSAGE_MERGEPROMPT = 9;
58
	public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
59
	public final static int MESSAGE_CONTACTCREATED = 11;
60
	public final static int MESSAGE_CONTACTMERGED = 12;
61
	public final static int MESSAGE_CONTACTSKIPPED = 13;
62
9 by edam
- added scroll view to all layouts
63
	public final static int ACTION_PROMPT = 0;
64
	public final static int ACTION_KEEP = 1;
65
	public final static int ACTION_MERGE_MERGE = 2;
66
	public final static int ACTION_OVERWRITE = 3;
67
10 by edam
- made behaviour of next button overridable
68
	public final static int NEXT_BEGIN = 0;
69
	public final static int NEXT_CLOSE = 1;
70
1 by edam
Initial import
71
	private boolean _startedProgress;
72
	private int _maxProgress;
73
	private int _tmpProgress;
74
	private int _progress;
75
	protected String _dialogMessage;
76
	private Dialog _mergePromptDialog;
77
	private boolean _mergePromptAlwaysSelected;
10 by edam
- made behaviour of next button overridable
78
	private int _nextAction;
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
79
	private int _currentDialogId;
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
			{
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
96
			case MESSAGE_ALLDONE:
3 by edam
- added "all done" message
97
				( (TextView)findViewById( R.id.doit_alldone ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
98
					setVisibility( View.VISIBLE );
99
				( (Button)findViewById( R.id.back ) ).setEnabled( false );
10 by edam
- made behaviour of next button overridable
100
				updateNext( NEXT_CLOSE );
101
				findViewById( R.id.doit_abort_disp ).setVisibility(
102
						View.GONE );
1 by edam
Initial import
103
				break;
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
104
			case MESSAGE_ABORT:
105
				manualAbort();
1 by edam
Initial import
106
				break;
3 by edam
- added "all done" message
107
			case MESSAGE_ERROR:
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
108
				_dialogMessage = (String)msg.obj;
1 by edam
Initial import
109
				showDialog( DIALOG_ERROR );
110
				break;
3 by edam
- added "all done" message
111
			case MESSAGE_CONTINUEORABORT:
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
112
				_dialogMessage = (String)msg.obj;
1 by edam
Initial import
113
				showDialog( DIALOG_CONTINUEORABORT );
114
				break;
3 by edam
- added "all done" message
115
			case MESSAGE_SETPROGRESSMESSAGE:
1 by edam
Initial import
116
				( (TextView)findViewById( R.id.doit_percentage ) ).
117
						setText( (String)msg.obj );
118
				break;
3 by edam
- added "all done" message
119
			case MESSAGE_SETMAXPROGRESS:
1 by edam
Initial import
120
				if( _maxProgress > 0 ) {
10 by edam
- made behaviour of next button overridable
121
					if( _tmpProgress == _maxProgress - 1 )
1 by edam
Initial import
122
						_tmpProgress = (Integer)msg.obj;
10 by edam
- made behaviour of next button overridable
123
					if( _progress == _maxProgress - 1 )
1 by edam
Initial import
124
						_progress = (Integer)msg.obj;
125
				}
126
				_maxProgress = (Integer)msg.obj;
127
				updateProgress();
128
				break;
3 by edam
- added "all done" message
129
			case MESSAGE_SETTMPPROGRESS:
1 by edam
Initial import
130
				_tmpProgress = (Integer)msg.obj;
131
				updateProgress();
132
				break;
3 by edam
- added "all done" message
133
			case MESSAGE_SETPROGRESS:
1 by edam
Initial import
134
				_startedProgress = true;
135
				_progress = (Integer)msg.obj;
136
				updateProgress();
137
				break;
3 by edam
- added "all done" message
138
			case MESSAGE_MERGEPROMPT:
1 by edam
Initial import
139
				_dialogMessage = (String)msg.obj;
140
				showDialog( DIALOG_MERGEPROMPT );
141
				break;
3 by edam
- added "all done" message
142
			case MESSAGE_CONTACTOVERWRITTEN:
1 by edam
Initial import
143
				_countOverwrites++;
144
				updateStats();
145
				break;
3 by edam
- added "all done" message
146
			case MESSAGE_CONTACTCREATED:
1 by edam
Initial import
147
				_countCreates++;
148
				updateStats();
149
				break;
3 by edam
- added "all done" message
150
			case MESSAGE_CONTACTMERGED:
1 by edam
Initial import
151
				_countMerges++;
152
				updateStats();
153
				break;
3 by edam
- added "all done" message
154
			case MESSAGE_CONTACTSKIPPED:
1 by edam
Initial import
155
				_countSkips++;
156
				updateStats();
157
				break;
158
			default:
159
				super.handleMessage( msg );
160
			}
161
		}
162
	}
163
164
	@Override
165
	protected void onCreate(Bundle savedInstanceState)
166
	{
167
		setContentView( R.layout.doit );
168
		super.onCreate( savedInstanceState );
169
170
		// hide page 2
171
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
172
10 by edam
- made behaviour of next button overridable
173
		// set up abort button
174
		Button begin = (Button)findViewById( R.id.abort );
1 by edam
Initial import
175
		begin.setOnClickListener( new View.OnClickListener() {
176
			public void onClick( View view ) {
10 by edam
- made behaviour of next button overridable
177
				manualAbort();
1 by edam
Initial import
178
			}
179
		} );
180
181
		_startedProgress = false;
182
		_maxProgress = 0;
183
		_tmpProgress = 0;
184
		_progress = 0;
185
		_handler = new DoitHandler();
186
187
		_countOverwrites = 0;
188
		_countCreates = 0;
189
		_countMerges = 0;
190
		_countSkips = 0;
191
10 by edam
- made behaviour of next button overridable
192
		updateNext( NEXT_BEGIN );
193
1 by edam
Initial import
194
		updateProgress();
195
		updateStats();
196
	}
197
198
	@Override
199
	protected void onPause()
200
	{
201
		super.onPause();
202
2 by edam
- added toaster message about import abortion in onPause()
203
		// saving the state of an import sounds complicated! Lets just abort!
19 by edam
- added file chooser
204
		if( _nextAction != NEXT_CLOSE )
205
			manualAbort( true );
1 by edam
Initial import
206
	}
207
208
	@Override
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
209
	protected Dialog onCreateDialog( int id )
1 by edam
Initial import
210
	{
211
		switch( id )
212
		{
213
		case DIALOG_ERROR:
214
			return new AlertDialog.Builder( this )
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
215
				.setIcon( R.drawable.alert_dialog_icon )
216
				.setTitle( R.string.error_title )
217
				.setMessage( "" )
218
				.setPositiveButton( R.string.error_ok,
219
					new DialogInterface.OnClickListener() {
1 by edam
Initial import
220
						public void onClick(DialogInterface dialog,
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
221
							int whichButton)
222
						{
1 by edam
Initial import
223
							Doit.this._importer.wake();
224
						}
225
					} )
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
226
				.setOnCancelListener( _dialogOnCancelListener )
227
				.create();
1 by edam
Initial import
228
		case DIALOG_CONTINUEORABORT:
229
			return new AlertDialog.Builder( this )
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
230
				.setIcon( R.drawable.alert_dialog_icon )
231
				.setTitle( R.string.error_title )
232
				.setMessage( "" )
233
				.setPositiveButton( R.string.error_continue,
234
					new DialogInterface.OnClickListener() {
235
						public void onClick(DialogInterface dialog,
236
							int whichButton)
237
						{
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
						{
247
							Doit.this._importer.wake(
248
								Importer.RESPONSE_NEGATIVE );
249
						}
250
					} )
251
				.setOnCancelListener( _dialogOnCancelListener )
252
				.create();
1 by edam
Initial import
253
		case DIALOG_MERGEPROMPT:
254
			// custom layout in an AlertDialog
255
			LayoutInflater factory = LayoutInflater.from( this );
256
			final View dialogView = factory.inflate(
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
257
				R.layout.mergeprompt, null );
1 by edam
Initial import
258
			( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
259
				setOnCheckedChangeListener(
260
					new CompoundButton.OnCheckedChangeListener() {
261
						public void onCheckedChanged( CompoundButton buttonView,
262
							boolean isChecked )
263
						{
264
							Doit.this._mergePromptAlwaysSelected = isChecked;
265
						}
266
					} );
1 by edam
Initial import
267
			( (Button)dialogView.findViewById( R.id.merge_keep ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
268
				setOnClickListener( _mergePromptButtonListener );
1 by edam
Initial import
269
			( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
270
				setOnClickListener( _mergePromptButtonListener );
1 by edam
Initial import
271
			( (Button)dialogView.findViewById( R.id.merge_merge ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
272
				setOnClickListener( _mergePromptButtonListener );
10 by edam
- made behaviour of next button overridable
273
			( (Button)dialogView.findViewById( R.id.abort ) ).
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
274
				setOnClickListener( _mergePromptButtonListener );
1 by edam
Initial import
275
			_mergePromptAlwaysSelected = false;
276
			return new AlertDialog.Builder( this )
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
277
				.setIcon( R.drawable.alert_dialog_icon )
278
				.setTitle( R.string.mergeprompt_title )
279
				.setView( dialogView )
280
				.setOnCancelListener( _dialogOnCancelListener )
281
				.create();
1 by edam
Initial import
282
		}
283
		return null;
284
	}
285
13 by edam
- converted project to use Android 1.5 SDK
286
	private OnClickListener _mergePromptButtonListener = new OnClickListener() {
10 by edam
- made behaviour of next button overridable
287
		public void onClick( View view )
288
		{
289
			// handle abort
290
			if( view.getId() == R.id.abort )
291
				manualAbort();
292
19 by edam
- added file chooser
293
			// else, response (just check we haven't aborted already!)
294
			else if( Doit.this._importer != null ) {
10 by edam
- made behaviour of next button overridable
295
				int responseExtra = _mergePromptAlwaysSelected?
296
						Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
297
				Doit.this._importer.wake( convertIdToAction( view.getId() ),
298
						responseExtra );
299
			}
300
301
			// close dialog and free (don't keep a reference)
1 by edam
Initial import
302
			Doit.this._mergePromptDialog.dismiss();
10 by edam
- made behaviour of next button overridable
303
			Doit.this._mergePromptDialog = null;
1 by edam
Initial import
304
		}
305
	};
306
10 by edam
- made behaviour of next button overridable
307
	@Override
308
	protected void onNext()
309
	{
310
		Button next = (Button)findViewById( R.id.next );
311
		next.setEnabled( false );
312
313
		switch( _nextAction )
314
		{
315
		case NEXT_BEGIN:
316
			importContacts();
317
			break;
318
		case NEXT_CLOSE:
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
319
			setResult( RESULT_OK );
10 by edam
- made behaviour of next button overridable
320
			finish();
321
			break;
322
		}
323
	}
324
325
	private void manualAbort()
326
	{
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
327
		manualAbort( false );
328
	}
329
330
	private void manualAbort( boolean showToasterPopup )
331
	{
332
		abortImport( showToasterPopup );
333
10 by edam
- made behaviour of next button overridable
334
		updateNext( NEXT_CLOSE );
335
		( (Button)findViewById( R.id.back ) ).setEnabled( true );
336
		findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
337
		( (TextView)findViewById( R.id.doit_aborted ) ).
338
			setVisibility( View.VISIBLE );
339
		( (TextView)findViewById( R.id.doit_alldone ) ).
340
			setVisibility( View.GONE );
19 by edam
- added file chooser
341
342
		// close any open dialogs
343
		try {
344
			dismissDialog( _currentDialogId );
345
		}
346
		catch( Exception e ) {
347
		}
10 by edam
- made behaviour of next button overridable
348
	}
349
350
	private void updateNext( int nextAction )
351
	{
352
		Button next = (Button)findViewById( R.id.next );
353
		switch( nextAction ) {
354
		case NEXT_BEGIN:	next.setText( R.string.doit_begin ); break;
355
		case NEXT_CLOSE:	next.setText( R.string.doit_close ); break;
356
		}
357
		next.setEnabled( true );
358
		_nextAction = nextAction;
359
	}
360
9 by edam
- added scroll view to all layouts
361
	public static int convertIdToAction( int id ) {
362
		switch( id ) {
363
		case R.id.merge_keep:		return ACTION_KEEP;
364
		case R.id.merge_merge:		return ACTION_MERGE_MERGE;
365
		case R.id.merge_overwrite:	return ACTION_OVERWRITE;
366
		default: return ACTION_PROMPT;
367
		}
368
	}
369
370
	public static int convertActionToId( int action ) {
371
		switch( action ) {
372
		case ACTION_KEEP:		return R.id.merge_keep;
373
		case ACTION_MERGE_MERGE:return R.id.merge_merge;
374
		case ACTION_OVERWRITE:	return R.id.merge_overwrite;
375
		default: return R.id.merge_prompt;
376
		}
377
	}
378
1 by edam
Initial import
379
	private DialogInterface.OnCancelListener _dialogOnCancelListener =
380
			new DialogInterface.OnCancelListener() {
381
		public void onCancel( DialogInterface dialog ) {
19 by edam
- added file chooser
382
			manualAbort();
1 by edam
Initial import
383
		}
384
	};
385
19 by edam
- added file chooser
386
1 by edam
Initial import
387
	@Override
388
	protected void onActivityResult( int requestCode, int resultCode,
389
			Intent data )
390
	{
2 by edam
- added toaster message about import abortion in onPause()
391
		// if we're cancelling, abort any import
1 by edam
Initial import
392
		if( resultCode == RESULT_CANCELED )
10 by edam
- made behaviour of next button overridable
393
			abortImport( true );
1 by edam
Initial import
394
	}
395
396
	@Override
397
	protected void onPrepareDialog(int id, Dialog dialog)
398
	{
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
399
		_currentDialogId = id;
400
1 by edam
Initial import
401
		switch( id )
402
		{
403
		case DIALOG_ERROR:	// fall through
404
		case DIALOG_CONTINUEORABORT:
405
			// set dialog message
406
			( (AlertDialog)dialog ).setMessage( _dialogMessage );
407
			break;
408
		case DIALOG_MERGEPROMPT:
409
			// set contact's name
410
			( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
411
					_dialogMessage );
412
			// and set up reference to dialog
413
			_mergePromptDialog = dialog;
414
			break;
415
		}
416
417
		super.onPrepareDialog( id, dialog );
418
	}
419
420
	private void importContacts()
421
	{
422
		// switch interfaces
423
		( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
424
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
425
426
		// disable back button
427
		( (Button)findViewById( R.id.back ) ).setEnabled( false );
428
429
		// create importer
430
		_importer = new VCFImporter( this );
431
432
		// start the service's thread
433
		_importer.start();
434
	}
435
436
	private void updateProgress()
437
	{
438
		ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
439
		TextView outOf = (TextView)findViewById( R.id.doit_outof );
440
441
		if( _maxProgress > 0 )
442
		{
443
			bar.setMax( _maxProgress );
444
			bar.setSecondaryProgress( _tmpProgress );
445
446
			if( _startedProgress )
447
			{
448
				( (TextView)findViewById( R.id.doit_percentage ) ).setText(
449
						(int)Math.round( 100 * _progress / _maxProgress ) + "%" );
450
				outOf.setText( _progress + "/" + _maxProgress );
451
				bar.setProgress( _progress );
452
			}
453
		}
454
	}
455
456
	private void updateStats()
457
	{
458
		( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
459
				"" + _countOverwrites );
460
		( (TextView)findViewById( R.id.doit_creates ) ).setText(
461
				"" + _countCreates );
462
		( (TextView)findViewById( R.id.doit_merges ) ).setText(
463
				"" + _countMerges );
464
		( (TextView)findViewById( R.id.doit_skips ) ).setText(
465
				"" + _countSkips );
466
	}
467
10 by edam
- made behaviour of next button overridable
468
	private void abortImport( boolean showToasterPopup )
1 by edam
Initial import
469
	{
470
		if( _importer != null )
471
		{
3 by edam
- added "all done" message
472
			// try and flag worker thread - did we need to?
473
			if( _importer.setAbort() )
474
			{
475
				// wait for worker thread to end
476
				while( true ) {
477
					try {
478
						_importer.join();
479
						break;
480
					}
481
					catch( InterruptedException e ) {}
1 by edam
Initial import
482
				}
3 by edam
- added "all done" message
483
484
				// notify the user
10 by edam
- made behaviour of next button overridable
485
				if( showToasterPopup )
486
					Toast.makeText( this, R.string.doit_importaborted,
487
							Toast.LENGTH_LONG ).show();
1 by edam
Initial import
488
			}
489
		}
10 by edam
- made behaviour of next button overridable
490
491
		// destroy some stuff
492
		_importer = null;
493
		_handler = null;
1 by edam
Initial import
494
	}
495
}