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

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
*/ */