1818
1919import android .annotation .SuppressLint ;
2020import android .app .Activity ;
21- import android .content .Context ;
2221import android .content .DialogInterface ;
2322import android .os .AsyncTask ;
2423import android .os .Environment ;
2524import android .support .v7 .app .AlertDialog ;
2625import android .util .Xml ;
27- import android .widget .Toast ;
2826
2927import com .djonique .birdays .R ;
3028import com .djonique .birdays .database .DbHelper ;
4442
4543public class ExportHelper {
4644
47- // TODO: 30.11.2017 Toast in main thread
48-
4945 // XML constants
5046 private static final String RECORDS = "records" ;
5147 private static final String PERSON = "person" ;
@@ -63,12 +59,12 @@ public class ExportHelper {
6359 private static final String IO_EXCEPTION = "IOException" ;
6460 private static final String FILE_NOT_FOUND_EXCEPTION = "FileNotFoundException" ;
6561
66- private Context context ;
62+ private Activity activity ;
6763 private File folder ;
6864 private boolean storageAvailable = true ;
6965
70- public ExportHelper (Context context ) {
71- this .context = context ;
66+ public ExportHelper (Activity activity ) {
67+ this .activity = activity ;
7268 }
7369
7470 public void exportRecords () {
@@ -121,36 +117,41 @@ private String writeXml(List<Person> persons) {
121117 xmlSerializer .endDocument ();
122118 xmlSerializer .flush ();
123119 } catch (IllegalArgumentException e ) {
124- Toast . makeText ( context , ILLEGAL_ARGUMENT_EXCEPTION , Toast . LENGTH_LONG ). show ( );
120+ showAlertDialog ( activity , ILLEGAL_ARGUMENT_EXCEPTION );
125121 } catch (IllegalStateException e ) {
126- Toast . makeText ( context , ILLEGAL_STATE_EXCEPTION , Toast . LENGTH_LONG ). show ( );
122+ showAlertDialog ( activity , ILLEGAL_STATE_EXCEPTION );
127123 } catch (IOException e ) {
128- Toast . makeText ( context , IO_EXCEPTION , Toast . LENGTH_LONG ). show ( );
124+ showAlertDialog ( activity , IO_EXCEPTION );
129125 }
130126 return stringWriter .toString ();
131127 }
132128
133- private void showAlertDialog (Context context , String text ) {
134- AlertDialog .Builder builder = new AlertDialog .Builder (context );
135- builder .setMessage (text );
136- builder .setPositiveButton (R .string .ok_button , new DialogInterface .OnClickListener () {
129+ private void showAlertDialog (final Activity activity , final String text ) {
130+ activity .runOnUiThread (new Runnable () {
137131 @ Override
138- public void onClick (DialogInterface dialog , int which ) {
139- dialog .cancel ();
132+ public void run () {
133+ AlertDialog .Builder builder = new AlertDialog .Builder (activity );
134+ builder .setMessage (text );
135+ builder .setPositiveButton (R .string .ok_button , new DialogInterface .OnClickListener () {
136+ @ Override
137+ public void onClick (DialogInterface dialog , int which ) {
138+ dialog .cancel ();
139+ }
140+ });
141+ builder .show ();
140142 }
141143 });
142- builder .show ();
143144 }
144145
145146 @ SuppressLint ("StaticFieldLeak" )
146147 private class ExportAsyncTask extends AsyncTask <Void , Void , Void > {
147148
148- ProgressDialogHelper progressDialogHelper = new ProgressDialogHelper (context );
149+ ProgressDialogHelper progressDialogHelper = new ProgressDialogHelper (activity );
149150
150151 @ Override
151152 protected void onPreExecute () {
152153 super .onPreExecute ();
153- progressDialogHelper .startProgressDialog (context .getString (R .string .exporting_records ));
154+ progressDialogHelper .startProgressDialog (activity .getString (R .string .exporting_records ));
154155 }
155156
156157 @ SuppressWarnings ("ResultOfMethodCallIgnored" )
@@ -159,29 +160,24 @@ protected Void doInBackground(Void... params) {
159160 File sd = Environment .getExternalStorageDirectory ();
160161 if (isExternalStorageWritable ()) {
161162 try {
162- folder = new File (sd .getPath () + File .separator + context .getString (R .string .app_name ));
163+ folder = new File (sd .getPath () + File .separator + activity .getString (R .string .app_name ));
163164 if (!folder .exists ()) {
164165 folder .mkdir ();
165166 }
166167 File backupFile = new File (folder + File .separator + getBackupFileName ());
167168 backupFile .createNewFile ();
168169 FileOutputStream outputStream = new FileOutputStream (backupFile );
169- List <Person > persons = new DbHelper (context ).query ().getPersons ();
170+ List <Person > persons = new DbHelper (activity ).query ().getPersons ();
170171 outputStream .write (writeXml (persons ).getBytes ());
171172 outputStream .close ();
172173 } catch (FileNotFoundException e ) {
173- Toast . makeText ( context , FILE_NOT_FOUND_EXCEPTION , Toast . LENGTH_LONG ). show ( );
174+ showAlertDialog ( activity , FILE_NOT_FOUND_EXCEPTION );
174175 } catch (IOException e ) {
175- Toast . makeText ( context , IO_EXCEPTION , Toast . LENGTH_LONG ). show ( );
176+ showAlertDialog ( activity , IO_EXCEPTION );
176177 }
177178 } else {
178179 storageAvailable = false ;
179- ((Activity ) context ).runOnUiThread (new Runnable () {
180- @ Override
181- public void run () {
182- showAlertDialog (context , context .getString (R .string .ext_storage_error ));
183- }
184- });
180+ showAlertDialog (activity , activity .getString (R .string .ext_storage_error ));
185181 }
186182 return null ;
187183 }
@@ -191,7 +187,7 @@ protected void onPostExecute(Void aVoid) {
191187 super .onPostExecute (aVoid );
192188 progressDialogHelper .dismissProgressDialog ();
193189 if (storageAvailable ) {
194- showAlertDialog (context , context .getString (R .string .backup_finished ) + folder );
190+ showAlertDialog (activity , activity .getString (R .string .backup_finished ) + folder );
195191 }
196192 }
197193 }
0 commit comments