exception handler

This commit is contained in:
Daniel Gultsch 2014-03-09 13:21:28 +01:00
parent 05386b5052
commit baf0d64787
4 changed files with 63 additions and 0 deletions

View file

@ -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();

View file

@ -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());
}
}

View file

@ -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);
}
}

View file

@ -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));
}
}
}