Code cleanup for saving the Log to file

This commit is contained in:
Ampli-fier
2019-06-26 22:06:47 +02:00
parent a50d43d24f
commit 811ea4fd23
2 changed files with 13 additions and 26 deletions
@@ -64,15 +64,10 @@ public class LogViewActivity extends BaseActionBarActivity {
finish();
return true;
case R.id.save_log:
try {
if (logViewFragment.saveLogFile())
Toast.makeText(getApplicationContext(), R.string.pref_saved_log, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), R.string.pref_save_log_failed, Toast.LENGTH_LONG).show();
} catch (NoExternalStorageException e) {
e.printStackTrace();
if (logViewFragment.saveLogFile())
Toast.makeText(getApplicationContext(), R.string.pref_saved_log, Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), R.string.pref_save_log_failed, Toast.LENGTH_LONG).show();
}
return true;
case R.id.copy_log_to_clipboard:
Util.writeTextToClipboard(this, logViewFragment.getLogText());
@@ -102,7 +102,7 @@ public class LogViewFragment extends Fragment {
public void scrollUpLog() { logPreview.setSelection(0); }
public boolean saveLogFile() throws NoExternalStorageException {
public boolean saveLogFile() {
File outputDir = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
@@ -111,26 +111,18 @@ public class LogViewFragment extends Fragment {
try {
outputDir = StorageUtil.getDownloadDir();
try {
String logText = logPreview.getText().toString();
if(!logText.trim().equals("")){
File logFile = new File(outputDir + "/" + logFileName);
String logText = logPreview.getText().toString();
if(!logText.trim().equals("")){
File logFile = new File(outputDir + "/" + logFileName);
if(!logFile.exists()){
logFile.createNewFile();
}
if(!logFile.exists()) logFile.createNewFile();
FileWriter logFileWriter = new FileWriter(logFile, false);
BufferedWriter logFileBufferWriter = new BufferedWriter(logFileWriter);
logFileBufferWriter.write(logText);
logFileBufferWriter.close();
}
FileWriter logFileWriter = new FileWriter(logFile, false);
BufferedWriter logFileBufferWriter = new BufferedWriter(logFileWriter);
logFileBufferWriter.write(logText);
logFileBufferWriter.close();
}
catch (IOException e) {
e.printStackTrace();
return false;
}
} catch (NoExternalStorageException e) {
} catch (IOException | NoExternalStorageException e) {
e.printStackTrace();
return false;
}