4
* Copyright (C) 2011 Tim Marston <tim@ed.am>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/export-contacts
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.
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.
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/>.
24
package am.ed.exportcontacts;
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.View;
34
import android.widget.Button;
35
import android.widget.ProgressBar;
36
import android.widget.TextView;
37
import android.widget.Toast;
39
public class Doit extends WizardActivity
41
private final static int DIALOG_ERROR = 0;
42
private final static int DIALOG_CONTINUEORABORT = 1;
44
public final static int MESSAGE_ALLDONE = 0;
45
public final static int MESSAGE_ABORT = 1;
46
public final static int MESSAGE_ERROR = 2;
47
public final static int MESSAGE_CONTINUEORABORT = 3;
48
public final static int MESSAGE_SETPROGRESSMESSAGE = 4;
49
public final static int MESSAGE_SETMAXPROGRESS = 5;
50
public final static int MESSAGE_SETTMPPROGRESS = 6;
51
public final static int MESSAGE_SETPROGRESS = 7;
52
public final static int MESSAGE_CONTACTWRITTEN = 8;
53
public final static int MESSAGE_CONTACTSKIPPED = 9;
55
public final static int NEXT_BEGIN = 0;
56
public final static int NEXT_CLOSE = 1;
58
private boolean _started_progress;
59
private int _max_progress;
60
private int _tmp_progress;
61
private int _progress;
62
protected String _dialog_message;
63
private int _next_action;
64
private int _current_dialog_id;
66
private int _count_writes;
67
private int _count_skips;
69
protected Exporter _exporter = null;
71
public Handler _handler;
73
public class DoitHandler extends Handler
76
public void handleMessage( Message msg ) {
80
( (TextView)findViewById( R.id.doit_alldone ) ).
81
setVisibility( View.VISIBLE );
82
( (Button)findViewById( R.id.back ) ).setEnabled( false );
83
updateNext( NEXT_CLOSE );
84
findViewById( R.id.doit_abort_disp ).setVisibility(
91
_dialog_message = (String)msg.obj;
92
showDialog( DIALOG_ERROR );
94
case MESSAGE_CONTINUEORABORT:
95
_dialog_message = (String)msg.obj;
96
showDialog( DIALOG_CONTINUEORABORT );
98
case MESSAGE_SETPROGRESSMESSAGE:
99
( (TextView)findViewById( R.id.doit_percentage ) ).
100
setText( (String)msg.obj );
102
case MESSAGE_SETMAXPROGRESS:
103
if( _max_progress > 0 ) {
104
if( _tmp_progress == _max_progress - 1 )
105
_tmp_progress = (Integer)msg.obj;
106
if( _progress == _max_progress - 1 )
107
_progress = (Integer)msg.obj;
109
_max_progress = (Integer)msg.obj;
112
case MESSAGE_SETTMPPROGRESS:
113
_tmp_progress = (Integer)msg.obj;
116
case MESSAGE_SETPROGRESS:
117
_started_progress = true;
118
_progress = (Integer)msg.obj;
121
case MESSAGE_CONTACTWRITTEN:
125
case MESSAGE_CONTACTSKIPPED:
130
super.handleMessage( msg );
136
protected void onCreate(Bundle saved_instance_state)
138
setContentView( R.layout.doit );
139
super.onCreate( saved_instance_state );
142
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
144
// set up abort button
145
Button begin = (Button)findViewById( R.id.abort );
146
begin.setOnClickListener( new View.OnClickListener() {
147
public void onClick( View view ) {
152
_started_progress = false;
156
_handler = new DoitHandler();
161
updateNext( NEXT_BEGIN );
168
protected void onPause()
172
// saving the state of an export sounds complicated! Lets just abort!
173
if( _next_action != NEXT_CLOSE )
178
protected Dialog onCreateDialog( int id )
183
return new AlertDialog.Builder( this )
184
.setIcon( R.drawable.alert_dialog_icon )
185
.setTitle( R.string.error_title )
187
.setPositiveButton( R.string.error_ok,
188
new DialogInterface.OnClickListener() {
189
public void onClick( DialogInterface dialog,
192
if( Doit.this != null )
193
Doit.this._exporter.wake();
196
.setOnCancelListener( _dialog_on_cancel_listener )
198
case DIALOG_CONTINUEORABORT:
199
return new AlertDialog.Builder( this )
200
.setIcon( R.drawable.alert_dialog_icon )
201
.setTitle( R.string.error_title )
203
.setPositiveButton( R.string.error_continue,
204
new DialogInterface.OnClickListener() {
205
public void onClick( DialogInterface dialog,
208
if( Doit.this != null )
209
Doit.this._exporter.wake(
210
Exporter.RESPONSE_POSITIVE );
213
.setNegativeButton( R.string.error_abort,
214
new DialogInterface.OnClickListener() {
215
public void onClick( DialogInterface dialog,
218
if( Doit.this != null )
219
Doit.this._exporter.wake(
220
Exporter.RESPONSE_NEGATIVE );
223
.setOnCancelListener( _dialog_on_cancel_listener )
230
protected void onNext()
232
Button next = (Button)findViewById( R.id.next );
233
next.setEnabled( false );
235
switch( _next_action )
241
setResult( RESULT_OK );
247
private void manualAbort()
249
manualAbort( false );
252
private void manualAbort( boolean show_toaster_popup )
254
abortExport( show_toaster_popup );
256
updateNext( NEXT_CLOSE );
257
( (Button)findViewById( R.id.back ) ).setEnabled( true );
258
findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
259
( (TextView)findViewById( R.id.doit_aborted ) ).
260
setVisibility( View.VISIBLE );
261
( (TextView)findViewById( R.id.doit_alldone ) ).
262
setVisibility( View.GONE );
264
// close any open dialogs
266
dismissDialog( _current_dialog_id );
268
catch( Exception e ) {
273
private void updateNext( int next_action )
275
Button next = (Button)findViewById( R.id.next );
276
switch( next_action ) {
277
case NEXT_BEGIN: next.setText( R.string.doit_begin ); break;
278
case NEXT_CLOSE: next.setText( R.string.doit_close ); break;
280
next.setEnabled( true );
281
_next_action = next_action;
284
private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
285
new DialogInterface.OnCancelListener()
287
public void onCancel( DialogInterface dialog ) {
294
protected void onActivityResult( int request_code, int result_code,
297
// if we're cancelling, abort any export
298
if( result_code == RESULT_CANCELED )
303
protected void onPrepareDialog( int id, Dialog dialog )
305
_current_dialog_id = id;
309
case DIALOG_ERROR: // fall through
310
case DIALOG_CONTINUEORABORT:
311
// set dialog message
312
( (AlertDialog)dialog ).setMessage( _dialog_message );
316
super.onPrepareDialog( id, dialog );
319
private void exportContacts()
322
( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
323
( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
325
// disable back button
326
( (Button)findViewById( R.id.back ) ).setEnabled( false );
329
_exporter = new VcardExporter( this );
331
// start the service's thread
335
private void updateProgress()
337
ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
338
TextView out_of = (TextView)findViewById( R.id.doit_outof );
340
if( _max_progress > 0 )
342
bar.setMax( _max_progress );
343
bar.setSecondaryProgress( _tmp_progress );
345
if( _started_progress )
347
( (TextView)findViewById( R.id.doit_percentage ) ).setText(
348
(int)Math.round( 100 * _progress / _max_progress ) + "%" );
349
out_of.setText( _progress + "/" + _max_progress );
350
bar.setProgress( _progress );
355
private void updateStats()
357
( (TextView)findViewById( R.id.doit_writes ) ).setText(
358
"" + _count_writes );
359
( (TextView)findViewById( R.id.doit_skips ) ).setText(
363
private void abortExport( boolean show_toaster_popup )
365
if( _exporter != null )
367
// try and flag worker thread - did we need to?
368
if( _exporter.setAbort() )
370
// wait for worker thread to end
376
catch( InterruptedException e ) {}
380
if( show_toaster_popup )
381
Toast.makeText( this, R.string.doit_exportaborted,
382
Toast.LENGTH_LONG ).show();
386
// destroy some stuff