/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-17 18:43:13 UTC
  • Revision ID: edam@waxworlds.org-20101017184313-y2m6gsdzctjq5wtl
- changed case on charset and encoding warning strings (it looked bad)
- properly handle quoted-printable unencoding (including multi-line values)
- ignore blank lines in vCard
- ignore empty properties in vCard

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
                // close any open dialogs
 
204
                try {
 
205
                        dismissDialog( _currentDialogId );
 
206
                }
 
207
                catch( Exception e ) {
 
208
                }
 
209
 
 
210
                // saving the state of an import sounds complicated! Lets just abort!
 
211
                manualAbort( true );
166
212
        }
167
213
 
168
214
        @Override
169
 
        protected Dialog onCreateDialog(int id)
 
215
        protected Dialog onCreateDialog( int id )
170
216
        {
171
217
                switch( id )
172
218
                {
173
219
                case DIALOG_ERROR:
174
220
                        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() {
 
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() {
180
226
                                                public void onClick(DialogInterface dialog,
181
 
                                                                int whichButton) {
 
227
                                                        int whichButton)
 
228
                                                {
182
229
                                                        Doit.this._importer.wake();
183
230
                                                }
184
231
                                        } )
185
 
                                        .setOnCancelListener( _dialogOnCancelListener )
186
 
                                        .create();
 
232
                                .setOnCancelListener( _dialogOnCancelListener )
 
233
                                .create();
187
234
                case DIALOG_CONTINUEORABORT:
188
235
                        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();
 
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();
210
259
                case DIALOG_MERGEPROMPT:
211
260
                        // custom layout in an AlertDialog
212
261
                        LayoutInflater factory = LayoutInflater.from( this );
213
262
                        final View dialogView = factory.inflate(
214
 
                                        R.layout.mergeprompt, null );
 
263
                                R.layout.mergeprompt, null );
215
264
                        ( (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
 
                        } );
 
265
                                setOnCheckedChangeListener(
 
266
                                        new CompoundButton.OnCheckedChangeListener() {
 
267
                                                public void onCheckedChanged( CompoundButton buttonView,
 
268
                                                        boolean isChecked )
 
269
                                                {
 
270
                                                        Doit.this._mergePromptAlwaysSelected = isChecked;
 
271
                                                }
 
272
                                        } );
223
273
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
224
 
                                        setOnClickListener( _mergePromptButtonListener );
 
274
                                setOnClickListener( _mergePromptButtonListener );
225
275
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
226
 
                                        setOnClickListener( _mergePromptButtonListener );
 
276
                                setOnClickListener( _mergePromptButtonListener );
227
277
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
228
 
                                        setOnClickListener( _mergePromptButtonListener );
 
278
                                setOnClickListener( _mergePromptButtonListener );
 
279
                        ( (Button)dialogView.findViewById( R.id.abort ) ).
 
280
                                setOnClickListener( _mergePromptButtonListener );
229
281
                        _mergePromptAlwaysSelected = false;
230
282
                        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();
 
283
                                .setIcon( R.drawable.alert_dialog_icon )
 
284
                                .setTitle( R.string.mergeprompt_title )
 
285
                                .setView( dialogView )
 
286
                                .setOnCancelListener( _dialogOnCancelListener )
 
287
                                .create();
236
288
                }
237
289
                return null;
238
290
        }
239
291
 
240
 
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
241
 
                public void onClick( View view ) {
242
 
                        int responseExtra = _mergePromptAlwaysSelected?
243
 
                                        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)
244
308
                        Doit.this._mergePromptDialog.dismiss();
245
 
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
246
 
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
309
                        Doit.this._mergePromptDialog = null;
247
310
                }
248
311
        };
249
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
 
250
378
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
251
379
                        new DialogInterface.OnCancelListener() {
252
380
                public void onCancel( DialogInterface dialog ) {
259
387
        protected void onActivityResult( int requestCode, int resultCode,
260
388
                        Intent data )
261
389
        {
 
390
                // if we're cancelling, abort any import
262
391
                if( resultCode == RESULT_CANCELED )
263
 
                        abortImport();
 
392
                        abortImport( true );
264
393
        }
265
394
 
266
395
        @Override
267
396
        protected void onPrepareDialog(int id, Dialog dialog)
268
397
        {
 
398
                _currentDialogId = id;
 
399
 
269
400
                switch( id )
270
401
                {
271
402
                case DIALOG_ERROR:      // fall through
333
464
                                "" + _countSkips );
334
465
        }
335
466
 
336
 
        private void abortImport()
 
467
        private void abortImport( boolean showToasterPopup )
337
468
        {
338
469
                if( _importer != null )
339
470
                {
340
 
                        _importer.setAbort();
341
 
                        while( true ) {
342
 
                                try {
343
 
                                        _importer.join();
344
 
                                        break;
 
471
                        // try and flag worker thread - did we need to?
 
472
                        if( _importer.setAbort() )
 
473
                        {
 
474
                                // wait for worker thread to end
 
475
                                while( true ) {
 
476
                                        try {
 
477
                                                _importer.join();
 
478
                                                break;
 
479
                                        }
 
480
                                        catch( InterruptedException e ) {}
345
481
                                }
346
 
                                catch( InterruptedException e ) {}
 
482
 
 
483
                                // notify the user
 
484
                                if( showToasterPopup )
 
485
                                        Toast.makeText( this, R.string.doit_importaborted,
 
486
                                                        Toast.LENGTH_LONG ).show();
347
487
                        }
348
488
                }
 
489
 
 
490
                // destroy some stuff
 
491
                _importer = null;
 
492
                _handler = null;
349
493
        }
350
494
}