From baf0d6478750bb59e3f883fb7f814a92f27b8a29 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 9 Mar 2014 13:21:28 +0100 Subject: exception handler --- .../services/XmppConnectionService.java | 2 ++ src/eu/siacs/conversations/ui/XmppActivity.java | 9 +++++ .../conversations/utils/ExceptionHandler.java | 41 ++++++++++++++++++++++ .../siacs/conversations/utils/ExceptionHelper.java | 11 ++++++ 4 files changed, 63 insertions(+) create mode 100644 src/eu/siacs/conversations/utils/ExceptionHandler.java create mode 100644 src/eu/siacs/conversations/utils/ExceptionHelper.java (limited to 'src/eu') diff --git a/src/eu/siacs/conversations/services/XmppConnectionService.java b/src/eu/siacs/conversations/services/XmppConnectionService.java index da43cb16..5adfa981 100644 --- a/src/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/eu/siacs/conversations/services/XmppConnectionService.java @@ -29,6 +29,7 @@ import eu.siacs.conversations.persistance.OnPhoneContactsMerged; import eu.siacs.conversations.ui.OnAccountListChangedListener; import eu.siacs.conversations.ui.OnConversationListChangedListener; import eu.siacs.conversations.ui.OnRosterFetchedListener; +import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.MessageParser; import eu.siacs.conversations.utils.OnPhoneContactsLoadedListener; import eu.siacs.conversations.utils.PhoneHelper; @@ -426,6 +427,7 @@ public class XmppConnectionService extends Service { @Override public void onCreate() { + ExceptionHelper.init(getApplicationContext()); databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); this.accounts = databaseBackend.getAccounts(); diff --git a/src/eu/siacs/conversations/ui/XmppActivity.java b/src/eu/siacs/conversations/ui/XmppActivity.java index 18df6f5b..2f1b1d50 100644 --- a/src/eu/siacs/conversations/ui/XmppActivity.java +++ b/src/eu/siacs/conversations/ui/XmppActivity.java @@ -3,6 +3,7 @@ package eu.siacs.conversations.ui; import eu.siacs.conversations.R; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder; +import eu.siacs.conversations.utils.ExceptionHelper; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; @@ -10,6 +11,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.os.Bundle; import android.os.IBinder; import android.view.MenuItem; import android.view.View; @@ -19,6 +21,7 @@ public abstract class XmppActivity extends Activity { public XmppConnectionService xmppConnectionService; public boolean xmppConnectionServiceBound = false; protected boolean handledViewIntent = false; + protected ServiceConnection mConnection = new ServiceConnection() { @Override @@ -93,4 +96,10 @@ public abstract class XmppActivity extends Activity { } return super.onOptionsItemSelected(item); } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ExceptionHelper.init(getApplicationContext()); + } } diff --git a/src/eu/siacs/conversations/utils/ExceptionHandler.java b/src/eu/siacs/conversations/utils/ExceptionHandler.java new file mode 100644 index 00000000..01cfebbb --- /dev/null +++ b/src/eu/siacs/conversations/utils/ExceptionHandler.java @@ -0,0 +1,41 @@ +package eu.siacs.conversations.utils; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.io.Writer; +import java.lang.Thread.UncaughtExceptionHandler; + +import android.content.Context; + +public class ExceptionHandler implements UncaughtExceptionHandler { + + private UncaughtExceptionHandler defaultHandler; + private Context context; + public ExceptionHandler(Context context) { + this.context = context; + this.defaultHandler = Thread.getDefaultUncaughtExceptionHandler(); + } + @Override + public void uncaughtException(Thread thread, Throwable ex) { + Writer result = new StringWriter(); + PrintWriter printWriter = new PrintWriter(result); + ex.printStackTrace(printWriter); + String stacktrace = result.toString(); + printWriter.close(); + try { + OutputStream os = context.openFileOutput("stacktrace.txt",Context.MODE_PRIVATE); + os.write(stacktrace.getBytes()); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + this.defaultHandler.uncaughtException(thread, ex); + } + +} diff --git a/src/eu/siacs/conversations/utils/ExceptionHelper.java b/src/eu/siacs/conversations/utils/ExceptionHelper.java new file mode 100644 index 00000000..def184d0 --- /dev/null +++ b/src/eu/siacs/conversations/utils/ExceptionHelper.java @@ -0,0 +1,11 @@ +package eu.siacs.conversations.utils; + +import android.content.Context; + +public class ExceptionHelper { + public static void init(Context context) { + if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) { + Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(context)); + } + } +} -- cgit v1.2.3