4
* Copyright (C) 2011 Tim Marston <edam@waxworlds.org>
6
* This file is part of the Export Contacts program (hereafter referred
7
* to as "this program"). For more information, see
8
* http://www.waxworlds.org/edam/software/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 org.waxworlds.edam.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
ContactReader contact_reader = new ContactsContactReader( _doit, this );
269
int num_contacts = contact_reader.getNumContacts();
270
if( num_contacts == 0 )
271
showError( R.string.error_nothingtodo );
273
// count the number of contacts and set the progress bar max
275
setProgressMax( num_contacts );
280
// loop through contacts
284
ContactData contact = new ContactData();
285
if( !contact_reader.getNextContact( contact ) )
290
if( exportContact( contact ) )
291
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTWRITTEN );
293
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
294
setProgress( count++ );
296
setProgress( num_contacts );
306
synchronized public void wake( int response )
308
_response = response;
312
synchronized public boolean setAbort()
314
if( !_is_finished && !_abort ) {
322
protected SharedPreferences getSharedPreferences()
324
return _doit.getSharedPreferences();
327
protected void showError( int res ) throws AbortExportException
329
showError( _doit.getText( res ).toString() );
332
synchronized protected void showError( String message )
333
throws AbortExportException
336
_doit._handler.sendMessage( Message.obtain(
337
_doit._handler, Doit.MESSAGE_ERROR, message ) );
341
catch( InterruptedException e ) { }
343
// no need to check if an abortion happened during the wait, we are
344
// about to finish anyway!
345
finish( ACTION_ABORT );
348
protected void showFatalError( int res ) throws AbortExportException
350
showFatalError( _doit.getText( res ).toString() );
353
synchronized protected void showFatalError( String message )
354
throws AbortExportException
357
_doit._handler.sendMessage( Message.obtain(
358
_doit._handler, Doit.MESSAGE_ERROR, message ) );
362
catch( InterruptedException e ) { }
364
// no need to check if an abortion happened during the wait, we are
365
// about to finish anyway!
366
finish( ACTION_ABORT );
369
protected boolean showContinue( int res ) throws AbortExportException
371
return showContinue( _doit.getText( res ).toString() );
374
synchronized protected boolean showContinue( String message )
375
throws AbortExportException
378
_doit._handler.sendMessage( Message.obtain(
379
_doit._handler, Doit.MESSAGE_CONTINUEORABORT, message ) );
383
catch( InterruptedException e ) { }
385
// check if an abortion happened during the wait
388
return _response == RESPONSE_POSITIVE;
391
protected void setProgressMessage( int res ) throws AbortExportException
394
_doit._handler.sendMessage( Message.obtain( _doit._handler,
395
Doit.MESSAGE_SETPROGRESSMESSAGE, getText( res ) ) );
398
protected void setProgressMax( int max_progress )
399
throws AbortExportException
402
_doit._handler.sendMessage( Message.obtain(
403
_doit._handler, Doit.MESSAGE_SETMAXPROGRESS,
404
new Integer( max_progress ) ) );
407
protected void setTmpProgress( int tmp_progress )
408
throws AbortExportException
411
_doit._handler.sendMessage( Message.obtain(
412
_doit._handler, Doit.MESSAGE_SETTMPPROGRESS,
413
new Integer( tmp_progress ) ) );
416
protected void setProgress( int progress ) throws AbortExportException
419
_doit._handler.sendMessage( Message.obtain(
420
_doit._handler, Doit.MESSAGE_SETPROGRESS,
421
new Integer( progress ) ) );
424
protected void finish( int action ) throws AbortExportException
426
// update UI to reflect action
430
case ACTION_ALLDONE: message = Doit.MESSAGE_ALLDONE; break;
431
default: // fall through
432
case ACTION_ABORT: message = Doit.MESSAGE_ABORT; break;
434
_doit._handler.sendEmptyMessage( message );
437
throw new AbortExportException();
440
protected CharSequence getText( int res )
442
return _doit.getText( res );
445
protected void skipContact() throws AbortExportException
448
_doit._handler.sendEmptyMessage( Doit.MESSAGE_CONTACTSKIPPED );
451
synchronized protected void checkAbort() throws AbortExportException
455
throw new AbortExportException();
459
protected void preExport() throws AbortExportException
463
protected boolean exportContact( ContactData contact )
464
throws AbortExportException
466
throw new UnsupportedOperationException();
469
protected void postExport() throws AbortExportException