/android/import-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/import-contacts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * Doit.java
 *
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 *
 * This file is part of the Import Contacts program (hereafter referred
 * to as "this program"). For more information, see
 * http://www.waxworlds.org/edam/software/android/import-contacts
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.waxworlds.importcontacts;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class Doit extends WizardActivity
{
	private final static int DIALOG_ERROR = 0;
	private final static int DIALOG_CONTINUEORABORT = 1;
	private final static int DIALOG_MERGEPROMPT = 2;

	public final static int MESSAGE_FINISHED_ALLDONE = 0;
	public final static int MESSAGE_FINISHED = 1;
	public final static int MESSAGE_FINISHED_GOBACK = 2;
	public final static int MESSAGE_ERROR = 3;
	public final static int MESSAGE_CONTINUEORABORT = 4;
	public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
	public final static int MESSAGE_SETMAXPROGRESS = 6;
	public final static int MESSAGE_SETTMPPROGRESS = 7;
	public final static int MESSAGE_SETPROGRESS = 8;
	public final static int MESSAGE_MERGEPROMPT = 9;
	public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
	public final static int MESSAGE_CONTACTCREATED = 11;
	public final static int MESSAGE_CONTACTMERGED = 12;
	public final static int MESSAGE_CONTACTSKIPPED = 13;

	public final static int ACTION_PROMPT = 0;
	public final static int ACTION_KEEP = 1;
	public final static int ACTION_MERGE_MERGE = 2;
	public final static int ACTION_OVERWRITE = 3;

	public final static int NEXT_BEGIN = 0;
	public final static int NEXT_CLOSE = 1;

	private boolean _startedProgress;
	private int _maxProgress;
	private int _tmpProgress;
	private int _progress;
	protected String _dialogMessage;
	private Dialog _mergePromptDialog;
	private boolean _mergePromptAlwaysSelected;
	private int _nextAction;

	private int _countOverwrites;
	private int _countCreates;
	private int _countMerges;
	private int _countSkips;

	protected Importer _importer = null;

	public Handler _handler;

	public class DoitHandler extends Handler
	{
		@Override
		public void handleMessage( Message msg ) {
			switch( msg.what )
			{
			case MESSAGE_FINISHED_ALLDONE:
				( (TextView)findViewById( R.id.doit_alldone ) ).
						setVisibility( View.VISIBLE );
				// fall through
			case MESSAGE_FINISHED:
				updateNext( NEXT_CLOSE );
				findViewById( R.id.doit_abort_disp ).setVisibility(
						View.GONE );
				break;
			case MESSAGE_FINISHED_GOBACK:
				( (Button)findViewById( R.id.back ) ).setEnabled( true );
				findViewById( R.id.doit_abort_disp ).setVisibility(
						View.GONE );
				break;
			case MESSAGE_ERROR:
				Doit.this._dialogMessage = (String)msg.obj;
				showDialog( DIALOG_ERROR );
				break;
			case MESSAGE_CONTINUEORABORT:
				Doit.this._dialogMessage = (String)msg.obj;
				showDialog( DIALOG_CONTINUEORABORT );
				break;
			case MESSAGE_SETPROGRESSMESSAGE:
				( (TextView)findViewById( R.id.doit_percentage ) ).
						setText( (String)msg.obj );
				break;
			case MESSAGE_SETMAXPROGRESS:
				if( _maxProgress > 0 ) {
					if( _tmpProgress == _maxProgress - 1 )
						_tmpProgress = (Integer)msg.obj;
					if( _progress == _maxProgress - 1 )
						_progress = (Integer)msg.obj;
				}
				_maxProgress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_SETTMPPROGRESS:
				_tmpProgress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_SETPROGRESS:
				_startedProgress = true;
				_progress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_MERGEPROMPT:
				_dialogMessage = (String)msg.obj;
				showDialog( DIALOG_MERGEPROMPT );
				break;
			case MESSAGE_CONTACTOVERWRITTEN:
				_countOverwrites++;
				updateStats();
				break;
			case MESSAGE_CONTACTCREATED:
				_countCreates++;
				updateStats();
				break;
			case MESSAGE_CONTACTMERGED:
				_countMerges++;
				updateStats();
				break;
			case MESSAGE_CONTACTSKIPPED:
				_countSkips++;
				updateStats();
				break;
			default:
				super.handleMessage( msg );
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		setContentView( R.layout.doit );
		super.onCreate( savedInstanceState );

		// hide page 2
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );

		// set up abort button
		Button begin = (Button)findViewById( R.id.abort );
		begin.setOnClickListener( new View.OnClickListener() {
			public void onClick( View view ) {
				manualAbort();
			}
		} );

		_startedProgress = false;
		_maxProgress = 0;
		_tmpProgress = 0;
		_progress = 0;
		_handler = new DoitHandler();

		_countOverwrites = 0;
		_countCreates = 0;
		_countMerges = 0;
		_countSkips = 0;

		updateNext( NEXT_BEGIN );

		updateProgress();
		updateStats();
	}

	@Override
	protected void onPause()
	{
		super.onPause();

		// saving the state of an import sounds complicated! Lets just abort!
		abortImport( true );
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		switch( id )
		{
		case DIALOG_ERROR:
			return new AlertDialog.Builder( this )
					.setIcon( R.drawable.alert_dialog_icon )
					.setTitle( R.string.error_title )
					.setMessage( "" )
					.setPositiveButton( R.string.error_ok,
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							Doit.this._importer.wake();
						}
					} )
					.setOnCancelListener( _dialogOnCancelListener )
					.create();
		case DIALOG_CONTINUEORABORT:
			return new AlertDialog.Builder( this )
					.setIcon( R.drawable.alert_dialog_icon )
					.setTitle( R.string.error_title )
					.setMessage( "" )
					.setPositiveButton( R.string.error_continue,
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							Doit.this._importer.wake(
									Importer.RESPONSE_POSITIVE );
						}
					} )
					.setNegativeButton( R.string.error_abort,
							new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							Doit.this._importer.wake(
									Importer.RESPONSE_NEGATIVE );
						}
					} )
					.setOnCancelListener( _dialogOnCancelListener )
					.create();
		case DIALOG_MERGEPROMPT:
			// custom layout in an AlertDialog
			LayoutInflater factory = LayoutInflater.from( this );
			final View dialogView = factory.inflate(
					R.layout.mergeprompt, null );
			( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
					setOnCheckedChangeListener(
							new CompoundButton.OnCheckedChangeListener() {
				public void onCheckedChanged( CompoundButton buttonView,
						boolean isChecked ) {
					Doit.this._mergePromptAlwaysSelected = isChecked;
				}
			} );
			( (Button)dialogView.findViewById( R.id.merge_keep ) ).
					setOnClickListener( _mergePromptButtonListener );
			( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
					setOnClickListener( _mergePromptButtonListener );
			( (Button)dialogView.findViewById( R.id.merge_merge ) ).
					setOnClickListener( _mergePromptButtonListener );
			( (Button)dialogView.findViewById( R.id.abort ) ).
					setOnClickListener( _mergePromptButtonListener );
			_mergePromptAlwaysSelected = false;
			return new AlertDialog.Builder( this )
					.setIcon( R.drawable.alert_dialog_icon )
					.setTitle( R.string.mergeprompt_title )
					.setView( dialogView )
					.setOnCancelListener( _dialogOnCancelListener )
					.create();
		}
		return null;
	}

    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
		public void onClick( View view )
		{
			// handle abort
			if( view.getId() == R.id.abort )
				manualAbort();

			// else, response
			else {
				int responseExtra = _mergePromptAlwaysSelected?
						Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
				Doit.this._importer.wake( convertIdToAction( view.getId() ),
						responseExtra );
			}

			// close dialog and free (don't keep a reference)
			Doit.this._mergePromptDialog.dismiss();
			Doit.this._mergePromptDialog = null;
		}
	};

	@Override
	protected void onNext()
	{
		Button next = (Button)findViewById( R.id.next );
		next.setEnabled( false );

		switch( _nextAction )
		{
		case NEXT_BEGIN:
			importContacts();
			break;
		case NEXT_CLOSE:
			setResult( RESULT_CANCELED );
			finish();
			break;
		}
	}

	private void manualAbort()
	{
		abortImport( false );
		updateNext( NEXT_CLOSE );
		( (Button)findViewById( R.id.back ) ).setEnabled( true );
		findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
	}

	private void updateNext( int nextAction )
	{
		Button next = (Button)findViewById( R.id.next );
		switch( nextAction ) {
		case NEXT_BEGIN:	next.setText( R.string.doit_begin ); break;
		case NEXT_CLOSE:	next.setText( R.string.doit_close ); break;
		}
		next.setEnabled( true );
		_nextAction = nextAction;
	}

	public static int convertIdToAction( int id ) {
		switch( id ) {
		case R.id.merge_keep:		return ACTION_KEEP;
		case R.id.merge_merge:		return ACTION_MERGE_MERGE;
		case R.id.merge_overwrite:	return ACTION_OVERWRITE;
		default: return ACTION_PROMPT;
		}
	}

	public static int convertActionToId( int action ) {
		switch( action ) {
		case ACTION_KEEP:		return R.id.merge_keep;
		case ACTION_MERGE_MERGE:return R.id.merge_merge;
		case ACTION_OVERWRITE:	return R.id.merge_overwrite;
		default: return R.id.merge_prompt;
		}
	}

	private DialogInterface.OnCancelListener _dialogOnCancelListener =
			new DialogInterface.OnCancelListener() {
		public void onCancel( DialogInterface dialog ) {
			setResult( RESULT_CANCELED );
			finish();
		}
	};

	@Override
	protected void onActivityResult( int requestCode, int resultCode,
			Intent data )
	{
		// if we're cancelling, abort any import
		if( resultCode == RESULT_CANCELED )
			abortImport( true );
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog)
	{
		switch( id )
		{
		case DIALOG_ERROR:	// fall through
		case DIALOG_CONTINUEORABORT:
			// set dialog message
			( (AlertDialog)dialog ).setMessage( _dialogMessage );
			break;
		case DIALOG_MERGEPROMPT:
			// set contact's name
			( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
					_dialogMessage );
			// and set up reference to dialog
			_mergePromptDialog = dialog;
			break;
		}

		super.onPrepareDialog( id, dialog );
	}

	private void importContacts()
	{
		// switch interfaces
		( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
		( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );

		// disable back button
		( (Button)findViewById( R.id.back ) ).setEnabled( false );

		// create importer
		_importer = new VCFImporter( this );

		// start the service's thread
		_importer.start();
	}

	private void updateProgress()
	{
		ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
		TextView outOf = (TextView)findViewById( R.id.doit_outof );

		if( _maxProgress > 0 )
		{
			bar.setMax( _maxProgress );
			bar.setSecondaryProgress( _tmpProgress );

			if( _startedProgress )
			{
				( (TextView)findViewById( R.id.doit_percentage ) ).setText(
						(int)Math.round( 100 * _progress / _maxProgress ) + "%" );
				outOf.setText( _progress + "/" + _maxProgress );
				bar.setProgress( _progress );
			}
		}
	}

	private void updateStats()
	{
		( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
				"" + _countOverwrites );
		( (TextView)findViewById( R.id.doit_creates ) ).setText(
				"" + _countCreates );
		( (TextView)findViewById( R.id.doit_merges ) ).setText(
				"" + _countMerges );
		( (TextView)findViewById( R.id.doit_skips ) ).setText(
				"" + _countSkips );
	}

	private void abortImport( boolean showToasterPopup )
	{
		if( _importer != null )
		{
			// try and flag worker thread - did we need to?
			if( _importer.setAbort() )
			{
				// wait for worker thread to end
				while( true ) {
					try {
						_importer.join();
						break;
					}
					catch( InterruptedException e ) {}
				}

				// notify the user
				if( showToasterPopup )
					Toast.makeText( this, R.string.doit_importaborted,
							Toast.LENGTH_LONG ).show();
			}
		}

		// destroy some stuff
		_importer = null;
		_handler = null;
	}
}