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.Bundle;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull; import androidx.annotation.NonNull;
import android.support.annotation.Nullable; import androidx.annotation.Nullable;
import android.support.v4.app.Fragment; import androidx.fragment.app.Fragment;
import android.support.v7.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -49,9 +49,15 @@ public class ConfigEditorFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{ {
try {
//Inflate the fragment layout //Inflate the fragment layout
return inflater.inflate(R.layout.config_editing, container, false); 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 * 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 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{ {
try {
//Find the Recycler View and set the hasFixedSize //Find the Recycler View and set the hasFixedSize
mRecyclerView = view.findViewById(R.id.recycler_view); mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(false); mRecyclerView.setHasFixedSize(false);
@ -78,6 +85,10 @@ public class ConfigEditorFragment extends Fragment {
mAdapter = new ConfigAdapter(mModFiles); mAdapter = new ConfigAdapter(mModFiles);
mRecyclerView.setAdapter(mAdapter); mRecyclerView.setAdapter(mAdapter);
} }
catch (Exception e) {
MainActivity.writeToCrashLog(e);
}
}
/** /**
* Get the mod files using a recursive file check * Get the mod files using a recursive file check

View File

@ -3,18 +3,20 @@ package com.MartyrPher.smapiandroidinstaller;
import android.Manifest; import android.Manifest;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Environment; import android.os.Environment;
import android.support.annotation.NonNull; import androidx.annotation.NonNull;
import android.support.design.widget.TabLayout; import com.google.android.material.tabs.TabLayout;
import android.support.v4.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import android.support.v4.content.ContextCompat; import androidx.core.content.ContextCompat;
import android.support.v4.view.ViewPager; import androidx.viewpager.widget.ViewPager;
import android.support.v7.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@ -54,23 +56,16 @@ public class MainActivity extends AppCompatActivity {
} }
catch (Exception e) catch (Exception e)
{ {
//Try to log a crash in onCreate() writeToCrashLog(e);
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());
}
} }
} }
/**
* Sets up the view after the app has the needed permissions.
*/
private void setUpView() private void setUpView()
{ {
File stardewValleyFolder = new File(Environment.getExternalStorageDirectory() + "/StardewValley"); File stardewValleyFolder = new File(Environment.getExternalStorageDirectory() + "/StardewValley/");
try try
{ {
if (!stardewValleyFolder.exists()) if (!stardewValleyFolder.exists())
@ -78,7 +73,7 @@ public class MainActivity extends AppCompatActivity {
} }
catch(Exception e) catch(Exception e)
{ {
Toast.makeText(this, "Could not create StardewValley folder", Toast.LENGTH_LONG).show(); Toast.makeText(this, "Could not create Stardew Valley folder", Toast.LENGTH_LONG).show();
} }
//Find the ViewPager and TabLayout ids //Find the ViewPager and TabLayout ids
@ -99,6 +94,35 @@ public class MainActivity extends AppCompatActivity {
mTabLayout.setupWithViewPager(mViewPager); 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 * Request permissions to be able to read/write external storage
*/ */