From bc9a26c2c7356ab37f26b3f07431966b724b26d3 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 3 Aug 2018 18:07:12 +0000 Subject: [PATCH] Store crash info as draft email --- .../java/eu/faircode/email/ApplicationEx.java | 51 +++++++++++++++++++ app/src/main/java/eu/faircode/email/DB.java | 4 ++ 2 files changed, 55 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 7cdce92e..e14213d5 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -20,6 +20,57 @@ package eu.faircode.email; */ import android.app.Application; +import android.util.Log; + +import java.util.Date; + +import javax.mail.Address; +import javax.mail.internet.InternetAddress; public class ApplicationEx extends Application { + private Thread.UncaughtExceptionHandler prev = null; + + @Override + public void onCreate() { + super.onCreate(); + + prev = Thread.getDefaultUncaughtExceptionHandler(); + + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread thread, Throwable ex) { + Log.w(Helper.TAG, "Handling crash"); + DB db = null; + try { + db = DB.getBlockingInstance(ApplicationEx.this); + EntityFolder drafts = db.folder().getPrimaryDraftFolder(); + if (drafts != null) { + Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); + + EntityMessage draft = new EntityMessage(); + draft.account = drafts.account; + draft.folder = drafts.id; + draft.to = MessageHelper.encodeAddresses(new Address[]{to}); + draft.subject = BuildConfig.APPLICATION_ID + " crash info"; + draft.body = "
" + ex.toString().replaceAll("\\r?\\n", "
") + "
"; + draft.received = new Date().getTime(); + draft.seen = false; + draft.ui_seen = false; + draft.ui_hide = false; + draft.id = db.message().insertMessage(draft); + + Log.w(Helper.TAG, "Crash info stored as draft"); + } + } catch (Throwable e1) { + Log.e(Helper.TAG, e1 + "\n" + Log.getStackTraceString(e1)); + } finally { + if (db != null) + db.close(); + } + + if (prev != null) + prev.uncaughtException(thread, ex); + } + }); + } } diff --git a/app/src/main/java/eu/faircode/email/DB.java b/app/src/main/java/eu/faircode/email/DB.java index 03e2155e..a61f1cf6 100644 --- a/app/src/main/java/eu/faircode/email/DB.java +++ b/app/src/main/java/eu/faircode/email/DB.java @@ -68,6 +68,10 @@ public abstract class DB extends RoomDatabase { return sInstance; } + public static DB getBlockingInstance(Context context) { + return migrate(Room.databaseBuilder(context.getApplicationContext(), DB.class, DB_NAME).allowMainThreadQueries()); + } + private static DB migrate(RoomDatabase.Builder builder) { return builder .addMigrations(MIGRATION_1_2)