Save unexpected errors

This commit is contained in:
M66B 2018-12-01 10:58:19 +01:00
parent ce88d04a0f
commit dabde3d4aa
2 changed files with 21 additions and 9 deletions

View File

@ -23,6 +23,7 @@ import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.os.DeadSystemException;
import android.os.RemoteException;
@ -46,7 +47,7 @@ public class ApplicationEx extends Application {
public void uncaughtException(Thread thread, Throwable ex) {
if (ownFault(ex)) {
Log.e(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex));
writeCrashLog(ex);
writeCrashLog(ApplicationEx.this, ex);
if (prev != null)
prev.uncaughtException(thread, ex);
@ -112,8 +113,8 @@ public class ApplicationEx extends Application {
return false;
}
private void writeCrashLog(Throwable ex) {
File file = new File(getCacheDir(), "crash.log");
static void writeCrashLog(Context context, Throwable ex) {
File file = new File(context.getCacheDir(), "crash.log");
Log.w(Helper.TAG, "Writing exception to " + file);
FileWriter out = null;

View File

@ -171,12 +171,23 @@ public class Helper {
return sb.toString();
}
static void unexpectedError(Context context, Throwable ex) {
new AlertDialog.Builder(context)
.setTitle(R.string.title_unexpected_error)
.setMessage(ex.toString())
.setPositiveButton(android.R.string.cancel, null)
.show();
static void unexpectedError(final Context context, final Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
if (context != null) {
new AlertDialog.Builder(context)
.setTitle(R.string.title_unexpected_error)
.setMessage(ex.toString())
.setPositiveButton(android.R.string.cancel, null)
.show();
new Thread(new Runnable() {
@Override
public void run() {
ApplicationEx.writeCrashLog(context, ex);
}
}).start();
}
}
static String humanReadableByteCount(long bytes, boolean si) {