From 63b4910f17c20aa8decc0d206ec72e97674a449b Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 14 Sep 2018 08:31:49 +0000 Subject: [PATCH] Limit crash logs, refactoring --- .../java/eu/faircode/email/ApplicationEx.java | 68 +++++++++++++------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 7ff6b9a9..2cbffa7c 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -42,34 +42,23 @@ public class ApplicationEx extends Application { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { - if (!(ex instanceof RemoteException) /* DeadSystemException */) { + if (ownFault(ex)) { Log.e(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex)); + writeCrashLog(ex); - File file = new File(getCacheDir(), "crash.log"); - Log.w(Helper.TAG, "Writing exception to " + file); - - FileWriter out = null; - try { - out = new FileWriter(file); - out.write(ex.toString() + "\n" + Log.getStackTraceString(ex)); - } catch (IOException e) { - Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e)); - } finally { - if (out != null) { - try { - out.close(); - } catch (IOException e) { - Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e)); - } - } - } + if (prev != null) + prev.uncaughtException(thread, ex); + } else { + Log.w(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex)); + System.exit(1); } - - if (prev != null) - prev.uncaughtException(thread, ex); } }); + createNotificationChannels(); + } + + private void createNotificationChannels() { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { NotificationManager nm = getSystemService(NotificationManager.class); @@ -93,4 +82,39 @@ public class ApplicationEx extends Application { nm.createNotificationChannel(error); } } + + public boolean ownFault(Throwable ex) { + if (ex instanceof OutOfMemoryError) + return false; + if (ex instanceof RemoteException) + return false; + while (ex != null) { + for (StackTraceElement ste : ex.getStackTrace()) + if (ste.getClassName().startsWith(getPackageName())) + return true; + ex = ex.getCause(); + } + return false; + } + + private void writeCrashLog(Throwable ex) { + File file = new File(getCacheDir(), "crash.log"); + Log.w(Helper.TAG, "Writing exception to " + file); + + FileWriter out = null; + try { + out = new FileWriter(file); + out.write(ex.toString() + "\n" + Log.getStackTraceString(ex)); + } catch (IOException e) { + Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e)); + } finally { + if (out != null) { + try { + out.close(); + } catch (IOException e) { + Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(e)); + } + } + } + } }