/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-11 13:00:52 UTC
  • Revision ID: edam@waxworlds.org-20090111130052-q9bh0zz4ey47egbv
- updated todo list

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
 
 
24
 
package org.waxworlds.edam.importcontacts;
 
1
package org.waxworlds.importcontacts;
25
2
 
26
3
import android.app.AlertDialog;
27
4
import android.app.Dialog;
36
13
import android.widget.Button;
37
14
import android.widget.CheckBox;
38
15
import android.widget.CompoundButton;
 
16
import android.widget.LinearLayout;
39
17
import android.widget.ProgressBar;
40
18
import android.widget.TextView;
41
19
import android.widget.Toast;
46
24
        private final static int DIALOG_CONTINUEORABORT = 1;
47
25
        private final static int DIALOG_MERGEPROMPT = 2;
48
26
 
49
 
        public final static int MESSAGE_ALLDONE = 0;
50
 
        public final static int MESSAGE_ABORT = 1;
 
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;
51
30
        public final static int MESSAGE_ERROR = 3;
52
31
        public final static int MESSAGE_CONTINUEORABORT = 4;
53
32
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
60
39
        public final static int MESSAGE_CONTACTMERGED = 12;
61
40
        public final static int MESSAGE_CONTACTSKIPPED = 13;
62
41
 
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
 
 
71
42
        private boolean _startedProgress;
72
43
        private int _maxProgress;
73
44
        private int _tmpProgress;
75
46
        protected String _dialogMessage;
76
47
        private Dialog _mergePromptDialog;
77
48
        private boolean _mergePromptAlwaysSelected;
78
 
        private int _nextAction;
79
 
        private int _currentDialogId;
80
49
 
81
50
        private int _countOverwrites;
82
51
        private int _countCreates;
93
62
                public void handleMessage( Message msg ) {
94
63
                        switch( msg.what )
95
64
                        {
96
 
                        case MESSAGE_ALLDONE:
 
65
                        case MESSAGE_FINISHED_ALLDONE:
97
66
                                ( (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 );
 
67
                                                setVisibility( View.VISIBLE );
 
68
                                // fall through
 
69
                        case MESSAGE_FINISHED:
 
70
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
 
71
                                                setVisibility( View.VISIBLE );
103
72
                                break;
104
 
                        case MESSAGE_ABORT:
105
 
                                manualAbort();
 
73
                        case MESSAGE_FINISHED_GOBACK:
 
74
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
106
75
                                break;
107
76
                        case MESSAGE_ERROR:
108
 
                                _dialogMessage = (String)msg.obj;
 
77
                                Doit.this._dialogMessage = (String)msg.obj;
109
78
                                showDialog( DIALOG_ERROR );
110
79
                                break;
111
80
                        case MESSAGE_CONTINUEORABORT:
112
 
                                _dialogMessage = (String)msg.obj;
 
81
                                Doit.this._dialogMessage = (String)msg.obj;
113
82
                                showDialog( DIALOG_CONTINUEORABORT );
114
83
                                break;
115
84
                        case MESSAGE_SETPROGRESSMESSAGE:
118
87
                                break;
119
88
                        case MESSAGE_SETMAXPROGRESS:
120
89
                                if( _maxProgress > 0 ) {
121
 
                                        if( _tmpProgress == _maxProgress - 1 )
 
90
                                        if( _tmpProgress == _maxProgress )
122
91
                                                _tmpProgress = (Integer)msg.obj;
123
 
                                        if( _progress == _maxProgress - 1 )
 
92
                                        if( _progress == _maxProgress )
124
93
                                                _progress = (Integer)msg.obj;
125
94
                                }
126
95
                                _maxProgress = (Integer)msg.obj;
170
139
                // hide page 2
171
140
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
172
141
 
173
 
                // set up abort button
174
 
                Button begin = (Button)findViewById( R.id.abort );
 
142
                // set up begin button
 
143
                Button begin = (Button)findViewById( R.id.doit_begin );
175
144
                begin.setOnClickListener( new View.OnClickListener() {
176
145
                        public void onClick( View view ) {
177
 
                                manualAbort();
 
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();
178
156
                        }
179
157
                } );
180
158
 
189
167
                _countMerges = 0;
190
168
                _countSkips = 0;
191
169
 
192
 
                updateNext( NEXT_BEGIN );
193
 
 
194
170
                updateProgress();
195
171
                updateStats();
196
172
        }
201
177
                super.onPause();
202
178
 
203
179
                // saving the state of an import sounds complicated! Lets just abort!
204
 
                if( _nextAction != NEXT_CLOSE )
205
 
                        manualAbort( true );
 
180
                abortImport();
 
181
 
 
182
                // destroy some stuff
 
183
                _importer = null;
 
184
                _handler = null;
206
185
        }
207
186
 
208
187
        @Override
209
 
        protected Dialog onCreateDialog( int id )
 
188
        protected Dialog onCreateDialog(int id)
210
189
        {
211
190
                switch( id )
212
191
                {
213
192
                case DIALOG_ERROR:
214
193
                        return new AlertDialog.Builder( this )
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() {
 
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() {
220
199
                                                public void onClick(DialogInterface dialog,
221
 
                                                        int whichButton)
222
 
                                                {
 
200
                                                                int whichButton) {
223
201
                                                        Doit.this._importer.wake();
224
202
                                                }
225
203
                                        } )
226
 
                                .setOnCancelListener( _dialogOnCancelListener )
227
 
                                .create();
 
204
                                        .setOnCancelListener( _dialogOnCancelListener )
 
205
                                        .create();
228
206
                case DIALOG_CONTINUEORABORT:
229
207
                        return new AlertDialog.Builder( this )
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();
 
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();
253
229
                case DIALOG_MERGEPROMPT:
254
230
                        // custom layout in an AlertDialog
255
231
                        LayoutInflater factory = LayoutInflater.from( this );
256
232
                        final View dialogView = factory.inflate(
257
 
                                R.layout.mergeprompt, null );
 
233
                                        R.layout.mergeprompt, null );
258
234
                        ( (CheckBox)dialogView.findViewById( R.id.mergeprompt_always ) ).
259
 
                                setOnCheckedChangeListener(
260
 
                                        new CompoundButton.OnCheckedChangeListener() {
261
 
                                                public void onCheckedChanged( CompoundButton buttonView,
262
 
                                                        boolean isChecked )
263
 
                                                {
264
 
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
265
 
                                                }
266
 
                                        } );
 
235
                                        setOnCheckedChangeListener(
 
236
                                                        new CompoundButton.OnCheckedChangeListener() {
 
237
                                public void onCheckedChanged( CompoundButton buttonView,
 
238
                                                boolean isChecked ) {
 
239
                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
240
                                }
 
241
                        } );
267
242
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
268
 
                                setOnClickListener( _mergePromptButtonListener );
 
243
                                        setOnClickListener( _mergePromptButtonListener );
269
244
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
270
 
                                setOnClickListener( _mergePromptButtonListener );
 
245
                                        setOnClickListener( _mergePromptButtonListener );
271
246
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
272
 
                                setOnClickListener( _mergePromptButtonListener );
273
 
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
274
 
                                setOnClickListener( _mergePromptButtonListener );
 
247
                                        setOnClickListener( _mergePromptButtonListener );
275
248
                        _mergePromptAlwaysSelected = false;
276
249
                        return new AlertDialog.Builder( this )
277
 
                                .setIcon( R.drawable.alert_dialog_icon )
278
 
                                .setTitle( R.string.mergeprompt_title )
279
 
                                .setView( dialogView )
280
 
                                .setOnCancelListener( _dialogOnCancelListener )
281
 
                                .create();
 
250
                                        .setIcon( R.drawable.alert_dialog_icon )
 
251
                                        .setTitle( R.string.mergeprompt_title )
 
252
                                        .setView( dialogView )
 
253
                                        .setOnCancelListener( _dialogOnCancelListener )
 
254
                                        .create();
282
255
                }
283
256
                return null;
284
257
        }
285
258
 
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)
 
259
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
260
                public void onClick( View view ) {
 
261
                        int responseExtra = _mergePromptAlwaysSelected?
 
262
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
302
263
                        Doit.this._mergePromptDialog.dismiss();
303
 
                        Doit.this._mergePromptDialog = null;
 
264
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
265
                        Doit.this._importer.wake( view.getId(), responseExtra );
304
266
                }
305
267
        };
306
268
 
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
 
 
379
269
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
380
270
                        new DialogInterface.OnCancelListener() {
381
271
                public void onCancel( DialogInterface dialog ) {
382
 
                        manualAbort();
 
272
                        setResult( RESULT_CANCELED );
 
273
                        finish();
383
274
                }
384
275
        };
385
276
 
386
 
 
387
277
        @Override
388
278
        protected void onActivityResult( int requestCode, int resultCode,
389
279
                        Intent data )
390
280
        {
391
281
                // if we're cancelling, abort any import
392
282
                if( resultCode == RESULT_CANCELED )
393
 
                        abortImport( true );
 
283
                        abortImport();
394
284
        }
395
285
 
396
286
        @Override
397
287
        protected void onPrepareDialog(int id, Dialog dialog)
398
288
        {
399
 
                _currentDialogId = id;
400
 
 
401
289
                switch( id )
402
290
                {
403
291
                case DIALOG_ERROR:      // fall through
465
353
                                "" + _countSkips );
466
354
        }
467
355
 
468
 
        private void abortImport( boolean showToasterPopup )
 
356
        private void abortImport()
469
357
        {
470
358
                if( _importer != null )
471
359
                {
482
370
                                }
483
371
 
484
372
                                // notify the user
485
 
                                if( showToasterPopup )
486
 
                                        Toast.makeText( this, R.string.doit_importaborted,
487
 
                                                        Toast.LENGTH_LONG ).show();
 
373
                        Toast.makeText( this, R.string.doit_importaborted,
 
374
                                        Toast.LENGTH_LONG ).show();
488
375
                        }
489
376
                }
490
 
 
491
 
                // destroy some stuff
492
 
                _importer = null;
493
 
                _handler = null;
494
377
        }
495
378
}