/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/Doit.java

  • Committer: edam
  • Date: 2010-10-22 18:56:17 UTC
  • Revision ID: edam@waxworlds.org-20101022185617-6p6ivjqt28avwlzo
- added file chooser
- changed directory edit box for a directory "browse" button
- wired file chooser in to configure vcf page
- fixed intro
- made app debuggable
- added licence and changelog

Show diffs side-by-side

added added

removed removed

1
 
/*
2
 
 * Doit.java
3
 
 *
4
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
5
 
 *
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
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
 
 
24
 
package am.ed.exportcontacts;
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.View;
34
 
import android.widget.Button;
35
 
import android.widget.ProgressBar;
36
 
import android.widget.TextView;
37
 
import android.widget.Toast;
38
 
 
39
 
public class Doit extends WizardActivity
40
 
{
41
 
        private final static int DIALOG_ERROR = 0;
42
 
        private final static int DIALOG_CONTINUEORABORT = 1;
43
 
 
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;
54
 
 
55
 
        public final static int NEXT_BEGIN = 0;
56
 
        public final static int NEXT_CLOSE = 1;
57
 
 
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;
65
 
 
66
 
        private int _count_writes;
67
 
        private int _count_skips;
68
 
 
69
 
        protected Exporter _exporter = null;
70
 
 
71
 
        public Handler _handler;
72
 
 
73
 
        public class DoitHandler extends Handler
74
 
        {
75
 
                @Override
76
 
                public void handleMessage( Message msg ) {
77
 
                        switch( msg.what )
78
 
                        {
79
 
                        case MESSAGE_ALLDONE:
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(
85
 
                                        View.GONE );
86
 
                                break;
87
 
                        case MESSAGE_ABORT:
88
 
                                manualAbort();
89
 
                                break;
90
 
                        case MESSAGE_ERROR:
91
 
                                _dialog_message = (String)msg.obj;
92
 
                                showDialog( DIALOG_ERROR );
93
 
                                break;
94
 
                        case MESSAGE_CONTINUEORABORT:
95
 
                                _dialog_message = (String)msg.obj;
96
 
                                showDialog( DIALOG_CONTINUEORABORT );
97
 
                                break;
98
 
                        case MESSAGE_SETPROGRESSMESSAGE:
99
 
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
100
 
                                        setText( (String)msg.obj );
101
 
                                break;
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;
108
 
                                }
109
 
                                _max_progress = (Integer)msg.obj;
110
 
                                updateProgress();
111
 
                                break;
112
 
                        case MESSAGE_SETTMPPROGRESS:
113
 
                                _tmp_progress = (Integer)msg.obj;
114
 
                                updateProgress();
115
 
                                break;
116
 
                        case MESSAGE_SETPROGRESS:
117
 
                                _started_progress = true;
118
 
                                _progress = (Integer)msg.obj;
119
 
                                updateProgress();
120
 
                                break;
121
 
                        case MESSAGE_CONTACTWRITTEN:
122
 
                                _count_writes++;
123
 
                                updateStats();
124
 
                                break;
125
 
                        case MESSAGE_CONTACTSKIPPED:
126
 
                                _count_skips++;
127
 
                                updateStats();
128
 
                                break;
129
 
                        default:
130
 
                                super.handleMessage( msg );
131
 
                        }
132
 
                }
133
 
        }
134
 
 
135
 
        @Override
136
 
        protected void onCreate(Bundle saved_instance_state)
137
 
        {
138
 
                setContentView( R.layout.doit );
139
 
                super.onCreate( saved_instance_state );
140
 
 
141
 
                // hide page 2
142
 
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
143
 
 
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 ) {
148
 
                                manualAbort();
149
 
                        }
150
 
                } );
151
 
 
152
 
                _started_progress = false;
153
 
                _max_progress = 0;
154
 
                _tmp_progress = 0;
155
 
                _progress = 0;
156
 
                _handler = new DoitHandler();
157
 
 
158
 
                _count_writes = 0;
159
 
                _count_skips = 0;
160
 
 
161
 
                updateNext( NEXT_BEGIN );
162
 
 
163
 
                updateProgress();
164
 
                updateStats();
165
 
        }
166
 
 
167
 
        @Override
168
 
        protected void onPause()
169
 
        {
170
 
                super.onPause();
171
 
 
172
 
                // saving the state of an export sounds complicated! Lets just abort!
173
 
                if( _next_action != NEXT_CLOSE )
174
 
                        manualAbort( true );
175
 
        }
176
 
 
177
 
        @Override
178
 
        protected Dialog onCreateDialog( int id )
179
 
        {
180
 
                switch( id )
181
 
                {
182
 
                case DIALOG_ERROR:
183
 
                        return new AlertDialog.Builder( this )
184
 
                                .setIcon( R.drawable.alert_dialog_icon )
185
 
                                .setTitle( R.string.error_title )
186
 
                                .setMessage( "" )
187
 
                                .setPositiveButton( R.string.error_ok,
188
 
                                        new DialogInterface.OnClickListener() {
189
 
                                                public void onClick( DialogInterface dialog,
190
 
                                                        int whichButton )
191
 
                                                {
192
 
                                                        if( Doit.this != null )
193
 
                                                                Doit.this._exporter.wake();
194
 
                                                }
195
 
                                        } )
196
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
197
 
                                .create();
198
 
                case DIALOG_CONTINUEORABORT:
199
 
                        return new AlertDialog.Builder( this )
200
 
                                .setIcon( R.drawable.alert_dialog_icon )
201
 
                                .setTitle( R.string.error_title )
202
 
                                .setMessage( "" )
203
 
                                .setPositiveButton( R.string.error_continue,
204
 
                                        new DialogInterface.OnClickListener() {
205
 
                                                public void onClick( DialogInterface dialog,
206
 
                                                        int which_button )
207
 
                                                {
208
 
                                                        if( Doit.this != null )
209
 
                                                                Doit.this._exporter.wake(
210
 
                                                                        Exporter.RESPONSE_POSITIVE );
211
 
                                                }
212
 
                                        } )
213
 
                                .setNegativeButton( R.string.error_abort,
214
 
                                        new DialogInterface.OnClickListener() {
215
 
                                                public void onClick( DialogInterface dialog,
216
 
                                                        int which_button )
217
 
                                                {
218
 
                                                        if( Doit.this != null )
219
 
                                                                Doit.this._exporter.wake(
220
 
                                                                        Exporter.RESPONSE_NEGATIVE );
221
 
                                                }
222
 
                                        } )
223
 
                                .setOnCancelListener( _dialog_on_cancel_listener )
224
 
                                .create();
225
 
                }
226
 
                return null;
227
 
        }
228
 
 
229
 
        @Override
230
 
        protected void onNext()
231
 
        {
232
 
                Button next = (Button)findViewById( R.id.next );
233
 
                next.setEnabled( false );
234
 
 
235
 
                switch( _next_action )
236
 
                {
237
 
                case NEXT_BEGIN:
238
 
                        exportContacts();
239
 
                        break;
240
 
                case NEXT_CLOSE:
241
 
                        setResult( RESULT_OK );
242
 
                        finish();
243
 
                        break;
244
 
                }
245
 
        }
246
 
 
247
 
        private void manualAbort()
248
 
        {
249
 
                manualAbort( false );
250
 
        }
251
 
 
252
 
        private void manualAbort( boolean show_toaster_popup )
253
 
        {
254
 
                abortExport( show_toaster_popup );
255
 
 
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 );
263
 
 
264
 
                // close any open dialogs
265
 
                try {
266
 
                        dismissDialog( _current_dialog_id );
267
 
                }
268
 
                catch( Exception e ) {
269
 
                        // ignore errors
270
 
                }
271
 
        }
272
 
 
273
 
        private void updateNext( int next_action )
274
 
        {
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;
279
 
                }
280
 
                next.setEnabled( true );
281
 
                _next_action = next_action;
282
 
        }
283
 
 
284
 
        private DialogInterface.OnCancelListener _dialog_on_cancel_listener =
285
 
                new DialogInterface.OnCancelListener()
286
 
        {
287
 
                public void onCancel( DialogInterface dialog ) {
288
 
                        manualAbort();
289
 
                }
290
 
        };
291
 
 
292
 
 
293
 
        @Override
294
 
        protected void onActivityResult( int request_code, int result_code,
295
 
                Intent data )
296
 
        {
297
 
                // if we're cancelling, abort any export
298
 
                if( result_code == RESULT_CANCELED )
299
 
                        abortExport( true );
300
 
        }
301
 
 
302
 
        @Override
303
 
        protected void onPrepareDialog( int id, Dialog dialog )
304
 
        {
305
 
                _current_dialog_id = id;
306
 
 
307
 
                switch( id )
308
 
                {
309
 
                case DIALOG_ERROR:      // fall through
310
 
                case DIALOG_CONTINUEORABORT:
311
 
                        // set dialog message
312
 
                        ( (AlertDialog)dialog ).setMessage( _dialog_message );
313
 
                        break;
314
 
                }
315
 
 
316
 
                super.onPrepareDialog( id, dialog );
317
 
        }
318
 
 
319
 
        private void exportContacts()
320
 
        {
321
 
                // switch interfaces
322
 
                ( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
323
 
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
324
 
 
325
 
                // disable back button
326
 
                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
327
 
 
328
 
                // create exporter
329
 
                _exporter = new VcardExporter( this );
330
 
 
331
 
                // start the service's thread
332
 
                _exporter.start();
333
 
        }
334
 
 
335
 
        private void updateProgress()
336
 
        {
337
 
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
338
 
                TextView out_of = (TextView)findViewById( R.id.doit_outof );
339
 
 
340
 
                if( _max_progress > 0 )
341
 
                {
342
 
                        bar.setMax( _max_progress );
343
 
                        bar.setSecondaryProgress( _tmp_progress );
344
 
 
345
 
                        if( _started_progress )
346
 
                        {
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 );
351
 
                        }
352
 
                }
353
 
        }
354
 
 
355
 
        private void updateStats()
356
 
        {
357
 
                ( (TextView)findViewById( R.id.doit_writes ) ).setText(
358
 
                        "" + _count_writes );
359
 
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
360
 
                        "" + _count_skips );
361
 
        }
362
 
 
363
 
        private void abortExport( boolean show_toaster_popup )
364
 
        {
365
 
                if( _exporter != null )
366
 
                {
367
 
                        // try and flag worker thread - did we need to?
368
 
                        if( _exporter.setAbort() )
369
 
                        {
370
 
                                // wait for worker thread to end
371
 
                                while( true ) {
372
 
                                        try {
373
 
                                                _exporter.join();
374
 
                                                break;
375
 
                                        }
376
 
                                        catch( InterruptedException e ) {}
377
 
                                }
378
 
 
379
 
                                // notify the user
380
 
                                if( show_toaster_popup )
381
 
                                        Toast.makeText( this, R.string.doit_exportaborted,
382
 
                                                Toast.LENGTH_LONG ).show();
383
 
                        }
384
 
                }
385
 
 
386
 
                // destroy some stuff
387
 
                _exporter = null;
388
 
                _handler = null;
389
 
        }
390
 
}