/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/importcontacts/Doit.java

  • Committer: edam
  • Date: 2009-01-28 19:55:17 UTC
  • Revision ID: edam@waxworlds.org-20090128195517-z1rmmys8tc8zvgqe
- made behaviour of next button overridable
- added abort button to merge prompt
- moved begin and close buttons to the next button, iverriding its behaviour
- added abort button to import and wired it up
- bugfix: tmp-progress bar didn't fill up when progress max was changed

Show diffs side-by-side

added added

removed removed

 
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
 
1
24
package org.waxworlds.importcontacts;
2
25
 
3
26
import android.app.AlertDialog;
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;
19
41
import android.widget.Toast;
39
61
        public final static int MESSAGE_CONTACTMERGED = 12;
40
62
        public final static int MESSAGE_CONTACTSKIPPED = 13;
41
63
 
 
64
        public final static int ACTION_PROMPT = 0;
 
65
        public final static int ACTION_KEEP = 1;
 
66
        public final static int ACTION_MERGE_MERGE = 2;
 
67
        public final static int ACTION_OVERWRITE = 3;
 
68
 
 
69
        public final static int NEXT_BEGIN = 0;
 
70
        public final static int NEXT_CLOSE = 1;
 
71
 
42
72
        private boolean _startedProgress;
43
73
        private int _maxProgress;
44
74
        private int _tmpProgress;
46
76
        protected String _dialogMessage;
47
77
        private Dialog _mergePromptDialog;
48
78
        private boolean _mergePromptAlwaysSelected;
 
79
        private int _nextAction;
49
80
 
50
81
        private int _countOverwrites;
51
82
        private int _countCreates;
67
98
                                                setVisibility( View.VISIBLE );
68
99
                                // fall through
69
100
                        case MESSAGE_FINISHED:
70
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
71
 
                                                setVisibility( View.VISIBLE );
 
101
                                updateNext( NEXT_CLOSE );
 
102
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
103
                                                View.GONE );
72
104
                                break;
73
105
                        case MESSAGE_FINISHED_GOBACK:
74
106
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
107
                                findViewById( R.id.doit_abort_disp ).setVisibility(
 
108
                                                View.GONE );
75
109
                                break;
76
110
                        case MESSAGE_ERROR:
77
111
                                Doit.this._dialogMessage = (String)msg.obj;
87
121
                                break;
88
122
                        case MESSAGE_SETMAXPROGRESS:
89
123
                                if( _maxProgress > 0 ) {
90
 
                                        if( _tmpProgress == _maxProgress )
 
124
                                        if( _tmpProgress == _maxProgress - 1 )
91
125
                                                _tmpProgress = (Integer)msg.obj;
92
 
                                        if( _progress == _maxProgress )
 
126
                                        if( _progress == _maxProgress - 1 )
93
127
                                                _progress = (Integer)msg.obj;
94
128
                                }
95
129
                                _maxProgress = (Integer)msg.obj;
139
173
                // hide page 2
140
174
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
141
175
 
142
 
                // set up begin button
143
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
176
                // set up abort button
 
177
                Button begin = (Button)findViewById( R.id.abort );
144
178
                begin.setOnClickListener( new View.OnClickListener() {
145
179
                        public void onClick( View view ) {
146
 
                                importContacts();
147
 
                        }
148
 
                } );
149
 
 
150
 
                // set up close button
151
 
                Button close = (Button)findViewById( R.id.doit_close );
152
 
                close.setOnClickListener( new View.OnClickListener() {
153
 
                        public void onClick( View view ) {
154
 
                                setResult( RESULT_CANCELED );
155
 
                                finish();
 
180
                                manualAbort();
156
181
                        }
157
182
                } );
158
183
 
167
192
                _countMerges = 0;
168
193
                _countSkips = 0;
169
194
 
 
195
                updateNext( NEXT_BEGIN );
 
196
 
170
197
                updateProgress();
171
198
                updateStats();
172
199
        }
177
204
                super.onPause();
178
205
 
179
206
                // saving the state of an import sounds complicated! Lets just abort!
180
 
                abortImport();
181
 
 
182
 
                // destroy some stuff
183
 
                _importer = null;
184
 
                _handler = null;
 
207
                abortImport( true );
185
208
        }
186
209
 
187
210
        @Override
245
268
                                        setOnClickListener( _mergePromptButtonListener );
246
269
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
247
270
                                        setOnClickListener( _mergePromptButtonListener );
 
271
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
272
                                        setOnClickListener( _mergePromptButtonListener );
248
273
                        _mergePromptAlwaysSelected = false;
249
274
                        return new AlertDialog.Builder( this )
250
275
                                        .setIcon( R.drawable.alert_dialog_icon )
257
282
        }
258
283
 
259
284
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
260
 
                public void onClick( View view ) {
261
 
                        int responseExtra = _mergePromptAlwaysSelected?
262
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
285
                public void onClick( View view )
 
286
                {
 
287
                        // handle abort
 
288
                        if( view.getId() == R.id.abort )
 
289
                                manualAbort();
 
290
 
 
291
                        // else, response
 
292
                        else {
 
293
                                int responseExtra = _mergePromptAlwaysSelected?
 
294
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
295
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
296
                                                responseExtra );
 
297
                        }
 
298
 
 
299
                        // close dialog and free (don't keep a reference)
263
300
                        Doit.this._mergePromptDialog.dismiss();
264
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
265
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
301
                        Doit.this._mergePromptDialog = null;
266
302
                }
267
303
        };
268
304
 
 
305
        @Override
 
306
        protected void onNext()
 
307
        {
 
308
                Button next = (Button)findViewById( R.id.next );
 
309
                next.setEnabled( false );
 
310
 
 
311
                switch( _nextAction )
 
312
                {
 
313
                case NEXT_BEGIN:
 
314
                        importContacts();
 
315
                        break;
 
316
                case NEXT_CLOSE:
 
317
                        setResult( RESULT_CANCELED );
 
318
                        finish();
 
319
                        break;
 
320
                }
 
321
        }
 
322
 
 
323
        private void manualAbort()
 
324
        {
 
325
                abortImport( false );
 
326
                updateNext( NEXT_CLOSE );
 
327
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
328
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
 
329
        }
 
330
 
 
331
        private void updateNext( int nextAction )
 
332
        {
 
333
                Button next = (Button)findViewById( R.id.next );
 
334
                switch( nextAction ) {
 
335
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
336
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
337
                }
 
338
                next.setEnabled( true );
 
339
                _nextAction = nextAction;
 
340
        }
 
341
 
 
342
        public static int convertIdToAction( int id ) {
 
343
                switch( id ) {
 
344
                case R.id.merge_keep:           return ACTION_KEEP;
 
345
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
346
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
347
                default: return ACTION_PROMPT;
 
348
                }
 
349
        }
 
350
 
 
351
        public static int convertActionToId( int action ) {
 
352
                switch( action ) {
 
353
                case ACTION_KEEP:               return R.id.merge_keep;
 
354
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
355
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
356
                default: return R.id.merge_prompt;
 
357
                }
 
358
        }
 
359
 
269
360
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
270
361
                        new DialogInterface.OnCancelListener() {
271
362
                public void onCancel( DialogInterface dialog ) {
280
371
        {
281
372
                // if we're cancelling, abort any import
282
373
                if( resultCode == RESULT_CANCELED )
283
 
                        abortImport();
 
374
                        abortImport( true );
284
375
        }
285
376
 
286
377
        @Override
353
444
                                "" + _countSkips );
354
445
        }
355
446
 
356
 
        private void abortImport()
 
447
        private void abortImport( boolean showToasterPopup )
357
448
        {
358
449
                if( _importer != null )
359
450
                {
370
461
                                }
371
462
 
372
463
                                // notify the user
373
 
                        Toast.makeText( this, R.string.doit_importaborted,
374
 
                                        Toast.LENGTH_LONG ).show();
 
464
                                if( showToasterPopup )
 
465
                                        Toast.makeText( this, R.string.doit_importaborted,
 
466
                                                        Toast.LENGTH_LONG ).show();
375
467
                        }
376
468
                }
 
469
 
 
470
                // destroy some stuff
 
471
                _importer = null;
 
472
                _handler = null;
377
473
        }
378
474
}