/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-07-13 19:13:38 UTC
  • Revision ID: edam@waxworlds.org-20100713191338-e23wre83uellvtt4
Tags: 1.0
- updated NEWS, TODO and manifest

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;
19
41
import android.widget.Toast;
24
46
        private final static int DIALOG_CONTINUEORABORT = 1;
25
47
        private final static int DIALOG_MERGEPROMPT = 2;
26
48
 
27
 
        public final static int MESSAGE_FINISHED_ALLDONE = 0;
28
 
        public final static int MESSAGE_FINISHED = 1;
29
 
        public final static int MESSAGE_FINISHED_GOBACK = 2;
 
49
        public final static int MESSAGE_ALLDONE = 0;
 
50
        public final static int MESSAGE_ABORT = 1;
30
51
        public final static int MESSAGE_ERROR = 3;
31
52
        public final static int MESSAGE_CONTINUEORABORT = 4;
32
53
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
39
60
        public final static int MESSAGE_CONTACTMERGED = 12;
40
61
        public final static int MESSAGE_CONTACTSKIPPED = 13;
41
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
 
42
71
        private boolean _startedProgress;
43
72
        private int _maxProgress;
44
73
        private int _tmpProgress;
46
75
        protected String _dialogMessage;
47
76
        private Dialog _mergePromptDialog;
48
77
        private boolean _mergePromptAlwaysSelected;
 
78
        private int _nextAction;
 
79
        private int _currentDialogId;
49
80
 
50
81
        private int _countOverwrites;
51
82
        private int _countCreates;
62
93
                public void handleMessage( Message msg ) {
63
94
                        switch( msg.what )
64
95
                        {
65
 
                        case MESSAGE_FINISHED_ALLDONE:
 
96
                        case MESSAGE_ALLDONE:
66
97
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
67
 
                                                setVisibility( View.VISIBLE );
68
 
                                // fall through
69
 
                        case MESSAGE_FINISHED:
70
 
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
71
 
                                                setVisibility( View.VISIBLE );
 
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 );
72
103
                                break;
73
 
                        case MESSAGE_FINISHED_GOBACK:
74
 
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
104
                        case MESSAGE_ABORT:
 
105
                                manualAbort();
75
106
                                break;
76
107
                        case MESSAGE_ERROR:
77
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
108
                                _dialogMessage = (String)msg.obj;
78
109
                                showDialog( DIALOG_ERROR );
79
110
                                break;
80
111
                        case MESSAGE_CONTINUEORABORT:
81
 
                                Doit.this._dialogMessage = (String)msg.obj;
 
112
                                _dialogMessage = (String)msg.obj;
82
113
                                showDialog( DIALOG_CONTINUEORABORT );
83
114
                                break;
84
115
                        case MESSAGE_SETPROGRESSMESSAGE:
87
118
                                break;
88
119
                        case MESSAGE_SETMAXPROGRESS:
89
120
                                if( _maxProgress > 0 ) {
90
 
                                        if( _tmpProgress == _maxProgress )
 
121
                                        if( _tmpProgress == _maxProgress - 1 )
91
122
                                                _tmpProgress = (Integer)msg.obj;
92
 
                                        if( _progress == _maxProgress )
 
123
                                        if( _progress == _maxProgress - 1 )
93
124
                                                _progress = (Integer)msg.obj;
94
125
                                }
95
126
                                _maxProgress = (Integer)msg.obj;
139
170
                // hide page 2
140
171
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
141
172
 
142
 
                // set up begin button
143
 
                Button begin = (Button)findViewById( R.id.doit_begin );
 
173
                // set up abort button
 
174
                Button begin = (Button)findViewById( R.id.abort );
144
175
                begin.setOnClickListener( new View.OnClickListener() {
145
176
                        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();
 
177
                                manualAbort();
156
178
                        }
157
179
                } );
158
180
 
167
189
                _countMerges = 0;
168
190
                _countSkips = 0;
169
191
 
 
192
                updateNext( NEXT_BEGIN );
 
193
 
170
194
                updateProgress();
171
195
                updateStats();
172
196
        }
176
200
        {
177
201
                super.onPause();
178
202
 
 
203
                // close any open dialogs
 
204
                try {
 
205
                        dismissDialog( _currentDialogId );
 
206
                }
 
207
                catch( Exception e ) {
 
208
                }
 
209
 
179
210
                // saving the state of an import sounds complicated! Lets just abort!
180
 
                abortImport();
181
 
 
182
 
                // destroy some stuff
183
 
                _importer = null;
184
 
                _handler = null;
 
211
                manualAbort( true );
185
212
        }
186
213
 
187
214
        @Override
188
 
        protected Dialog onCreateDialog(int id)
 
215
        protected Dialog onCreateDialog( int id )
189
216
        {
190
217
                switch( id )
191
218
                {
192
219
                case DIALOG_ERROR:
193
220
                        return new AlertDialog.Builder( this )
194
 
                                        .setIcon( R.drawable.alert_dialog_icon )
195
 
                                        .setTitle( R.string.error_title )
196
 
                                        .setMessage( "" )
197
 
                                        .setPositiveButton( R.string.error_ok,
198
 
                                                        new DialogInterface.OnClickListener() {
 
221
                                .setIcon( R.drawable.alert_dialog_icon )
 
222
                                .setTitle( R.string.error_title )
 
223
                                .setMessage( "" )
 
224
                                .setPositiveButton( R.string.error_ok,
 
225
                                        new DialogInterface.OnClickListener() {
199
226
                                                public void onClick(DialogInterface dialog,
200
 
                                                                int whichButton) {
 
227
                                                        int whichButton)
 
228
                                                {
201
229
                                                        Doit.this._importer.wake();
202
230
                                                }
203
231
                                        } )
204
 
                                        .setOnCancelListener( _dialogOnCancelListener )
205
 
                                        .create();
 
232
                                .setOnCancelListener( _dialogOnCancelListener )
 
233
                                .create();
206
234
                case DIALOG_CONTINUEORABORT:
207
235
                        return new AlertDialog.Builder( this )
208
 
                                        .setIcon( R.drawable.alert_dialog_icon )
209
 
                                        .setTitle( R.string.error_title )
210
 
                                        .setMessage( "" )
211
 
                                        .setPositiveButton( R.string.error_continue,
212
 
                                                        new DialogInterface.OnClickListener() {
213
 
                                                public void onClick(DialogInterface dialog,
214
 
                                                                int whichButton) {
215
 
                                                        Doit.this._importer.wake(
216
 
                                                                        Importer.RESPONSE_POSITIVE );
217
 
                                                }
218
 
                                        } )
219
 
                                        .setNegativeButton( R.string.error_abort,
220
 
                                                        new DialogInterface.OnClickListener() {
221
 
                                                public void onClick(DialogInterface dialog,
222
 
                                                                int whichButton) {
223
 
                                                        Doit.this._importer.wake(
224
 
                                                                        Importer.RESPONSE_NEGATIVE );
225
 
                                                }
226
 
                                        } )
227
 
                                        .setOnCancelListener( _dialogOnCancelListener )
228
 
                                        .create();
 
236
                                .setIcon( R.drawable.alert_dialog_icon )
 
237
                                .setTitle( R.string.error_title )
 
238
                                .setMessage( "" )
 
239
                                .setPositiveButton( R.string.error_continue,
 
240
                                        new DialogInterface.OnClickListener() {
 
241
                                                public void onClick(DialogInterface dialog,
 
242
                                                        int whichButton)
 
243
                                                {
 
244
                                                        Doit.this._importer.wake(
 
245
                                                                Importer.RESPONSE_POSITIVE );
 
246
                                                }
 
247
                                        } )
 
248
                                .setNegativeButton( R.string.error_abort,
 
249
                                        new DialogInterface.OnClickListener() {
 
250
                                                public void onClick(DialogInterface dialog,
 
251
                                                        int whichButton)
 
252
                                                {
 
253
                                                        Doit.this._importer.wake(
 
254
                                                                Importer.RESPONSE_NEGATIVE );
 
255
                                                }
 
256
                                        } )
 
257
                                .setOnCancelListener( _dialogOnCancelListener )
 
258
                                .create();
229
259
                case DIALOG_MERGEPROMPT:
230
260
                        // custom layout in an AlertDialog
231
261
                        LayoutInflater factory = LayoutInflater.from( this );
232
262
                        final View dialogView = factory.inflate(
233
 
                                        R.layout.mergeprompt, null );
 
263
                                R.layout.mergeprompt, null );
234
264
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
235
 
                                        setOnCheckedChangeListener(
236
 
                                                        new CompoundButton.OnCheckedChangeListener() {
237
 
                                public void onCheckedChanged( CompoundButton buttonView,
238
 
                                                boolean isChecked ) {
239
 
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
240
 
                                }
241
 
                        } );
 
265
                                setOnCheckedChangeListener(
 
266
                                        new CompoundButton.OnCheckedChangeListener() {
 
267
                                                public void onCheckedChanged( CompoundButton buttonView,
 
268
                                                        boolean isChecked )
 
269
                                                {
 
270
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
271
                                                }
 
272
                                        } );
242
273
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
243
 
                                        setOnClickListener( _mergePromptButtonListener );
 
274
                                setOnClickListener( _mergePromptButtonListener );
244
275
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
245
 
                                        setOnClickListener( _mergePromptButtonListener );
 
276
                                setOnClickListener( _mergePromptButtonListener );
246
277
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
247
 
                                        setOnClickListener( _mergePromptButtonListener );
 
278
                                setOnClickListener( _mergePromptButtonListener );
 
279
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
280
                                setOnClickListener( _mergePromptButtonListener );
248
281
                        _mergePromptAlwaysSelected = false;
249
282
                        return new AlertDialog.Builder( this )
250
 
                                        .setIcon( R.drawable.alert_dialog_icon )
251
 
                                        .setTitle( R.string.mergeprompt_title )
252
 
                                        .setView( dialogView )
253
 
                                        .setOnCancelListener( _dialogOnCancelListener )
254
 
                                        .create();
 
283
                                .setIcon( R.drawable.alert_dialog_icon )
 
284
                                .setTitle( R.string.mergeprompt_title )
 
285
                                .setView( dialogView )
 
286
                                .setOnCancelListener( _dialogOnCancelListener )
 
287
                                .create();
255
288
                }
256
289
                return null;
257
290
        }
258
291
 
259
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
260
 
                public void onClick( View view ) {
261
 
                        int responseExtra = _mergePromptAlwaysSelected?
262
 
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
292
        private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
293
                public void onClick( View view )
 
294
                {
 
295
                        // handle abort
 
296
                        if( view.getId() == R.id.abort )
 
297
                                manualAbort();
 
298
 
 
299
                        // else, response
 
300
                        else {
 
301
                                int responseExtra = _mergePromptAlwaysSelected?
 
302
                                                Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
303
                                Doit.this._importer.wake( convertIdToAction( view.getId() ),
 
304
                                                responseExtra );
 
305
                        }
 
306
 
 
307
                        // close dialog and free (don't keep a reference)
263
308
                        Doit.this._mergePromptDialog.dismiss();
264
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
265
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
309
                        Doit.this._mergePromptDialog = null;
266
310
                }
267
311
        };
268
312
 
 
313
        @Override
 
314
        protected void onNext()
 
315
        {
 
316
                Button next = (Button)findViewById( R.id.next );
 
317
                next.setEnabled( false );
 
318
 
 
319
                switch( _nextAction )
 
320
                {
 
321
                case NEXT_BEGIN:
 
322
                        importContacts();
 
323
                        break;
 
324
                case NEXT_CLOSE:
 
325
                        setResult( RESULT_OK );
 
326
                        finish();
 
327
                        break;
 
328
                }
 
329
        }
 
330
 
 
331
        private void manualAbort()
 
332
        {
 
333
                manualAbort( false );
 
334
        }
 
335
 
 
336
        private void manualAbort( boolean showToasterPopup )
 
337
        {
 
338
                abortImport( showToasterPopup );
 
339
 
 
340
                updateNext( NEXT_CLOSE );
 
341
                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
 
342
                findViewById( R.id.doit_abort_disp ).setVisibility( View.GONE );
 
343
                ( (TextView)findViewById( R.id.doit_aborted ) ).
 
344
                        setVisibility( View.VISIBLE );
 
345
                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
346
                        setVisibility( View.GONE );
 
347
        }
 
348
 
 
349
        private void updateNext( int nextAction )
 
350
        {
 
351
                Button next = (Button)findViewById( R.id.next );
 
352
                switch( nextAction ) {
 
353
                case NEXT_BEGIN:        next.setText( R.string.doit_begin ); break;
 
354
                case NEXT_CLOSE:        next.setText( R.string.doit_close ); break;
 
355
                }
 
356
                next.setEnabled( true );
 
357
                _nextAction = nextAction;
 
358
        }
 
359
 
 
360
        public static int convertIdToAction( int id ) {
 
361
                switch( id ) {
 
362
                case R.id.merge_keep:           return ACTION_KEEP;
 
363
                case R.id.merge_merge:          return ACTION_MERGE_MERGE;
 
364
                case R.id.merge_overwrite:      return ACTION_OVERWRITE;
 
365
                default: return ACTION_PROMPT;
 
366
                }
 
367
        }
 
368
 
 
369
        public static int convertActionToId( int action ) {
 
370
                switch( action ) {
 
371
                case ACTION_KEEP:               return R.id.merge_keep;
 
372
                case ACTION_MERGE_MERGE:return R.id.merge_merge;
 
373
                case ACTION_OVERWRITE:  return R.id.merge_overwrite;
 
374
                default: return R.id.merge_prompt;
 
375
                }
 
376
        }
 
377
 
269
378
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
270
379
                        new DialogInterface.OnCancelListener() {
271
380
                public void onCancel( DialogInterface dialog ) {
280
389
        {
281
390
                // if we're cancelling, abort any import
282
391
                if( resultCode == RESULT_CANCELED )
283
 
                        abortImport();
 
392
                        abortImport( true );
284
393
        }
285
394
 
286
395
        @Override
287
396
        protected void onPrepareDialog(int id, Dialog dialog)
288
397
        {
 
398
                _currentDialogId = id;
 
399
 
289
400
                switch( id )
290
401
                {
291
402
                case DIALOG_ERROR:      // fall through
353
464
                                "" + _countSkips );
354
465
        }
355
466
 
356
 
        private void abortImport()
 
467
        private void abortImport( boolean showToasterPopup )
357
468
        {
358
469
                if( _importer != null )
359
470
                {
370
481
                                }
371
482
 
372
483
                                // notify the user
373
 
                        Toast.makeText( this, R.string.doit_importaborted,
374
 
                                        Toast.LENGTH_LONG ).show();
 
484
                                if( showToasterPopup )
 
485
                                        Toast.makeText( this, R.string.doit_importaborted,
 
486
                                                        Toast.LENGTH_LONG ).show();
375
487
                        }
376
488
                }
 
489
 
 
490
                // destroy some stuff
 
491
                _importer = null;
 
492
                _handler = null;
377
493
        }
378
494
}