/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-10 16:14:11 UTC
  • Revision ID: edam@waxworlds.org-20090110161411-4d17l9hs9d6j277q
Initial import

Show diffs side-by-side

added added

removed removed

 
1
package org.waxworlds.importcontacts;
 
2
 
 
3
import android.app.AlertDialog;
 
4
import android.app.Dialog;
 
5
import android.content.DialogInterface;
 
6
import android.content.Intent;
 
7
import android.os.Bundle;
 
8
import android.os.Handler;
 
9
import android.os.Message;
 
10
import android.view.LayoutInflater;
 
11
import android.view.View;
 
12
import android.view.View.OnClickListener;
 
13
import android.widget.Button;
 
14
import android.widget.CheckBox;
 
15
import android.widget.CompoundButton;
 
16
import android.widget.LinearLayout;
 
17
import android.widget.ProgressBar;
 
18
import android.widget.TextView;
 
19
 
 
20
public class Doit extends WizardActivity
 
21
{
 
22
        private final static int DIALOG_ERROR = 0;
 
23
        private final static int DIALOG_CONTINUEORABORT = 1;
 
24
        private final static int DIALOG_MERGEPROMPT = 2;
 
25
 
 
26
        private boolean _startedProgress;
 
27
        private int _maxProgress;
 
28
        private int _tmpProgress;
 
29
        private int _progress;
 
30
        protected String _dialogMessage;
 
31
        private Dialog _mergePromptDialog;
 
32
        private boolean _mergePromptAlwaysSelected;
 
33
 
 
34
        private int _countOverwrites;
 
35
        private int _countCreates;
 
36
        private int _countMerges;
 
37
        private int _countSkips;
 
38
 
 
39
        protected Importer _importer;
 
40
 
 
41
        public Handler _handler;
 
42
 
 
43
        public class DoitHandler extends Handler
 
44
        {
 
45
                @Override
 
46
                public void handleMessage( Message msg ) {
 
47
                        switch( msg.what )
 
48
                        {
 
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;
 
58
                                showDialog( DIALOG_ERROR );
 
59
                                break;
 
60
                        case Importer.MESSAGE_CONTINUEORABORT:
 
61
                                Doit.this._dialogMessage = (String)msg.obj;
 
62
                                showDialog( DIALOG_CONTINUEORABORT );
 
63
                                break;
 
64
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
 
65
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
 
66
                                                setText( (String)msg.obj );
 
67
                                break;
 
68
                        case Importer.MESSAGE_SETMAXPROGRESS:
 
69
                                if( _maxProgress > 0 ) {
 
70
                                        if( _tmpProgress == _maxProgress )
 
71
                                                _tmpProgress = (Integer)msg.obj;
 
72
                                        if( _progress == _maxProgress )
 
73
                                                _progress = (Integer)msg.obj;
 
74
                                }
 
75
                                _maxProgress = (Integer)msg.obj;
 
76
                                updateProgress();
 
77
                                break;
 
78
                        case Importer.MESSAGE_SETTMPPROGRESS:
 
79
                                _tmpProgress = (Integer)msg.obj;
 
80
                                updateProgress();
 
81
                                break;
 
82
                        case Importer.MESSAGE_SETPROGRESS:
 
83
                                _startedProgress = true;
 
84
                                _progress = (Integer)msg.obj;
 
85
                                updateProgress();
 
86
                                break;
 
87
                        case Importer.MESSAGE_MERGEPROMPT:
 
88
                                _dialogMessage = (String)msg.obj;
 
89
                                showDialog( DIALOG_MERGEPROMPT );
 
90
                                break;
 
91
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
 
92
                                _countOverwrites++;
 
93
                                updateStats();
 
94
                                break;
 
95
                        case Importer.MESSAGE_CONTACTCREATED:
 
96
                                _countCreates++;
 
97
                                updateStats();
 
98
                                break;
 
99
                        case Importer.MESSAGE_CONTACTMERGED:
 
100
                                _countMerges++;
 
101
                                updateStats();
 
102
                                break;
 
103
                        case Importer.MESSAGE_CONTACTSKIPPED:
 
104
                                _countSkips++;
 
105
                                updateStats();
 
106
                                break;
 
107
                        default:
 
108
                                super.handleMessage( msg );
 
109
                        }
 
110
                }
 
111
        }
 
112
 
 
113
        @Override
 
114
        protected void onCreate(Bundle savedInstanceState)
 
115
        {
 
116
                setContentView( R.layout.doit );
 
117
                super.onCreate( savedInstanceState );
 
118
 
 
119
                // hide page 2
 
120
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.GONE );
 
121
 
 
122
                // set up begin button
 
123
                Button begin = (Button)findViewById( R.id.doit_begin );
 
124
                begin.setOnClickListener( new View.OnClickListener() {
 
125
                        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
 
 
140
 
 
141
                _startedProgress = false;
 
142
                _maxProgress = 0;
 
143
                _tmpProgress = 0;
 
144
                _progress = 0;
 
145
                _handler = new DoitHandler();
 
146
 
 
147
                _countOverwrites = 0;
 
148
                _countCreates = 0;
 
149
                _countMerges = 0;
 
150
                _countSkips = 0;
 
151
 
 
152
                updateProgress();
 
153
                updateStats();
 
154
        }
 
155
 
 
156
        @Override
 
157
        protected void onPause()
 
158
        {
 
159
                super.onPause();
 
160
 
 
161
                abortImport();
 
162
 
 
163
                // notify the user
 
164
 
 
165
 
 
166
        }
 
167
 
 
168
        @Override
 
169
        protected Dialog onCreateDialog(int id)
 
170
        {
 
171
                switch( id )
 
172
                {
 
173
                case DIALOG_ERROR:
 
174
                        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() {
 
180
                                                public void onClick(DialogInterface dialog,
 
181
                                                                int whichButton) {
 
182
                                                        Doit.this._importer.wake();
 
183
                                                }
 
184
                                        } )
 
185
                                        .setOnCancelListener( _dialogOnCancelListener )
 
186
                                        .create();
 
187
                case DIALOG_CONTINUEORABORT:
 
188
                        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();
 
210
                case DIALOG_MERGEPROMPT:
 
211
                        // custom layout in an AlertDialog
 
212
                        LayoutInflater factory = LayoutInflater.from( this );
 
213
                        final View dialogView = factory.inflate(
 
214
                                        R.layout.mergeprompt, null );
 
215
                        ( (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
                        } );
 
223
                        ( (Button)dialogView.findViewById( R.id.merge_keep ) ).
 
224
                                        setOnClickListener( _mergePromptButtonListener );
 
225
                        ( (Button)dialogView.findViewById( R.id.merge_overwrite ) ).
 
226
                                        setOnClickListener( _mergePromptButtonListener );
 
227
                        ( (Button)dialogView.findViewById( R.id.merge_merge ) ).
 
228
                                        setOnClickListener( _mergePromptButtonListener );
 
229
                        _mergePromptAlwaysSelected = false;
 
230
                        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();
 
236
                }
 
237
                return null;
 
238
        }
 
239
 
 
240
    private OnClickListener _mergePromptButtonListener = new OnClickListener() {
 
241
                public void onClick( View view ) {
 
242
                        int responseExtra = _mergePromptAlwaysSelected?
 
243
                                        Importer.RESPONSEEXTRA_ALWAYS : Importer.RESPONSEEXTRA_NONE;
 
244
                        Doit.this._mergePromptDialog.dismiss();
 
245
                        Doit.this._mergePromptDialog = null;    // dont keep a reference!
 
246
                        Doit.this._importer.wake( view.getId(), responseExtra );
 
247
                }
 
248
        };
 
249
 
 
250
        private DialogInterface.OnCancelListener _dialogOnCancelListener =
 
251
                        new DialogInterface.OnCancelListener() {
 
252
                public void onCancel( DialogInterface dialog ) {
 
253
                        setResult( RESULT_CANCELED );
 
254
                        finish();
 
255
                }
 
256
        };
 
257
 
 
258
        @Override
 
259
        protected void onActivityResult( int requestCode, int resultCode,
 
260
                        Intent data )
 
261
        {
 
262
                if( resultCode == RESULT_CANCELED )
 
263
                        abortImport();
 
264
        }
 
265
 
 
266
        @Override
 
267
        protected void onPrepareDialog(int id, Dialog dialog)
 
268
        {
 
269
                switch( id )
 
270
                {
 
271
                case DIALOG_ERROR:      // fall through
 
272
                case DIALOG_CONTINUEORABORT:
 
273
                        // set dialog message
 
274
                        ( (AlertDialog)dialog ).setMessage( _dialogMessage );
 
275
                        break;
 
276
                case DIALOG_MERGEPROMPT:
 
277
                        // set contact's name
 
278
                        ( (TextView)dialog.findViewById( R.id.mergeprompt_name ) ).setText(
 
279
                                        _dialogMessage );
 
280
                        // and set up reference to dialog
 
281
                        _mergePromptDialog = dialog;
 
282
                        break;
 
283
                }
 
284
 
 
285
                super.onPrepareDialog( id, dialog );
 
286
        }
 
287
 
 
288
        private void importContacts()
 
289
        {
 
290
                // switch interfaces
 
291
                ( findViewById( R.id.doit_page_1 ) ).setVisibility( View.GONE );
 
292
                ( findViewById( R.id.doit_page_2 ) ).setVisibility( View.VISIBLE );
 
293
 
 
294
                // disable back button
 
295
                ( (Button)findViewById( R.id.back ) ).setEnabled( false );
 
296
 
 
297
                // create importer
 
298
                _importer = new VCFImporter( this );
 
299
 
 
300
                // start the service's thread
 
301
                _importer.start();
 
302
        }
 
303
 
 
304
        private void updateProgress()
 
305
        {
 
306
                ProgressBar bar = (ProgressBar)findViewById( R.id.doit_progress );
 
307
                TextView outOf = (TextView)findViewById( R.id.doit_outof );
 
308
 
 
309
                if( _maxProgress > 0 )
 
310
                {
 
311
                        bar.setMax( _maxProgress );
 
312
                        bar.setSecondaryProgress( _tmpProgress );
 
313
 
 
314
                        if( _startedProgress )
 
315
                        {
 
316
                                ( (TextView)findViewById( R.id.doit_percentage ) ).setText(
 
317
                                                (int)Math.round( 100 * _progress / _maxProgress ) + "%" );
 
318
                                outOf.setText( _progress + "/" + _maxProgress );
 
319
                                bar.setProgress( _progress );
 
320
                        }
 
321
                }
 
322
        }
 
323
 
 
324
        private void updateStats()
 
325
        {
 
326
                ( (TextView)findViewById( R.id.doit_overwrites ) ).setText(
 
327
                                "" + _countOverwrites );
 
328
                ( (TextView)findViewById( R.id.doit_creates ) ).setText(
 
329
                                "" + _countCreates );
 
330
                ( (TextView)findViewById( R.id.doit_merges ) ).setText(
 
331
                                "" + _countMerges );
 
332
                ( (TextView)findViewById( R.id.doit_skips ) ).setText(
 
333
                                "" + _countSkips );
 
334
        }
 
335
 
 
336
        private void abortImport()
 
337
        {
 
338
                if( _importer != null )
 
339
                {
 
340
                        _importer.setAbort();
 
341
                        while( true ) {
 
342
                                try {
 
343
                                        _importer.join();
 
344
                                        break;
 
345
                                }
 
346
                                catch( InterruptedException e ) {}
 
347
                        }
 
348
                }
 
349
        }
 
350
}