/android/import-contacts

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

« back to all changes in this revision

Viewing changes to src/org/waxworlds/edam/importcontacts/Doit.java

  • Committer: edam
  • Date: 2010-10-28 16:12:57 UTC
  • Revision ID: edam@waxworlds.org-20101028161257-9stn23tn7tvkkjno
- fixed bug where a file chooser wouldn't know it's context when the context was used

Show diffs side-by-side

added added

removed removed

1
 
package org.waxworlds.importcontacts;
 
1
/*
 
2
 * Doit.java
 
3
 *
 
4
 * Copyright (C) 2009 Tim Marston <edam@waxworlds.org>
 
5
 *
 
6
 * This file is part of the Import Contacts program (hereafter referred
 
7
 * to as "this program"). For more information, see
 
8
 * http://www.waxworlds.org/edam/software/android/import-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 org.waxworlds.edam.importcontacts;
2
25
 
3
26
import android.app.AlertDialog;
4
27
import android.app.Dialog;
13
36
import android.widget.Button;
14
37
import android.widget.CheckBox;
15
38
import android.widget.CompoundButton;
16
 
import android.widget.LinearLayout;
17
39
import android.widget.ProgressBar;
18
40
import android.widget.TextView;
 
41
import android.widget.Toast;
19
42
 
20
43
public class Doit extends WizardActivity
21
44
{
23
46
        private final static int DIALOG_CONTINUEORABORT = 1;
24
47
        private final static int DIALOG_MERGEPROMPT = 2;
25
48
 
 
49
        public final static int MESSAGE_ALLDONE = 0;
 
50
        public final static int MESSAGE_ABORT = 1;
 
51
        public final static int MESSAGE_ERROR = 3;
 
52
        public final static int MESSAGE_CONTINUEORABORT = 4;
 
53
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
 
54
        public final static int MESSAGE_SETMAXPROGRESS = 6;
 
55
        public final static int MESSAGE_SETTMPPROGRESS = 7;
 
56
        public final static int MESSAGE_SETPROGRESS = 8;
 
57
        public final static int MESSAGE_MERGEPROMPT = 9;
 
58
        public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
 
59
        public final static int MESSAGE_CONTACTCREATED = 11;
 
60
        public final static int MESSAGE_CONTACTMERGED = 12;
 
61
        public final static int MESSAGE_CONTACTSKIPPED = 13;
 
62
 
 
63
        public final static int ACTION_PROMPT = 0;
 
64
        public final static int ACTION_KEEP = 1;
 
65
        public final static int ACTION_MERGE_MERGE = 2;
 
66
        public final static int ACTION_OVERWRITE = 3;
 
67
 
 
68
        public final static int NEXT_BEGIN = 0;
 
69
        public final static int NEXT_CLOSE = 1;
 
70
 
26
71
        private boolean _startedProgress;
27
72
        private int _maxProgress;
28
73
        private int _tmpProgress;
30
75
        protected String _dialogMessage;
31
76
        private Dialog _mergePromptDialog;
32
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
33
80
 
34
81
        private int _countOverwrites;
35
82
        private int _countCreates;
36
83
        private int _countMerges;
37
84
        private int _countSkips;
38
85
 
39
 
        protected Importer _importer;
 
86
        protected Importer _importer = null;
40
87
 
41
88
        public Handler _handler;
42
89
 
46
93
                public void handleMessage( Message msg ) {
47
94
                        switch( msg.what )
48
95
                        {
49
 
                        case Importer.MESSAGE_FINISHED:
50
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
51
 
                                                setVisibility( View.VISIBLE );
52
 
                                break;
53
 
                        case Importer.MESSAGE_FINISHED_BACK:
54
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
55
 
                                break;
56
 
                        case Importer.MESSAGE_ERROR:
57
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
96
                        case MESSAGE_ALLDONE:
 
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
98
                                        setVisibility( View.VISIBLE );
 
99
                                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
100
                                updateNext( NEXT_CLOSE );
 
101
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
102
                                                View.GONE );
 
103
                                break;
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
 
106
                                break;
 
107
                        case MESSAGE_ERROR:
 
108
                                _dialogMessage = (String)msg.obj;
58
109
                                showDialog( DIALOG_ERROR );
59
110
                                break;
60
 
                        case Importer.MESSAGE_CONTINUEORABORT:
61
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
111
                        case MESSAGE_CONTINUEORABORT:
 
112
                                _dialogMessage = (String)msg.obj;
62
113
                                showDialog( DIALOG_CONTINUEORABORT );
63
114
                                break;
64
 
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
 
115
                        case MESSAGE_SETPROGRESSMESSAGE:
65
116
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
66
117
                                                setText( (String)msg.obj );
67
118
                                break;
68
 
                        case Importer.MESSAGE_SETMAXPROGRESS:
 
119
                        case MESSAGE_SETMAXPROGRESS:
69
120
                                if( _maxProgress > 0 ) {
70
 
                                        if( _tmpProgress == _maxProgress )
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
71
122
                                                _tmpProgress = (Integer)msg.obj;
72
 
                                        if( _progress == _maxProgress )
 
123
                                        if( _progress == _maxProgress - 1 )
73
124
                                                _progress = (Integer)msg.obj;
74
125
                                }
75
126
                                _maxProgress = (Integer)msg.obj;
76
127
                                updateProgress();
77
128
                                break;
78
 
                        case Importer.MESSAGE_SETTMPPROGRESS:
 
129
                        case MESSAGE_SETTMPPROGRESS:
79
130
                                _tmpProgress = (Integer)msg.obj;
80
131
                                updateProgress();
81
132
                                break;
82
 
                        case Importer.MESSAGE_SETPROGRESS:
 
133
                        case MESSAGE_SETPROGRESS:
83
134
                                _startedProgress = true;
84
135
                                _progress = (Integer)msg.obj;
85
136
                                updateProgress();
86
137
                                break;
87
 
                        case Importer.MESSAGE_MERGEPROMPT:
 
138
                        case MESSAGE_MERGEPROMPT:
88
139
                                _dialogMessage = (String)msg.obj;
89
140
                                showDialog( DIALOG_MERGEPROMPT );
90
141
                                break;
91
 
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
 
142
                        case MESSAGE_CONTACTOVERWRITTEN:
92
143
                                _countOverwrites++;
93
144
                                updateStats();
94
145
                                break;
95
 
                        case Importer.MESSAGE_CONTACTCREATED:
 
146
                        case MESSAGE_CONTACTCREATED:
96
147
                                _countCreates++;
97
148
                                updateStats();
98
149
                                break;
99
 
                        case Importer.MESSAGE_CONTACTMERGED:
 
150
                        case MESSAGE_CONTACTMERGED:
100
151
                                _countMerges++;
101
152
                                updateStats();
102
153
                                break;
103
 
                        case Importer.MESSAGE_CONTACTSKIPPED:
 
154
                        case MESSAGE_CONTACTSKIPPED:
104
155
                                _countSkips++;
105
156
                                updateStats();
106
157
                                break;
119
170
                // hide page 2
120
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
121
172
 
122
 
                // set up begin button
123
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
173
                // set up abort button
 
174
                Button begin = (Button)findViewById( R.id.abort );
124
175
                begin.setOnClickListener( new View.OnClickListener() {
125
176
                        public void onClick( View view ) {
126
 
                                importContacts();
127
 
                        }
128
 
                } );
129
 
 
130
 
                // set up close button
131
 
                Button close = (Button)findViewById( R.id.doit_close );
132
 
                close.setOnClickListener( new View.OnClickListener() {
133
 
                        public void onClick( View view ) {
134
 
                                setResult( RESULT_CANCELED );
135
 
                                finish();
136
 
                        }
137
 
                } );
138
 
 
139
 
 
 
177
                                manualAbort();
 
178
                        }
 
179
                } );
140
180
 
141
181
                _startedProgress = false;
142
182
                _maxProgress = 0;
149
189
                _countMerges = 0;
150
190
                _countSkips = 0;
151
191
 
 
192
                updateNext( NEXT_BEGIN );
 
193
 
152
194
                updateProgress();
153
195
                updateStats();
154
196
        }
158
200
        {
159
201
                super.onPause();
160
202
 
161
 
                abortImport();
162
 
 
163
 
                // notify the user
164
 
 
165
 
 
 
203
                // saving the state of an import sounds complicated! Lets just abort!
 
204
                if( _nextAction != NEXT_CLOSE )
 
205
                        manualAbort( true );
166
206
        }
167
207
 
168
208
        @Override
169
 
        protected Dialog onCreateDialog(int id)
 
209
        protected Dialog onCreateDialog( int id )
170
210
        {
171
211
                switch( id )
172
212
                {
173
213
                case DIALOG_ERROR:
174
214
                        return new AlertDialog.Builder( this )
175
 
                                        .setIcon( R.drawable.alert_dialog_icon )
176
 
                                        .setTitle( R.string.error_title )
177
 
                                        .setMessage( "" )
178
 
                                        .setPositiveButton( R.string.error_ok,
179
 
                                                        new DialogInterface.OnClickListener() {
 
215
                                .setIcon( R.drawable.alert_dialog_icon )
 
216
                                .setTitle( R.string.error_title )
 
217
                                .setMessage( "" )
 
218
                                .setPositiveButton( R.string.error_ok,
 
219
                                        new DialogInterface.OnClickListener() {
180
220
                                                public void onClick(DialogInterface dialog,
181
 
                                                                int whichButton) {
 
221
                                                        int whichButton)
 
222
                                                {
182
223
                                                        Doit.this._importer.wake();
183
224
                                                }
184
225
                                        } )
185
 
                                        .setOnCancelListener( _dialogOnCancelListener )
186
 
                                        .create();
 
226
                                .setOnCancelListener( _dialogOnCancelListener )
 
227
                                .create();
187
228
                case DIALOG_CONTINUEORABORT:
188
229
                        return new AlertDialog.Builder( this )
189
 
                                        .setIcon( R.drawable.alert_dialog_icon )
190
 
                                        .setTitle( R.string.error_title )
191
 
                                        .setMessage( "" )
192
 
                                        .setPositiveButton( R.string.error_continue,
193
 
                                                        new DialogInterface.OnClickListener() {
194
 
                                                public void onClick(DialogInterface dialog,
195
 
                                                                int whichButton) {
196
 
                                                        Doit.this._importer.wake(
197
 
                                                                        Importer.RESPONSE_POSITIVE );
198
 
                                                }
199
 
                                        } )
200
 
                                        .setNegativeButton( R.string.error_abort,
201
 
                                                        new DialogInterface.OnClickListener() {
202
 
                                                public void onClick(DialogInterface dialog,
203
 
                                                                int whichButton) {
204
 
                                                        Doit.this._importer.wake(
205
 
                                                                        Importer.RESPONSE_NEGATIVE );
206
 
                                                }
207
 
                                        } )
208
 
                                        .setOnCancelListener( _dialogOnCancelListener )
209
 
                                        .create();
 
230
                                .setIcon( R.drawable.alert_dialog_icon )
 
231
                                .setTitle( R.string.error_title )
 
232
                                .setMessage( "" )
 
233
                                .setPositiveButton( R.string.error_continue,
 
234
                                        new DialogInterface.OnClickListener() {
 
235
                                                public void onClick(DialogInterface dialog,
 
236
                                                        int whichButton)
 
237
                                                {
 
238
                                                        Doit.this._importer.wake(
 
239
                                                                Importer.RESPONSE_POSITIVE );
 
240
                                                }
 
241
                                        } )
 
242
                                .setNegativeButton( R.string.error_abort,
 
243
                                        new DialogInterface.OnClickListener() {
 
244
                                                public void onClick(DialogInterface dialog,
 
245
                                                        int whichButton)
 
246
                                                {
 
247
                                                        Doit.this._importer.wake(
 
248
                                                                Importer.RESPONSE_NEGATIVE );
 
249
                                                }
 
250
                                        } )
 
251
                                .setOnCancelListener( _dialogOnCancelListener )
 
252
                                .create();
210
253
                case DIALOG_MERGEPROMPT:
211
254
                        // custom layout in an AlertDialog
212
255
                        LayoutInflater factory = LayoutInflater.from( this );
213
256
                        final View dialogView = factory.inflate(
214
 
                                        R.layout.mergeprompt, null );
 
257
                                R.layout.mergeprompt, null );
215
258
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
216
 
                                        setOnCheckedChangeListener(
217
 
                                                        new CompoundButton.OnCheckedChangeListener() {
218
 
                                public void onCheckedChanged( CompoundButton buttonView,
219
 
                                                boolean isChecked ) {
220
 
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
221
 
                                }
222
 
                        } );
 
259
                                setOnCheckedChangeListener(
 
260
                                        new CompoundButton.OnCheckedChangeListener() {
 
261
                                                public void onCheckedChanged( CompoundButton buttonView,
 
262
                                                        boolean isChecked )
 
263
                                                {
 
264
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
265
                                                }
 
266
                                        } );
223
267
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
224
 
                                        setOnClickListener( _mergePromptButtonListener );
 
268
                                setOnClickListener( _mergePromptButtonListener );
225
269
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
226
 
                                        setOnClickListener( _mergePromptButtonListener );
 
270
                                setOnClickListener( _mergePromptButtonListener );
227
271
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
228
 
                                        setOnClickListener( _mergePromptButtonListener );
 
272
                                setOnClickListener( _mergePromptButtonListener );
 
273
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
274
                                setOnClickListener( _mergePromptButtonListener );
229
275
                        _mergePromptAlwaysSelected = false;
230
276
                        return new AlertDialog.Builder( this )
231
 
                                        .setIcon( R.drawable.alert_dialog_icon )
232
 
                                        .setTitle( R.string.mergeprompt_title )
233
 
                                        .setView( dialogView )
234
 
                                        .setOnCancelListener( _dialogOnCancelListener )
235
 
                                        .create();
 
277
                                .setIcon( R.drawable.alert_dialog_icon )
 
278
                                .setTitle( R.string.mergeprompt_title )
 
279
                                .setView( dialogView )
 
280
                                .setOnCancelListener( _dialogOnCancelListener )
 
281
                                .create();
236
282
                }
237
283
                return null;
238
284
        }
239
285
 
240
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
241
 
                public void onClick( View view ) {
242
 
                        int responseExtra = _mergePromptAlwaysSelected?
243
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
286
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
287
                public void onClick( View view )
 
288
                {
 
289
                        // handle abort
 
290
                        if( view.getId() == R.id.abort )
 
291
                                manualAbort();
 
292
 
 
293
                        // else, response (just check we haven't aborted already!)
 
294
                        else if( Doit.this._importer != null ) {
 
295
                                int responseExtra = _mergePromptAlwaysSelected?
 
296
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
297
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
298
                                                responseExtra );
 
299
                        }
 
300
 
 
301
                        // close dialog and free (don't keep a reference)
244
302
                        Doit.this._mergePromptDialog.dismiss();
245
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
246
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
303
                        Doit.this._mergePromptDialog = null;
247
304
                }
248
305
        };
249
306
 
 
307
        @Override
 
308
        protected void onNext()
 
309
        {
 
310
                Button next = (Button)findViewById( R.id.next );
 
311
                next.setEnabled( false );
 
312
 
 
313
                switch( _nextAction )
 
314
                {
 
315
                case NEXT_BEGIN:
 
316
                        importContacts();
 
317
                        break;
 
318
                case NEXT_CLOSE:
 
319
                        setResult( RESULT_OK );
 
320
                        finish();
 
321
                        break;
 
322
                }
 
323
        }
 
324
 
 
325
        private void manualAbort()
 
326
        {
 
327
                manualAbort( false );
 
328
        }
 
329
 
 
330
        private void manualAbort( boolean showToasterPopup )
 
331
        {
 
332
                abortImport( showToasterPopup );
 
333
 
 
334
                updateNext( NEXT_CLOSE );
 
335
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
336
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
 
337
                ( (TextView)findViewById( R.id.doit_aborted ) ).
 
338
                        setVisibility( View.VISIBLE );
 
339
                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
340
                        setVisibility( View.GONE );
 
341
 
 
342
                // close any open dialogs
 
343
                try {
 
344
                        dismissDialog( _currentDialogId );
 
345
                }
 
346
                catch( Exception e ) {
 
347
                }
 
348
        }
 
349
 
 
350
        private void updateNext( int nextAction )
 
351
        {
 
352
                Button next = (Button)findViewById( R.id.next );
 
353
                switch( nextAction ) {
 
354
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
355
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
356
                }
 
357
                next.setEnabled( true );
 
358
                _nextAction = nextAction;
 
359
        }
 
360
 
 
361
        public static int convertIdToAction( int id ) {
 
362
                switch( id ) {
 
363
                case R.id.merge_keep:           return ACTION_KEEP;
 
364
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
365
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
366
                default: return ACTION_PROMPT;
 
367
                }
 
368
        }
 
369
 
 
370
        public static int convertActionToId( int action ) {
 
371
                switch( action ) {
 
372
                case ACTION_KEEP:               return R.id.merge_keep;
 
373
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
374
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
375
                default: return R.id.merge_prompt;
 
376
                }
 
377
        }
 
378
 
250
379
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
380
                        new DialogInterface.OnCancelListener() {
252
381
                public void onCancel( DialogInterface dialog ) {
253
 
                        setResult( RESULT_CANCELED );
254
 
                        finish();
 
382
                        manualAbort();
255
383
                }
256
384
        };
257
385
 
 
386
 
258
387
        @Override
259
388
        protected void onActivityResult( int requestCode, int resultCode,
260
389
                        Intent data )
261
390
        {
 
391
                // if we're cancelling, abort any import
262
392
                if( resultCode == RESULT_CANCELED )
263
 
                        abortImport();
 
393
                        abortImport( true );
264
394
        }
265
395
 
266
396
        @Override
267
397
        protected void onPrepareDialog(int id, Dialog dialog)
268
398
        {
 
399
                _currentDialogId = id;
 
400
 
269
401
                switch( id )
270
402
                {
271
403
                case DIALOG_ERROR:      // fall through
333
465
                                "" + _countSkips );
334
466
        }
335
467
 
336
 
        private void abortImport()
 
468
        private void abortImport( boolean showToasterPopup )
337
469
        {
338
470
                if( _importer != null )
339
471
                {
340
 
                        _importer.setAbort();
341
 
                        while( true ) {
342
 
                                try {
343
 
                                        _importer.join();
344
 
                                        break;
 
472
                        // try and flag worker thread - did we need to?
 
473
                        if( _importer.setAbort() )
 
474
                        {
 
475
                                // wait for worker thread to end
 
476
                                while( true ) {
 
477
                                        try {
 
478
                                                _importer.join();
 
479
                                                break;
 
480
                                        }
 
481
                                        catch( InterruptedException e ) {}
345
482
                                }
346
 
                                catch( InterruptedException e ) {}
 
483
 
 
484
                                // notify the user
 
485
                                if( showToasterPopup )
 
486
                                        Toast.makeText( this, R.string.doit_importaborted,
 
487
                                                        Toast.LENGTH_LONG ).show();
347
488
                        }
348
489
                }
 
490
 
 
491
                // destroy some stuff
 
492
                _importer = null;
 
493
                _handler = null;
349
494
        }
350
495
}