diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index d3e0eeb7..6b0fef1e 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -25,6 +25,7 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.res.Configuration; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; @@ -38,6 +39,9 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; import java.text.Collator; import java.util.Collections; import java.util.Comparator; @@ -45,6 +49,9 @@ import java.util.Date; import java.util.List; import java.util.Locale; +import javax.mail.Address; +import javax.mail.internet.InternetAddress; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBarDrawerToggle; @@ -60,20 +67,21 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack private ListView drawerList; private ActionBarDrawerToggle drawerToggle; - static final int LOADER_ACCOUNT_CHECK = 1; - static final int LOADER_ACCOUNT_PUT = 2; - static final int LOADER_IDENTITY_PUT = 3; - static final int LOADER_FOLDER_PUT = 4; - static final int LOADER_MESSAGE_ACCOUNT = 5; - static final int LOADER_MESSAGE_VIEW = 6; - static final int LOADER_MESSAGE_SEEN = 7; - static final int LOADER_MESSAGE_EDIT = 8; - static final int LOADER_MESSAGE_SPAM = 9; - static final int LOADER_MESSAGE_TRASH = 10; - static final int LOADER_MESSAGE_MOVE = 11; - static final int LOADER_MESSAGE_ARCHIVE = 12; - static final int LOADER_SEEN_UNTIL = 13; - static final int LOADER_DEBUG_INFO = 14; + static final int LOADER_EXCEPTION = 1; + static final int LOADER_ACCOUNT_CHECK = 2; + static final int LOADER_ACCOUNT_PUT = 3; + static final int LOADER_IDENTITY_PUT = 4; + static final int LOADER_FOLDER_PUT = 5; + static final int LOADER_MESSAGE_ACCOUNT = 6; + static final int LOADER_MESSAGE_VIEW = 7; + static final int LOADER_MESSAGE_SEEN = 8; + static final int LOADER_MESSAGE_EDIT = 9; + static final int LOADER_MESSAGE_SPAM = 10; + static final int LOADER_MESSAGE_TRASH = 11; + static final int LOADER_MESSAGE_MOVE = 12; + static final int LOADER_MESSAGE_ARCHIVE = 13; + static final int LOADER_SEEN_UNTIL = 14; + static final int LOADER_DEBUG_INFO = 15; static final int REQUEST_VIEW = 1; static final int REQUEST_UNSEEN = 2; @@ -173,6 +181,76 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack fragmentTransaction.commit(); } + new SimpleLoader() { + @Override + public Object onLoad(Bundle args) throws Throwable { + File file = new File(getCacheDir(), "crash.log"); + if (file.exists()) { + DB db = DB.getInstance(ActivityView.this); + EntityFolder drafts = db.folder().getPrimaryDrafts(); + if (drafts != null) { + Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); + + // Get version info + StringBuilder sb = new StringBuilder(); + sb.append(String.format("%s: %s/%d\r\n", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)); + sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT)); + sb.append("\r\n"); + + // Get device info + sb.append(String.format("Brand: %s\r\n", Build.BRAND)); + sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER)); + sb.append(String.format("Model: %s\r\n", Build.MODEL)); + sb.append(String.format("Product: %s\r\n", Build.PRODUCT)); + sb.append(String.format("Device: %s\r\n", Build.DEVICE)); + sb.append(String.format("Host: %s\r\n", Build.HOST)); + sb.append(String.format("Display: %s\r\n", Build.DISPLAY)); + sb.append(String.format("Id: %s\r\n", Build.ID)); + sb.append("\r\n"); + + BufferedReader in = null; + try { + String line; + in = new BufferedReader(new FileReader(file)); + while ((line = in.readLine()) != null) + sb.append(line); + } finally { + if (in != null) + in.close(); + } + + EntityMessage draft = new EntityMessage(); + draft.account = drafts.account; + draft.folder = drafts.id; + draft.to = new Address[]{to}; + draft.subject = getString(R.string.app_name) + " crash log"; + draft.body = "
" + sb.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); + + file.delete(); + + return draft.id; + } + } + + return null; + } + + @Override + public void onLoaded(Bundle args, Result result) { + if (result.ex == null && result.data != null) + startActivity( + new Intent(ActivityView.this, ActivityCompose.class) + .putExtra("action", "edit") + .putExtra("id", (Long) result.data)); + + } + }.load(this, LOADER_EXCEPTION, new Bundle()); + checkIntent(getIntent()); } diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index 69370c91..e7969475 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -20,13 +20,11 @@ package eu.faircode.email; */ import android.app.Application; -import android.os.Build; import android.util.Log; -import java.util.Date; - -import javax.mail.Address; -import javax.mail.internet.InternetAddress; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; public class ApplicationEx extends Application { private Thread.UncaughtExceptionHandler prev = null; @@ -40,53 +38,25 @@ public class ApplicationEx extends Application { Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread thread, Throwable ex) { - Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); - - DB db = null; - try { - db = DB.getBlockingInstance(ApplicationEx.this); - EntityFolder drafts = db.folder().getPrimaryDrafts(); - if (drafts != null) { - Address to = new InternetAddress("marcel+email@faircode.eu", "FairCode"); - - // Get version info - StringBuilder sb = new StringBuilder(); - sb.append(String.format("%s: %s/%d\r\n", BuildConfig.APPLICATION_ID, BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE)); - sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT)); - sb.append("\r\n"); - - // Get device info - sb.append(String.format("Brand: %s\r\n", Build.BRAND)); - sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER)); - sb.append(String.format("Model: %s\r\n", Build.MODEL)); - sb.append(String.format("Product: %s\r\n", Build.PRODUCT)); - sb.append(String.format("Device: %s\r\n", Build.DEVICE)); - sb.append(String.format("Host: %s\r\n", Build.HOST)); - sb.append(String.format("Display: %s\r\n", Build.DISPLAY)); - sb.append(String.format("Id: %s\r\n", Build.ID)); - sb.append("\r\n"); + Log.e(Helper.TAG, ex + "\r\n" + Log.getStackTraceString(ex)); - sb.append(ex.toString()).append("\r\n").append(Log.getStackTraceString(ex)); + File file = new File(getCacheDir(), "crash.log"); + Log.w(Helper.TAG, "Writing exception to " + file); - EntityMessage draft = new EntityMessage(); - draft.account = drafts.account; - draft.folder = drafts.id; - draft.to = new Address[]{to}; - draft.subject = BuildConfig.APPLICATION_ID + " crash info"; - draft.body = "
" + sb.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)); + 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(ex)); } finally { - if (db != null) - db.close(); + if (out != null) { + try { + out.close(); + } catch (IOException e) { + Log.e(Helper.TAG, e + "\n" + Log.getStackTraceString(ex)); + } + } } if (prev != null)