Progress displayed

This commit is contained in:
jens 2021-09-19 20:16:56 +02:00
parent 11f5f8905f
commit 19e85289d9
2 changed files with 32 additions and 15 deletions

View File

@ -48,6 +48,7 @@ public class MainActivity extends Activity
long messageCount = 0; long messageCount = 0;
enum EnumResult { success, errorReadingMessages, noMessages, errorWritingFile };
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) protected void onCreate(@Nullable Bundle savedInstanceState)
@ -92,7 +93,7 @@ public class MainActivity extends Activity
} }
} }
class AsyncTaskExport extends AsyncTask<Uri, Integer, Boolean> class AsyncTaskExport extends AsyncTask<Uri, Integer, EnumResult>
{ {
String getExportString() String getExportString()
{ {
@ -168,7 +169,7 @@ public class MainActivity extends Activity
} }
@Override @Override
protected Boolean doInBackground(Uri... uriTree) protected EnumResult doInBackground(Uri... uriTree)
{ {
int selectedMessageType; int selectedMessageType;
if(rbRcs.isChecked()) if(rbRcs.isChecked())
@ -185,10 +186,11 @@ public class MainActivity extends Activity
{ {
if (Looper.myLooper() == null) if (Looper.myLooper() == null)
Looper.prepare(); Looper.prepare();
Toast.makeText(MainActivity.this, "Error reading messages. Can\'t export.", Toast.LENGTH_SHORT).show();
}
return false; Toast.makeText(MainActivity.this, "Error reading messages. Can\'t export.", Toast.LENGTH_SHORT).show();
return EnumResult.noMessages;
}
} }
@Override @Override
@ -199,14 +201,25 @@ public class MainActivity extends Activity
} }
@Override @Override
protected void onPostExecute(Boolean result) protected void onPostExecute(EnumResult result)
{ {
super.onPostExecute(result); super.onPostExecute(result);
if(result) switch(result)
{
case success:
tvStatus.setText(String.format(getResources().getString(R.string.exported), String.valueOf(messageCount))); tvStatus.setText(String.format(getResources().getString(R.string.exported), String.valueOf(messageCount)));
else break;
tvStatus.setText(getResources().getString(R.string.errorExporting)); case errorReadingMessages:
tvStatus.setText(getResources().getString(R.string.errorReadingMessages));
break;
case noMessages:
tvStatus.setText(getResources().getString(R.string.noMessages));
break;
case errorWritingFile:
tvStatus.setText(getResources().getString(R.string.errorWritingFile));
break;
}
} }
} }
@ -220,7 +233,7 @@ public class MainActivity extends Activity
switch (requestCode) switch (requestCode)
{ {
case requestCodeExport: case requestCodeExport:
AsyncTask<Uri,Integer,Boolean> exportTask = new AsyncTaskExport(); AsyncTask<Uri,Integer,EnumResult> exportTask = new AsyncTaskExport();
exportTask.execute(data.getData()); exportTask.execute(data.getData());
break; break;
case requestCodePermissionRequest: case requestCodePermissionRequest:
@ -230,7 +243,7 @@ public class MainActivity extends Activity
} }
} }
boolean exportFiles(Uri uriTree, int messageType, String content) EnumResult exportFiles(Uri uriTree, int messageType, String content)
{ {
String messageTypeString; String messageTypeString;
if(messageType == typeRCS) if(messageType == typeRCS)
@ -269,17 +282,18 @@ public class MainActivity extends Activity
OutputStream out = getApplicationContext().getContentResolver().openOutputStream(exportFile.getUri()); OutputStream out = getApplicationContext().getContentResolver().openOutputStream(exportFile.getUri());
out.write(content.getBytes()); out.write(content.getBytes());
out.close(); out.close();
return true; return EnumResult.success;
} }
catch (IOException e) catch (IOException e)
{ {
e.printStackTrace(); e.printStackTrace();
return EnumResult.errorWritingFile;
} }
} }
else
return EnumResult.errorWritingFile;
// else // else
// Toast.makeText(MainActivity.this, getResources().getString(R.string.ConfigurationExportError), Toast.LENGTH_LONG).show(); // Toast.makeText(MainActivity.this, getResources().getString(R.string.ConfigurationExportError), Toast.LENGTH_LONG).show();
return false;
} }
List<Map<String,String>> readMessages(int messageType) List<Map<String,String>> readMessages(int messageType)

View File

@ -13,4 +13,7 @@
<string name="exporting">Exporting %1$s messages.</string> <string name="exporting">Exporting %1$s messages.</string>
<string name="exported">%1$s messages have been exported.</string> <string name="exported">%1$s messages have been exported.</string>
<string name="errorExporting">An error occured while exporting your messages.</string> <string name="errorExporting">An error occured while exporting your messages.</string>
<string name="errorReadingMessages">Error reading messages.</string>
<string name="noMessages">There are no messages of the selected type. Nothing exported.</string>
<string name="errorWritingFile">File could not be written.</string>
</resources> </resources>