/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 12:58:54 UTC
  • Revision ID: edam@waxworlds.org-20090111125854-u8ofzso4jatk12me
- added "all done" message
- rewrote Importer.finish() to make the exit process more consistent
- moved Doit's message definitions to Doit
- ensure the importer is destroyed in Doit.onPause()
- only show the toaster popup if a) there is an importer to abort, and b) the abort actually did something (i.e., it's not already aborted)
- bugfix: added some checks for abortion to the Importer after wait()ing after a dialog. Importer.wake() now alsy does a notify() to break out of dialog waits.
- also, made Importer.checkAbort() protected, so it can be checked from specific importers as desired

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
        private final static int DIALOG_CONTINUEORABORT = 1;
25
25
        private final static int DIALOG_MERGEPROMPT = 2;
26
26
 
 
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;
 
30
        public final static int MESSAGE_ERROR = 3;
 
31
        public final static int MESSAGE_CONTINUEORABORT = 4;
 
32
        public final static int MESSAGE_SETPROGRESSMESSAGE = 5;
 
33
        public final static int MESSAGE_SETMAXPROGRESS = 6;
 
34
        public final static int MESSAGE_SETTMPPROGRESS = 7;
 
35
        public final static int MESSAGE_SETPROGRESS = 8;
 
36
        public final static int MESSAGE_MERGEPROMPT = 9;
 
37
        public final static int MESSAGE_CONTACTOVERWRITTEN = 10;
 
38
        public final static int MESSAGE_CONTACTCREATED = 11;
 
39
        public final static int MESSAGE_CONTACTMERGED = 12;
 
40
        public final static int MESSAGE_CONTACTSKIPPED = 13;
 
41
 
27
42
        private boolean _startedProgress;
28
43
        private int _maxProgress;
29
44
        private int _tmpProgress;
37
52
        private int _countMerges;
38
53
        private int _countSkips;
39
54
 
40
 
        protected Importer _importer;
 
55
        protected Importer _importer = null;
41
56
 
42
57
        public Handler _handler;
43
58
 
47
62
                public void handleMessage( Message msg ) {
48
63
                        switch( msg.what )
49
64
                        {
50
 
                        case Importer.MESSAGE_FINISHED:
 
65
                        case MESSAGE_FINISHED_ALLDONE:
 
66
                                ( (TextView)findViewById( R.id.doit_alldone ) ).
 
67
                                                setVisibility( View.VISIBLE );
 
68
                                // fall through
 
69
                        case MESSAGE_FINISHED:
51
70
                                ( (LinearLayout)findViewById( R.id.doit_closedisplay ) ).
52
71
                                                setVisibility( View.VISIBLE );
53
72
                                break;
54
 
                        case Importer.MESSAGE_FINISHED_BACK:
 
73
                        case MESSAGE_FINISHED_GOBACK:
55
74
                                ( (Button)findViewById( R.id.back ) ).setEnabled( true );
56
75
                                break;
57
 
                        case Importer.MESSAGE_ERROR:
 
76
                        case MESSAGE_ERROR:
58
77
                                Doit.this._dialogMessage = (String)msg.obj;
59
78
                                showDialog( DIALOG_ERROR );
60
79
                                break;
61
 
                        case Importer.MESSAGE_CONTINUEORABORT:
 
80
                        case MESSAGE_CONTINUEORABORT:
62
81
                                Doit.this._dialogMessage = (String)msg.obj;
63
82
                                showDialog( DIALOG_CONTINUEORABORT );
64
83
                                break;
65
 
                        case Importer.MESSAGE_SETPROGRESSMESSAGE:
 
84
                        case MESSAGE_SETPROGRESSMESSAGE:
66
85
                                ( (TextView)findViewById( R.id.doit_percentage ) ).
67
86
                                                setText( (String)msg.obj );
68
87
                                break;
69
 
                        case Importer.MESSAGE_SETMAXPROGRESS:
 
88
                        case MESSAGE_SETMAXPROGRESS:
70
89
                                if( _maxProgress > 0 ) {
71
90
                                        if( _tmpProgress == _maxProgress )
72
91
                                                _tmpProgress = (Integer)msg.obj;
76
95
                                _maxProgress = (Integer)msg.obj;
77
96
                                updateProgress();
78
97
                                break;
79
 
                        case Importer.MESSAGE_SETTMPPROGRESS:
 
98
                        case MESSAGE_SETTMPPROGRESS:
80
99
                                _tmpProgress = (Integer)msg.obj;
81
100
                                updateProgress();
82
101
                                break;
83
 
                        case Importer.MESSAGE_SETPROGRESS:
 
102
                        case MESSAGE_SETPROGRESS:
84
103
                                _startedProgress = true;
85
104
                                _progress = (Integer)msg.obj;
86
105
                                updateProgress();
87
106
                                break;
88
 
                        case Importer.MESSAGE_MERGEPROMPT:
 
107
                        case MESSAGE_MERGEPROMPT:
89
108
                                _dialogMessage = (String)msg.obj;
90
109
                                showDialog( DIALOG_MERGEPROMPT );
91
110
                                break;
92
 
                        case Importer.MESSAGE_CONTACTOVERWRITTEN:
 
111
                        case MESSAGE_CONTACTOVERWRITTEN:
93
112
                                _countOverwrites++;
94
113
                                updateStats();
95
114
                                break;
96
 
                        case Importer.MESSAGE_CONTACTCREATED:
 
115
                        case MESSAGE_CONTACTCREATED:
97
116
                                _countCreates++;
98
117
                                updateStats();
99
118
                                break;
100
 
                        case Importer.MESSAGE_CONTACTMERGED:
 
119
                        case MESSAGE_CONTACTMERGED:
101
120
                                _countMerges++;
102
121
                                updateStats();
103
122
                                break;
104
 
                        case Importer.MESSAGE_CONTACTSKIPPED:
 
123
                        case MESSAGE_CONTACTSKIPPED:
105
124
                                _countSkips++;
106
125
                                updateStats();
107
126
                                break;
160
179
                // saving the state of an import sounds complicated! Lets just abort!
161
180
                abortImport();
162
181
 
163
 
                // notify the user
164
 
        Toast.makeText( this, R.string.doit_importaborted,
165
 
                        Toast.LENGTH_LONG ).show();
 
182
                // destroy some stuff
 
183
                _importer = null;
 
184
                _handler = null;
166
185
        }
167
186
 
168
187
        @Override
338
357
        {
339
358
                if( _importer != null )
340
359
                {
341
 
                        _importer.setAbort();
342
 
                        while( true ) {
343
 
                                try {
344
 
                                        _importer.join();
345
 
                                        break;
 
360
                        // try and flag worker thread - did we need to?
 
361
                        if( _importer.setAbort() )
 
362
                        {
 
363
                                // wait for worker thread to end
 
364
                                while( true ) {
 
365
                                        try {
 
366
                                                _importer.join();
 
367
                                                break;
 
368
                                        }
 
369
                                        catch( InterruptedException e ) {}
346
370
                                }
347
 
                                catch( InterruptedException e ) {}
 
371
 
 
372
                                // notify the user
 
373
                        Toast.makeText( this, R.string.doit_importaborted,
 
374
                                        Toast.LENGTH_LONG ).show();
348
375
                        }
349
376
                }
350
377
        }