4
* Copyright (C) 2011 Tim Marston <tim@ed.am>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://ed.am/dev/android/export-contacts
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.
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.
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/>.
24
package am.ed.exportcontacts;
26
import java.util.ArrayList;
28
import android.content.SharedPreferences;
29
import android.os.Message;
32
public class Exporter extends Thread
34
public final static int ACTION_ABORT = 1;
35
public final static int ACTION_ALLDONE = 2;
37
public final static int RESPONSE_NEGATIVE = 0;
38
public final static int RESPONSE_POSITIVE = 1;
40
public final static int RESPONSEEXTRA_NONE = 0;
41
public final static int RESPONSEEXTRA_ALWAYS = 1;
44
private int _response;
45
private boolean _abort = false;
46
private boolean _is_finished = false;
49
* Data about a contact
51
public class ContactData
53
class OrganisationDetail
55
protected String _org;
56
protected String _title;
58
public OrganisationDetail( String org, String title )
60
_org = org != null && org.length() > 0? org : null;
61
_title = title != null && title.length() > 0? title : null;
64
public String getOrganisation()
69
public String getTitle()
78
protected String _num;
80
public NumberDetail( int type, String num )
83
_num = num != null && num.length() > 0? num : null;
91
public String getNumber()
100
protected String _email;
102
public EmailDetail( int type, String email )
105
_email = email != null && email.length() > 0? email : null;
113
public String getEmail()
122
protected String _addr;
124
public AddressDetail( int type, String addr )
127
_addr = addr != null && addr.length() > 0? addr : null;
135
public String getAddress()
141
protected String _name = null;
142
protected ArrayList< OrganisationDetail > _organisations = null;
143
protected ArrayList< NumberDetail > _numbers = null;
144
protected ArrayList< EmailDetail > _emails = null;
145
protected ArrayList< AddressDetail > _addresses = null;
147
public void setName( String name )
149
_name = name != null && name.length() > 0? name : null;
152
public String getName()
157
public void addOrganisation( OrganisationDetail organisation )
159
if( organisation.getOrganisation() == null ) return;
160
if( _organisations == null )
161
_organisations = new ArrayList< OrganisationDetail >();
162
_organisations.add( organisation );
165
public ArrayList< OrganisationDetail > getOrganisations()
167
return _organisations;
170
public void addNumber( NumberDetail number )
172
if( number.getNumber() == null ) return;
173
if( _numbers == null )
174
_numbers = new ArrayList< NumberDetail >();
175
_numbers.add( number );
178
public ArrayList< NumberDetail > getNumbers()
183
public void addEmail( EmailDetail email )
185
if( email.getEmail() == null ) return;
186
if( _emails == null )
187
_emails = new ArrayList< EmailDetail >();
188
_emails.add( email );
191
public ArrayList< EmailDetail > getEmails()
196
public void addAddress( AddressDetail address )
198
if( address.getAddress() == null ) return;
199
if( _addresses == null )
200
_addresses = new ArrayList< AddressDetail >();
201
_addresses.add( address );
204
public ArrayList< AddressDetail > getAddresses()
209
public String getPrimaryIdentifier()
214
if( _organisations != null &&
215
_organisations.get( 0 ).getOrganisation() != null )
216
return _organisations.get( 0 ).getOrganisation();
218
if( _numbers!= null &&
219
_numbers.get( 0 ).getNumber() != null )
220
return _numbers.get( 0 ).getNumber();
222
if( _emails!= null &&
223
_emails.get( 0 ).getEmail() != null )
224
return _emails.get( 0 ).getEmail();
226
// no primary identifier
231
@SuppressWarnings( "serial" )
232
protected class AbortExportException extends Exception { };
234
public Exporter( Doit doit )
245
setProgressMessage( R.string.doit_scanning );
251
finish( ACTION_ALLDONE );
253
catch( AbortExportException e )
256
// flag as finished to prevent interrupts
260
synchronized private void setIsFinished()
265
protected void exportContacts() throws AbortExportException
267
// set up a contact reader
268
ContactAccessor contact_reader =
269
new ContactsContactAccessor( _doit, this );
270
int num_contacts = contact_reader.getNumContacts();
271
if( num_contacts == 0 )
272
showError( R.string.error_nothingtodo );
274
// count the number of contacts and set the progress bar max
276
setProgressMax( num_contacts );
281
// loop through contacts
285
ContactData contact = new ContactData();
286
if( !contact_reader.getNextContact( contact ) )
291
if( exportContact( contact ) )
292
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTWRITTEN );
294
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
295
setProgress( count++ );
297
setProgress( num_contacts );
307
synchronized public void wake( int response )
309
_response = response;
313
synchronized public boolean setAbort()
315
if( !_is_finished && !_abort ) {
323
protected SharedPreferences getSharedPreferences()
325
return _doit.getSharedPreferences();
328
protected void showError( int res ) throws AbortExportException
330
showError( _doit.getText( res ).toString() );
333
synchronized protected void showError( String message )
334
throws AbortExportException
337
_doit._handler.sendMessage( Message.obtain(
338
_doit._handler, Doit.MESSAGE_ERROR, message ) );
342
catch( InterruptedException e ) { }
344
// no need to check if an abortion happened during the wait, we are
345
// about to finish anyway!
346
finish( ACTION_ABORT );
349
protected void showFatalError( int res ) throws AbortExportException
351
showFatalError( _doit.getText( res ).toString() );
354
synchronized protected void showFatalError( String message )
355
throws AbortExportException
358
_doit._handler.sendMessage( Message.obtain(
359
_doit._handler, Doit.MESSAGE_ERROR, message ) );
363
catch( InterruptedException e ) { }
365
// no need to check if an abortion happened during the wait, we are
366
// about to finish anyway!
367
finish( ACTION_ABORT );
370
protected boolean showContinue( int res ) throws AbortExportException
372
return showContinue( _doit.getText( res ).toString() );
375
synchronized protected boolean showContinue( String message )
376
throws AbortExportException
379
_doit._handler.sendMessage( Message.obtain(
380
_doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
384
catch( InterruptedException e ) { }
386
// check if an abortion happened during the wait
389
return _response == RESPONSE_POSITIVE;
392
protected void setProgressMessage( int res ) throws AbortExportException
395
_doit._handler.sendMessage( Message.obtain( _doit._handler,
396
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
399
protected void setProgressMax( int max_progress )
400
throws AbortExportException
403
_doit._handler.sendMessage( Message.obtain(
404
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
405
new Integer( max_progress ) ) );
408
protected void setTmpProgress( int tmp_progress )
409
throws AbortExportException
412
_doit._handler.sendMessage( Message.obtain(
413
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
414
new Integer( tmp_progress ) ) );
417
protected void setProgress( int progress ) throws AbortExportException
420
_doit._handler.sendMessage( Message.obtain(
421
_doit._handler, Doit.MESSAGE_SETPROGRESS,
422
new Integer( progress ) ) );
425
protected void finish( int action ) throws AbortExportException
427
// update UI to reflect action
431
case ACTION_ALLDONE: message = Doit.MESSAGE_ALLDONE; break;
432
default: // fall through
433
case ACTION_ABORT: message = Doit.MESSAGE_ABORT; break;
435
_doit._handler.sendEmptyMessage( message );
438
throw new AbortExportException();
441
protected CharSequence getText( int res )
443
return _doit.getText( res );
446
protected void skipContact() throws AbortExportException
449
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
452
synchronized protected void checkAbort() throws AbortExportException
456
throw new AbortExportException();
460
protected void preExport() throws AbortExportException
464
protected boolean exportContact( ContactData contact )
465
throws AbortExportException
467
throw new UnsupportedOperationException();
470
protected void postExport() throws AbortExportException