/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
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.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

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;

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

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

	protected Importer _importer;

	public Handler _handler;

	public class DoitHandler extends Handler
	{
		@Override
		public void handleMessage( Message msg ) {
			switch( msg.what )
			{
			case Importer.MESSAGE_FINISHED:
				( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
						setVisibility( View.VISIBLE );
				break;
			case Importer.MESSAGE_FINISHED_BACK:
				( (Button)findViewById( R.id.back ) ).setEnabled( true );
				break;
			case Importer.MESSAGE_ERROR:
				Doit.this._dialogMessage = (String)msg.obj;
				showDialog( DIALOG_ERROR );
				break;
			case Importer.MESSAGE_CONTINUEORABORT:
				Doit.this._dialogMessage = (String)msg.obj;
				showDialog( DIALOG_CONTINUEORABORT );
				break;
			case Importer.MESSAGE_SETPROGRESSMESSAGE:
				( (TextView)findViewById( R.id.doit_percentage ) ).
						setText( (String)msg.obj );
				break;
			case Importer.MESSAGE_SETMAXPROGRESS:
				if( _maxProgress > 0 ) {
					if( _tmpProgress == _maxProgress )
						_tmpProgress = (Integer)msg.obj;
					if( _progress == _maxProgress )
						_progress = (Integer)msg.obj;
				}
				_maxProgress = (Integer)msg.obj;
				updateProgress();
				break;
			case Importer.MESSAGE_SETTMPPROGRESS:
				_tmpProgress = (Integer)msg.obj;
				updateProgress();
				break;
			case Importer.MESSAGE_SETPROGRESS:
				_startedProgress = true;
				_progress = (Integer)msg.obj;
				updateProgress();
				break;
			case Importer.MESSAGE_MERGEPROMPT:
				_dialogMessage = (String)msg.obj;
				showDialog( DIALOG_MERGEPROMPT );
				break;
			case Importer.MESSAGE_CONTACTOVERWRITTEN:
				_countOverwrites++;
				updateStats();
				break;
			case Importer.MESSAGE_CONTACTCREATED:
				_countCreates++;
				updateStats();
				break;
			case Importer.MESSAGE_CONTACTMERGED:
				_countMerges++;
				updateStats();
				break;
			case Importer.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 begin button
		Button begin = (Button)findViewById( R.id.doit_begin );
		begin.setOnClickListener( new View.OnClickListener() {
			public void onClick( View view ) {
				importContacts();
			}
		} );

		// set up close button
		Button close = (Button)findViewById( R.id.doit_close );
		close.setOnClickListener( new View.OnClickListener() {
			public void onClick( View view ) {
				setResult( RESULT_CANCELED );
				finish();
			}
		} );



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

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

		updateProgress();
		updateStats();
	}

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

		abortImport();

		// notify the user


	}

	@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 );
			_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 ) {
			int responseExtra = _mergePromptAlwaysSelected?
					Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
			Doit.this._mergePromptDialog.dismiss();
			Doit.this._mergePromptDialog = null;	// dont keep a reference!
			Doit.this._importer.wake( view.getId(), responseExtra );
		}
	};

	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( resultCode == RESULT_CANCELED )
			abortImport();
	}

	@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()
	{
		if( _importer != null )
		{
			_importer.setAbort();
			while( true ) {
				try {
					_importer.join();
					break;
				}
				catch( InterruptedException e ) {}
			}
		}
	}
}