BarInsta/app/src/main/java/awaisomereport/CrashReporter.java

192 lines
8.0 KiB
Java
Raw Normal View History

2020-07-01 17:08:28 +00:00
package awaisomereport;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Process;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
2021-03-19 01:20:15 +00:00
//import java.util.zip.ZipEntry;
//import java.util.zip.ZipOutputStream;
2020-07-01 17:08:28 +00:00
import awais.instagrabber.BuildConfig;
import awais.instagrabber.utils.Utils;
public final class CrashReporter implements Thread.UncaughtExceptionHandler {
private static CrashReporter reporterInstance;
private final Application application;
private final String email;
2021-03-19 01:20:15 +00:00
// private final File crashLogsZip;
2020-07-01 17:08:28 +00:00
private boolean startAttempted = false;
public static CrashReporter get(final Application application) {
if (reporterInstance == null) reporterInstance = new CrashReporter(application);
return reporterInstance;
}
private CrashReporter(@NonNull final Application application) {
this.application = application;
2020-11-07 21:10:18 +00:00
this.email = "barinsta@austinhuang.me";
2021-03-19 01:20:15 +00:00
// this.crashLogsZip = new File(application.getExternalCacheDir(), "crash_logs.zip");
2020-07-01 17:08:28 +00:00
}
public void start() {
if (!startAttempted) {
Thread.setDefaultUncaughtExceptionHandler(this);
startAttempted = true;
}
}
@Override
public void uncaughtException(@NonNull final Thread t, @NonNull final Throwable exception) {
final StringBuilder reportBuilder = new StringBuilder();
2020-11-17 15:01:42 +00:00
reportBuilder.append("IMPORTANT: If sending by email, your email address and the entire content will be made public on GitHub issues.");
2020-12-28 02:45:34 +00:00
reportBuilder.append("\r\nIMPORTANT: When possible, please describe the steps leading to this crash. Thank you for your cooperation.");
2020-11-17 15:01:42 +00:00
reportBuilder.append("\r\n\r\nError report collected on: ").append(new Date().toString());
2020-07-01 17:08:28 +00:00
reportBuilder
2020-11-17 15:01:42 +00:00
.append("\r\n\r\nInformation:\r\n==============")
2020-07-01 17:08:28 +00:00
.append("\r\nVERSION : ").append(BuildConfig.VERSION_NAME)
.append("\r\nVERSION_CODE : ").append(BuildConfig.VERSION_CODE)
.append("\r\nPHONE-MODEL : ").append(Build.MODEL)
.append("\r\nANDROID_VERS : ").append(Build.VERSION.RELEASE)
.append("\r\nANDROID_REL : ").append(Build.VERSION.SDK_INT)
.append("\r\nBRAND : ").append(Build.BRAND)
.append("\r\nMANUFACTURER : ").append(Build.MANUFACTURER)
.append("\r\nBOARD : ").append(Build.BOARD)
.append("\r\nDEVICE : ").append(Build.DEVICE)
.append("\r\nPRODUCT : ").append(Build.PRODUCT)
.append("\r\nHOST : ").append(Build.HOST)
2020-11-18 13:18:23 +00:00
.append("\r\nTAGS : ").append(Build.TAGS);
2020-07-01 17:08:28 +00:00
reportBuilder.append("\r\n\r\nStack:\r\n==============\r\n");
final Writer result = new StringWriter();
try (final PrintWriter printWriter = new PrintWriter(result)) {
exception.printStackTrace(printWriter);
reportBuilder.append(result.toString());
reportBuilder.append("\r\nCause:\r\n==============");
// for AsyncTask crashes
Throwable cause = exception.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
reportBuilder.append(result.toString());
cause = cause.getCause();
}
}
reportBuilder.append("\r\n\r\n**** End of current Report ***");
final String errorContent = reportBuilder.toString();
try (final FileOutputStream trace = application.openFileOutput("stack-" + System.currentTimeMillis() + ".stacktrace", Context.MODE_PRIVATE)) {
trace.write(errorContent.getBytes());
} catch (final Exception ex) {
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", ex);
}
application.startActivity(new Intent(application, ErrorReporterActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
2021-03-19 01:20:15 +00:00
// zipLogs();
2020-07-01 17:08:28 +00:00
Process.killProcess(Process.myPid());
System.exit(10);
}
2021-03-19 01:20:15 +00:00
// public synchronized CrashReporter zipLogs() {
// final File logDir = Utils.logCollector != null ? Utils.logCollector.getLogDir() :
// new File(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? application.getDataDir() : application.getFilesDir(), "crashlogs");
//
// try (final FileOutputStream fos = new FileOutputStream(crashLogsZip);
// final ZipOutputStream zos = new ZipOutputStream(fos)) {
//
// final File[] files = logDir.listFiles();
//
// if (files != null) {
// zos.setLevel(5);
// byte[] buffer;
// for (final File file : files) {
// if (file != null && file.length() > 0) {
// buffer = new byte[1024];
// try (final FileInputStream fis = new FileInputStream(file)) {
// zos.putNextEntry(new ZipEntry(file.getName()));
// int length;
// while ((length = fis.read(buffer)) > 0) zos.write(buffer, 0, length);
// zos.closeEntry();
// }
// }
// }
// }
//
// } catch (final Exception e) {
// if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
// }
//
// return this;
// }
2020-07-01 17:08:28 +00:00
@SuppressWarnings("ResultOfMethodCallIgnored")
2021-03-19 01:20:15 +00:00
public void startCrashEmailIntent(final Context context) {
2020-07-01 17:08:28 +00:00
try {
final String filePath = context.getFilesDir().getAbsolutePath();
String[] errorFileList;
2021-03-19 01:20:15 +00:00
try {
final File dir = new File(filePath);
if (dir.exists() && !dir.isDirectory()) dir.delete();
dir.mkdir();
errorFileList = dir.list((d, name) -> name.endsWith(".stacktrace"));
} catch (final Exception e) {
errorFileList = null;
2020-07-01 17:08:28 +00:00
}
2021-03-19 01:20:15 +00:00
if (errorFileList != null && errorFileList.length > 0) {
2020-07-01 17:08:28 +00:00
final StringBuilder errorStringBuilder;
2021-03-19 01:20:15 +00:00
errorStringBuilder = new StringBuilder("\r\n\r\n");
final int maxSendMail = 5;
2020-07-01 17:08:28 +00:00
2021-03-19 01:20:15 +00:00
int curIndex = 0;
for (final String curString : errorFileList) {
final File file = new File(filePath + '/' + curString);
if (curIndex++ <= maxSendMail) {
errorStringBuilder.append("New Trace collected:\r\n=====================\r\n");
try (final BufferedReader input = new BufferedReader(new FileReader(file))) {
String line;
while ((line = input.readLine()) != null)
errorStringBuilder.append(line).append("\r\n");
}
2020-07-01 17:08:28 +00:00
}
2021-03-19 01:20:15 +00:00
file.delete();
2020-07-01 17:08:28 +00:00
}
2021-03-19 01:20:15 +00:00
errorStringBuilder.append("\r\n\r\n");
2020-07-01 17:08:28 +00:00
context.startActivity(Intent.createChooser(new Intent(Intent.ACTION_SEND).setType("message/rfc822")
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
.putExtra(Intent.EXTRA_EMAIL, new String[]{email})
2021-03-19 01:20:15 +00:00
// .putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(application, BuildConfig.APPLICATION_ID + ".provider", crashLogsZip))
2020-09-17 14:30:04 +00:00
.putExtra(Intent.EXTRA_SUBJECT, "Barinsta Crash Report")
2020-07-01 17:08:28 +00:00
.putExtra(Intent.EXTRA_TEXT, errorStringBuilder.toString()), "Select an email app to send crash logs"));
}
} catch (final Exception e) {
if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
}
}
2020-07-25 22:05:48 +00:00
}