Refractor writing a crash log on installer crash.

This commit is contained in:
Chris 2020-02-17 00:24:22 -05:00
parent da22ea8197
commit 73993bf97f
2 changed files with 74 additions and 39 deletions

View File

@ -2,11 +2,11 @@ package com.MartyrPher.smapiandroidinstaller;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -49,9 +49,15 @@ public class ConfigEditorFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
try {
//Inflate the fragment layout
return inflater.inflate(R.layout.config_editing, container, false);
}
catch (Exception e) {
MainActivity.writeToCrashLog(e);
return null;
}
}
/**
* Override that gets called when the view is created, used to setup the Recycler View
@ -61,6 +67,7 @@ public class ConfigEditorFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
try {
//Find the Recycler View and set the hasFixedSize
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(false);
@ -78,6 +85,10 @@ public class ConfigEditorFragment extends Fragment {
mAdapter = new ConfigAdapter(mModFiles);
mRecyclerView.setAdapter(mAdapter);
}
catch (Exception e) {
MainActivity.writeToCrashLog(e);
}
}
/**
* Get the mod files using a recursive file check

View File

@ -3,18 +3,20 @@ package com.MartyrPher.smapiandroidinstaller;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import androidx.annotation.NonNull;
import com.google.android.material.tabs.TabLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
public class MainActivity extends AppCompatActivity {
@ -54,23 +56,16 @@ public class MainActivity extends AppCompatActivity {
}
catch (Exception e)
{
//Try to log a crash in onCreate()
File logFile = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/crash.txt");
try {
FileOutputStream stream = new FileOutputStream(logFile);
stream.write(e.getMessage().getBytes());
stream.close();
}
catch(Exception er)
{
Log.e(TAG, er.toString());
}
writeToCrashLog(e);
}
}
/**
* Sets up the view after the app has the needed permissions.
*/
private void setUpView()
{
File stardewValleyFolder = new File(Environment.getExternalStorageDirectory() + "/StardewValley");
File stardewValleyFolder = new File(Environment.getExternalStorageDirectory() + "/StardewValley/");
try
{
if (!stardewValleyFolder.exists())
@ -99,6 +94,35 @@ public class MainActivity extends AppCompatActivity {
mTabLayout.setupWithViewPager(mViewPager);
}
/**
* Writes to a text file if the app crashes.
* @param e = The exception to be written to the log.
*/
public static void writeToCrashLog(Exception e)
{
//Try to log a crash
File smapiInstallerFolder = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/");
File logFile = new File(Environment.getExternalStorageDirectory() + "/SMAPI Installer/crash.txt");
if (!mHasPermissions)
return;
if (!smapiInstallerFolder.exists())
smapiInstallerFolder.mkdir();
try {
FileOutputStream stream = new FileOutputStream(logFile);
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String stackTrace = stringWriter.toString();
stream.write(stackTrace.getBytes());
stream.close();
}
catch(Exception er) {
Log.e(TAG, er.toString());
}
}
/**
* Request permissions to be able to read/write external storage
*/