/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(
36 by edam
- formatting: removed some double-indents on overrunning lines
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 ) ).
36 by edam
- formatting: removed some double-indents on overrunning lines
117
					setText( (String)msg.obj );
1 by edam
Initial import
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?
36 by edam
- formatting: removed some double-indents on overrunning lines
296
					Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
10 by edam
- made behaviour of next button overridable
297
				Doit.this._importer.wake( convertIdToAction( view.getId() ),
36 by edam
- formatting: removed some double-indents on overrunning lines
298
					responseExtra );
10 by edam
- made behaviour of next button overridable
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 ) {
36 by edam
- formatting: removed some double-indents on overrunning lines
347
			// ignore errors
19 by edam
- added file chooser
348
		}
10 by edam
- made behaviour of next button overridable
349
	}
350
351
	private void updateNext( int nextAction )
352
	{
353
		Button next = (Button)findViewById( R.id.next );
354
		switch( nextAction ) {
355
		case NEXT_BEGIN:	next.setText( R.string.doit_begin ); break;
356
		case NEXT_CLOSE:	next.setText( R.string.doit_close ); break;
357
		}
358
		next.setEnabled( true );
359
		_nextAction = nextAction;
360
	}
361
9 by edam
- added scroll view to all layouts
362
	public static int convertIdToAction( int id ) {
363
		switch( id ) {
364
		case R.id.merge_keep:		return ACTION_KEEP;
365
		case R.id.merge_merge:		return ACTION_MERGE_MERGE;
366
		case R.id.merge_overwrite:	return ACTION_OVERWRITE;
367
		default: return ACTION_PROMPT;
368
		}
369
	}
370
371
	public static int convertActionToId( int action ) {
372
		switch( action ) {
373
		case ACTION_KEEP:		return R.id.merge_keep;
374
		case ACTION_MERGE_MERGE:return R.id.merge_merge;
375
		case ACTION_OVERWRITE:	return R.id.merge_overwrite;
376
		default: return R.id.merge_prompt;
377
		}
378
	}
379
1 by edam
Initial import
380
	private DialogInterface.OnCancelListener _dialogOnCancelListener =
381
			new DialogInterface.OnCancelListener() {
382
		public void onCancel( DialogInterface dialog ) {
19 by edam
- added file chooser
383
			manualAbort();
1 by edam
Initial import
384
		}
385
	};
386
19 by edam
- added file chooser
387
1 by edam
Initial import
388
	@Override
389
	protected void onActivityResult( int requestCode, int resultCode,
390
			Intent data )
391
	{
2 by edam
- added toaster message about import abortion in onPause()
392
		// if we're cancelling, abort any import
1 by edam
Initial import
393
		if( resultCode == RESULT_CANCELED )
10 by edam
- made behaviour of next button overridable
394
			abortImport( true );
1 by edam
Initial import
395
	}
396
397
	@Override
398
	protected void onPrepareDialog(int id, Dialog dialog)
399
	{
14 by edam
- got rid of the pretend ImportContacts activity alltogether (and made the Intro activity the startup one)
400
		_currentDialogId = id;
401
1 by edam
Initial import
402
		switch( id )
403
		{
404
		case DIALOG_ERROR:	// fall through
405
		case DIALOG_CONTINUEORABORT:
406
			// set dialog message
407
			( (AlertDialog)dialog ).setMessage( _dialogMessage );
408
			break;
409
		case DIALOG_MERGEPROMPT:
410
			// set contact's name
411
			( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
412
				_dialogMessage );
1 by edam
Initial import
413
			// and set up reference to dialog
414
			_mergePromptDialog = dialog;
415
			break;
416
		}
417
418
		super.onPrepareDialog( id, dialog );
419
	}
420
421
	private void importContacts()
422
	{
423
		// switch interfaces
424
		( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
425
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
426
427
		// disable back button
428
		( (Button)findViewById( R.id.back ) ).setEnabled( false );
429
430
		// create importer
431
		_importer = new VCFImporter( this );
432
433
		// start the service's thread
434
		_importer.start();
435
	}
436
437
	private void updateProgress()
438
	{
439
		ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
440
		TextView outOf = (TextView)findViewById( R.id.doit_outof );
441
442
		if( _maxProgress > 0 )
443
		{
444
			bar.setMax( _maxProgress );
445
			bar.setSecondaryProgress( _tmpProgress );
446
447
			if( _startedProgress )
448
			{
449
				( (TextView)findViewById( R.id.doit_percentage ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
450
					(int)Math.round( 100 * _progress / _maxProgress ) + "%" );
1 by edam
Initial import
451
				outOf.setText( _progress + "/" + _maxProgress );
452
				bar.setProgress( _progress );
453
			}
454
		}
455
	}
456
457
	private void updateStats()
458
	{
459
		( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
460
			"" + _countOverwrites );
1 by edam
Initial import
461
		( (TextView)findViewById( R.id.doit_creates ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
462
			"" + _countCreates );
1 by edam
Initial import
463
		( (TextView)findViewById( R.id.doit_merges ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
464
			"" + _countMerges );
1 by edam
Initial import
465
		( (TextView)findViewById( R.id.doit_skips ) ).setText(
36 by edam
- formatting: removed some double-indents on overrunning lines
466
			"" + _countSkips );
1 by edam
Initial import
467
	}
468
10 by edam
- made behaviour of next button overridable
469
	private void abortImport( boolean showToasterPopup )
1 by edam
Initial import
470
	{
471
		if( _importer != null )
472
		{
3 by edam
- added "all done" message
473
			// try and flag worker thread - did we need to?
474
			if( _importer.setAbort() )
475
			{
476
				// wait for worker thread to end
477
				while( true ) {
478
					try {
479
						_importer.join();
480
						break;
481
					}
482
					catch( InterruptedException e ) {}
1 by edam
Initial import
483
				}
3 by edam
- added "all done" message
484
485
				// notify the user
10 by edam
- made behaviour of next button overridable
486
				if( showToasterPopup )
487
					Toast.makeText( this, R.string.doit_importaborted,
36 by edam
- formatting: removed some double-indents on overrunning lines
488
						Toast.LENGTH_LONG ).show();
1 by edam
Initial import
489
			}
490
		}
10 by edam
- made behaviour of next button overridable
491
492
		// destroy some stuff
493
		_importer = null;
494
		_handler = null;
1 by edam
Initial import
495
	}
496
}