/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-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
/*
 * Doit.java
 *
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
 *
 * This file is part of the Export Contacts program (hereafter referred
 * to as "this program").  For more information, see
 * http://ed.am/dev/android/export-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 am.ed.exportcontacts;

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.View;
import android.widget.Button;
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;

	public final static int MESSAGE_ALLDONE = 0;
	public final static int MESSAGE_ABORT = 1;
	public final static int MESSAGE_ERROR = 2;
	public final static int MESSAGE_CONTINUEORABORT = 3;
	public final static int MESSAGE_SETPROGRESSMESSAGE = 4;
	public final static int MESSAGE_SETMAXPROGRESS = 5;
	public final static int MESSAGE_SETTMPPROGRESS = 6;
	public final static int MESSAGE_SETPROGRESS = 7;
	public final static int MESSAGE_CONTACTWRITTEN = 8;
	public final static int MESSAGE_CONTACTSKIPPED = 9;

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

	private boolean _started_progress;
	private int _max_progress;
	private int _tmp_progress;
	private int _progress;
	protected String _dialog_message;
	private int _next_action;
	private int _current_dialog_id;

	private int _count_writes;
	private int _count_skips;

	protected Exporter _exporter = null;

	public Handler _handler;

	public class DoitHandler extends Handler
	{
		@Override
		public void handleMessage( Message msg ) {
			switch( msg.what )
			{
			case MESSAGE_ALLDONE:
				( (TextView)findViewById( R.id.doit_alldone ) ).
					setVisibility( View.VISIBLE );
				( (Button)findViewById( R.id.back ) ).setEnabled( false );
				updateNext( NEXT_CLOSE );
				findViewById( R.id.doit_abort_disp ).setVisibility(
					View.GONE );
				break;
			case MESSAGE_ABORT:
				manualAbort();
				break;
			case MESSAGE_ERROR:
				_dialog_message = (String)msg.obj;
				showDialog( DIALOG_ERROR );
				break;
			case MESSAGE_CONTINUEORABORT:
				_dialog_message = (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( _max_progress > 0 ) {
					if( _tmp_progress == _max_progress - 1 )
						_tmp_progress = (Integer)msg.obj;
					if( _progress == _max_progress - 1 )
						_progress = (Integer)msg.obj;
				}
				_max_progress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_SETTMPPROGRESS:
				_tmp_progress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_SETPROGRESS:
				_started_progress = true;
				_progress = (Integer)msg.obj;
				updateProgress();
				break;
			case MESSAGE_CONTACTWRITTEN:
				_count_writes++;
				updateStats();
				break;
			case MESSAGE_CONTACTSKIPPED:
				_count_skips++;
				updateStats();
				break;
			default:
				super.handleMessage( msg );
			}
		}
	}

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

		// 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();
			}
		} );

		_started_progress = false;
		_max_progress = 0;
		_tmp_progress = 0;
		_progress = 0;
		_handler = new DoitHandler();

		_count_writes = 0;
		_count_skips = 0;

		updateNext( NEXT_BEGIN );

		updateProgress();
		updateStats();
	}

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

		// saving the state of an export sounds complicated! Lets just abort!
		if( _next_action != NEXT_CLOSE )
			manualAbort( 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 )
						{
							if( Doit.this != null )
								Doit.this._exporter.wake();
						}
					} )
				.setOnCancelListener( _dialog_on_cancel_listener )
				.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 which_button )
						{
							if( Doit.this != null )
								Doit.this._exporter.wake(
									Exporter.RESPONSE_POSITIVE );
						}
					} )
				.setNegativeButton( R.string.error_abort,
					new DialogInterface.OnClickListener() {
						public void onClick( DialogInterface dialog,
							int which_button )
						{
							if( Doit.this != null )
								Doit.this._exporter.wake(
									Exporter.RESPONSE_NEGATIVE );
						}
					} )
				.setOnCancelListener( _dialog_on_cancel_listener )
				.create();
		}
		return null;
	}

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

		switch( _next_action )
		{
		case NEXT_BEGIN:
			exportContacts();
			break;
		case NEXT_CLOSE:
			setResult( RESULT_OK );
			finish();
			break;
		}
	}

	private void manualAbort()
	{
		manualAbort( false );
	}

	private void manualAbort( boolean show_toaster_popup )
	{
		abortExport( show_toaster_popup );

		updateNext( NEXT_CLOSE );
		( (Button)findViewById( R.id.back ) ).setEnabled( true );
		findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
		( (TextView)findViewById( R.id.doit_aborted ) ).
			setVisibility( View.VISIBLE );
		( (TextView)findViewById( R.id.doit_alldone ) ).
			setVisibility( View.GONE );

		// close any open dialogs
		try {
			dismissDialog( _current_dialog_id );
		}
		catch( Exception e ) {
			// ignore errors
		}
	}

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

	private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
		new DialogInterface.OnCancelListener()
	{
		public void onCancel( DialogInterface dialog ) {
			manualAbort();
		}
	};


	@Override
	protected void onActivityResult( int request_code, int result_code,
		Intent data )
	{
		// if we're cancelling, abort any export
		if( result_code == RESULT_CANCELED )
			abortExport( true );
	}

	@Override
	protected void onPrepareDialog( int id, Dialog dialog )
	{
		_current_dialog_id = id;

		switch( id )
		{
		case DIALOG_ERROR:	// fall through
		case DIALOG_CONTINUEORABORT:
			// set dialog message
			( (AlertDialog)dialog ).setMessage( _dialog_message );
			break;
		}

		super.onPrepareDialog( id, dialog );
	}

	private void exportContacts()
	{
		// 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 exporter
		_exporter = new VcardExporter( this );

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

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

		if( _max_progress > 0 )
		{
			bar.setMax( _max_progress );
			bar.setSecondaryProgress( _tmp_progress );

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

	private void updateStats()
	{
		( (TextView)findViewById( R.id.doit_writes ) ).setText(
			"" + _count_writes );
		( (TextView)findViewById( R.id.doit_skips ) ).setText(
			"" + _count_skips );
	}

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

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

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