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,8 +49,14 @@ public class ConfigEditorFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//Inflate the fragment layout
return inflater.inflate(R.layout.config_editing, container, false);
try {
//Inflate the fragment layout
return inflater.inflate(R.layout.config_editing, container, false);
}
catch (Exception e) {
MainActivity.writeToCrashLog(e);
return null;
}
}
/**
@ -61,22 +67,27 @@ public class ConfigEditorFragment extends Fragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{
//Find the Recycler View and set the hasFixedSize
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(false);
try {
//Find the Recycler View and set the hasFixedSize
mRecyclerView = view.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(false);
//Create a new Layout Manager and set Recyclers View layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
//Create a new Layout Manager and set Recyclers View layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
//Only grab the mod file list once
//Not having this allows duplicate mods to show (Thanks Minerva!!!)
if (!MainActivity.mHasFoundMods)
getModFiles();
//Only grab the mod file list once
//Not having this allows duplicate mods to show (Thanks Minerva!!!)
if (!MainActivity.mHasFoundMods)
getModFiles();
//Create a new Config Adapter and set Recycle Views adapter to the new adapter
mAdapter = new ConfigAdapter(mModFiles);
mRecyclerView.setAdapter(mAdapter);
//Create a new Config Adapter and set Recycle Views adapter to the new adapter
mAdapter = new ConfigAdapter(mModFiles);
mRecyclerView.setAdapter(mAdapter);
}
catch (Exception e) {
MainActivity.writeToCrashLog(e);
}
}
/**

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())
@ -78,7 +73,7 @@ public class MainActivity extends AppCompatActivity {
}
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
@ -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
*/