Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AndroidSDKCore/src/main/java/com/leanplum/Leanplum.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ protected Void doInBackground(Void... params) {
return null;
}
});

Util.initExceptionHandling(context);
} catch (Throwable t) {
Util.handleException(t);
}
Expand Down
10 changes: 10 additions & 0 deletions AndroidSDKCore/src/main/java/com/leanplum/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.leanplum.LeanplumException;
import com.leanplum.internal.Constants.Methods;
import com.leanplum.internal.Constants.Params;
import com.leanplum.monitoring.ExceptionHandler;
import com.leanplum.utils.SharedPreferencesUtil;

import org.json.JSONException;
Expand Down Expand Up @@ -837,10 +838,19 @@ private static void setUpdateTime(Map<String, Object> params, PackageManager pac
}
}

/**
* Initialize exception handling in the SDK.
*/
public static void initExceptionHandling(Context context) {
ExceptionHandler.getInstance().setContext(context);
}

/**
* Handles uncaught exceptions in the SDK.
*/
public static void handleException(Throwable t) {
ExceptionHandler.getInstance().reportException(t);

if (t instanceof OutOfMemoryError) {
if (Constants.isDevelopmentModeEnabled) {
throw (OutOfMemoryError) t;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.leanplum.monitoring;

import android.content.Context;

import com.leanplum.internal.Log;

public class ExceptionHandler {
private static final String LEANPLUM_CRASH_REPORTER_CLASS =
"com.leanplum.monitoring.internal.LeanplumExceptionReporter";
private static final ExceptionHandler instance = new ExceptionHandler();
public ExceptionReporting exceptionReporter = null;

private ExceptionHandler() {}

public static ExceptionHandler getInstance() {
return instance;
}

public void setContext(Context context) {
try {
// Class.forName runs the static initializer in LeanplumExceptionReporter
// which sets the exceptionReporter on the singleton
Class.forName(LEANPLUM_CRASH_REPORTER_CLASS);
if (exceptionReporter != null) {
try {
exceptionReporter.setContext(context);
} catch (Throwable t) {
Log.e("LeanplumCrashHandler", t);
}
}
} catch (Throwable t) {
Log.e("LeanplumCrashHandler", t);
}
}

public void reportException(Throwable exception) {
if (exceptionReporter != null) {
try {
exceptionReporter.reportException(exception);
} catch (Throwable t) {
Log.e("LeanplumCrashHandler", t);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.leanplum.monitoring;

import android.content.Context;

public interface ExceptionReporting {
void setContext(Context context);
void reportException(Throwable t);
}