Refactor CrashFragment.java

This commit is contained in:
SerpentSpirale 2022-02-25 23:28:55 +01:00 committed by ArtDev
parent 668b49eeb8
commit 9705b01bb3

View File

@ -2,7 +2,10 @@ package net.kdt.pojavlaunch.fragments;
import android.os.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.*;
import android.widget.*;
import java.io.*;
@ -11,80 +14,76 @@ import net.kdt.pojavlaunch.*;
import android.graphics.*;
import androidx.fragment.app.Fragment;
public class CrashFragment extends Fragment
{
public static String lastCrashFile = Tools.DIR_DATA + "/lastcrash.txt";
private String crashContent;
private TextView crashView;
public boolean resetCrashLog = false;
public static boolean isNewCrash(File crashLog) throws Exception {
String content = Tools.read(crashLog.getAbsolutePath());
return crashLog != null && content.startsWith("---- Minecraft Crash Report ----");
}
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.lmaintab_crashlog, container, false);
public class CrashFragment extends Fragment {
public static String LAST_CRASH_FILE = Tools.DIR_DATA + "/lastcrash.txt";
return view;
}
@Override
public void onActivityCreated(Bundle b)
{
super.onActivityCreated(b);
crashView = (TextView) getView().findViewById(R.id.lmaintabconsoleLogCrashTextView);
crashView.setTypeface(Typeface.MONOSPACE);
crashView.setHint(this.getText(R.string.main_nocrash));
private TextView mCrashView;
public boolean mResetCrashLog = false;
}
@Override
public void onResume()
{
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.lmaintab_crashlog, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCrashView = (TextView) getView().findViewById(R.id.lmaintabconsoleLogCrashTextView);
mCrashView.setTypeface(Typeface.MONOSPACE);
mCrashView.setHint(this.getText(R.string.main_nocrash));
}
@Override
public void onResume(){
super.onResume();
refreshCrashFile();
}
public void refreshCrashFile()
{
private void refreshCrashFile() {
// Default text
mCrashView.setText("");
setLastCrash("");
if(mResetCrashLog) return;
File crashLog = Tools.lastFileModified(Tools.DIR_HOME_CRASH);
String lastCrash = getLastCrash();
String crashContent;
try {
if(!resetCrashLog){
File crashLog = Tools.lastFileModified(Tools.DIR_HOME_CRASH);
String lastCrash = getLastCrash();
if (isNewCrash(crashLog)) {
crashContent = Tools.read(crashLog.getAbsolutePath());
Tools.write(crashLog.getAbsolutePath(), "\n" + crashContent);
setLastCrash(crashLog.getAbsolutePath());
crashView.setText(crashContent);
} else if(!lastCrash.isEmpty()) {
crashContent = Tools.read(lastCrash);
crashView.setText(crashContent);
} else throw new Exception();
} else throw new Exception();
} catch (Exception e) {
// Can't find crash or no NEW crashes
crashView.setText(""/*Log.getStackTraceString(e)*/);
setLastCrash("");
if (isNewCrash(crashLog)) {
crashContent = Tools.read(crashLog.getAbsolutePath());
Tools.write(crashLog.getAbsolutePath(), "\n" + crashContent);
setLastCrash(crashLog.getAbsolutePath());
mCrashView.setText(crashContent);
} else if(!lastCrash.isEmpty()) {
crashContent = Tools.read(lastCrash);
mCrashView.setText(crashContent);
}
}catch (IOException ioException){
Log.e("CrashFragment", ioException.toString());
}
}
public void setLastCrash(String newValue) {
private static boolean isNewCrash(File crashLog) throws IOException {
String content = Tools.read(crashLog.getAbsolutePath());
return crashLog != null && content.startsWith("---- Minecraft Crash Report ----");
}
private void setLastCrash(String newValue) {
try {
Tools.write(lastCrashFile, newValue);
} catch (Throwable th) {
throw new RuntimeException(th);
Tools.write(LAST_CRASH_FILE, newValue);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}
}
public String getLastCrash() {
private String getLastCrash() {
try {
return Tools.read(lastCrashFile);
} catch (Throwable th) {
return Tools.read(LAST_CRASH_FILE);
} catch (IOException ioException) {
return "";
}
}